-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
414 lines (319 loc) · 12.1 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/**
* Copyright 2020 DriveX.Tech. All rights reserved.
*
* Licensed under the License.
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "sdk_config.h"
#include "nordic_common.h"
#include "nrf.h"
#include "nrf_delay.h"
#include "app_error.h"
#include "app_util.h"
#include "app_timer.h"
#include "app_scheduler.h"
#include "nrf_drv_power.h"
#include "nrf_drv_clock.h"
#include "nrf_pwr_mgmt.h"
#include "boards.h"
#include "bsp_btn_ble.h"
#include "protothread/pt.h"
#include "util.h"
#include "packet_buf.h"
#include "packet_queue.h"
#include "bt_collector.h"
#include "bt_collector_cacher.h"
#include "usb_ser.h"
#include "usb_ser_cacher.h"
#define PEER_NUM_LED_MASK BSP_LED_0_MASK
#define HOST_USB_LED_MASK BSP_LED_1_MASK
#define USER_BUTTON_NO BSP_BUTTON_0 /**< Button for user */
#define BUTTON_DETECTION_DELAY APP_TIMER_TICKS(80) /**< Delay for a button is reported as pushed (in number of timer ticks). */
typedef struct _bt_data_t {
bt_addr_t bt_addr;
BUF_ELEM_TYPE buf_elem;
} bt_data_t;
typedef struct _usb_data_t {
BUF_ELEM_TYPE buf_elem;
} usb_data_t;
//APP_TIMER_SCHED_EVENT_DATA_SIZE
#define MAX_SCHED_EVENT_DATA_SIZE MAX(sizeof(bt_data_t), sizeof(usb_data_t))
#define MAX_SCHED_QUEUE_SIZE 28
#define MAX_PACKET_BUF_LENGTH 18
BUF_DEF(g_bt_buf, 256, MAX_PACKET_BUF_LENGTH);
BUF_DEF(g_usb_buf, 256, MAX_PACKET_BUF_LENGTH);
PT_THREAD(task_pwr_mgmt(struct pt* pt, volatile bool* idle))
{
PT_BEGIN(pt);
while(true) {
//PT_YIELD_UNTIL(pt, *idle);
PT_WAIT_UNTIL(pt, *idle);
nrf_pwr_mgmt_run();
if(SOFTDEVICE_PRESENT) {
sd_app_evt_wait();
} else {
__WFI();
}
PT_YIELD(pt);
}
PT_END(pt);
}
static struct pt pt_task_bt, pt_task_usb, pt_task_pwr_mgmt;
void scheduler_handler()
{
volatile bool idle = false;
//const int max_num_packet_cur = get_cached_bt_packet_num() + get_cached_usb_packet_num();
const int max_num_packet_cur = 2 * get_cached_bt_packet_num() + get_cached_usb_packet_num();
int num_packet_cur = 0;
while(! idle && num_packet_cur < max_num_packet_cur) {
int num_bt_packet_processed = 0, num_usb_packet_processed = 0;
process_cached_bt_packet(&pt_task_bt, &num_bt_packet_processed);
process_cached_usb_packet(&pt_task_usb, &num_usb_packet_processed);
num_packet_cur += num_bt_packet_processed + num_usb_packet_processed;
if(num_bt_packet_processed == 0 && num_usb_packet_processed == 0) {
idle = true;
}
}
if(app_sched_queue_space_get() == MAX_SCHED_QUEUE_SIZE) {
task_pwr_mgmt(&pt_task_pwr_mgmt, &idle);
}
}
void usb_data_scheduler_handler(void *p_event_data, uint16_t event_size)
{
if(event_size > 0) { // p_event_data may NOT be NULL in `app_sched_event_put`'s impl!
static BYTE recev_buf[MAX_PACKET_LEN];
static WORD recev_data_len;
usb_data_t* usb_data = p_event_data;
// SEND_LOG("(%s): usb data received: %lu B\r\n", __func__, usb_data->buf_elem.elem_len);
// SEND_LOG("(%s)(buf: %u, %u): usb data received: %lu B\r\n", __func__,
// QUEUE_AVAILABLE(*g_log_packet_buf.packet_queue), QUEUE_AVAILABLE(*g_data_packet_buf.packet_queue), usb_data->buf_elem.elem_len);
tag_t tag;
WORD data_total_len;
data_total_len = tlv_parse(usb_data->buf_elem.elem_data, &tag, &recev_data_len, recev_buf);
assert(data_total_len == usb_data->->buf_elem.elem_len);
//TODO
switch(tag) {
case TAG_CMD:
break;
case TAG_DATA:
do {
bt_addr_t peer_addr;
fast_copy(peer_addr.addr1s, recev_buf, sizeof(peer_addr.addr1s));
SEND_BT_DATA(&peer_addr, recev_buf + sizeof(peer_addr.addr1s), recev_data_len - sizeof(peer_addr.addr1s));
} while(0);
break;
default:
break;
}
BUF_ELEM_FREE(g_usb_buf, usb_data->buf_elem);
}
scheduler_handler();
}
void on_usb_data_received(const BYTE* data, WORD data_len)
{
assert(data != NULL && data_len > 0);
ret_code_t err_code;
usb_data_t usb_data;
BUF_ELEM_ALLOC(g_usb_buf, data_len, usb_data.buf_elem);
if(usb_data.buf_elem.elem_data == NULL) {
SEND_LOG("(%s): BUF_ELEM_ALLOC failed!\r\n", __func__);
return;
}
fast_copy(usb_data.buf_elem.elem_data, data, data_len);
err_code = app_sched_event_put(&usb_data, sizeof(usb_data), usb_data_scheduler_handler);
if(err_code != NRF_SUCCESS) {
BUF_ELEM_FREE(g_usb_buf, usb_data.buf_elem);
SEND_LOG("(%s): app_sched_event_put failed!\r\n", __func__);
}
}
void on_usb_data_sent()
{
ret_code_t err_code;
err_code = app_sched_event_put(NULL, 0, usb_data_scheduler_handler);
if(err_code != NRF_SUCCESS) {
SEND_LOG("(%s): app_sched_event_put failed!\r\n", __func__);
}
}
void ble_data_scheduler_handler(void *p_event_data, uint16_t event_size)
{
if(event_size > 0) { // p_event_data may NOT be NULL in `app_sched_event_put`'s impl!
static BYTE recev_buf[MAX_PACKET_LEN];
bt_data_t* bt_data = p_event_data;
// SEND_LOG("(%s): ble data received: %lu B\r\n", __func__, bt_data->buf_elem.elem_len);
SEND_LOG("(%s): ble data received: %lu B, from: 0x%02X%02X, epoch: %u\r\n", __func__,
bt_data->buf_elem.elem_len,
bt_data->buf_elem.elem_data[5], bt_data->buf_elem.elem_data[6],
bt_data->buf_elem.elem_data[40+5]); // TODO: delete
// SEND_LOG("(%s)(bt_buf: %d, usb_buf: %d): ble data received: %lu B\r\n", __func__,
// get_cached_bt_packet_num(), get_cached_usb_packet_num(), bt_data->buf_elem.elem_len);
fast_copy(recev_buf, bt_data->bt_addr.addr1s, sizeof(bt_data->bt_addr.addr1s));
fast_copy(recev_buf + sizeof(bt_data->bt_addr.addr1s), bt_data->buf_elem.elem_data, bt_data->buf_elem.elem_len);
SEND_USB_DATA(recev_buf, sizeof(bt_data->bt_addr.addr1s) + bt_data->buf_elem.elem_len);
// SEND_BT_DATA(&bt_data->bt_addr, bt_data->buf_elem.elem_data, bt_data->buf_elem.elem_len);
BUF_ELEM_FREE(g_bt_buf, bt_data->buf_elem);
}
scheduler_handler();
}
void on_ble_data_received(const bt_addr_t* peer_addr, const BYTE* data, WORD data_len)
{
assert(data != NULL && data_len > 0);
ret_code_t err_code;
bt_data_t bt_data;
bt_data.bt_addr = *peer_addr;
fast_copy(&bt_data.bt_addr, peer_addr, sizeof(bt_data.bt_addr));
BUF_ELEM_ALLOC(g_bt_buf, data_len, bt_data.buf_elem);
if(bt_data.buf_elem.elem_data == NULL) {
SEND_LOG("(%s): BUF_ELEM_ALLOC failed!\r\n", __func__);
assert(bt_data.buf_elem.elem_data != NULL);
return;
}
fast_copy(bt_data.buf_elem.elem_data, data, data_len);
err_code = app_sched_event_put(&bt_data, sizeof(bt_data), ble_data_scheduler_handler);
if(err_code != NRF_SUCCESS) {
BUF_ELEM_FREE(g_bt_buf, bt_data.buf_elem);
SEND_LOG("(%s): app_sched_event_put failed, spaces available: #%u!\r\n", __func__, app_sched_queue_space_get());
}
}
void on_ble_data_sent(const bt_addr_t* peer_addr)
{
ret_code_t err_code;
err_code = app_sched_event_put(NULL, 0, ble_data_scheduler_handler);
if(err_code != NRF_SUCCESS) {
SEND_LOG("(%s): app_sched_event_put failed, spaces available: #%u!\r\n", __func__, app_sched_queue_space_get());
}
}
/**@brief Function for handling events from the button handler module.
*
* @param[in] pin_no The pin that the event applies to.
* @param[in] button_action The button action (press or release).
*/
static void button_handler(uint8_t pin_no, uint8_t button_action)
{
ret_code_t err_code;
if(pin_no == USER_BUTTON_NO) {
};
}
/**@brief Function for initializing the button handler module.
*/
void buttons_init()
{
ret_code_t err_code;
static app_button_cfg_t button_conf;
button_conf.pin_no = USER_BUTTON_NO;
button_conf.pull_cfg = BUTTON_PULL;
button_conf.active_state = false;
button_conf.button_handler = button_handler;
err_code = app_button_init(&button_conf, 1, BUTTON_DETECTION_DELAY);
APP_ERROR_CHECK(err_code);
err_code = app_button_enable();
APP_ERROR_CHECK(err_code);
}
void power_mgmt_init()
{
ret_code_t err_code;
nrf_drv_power_config_t power_conf;
err_code = nrf_drv_power_init(NULL); // Must be called before SoftDevice init
APP_ERROR_CHECK(err_code);
err_code = nrf_pwr_mgmt_init();
APP_ERROR_CHECK(err_code);
}
APP_TIMER_DEF(m_app_timer_debug);
APP_TIMER_DEF(m_app_timer_maintainer);
void timer_debug_cb(void* p_context)
{
static int cached_peer_num = -8;
if(check_usb_connected()) {
// if(!check_usb_busy()) {
LEDS_ON(HOST_USB_LED_MASK);
} else {
LEDS_OFF(HOST_USB_LED_MASK);
}
int peer_num = get_connected_peer_num();
if(peer_num == cached_peer_num) {
if(cached_peer_num != 0) {
LEDS_INVERT(PEER_NUM_LED_MASK);
}
return;
}
if(peer_num == 0) {
LEDS_ON(PEER_NUM_LED_MASK);
} else {
app_timer_stop(m_app_timer_debug);
if(peer_num > 0 && peer_num <= 8) {
app_timer_start(m_app_timer_debug, APP_TIMER_TICKS(1000 * peer_num), NULL);
} else {
app_timer_start(m_app_timer_debug, APP_TIMER_TICKS(1000 / 8), NULL);
}
}
cached_peer_num = peer_num;
}
void timer_maintainer_cb(void* p_context)
{
app_sched_event_put(NULL, 0, usb_data_scheduler_handler);
}
/** @brief Function for initializing the timer.
*/
void timer_init()
{
ret_code_t err_code;
err_code = nrf_drv_clock_init();
APP_ERROR_CHECK(err_code);
nrf_drv_clock_lfclk_request(NULL);
err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
// Create debug timer.
err_code = app_timer_create(&m_app_timer_debug, APP_TIMER_MODE_REPEATED, timer_debug_cb);
// err_code = app_timer_create(&m_app_timer_debug, APP_TIMER_MODE_SINGLE_SHOT, timer_debug_cb);
APP_ERROR_CHECK(err_code);
// Create maintainer timer.
err_code = app_timer_create(&m_app_timer_maintainer, APP_TIMER_MODE_REPEATED, timer_maintainer_cb);
// err_code = app_timer_create(&m_app_timer_maintainer, APP_TIMER_MODE_SINGLE_SHOT, timer_maintainer_cb);
APP_ERROR_CHECK(err_code);
}
void timer_start()
{
SEND_LOG("(%s): start...\r\n", __func__);
ret_code_t err_code;
//err_code = app_timer_start(m_app_timer_debug, APP_TIMER_TICKS(100), NULL);
err_code = app_timer_start(m_app_timer_debug, APP_TIMER_TICKS(1000), NULL);
//err_code = app_timer_start(m_app_timer_debug, APP_TIMER_TICKS(10000), NULL);
APP_ERROR_CHECK(err_code);
err_code = app_timer_start(m_app_timer_maintainer, APP_TIMER_TICKS(180), NULL);
APP_ERROR_CHECK(err_code);
}
int main()
{
ret_code_t ret;
// Scheduler settings
APP_SCHED_INIT(MAX_SCHED_EVENT_DATA_SIZE, MAX_SCHED_QUEUE_SIZE);
BUF_INIT(g_bt_buf);
BUF_INIT(g_usb_buf);
// Initialize.
LEDS_CONFIGURE(PEER_NUM_LED_MASK | HOST_USB_LED_MASK);
LEDS_OFF(PEER_NUM_LED_MASK | HOST_USB_LED_MASK);
power_mgmt_init(); // must be called before SoftDevice
timer_init();
usb_init(); // since using power, must be called before ble init
buttons_init();
bt_collector_init();
usb_ser_cacher_init();
bt_collector_cacher_init();
// Start execution.
SEND_LOG("test_dtc started.\r\n");
timer_start();
scan_start();
PT_INIT(&pt_task_bt);
PT_INIT(&pt_task_usb);
PT_INIT(&pt_task_pwr_mgmt);
while(true) {
app_sched_execute();
// if(SOFTDEVICE_PRESENT) {
// sd_app_evt_wait();
// } else {
// __WFI();
// }
}
}