1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/examples/nimble_gatt/main.c

113 lines
2.6 KiB
C
Raw Normal View History

/*
* Copyright (C) 2018 Freie Universität Berlin
* 2018 Codecoup
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup examples
* @{
*
* @file
* @brief BLE peripheral example using NimBLE
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
* @author Andrzej Kaczmarek <andrzej.kaczmarek@codecoup.pl>
*
* @}
*/
#include <stdio.h>
#include <string.h>
2018-12-07 18:05:00 +01:00
#include "net/bluetil/ad.h"
#include "host/ble_hs.h"
#include "host/util/util.h"
2018-10-19 14:00:36 +02:00
#include "host/ble_gatt.h"
#include "services/gap/ble_svc_gap.h"
#include "services/gatt/ble_svc_gatt.h"
2018-12-07 18:05:00 +01:00
static const char *device_name = "NimBLE on RIOT";
static uint8_t own_addr_type;
static void start_advertise(void);
static void update_ad(void)
{
2018-12-07 18:05:00 +01:00
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;
switch (event->type) {
case BLE_GAP_EVENT_CONNECT:
if (event->connect.status) {
start_advertise();
}
break;
case BLE_GAP_EVENT_DISCONNECT:
start_advertise();
break;
}
return 0;
}
static void start_advertise(void)
{
struct ble_gap_adv_params advp;
int rc;
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,
&advp, gap_event_cb, NULL);
assert(rc == 0);
(void)rc;
}
2018-10-19 14:00:36 +02:00
int main(void)
{
2018-10-19 14:00:36 +02:00
puts("NimBLE GATT Server Example");
2018-10-19 14:00:36 +02:00
/* set the device name */
ble_svc_gap_device_name_set(device_name);
2018-10-19 14:00:36 +02:00
/* initialize the GAP and GATT services */
ble_svc_gap_init();
ble_svc_gatt_init();
/* XXX: seems to be needed to apply the 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 */
int 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();
2018-10-19 14:00:36 +02:00
/* start to advertise this node */
start_advertise();
return 0;
}