]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - tests/tests_frescan/test_frescan_queues.c
a556601994fb1c2aa1656271aabb8798b5412f11
[frescor/fna.git] / tests / tests_frescan / test_frescan_queues.c
1 #include <stdio.h>  // perror
2 #include <stdlib.h> // exit
3 #include <unistd.h> // sleep
4 #include <stdint.h> // uint32_t
5
6 #include "frescan_queues.h"
7 #include <drivers/can.h>
8 #include <misc/linux_list.h>
9
10 #define ERROR(s) {perror (s); exit (-1);}
11 #define DEBUG(enable,x,args...) if(enable) {printf("\t>> %s: " x, __func__ , ##args);} // pause();}
12
13 #define DEBUG_ENABLE 1
14
15 // static void pause(){
16 //         char key;
17 //         printf(" press Enter...");
18 //         key = getchar();
19 // }
20
21 int main ()
22 {
23         int i, ret;
24         frescan_queues_t queues;
25         frescan_packet_t *packet, *head;
26         struct can_frame_t *frame;
27         frescan_prio_queue_t *pqueue;
28         frescan_prio_t prio;
29         bool blocking;
30         frescan_init_params_t init_params;
31
32         DEBUG(DEBUG_ENABLE, "init frames and packets pool\n");
33
34         // ret = can_framespool_init();
35         // if (ret != 0) ERROR ("could not init frames pool\n");
36
37         ret = frescan_packets_init();
38         if (ret != 0) ERROR ("could not init packet pool\n");
39
40         DEBUG(DEBUG_ENABLE, "init queues\n");
41
42         init_params.tx_fp_max_prio = 10;
43         init_params.rx_num_of_channels = 5;
44         init_params.rx_channel_max_prio = NULL;
45
46         ret = frescan_queues_init(&queues, &init_params);
47         if (ret != 0) ERROR("could not initialize the queues\n");
48
49         head = NULL;
50
51         DEBUG(DEBUG_ENABLE, "create queue of 5 packets\n");
52
53         for (i=0; i<5; i++) {
54                 frame = can_framespool_alloc();
55                 if (frame == NULL) ERROR ("frames pool is exhausted\n");
56
57                 frame->is_extended_format = 1;
58                 frame->is_rtr = 0;
59                 frame->id = 0x696969;
60                 frame->dlc = 8;
61                 frame->data[0] = 69;
62                 frame->data[7] = 69;
63
64                 packet = frescan_packets_alloc();
65                 if (packet == NULL) ERROR ("could not alloc packet\n");
66
67                 packet->frame = frame;
68
69                 if (head == NULL) {
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);
74                 }
75
76                 list_add_tail(&packet->msg_list, &head->msg_list);
77         }
78
79         DEBUG(DEBUG_ENABLE, "enqueue head in priority queue\n");
80
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");
84
85         blocking = true;
86
87         DEBUG(DEBUG_ENABLE, "dequeue head from priority queue\n");
88
89         ret = frescan_pqueue_dequeue(pqueue, &head, &prio, blocking);
90         if (ret != 0) ERROR ("could not dequeue packet\n");
91
92         if (head == NULL) {
93                 if (blocking == false) {
94                         ERROR ("blocking false packet null\n");
95                 } else {
96                         ERROR ("blocking true, and packet = null\n");
97                 }
98         }
99
100         DEBUG(DEBUG_ENABLE, "traverse the list of packets\n");
101
102         list_for_each_entry(packet, &head->msg_list, msg_list) {
103                 DEBUG(DEBUG_ENABLE,
104                       "ID Packet, dlc: %u, frame pool pos: %u\n",
105                        packet->frame->dlc, packet->frame->pool_pos);
106
107                 ret = can_framespool_free(packet->frame);
108                 if (ret != 0) ERROR("could not free frame\n");
109
110                 ret = frescan_packets_free(packet);
111                 if (ret != 0) ERROR("could not free packet\n");
112         }
113
114         DEBUG(DEBUG_ENABLE, "free head\n");
115
116         ret = frescan_packets_free(head);
117         if (ret != 0) {
118                 ERROR("could not free head packet\n");
119                 return -1;
120         }
121
122         return 0;
123 }