Aruna
aruna::comm Namespace Reference

Namespaces

 anonymous_namespace{comm.cpp}
 

Classes

struct  channel_t
 endpoint type of a comm channel More...
 
class  Link
 
struct  transmitpackage_t
 transmit ready package. More...
 

Typedefs

typedef uint8_t port_t
 

Enumerations

enum  link_t { link_t::RADIO, link_t::WIRED, link_t::NONE }
 
enum  status_t { status_t::RUNNING, status_t::STOPPED, status_t::PAUSED }
 

Functions

err_t start ()
 Start new communication. More...
 
err_t start (Link *driver)
 Start new communication. More...
 
err_t stop ()
 Stop the communication, free all queue's, channels and buffers. More...
 
err_t pause ()
 pause all communication. More...
 
err_t resume ()
 resume all communication. More...
 
err_t register_channel (channel_t *channel)
 Register a new communication endpoint. More...
 
err_t unregister_channel (channel_t &channel)
 unregister an endpoint More...
 
err_t send (channel_t *channel, port_t to_port, uint8_t *data, size_t data_size, bool wait_for_ack=false)
 Send data. More...
 
bool is_connected ()
 Get the status of the comm link. More...
 
status_t get_status ()
 Get the running status of com. More...
 
err_t get_channels (char *buffer)
 get all names of the channels currently registered More...
 
unsigned int get_speed ()
 Get the speed of the link to the other hosts in bits per second. More...
 
err_t incoming_connection (uint8_t *package, uint8_t package_size)
 Interrupt incomming connection handeler. More...
 
err_t register_candidate_driver (Link *driver)
 register a driver to be a comm driver candidate. More...
 
err_t unregister_candidate_driver (Link *driver)
 unregister a driver to be a comdiver candidate More...
 
link_t get_link_type ()
 Get connection hardware type. More...
 
void get_candidate_drivers (char *buffer[])
 get all the comm driver candidates More...
 

Variables

static constexpr size_t MAX_DATA_SIZE = 150
 

Typedef Documentation

◆ port_t

typedef uint8_t aruna::comm::port_t

Definition at line 30 of file commTypes.h.

Enumeration Type Documentation

◆ link_t

enum aruna::comm::link_t
strong
Enumerator
RADIO 
WIRED 
NONE 

Definition at line 17 of file commTypes.h.

◆ status_t

enum aruna::comm::status_t
strong
Enumerator
RUNNING 
STOPPED 
PAUSED 

Definition at line 24 of file commTypes.h.

Function Documentation

◆ get_candidate_drivers()

void aruna::comm::get_candidate_drivers ( char *  buffer[])

get all the comm driver candidates

◆ get_channels()

err_t aruna::comm::get_channels ( char *  buffer)

get all names of the channels currently registered

