]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - tests/tests_frescan/test_frescan_fp_send_basic.c
misc/ is in marte os library already now
[frescor/fna.git] / tests / tests_frescan / test_frescan_fp_send_basic.c
1 #include <stdio.h>  // perror
2 #include <stdlib.h> // exit
3 #include <unistd.h>   // sleep
4
5 #include "frescan.h"
6
7 #define PUT_ERROR(s) {perror (s); exit (-1);}
8
9 #define NETWORK 0
10
11 // #define SENDER
12 // #define ENABLE_LOGGING
13
14 #ifdef SENDER
15
16 #define LOCAL_NODE 1
17
18 #ifdef ENABLE_LOGGING
19
20 #include <drivers/console_switcher.h>
21 #include <misc/logger.h>
22 #include <assert.h>
23 #define LOG_DEVICE LOG_ETHERNET
24
25 #endif
26
27 static void pause(){
28         char key;
29         printf(" press Enter...");
30         key = getchar();
31 }
32
33 int main ()
34 {
35         int i, ret;
36         frescan_send_params_t params;
37         char msg[200];
38         int written;
39         frescan_init_params_t init_params;
40
41         init_params.net = NETWORK;
42         init_params.node = LOCAL_NODE;
43         init_params.tx_fp_max_prio = 10;
44         init_params.rx_num_of_channels = 5;
45         init_params.rx_channel_max_prio = NULL;
46
47 #ifdef ENABLE_LOGGING
48
49         ret = logger_init(LOG_DEVICE);
50         assert(ret == 0);
51
52         printf("Changing to membuffer console\n");
53         MEMBUFFER_CONSOLE_INIT();
54
55 #endif
56
57         ret = frescan_init(&init_params);
58         if (ret != 0) PUT_ERROR ("could not init FRESCAN");
59
60         printf("FRESCAN initialized (local node: %u)\n", LOCAL_NODE);
61
62         params.net      = NETWORK;
63         params.to       = 0;
64         params.channel  = 0;
65         params.flags    = FRESCAN_FP | FRESCAN_ASYNC;
66         params.prio     = 7;
67
68         while(1) {
69                 pause();
70                 for (i=0; i<=2; i++) {
71                         written = snprintf(msg, sizeof(msg), "his number is... %d", i);
72                         ret = frescan_send(&params, (uint8_t *)msg, written);
73                         if (ret != 0) PUT_ERROR ("could not send message\n");
74                         printf("SENT: %d\n", i);
75                 }
76 #ifdef ENABLE_LOGGING
77                 while (logger_manual_call() > 0);
78 #endif
79         }
80
81         return 0;
82 }
83
84 #else
85
86 #define LOCAL_NODE 0
87
88 int main ()
89 {
90         int ret;
91         frescan_recv_params_t params;
92         uint8_t msg[3000];
93         size_t recv_bytes;
94         frescan_node_t from;
95         frescan_init_params_t init_params;
96         frescan_prio_t prio;
97
98         init_params.net = NETWORK;
99         init_params.node = LOCAL_NODE;
100         init_params.tx_fp_max_prio = 10;
101         init_params.rx_num_of_channels = 5;
102         init_params.rx_channel_max_prio = NULL;
103
104         ret = frescan_init(&init_params);
105         if (ret != 0) PUT_ERROR ("could not init FRESCAN");
106
107         printf("FRESCAN initialized (local node: %u)\n", LOCAL_NODE);
108
109         params.net      = NETWORK;
110         params.channel  = 0;
111         params.flags    = FRESCAN_SYNC;
112
113         while (1) {
114                 printf("RECEIVING...\n");
115                 ret = frescan_recv(&params, (uint8_t *)msg, sizeof(msg),
116                                     &recv_bytes, &from, &prio);
117                 if (ret != 0) PUT_ERROR ("could not send message");
118
119                 msg[recv_bytes] = '\0';
120                 printf("RECEIVED: %s with prio:%u from:%u\n", msg, prio, from);
121
122 //                 for (i=0; i<recv_bytes; i++) {
123 //                         printf("msg[%d] = 0x%X;\n", i, msg[i]);
124 //                 }
125         }
126
127         return 0;
128 }
129
130 #endif