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 "frescan_debug.h"
8 #include <drivers/can.h>
9 #include <misc/linux_list.h>
11 #define DEBUG_ENABLE 1
13 // static void pause(){
15 // printf(" press Enter...");
22 frescan_queues_t queues;
23 frescan_packet_t *packet, *head;
24 struct can_frame_t *frame;
25 frescan_prio_queue_t *pqueue;
28 frescan_init_params_t init_params;
30 DEBUG(DEBUG_ENABLE, "init frames and packets pool\n");
32 // ret = can_framespool_init();
33 // if (ret != 0) FRESCAN_ERROR ("could not init frames pool\n");
35 ret = frescan_packets_init();
36 if (ret != 0) FRESCAN_ERROR ("could not init packet pool\n");
38 DEBUG(DEBUG_ENABLE, "init queues\n");
40 init_params.tx_fp_max_prio = 10;
41 init_params.rx_num_of_channels = 5;
42 init_params.rx_channel_max_prio = NULL;
44 ret = frescan_queues_init(&queues, &init_params);
45 if (ret != 0) FRESCAN_ERROR("could not initialize the queues\n");
49 DEBUG(DEBUG_ENABLE, "create queue of 5 packets\n");
52 frame = can_framespool_alloc();
53 if (frame == NULL) FRESCAN_ERROR ("frames pool is exhausted\n");
55 frame->is_extended_format = 1;
62 packet = frescan_packets_alloc();
63 if (packet == NULL) FRESCAN_ERROR ("could not alloc packet\n");
65 packet->frame = frame;
68 DEBUG(DEBUG_ENABLE, "head was null\n");
69 head = frescan_packets_alloc();
70 if (head == NULL) FRESCAN_ERROR ("could not alloc packet\n");
71 INIT_LIST_HEAD(&head->msg_list);
74 list_add_tail(&packet->msg_list, &head->msg_list);
77 DEBUG(DEBUG_ENABLE, "enqueue head in priority queue\n");
79 pqueue = queues.rx_channel_queues[0];
80 ret = frescan_pqueue_enqueue(pqueue, head, 7);
81 if (ret != 0) FRESCAN_ERROR ("could not enqueue packet\n");
85 DEBUG(DEBUG_ENABLE, "dequeue head from priority queue\n");
87 ret = frescan_pqueue_dequeue(pqueue, &head, &prio, blocking);
88 if (ret != 0) FRESCAN_ERROR ("could not dequeue packet\n");
91 if (blocking == false) {
92 FRESCAN_ERROR ("blocking false packet null\n");
94 FRESCAN_ERROR ("blocking true, and packet = null\n");
98 DEBUG(DEBUG_ENABLE, "traverse the list of packets\n");
100 list_for_each_entry(packet, &head->msg_list, msg_list) {
102 "ID Packet, dlc: %u, frame pool pos: %u\n",
103 packet->frame->dlc, packet->frame->pool_pos);
105 ret = can_framespool_free(packet->frame);
106 if (ret != 0) FRESCAN_ERROR("could not free frame\n");
108 ret = frescan_packets_free(packet);
109 if (ret != 0) FRESCAN_ERROR("could not free packet\n");
112 DEBUG(DEBUG_ENABLE, "free head\n");
114 ret = frescan_packets_free(head);
116 FRESCAN_ERROR("could not free head packet\n");