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