-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.c
149 lines (125 loc) · 3.42 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Wirepas Oy licensed under Apache License, Version 2.0
*
* See file LICENSE for full license details.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "wpc.h"
#define LOG_MODULE_NAME "Main"
#define MAX_LOG_LEVEL INFO_LOG_LEVEL
#include "logger.h"
// Default serial port
char * port_name = "/dev/ttyACM0";
// Node configuration
#define NODE_ADDRESS 0x1
#define NODE_ROLE APP_ROLE_SINK
#define NETWORK_CHANNEL 0x2
#define NETWORK_ADDRESS 0xABCDEF
static bool onDataReceived(const uint8_t * bytes,
size_t num_bytes,
app_addr_t src_addr,
app_addr_t dst_addr,
app_qos_e qos,
uint8_t src_ep,
uint8_t dst_ep,
uint32_t travel_time,
uint8_t hop_count,
unsigned long long timestamp_ms_epoch)
{
// Handle Data received here
LOGI("Data received on EP %d of len %d with id %d from 0x%x to 0x%x\n",
dst_ep,
num_bytes,
bytes[0],
src_addr,
dst_addr);
return true;
}
int main(int argc, char * argv[])
{
app_res_e res;
unsigned long bitrate = DEFAULT_BITRATE;
if (argc > 1)
{
port_name = argv[1];
}
if (argc > 2)
{
bitrate = strtoul(argv[2], NULL, 0);
}
if (WPC_initialize(port_name, bitrate) != APP_RES_OK)
return -1;
res = WPC_stop_stack();
sleep(2);
if (res != APP_RES_OK && res != APP_RES_STACK_ALREADY_STOPPED)
{
LOGE("Cannot stop stack %d\n", res);
goto exit_on_error;
}
LOGI("Stack is stopped\n");
// Register for all datas
WPC_register_for_data(onDataReceived);
// Configure the node
if (WPC_set_node_address(NODE_ADDRESS) != APP_RES_OK)
{
LOGE("Cannot set node address\n");
goto exit_on_error;
}
LOGI("Node address set\n");
if (WPC_set_network_channel(NETWORK_CHANNEL) != APP_RES_OK)
{
LOGE("Cannot set network channel\n");
goto exit_on_error;
}
LOGI("Network channel set\n");
if (WPC_set_role(NODE_ROLE) != APP_RES_OK)
{
LOGE("Cannot set node role\n");
goto exit_on_error;
}
LOGI("Current role set\n");
if (WPC_set_network_address(NETWORK_ADDRESS) != APP_RES_OK)
{
LOGE("Cannot set network address\n");
goto exit_on_error;
}
LOGI("Network address set\n");
uint8_t config[100] = "Test Mesh API\0";
uint8_t seq;
uint16_t interval;
uint8_t max_size;
if (WPC_get_app_config_data_size(&max_size) != APP_RES_OK)
{
LOGE("Cannot determine app config max size\n");
goto exit_on_error;
}
LOGI("App config max size is %d\n", max_size);
if (WPC_get_app_config_data(&seq, &interval, config, 100) == APP_RES_NO_CONFIG)
{
seq = 1;
interval = 30;
}
if (NODE_ROLE == APP_ROLE_SINK &&
WPC_set_app_config_data(seq + 1, 30, config, max_size) != APP_RES_OK)
{
LOGE("Cannot set config data\n");
goto exit_on_error;
}
// Node is configured, start it
res = WPC_start_stack();
if (res != APP_RES_OK)
{
LOGE("Cannot start node error=%d\n", res);
return -1;
}
LOGI("Stack is started\n");
// Then wait !
for (;;)
pause();
exit_on_error:
WPC_close();
return -1;
}