Skip to content

Commit

Permalink
Moved BLE scan to separate module/thread
Browse files Browse the repository at this point in the history
  • Loading branch information
danielinux committed May 3, 2020
1 parent 3fd6fac commit 336483e
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 100 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
- [x] Communication based on BLE advertisements
- [x] Integrate wolfCrypt
- [ ] Store received info in FLASH memory
- [ ] Mock application to generate/collect DP3T-EphIds
- [x] Mock application to generate/collect DP3T-EphIds
- [ ] Design IoT communication
124 changes: 124 additions & 0 deletions ble_scan.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include "event/timeout.h"
#include "nimble_scanner.h"
#include "net/bluetil/ad.h"
#include "nimble_scanlist.h"
#include "dp3t-config.h"
#include "dp3t.h"
#include "keystore.h"
#include "contactstore.h"


static event_queue_t eq;
static event_t blescan_evt;
static event_timeout_t blescan_timeout_evt;


/*** SCAN ***/
static void dp3t_print_entry(int *idx, nimble_scanlist_entry_t *e)
{
char name[(BLE_ADV_PDU_LEN + 1)] = { 0 };
char peer_ephid[17] = { 0 };
int res;
bluetil_ad_t ad = BLUETIL_AD_INIT(e->ad, e->ad_len, e->ad_len);
res = bluetil_ad_find_str(&ad, BLE_GAP_AD_NAME, name, sizeof(name));
if (res != BLUETIL_AD_OK) {
res = bluetil_ad_find_str(&ad, BLE_GAP_AD_NAME_SHORT, name, sizeof(name));
}
if (res != BLUETIL_AD_OK) {
strncpy(name, "undefined", sizeof(name));
}
res = bluetil_ad_find_str(&ad, BLE_GAP_AD_UUID128_COMP, peer_ephid, sizeof(peer_ephid));
if (res == BLUETIL_AD_OK) {
printf("[rx ephid %02d RSSI %d]: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
(*idx)++,
e->last_rssi,
peer_ephid[0],
peer_ephid[1],
peer_ephid[2],
peer_ephid[3],
peer_ephid[4],
peer_ephid[5],
peer_ephid[6],
peer_ephid[7],
peer_ephid[8],
peer_ephid[9],
peer_ephid[10],
peer_ephid[11],
peer_ephid[12],
peer_ephid[13],
peer_ephid[14],
peer_ephid[15]);
cstore_add(0, 0, (uint8_t) ((0 - e->last_rssi) & 0xFF), (uint8_t *)peer_ephid);
}
}

void dp3t_scanlist_print(void)
{
int i = 0;
nimble_scanlist_entry_t *e = nimble_scanlist_get_next(NULL);
while (e) {
dp3t_print_entry(&i, e);
e = nimble_scanlist_get_next(e);
}
}

static void blescan(event_t *e)
{
uint32_t timeout = BLESCAN_DURATION;
nimble_scanlist_clear();
// printf("BLE scan\n");
nimble_scanner_start();
xtimer_usleep(timeout * 1000);
nimble_scanner_stop();
// puts("Contacts:");
// nimble_scanlist_print();
dp3t_scanlist_print();
/* schedule next update event */
event_timeout_set(&blescan_timeout_evt, BLESCAN_INTERVAL * 1000);
}


#define INITIAL_BLESCAN_INTERVAL 2000
static char blescan_thread_stack[THREAD_STACKSIZE_MAIN];

void dp3t_blescan_start(void)
{
event_timeout_set(&blescan_timeout_evt, INITIAL_BLESCAN_INTERVAL * 1000);
}

static void *blescan_task(void *arg)
{
(void)arg;
struct ble_gap_disc_params scan_params = {
.itvl = BLE_GAP_LIM_DISC_SCAN_INT,
.window = BLE_GAP_LIM_DISC_SCAN_WINDOW,
.filter_policy = 0, /* don't use */
.limited = 0, /* no limited discovery */
.passive = 0, /* no passive scanning */
. filter_duplicates = 0, /* no duplicate filtering */
};
/* initialize the nimble scanner */
nimble_scanlist_init();
nimble_scanner_init(&scan_params, nimble_scanlist_update);

/* initialize the timer to trigger scan events */
event_queue_init(&eq);
blescan_evt.handler = blescan;
event_timeout_init(&blescan_timeout_evt, &eq, &blescan_evt);
dp3t_blescan_start();
event_loop(&eq);
return NULL; /* never reached */
}


void dp3t_blescan_init(void)
{
thread_create(blescan_thread_stack, THREAD_STACKSIZE_MAIN,
THREAD_PRIORITY_MAIN - 1, THREAD_CREATE_STACKTEST,
blescan_task, NULL, "blescan");
}

void dp3t_blescan_stop(void)
{
event_timeout_clear(&blescan_timeout_evt);
}
6 changes: 6 additions & 0 deletions ble_scan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef BLESCAN_H
#define BLESCAN_H
void dp3t_blescan_init(void);
int dp3t_blescan_start(void);
void dp3t_blescan_stop(void);
#endif
20 changes: 20 additions & 0 deletions contactstore.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdint.h>
#include <string.h>

#include "dp3t.h"
#include "dp3t-config.h"

#define CSTORE_ADDR 0
#define CONTACTS_BASE (CSTORE_ADDR + sizeof(contacts_t))


