1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-26 15:03:53 +01:00

Merge pull request #11131 from haukepetersen/opt_nimble_initialization

pkg/nimble: optimize package initialization
This commit is contained in:
Hauke Petersen 2019-03-07 17:07:55 +01:00 committed by GitHub
commit ed5bd8e13e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 30 deletions

View File

@ -35,6 +35,7 @@
#include <stdlib.h>
#include <string.h>
#include "nimble_riot.h"
#include "net/bluetil/ad.h"
#include "host/ble_hs.h"
@ -65,7 +66,6 @@ static const ble_uuid128_t gatt_svr_chr_rw_demo_readonly_uuid
0x4d, 0xd5, 0x40, 0x3f, 0x11, 0xdd, 0xcc);
static const char *device_name = "NimBLE on RIOT";
static uint8_t own_addr_type;
static char rm_demo_write_data[64] = "This characteristic is read- and writeable!";
@ -132,15 +132,6 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
},
};
static void update_ad(void)
{
uint8_t buf[BLE_HS_ADV_MAX_SZ];
bluetil_ad_t ad;
bluetil_ad_init_with_flags(&ad, buf, sizeof(buf), BLUETIL_AD_FLAGS_DEFAULT);
bluetil_ad_add_name(&ad, device_name);
ble_gap_adv_set_data(ad.buf, ad.pos);
}
static int gap_event_cb(struct ble_gap_event *event, void *arg)
{
(void)arg;
@ -168,7 +159,7 @@ static void start_advertise(void)
memset(&advp, 0, sizeof advp);
advp.conn_mode = BLE_GAP_CONN_MODE_UND;
advp.disc_mode = BLE_GAP_DISC_MODE_GEN;
rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER,
rc = ble_gap_adv_start(nimble_riot_own_addr_type, NULL, BLE_HS_FOREVER,
&advp, gap_event_cb, NULL);
assert(rc == 0);
(void)rc;
@ -320,34 +311,23 @@ int main(void)
int rc = 0;
/* verify and add our custom services */
rc = ble_gatts_count_cfg(gatt_svr_svcs);
assert(rc == 0);
rc = ble_gatts_add_svcs(gatt_svr_svcs);
assert(rc == 0);
/* set the device name */
ble_svc_gap_device_name_set(device_name);
/* initialize the GAP and GATT services */
ble_svc_gap_init();
ble_svc_gatt_init();
/* XXX: seems to be needed to apply the added services */
/* reload the GATT server to link our added services */
ble_gatts_start();
/* make sure synchronization of host and controller is done, this should
* always be the case */
while (!ble_hs_synced()) {}
/* configure device address */
rc = ble_hs_util_ensure_addr(0);
assert(rc == 0);
rc = ble_hs_id_infer_auto(0, &own_addr_type);
assert(rc == 0);
(void)rc;
/* generate the advertising data */
update_ad();
/* configure and set the advertising data */
uint8_t buf[BLE_HS_ADV_MAX_SZ];
bluetil_ad_t ad;
bluetil_ad_init_with_flags(&ad, buf, sizeof(buf), BLUETIL_AD_FLAGS_DEFAULT);
bluetil_ad_add_name(&ad, device_name);
ble_gap_adv_set_data(ad.buf, ad.pos);
/* start to advertise this node */
start_advertise();

View File

@ -53,6 +53,11 @@ extern "C" {
#define NIMBLE_HOST_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
#endif
/**
* @brief Export our own address type for later usage
*/
extern uint8_t nimble_riot_own_addr_type;
/**
* @brief Setup and run NimBLE's controller and host threads
*/

View File

@ -22,6 +22,8 @@
#include "nimble_riot.h"
#include "nimble/nimble_port.h"
#include "host/ble_hs.h"
#include "host/util/util.h"
#include "services/gap/ble_svc_gap.h"
#include "services/gatt/ble_svc_gatt.h"
@ -37,6 +39,8 @@ static char _stack_controller[NIMBLE_CONTROLLER_STACKSIZE];
#ifdef MODULE_NIMBLE_HOST
static char _stack_host[NIMBLE_HOST_STACKSIZE];
uint8_t nimble_riot_own_addr_type;
static void *_host_thread(void *arg)
{
(void)arg;
@ -77,4 +81,24 @@ void nimble_riot_init(void)
_host_thread, NULL,
"nimble_host");
#endif
/* make sure synchronization of host and controller is done, this should
* always be the case at this point */
while (!ble_hs_synced()) {}
/* for reducing code duplication, we read our own address type once here
* so it can be re-used later on */
int res = ble_hs_util_ensure_addr(0);
assert(res == 0);
res = ble_hs_id_infer_auto(0, &nimble_riot_own_addr_type);
assert(res == 0);
(void)res;
/* initialize the configured, build-in services */
#ifdef MODULE_NIMBLE_SVC_GAP
ble_svc_gap_init();
#endif
#ifdef MODULE_NIMBLE_SVC_GATT
ble_svc_gatt_init();
#endif
}