-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
executable file
·406 lines (335 loc) · 11.6 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
/****************************************************************************************/
/*
* MOTAM-Scanner
* Created by Manuel Montenegro, Jun 14, 2019.
* Developed for MOTAM project.
*
* This is a Bluetooth 5 scanner. This code reads every advertisement from MOTAM beacons
* and sends its data through serial port.
*
* This code has been developed for Nordic Semiconductor nRF52840 PDK & nRF52840 dongle.
*/
/****************************************************************************************/
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include "nrf.h"
#include "nrf_error.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_drv_usbd.h"
#include "nrf_drv_clock.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "nrf_drv_power.h"
#include "app_error.h"
#include "app_util.h"
#include "app_usbd_core.h"
#include "app_usbd.h"
#include "app_usbd_string_desc.h"
#include "app_usbd_cdc_acm.h"
#include "app_usbd_serial_num.h"
#include "boards.h"
#include "bsp.h"
#include "bsp_cli.h"
#include "nrf_cli.h"
#include "nrf_cli_uart.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "ble.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "nrf_sdh_soc.h"
#include "nrf_ble_scan.h"
#include "nrf_fstorage.h"
// ======== USB Device CDC ACM configuration ========
// Enable power USB detection
#ifndef USBD_POWER_DETECTION
#define USBD_POWER_DETECTION true
#endif
// USB Device CDC ACM board configuration parameters
#define CDC_ACM_COMM_INTERFACE 0
#define CDC_ACM_COMM_EPIN NRF_DRV_USBD_EPIN2
#define CDC_ACM_DATA_INTERFACE 1
#define CDC_ACM_DATA_EPIN NRF_DRV_USBD_EPIN1
#define CDC_ACM_DATA_EPOUT NRF_DRV_USBD_EPOUT1
// USB Device CDC ACM user event handler
static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst, app_usbd_cdc_acm_user_event_t event);
// USB Device CDC ACM instance
APP_USBD_CDC_ACM_GLOBAL_DEF(m_app_cdc_acm,
cdc_acm_user_ev_handler,
CDC_ACM_COMM_INTERFACE,
CDC_ACM_DATA_INTERFACE,
CDC_ACM_COMM_EPIN,
CDC_ACM_DATA_EPIN,
CDC_ACM_DATA_EPOUT,
APP_USBD_CDC_COMM_PROTOCOL_AT_V250
);
// USB Device CDC ACM transmission buffers
static char m_tx_buffer[255*2];
// USB Device CDC ACM user event handler
static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst, app_usbd_cdc_acm_user_event_t event)
{
switch (event)
{
case APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN:
break;
case APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE:
break;
case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
break;
case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
break;
default:
break;
}
}
// USB Device event handler
static void usbd_user_ev_handler(app_usbd_event_type_t event)
{
switch (event)
{
case APP_USBD_EVT_DRV_SUSPEND:
break;
case APP_USBD_EVT_DRV_RESUME:
break;
case APP_USBD_EVT_STARTED:
break;
case APP_USBD_EVT_STOPPED:
app_usbd_disable();
break;
case APP_USBD_EVT_POWER_DETECTED:
if (!nrf_drv_usbd_is_enabled())
{
app_usbd_enable();
}
break;
case APP_USBD_EVT_POWER_REMOVED:
app_usbd_stop();
break;
case APP_USBD_EVT_POWER_READY:
app_usbd_start();
break;
default:
break;
}
}
// USB Device initialization
static void usbd_init (void)
{
ret_code_t err_code;
app_usbd_serial_num_generate();
static const app_usbd_config_t usbd_config = {
.ev_state_proc = usbd_user_ev_handler
};
err_code = app_usbd_init(&usbd_config);
APP_ERROR_CHECK(err_code);
app_usbd_class_inst_t const * class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&m_app_cdc_acm);
err_code = app_usbd_class_append(class_cdc_acm);
APP_ERROR_CHECK(err_code);
}
// Function that enables USB Device events. This function must be called after BLE functions starts
static void usbd_start (void)
{
ret_code_t err_code;
err_code = app_usbd_power_events_enable();
APP_ERROR_CHECK(err_code);
}
// ======== BLE configuration ========
// BLE configuration parameters
#define APP_BLE_CONN_CFG_TAG 1 // Tag that identifies the BLE configuration of the SoftDevice.
#define APP_BLE_OBSERVER_PRIO 3 // BLE observer priority of the application. There is no need to modify this value.
#define APP_SOC_OBSERVER_PRIO 1 // SoC observer priority of the application. There is no need to modify this value.
#define SCAN_INTERVAL 0x0320 // Determines scan interval in units of 0.625 millisecond.
#define SCAN_WINDOW 0x0320 // Determines scan window in units of 0.625 millisecond.
#define SCAN_DURATION 0x0000 // Duration of the scanning in units of 10 milliseconds. If set to 0x0000, scanning continues until it is explicitly disabled.
static bool m_mem_acc_in_progress; // Flag to keep track of ongoing operations on persistent memory.
static ble_gap_scan_params_t m_scan_param = // Scan parameters requested for scanning and connection.
{
.active = 0x00,
.interval = SCAN_INTERVAL,
.window = SCAN_WINDOW,
.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL,
.timeout = SCAN_DURATION,
.scan_phys = BLE_GAP_PHY_CODED, // Here you can select the PHY (BLE_GAP_PHY_CODED or BLE_GAP_PHY_1MBPS)
.extended = 1,
};
// Scanning Module instance.
NRF_BLE_SCAN_DEF (m_scan);
// Function for starting scanner
static void scan_start(void);
// Function for handling Scanning Module events
static void scan_evt_handler(scan_evt_t const * p_scan_evt)
{
NRF_LOG_RAW_INFO("PRUEBA\r\n");
switch(p_scan_evt->scan_evt_id)
{
case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT:
{
scan_start();
}
break;
// No whitelist set up, so the filter is not matched for the scan data.
case NRF_BLE_SCAN_EVT_NOT_FOUND:
{
int i; // Index for loop
uint8_t * p_data = p_scan_evt->params.p_not_found->data.p_data; // Pointer to packet data
int data_len = p_scan_evt->params.p_not_found->data.len; // Packet data length
// Filtering MOTAM advertisements
if (p_data[1] == 0xFF && p_data[2] == 0xBE && p_data [3] == 0x5E)
{
sprintf(m_tx_buffer,"1-");
// Conversion from byte string to hexadecimal char string
for (i = 0; i < data_len-64; i++ )
{
sprintf(&m_tx_buffer[(i*2)+2], "%02x", p_data[i]);
}
sprintf(&m_tx_buffer[(i*2)+2], "\r\n");
// Send the hexadecimal char string by serial port
app_usbd_cdc_acm_write(&m_app_cdc_acm, m_tx_buffer, (i*2)+2+2);
}
}
break;
default:
break;
}
}
// BLE events handler
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
switch (p_ble_evt->header.evt_id)
{
case BLE_GAP_EVT_ADV_REPORT:
break;
default:
break;
}
}
// SoftDevice SoC event handler.
static void soc_evt_handler(uint32_t evt_id, void * p_context)
{
switch (evt_id)
{
case NRF_EVT_FLASH_OPERATION_SUCCESS:
/* fall through */
case NRF_EVT_FLASH_OPERATION_ERROR:
if (m_mem_acc_in_progress)
{
m_mem_acc_in_progress = false;
scan_start();
}
break;
default:
// No implementation needed.
break;
}
}
// Function for initializing the BLE stack
static void ble_stack_init(void)
{
ret_code_t err_code;
err_code = nrf_sdh_enable_request();
APP_ERROR_CHECK(err_code);
// Configure the BLE stack using the default settings.
// Fetch the start address of the application RAM.
uint32_t ram_start = 0;
err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
APP_ERROR_CHECK(err_code);
// Enable BLE stack.
err_code = nrf_sdh_ble_enable(&ram_start);
APP_ERROR_CHECK(err_code);
// Register handlers for BLE and SoC events.
NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
NRF_SDH_SOC_OBSERVER(m_soc_observer, APP_SOC_OBSERVER_PRIO, soc_evt_handler, NULL);
}
// Function for initializing the scanning and setting the filters
static void scan_init(void)
{
ret_code_t err_code;
nrf_ble_scan_init_t init_scan;
memset(&init_scan, 0, sizeof(init_scan));
init_scan.connect_if_match = false;
init_scan.conn_cfg_tag = APP_BLE_CONN_CFG_TAG;
init_scan.p_scan_param = &m_scan_param;
err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
APP_ERROR_CHECK(err_code);
}
// Function for starting scanning
static void scan_start(void)
{
ret_code_t err_code;
// If there is any pending write to flash, defer scanning until it completes.
if (nrf_fstorage_is_busy(NULL))
{
m_mem_acc_in_progress = true;
return;
}
err_code = nrf_ble_scan_start(&m_scan);
APP_ERROR_CHECK(err_code);
}
// ======== Board Initialization ========
// Logging initialization
static void log_init(void)
{
ret_code_t err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_DEFAULT_BACKENDS_INIT();
}
// Clock initialization
static void clock_init (void)
{
ret_code_t err_code = nrf_drv_clock_init();
APP_ERROR_CHECK(err_code);
nrf_drv_clock_lfclk_request(NULL);
while(!nrf_drv_clock_lfclk_is_running())
{
/* Just waiting */
}
}
// Timer initialization
static void timer_init(void)
{
ret_code_t err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
}
// Power managemente initialization
static void power_management_init(void)
{
ret_code_t err_code;
err_code = nrf_pwr_mgmt_init();
APP_ERROR_CHECK(err_code);
}
// Function for handling the idle state (main loop)
static void idle_state_handle(void)
{
if (NRF_LOG_PROCESS() == false)
{
nrf_pwr_mgmt_run();
}
}
int main(void)
{
// Initialize.
log_init();
timer_init();
clock_init();
usbd_init();
power_management_init();
ble_stack_init();
scan_init();
scan_start();
usbd_start();
// Start execution.
NRF_LOG_RAW_INFO( " -------------\r\n");
NRF_LOG_RAW_INFO( "| MOTAM scanner |");
NRF_LOG_RAW_INFO("\r\n -------------\r\n");
for (;;)
{
while (app_usbd_event_queue_process())
{
// While there are USBD events in queue...
}
idle_state_handle();
}
}