-
Notifications
You must be signed in to change notification settings - Fork 16
/
user_app.c
273 lines (239 loc) · 8.64 KB
/
user_app.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "rui.h"
#include "at.h"
#ifndef __RUI_LOG_PRINT_MERGE
#define __RUI_LOG_PRINT_MERGE
#define RUI_LOG_PRINTF_MERGE(fmt, args...); {uart_log_printf(fmt, ##args);RUI_LOG_PRINTF(fmt, ##args);}
#endif
/* User Code */
//#define FLASH_WRITE_TEST // If it is commented out, flash write will not be performed
uint8_t flash_buf[128] = {0};
uint16_t flash_len = 128;
void print_hex(uint8_t *pdata, uint16_t len)
{
while (len--)
uart_log_printf("%02X", *pdata++);
}
void setup(void)
{
RUI_RETURN_STATUS ret_code;
#ifdef FLASH_WRITE_TEST
for(uint32_t i=0; i<sizeof(flash_buf); i++)
flash_buf[i] = i;
// Write data to flash
ret_code = rui_flash_write(RUI_FLASH_USER, flash_buf, flash_len);
if (ret_code != RUI_STATUS_OK)
uart_log_printf("Flash write error! %d\r\n", ret_code);
#endif
// Read data from flash
ret_code = rui_flash_read(RUI_FLASH_USER, flash_buf, flash_len);
if (ret_code != RUI_STATUS_OK)
uart_log_printf("Flash write error! %d\r\n", ret_code);
else
{
uart_log_printf("Read data is:\r\n");
print_hex(flash_buf, flash_len);
uart_log_printf("\r\n");
}
}
/* End User Code */
/*******************************************************************************************
* LoRaMac callback functions
* * void LoRaReceive_callback(RUI_RECEIVE_T* Receive_datapackage);//LoRaWAN callback if receive data
* * void LoRaP2PReceive_callback(RUI_LORAP2P_RECEIVE_T *Receive_P2Pdatapackage);//LoRaP2P callback if receive data
* * void LoRaWANJoined_callback(uint32_t status);//LoRaWAN callback after join server request
* * void LoRaWANSendsucceed_callback(RUI_MCPS_T status);//LoRaWAN call back after send data complete
* *****************************************************************************************/
void LoRaReceive_callback(RUI_RECEIVE_T* Receive_datapackage)
{
char hex_str[3] = {0};
RUI_LOG_PRINTF_MERGE("at+recv=%d,%d,%d,%d", Receive_datapackage->Port, Receive_datapackage->Rssi, Receive_datapackage->Snr, Receive_datapackage->BufferSize);
if ((Receive_datapackage->Buffer != NULL) && Receive_datapackage->BufferSize) {
RUI_LOG_PRINTF_MERGE(":");
for (int i = 0; i < Receive_datapackage->BufferSize; i++) {
sprintf(hex_str, "%02x", Receive_datapackage->Buffer[i]);
RUI_LOG_PRINTF_MERGE("%s", hex_str);
}
}
RUI_LOG_PRINTF_MERGE("\r\n");
}
void LoRaP2PReceive_callback(RUI_LORAP2P_RECEIVE_T *Receive_P2Pdatapackage)
{
char hex_str[3]={0};
RUI_LOG_PRINTF_MERGE("at+recv=%d,%d,%d:", Receive_P2Pdatapackage -> Rssi, Receive_P2Pdatapackage -> Snr, Receive_P2Pdatapackage -> BufferSize);
for(int i=0;i < Receive_P2Pdatapackage->BufferSize; i++)
{
sprintf(hex_str, "%02X", Receive_P2Pdatapackage->Buffer[i]);
RUI_LOG_PRINTF_MERGE("%s",hex_str);
}
RUI_LOG_PRINTF_MERGE("\r\n");
}
void LoRaWANJoined_callback(uint32_t status)
{
if(status) //Join Success
{
RUI_LOG_PRINTF_MERGE("OK Join Success\r\n");
}
else
{
RUI_LOG_PRINTF_MERGE("ERROR: %d\r\n",RUI_AT_LORA_INFO_STATUS_JOIN_FAIL);
}
}
void LoRaWANSendsucceed_callback(RUI_MCPS_T mcps_type,RUI_RETURN_STATUS status)
{
if(status == RUI_STATUS_OK)
{
switch( mcps_type )
{
case RUI_MCPS_UNCONFIRMED:
{
RUI_LOG_PRINTF_MERGE("OK\r\n");
break;
}
case RUI_MCPS_CONFIRMED:
{
RUI_LOG_PRINTF_MERGE("OK\r\n");
break;
}
case RUI_MCPS_PROPRIETARY:
{
RUI_LOG_PRINTF_MERGE("OK\r\n");
break;
}
case RUI_MCPS_MULTICAST:
{
RUI_LOG_PRINTF_MERGE("OK\r\n");
break;
}
default:
break;
}
}
else if(status != RUI_AT_LORA_INFO_STATUS_ADDRESS_FAIL)
{
RUI_LOG_PRINTF_MERGE("ERROR: %d\r\n",status);
}
}
void LoRaP2PSendsucceed_callback(void)
{
RUI_LOG_PRINTF_MERGE("OK \r\n");
}
/*******************************************************************************************
* If the uart is in RUI_UART_USER mode, this interface will be called.
*
* *****************************************************************************************/
void rui_uart_recv(RUI_UART_DEF uart_def, uint8_t *pdata, uint16_t len)
{
if (uart_def == RUI_UART1)
{
// Add user code...
}
}
/* the function will run before sleep,
user can add code to make sensor into low power mode */
void user_sensor_sleep(void)
{
// ...
}
/* the function will run after wake up,
user can add code to wake up and init sensor module. */
void user_sensor_wakeup(void)
{
// ...
}
void main(void)
{
RUI_LORA_STATUS_T app_lora_status; //record status
rui_init();
/*******************************************************************************************
* Register LoRaMac callback function
*
* *****************************************************************************************/
rui_lora_register_recv_callback(LoRaReceive_callback);
rui_lorajoin_register_callback(LoRaWANJoined_callback);
rui_lorasend_complete_register_callback(LoRaWANSendsucceed_callback);
rui_lorap2p_register_recv_callback(LoRaP2PReceive_callback);
rui_lorap2p_complete_register_callback(LoRaP2PSendsucceed_callback);
/*******************************************************************************************
* Register Sleep and Wakeup callback function
*
* *****************************************************************************************/
rui_sensor_register_callback(user_sensor_wakeup, user_sensor_sleep);
/*******************************************************************************************
* Obtain status information and print.
*
* *****************************************************************************************/
rui_lora_get_status(false,&app_lora_status);
switch(app_lora_status.work_mode)
{
case RUI_LORAWAN:
RUI_LOG_PRINTF_MERGE("Current work_mode:LoRaWAN,");
if(app_lora_status.join_mode == RUI_OTAA)
{
RUI_LOG_PRINTF_MERGE(" join_mode:OTAA,");
if(app_lora_status.MulticastEnable)
{
RUI_LOG_PRINTF_MERGE(" MulticastEnable:true.\r\n");
}
else
{
RUI_LOG_PRINTF_MERGE(" MulticastEnable: false,");
switch(app_lora_status.class_status)
{
case RUI_CLASS_A:RUI_LOG_PRINTF_MERGE(" Class: A\r\n");
break;
case RUI_CLASS_B:RUI_LOG_PRINTF_MERGE(" Class: B\r\n");
break;
case RUI_CLASS_C:RUI_LOG_PRINTF_MERGE(" Class: C\r\n");
break;
default:break;
}
}
}
else if(app_lora_status.join_mode == RUI_ABP)
{
RUI_LOG_PRINTF_MERGE(" join_mode:ABP,\r\n");
if(app_lora_status.MulticastEnable)
{
RUI_LOG_PRINTF_MERGE(" MulticastEnable:true.\r\n");
}
else
{
RUI_LOG_PRINTF_MERGE(" MulticastEnable: false,");
switch(app_lora_status.class_status)
{
case RUI_CLASS_A: RUI_LOG_PRINTF_MERGE(" Class: A\r\n");
break;
case RUI_CLASS_B: RUI_LOG_PRINTF_MERGE(" Class: B\r\n");
break;
case RUI_CLASS_C: RUI_LOG_PRINTF_MERGE(" Class: C\r\n");
break;
default:break;
}
}
}
break;
case RUI_P2P:
RUI_LOG_PRINTF_MERGE("Current work_mode:P2P\r\n");
if (g_rui_cfg_t.g_lora_p2p_cfg_t.p2p_workmode == 1)
{
RUI_LOG_PRINTF_MERGE("LoRa P2P Transfer_mode: Receiver\r\n");
}
else
{
RUI_LOG_PRINTF_MERGE("LoRa P2P Transfer_mode: Sender\r\n");
}
break;
default:
break;
}
RUI_LOG_PRINTF_MERGE("Initialization OK\r\n");
rui_delay_ms(100);
/* User Code */
setup();
/* End User Code */
while(1)
{
//here run system work and do not modify
rui_running();
}
}