Aruna
commTypes.h
Go to the documentation of this file.
1 //
2 // Created by noeel on 17-08-19.
3 //
4 
5 #ifndef ARUNA_COMMTYPES_H
6 #define ARUNA_COMMTYPES_H
7 
8 #include <stdlib.h>
9 #include <string.h>
10 #include "pthread.h"
11 #include <queue>
12 
13 namespace aruna {
14  namespace comm {
15 
16  // kind of link, wired, wireless of non existing
17  enum class link_t {
18  RADIO,
19  WIRED,
20  NONE
21  };
22 
23  // status of the comm object
24  enum class status_t {
25  RUNNING,
26  STOPPED,
27  PAUSED
28  };
29 
30  typedef uint8_t port_t;
31 
32  // comm data size minus the header
33  // TODO increasing this causes an overflow
34  static constexpr size_t MAX_DATA_SIZE = 150;
35 
40  constexpr static uint8_t HEADER_SIZE = 4;
44  port_t from_port;
45 
49  port_t to_port;
50 
51  /*
52  * Number of the package (used for ack.)
53  */
54  uint8_t n;
55 
56 
61 
65  uint8_t *data_received;
66 
70  uint8_t data_lenght;
71 
75  uint8_t size;
76 
77 
81  bool notify_on_ack = false;
82 
86  bool resend = false;
87 
88 
95  static void transmitpackage_to_binary(transmitpackage_t transp, uint8_t *bin) {
96 
97  // bit 0: size
98  memcpy(&bin[0],
99  &transp.size,
100  (sizeof(transp.size)));
101  // bit 1: N
102  memcpy(&bin[sizeof(transp.size)],
103  &transp.n,
104  (sizeof(transp.n)));
105  // bit 2: from port
106  memcpy(&bin[sizeof(transp.size) + sizeof(transp.n)],
107  &transp.from_port,
108  (sizeof(transp.from_port)));
109  // bit 3: to port
110  memcpy(&bin[sizeof(transp.size) + sizeof(transp.n) + sizeof(transp.from_port)],
111  &transp.to_port,
112  sizeof(transp.to_port));
113  // bit 4: data
114  memcpy(&bin[sizeof(transp.size) + sizeof(transp.n) + sizeof(transp.from_port) + sizeof(transp.to_port)],
115  transp.data_transmitting,
116  transp.data_lenght);
117  }
118 
125  static bool binary_to_transmitpackage(uint8_t *bin, transmitpackage_t &transp) {
126  // check for complete header
127  const static int header_length = (sizeof(transp.size) + sizeof(transp.n) + sizeof(transp.from_port) +
128  sizeof(transp.to_port));
129  if (bin[0] < header_length)
130  return 0;
131 
132  int dataLength = bin[0] - header_length;
133  transp.data_received = (uint8_t *) malloc(dataLength);
134  memcpy(&transp.size, &bin[0], (sizeof(transp.size)));
135  memcpy(&transp.n, &bin[sizeof(transp.size)], (sizeof(transp.n)));
136  memcpy(&transp.from_port, &bin[sizeof(transp.size) + sizeof(transp.n)], (sizeof(transp.from_port)));
137  memcpy(&transp.to_port, &bin[sizeof(transp.size) + sizeof(transp.n) + sizeof(transp.from_port)],
138  (sizeof(transp.to_port)));
139  memcpy(transp.data_received,
140  &bin[sizeof(transp.size) + sizeof(transp.n) + sizeof(transp.from_port) + sizeof(transp.to_port)],
141  dataLength);
142  transp.data_lenght = dataLength;
143  return 1;
144  }
145  };
146 
150  struct channel_t {
151  pthread_cond_t in_buffer_not_empty;
152  pthread_mutex_t in_buffer_lock;
153  std::queue<transmitpackage_t> in_buffer;
154  err_t register_err = err_t::NOT_STARTED;
155 
156  channel_t(port_t port);
157  ~channel_t();
158 
159  err_t send(port_t to_port, uint8_t *data, size_t data_size, bool wait_for_ack = false);
160 
164  const port_t port;
165 
170 // QueueHandle_t *handeler;
172  pthread_mutex_lock(&in_buffer_lock);
173  while (in_buffer.empty()) {
174  pthread_cond_wait(&in_buffer_not_empty, &in_buffer_lock);
175  }
176  *tpp = in_buffer.front();
177  pthread_mutex_unlock(&in_buffer_lock);
178  return true;
179  }
180 
181 // TODO bool receive(transmitpackage_t*, uint32_t timeout_ms)
182  bool receive_clear() {
183  free(in_buffer.front().data_received);
184  in_buffer.pop();
185  return true;
186  }
187 
188  bool operator<(const channel_t &b) const {
189  return this->port < b.port;
190  }
191 
192  bool operator==(const uint8_t port) const {
193  return this->port == port;
194  }
195 
197  inline bool operator()(const channel_t* a, const channel_t* b) const {
198  return a->port < b->port;
199  }
200  };
201 
202  struct compare_value {
203  inline bool operator()(const channel_t& a, const channel_t& b) const {
204  return a.port < b.port;
205  }
206  };
207  };
208  }
209 }
210 #endif //ARUNA_COMMTYPES_H
Definition: comm.cpp:14
uint8_t * data_transmitting
Data to be transmitted.
Definition: commTypes.h:60
static void transmitpackage_to_binary(transmitpackage_t transp, uint8_t *bin)
Get binary array of transmitpackage, for sending over a link.
Definition: commTypes.h:95
static constexpr size_t MAX_DATA_SIZE
Definition: commTypes.h:34
pthread_mutex_t in_buffer_lock
Definition: commTypes.h:152
port_t to_port
to whom to send it to.
Definition: commTypes.h:49
uint8_t port_t
Definition: commTypes.h:30
err_t send(channel_t *channel, port_t to_port, uint8_t *data, size_t data_size, bool wait_for_ack)
Send data.
Definition: comm.cpp:386
bool operator()(const channel_t &a, const channel_t &b) const
Definition: commTypes.h:203
uint8_t data_lenght
size of the data
Definition: commTypes.h:70
uint8_t size
total size of package (+ the size of the size variable)
Definition: commTypes.h:75
port_t from_port
channel who is sending the data.
Definition: commTypes.h:44
std::queue< transmitpackage_t > in_buffer
Definition: commTypes.h:153
bool receive(transmitpackage_t *tpp)
handeler to handle incomming connections
Definition: commTypes.h:171
bool operator()(const channel_t *a, const channel_t *b) const
Definition: commTypes.h:197
pthread_cond_t in_buffer_not_empty
Definition: commTypes.h:151
endpoint type of a comm channel
Definition: commTypes.h:150
bool operator<(const channel_t &b) const
Definition: commTypes.h:188
static const comm::port_t port
Definition: reporter.h:15
const port_t port
port nr.
Definition: commTypes.h:164
uint8_t * data_received
pointer to where incoming data must be stored.
Definition: commTypes.h:65
bool operator==(const uint8_t port) const
Definition: commTypes.h:192
static bool binary_to_transmitpackage(uint8_t *bin, transmitpackage_t &transp)
get tansmitpackage of binary array.
Definition: commTypes.h:125
transmit ready package.
Definition: commTypes.h:39