43 #define NONE_MSG 0xFFFF 44 #define HELLO_MSG 0x0101 45 #define DATA_MSG 0x0201 50 #define CHECK_STATUS() \ 53 print_error(uv_last_error(uv_default_loop())); \ 99 uint16_t start = offset;
101 uint16_t temp = (uint16_t) (
sizeof(uint16_t) +
data.size());
103 offset += array.
write(offset, temp);
107 std::copy(
data.begin(),
data.end(), array.begin() + offset);
109 return offset - start;
117 uint16_t start = offset;
127 auto begin = array.begin();
131 auto end = begin + data_size;
133 std::copy(begin, end,
data.begin());
135 return offset - start;
155 static constexpr uint16_t
min_size = 3 *
sizeof(uint8_t);
168 uint16_t start = offset;
174 offset +=
uid.
pack(array, offset);
176 return offset - start;
184 uint16_t start = offset;
192 return offset - start;
215 return uv_buf_init((
char *) calloc(1, suggested_size), suggested_size);
229 LOG(ERROR) << uv_err_name((uv_err_t &) status) <<
" - " 230 << uv_strerror((uv_err_t &) status) <<
NL;
244 static void on_close(uv_handle_t *handle)
246 LOG(DEBUG) <<
"Connection Closed!" <<
NL;
262 static void on_read(uv_stream_t *stream, ssize_t nread, uv_buf_t buf)
268 LOG(TRACE) << __PRETTY_FUNCTION__ <<
" : " << stream <<
" : " << link <<
NL;
272 LOG(DEBUG) <<
"Could not read from stream !" <<
NL;
273 LOG(DEBUG) <<
"Stream closed !" <<
NL;
277 uv_close((uv_handle_t *) stream, on_close);
283 LOG(TRACE) <<
"RECV : " << payload <<
NL;
289 LOG(TRACE) <<
"Primitive : " << std::hex << std::setw(4) << std::setfill(
'0')
291 LOG(TRACE) <<
"Data : " << msg.
data <<
NL;
293 handle_message(link, msg);
309 static void on_write(uv_write_t *req,
int status)
313 LOG(TRACE) << __PRETTY_FUNCTION__ <<
NL;
315 assert(req->type == UV_WRITE);
331 static void send_message(uv_stream_t *stream,
msg_t &msg)
336 LOG(TRACE) <<
"SEND : " << payload <<
NL;
338 uv_write_t *req = (uv_write_t *) calloc(1,
sizeof(uv_write_t));
339 uv_buf_t buf = uv_buf_init((
char *) payload.data(), payload.size());
341 uv_write(req, (uv_stream_t *) stream, &buf, 1 , on_write);
366 send_message(stream, msg);
431 static void on_connect(uv_stream_t *server,
int status)
435 LOG(TRACE) << __PRETTY_FUNCTION__ <<
NL;
437 uv_tcp_t *client = (uv_tcp_t *) calloc(1,
sizeof(uv_tcp_t));
439 client->data =
nullptr;
441 uv_tcp_init(server->loop, client);
443 if (uv_accept(server, (uv_stream_t *) client))
445 LOG(ERROR) <<
"Could not accept the connection !" <<
NL;
446 uv_close((uv_handle_t *) client, on_close);
450 LOG(INFO) <<
"Connection accepted !" <<
NL;
457 uv_read_start((uv_stream_t *) client, (uv_alloc_cb)
alloc_buffer, on_read);
459 send_hello((uv_stream_t *) client, tsp);
476 uv_loop_t *loop = uv_default_loop();
478 uv_tcp_init(loop, &
socket);
482 LOG(TRACE) << __PRETTY_FUNCTION__ <<
NL;
484 struct sockaddr_in bind_addr = uv_ip4_addr(
"127.0.0.1", 8000);
486 if (uv_tcp_bind(&
socket, bind_addr) != 0)
492 if (uv_listen((uv_stream_t *) &
socket, 128, on_connect) != 0)
510 static void on_connect(uv_connect_t *conn,
int status)
514 LOG(TRACE) << __PRETTY_FUNCTION__ <<
NL;
516 uv_stream_t *stream = conn->handle;
518 uv_read_start(stream, (uv_alloc_cb)
alloc_buffer, on_read);
520 if (uv_is_writable(stream) && uv_is_readable(stream))
522 LOG(INFO) <<
"Connected !" <<
NL;
529 send_hello(stream, tsp);
533 LOG(ERROR) <<
"Cannot read/write to stream !" <<
NL;
534 uv_close((uv_handle_t *) conn->handle, NULL);
553 LOG(TRACE) << __PRETTY_FUNCTION__ <<
NL;
555 uv_tcp_init(uv_default_loop(), &socket);
556 uv_tcp_keepalive(&socket, 1, 60);
560 uv_connect_t *connect = (uv_connect_t *) calloc(1,
sizeof(uv_connect_t));
562 struct sockaddr_in dest = uv_ip4_addr(
"127.0.0.1", 8000);
564 uv_tcp_connect(connect, &socket, dest, on_connect);
578 LOG(TRACE) << __PRETTY_FUNCTION__ <<
NL;
590 LOG(TRACE) << __PRETTY_FUNCTION__ <<
NL;
596 send_message((uv_stream_t *) stream, msg);
615 LOG(WARN) << __PRETTY_FUNCTION__ <<
": Not implemented !!" <<
NL;
633 LOG(WARN) << __PRETTY_FUNCTION__ <<
": Not implemented !!" <<
NL;
651 LOG(WARN) << __PRETTY_FUNCTION__ <<
": Not implemented !!" <<
NL;
666 LOG(WARN) << __PRETTY_FUNCTION__ <<
": Not implemented !!" <<
NL;
uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size)
This function is used to allocate the buffers used with libuv.
uint16_t write(uint16_t offset, uint8_t data)
Write a byte into the array at the given offset.
void receive(HF::Transport::Link *link, HF::Common::ByteArray &payload)
Receive the data in payload, coming in in the given link and deliver the decoded packet to all regist...
uint8_t core
HAN-FUN Core Services & Interfaces version.
constexpr uint8_t CORE_VERSION
Core Service & Interfaces major version supported.
This is the top level include file for the HAN-FUN library.
uint16_t unpack(HF::Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Common::Result create(Endpoint &ep, uint16_t group)
Create a new group of devices at the transport layer with the given address.
Transport Layer implementation over TCP/IP using libuv.
This file contains the definitions for the HAN-FUN example applications.
#define CHECK_STATUS()
Helper macro the check libuv command status.
constexpr uint8_t PROFILES_VERSION
Profiles major version supported.
msg_t(uint16_t primitive, HF::Common::ByteArray &data)
Constructor.
virtual T * clone() const =0
Create a clone object of the object where this method is being called.
uint16_t size() const
Number bytes needed to serialize the message.
uint16_t size() const
Number bytes needed to serialize the message.
UID_T const * raw() const
Get the underling wrapped UID_T pointer.
uint16_t pack(Common::ByteArray &array, uint16_t offset) const
Write the object on to a ByteArray so it can be sent over the network.
This file contains the declarations of the transport layer over libuv.
#define NONE_MSG
No message primitive id.
void destroy()
Free the system resources associated with this transport layer.
#define HF_SERIALIZABLE_CHECK(__array, __offset, __size)
Helper macro to check if the given __array has enough size so __size bytes can be written/read from t...
uint8_t profiles
HAN-FUN Profiles version.
uv_tcp_t socket
libuv tcp/ip socket.
uint16_t pack(HF::Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
uint16_t unpack(HF::Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
uint16_t primitive
Message type.
uint8_t interfaces
HAN-FUN Interfaces version.
constexpr uint8_t INTERFACES_VERSION
Interfaces major version supported.
Common::Result add(Endpoint &ep, uint16_t group, uint16_t device)
Add the given device to the given group for the given end-point ep.
void print_error(uv_err_t status)
Print a user friendly string for given status code.
uint16_t nbytes
Number of bytes in the message.
Message sent between the concentrator and the nodes.
Wrapper around UID_T pointer's.
This class represents a byte array.
#define NL
Helper define for new-line and stream clear.
const HF::UID::UID uid() const
Return the UID of the local device on this transport layer.
#define DATA_MSG
Data message primitive id.
HF::Common::ByteArray data
Message payload.
void add(HF::Transport::Endpoint *ep)
Register the given Endpoint to receive events.
uint16_t read(uint16_t offset, uint8_t &data) const
Read the byte at offset into data.
void send(HF::Common::ByteArray &array)
Send the data in the given ByteArray using the link to the remote end-point.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.
void initialize()
Initialize the associated transport layer.
This file contains the definitions for the common functionality in the HAN-FUN example applications...
uint16_t size() const
Number bytes needed to serialize the message.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
void remove(Endpoint &ep, uint16_t group, uint16_t device)
Remove the given device from the given group for the given end-point ep.
HF::UID::UID uid
Remote device UID.
#define LOG(X)
Log messages with the level given by X.
uint16_t pack(HF::Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
Transport Layer link implementation over TCP/IP using libuv.
#define HELLO_MSG
Hello message primitive id.
msg_t(uint16_t primitive=NONE_MSG)
Constructor.
Result
Commands result codes.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset)
Read a message from a ByteArray.
Top-level namespace for the HAN-FUN library.