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