Parameters
bufferto write the names into.
Returns
err_t
  • OK if it was a success
  • BUFFER_OVERFLOW if the suplied buffer is to small,
  • `INVALID_PARAMETERS if the parameters are incorrect,

Definition at line 444 of file comm.cpp.

444  {
445 // TODO
446  return err_t::OK;
447  }

◆ get_link_type()

link_t aruna::comm::get_link_type ( )

Get connection hardware type.

Return values
link_t

◆ get_speed()

unsigned int aruna::comm::get_speed ( )

Get the speed of the link to the other hosts in bits per second.

Note
returns 0 if there is no connection.
Return values
int,speed

Definition at line 449 of file comm.cpp.

449  {
450  return getDriver()->get_speed();
451  }
Link * getDriver()
get the driver
Definition: comm.cpp:154
Here is the call graph for this function:

◆ get_status()

status_t aruna::comm::get_status ( )

Get the running status of com.

Returns
status_t (RUNNING, PAUSED, STOPPED)

Definition at line 440 of file comm.cpp.

440  {
441  return status;
442  }
status_t status
stores the comm status
Definition: comm.cpp:40
Here is the caller graph for this function:

◆ incoming_connection()

err_t aruna::comm::incoming_connection ( uint8_t *  package,
uint8_t  package_size 
)

Interrupt incomming connection handeler.

Parameters
packagethat needs to be handeled.
Return values
err_tNOT_STARTED comm is not started OK handeled NO_CHANNEL there is no channel to handle it

Definition at line 453 of file comm.cpp.

453  {
454 /*
455  * TODO O(N) is eigenlijk te langzaam.
456  * Als we het meteen kunnen opzoeken door ook nog een set te maken (of hashing table) van de namen
457  * scheelt dat ons heel veel tijd want dan is het O(1)!
458  */
459  transmitpackage_t tp;
460  if (get_status() != status_t::RUNNING) return err_t::NOT_STARTED;
461  log->verbose("incoming connection");
462 
463  if (!transmitpackage_t::binary_to_transmitpackage(package, tp))
464  return err_t::INVALID_PARAMETERS;
465 
466  for (auto channel : channels) {
467  if (channel->port == tp.to_port) {
468  pthread_mutex_lock((pthread_mutex_t*)&channel->in_buffer_lock);
469  channel->in_buffer.push(tp);
470  pthread_mutex_unlock((pthread_mutex_t*)&channel->in_buffer_lock);
471  pthread_cond_broadcast((pthread_cond_t*)&channel->in_buffer_not_empty);
472  return err_t::OK;
473  }
474  }
475  return err_t::NO_CHANNEL;
476 
477  }
std::set< channel_t *, channel_t::compare_refrence > channels
all endpoints
Definition: comm.cpp:35
static log::channel_t * log
Definition: comm.cpp:23
status_t get_status()
Get the running status of com.
Definition: comm.cpp:440
int verbose(const char *format,...)
log verbose message
Definition: log.cpp:38
Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_connected()

bool aruna::comm::is_connected ( )

Get the status of the comm link.

Return values
1is connected, 0 if not.

Definition at line 436 of file comm.cpp.

436  {
437  return getDriver()->is_connected();
438  }
Link * getDriver()
get the driver
Definition: comm.cpp:154
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pause()

err_t aruna::comm::pause ( )

pause all communication.

buffers, channels and queue's will be saved

Returns
err_t
  • OK if it was a success,
  • NOT_STARTED if the comm was not started.

Definition at line 335 of file comm.cpp.

335  {
336  switch (get_status()) {
337  case status_t::STOPPED:
338  case status_t::PAUSED:
339  return err_t::NOT_STARTED;
340  default:
341  break;
342  }
343 // TODO suspend transmissionQueueHandeler_thread_handeler
344  set_status(status_t::PAUSED);
345  return err_t::OK;
346  }
status_t get_status()
Get the running status of com.
Definition: comm.cpp:440
void set_status(status_t status)
set the comm status
Definition: comm.cpp:116
Here is the call graph for this function:

◆ register_candidate_driver()

err_t aruna::comm::register_candidate_driver ( Link driver)

register a driver to be a comm driver candidate.

Parameters
driverComDriver object
Returns
err_t
  • DRIVER_EXISTS if driver already exists.
  • BUFFER_OVERFLOW driver buffer overflow
  • OK all is well :).

Definition at line 479 of file comm.cpp.

479  {
480 // comdriver moet eigenlijk een referentie zijn en niet een levend object.
481  register_log();
482  log->debug("registering driver");
483  if (driverCandidates.find(driver) != driverCandidates.end()) {
484  return err_t::DRIVER_EXISTS;
485  }
486  if (!driverCandidates.insert(driver).second)
487  return err_t::BUFFER_OVERFLOW;
488 // select new driver if comm is running
489  if (get_status() == status_t::RUNNING) {
490 // TODO its better to rate this new driver and compare its score to the current driver.
492  }
493  return err_t::OK;
494  }
int debug(const char *format,...)
log debug message
Definition: log.cpp:51
static log::channel_t * log
Definition: comm.cpp:23
void selectDriverTask()
start a task to select a driver, does not block.
Definition: comm.cpp:207
status_t get_status()
Get the running status of com.
Definition: comm.cpp:440
Link * driver
stores the driver.
Definition: comm.cpp:45
Here is the call graph for this function:

◆ register_channel()

err_t aruna::comm::register_channel ( channel_t channel)

Register a new communication endpoint.

Parameters
channelchannel_t object
Return values
err_t
  • INVALID_PARAMETERS if parameters are invalid
  • OK if it was succesfully added.
  • CHANNEL_EXISTS if the channel already exists.
  • BUFFER_OVERFLOW channel buffer overflow

Definition at line 362 of file comm.cpp.

362  {
363 /*
364  * TODO paramters should consist of the paramters of channel_t together with a handeler.
365  * handeler can then later be used for sending and accessing Rx array
366  */
367 
368 // TODO testen of deze check wel werkt.
369  if (channels.find(channel) != channels.end())
370  return err_t::CHANNEL_EXISTS;
371  if (channels.insert(channel).second) {
372  return err_t::OK;
373  } else
374  return err_t::BUFFER_OVERFLOW;
375 // TODO handeler fixen.
376  }
std::set< channel_t *, channel_t::compare_refrence > channels
all endpoints
Definition: comm.cpp:35
Here is the caller graph for this function:

◆ resume()

err_t aruna::comm::resume ( )

resume all communication.

Returns
err_t
  • OK if it was a success,
  • NOT_STARTED if the comm was not started.
  • NOT_PAUSED if it was not paused.

Definition at line 348 of file comm.cpp.

348  {
349  switch (get_status()) {
350  case status_t::STOPPED:
351  return err_t::NOT_STARTED;
352  case status_t::RUNNING:
353  return err_t::NOT_PAUSED;
354  default:
355  break;
356  }
357 // TODO continue transmissionQueueHandeler_thread_handeler
358  set_status(status_t::RUNNING);
359  return err_t::OK;
360  }
status_t get_status()
Get the running status of com.
Definition: comm.cpp:440
void set_status(status_t status)
set the comm status
Definition: comm.cpp:116
Here is the call graph for this function:
Here is the caller graph for this function:

◆ send()

err_t aruna::comm::send ( channel_t channel,
port_t  to_port,
uint8_t *  data,
size_t  data_size,
bool  wait_for_ack = false 
)

Send data.

Note
Parameters
channelchannel handler that is sending the data
to_portto which port to send de data to.
dataData to send.
data_sizelength of the data
wait_for_ackif set to thrue, thread will block until ack is received or until timeout is reached
Return values
err_t
  • INVALID_PARAMETERS if parameters are invalid
  • OK if it was succesfully send.
  • NO_CONNECTION if there is no connection,
  • BUFFER_OVERFLOW if the data was not added to the bugger due an overflow,
  • NO_CHANNEL if the channel does'nt exist.
  • NO_RESPONSE if there was no response (only if wait_for_ack is true)

Definition at line 386 of file comm.cpp.

386  {
387  if (get_status() != status_t::RUNNING) return err_t::NOT_STARTED;
388  if (channels.find(channel) == channels.end()) {
389  return err_t::NO_CHANNEL;
390  }
391  uint16_t packages = ceil((double) data_size / (double) MAX_DATA_SIZE);
392 // how many packages are we waiting for?
393  uint8_t n_to_wait_for = 0;
394  // datasize length
395  size_t ds_l;
396  // datasize pointer
397  size_t ds_p = 0;
398 
399  if (!getDriver()->is_connected())
400  return err_t::NO_CONNECTION;
401 
402 // TODO check if tp is allocated
403  auto tp = (transmitpackage_t *) calloc(packages, sizeof(transmitpackage_t));
404 
405 // loop until all data in send
406  while (data_size > 0) {
407 // static data
408  tp[n_to_wait_for].to_port = to_port;
409  tp[n_to_wait_for].from_port = channel->port;
410  tp[n_to_wait_for].n = 0;
411  tp[n_to_wait_for].notify_on_ack = wait_for_ack;
412 
413  tp[n_to_wait_for].data_transmitting = &data[ds_p];
414 
415  ds_l = (data_size >= MAX_DATA_SIZE) ? MAX_DATA_SIZE : data_size;
416 
417  tp[n_to_wait_for].data_lenght = ds_l;
418  tp[n_to_wait_for].size = ds_l + transmitpackage_t::HEADER_SIZE;
419 
420  ds_p += ds_l;
421  data_size -= ds_l;
422  log->verbose("sending from: %d to: %d", tp[n_to_wait_for].from_port,
423  tp[n_to_wait_for].to_port);
424  log->dump(log::level_t::VERBOSE, tp[n_to_wait_for].data_transmitting, tp[n_to_wait_for].data_lenght);
425  pthread_mutex_lock(&out_buffer_critical);
426  out_buffer.push(tp[n_to_wait_for]);
427  pthread_mutex_unlock(&out_buffer_critical);
428  pthread_cond_broadcast(&out_buffer_not_empty);
429 
430  n_to_wait_for++;
431  }
432  free(tp);
433  return err_t::OK;
434  }
std::set< channel_t *, channel_t::compare_refrence > channels
all endpoints
Definition: comm.cpp:35
static constexpr size_t MAX_DATA_SIZE
Definition: commTypes.h:34
static log::channel_t * log
Definition: comm.cpp:23
status_t get_status()
Get the running status of com.
Definition: comm.cpp:440
std::queue< transmitpackage_t > out_buffer
Definition: comm.cpp:29
Link * getDriver()
get the driver
Definition: comm.cpp:154
int verbose(const char *format,...)
log verbose message
Definition: log.cpp:38
bool is_connected()
Get the status of the comm link.
Definition: comm.cpp:436
int dump(level_t level, uint8_t *bin, size_t size)
dump array of bin data
Definition: log.cpp:135
Here is the call graph for this function:
Here is the caller graph for this function:

◆ start() [1/2]

err_t aruna::comm::start ( )

Start new communication.

start SIS report watcher thread

start reporter to report all SIS activiry to the watcher over comm channels

Using LINK_HARDWARE to define hardware

Return values
err_t
  • HARDWARE_ERROR if the hardware fails.
  • OK great success!

Definition at line 251 of file comm.cpp.

251  {
252 // pick a driver
253  auto dr = pickDriver();
254  if (std::get<1>(dr) == err_t::OK) {
255 // if driver is found start comm with driver as paramter
256  return start(std::get<0>(dr));
257  }
258 // return pickDriver error.
259  return std::get<1>(dr);
260  }
err_t start(Link *driver)
Start new communication.
Definition: comm.cpp:262
std::tuple< Link *, err_t > pickDriver()
pick the best available driver
Definition: comm.cpp:161
Here is the call graph for this function:

◆ start() [2/2]

err_t aruna::comm::start ( Link driver)

Start new communication.

Using LINK_HARDWARE to define hardware

Parameters
driverforce to use specific driver.
Return values
err_t
  • HARDWARE_ERROR if the hardware fails.
  • OK great success!

Definition at line 262 of file comm.cpp.

262  {
263  switch (get_status()) {
264  case status_t::RUNNING:
265  return err_t::NOT_STOPPED;
266  case status_t::PAUSED:
267 // TODO is dit slim? kan ongwenst gedrag vertonen als je van driver wilt wisselen.
268  return resume();
269  default:
270  break;
271  }
272  int err = 0;
273 // register com channel
274  register_log();
275 // set the driver
276  err_t driver_err;
277  setDriver(*driver);
278 // TODO check if driver is not NULL
279  driver_err = getDriver()->startup_error;
280 // init pthread mutex and cond
281  pthread_cond_init(&out_buffer_not_empty, NULL);
282  pthread_mutex_init(&out_buffer_critical, NULL);
283  if (driver_err != err_t::OK)
284  return driver_err;
285 
286  err = pthread_create(&transmissionQueueHandeler_thread_handeler, NULL, transmissionQueueHandeler, NULL);
287  if (err) {
288  log->error("failed to start transmissionQueueHandeler: %i", err);
289 // stop when task failes
290 // TODO destroy driver object
291 // TODO driver unsetten en destroyen.
292  return err_t::TASK_FAILED;
293  }
294  err = pthread_create(&receiveHandeler_thread_handeler, NULL, receiveHandeler, NULL);
295  if (err) {
296  log->error("failed to start receiveHandeler: %i", err);
297 // stop when task failes
298 // TODO destroy driver object
299 // TODO driver unsetten en destroyen.
300  return err_t::TASK_FAILED;
301  }
302 // set status to running if all succeeded
303 
304  set_status(status_t::RUNNING);
305 
306  return err_t::OK;
307  }
void * transmissionQueueHandeler(void *)
Tramsmission handeler.
Definition: comm.cpp:133
static log::channel_t * log
Definition: comm.cpp:23
void setDriver(Link &driver)
set the driver
Definition: comm.cpp:157
status_t get_status()
Get the running status of com.
Definition: comm.cpp:440
void set_status(status_t status)
set the comm status
Definition: comm.cpp:116
Link * getDriver()
get the driver
Definition: comm.cpp:154
void * receiveHandeler(void *)
Task to handle incomming connections.
Definition: comm.cpp:220
err_t resume()
resume all communication.
Definition: comm.cpp:348
Link * driver
stores the driver.
Definition: comm.cpp:45
int error(const char *format,...)
log error message
Definition: log.cpp:90
Here is the call graph for this function:

◆ stop()

err_t aruna::comm::stop ( )

Stop the communication, free all queue's, channels and buffers.

Return values
err_t
  • HARDWARE_ERROR if the hardware fails.
  • OK great success!
  • NOT_STARTED if the comm was not started.

Definition at line 309 of file comm.cpp.

309  {
310  switch (get_status()) {
311  case status_t::STOPPED:
312  return err_t::NOT_STARTED;
313  default:
314  break;
315  }
316 // stop all
317  err_t driver_err;
318 // TODO destory driver object
319  pthread_cancel(receiveHandeler_thread_handeler);
321  int pret = 0;
322  pthread_cond_destroy(&out_buffer_not_empty);
323  pthread_mutex_destroy(&out_buffer_critical);
324  pthread_exit(&pret);
325 // TODO is this code even reached is the thread is terminated?
326  while(!out_buffer.empty())
327  out_buffer.pop();
328  set_status(status_t::STOPPED);
329 
330  if (driver_err != err_t::OK)
331  return driver_err;
332  return err_t::OK;
333  }
status_t get_status()
Get the running status of com.
Definition: comm.cpp:440
void set_status(status_t status)
set the comm status
Definition: comm.cpp:116
std::queue< transmitpackage_t > out_buffer
Definition: comm.cpp:29
Here is the call graph for this function:
Here is the caller graph for this function:

◆ unregister_candidate_driver()

err_t aruna::comm::unregister_candidate_driver ( Link driver)

unregister a driver to be a comdiver candidate

Parameters
driverComDriver to be deleted
Returns
err_t
  • OK great success!
  • NO_DRIVER driver does'nt exists.

Definition at line 497 of file comm.cpp.

497  {
498  if (driverCandidates.erase(driver)) {
499 // if we delete our own driver, search for a new one
500  if (comm::driver == driver)
502  return err_t::OK;
503  } else
504  return err_t::NO_DRIVER;
505  }
void selectDriverTask()
start a task to select a driver, does not block.
Definition: comm.cpp:207
Link * driver
stores the driver.
Definition: comm.cpp:45
Here is the call graph for this function:

◆ unregister_channel()

err_t aruna::comm::unregister_channel ( channel_t channel)

unregister an endpoint

Parameters
channelendpoint to be removed
Return values
err_t
  • OK if it was succesfully removed.
  • NO_CHANNEL ain't got no channel, ain't got no knife.

Definition at line 378 of file comm.cpp.

378  {
379  if (channels.erase(&channel))
380  return err_t::OK;
381  else
382  return err_t::NO_CHANNEL;
383  }
std::set< channel_t *, channel_t::compare_refrence > channels
all endpoints
Definition: comm.cpp:35
Here is the caller graph for this function:

Variable Documentation

◆ MAX_DATA_SIZE

constexpr size_t aruna::comm::MAX_DATA_SIZE = 150
static

Definition at line 34 of file commTypes.h.