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_objects.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);
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 and rx 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 DEBUG(FRESCAN_INIT_ENABLE_DEBUG, "init the rest of modules\n");
121 ret = frescan_data_init(fd, params);
123 ERROR("could not initialize the global data\n");
127 ret = frescan_packets_init();
129 ERROR("could not initialize the packets pool\n");
133 ret = frescan_queues_init(&the_networks[params->net].queues, params);
135 ERROR("could not initialize the queues\n");
139 ret = frescan_servers_init(params->net);
141 ERROR("could not initialize the servers\n");
145 ret = frescan_replenishments_init(params->net);
147 ERROR("could not initialize the replenishments\n");
151 ret = frescan_replyobjects_init(FRESCAN_REPLY_OBJECTS_MX_CEILING);
153 ERROR("could not initialize the reply objects\n");
161 * frescan_send - send a message
163 * @params: the parameters needed by the protocol to send the message
164 * @msg: the message buffer
165 * @size: the size of the message
167 * This is one of the main functions of the protocol and it provides the
168 * means to send a message through the protocol stack.
171 int frescan_send(const frescan_send_params_t *params,
176 frescan_packet_t *packet;
177 frescan_prio_queue_t *pqueue;
179 DEBUG(FRESCAN_SEND_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
180 "checking arguments (msg size=%d)\n", size);
182 if ((params == NULL) || (msg == NULL) || (size == 0)) {
183 ERROR("arguments are not ok\n");
187 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "allocating a packet\n");
189 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
190 packet = frescan_packets_alloc();
191 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
193 if (packet == NULL) {
194 ERROR("could not allocate packet\n");
197 packet->flags = params->flags; // set the flags (to remember them)
199 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "allocating a frame\n");
201 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
202 packet->frame = can_framespool_alloc();
203 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
205 if (packet->frame == NULL) {
206 ERROR("could not allocate frame\n");
210 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "set values for the frame header\n");
211 packet->frame->is_extended_format = true;
212 packet->frame->is_rtr = false;
214 if (params->flags & FRESCAN_FP) {
215 // NOTE: frag id for fp is: FRESCAN_MX_IDS, so the servers can
216 // have IDs in the range (0 .. FRESCAN_MX_IDS-1)
217 frescan_id_set_field(&packet->frame->id,
218 FRESCAN_FIELD_FRAG_ID,
219 (uint32_t)FRESCAN_MX_IDS);
221 frescan_id_set_field(&packet->frame->id,
223 (uint32_t)params->prio);
225 // NOTE: the priority is put when the packet is dequeued
226 // and it is the priority of th server
227 frescan_id_set_field(&packet->frame->id,
228 FRESCAN_FIELD_FRAG_ID,
229 (uint32_t)params->ss);
232 frescan_id_set_field(&packet->frame->id,
234 (uint32_t)params->to);
236 frescan_id_set_field(&packet->frame->id,
238 (uint32_t)the_networks[params->net].local_node);
240 frescan_id_set_field(&packet->frame->id,
242 (uint32_t)params->channel);
244 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "set the packet data bytes\n");
245 if (params->flags & FRESCAN_ASYNC) {
246 // allocate a buffer and copy the data
247 // NOTE: instead of this we could use a chain of frames but
248 // i think it would be inefficient since each one can only
249 // hold 8 user bytes and we need to write its headers.
250 packet->buffer_head = (uint8_t *)malloc(size*sizeof(uint8_t));
251 memcpy(packet->buffer_head, msg, size);
253 packet->buffer_head = (uint8_t *)msg;
256 packet->buffer_read_pointer = packet->buffer_head;
257 packet->buffer_pending_bytes = size;
258 pqueue = the_networks[params->net].queues.tx_fp_queue;
260 DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "enqueue the packet\n");
261 if (packet->flags & FRESCAN_FP) {
262 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
263 ret = frescan_pqueue_enqueue(pqueue, packet, params->prio);
264 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
267 ERROR("could not enqueue the packet\n");
271 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
272 ret = frescan_servers_enqueue(params->net, params->ss, packet);
273 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
276 ERROR("could not enqueue the packet\n");
281 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
282 ret = frescan_hw_buffer_update(params->net);
283 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
286 ERROR("could not update hw buffer\n");
294 * frescan_recv - receive a message
296 * @params: the parameters needed by the protocol to receive the message
297 * @msg: the message buffer
298 * @size: the size of the message buffer
299 * @recv_bytes: the number of bytes received
300 * @from: the node that sent the message
301 * @prio: the priority of the message
303 * This is one of the main functions of the protocol and it provides the
304 * means to receive a message through the protocol stack.
307 int frescan_recv(const frescan_recv_params_t *params,
311 frescan_node_t *from,
312 frescan_prio_t *prio)
315 frescan_prio_queue_t *pqueue;
317 frescan_packet_t *head, *packet;
319 if (params->flags & FRESCAN_SYNC) {
320 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
321 "receive a packet in blocking mode\n");
324 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
325 "receive a packet in non-blocking mode\n");
329 pqueue = the_networks[params->net].queues.rx_channel_queues
332 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
333 ret = frescan_pqueue_dequeue(pqueue, &head, prio, blocking);
334 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
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
508 * frescan_hook_frame_sent - frame sent hook
510 * This function will be called by the CAN driver's IRQ handler when a frame
511 * is sent through the CAN bus so we can enqueue another one, signal a
512 * semaphore, consume sporadic server capacity...
515 static int frescan_hook_frame_sent(const struct can_chip_t *chip)
518 frescan_packet_t *packet;
519 frescan_prio_queue_t *pqueue;
523 packet = the_networks[chip->minor].last_packet;
525 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG,
526 "frame sent, minor:%u flags:0x%X\n",
527 chip->minor, packet->flags);
529 if (packet->flags & FRESCAN_SS) {
530 id = frescan_id_get_field(packet->frame->id,
531 FRESCAN_FIELD_FRAG_ID);
533 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG,
534 "calling frame_sent + program repl for id:%u\n", id);
536 ret = frescan_replenishment_program(chip->minor, id);
538 ERROR("could not program replenishment\n");
542 ret = frescan_servers_frame_sent(chip->minor, id);
544 ERROR("could not let the server a frame was sent\n");
549 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
550 "last packet buffer_pending_bytes=%u\n",
551 packet->buffer_pending_bytes);
553 if (packet->buffer_pending_bytes > 0) {
554 if (packet->flags & FRESCAN_FP) {
555 prio = frescan_id_get_field(packet->frame->id,
558 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
559 "requeue fp packet, prio:%u\n", prio);
561 pqueue = the_networks[chip->minor].queues.tx_fp_queue;
562 ret = frescan_pqueue_requeue(pqueue, packet, prio);
564 ERROR("could not requeue the packet\n");
567 } else if (packet->flags & FRESCAN_SS) {
568 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
569 "requeue server %u packet\n", id);
570 ret = frescan_servers_requeue(chip->minor, id, packet);
572 ERROR("could not requeue the packet\n");
576 ERROR("flags are not correct\n");
580 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
581 "all packet fragmets sent, freeing the packet\n");
583 ret = can_framespool_free(packet->frame);
585 ERROR ("could not free the frame\n");
589 ret = frescan_packets_free(packet);
591 ERROR ("could not free the packet\n");
595 // TODO: signal semaphores, server consume budget...
598 the_networks[chip->minor].last_packet = NULL;
600 ret = frescan_hw_buffer_update(chip->minor);
602 ERROR("could not update hw buffer\n");