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_reply_objs.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");
160 ret = frescan_replyobjects_init(FRESCAN_REPLY_OBJECTS_MX_CEILING);
162 ERROR("could not initialize the reply objects\n");
170 * frescan_send - send a message
172 * @params: the parameters needed by the protocol to send the message
173 * @msg: the message buffer
174 * @size: the size of the message
176 * This is one of the main functions of the protocol and it provides the
177 * means to send a message through the protocol stack.
180 int frescan_send(const frescan_send_params_t *params,
185 frescan_packet_t *packet;
186 frescan_prio_queue_t *pqueue;
188 DEBUG(FRESCAN_SEND_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
189 "checking arguments (msg size=%d)\n", size);
191 if ((params == NULL) || (msg == NULL) || (size == 0)) {
192 ERROR("arguments are not ok\n");
196 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "allocating a packet\n");
198 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
199 packet = frescan_packets_alloc();
200 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
202 if (packet == NULL) {
203 ERROR("could not allocate packet\n");
206 packet->flags = params->flags; // set the flags (to remember them)
208 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "allocating a frame\n");
210 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
211 packet->frame = can_framespool_alloc();
212 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
214 if (packet->frame == NULL) {
215 ERROR("could not allocate frame\n");
219 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "set values for the frame header\n");
220 packet->frame->is_extended_format = true;
221 packet->frame->is_rtr = false;
223 if (params->flags & FRESCAN_FP) {
224 // NOTE: frag id for fp is: FRESCAN_MX_IDS, so the servers can
225 // have IDs in the range (0 .. FRESCAN_MX_IDS-1)
226 frescan_id_set_field(&packet->frame->id,
227 FRESCAN_FIELD_FRAG_ID,
228 (uint32_t)FRESCAN_MX_IDS);
230 frescan_id_set_field(&packet->frame->id,
232 (uint32_t)params->prio);
234 // NOTE: the priority is put when the packet is dequeued
235 // and it is the priority of th server
236 frescan_id_set_field(&packet->frame->id,
237 FRESCAN_FIELD_FRAG_ID,
238 (uint32_t)params->ss);
241 frescan_id_set_field(&packet->frame->id,
243 (uint32_t)params->to);
245 frescan_id_set_field(&packet->frame->id,
247 (uint32_t)the_networks[params->net].local_node);
249 frescan_id_set_field(&packet->frame->id,
251 (uint32_t)params->channel);
253 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "set the packet data bytes\n");
254 if (params->flags & FRESCAN_ASYNC) {
255 // allocate a buffer and copy the data
256 // NOTE: instead of this we could use a chain of frames but
257 // i think it would be inefficient since each one can only
258 // hold 8 user bytes and we need to write its headers.
259 packet->buffer_head = (uint8_t *)malloc(size*sizeof(uint8_t));
260 memcpy(packet->buffer_head, msg, size);
262 packet->buffer_head = (uint8_t *)msg;
265 packet->buffer_read_pointer = packet->buffer_head;
266 packet->buffer_pending_bytes = size;
267 pqueue = the_networks[params->net].queues.tx_fp_queue;
269 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "enqueue the packet\n");
270 if (packet->flags & FRESCAN_FP) {
271 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
272 ret = frescan_pqueue_enqueue(pqueue, packet, params->prio);
273 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
276 ERROR("could not enqueue the packet\n");
280 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
281 ret = frescan_servers_enqueue(params->net, params->ss, packet);
282 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
285 ERROR("could not enqueue the packet\n");
290 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
291 ret = frescan_hw_buffer_update(params->net);
292 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
295 ERROR("could not update hw buffer\n");
303 * frescan_recv - receive a message
305 * @params: the parameters needed by the protocol to receive the message
306 * @msg: the message buffer
307 * @size: the size of the message buffer
308 * @recv_bytes: the number of bytes received
309 * @from: the node that sent the message
310 * @prio: the priority of the message
312 * This is one of the main functions of the protocol and it provides the
313 * means to receive a message through the protocol stack.
316 int frescan_recv(const frescan_recv_params_t *params,
320 frescan_node_t *from,
321 frescan_prio_t *prio)
324 frescan_prio_queue_t *pqueue;
326 frescan_packet_t *head, *packet;
328 if (params->flags & FRESCAN_SYNC) {
329 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
330 "receive a packet in blocking mode\n");
333 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
334 "receive a packet in non-blocking mode\n");
338 pqueue = the_networks[params->net].queues.rx_channel_queues
341 ret = frescan_pqueue_dequeue(pqueue, &head, prio, blocking);
343 ERROR ("could not dequeue packet\n");
348 if (blocking == false) {
349 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
350 "blocking false, no packets\n");
354 ERROR ("blocking true, and packet = null\n");
359 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
360 "traverse the list of packets for this message\n");
364 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
366 list_for_each_entry(packet, &head->msg_list, msg_list) {
367 // TODO: memory checks, delete the packets
368 memcpy(msg + *recv_bytes,
371 *recv_bytes += packet->frame->dlc;
373 *from = (frescan_node_t)frescan_id_get_field(packet->frame->id,
375 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
376 "ID Packet, dlc: %u, frame pool pos: %u, from:%u\n",
377 packet->frame->dlc, packet->frame->pool_pos, *from);
379 ret = can_framespool_free(packet->frame);
381 ERROR("could not free frame\n");
385 ret = frescan_packets_free(packet);
387 ERROR("could not free packet\n");
392 ret = frescan_packets_free(head);
394 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
397 ERROR("could not free head packet\n");
401 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
402 "received bytes: %u\n", *recv_bytes);
409 * frescan_hook_frame_recv - frame received hook
411 * This function will be called by the CAN driver's IRQ handler when a frame
412 * is received so we can store it in an appropiate queue.
414 * NOTE: in the future it could consist simply of signaling a semaphore to
415 * let a bottom half thread do the hard work.
419 static int frescan_hook_frame_recv (const struct can_chip_t *chip,
420 struct can_frame_t *frame)
423 uint32_t prio, dest, src, channel, frag_id, frag_flag;
424 frescan_packet_t *packet, *head;
425 frescan_prio_queue_t *pqueue;
430 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG, "received a frame, net=%d\n", net);
431 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
432 "%s %s, id=0x%X, dlc=%u, pool:%u\n",
433 (frame->is_extended_format) ? "Ext" : "Stnd",
434 (frame->is_rtr) ? "RTR Frame" : "DATA Frame",
439 for (i=0; i<frame->dlc; i++) {
440 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG,
441 "data[%d] = 0x%X;\n", i, frame->data[i]);
444 prio = frescan_id_get_field(frame->id, FRESCAN_FIELD_PRIO);
445 dest = frescan_id_get_field(frame->id, FRESCAN_FIELD_DEST);
446 src = frescan_id_get_field(frame->id, FRESCAN_FIELD_SRC);
447 channel = frescan_id_get_field(frame->id, FRESCAN_FIELD_CHAN);
448 frag_id = frescan_id_get_field(frame->id, FRESCAN_FIELD_FRAG_ID);
449 frag_flag = frescan_id_get_field(frame->id, FRESCAN_FIELD_FRAG_FLAG);
451 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
452 "prio:%u dest:%u src:%u chan:%u id:%u flag:%u\n",
453 prio, dest, src, channel, frag_id, frag_flag);
455 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG,
456 "enqueue the packet in ID queue\n");
457 packet = frescan_packets_alloc();
458 packet->frame = frame;
461 head = the_networks[net].id_fp_queues[prio];
463 head = the_networks[net].id_queues[frag_id];
467 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG ||
468 FRESCAN_FRAG_ENABLE_DEBUG,
469 "allocate head for id=%u\n", frag_id);
470 head = frescan_packets_alloc();
471 INIT_LIST_HEAD(&head->msg_list);
474 the_networks[net].id_fp_queues[prio] = head;
476 the_networks[net].id_queues[frag_id] = head;
480 list_add_tail(&packet->msg_list, &head->msg_list);
482 if (frag_flag == false) {
483 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG ||
484 FRESCAN_FRAG_ENABLE_DEBUG,
485 "message complete, move msg to channel\n");
486 // TODO: select the highest priority??
487 pqueue = the_networks[net].queues.rx_channel_queues[channel];
488 ret = frescan_pqueue_enqueue(pqueue, head, prio);
490 ERROR("could not enqueue message in channel queue\n");
495 the_networks[net].id_fp_queues[prio] = NULL;
497 the_networks[net].id_queues[frag_id] = NULL;
501 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG ||
502 FRESCAN_FRAG_ENABLE_DEBUG,
503 "message not complete, wait for more fragments\n");
506 // NOTE: don't forget to free the frame and the packet when it is
507 // read by the user application
513 * frescan_hook_frame_sent - frame sent hook
515 * This function will be called by the CAN driver's IRQ handler when a frame
516 * is sent through the CAN bus so we can enqueue another one, signal a
517 * semaphore, consume sporadic server capacity...
520 static int frescan_hook_frame_sent(const struct can_chip_t *chip)
523 frescan_packet_t *packet;
524 frescan_prio_queue_t *pqueue;
528 packet = the_networks[chip->minor].last_packet;
530 id = frescan_id_get_field(packet->frame->id,
531 FRESCAN_FIELD_FRAG_ID);
533 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG,
534 "frame sent, minor:%u flags:0x%X frag_id:0x%X\n",
535 chip->minor, packet->flags, id);
537 if (packet->flags & FRESCAN_SS) {
538 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG,
539 "calling frame_sent + program repl for id:%u\n", id);
541 ret = frescan_replenishment_program(chip->minor, id);
543 ERROR("could not program replenishment\n");
547 ret = frescan_servers_frame_sent(chip->minor, id);
549 ERROR("could not let the server a frame was sent\n");
554 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
555 "last packet buffer_pending_bytes=%u\n",
556 packet->buffer_pending_bytes);
558 if (packet->buffer_pending_bytes > 0) {
559 if (packet->flags & FRESCAN_FP) {
560 prio = frescan_id_get_field(packet->frame->id,
563 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
564 "requeue fp packet, prio:%u\n", prio);
566 pqueue = the_networks[chip->minor].queues.tx_fp_queue;
567 ret = frescan_pqueue_requeue(pqueue, packet, prio);
569 ERROR("could not requeue the packet\n");
572 } else if (packet->flags & FRESCAN_SS) {
573 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
574 "requeue server %u packet\n", id);
575 ret = frescan_servers_requeue(chip->minor, id, packet);
577 ERROR("could not requeue the packet\n");
581 ERROR("flags are not correct\n");
585 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
586 "all packet fragmets sent, freeing the packet\n");
588 ret = can_framespool_free(packet->frame);
590 ERROR ("could not free the frame\n");
594 ret = frescan_packets_free(packet);
596 ERROR ("could not free the packet\n");
600 // TODO: signal semaphore for send_sync
603 the_networks[chip->minor].last_packet = NULL;
605 ret = frescan_hw_buffer_update(chip->minor);
607 ERROR("could not update hw buffer\n");
615 * frescan_hook_frame_aborted - frame frame aborted hook
617 * This function will be called by the CAN driver's IRQ handler when a frame
618 * is aborted (because another frame with higher priority is waiting). We
619 * have to requeue the frame and update the buffer.
622 static int frescan_hook_frame_aborted(const struct can_chip_t *chip)
625 frescan_packet_t *packet;
626 frescan_prio_queue_t *pqueue;
630 packet = the_networks[chip->minor].last_packet;
632 id = frescan_id_get_field(packet->frame->id,
633 FRESCAN_FIELD_FRAG_ID);
635 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG,
636 "frame aborted, minor:%u flags:0x%X frag_id:0x%X\n",
637 chip->minor, packet->flags, id);
639 if (packet->flags & FRESCAN_FP) {
640 prio = frescan_id_get_field(packet->frame->id,
643 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
644 "requeue fp packet, prio:%u\n", prio);
646 pqueue = the_networks[chip->minor].queues.tx_fp_queue;
647 ret = frescan_pqueue_requeue(pqueue, packet, prio);
649 ERROR("could not requeue the packet\n");
652 } else if (packet->flags & FRESCAN_SS) {
653 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
654 "requeue server %u packet\n", id);
656 ret = frescan_servers_requeue(chip->minor, id, packet);
658 ERROR("could not requeue the packet\n");
662 ERROR("flags are not correct\n");
666 the_networks[chip->minor].last_packet = NULL;
668 ret = frescan_hw_buffer_update(chip->minor);
670 ERROR("could not update hw buffer\n");