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