Aruna
aruna::comm::anonymous_namespace{comm.cpp} Namespace Reference

Functions

void set_status (status_t status)
 set the comm status More...
 
err_t transmit (transmitpackage_t transmitpackage)
 Transmit a package. More...
 
void * transmissionQueueHandeler (void *)
 Tramsmission handeler. More...
 
LinkgetDriver ()
 get the driver More...
 
std::tuple< Link *, err_tpickDriver ()
 pick the best available driver More...
 
void setDriver (Link &driver)
 set the driver More...
 
unsigned int rateDriver (Link &driver)
 rate the driver on speed, errors, active connection, realtime, connection type etc. More...
 
void * _selectDriverTask (void *)
 pick a new best driver, dont call directly will delete your process. More...
 
void selectDriverTask ()
 start a task to select a driver, does not block. More...
 
void * receiveHandeler (void *)
 Task to handle incomming connections. More...
 
bool register_log ()
 

Variables

static log::channel_tlog
 
pthread_t transmissionQueueHandeler_thread_handeler
 
pthread_t receiveHandeler_thread_handeler
 
pthread_cond_t out_buffer_not_empty
 
pthread_mutex_t out_buffer_critical
 
std::queue< transmitpackage_tout_buffer
 
std::set< Link * > driverCandidates
 
std::set< channel_t *, channel_t::compare_refrencechannels
 all endpoints More...
 
status_t status = status_t::STOPPED
 stores the comm status More...
 
Linkdriver
 stores the driver. More...
 

Function Documentation

◆ _selectDriverTask()

void * aruna::comm::anonymous_namespace{comm.cpp}::_selectDriverTask ( void *  )

pick a new best driver, dont call directly will delete your process.

Definition at line 194 of file comm.cpp.

194  {
195  auto dr = pickDriver();
196  int pret = 0;
197  if (std::get<1>(dr) == err_t::OK) {
198  setDriver(*std::get<0>(dr));
199  } else {
200  log->error("Failed to pick new driver: %s", err_to_char.at(std::get<1>(dr)));
201  comm::stop();
202  }
203  pthread_exit(&pret);
204  return nullptr;
205  }
static log::channel_t * log
Definition: comm.cpp:23
const std::map< err_t, char * > err_to_char
Definition: arunaTypes.h:54
void setDriver(Link &driver)
set the driver
Definition: comm.cpp:157
err_t stop()
Stop the communication, free all queue&#39;s, channels and buffers.
Definition: comm.cpp:309
std::tuple< Link *, err_t > pickDriver()
pick the best available driver
Definition: comm.cpp:161
int error(const char *format,...)
log error message
Definition: log.cpp:90
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getDriver()

Link * aruna::comm::anonymous_namespace{comm.cpp}::getDriver ( )

get the driver

Returns
ComdDriver object

Definition at line 154 of file comm.cpp.

154  {
155  return driver;
156  }
Link * driver
stores the driver.
Definition: comm.cpp:45
Here is the caller graph for this function:

◆ pickDriver()

std::tuple< Link *, err_t > aruna::comm::anonymous_namespace{comm.cpp}::pickDriver ( )

pick the best available driver

Returns
tuple 0: ComDriver best candidate object. 1: err_t. 1: NO_DRIVER if no driver can be found 1: OK if all is well

Definition at line 161 of file comm.cpp.

161  {
162 // bestpick initalisren omdat hij anders een lege terug kan geven.
163  Link *bestPick = nullptr;
164  unsigned int bestPickScore = 0;
165  unsigned int s = 0;
166  if (driverCandidates.empty())
167  return std::make_tuple(bestPick, err_t::NO_DRIVER);
168  for (auto driverCandidate : driverCandidates) {
169  s = rateDriver(*driverCandidate);
170  if (s > bestPickScore) {
171  bestPick = driverCandidate;
172  bestPickScore = s;
173  }
174 // TODO drivers die het niet zijn geworden moeten worden gedeleted.
175  }
176  return std::make_tuple(bestPick, err_t::OK);
177  }
unsigned int rateDriver(Link &driver)
rate the driver on speed, errors, active connection, realtime, connection type etc.
Definition: comm.cpp:179
Here is the call graph for this function:
Here is the caller graph for this function:

