4 * @brief the FRESCAN protocol
15 * This file contains the implementation of the FRESCAN protocol
19 * See MaRTE OS license
23 #include <sys/marte_configuration_parameters.h> // PATH_MX
24 #include <fcntl.h> // open
25 #include <unistd.h> // ioctl
26 #include <stdlib.h> // malloc
27 #include <string.h> // memcpy
29 #include <drivers/can.h> // can_chip_t, can_frame_t
30 #include "frescan.h" // frescan_init_params_t, frescan_send_params_t
31 #include "frescan_queues.h" // init, enqueue, requeue
32 #include "frescan_data.h" // init, the_networks
33 #include "frescan_servers.h" // init, frame_sent
34 #include "frescan_debug.h" // DEBUG
35 #include "frescan_id.h" // frescan_id_set_field, frescan_id_get_field
36 #include "frescan_hw_buffer.h" // frescan_hw_buffer_update
37 #include "frescan_bwres_robjs.h" // frescan_replyobjects_init
38 #include "frescan_servers_replenishments.h" // frescan_replenishments_xxx
39 #include "frescan_packets.h"
41 static int frescan_hook_frame_recv (const struct can_chip_t *chip,
42 struct can_frame_t *frame);
44 static int frescan_hook_frame_sent(const struct can_chip_t *chip);
46 static int frescan_hook_frame_aborted(const struct can_chip_t *chip);
49 * frescan_init - initializes the network and the internal structures
51 * @params: the initialization parameters
53 * This function initializes the frescan network protocol.
55 * First it opens and configures the corresponding CAN chip device. For the
56 * CAN chip acceptance filter we use a dual filter configuration. The first
57 * filter is to set my local address address and the second one is to allow
60 * Once the CAN chip is configured we call the initialization functions of
61 * the rest of modules of frescan.
65 int frescan_init(frescan_init_params_t *params)
68 char can_path[PATH_MX];
69 struct ioctl_filters_t ioctl_filters;
70 struct can_filter_t filters[2];
72 snprintf(can_path, PATH_MX, "/dev/can%u", params->net);
74 DEBUG(FRESCAN_INIT_ENABLE_DEBUG, "open %s\n", can_path);
76 fd = open (can_path, O_RDWR);
78 ERROR ("could not open /dev/can%u\n", params->net);
82 DEBUG(FRESCAN_INIT_ENABLE_DEBUG, "set acceptance filters\n");
84 filters[0].mask = 0xFFFFFFFF;
85 frescan_id_set_field(&filters[0].mask, FRESCAN_FIELD_DEST,0x00);
87 frescan_id_set_field(&filters[0].code, FRESCAN_FIELD_DEST,params->node);
89 filters[1].mask = filters[0].mask;
91 frescan_id_set_field(&filters[1].code,
93 FRESCAN_BROADCAST_ADDR);
95 ioctl_filters.filters = filters;
96 ioctl_filters.len = 2;
98 ret = ioctl(fd, CAN_IOCTL_SET_FILTERS, &ioctl_filters);
100 ERROR ("ioctl CAN_IOCTL_SET_FILTERS failed /dev/can%u\n",
105 DEBUG(FRESCAN_INIT_ENABLE_DEBUG, "set tx, rx, abort hooks\n");
107 ret = ioctl(fd, CAN_IOCTL_SET_TX_HOOK, frescan_hook_frame_sent);
109 ERROR ("ioctl CAN_IOCTL_SET_TX_HOOK failed /dev/can%u\n",
114 ret = ioctl(fd, CAN_IOCTL_SET_RX_HOOK, frescan_hook_frame_recv);
116 ERROR ("ioctl CAN_IOCTL_SET_RX_HOOK failed /dev/can%u\n",
121 ret = ioctl(fd, CAN_IOCTL_SET_AB_HOOK, frescan_hook_frame_aborted);
123 ERROR ("ioctl CAN_IOCTL_SET_AB_HOOK failed /dev/can%u\n",
128 DEBUG(FRESCAN_INIT_ENABLE_DEBUG, "init the rest of modules\n");
130 ret = frescan_data_init(fd, params);
132 ERROR("could not initialize the global data\n");
136 ret = frescan_packets_init();
138 ERROR("could not initialize the packets pool\n");
142 ret = frescan_queues_init(&the_networks[params->net].queues, params);
144 ERROR("could not initialize the queues\n");
148 ret = frescan_servers_init(params->net);
150 ERROR("could not initialize the servers\n");
154 ret = frescan_replenishments_init(params->net);
156 ERROR("could not initialize the replenishments\n");
164 * frescan_send - send a message
166 * @params: the parameters needed by the protocol to send the message
167 * @msg: the message buffer
168 * @size: the size of the message
170 * This is one of the main functions of the protocol and it provides the
171 * means to send a message through the protocol stack.
174 int frescan_send(const frescan_send_params_t *params,
179 frescan_packet_t *packet;
180 frescan_prio_queue_t *pqueue;
182 DEBUG(FRESCAN_SEND_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
183 "checking arguments (msg size=%d)\n", size);
185 if ((params == NULL) || (msg == NULL) || (size == 0)) {
186 ERROR("arguments are not ok\n");
190 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "allocating a packet\n");
192 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
193 packet = frescan_packets_alloc();
194 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
196 if (packet == NULL) {
197 ERROR("could not allocate packet\n");
200 packet->flags = params->flags; // set the flags (to remember them)
202 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "allocating a frame\n");
204 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
205 packet->frame = can_framespool_alloc();
206 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
208 if (packet->frame == NULL) {
209 ERROR("could not allocate frame\n");
213 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "set values for the frame header\n");
214 packet->frame->is_extended_format = true;
215 packet->frame->is_rtr = false;
217 if (params->flags & FRESCAN_FP) {
218 // NOTE: frag id for fp is: FRESCAN_MX_IDS, so the servers can
219 // have IDs in the range (0 .. FRESCAN_MX_IDS-1)
220 frescan_id_set_field(&packet->frame->id,
221 FRESCAN_FIELD_FRAG_ID,
222 (uint32_t)FRESCAN_MX_IDS);
224 frescan_id_set_field(&packet->frame->id,
226 (uint32_t)params->prio);
228 // NOTE: the priority is put when the packet is dequeued
229 // and it is the priority of th server
230 frescan_id_set_field(&packet->frame->id,
231 FRESCAN_FIELD_FRAG_ID,
232 (uint32_t)params->ss);
235 frescan_id_set_field(&packet->frame->id,
237 (uint32_t)params->to);
239 frescan_id_set_field(&packet->frame->id,
241 (uint32_t)the_networks[params->net].local_node);
243 frescan_id_set_field(&packet->frame->id,
245 (uint32_t)params->channel);
247 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "set the packet data bytes\n");
248 if (params->flags & FRESCAN_ASYNC) {
249 // allocate a buffer and copy the data
250 // NOTE: instead of this we could use a chain of frames but
251 // i think it would be inefficient since each one can only
252 // hold 8 user bytes and we need to write its headers.
253 packet->buffer_head = (uint8_t *)malloc(size*sizeof(uint8_t));
254 memcpy(packet->buffer_head, msg, size);
256 packet->buffer_head = (uint8_t *)msg;
259 packet->buffer_read_pointer = packet->buffer_head;
260 packet->buffer_pending_bytes = size;
261 pqueue = the_networks[params->net].queues.tx_fp_queue;
263 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "enqueue the packet\n");
264 if (packet->flags & FRESCAN_FP) {
265 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
266 ret = frescan_pqueue_enqueue(pqueue, packet, params->prio);
267 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
270 ERROR("could not enqueue the packet\n");
274 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
275 ret = frescan_servers_enqueue(params->net, params->ss, packet);
276 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
279 ERROR("could not enqueue the packet\n");
284 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
285 ret = frescan_hw_buffer_update(params->net);
286 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
289 ERROR("could not update hw buffer\n");
297 * frescan_recv - receive a message
299 * @params: the parameters needed by the protocol to receive the message
300 * @msg: the message buffer
301 * @size: the size of the message buffer
302 * @recv_bytes: the number of bytes received
303 * @from: the node that sent the message
304 * @prio: the priority of the message
306 * This is one of the main functions of the protocol and it provides the
307 * means to receive a message through the protocol stack.
310 int frescan_recv(const frescan_recv_params_t *params,
314 frescan_node_t *from,
315 frescan_prio_t *prio)
318 frescan_prio_queue_t *pqueue;
320 frescan_packet_t *head, *packet;
322 if (params->flags & FRESCAN_SYNC) {
323 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
324 "receive a packet in blocking mode\n");
327 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
328 "receive a packet in non-blocking mode\n");
332 pqueue = the_networks[params->net].queues.rx_channel_queues
335 ret = frescan_pqueue_dequeue(pqueue, &head, prio, blocking);
337 ERROR ("could not dequeue packet\n");
342 if (blocking == false) {
343 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
344 "blocking false, no packets\n");
348 ERROR ("blocking true, and packet = null\n");
353 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
354 "traverse the list of packets for this message\n");
358 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
360 list_for_each_entry(packet, &head->msg_list, msg_list) {
361 // TODO: memory checks, delete the packets
362 memcpy(msg + *recv_bytes,
365 *recv_bytes += packet->frame->dlc;
367 *from = (frescan_node_t)frescan_id_get_field(packet->frame->id,
369 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
370 "ID Packet, dlc: %u, frame pool pos: %u, from:%u\n",
371 packet->frame->dlc, packet->frame->pool_pos, *from);
373 ret = can_framespool_free(packet->frame);
375 ERROR("could not free frame\n");
379 ret = frescan_packets_free(packet);
381 ERROR("could not free packet\n");
386 ret = frescan_packets_free(head);
388 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
391 ERROR("could not free head packet\n");
395 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
396 "received bytes: %u\n", *recv_bytes);
403 * frescan_hook_frame_recv - frame received hook
405 * This function will be called by the CAN driver's IRQ handler when a frame
406 * is received so we can store it in an appropiate queue.
408 * NOTE: in the future it could consist simply of signaling a semaphore to
409 * let a bottom half thread do the hard work.
413 static int frescan_hook_frame_recv (const struct can_chip_t *chip,
414 struct can_frame_t *frame)
417 uint32_t prio, dest, src, channel, frag_id, frag_flag;
418 frescan_packet_t *packet, *head;
419 frescan_prio_queue_t *pqueue;
424 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG, "received a frame, net=%d\n", net);
425 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
426 "%s %s, id=0x%X, dlc=%u, pool:%u\n",
427 (frame->is_extended_format) ? "Ext" : "Stnd",
428 (frame->is_rtr) ? "RTR Frame" : "DATA Frame",
433 for (i=0; i<frame->dlc; i++) {
434 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG,
435 "data[%d] = 0x%X;\n", i, frame->data[i]);
438 prio = frescan_id_get_field(frame->id, FRESCAN_FIELD_PRIO);
439 dest = frescan_id_get_field(frame->id, FRESCAN_FIELD_DEST);
440 src = frescan_id_get_field(frame->id, FRESCAN_FIELD_SRC);
441 channel = frescan_id_get_field(frame->id, FRESCAN_FIELD_CHAN);
442 frag_id = frescan_id_get_field(frame->id, FRESCAN_FIELD_FRAG_ID);
443 frag_flag = frescan_id_get_field(frame->id, FRESCAN_FIELD_FRAG_FLAG);
445 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
446 "prio:%u dest:%u src:%u chan:%u id:%u flag:%u\n",
447 prio, dest, src, channel, frag_id, frag_flag);
449 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG,
450 "enqueue the packet in ID queue\n");
451 packet = frescan_packets_alloc();
452 packet->frame = frame;
455 head = the_networks[net].id_fp_queues[prio];
457 head = the_networks[net].id_queues[frag_id];
461 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG ||
462 FRESCAN_FRAG_ENABLE_DEBUG,
463 "allocate head for id=%u\n", frag_id);
464 head = frescan_packets_alloc();
465 INIT_LIST_HEAD(&head->msg_list);
468 the_networks[net].id_fp_queues[prio] = head;
470 the_networks[net].id_queues[frag_id] = head;
474 list_add_tail(&packet->msg_list, &head->msg_list);
476 if (frag_flag == false) {
477 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG ||
478 FRESCAN_FRAG_ENABLE_DEBUG,
479 "message complete, move msg to channel\n");
480 // TODO: select the highest priority??
481 pqueue = the_networks[net].queues.rx_channel_queues[channel];
482 ret = frescan_pqueue_enqueue(pqueue, head, prio);
484 ERROR("could not enqueue message in channel queue\n");
489 the_networks[net].id_fp_queues[prio] = NULL;
491 the_networks[net].id_queues[frag_id] = NULL;
495 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG ||
496 FRESCAN_FRAG_ENABLE_DEBUG,
497 "message not complete, wait for more fragments\n");
500 // NOTE: don't forget to free the frame and the packet when it is
501 // read by the user application
507 * frescan_hook_frame_sent - frame sent hook
509 * This function will be called by the CAN driver's IRQ handler when a frame
510 * is sent through the CAN bus so we can enqueue another one, signal a
511 * semaphore, consume sporadic server capacity...
514 static int frescan_hook_frame_sent(const struct can_chip_t *chip)
517 frescan_packet_t *packet;
518 frescan_prio_queue_t *pqueue;
522 packet = the_networks[chip->minor].last_packet;
524 id = frescan_id_get_field(packet->frame->id,
525 FRESCAN_FIELD_FRAG_ID);
527 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG,
528 "frame sent, minor:%u flags:0x%X frag_id:0x%X\n",
529 chip->minor, packet->flags, id);
531 if (packet->flags & FRESCAN_SS) {
532 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG,
533 "calling frame_sent + program repl for id:%u\n", id);
535 ret = frescan_replenishment_program(chip->minor, id);
537 ERROR("could not program replenishment\n");
541 ret = frescan_servers_frame_sent(chip->minor, id);
543 ERROR("could not let the server a frame was sent\n");
548 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
549 "last packet buffer_pending_bytes=%u\n",
550 packet->buffer_pending_bytes);
552 if (packet->buffer_pending_bytes > 0) {
553 if (packet->flags & FRESCAN_FP) {
554 prio = frescan_id_get_field(packet->frame->id,
557 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
558 "requeue fp packet, prio:%u\n", prio);
560 pqueue = the_networks[chip->minor].queues.tx_fp_queue;
561 ret = frescan_pqueue_requeue(pqueue, packet, prio);
563 ERROR("could not requeue the packet\n");
566 } else if (packet->flags & FRESCAN_SS) {
567 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
568 "requeue server %u packet\n", id);
569 ret = frescan_servers_requeue(chip->minor, id, packet);
571 ERROR("could not requeue the packet\n");
575 ERROR("flags are not correct\n");
579 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
580 "all packet fragmets sent, freeing the packet\n");
582 ret = can_framespool_free(packet->frame);
584 ERROR ("could not free the frame\n");
588 ret = frescan_packets_free(packet);
590 ERROR ("could not free the packet\n");
594 // TODO: signal semaphore for send_sync
597 the_networks[chip->minor].last_packet = NULL;
599 ret = frescan_hw_buffer_update(chip->minor);
601 ERROR("could not update hw buffer\n");
609 * frescan_hook_frame_aborted - frame frame aborted hook
611 * This function will be called by the CAN driver's IRQ handler when a frame
612 * is aborted (because another frame with higher priority is waiting). We
613 * have to requeue the frame and update the buffer.
616 static int frescan_hook_frame_aborted(const struct can_chip_t *chip)
619 frescan_packet_t *packet;
620 frescan_prio_queue_t *pqueue;
624 packet = the_networks[chip->minor].last_packet;
626 id = frescan_id_get_field(packet->frame->id,
627 FRESCAN_FIELD_FRAG_ID);
629 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG,
630 "frame aborted, minor:%u flags:0x%X frag_id:0x%X\n",
631 chip->minor, packet->flags, id);
633 if (packet->flags & FRESCAN_FP) {
634 prio = frescan_id_get_field(packet->frame->id,
637 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
638 "requeue fp packet, prio:%u\n", prio);
640 pqueue = the_networks[chip->minor].queues.tx_fp_queue;
641 ret = frescan_pqueue_requeue(pqueue, packet, prio);
643 ERROR("could not requeue the packet\n");
646 } else if (packet->flags & FRESCAN_SS) {
647 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
648 "requeue server %u packet\n", id);
650 ret = frescan_servers_requeue(chip->minor, id, packet);
652 ERROR("could not requeue the packet\n");
656 ERROR("flags are not correct\n");
660 the_networks[chip->minor].last_packet = NULL;
662 ret = frescan_hw_buffer_update(chip->minor);
664 ERROR("could not update hw buffer\n");