]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - tests/tests_frescan/test_frescan_queues.c
Do not enter unnecessary subdirectories
[frescor/frsh-forb.git] / tests / tests_frescan / test_frescan_queues.c
1 //----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2009 by the FRESCOR consortium:
3 //
4 //    Universidad de Cantabria,              SPAIN
5 //    University of York,                    UK
6 //    Scuola Superiore Sant'Anna,            ITALY
7 //    Kaiserslautern University,             GERMANY
8 //    Univ. Politecnica  Valencia,           SPAIN
9 //    Czech Technical University in Prague,  CZECH REPUBLIC
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //
16 //    See http://www.frescor.org
17 //
18 //        The FRESCOR project (FP6/2005/IST/5-034026) is funded
19 //        in part by the European Union Sixth Framework Programme
20 //        The European Union is not liable of any use that may be
21 //        made of this code.
22 //
23 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 // This file is part of FNA (Frescor Network Adaptation)
32 //
33 // FNA is free software; you can redistribute it and/or modify it
34 // under terms of the GNU General Public License as published by the
35 // Free Software Foundation; either version 2, or (at your option) any
36 // later version.  FNA is distributed in the hope that it will be
37 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
38 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 // General Public License for more details. You should have received a
40 // copy of the GNU General Public License along with FNA; see file
41 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
42 // Cambridge, MA 02139, USA.
43 //
44 // As a special exception, including FNA header files in a file,
45 // instantiating FNA generics or templates, or linking other files
46 // with FNA objects to produce an executable application, does not
47 // by itself cause the resulting executable application to be covered
48 // by the GNU General Public License. This exception does not
49 // however invalidate any other reasons why the executable file might be
50 // covered by the GNU Public License.
51 // -----------------------------------------------------------------------
52 #include <stdio.h>  // perror
53 #include <stdlib.h> // exit
54 #include <unistd.h> // sleep
55 #include <stdint.h> // uint32_t
56
57 #include "frescan_queues.h"
58 #include "frescan_packets.h"
59 #include "frescan_debug.h"
60 #include <drivers/can.h>
61 #include <misc/linux_list.h>
62
63 #define DEBUG_ENABLE 1
64
65 // static void pause(){
66 //         char key;
67 //         printf(" press Enter...");
68 //         key = getchar();
69 // }
70
71 int main ()
72 {
73         int i, ret;
74         frescan_queues_t queues;
75         frescan_packet_t *packet, *head;
76         struct can_frame_t *frame;
77         frescan_prio_queue_t *pqueue;
78         frescan_prio_t prio;
79         bool blocking;
80         frescan_init_params_t init_params;
81
82         DEBUG(DEBUG_ENABLE, "init frames and packets pool\n");
83
84         // ret = can_framespool_init();
85         // if (ret != 0) FRESCAN_ERROR ("could not init frames pool\n");
86
87         ret = frescan_packets_init();
88         if (ret != 0) FRESCAN_ERROR ("could not init packet pool\n");
89
90         DEBUG(DEBUG_ENABLE, "init queues\n");
91
92         init_params.tx_fp_max_prio = 10;
93         init_params.rx_num_of_channels = 5;
94         init_params.rx_channel_max_prio = NULL;
95
96         ret = frescan_queues_init(&queues, &init_params);
97         if (ret != 0) FRESCAN_ERROR("could not initialize the queues\n");
98
99         head = NULL;
100
101         DEBUG(DEBUG_ENABLE, "create queue of 5 packets\n");
102
103         for (i=0; i<5; i++) {
104                 frame = can_framespool_alloc();
105                 if (frame == NULL) FRESCAN_ERROR ("frames pool is exhausted\n");
106
107                 frame->is_extended_format = 1;
108                 frame->is_rtr = 0;
109                 frame->id = 0x696969;
110                 frame->dlc = 8;
111                 frame->data[0] = 69;
112                 frame->data[7] = 69;
113
114                 packet = frescan_packets_alloc();
115                 if (packet == NULL) FRESCAN_ERROR ("could not alloc packet\n");
116
117                 packet->frame = frame;
118
119                 if (head == NULL) {
120                         DEBUG(DEBUG_ENABLE, "head was null\n");
121                         head = frescan_packets_alloc();
122                         if (head == NULL) FRESCAN_ERROR ("could not alloc packet\n");
123                         INIT_LIST_HEAD(&head->msg_list);
124                 }
125
126                 list_add_tail(&packet->msg_list, &head->msg_list);
127         }
128
129         DEBUG(DEBUG_ENABLE, "enqueue head in priority queue\n");
130
131         pqueue = queues.rx_channel_queues[0];
132         ret = frescan_pqueue_enqueue(pqueue, head, 7);
133         if (ret != 0) FRESCAN_ERROR ("could not enqueue packet\n");
134
135         blocking = true;
136
137         DEBUG(DEBUG_ENABLE, "dequeue head from priority queue\n");
138
139         ret = frescan_pqueue_dequeue(pqueue, &head, &prio, blocking);
140         if (ret != 0) FRESCAN_ERROR ("could not dequeue packet\n");
141
142         if (head == NULL) {
143                 if (blocking == false) {
144                         FRESCAN_ERROR ("blocking false packet null\n");
145                 } else {
146                         FRESCAN_ERROR ("blocking true, and packet = null\n");
147                 }
148         }
149
150         DEBUG(DEBUG_ENABLE, "traverse the list of packets\n");
151
152         list_for_each_entry(packet, &head->msg_list, msg_list) {
153                 DEBUG(DEBUG_ENABLE,
154                       "ID Packet, dlc: %u, frame pool pos: %u\n",
155                        packet->frame->dlc, packet->frame->pool_pos);
156
157                 ret = can_framespool_free(packet->frame);
158                 if (ret != 0) FRESCAN_ERROR("could not free frame\n");
159
160                 ret = frescan_packets_free(packet);
161                 if (ret != 0) FRESCAN_ERROR("could not free packet\n");
162         }
163
164         DEBUG(DEBUG_ENABLE, "free head\n");
165
166         ret = frescan_packets_free(head);
167         if (ret != 0) {
168                 FRESCAN_ERROR("could not free head packet\n");
169                 return -1;
170         }
171
172         return 0;
173 }