◆ rateDriver()

unsigned int aruna::comm::anonymous_namespace{comm.cpp}::rateDriver ( Link driver)

rate the driver on speed, errors, active connection, realtime, connection type etc.

Parameters
driver
Returns
rating of the driver. Higher is better.

Definition at line 179 of file comm.cpp.

179  {
180  unsigned int score = 0;
181  err_t drivstrt = driver.startup_error;
182  if (drivstrt != err_t::OK) {
183  log->warning("driver failed to start: %s", err_to_char.at(drivstrt));
184  return score;
185  }
186  if (!driver.is_connected())
187  return score;
188  else
189  score += 15;
190  score *= (int) driver.get_speed() / 100;
191 
192  return score;
193  }
static log::channel_t * log
Definition: comm.cpp:23
const std::map< err_t, char * > err_to_char
Definition: arunaTypes.h:54
Link * driver
stores the driver.
Definition: comm.cpp:45
int warning(const char *format,...)
log warning message
Definition: log.cpp:77
Here is the call graph for this function:
Here is the caller graph for this function:

◆ receiveHandeler()

void * aruna::comm::anonymous_namespace{comm.cpp}::receiveHandeler ( void *  )

Task to handle incomming connections.

Returns
the big void

Definition at line 220 of file comm.cpp.

220  {
221  uint16_t bytes_read = 0;
222  uint8_t size = 0;
223  uint8_t *buff;
224  Link* driver;
225 // TODO make it less busyloopy.
226  while (1) {
227  driver = getDriver();
228 // read the size of the package
229  bytes_read = driver->receive(&size, 1);
230 // TODO if statement seems odd, `size = 1` should suffise.
231  if (bytes_read && size >= 2) {
232  log->verbose("new incoming connection, size: %i", size);
233  buff = (uint8_t *) malloc(size);
234  buff[0] = size;
235  bytes_read = driver->receive(&buff[1], (size - 1));
236  log->dump(aruna::log::level_t::VERBOSE, buff, size);
237 
238  incoming_connection(buff, size);
239 // cleanup
240  free(buff);
241  size = 0;
242  bytes_read = 0;
243  }
244  }
245  }
static log::channel_t * log
Definition: comm.cpp:23
Link * getDriver()
get the driver
Definition: comm.cpp:154
Link * driver
stores the driver.
Definition: comm.cpp:45
int verbose(const char *format,...)
log verbose message
Definition: log.cpp:38
err_t incoming_connection(uint8_t *package, uint8_t package_size)
Interrupt incomming connection handeler.
Definition: comm.cpp:453
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:

◆ register_log()

bool aruna::comm::anonymous_namespace{comm.cpp}::register_log ( )

Definition at line 211 of file comm.cpp.

211  {
212  static bool registerd = false;
213  if (!registerd){
214  log = new log::channel_t("comm");
215  registerd = true;
216  }
217  return registerd;
218  }
static log::channel_t * log
Definition: comm.cpp:23
Here is the caller graph for this function:

◆ selectDriverTask()

void aruna::comm::anonymous_namespace{comm.cpp}::selectDriverTask ( )

start a task to select a driver, does not block.

Definition at line 207 of file comm.cpp.

207  {
208  pthread_create(NULL, NULL, _selectDriverTask, NULL);
209  }
void * _selectDriverTask(void *)
pick a new best driver, dont call directly will delete your process.
Definition: comm.cpp:194
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_status()

void aruna::comm::anonymous_namespace{comm.cpp}::set_status ( status_t  status)

set the comm status

Parameters
statusnew status

Definition at line 116 of file comm.cpp.

116  {
118  }
status_t status
stores the comm status
Definition: comm.cpp:40
Here is the caller graph for this function:

◆ setDriver()

void aruna::comm::anonymous_namespace{comm.cpp}::setDriver ( Link driver)

set the driver

Parameters
driverto use.

Definition at line 157 of file comm.cpp.

157  {
158  comm::driver = &driver;
159  }
Link * driver
stores the driver.
Definition: comm.cpp:45
Here is the caller graph for this function:

◆ transmissionQueueHandeler()

void * aruna::comm::anonymous_namespace{comm.cpp}::transmissionQueueHandeler ( void *  )

