$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 #ifdef VTD_API_2_2_0
40  #include <scpIcd.h>
41 #else
42  #include <VtdToolkit/scpIcd.h>
43 #endif
44 #include <cloe/core.hpp> // for Json, Error
45 #include <cloe/utility/tcp_transceiver.hpp> // for TcpTransceiver
46 
47 #include "vtd_logger.hpp" // for scp_logger
48 
49 namespace vtd {
50 
56 class ScpMessage {
57  public:
58  virtual ~ScpMessage() = default;
59  virtual std::string to_scp() const = 0;
60 };
61 
69 class ScpError : public cloe::Error {
70  public:
71  using Error::Error;
72  virtual ~ScpError() noexcept = default;
73 };
74 
79  public:
80  using TcpTransceiver::TcpTransceiver;
81  ~ScpTransceiver() = default;
82 
83  void send(const ScpMessage& msg) { write(msg.to_scp()); }
84  void send(const std::string& msg) { write(msg.c_str()); }
85  void send(const char* msg) { write(msg); }
86 
87  // unstable API:
88  bool has() const {
89  return this->tcp_available_data() >= static_cast<std::streamsize>(sizeof(SCP_MSG_HDR_t));
90  }
91 
92  std::string receive();
93 
94  friend void to_json(cloe::Json j, const ScpTransceiver& t) {
95  j = cloe::Json{
96  {"connection_endpoint", t.tcp_endpoint()},
97  {"connection_ok", t.tcp_is_ok()},
98  {"num_errors", t.num_errors_},
99  {"num_messages_sent", t.num_sent_},
100  {"num_messages_received", t.num_received_},
101  };
102  }
103 
104  protected:
105  void write(const std::string& msg) { write(msg.c_str()); }
106  void write(const char* msg);
107 
108  private:
109  uint64_t num_errors_{0};
110  uint64_t num_sent_{0};
111  uint64_t num_received_{0};
112 };
113 
115  public:
116  using TcpTransceiverFactory::TcpTransceiverFactory;
117  ~ScpTransceiverFactory() = default;
118 
119  protected:
120  cloe::Logger factory_logger() const override { return scp_logger(); }
121  const char* instance_name() const override { return "ScpTransceiver"; }
122 };
123 
124 } // namespace vtd
Definition: scp_transceiver.hpp:56
Definition: actuator_component.hpp:35
Definition: error.hpp:35
Definition: scp_transceiver.hpp:78
Definition: scp_transceiver.hpp:114
Definition: tcp_transceiver.hpp:61
Definition: scp_transceiver.hpp:69
Definition: tcp_transceiver.hpp:151
bool tcp_is_ok() const
Definition: tcp_transceiver.hpp:94