1 #include <stdio.h> // perror
2 #include <stdlib.h> // exit
3 #include <unistd.h> // sleep
4 #include <stdint.h> // uint32_t
6 #include "frescan_queues.h"
7 #include <drivers/can.h>
8 #include <misc/linux_list.h>
10 #define ERROR(s) {perror (s); exit (-1);}
11 #define DEBUG(enable,x,args...) if(enable) {printf("\t>> %s: " x, __func__ , ##args);} // pause();}
13 #define DEBUG_ENABLE 1
15 // static void pause(){
17 // printf(" press Enter...");
24 frescan_queues_t queues;
25 frescan_packet_t *packet, *head;
26 struct can_frame_t *frame;
27 frescan_prio_queue_t *pqueue;
30 frescan_init_params_t init_params;
32 DEBUG(DEBUG_ENABLE, "init frames and packets pool\n");
34 // ret = can_framespool_init();
35 // if (ret != 0) ERROR ("could not init frames pool\n");
37 ret = frescan_packets_init();
38 if (ret != 0) ERROR ("could not init packet pool\n");
40 DEBUG(DEBUG_ENABLE, "init queues\n");
42 init_params.tx_fp_max_prio = 10;
43 init_params.rx_num_of_channels = 5;
44 init_params.rx_channel_max_prio = NULL;
46 ret = frescan_queues_init(&queues, &init_params);
47 if (ret != 0) ERROR("could not initialize the queues\n");
51 DEBUG(DEBUG_ENABLE, "create queue of 5 packets\n");
54 frame = can_framespool_alloc();
55 if (frame == NULL) ERROR ("frames pool is exhausted\n");
57 frame->is_extended_format = 1;
64 packet = frescan_packets_alloc();
65 if (packet == NULL) ERROR ("could not alloc packet\n");
67 packet->frame = frame;
70 DEBUG(DEBUG_ENABLE, "head was null\n");
71 head = frescan_packets_alloc();
72 if (head == NULL) ERROR ("could not alloc packet\n");
73 INIT_LIST_HEAD(&head->msg_list);
76 list_add_tail(&packet->msg_list, &head->msg_list);
79 DEBUG(DEBUG_ENABLE, "enqueue head in priority queue\n");
81 pqueue = queues.rx_channel_queues[0];
82 ret = frescan_pqueue_enqueue(pqueue, head, 7);
83 if (ret != 0) ERROR ("could not enqueue packet\n");
87 DEBUG(DEBUG_ENABLE, "dequeue head from priority queue\n");
89 ret = frescan_pqueue_dequeue(pqueue, &head, &prio, blocking);
90 if (ret != 0) ERROR ("could not dequeue packet\n");
93 if (blocking == false) {
94 ERROR ("blocking false packet null\n");
96 ERROR ("blocking true, and packet = null\n");
100 DEBUG(DEBUG_ENABLE, "traverse the list of packets\n");
102 list_for_each_entry(packet, &head->msg_list, msg_list) {
104 "ID Packet, dlc: %u, frame pool pos: %u\n",
105 packet->frame->dlc, packet->frame->pool_pos);
107 ret = can_framespool_free(packet->frame);
108 if (ret != 0) ERROR("could not free frame\n");
110 ret = frescan_packets_free(packet);
111 if (ret != 0) ERROR("could not free packet\n");
114 DEBUG(DEBUG_ENABLE, "free head\n");
116 ret = frescan_packets_free(head);
118 ERROR("could not free head packet\n");