int cstore_add( uint8_t day, uint8_t epoch, uint8_t rssi, uint8_t *ephid)
{
contact_t c;
c.day = day;
c.epoch = epoch;
c.rssi = rssi;
memcpy(c.data, ephid, 16);
/* TODO: write to store page, update index */
return 0;
}
6 changes: 6 additions & 0 deletions contactstore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef CONTACTSTORE_H
#define CONTACTSTORE_H
#include <stdint.h>
int cstore_add( uint8_t day, uint8_t epoch, uint8_t rssi, uint8_t *ephid);

#endif
6 changes: 3 additions & 3 deletions dp3t-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#define BROADCAST_KEY "Broadcast key"
#define BROADCAST_KEY_LEN 13UL

/* Bluetooth scan/adv timing */


/* Bluetooth scan/adv timing (in milliseconds) */
#define BLESCAN_INTERVAL 58000
#define BLESCAN_DURATION 2000

#endif
105 changes: 9 additions & 96 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,90 +11,11 @@

#include "shell.h"
#include "msg.h"
#include "nimble_scanner.h"
#include "net/bluetil/ad.h"
#include "nimble_scanlist.h"

#include "dp3t-config.h"
#include "dp3t.h"
#include "keystore.h"


/*** SCAN ***/
/* default scan duration (1s) */
#define DEFAULT_DURATION (1000000U)

void dp3t_print_entry(int *idx, nimble_scanlist_entry_t *e)
{
char name[(BLE_ADV_PDU_LEN + 1)] = { 0 };
char peer_ephid[17] = { 0 };
int res;
bluetil_ad_t ad = BLUETIL_AD_INIT(e->ad, e->ad_len, e->ad_len);
res = bluetil_ad_find_str(&ad, BLE_GAP_AD_NAME, name, sizeof(name));
if (res != BLUETIL_AD_OK) {
res = bluetil_ad_find_str(&ad, BLE_GAP_AD_NAME_SHORT, name, sizeof(name));
}
if (res != BLUETIL_AD_OK) {
strncpy(name, "undefined", sizeof(name));
}
res = bluetil_ad_find_str(&ad, BLE_GAP_AD_UUID128_COMP, peer_ephid, sizeof(peer_ephid));
if (res == BLUETIL_AD_OK) {
printf("[%02d] DP-3T: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\r\n",
(*idx)++,
peer_ephid[0],
peer_ephid[1],
peer_ephid[2],
peer_ephid[3],
peer_ephid[4],
peer_ephid[5],
peer_ephid[6],
peer_ephid[7],
peer_ephid[8],
peer_ephid[9],
peer_ephid[10],
peer_ephid[11],
peer_ephid[12],
peer_ephid[13],
peer_ephid[14],
peer_ephid[15]);
/* TODO: receive ephid */
}
}

void dp3t_scanlist_print(void)
{
int i = 0;
nimble_scanlist_entry_t *e = nimble_scanlist_get_next(NULL);
while (e) {
dp3t_print_entry(&i, e);
e = nimble_scanlist_get_next(e);
}
}

int cmd_scan(int argc, char **argv)
{
uint32_t timeout = DEFAULT_DURATION;

if ((argc == 2) && (memcmp(argv[1], "help", 4) == 0)) {
printf("usage: %s [timeout in ms]\n", argv[0]);
return 0;
}
if (argc >= 2) {
timeout = (uint32_t)(atoi(argv[1]) * 1000);
}

nimble_scanlist_clear();
printf("Scanning for %ums now ...", (unsigned)(timeout / 1000));
nimble_scanner_start();
xtimer_usleep(timeout);
nimble_scanner_stop();
puts(" done\n\nResults:");
nimble_scanlist_print();
dp3t_scanlist_print();
puts("");
return 0;
}

#include "ble_scan.h"

#define MAIN_QUEUE_SIZE (8)
static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];
Expand All @@ -115,9 +36,6 @@ static int wolftest(int argc, char **argv)
extern int gatt_server(void);

static const shell_command_t shell_commands[] = {
{ "dtlsc", "Start a DTLS client", dtls_client },
{ "dtlss", "Start and stop a DTLS server", dtls_server },
{ "scan", "trigger a BLE scan", cmd_scan },
{ "testvec", "print test vectors", dp3t_shellcmd_testvec },
{ "rekey", "regenerate DP3T secure key", dp3t_shellcmd_rekey },
#ifdef MODULE_WOLFCRYPT_TEST
Expand All @@ -131,33 +49,28 @@ char line_buf[SHELL_DEFAULT_BUFSIZE];
int main(void)
{
uint8_t *ephid, *sk_t0;
struct ble_gap_disc_params scan_params = {
.itvl = BLE_GAP_LIM_DISC_SCAN_INT,
.window = BLE_GAP_LIM_DISC_SCAN_WINDOW,
.filter_policy = 0, /* don't use */
.limited = 0, /* no limited discovery */
.passive = 0, /* no passive scanning */
. filter_duplicates = 0, /* no duplicate filtering */
};

/* initialize the nimble scanner */
nimble_scanlist_init();
nimble_scanner_init(&scan_params, nimble_scanlist_update);
/* we need a message queue for the thread running the shell in order to
* receive potentially fast incoming networking packets */
msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE);

/* Global wolfSSL initialization */
wolfSSL_Init();
wolfSSL_Debugging_ON();

/* dp3t */
dp3t_start();

/* Start Bluetooth service by default */
//gatt_server();
/* Start dp3t gatt advertisements */
gatt_server();

/* Start dp3t scan service */
dp3t_blescan_init();

/* start shell */
printf( "All up, running the shell now\r\n");
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);

/* should be never reached */
return 0;
}

0 comments on commit 336483e

Please sign in to comment.