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_packets.h"
8 #include "frescan_debug.h"
9 #include <drivers/can.h>
10 #include <misc/linux_list.h>
12 #define DEBUG_ENABLE 1
14 // static void pause(){
16 // printf(" press Enter...");
23 frescan_queues_t queues;
24 frescan_packet_t *packet, *head;
25 struct can_frame_t *frame;
26 frescan_prio_queue_t *pqueue;
29 frescan_init_params_t init_params;
31 DEBUG(DEBUG_ENABLE, "init frames and packets pool\n");
33 // ret = can_framespool_init();
34 // if (ret != 0) FRESCAN_ERROR ("could not init frames pool\n");
36 ret = frescan_packets_init();
37 if (ret != 0) FRESCAN_ERROR ("could not init packet pool\n");
39 DEBUG(DEBUG_ENABLE, "init queues\n");
41 init_params.tx_fp_max_prio = 10;
42 init_params.rx_num_of_channels = 5;
43 init_params.rx_channel_max_prio = NULL;
45 ret = frescan_queues_init(&queues, &init_params);
46 if (ret != 0) FRESCAN_ERROR("could not initialize the queues\n");
50 DEBUG(DEBUG_ENABLE, "create queue of 5 packets\n");
53 frame = can_framespool_alloc();
54 if (frame == NULL) FRESCAN_ERROR ("frames pool is exhausted\n");
56 frame->is_extended_format = 1;
63 packet = frescan_packets_alloc();
64 if (packet == NULL) FRESCAN_ERROR ("could not alloc packet\n");
66 packet->frame = frame;
69 DEBUG(DEBUG_ENABLE, "head was null\n");
70 head = frescan_packets_alloc();
71 if (head == NULL) FRESCAN_ERROR ("could not alloc packet\n");
72 INIT_LIST_HEAD(&head->msg_list);
75 list_add_tail(&packet->msg_list, &head->msg_list);
78 DEBUG(DEBUG_ENABLE, "enqueue head in priority queue\n");
80 pqueue = queues.rx_channel_queues[0];
81 ret = frescan_pqueue_enqueue(pqueue, head, 7);
82 if (ret != 0) FRESCAN_ERROR ("could not enqueue packet\n");
86 DEBUG(DEBUG_ENABLE, "dequeue head from priority queue\n");
88 ret = frescan_pqueue_dequeue(pqueue, &head, &prio, blocking);
89 if (ret != 0) FRESCAN_ERROR ("could not dequeue packet\n");
92 if (blocking == false) {
93 FRESCAN_ERROR ("blocking false packet null\n");
95 FRESCAN_ERROR ("blocking true, and packet = null\n");
99 DEBUG(DEBUG_ENABLE, "traverse the list of packets\n");
101 list_for_each_entry(packet, &head->msg_list, msg_list) {
103 "ID Packet, dlc: %u, frame pool pos: %u\n",
104 packet->frame->dlc, packet->frame->pool_pos);
106 ret = can_framespool_free(packet->frame);
107 if (ret != 0) FRESCAN_ERROR("could not free frame\n");
109 ret = frescan_packets_free(packet);
110 if (ret != 0) FRESCAN_ERROR("could not free packet\n");
113 DEBUG(DEBUG_ENABLE, "free head\n");
115 ret = frescan_packets_free(head);
117 FRESCAN_ERROR("could not free head packet\n");