$darkmode
scp_transceiver.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
36 #pragma once
37 
38 #include <string> // for string
39 
40 #include <cloe/core.hpp> // for Json, Error
41 #include <cloe/utility/tcp_transceiver.hpp> // for TcpTransceiver
42 
43 #include "vtd_version.hpp"
44 #if (VTD_API_VERSION_EPOCH == 0)
45  #include <scpIcd.h>
46 #else
47  #include <VtdToolkit/scpIcd.h>
48 #endif
49 
50 #include "vtd_logger.hpp" // for scp_logger
51 
52 namespace vtd {
53 
59 class ScpMessage {
60  public:
61  virtual ~ScpMessage() = default;
62  virtual std::string to_scp() const = 0;
63 };
64 
72 class ScpError : public cloe::Error {
73  public:
74  using Error::Error;
75  virtual ~ScpError() noexcept = default;
76 };
77 
82  public:
83  using TcpTransceiver::TcpTransceiver;
84  ~ScpTransceiver() = default;
85 
86  void send(const ScpMessage& msg) { write(msg.to_scp()); }
87  void send(const std::string& msg) { write(msg.c_str()); }
88  void send(const char* msg) { write(msg); }
89 
90  // unstable API:
91  bool has() const {
92  return this->tcp_available_data() >= static_cast<std::streamsize>(sizeof(SCP_MSG_HDR_t));
93  }
94 
95  std::string receive();
96 
97  friend void to_json(cloe::Json j, const ScpTransceiver& t) {
98  j = cloe::Json{
99  {"connection_endpoint", t.tcp_endpoint()},
100  {"connection_ok", t.tcp_is_ok()},
101  {"num_errors", t.num_errors_},
102  {"num_messages_sent", t.num_sent_},
103  {"num_messages_received", t.num_received_},
104  };
105  }
106 
107  protected:
108  void write(const std::string& msg) { write(msg.c_str()); }
109  void write(const char* msg);
110 
111  private:
112  uint64_t num_errors_{0};
113  uint64_t num_sent_{0};
114  uint64_t num_received_{0};
115 };
116 
118  public:
119  using TcpTransceiverFactory::TcpTransceiverFactory;
120  ~ScpTransceiverFactory() = default;
121 
122  protected:
123  cloe::Logger factory_logger() const override { return scp_logger(); }
124  const char* instance_name() const override { return "ScpTransceiver"; }
125 };
126 
127 } // namespace vtd
Definition: error.hpp:35
Definition: tcp_transceiver.hpp:149
Definition: tcp_transceiver.hpp:59
bool tcp_is_ok() const
Definition: tcp_transceiver.hpp:92
std::streamsize tcp_available_data() const
Definition: tcp_transceiver.hpp:112
Definition: scp_transceiver.hpp:72
Definition: scp_transceiver.hpp:59
Definition: scp_transceiver.hpp:117
Definition: scp_transceiver.hpp:81