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_packets.h"
39 static int frescan_hook_frame_recv (const struct can_chip_t *chip,
40 struct can_frame_t *frame);
42 static int frescan_hook_frame_sent(const struct can_chip_t *chip);
44 static int frescan_hook_frame_aborted(const struct can_chip_t *chip);
47 * frescan_init - initializes the network and the internal structures
49 * @params: the initialization parameters
51 * This function initializes the frescan network protocol.
53 * First it opens and configures the corresponding CAN chip device. For the
54 * CAN chip acceptance filter we use a dual filter configuration. The first
55 * filter is to set my local address address and the second one is to allow
58 * Once the CAN chip is configured we call the initialization functions of
59 * the rest of modules of frescan.
63 int frescan_init(frescan_init_params_t *params)
66 char can_path[PATH_MX];
67 struct ioctl_filters_t ioctl_filters;
68 struct can_filter_t filters[2];
70 snprintf(can_path, PATH_MX, "/dev/can%u", params->net);
72 DEBUG(FRESCAN_INIT_ENABLE_DEBUG, "open %s\n", can_path);
74 fd = open (can_path, O_RDWR);
76 ERROR ("could not open /dev/can%u\n", params->net);
80 DEBUG(FRESCAN_INIT_ENABLE_DEBUG, "set acceptance filters\n");
82 filters[0].mask = 0xFFFFFFFF;
83 frescan_id_set_field(&filters[0].mask, FRESCAN_FIELD_DEST,0x00);
85 frescan_id_set_field(&filters[0].code, FRESCAN_FIELD_DEST,params->node);
87 filters[1].mask = filters[0].mask;
89 frescan_id_set_field(&filters[1].code,
91 FRESCAN_BROADCAST_ADDR);
93 ioctl_filters.filters = filters;
94 ioctl_filters.len = 2;
96 ret = ioctl(fd, CAN_IOCTL_SET_FILTERS, &ioctl_filters);
98 ERROR ("ioctl CAN_IOCTL_SET_FILTERS failed /dev/can%u\n",
103 DEBUG(FRESCAN_INIT_ENABLE_DEBUG, "set tx, rx, abort hooks\n");
105 ret = ioctl(fd, CAN_IOCTL_SET_TX_HOOK, frescan_hook_frame_sent);
107 ERROR ("ioctl CAN_IOCTL_SET_TX_HOOK failed /dev/can%u\n",
112 ret = ioctl(fd, CAN_IOCTL_SET_RX_HOOK, frescan_hook_frame_recv);
114 ERROR ("ioctl CAN_IOCTL_SET_RX_HOOK failed /dev/can%u\n",
119 ret = ioctl(fd, CAN_IOCTL_SET_AB_HOOK, frescan_hook_frame_aborted);
121 ERROR ("ioctl CAN_IOCTL_SET_AB_HOOK failed /dev/can%u\n",
126 DEBUG(FRESCAN_INIT_ENABLE_DEBUG, "init the rest of modules\n");
128 ret = frescan_data_init(fd, params);
130 ERROR("could not initialize the global data\n");
134 ret = frescan_packets_init();
136 ERROR("could not initialize the packets pool\n");
140 ret = frescan_queues_init(&the_networks[params->net].queues, params);
142 ERROR("could not initialize the queues\n");
146 ret = frescan_servers_init(params->net);
148 ERROR("could not initialize the servers\n");
156 * frescan_send - send a message
158 * @params: the parameters needed by the protocol to send the message
159 * @msg: the message buffer
160 * @size: the size of the message
162 * This is one of the main functions of the protocol and it provides the
163 * means to send a message through the protocol stack.
166 int frescan_send(const frescan_send_params_t *params,
171 frescan_packet_t *packet;
172 frescan_prio_queue_t *pqueue;
174 DEBUG(FRESCAN_SEND_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
175 "checking arguments (msg size=%d)\n", size);
177 if ((params == NULL) || (msg == NULL) || (size == 0)) {
178 ERROR("arguments are not ok\n");
182 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "allocating a packet\n");
184 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
185 packet = frescan_packets_alloc();
186 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
188 if (packet == NULL) {
189 ERROR("could not allocate packet\n");
192 packet->flags = params->flags; // set the flags (to remember them)
194 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "allocating a frame\n");
196 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
197 packet->frame = can_framespool_alloc();
198 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
200 if (packet->frame == NULL) {
201 ERROR("could not allocate frame\n");
205 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "set values for the frame header\n");
206 packet->frame->is_extended_format = true;
207 packet->frame->is_rtr = false;
209 if (params->flags & FRESCAN_FP) {
210 // NOTE: frag id for fp is: FRESCAN_MX_IDS, so the servers can
211 // have IDs in the range (0 .. FRESCAN_MX_IDS-1)
212 frescan_id_set_field(&packet->frame->id,
213 FRESCAN_FIELD_FRAG_ID,
214 (uint32_t)FRESCAN_MX_IDS);
216 frescan_id_set_field(&packet->frame->id,
218 (uint32_t)params->prio);
220 // NOTE: the priority is put when the packet is dequeued
221 // and it is the priority of th server
222 frescan_id_set_field(&packet->frame->id,
223 FRESCAN_FIELD_FRAG_ID,
224 (uint32_t)params->ss);
227 frescan_id_set_field(&packet->frame->id,
229 (uint32_t)params->to);
231 frescan_id_set_field(&packet->frame->id,
233 (uint32_t)the_networks[params->net].local_node);
235 frescan_id_set_field(&packet->frame->id,
237 (uint32_t)params->channel);
239 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "set the packet data bytes\n");
240 if (params->flags & FRESCAN_ASYNC) {
241 // allocate a buffer and copy the data
242 // NOTE: instead of this we could use a chain of frames but
243 // i think it would be inefficient since each one can only
244 // hold 8 user bytes and we need to write its headers.
245 packet->buffer_head = (uint8_t *)malloc(size*sizeof(uint8_t));
246 memcpy(packet->buffer_head, msg, size);
248 packet->buffer_head = (uint8_t *)msg;
251 packet->buffer_read_pointer = packet->buffer_head;
252 packet->buffer_pending_bytes = size;
253 pqueue = the_networks[params->net].queues.tx_fp_queue;
255 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "enqueue the packet\n");
256 if (packet->flags & FRESCAN_FP) {
257 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
258 ret = frescan_pqueue_enqueue(pqueue, packet, params->prio);
259 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
262 ERROR("could not enqueue the packet\n");
266 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
267 ret = frescan_servers_enqueue(params->net, params->ss, packet);
268 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
271 ERROR("could not enqueue the packet\n");
276 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
277 ret = frescan_hw_buffer_update(params->net);
278 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
281 ERROR("could not update hw buffer\n");
289 * frescan_recv - receive a message
291 * @params: the parameters needed by the protocol to receive the message
292 * @msg: the message buffer
293 * @size: the size of the message buffer
294 * @recv_bytes: the number of bytes received
295 * @from: the node that sent the message
296 * @prio: the priority of the message
298 * This is one of the main functions of the protocol and it provides the
299 * means to receive a message through the protocol stack.
302 int frescan_recv(const frescan_recv_params_t *params,
306 frescan_node_t *from,
307 frescan_prio_t *prio)
310 frescan_prio_queue_t *pqueue;
312 frescan_packet_t *head, *packet;
314 if (params->flags & FRESCAN_SYNC) {
315 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
316 "receive a packet in blocking mode\n");
319 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
320 "receive a packet in non-blocking mode\n");
324 pqueue = the_networks[params->net].queues.rx_channel_queues
327 ret = frescan_pqueue_dequeue(pqueue, &head, prio, blocking);
329 ERROR ("could not dequeue packet\n");
334 if (blocking == false) {
335 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
336 "blocking false, no packets\n");
340 ERROR ("blocking true, and packet = null\n");
345 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
346 "traverse the list of packets for this message\n");
350 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
352 list_for_each_entry(packet, &head->msg_list, msg_list) {
353 // TODO: memory checks, delete the packets
354 memcpy(msg + *recv_bytes,
357 *recv_bytes += packet->frame->dlc;
359 *from = (frescan_node_t)frescan_id_get_field(packet->frame->id,
361 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
362 "ID Packet, dlc: %u, frame pool pos: %u, from:%u\n",
363 packet->frame->dlc, packet->frame->pool_pos, *from);
365 ret = can_framespool_free(packet->frame);
367 ERROR("could not free frame\n");
371 ret = frescan_packets_free(packet);
373 ERROR("could not free packet\n");
378 ret = frescan_packets_free(head);
380 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
383 ERROR("could not free head packet\n");
387 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
388 "received bytes: %u\n", *recv_bytes);
395 * frescan_hook_frame_recv - frame received hook
397 * This function will be called by the CAN driver's IRQ handler when a frame
398 * is received so we can store it in an appropiate queue.
400 * NOTE: in the future it could consist simply of signaling a semaphore to
401 * let a bottom half thread do the hard work.
405 static int frescan_hook_frame_recv (const struct can_chip_t *chip,
406 struct can_frame_t *frame)
409 uint32_t prio, dest, src, channel, frag_id, frag_flag;
410 frescan_packet_t *packet, *head;
411 frescan_prio_queue_t *pqueue;
416 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG, "received a frame, net=%d\n", net);
417 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
418 "%s %s, id=0x%X, dlc=%u, pool:%u\n",
419 (frame->is_extended_format) ? "Ext" : "Stnd",
420 (frame->is_rtr) ? "RTR Frame" : "DATA Frame",
425 for (i=0; i<frame->dlc; i++) {
426 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG,
427 "data[%d] = 0x%X;\n", i, frame->data[i]);
430 prio = frescan_id_get_field(frame->id, FRESCAN_FIELD_PRIO);
431 dest = frescan_id_get_field(frame->id, FRESCAN_FIELD_DEST);
432 src = frescan_id_get_field(frame->id, FRESCAN_FIELD_SRC);
433 channel = frescan_id_get_field(frame->id, FRESCAN_FIELD_CHAN);
434 frag_id = frescan_id_get_field(frame->id, FRESCAN_FIELD_FRAG_ID);
435 frag_flag = frescan_id_get_field(frame->id, FRESCAN_FIELD_FRAG_FLAG);
437 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
438 "prio:%u dest:%u src:%u chan:%u id:%u flag:%u\n",
439 prio, dest, src, channel, frag_id, frag_flag);
441 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG,
442 "enqueue the packet in ID queue\n");
443 packet = frescan_packets_alloc();
444 packet->frame = frame;
446 if (frag_id == FRESCAN_MX_IDS) {
447 head = the_networks[net].id_fp_queues[src][prio];
449 head = the_networks[net].id_queues[src][frag_id];
453 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG ||
454 FRESCAN_FRAG_ENABLE_DEBUG,
455 "allocate head for id=%u\n", frag_id);
456 head = frescan_packets_alloc();
457 INIT_LIST_HEAD(&head->msg_list);
459 if (frag_id == FRESCAN_MX_IDS) {
460 the_networks[net].id_fp_queues[src][prio] = head;
462 the_networks[net].id_queues[src][frag_id] = head;
466 list_add_tail(&packet->msg_list, &head->msg_list);
468 if (frag_flag == false) {
469 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG ||
470 FRESCAN_FRAG_ENABLE_DEBUG,
471 "message complete, move msg to channel\n");
472 // TODO: select the highest priority??
473 pqueue = the_networks[net].queues.rx_channel_queues[channel];
474 ret = frescan_pqueue_enqueue(pqueue, head, prio);
476 ERROR("could not enqueue message in channel queue\n");
480 if (frag_id == FRESCAN_MX_IDS) {
481 the_networks[net].id_fp_queues[src][prio] = NULL;
483 the_networks[net].id_queues[src][frag_id] = NULL;
487 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG ||
488 FRESCAN_FRAG_ENABLE_DEBUG,
489 "message not complete, wait for more fragments\n");
492 // NOTE: don't forget to free the frame and the packet when it is
493 // read by the user application
499 * frescan_hook_frame_sent - frame sent hook
501 * This function will be called by the CAN driver's IRQ handler when a frame
502 * is sent through the CAN bus so we can enqueue another one, signal a
503 * semaphore, consume sporadic server capacity...
506 static int frescan_hook_frame_sent(const struct can_chip_t *chip)
509 frescan_packet_t *packet;
510 frescan_prio_queue_t *pqueue;
514 packet = the_networks[chip->minor].last_packet;
516 id = frescan_id_get_field(packet->frame->id,
517 FRESCAN_FIELD_FRAG_ID);
519 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG,
520 "frame sent, minor:%u flags:0x%X frag_id:0x%X\n",
521 chip->minor, packet->flags, id);
523 if (packet->flags & FRESCAN_SS) {
524 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG,
525 "calling frame_sent + program repl for id:%u\n", id);
527 ret = frescan_servers_frame_sent(chip->minor, id, packet);
529 ERROR("could not let the server a frame was sent\n");
534 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
535 "last packet buffer_pending_bytes=%u\n",
536 packet->buffer_pending_bytes);
538 if (packet->buffer_pending_bytes > 0) {
539 if (packet->flags & FRESCAN_FP) {
540 prio = frescan_id_get_field(packet->frame->id,
543 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
544 "requeue fp packet, prio:%u\n", prio);
546 pqueue = the_networks[chip->minor].queues.tx_fp_queue;
547 ret = frescan_pqueue_requeue(pqueue, packet, prio);
549 ERROR("could not requeue the packet\n");
552 } else if (packet->flags & FRESCAN_SS) {
553 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
554 "requeue server %u packet\n", id);
555 ret = frescan_servers_requeue(chip->minor, id, packet);
557 ERROR("could not requeue the packet\n");
561 ERROR("flags are not correct\n");
565 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
566 "all packet fragmets sent, freeing the packet\n");
568 ret = can_framespool_free(packet->frame);
570 ERROR ("could not free the frame\n");
574 ret = frescan_packets_free(packet);
576 ERROR ("could not free the packet\n");
580 // TODO: signal semaphore for send_sync
583 the_networks[chip->minor].last_packet = NULL;
585 ret = frescan_hw_buffer_update(chip->minor);
587 ERROR("could not update hw buffer\n");
595 * frescan_hook_frame_aborted - frame frame aborted hook
597 * This function will be called by the CAN driver's IRQ handler when a frame
598 * is aborted (because another frame with higher priority is waiting). We
599 * have to requeue the frame and update the buffer.
602 static int frescan_hook_frame_aborted(const struct can_chip_t *chip)
605 frescan_packet_t *packet;
606 frescan_prio_queue_t *pqueue;
610 packet = the_networks[chip->minor].last_packet;
612 id = frescan_id_get_field(packet->frame->id,
613 FRESCAN_FIELD_FRAG_ID);
615 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG,
616 "frame aborted, minor:%u flags:0x%X frag_id:0x%X\n",
617 chip->minor, packet->flags, id);
619 if (packet->flags & FRESCAN_FP) {
620 prio = frescan_id_get_field(packet->frame->id,
623 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
624 "requeue fp packet, prio:%u\n", prio);
626 pqueue = the_networks[chip->minor].queues.tx_fp_queue;
627 ret = frescan_pqueue_requeue(pqueue, packet, prio);
629 ERROR("could not requeue the packet\n");
632 } else if (packet->flags & FRESCAN_SS) {
633 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
634 "requeue server %u packet\n", id);
636 ret = frescan_servers_requeue(chip->minor, id, packet);
638 ERROR("could not requeue the packet\n");
642 ERROR("flags are not correct\n");
646 the_networks[chip->minor].last_packet = NULL;
648 ret = frescan_hw_buffer_update(chip->minor);
650 ERROR("could not update hw buffer\n");