Tramsmission handeler.

Do not call directly, blocks CPU.

Return values
None

Definition at line 133 of file comm.cpp.

133  {
134  err_t transmit_msg = err_t::FAIL;
135  while(1) {
136 
137  pthread_mutex_lock(&out_buffer_critical);
138  while(out_buffer.empty()){
139 // TODO cond_wait after mutex_lock can cause a deadlock.
140  pthread_cond_wait(&out_buffer_not_empty, &out_buffer_critical);
141  }
142  transmit_msg = transmit(out_buffer.front());
143  if (transmit_msg != err_t::OK) {
144  log->warning("transmit of: %d, to: %d, failed: %s", out_buffer.front().from_port, out_buffer.front().to_port,
145  err_to_char.at(transmit_msg));
146  log->dump(log::level_t::WARNING, out_buffer.front().data_transmitting, out_buffer.front().data_lenght);
147  } else {
148  out_buffer.pop();
149  }
150  pthread_mutex_unlock(&out_buffer_critical);
151 
152  }
153  }
static log::channel_t * log
Definition: comm.cpp:23
const std::map< err_t, char * > err_to_char
Definition: arunaTypes.h:54
err_t transmit(transmitpackage_t transmitpackage)
Transmit a package.
Definition: comm.cpp:120
std::queue< transmitpackage_t > out_buffer
Definition: comm.cpp:29
int warning(const char *format,...)
log warning message
Definition: log.cpp:77
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:

◆ transmit()

err_t aruna::comm::anonymous_namespace{comm.cpp}::transmit ( transmitpackage_t  transmitpackage)

Transmit a package.

Parameters
transmitpackagepackage to be transmitted
Returns
err_t.
  • OK if success
  • HARDWARE_ERR if the hardware layer failed

Definition at line 120 of file comm.cpp.

120  {
121  err_t return_msg = err_t::OK;
122  size_t send;
123  uint8_t* data = (uint8_t*) malloc(transmitpackage.data_lenght + transmitpackage_t::HEADER_SIZE);
124 
125  transmitpackage_t::transmitpackage_to_binary(transmitpackage, data);
126  send = getDriver()->transmit(data, transmitpackage.size);
127  if (send != transmitpackage.size)
128  return_msg = err_t::FAIL;
129  free(data);
130  return return_msg;
131  }
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
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:

Variable Documentation

◆ channels

std::set<channel_t*, channel_t::compare_refrence> aruna::comm::anonymous_namespace{comm.cpp}::channels

all endpoints

Definition at line 35 of file comm.cpp.

◆ driver

Link* aruna::comm::anonymous_namespace{comm.cpp}::driver

stores the driver.

Definition at line 45 of file comm.cpp.

◆ driverCandidates

std::set<Link *> aruna::comm::anonymous_namespace{comm.cpp}::driverCandidates

Definition at line 30 of file comm.cpp.

◆ log

log::channel_t* aruna::comm::anonymous_namespace{comm.cpp}::log
static

Definition at line 23 of file comm.cpp.

◆ out_buffer

std::queue<transmitpackage_t> aruna::comm::anonymous_namespace{comm.cpp}::out_buffer

Definition at line 29 of file comm.cpp.

◆ out_buffer_critical

pthread_mutex_t aruna::comm::anonymous_namespace{comm.cpp}::out_buffer_critical

Definition at line 28 of file comm.cpp.

◆ out_buffer_not_empty

pthread_cond_t aruna::comm::anonymous_namespace{comm.cpp}::out_buffer_not_empty

Definition at line 27 of file comm.cpp.

◆ receiveHandeler_thread_handeler

pthread_t aruna::comm::anonymous_namespace{comm.cpp}::receiveHandeler_thread_handeler

Definition at line 26 of file comm.cpp.

◆ status

status_t aruna::comm::anonymous_namespace{comm.cpp}::status = status_t::STOPPED

stores the comm status

Definition at line 40 of file comm.cpp.

◆ transmissionQueueHandeler_thread_handeler

pthread_t aruna::comm::anonymous_namespace{comm.cpp}::transmissionQueueHandeler_thread_handeler

Definition at line 25 of file comm.cpp.