6 #ifndef MYOLINUX_BLED112CLIENT_H 7 #define MYOLINUX_BLED112CLIENT_H 11 #include "firstargument.h" 18 namespace MYOLINUX_NAMESPACE {
31 void write(
const T &);
34 void write(
const T &,
const Buffer &);
42 template <
typename... Functions>
43 void read(
const Functions&...);
46 template <
typename Function>
47 using DisableIfFirstArgumentIsPartial = std::enable_if<!Partial<typename FirstArgument<Function>::type>::value>;
49 template <
typename Function>
50 using EnableIfFirstArgumentIsPartial = std::enable_if<Partial<typename FirstArgument<Function>::type>::value>;
55 void checkHeader(
const Header &);
58 T readPayload(
const Header &);
61 T readPayload(
const Header &,
Buffer &);
63 void dispatch(
const Header &);
65 template <
typename Function,
typename... Functions>
66 auto dispatch(
const Header &,
const Function &,
const Functions&...)
67 ->
typename DisableIfFirstArgumentIsPartial<Function>::type;
69 template <
typename Function,
typename... Functions>
70 auto dispatch(
const Header &,
const Function &,
const Functions&...)
71 ->
typename EnableIfFirstArgumentIsPartial<Function>::type;
79 void Client::write(
const T &payload)
82 socket.write(
pack(payload));
89 void Client::write(
const T &payload,
const Buffer &leftover)
91 socket.write(
pack(getHeader<T>(leftover.size())));
92 socket.write(
pack(payload));
93 socket.write(leftover);
96 inline Header Client::readHeader()
98 return unpack<Header>(socket.read(
sizeof(Header)));
101 template <
typename T>
102 void Client::checkHeader(
const Header &header)
104 if (header.cls != T::cls) {
105 throw std::runtime_error(
"Class index does not match the expected value.");
107 if (header.cmd != T::cmd) {
108 throw std::runtime_error(
"Command index does not match the expected value.");
110 if (!Partial<T>::value && header.length() !=
sizeof(T)) {
111 throw std::runtime_error(
"Payload size does not match the expected value.");
115 template <
typename T>
116 T Client::readPayload(
const Header &header)
118 return unpack<T>(socket.read(header.length()));
121 template <
typename T>
122 T Client::readPayload(
const Header &header,
Buffer &leftover)
124 const auto payload = unpack<T>(socket.read(
sizeof(T)));
125 leftover = socket.read(header.length() -
sizeof(T));
131 template <
typename T>
134 const auto header = readHeader();
135 checkHeader<T>(header);
136 return readPayload<T>(header);
142 template <
typename T>
145 const auto header = readHeader();
146 checkHeader<T>(header);
147 return readPayload<T>(header, leftover);
150 inline void Client::dispatch(
const Header &)
153 template <
typename Function,
typename... Functions>
154 auto Client::dispatch(
const Header &header,
const Function &
function,
const Functions&... functions)
155 ->
typename DisableIfFirstArgumentIsPartial<Function>::type
157 using T =
typename FirstArgument<Function>::type;
159 if (header.cls == T::cls && header.cmd == T::cmd && header.length() ==
sizeof(T)) {
160 function(readPayload<T>(header));
163 dispatch(header, functions...);
166 template <
typename Function,
typename... Functions>
167 auto Client::dispatch(
const Header &header,
const Function &
function,
const Functions&... functions)
168 ->
typename EnableIfFirstArgumentIsPartial<Function>::type
170 using T =
typename FirstArgument<Function>::type;
172 if (header.cls == T::cls && header.cmd == T::cmd) {
174 const auto payload = readPayload<T>(header, leftover);
176 function(std::move(payload), std::move(leftover));
179 dispatch(header, functions...);
191 template <
typename... Functions>
192 void Client::read(
const Functions&... functions)
194 const auto header = readHeader();
195 dispatch(header, functions...);
201 #endif // MYOLINUX_BLED112CLIENT_H Class for communication using the BlueGiga protocol.
Definition: bled112client.h:24
std::vector< unsigned char > Buffer
Buffer used for packing and unpacking packets.
Definition: buffer.h:16
Buffer pack(const T &payload)
Pack payload.
Definition: buffer.h:20
std::size_t write(const Buffer &)
Read from serial port.
Definition: serial.cpp:86
Class for communication over the serial port.
Definition: serial.h:19