mirror of
https://gitee.com/beecue/fastbee.git
synced 2026-02-06 00:55:57 +08:00
添加智能灯固件代码
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(blehr)
|
||||
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := blehr
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
@@ -0,0 +1,116 @@
|
||||
| Supported Targets | ESP32 |
|
||||
| ----------------- | ----- |
|
||||
|
||||
# BLE Heart Rate Measurement example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
This example creates GATT server demonstrating standard Heart Rate measurement service. It simulates Hear rate measurement and notifies to client when the notifications are enabled.
|
||||
|
||||
It uses ESP32's Bluetooth controller and NimBLE stack based BLE host
|
||||
|
||||
This example aims at understanding notification subscriptions and sending notifications.
|
||||
|
||||
To test this demo, any BLE scanner app can be used.
|
||||
|
||||
A Python based utility `blehr_test.py` is also provided (which will run as a BLE GATT Client) and can be used to test this example.
|
||||
|
||||
Note :
|
||||
|
||||
* Make sure to run `python -m pip install --user -r $IDF_PATH/requirements.txt -r $IDF_PATH/tools/ble/requirements.txt` to install the dependency packages needed.
|
||||
* Currently this Python utility is only supported on Linux (BLE communication is via BLuez + DBus).
|
||||
|
||||
|
||||
## How to use example
|
||||
|
||||
### Configure the project
|
||||
|
||||
```
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
idf.py -p PORT flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
This console output can be observed when blehr is connected to client and client enables notifications:
|
||||
|
||||
```
|
||||
I (91) BTDM_INIT: BT controller compile version [fe7ced0]
|
||||
I (91) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
|
||||
I (181) phy: phy_version: 4100, 6fa5e27, Jan 25 2019, 17:02:06, 0, 0
|
||||
I (421) NimBLE_BLE_HeartRate: BLE Host Task Started
|
||||
GAP procedure initiated: stop advertising.
|
||||
Device Address: xx:xx:xx:xx:xx:xx
|
||||
GAP procedure initiated: advertise; disc_mode=2 adv_channel_map=0 own_addr_type=0 adv_filter_policy=0 adv_itvl_min=0 adv_itvl_max=0
|
||||
connection established; status=0
|
||||
subscribe event; cur_notify=1
|
||||
value handle; val_handle=3
|
||||
I (21611) BLE_GAP_SUBSCRIBE_EVENT: conn_handle from subscribe=0
|
||||
GATT procedure initiated: notify; att_handle=3
|
||||
GATT procedure initiated: notify; att_handle=3
|
||||
GATT procedure initiated: notify; att_handle=3
|
||||
GATT procedure initiated: notify; att_handle=3
|
||||
GATT procedure initiated: notify; att_handle=3
|
||||
GATT procedure initiated: notify; att_handle=3
|
||||
GATT procedure initiated: notify; att_handle=3
|
||||
|
||||
```
|
||||
|
||||
## Running Python Utility
|
||||
|
||||
```
|
||||
python blehr_test.py
|
||||
```
|
||||
|
||||
## Python Utility Output
|
||||
|
||||
This is this output seen on the python side on successful connection:
|
||||
|
||||
```
|
||||
discovering adapter...
|
||||
bluetooth adapter discovered
|
||||
powering on adapter...
|
||||
bluetooth adapter powered on
|
||||
|
||||
Started Discovery
|
||||
|
||||
Connecting to device...
|
||||
|
||||
Connected to device
|
||||
|
||||
Services
|
||||
|
||||
[dbus.String(u'00001801-0000-1000-8000-00805f9b34fb', variant_level=1), dbus.String(u'0000180d-0000-1000-8000-00805f9b34fb', variant_level=1), dbus.String(u'0000180a-0000-1000-8000-00805f9b34fb', variant_level=1)]
|
||||
|
||||
Subscribe to notifications: On
|
||||
dbus.Array([dbus.Byte(6), dbus.Byte(90)], signature=dbus.Signature('y'), variant_level=1)
|
||||
dbus.Array([dbus.Byte(6), dbus.Byte(91)], signature=dbus.Signature('y'), variant_level=1)
|
||||
dbus.Array([dbus.Byte(6), dbus.Byte(92)], signature=dbus.Signature('y'), variant_level=1)
|
||||
dbus.Array([dbus.Byte(6), dbus.Byte(93)], signature=dbus.Signature('y'), variant_level=1)
|
||||
dbus.Array([dbus.Byte(6), dbus.Byte(94)], signature=dbus.Signature('y'), variant_level=1)
|
||||
dbus.Array([dbus.Byte(6), dbus.Byte(95)], signature=dbus.Signature('y'), variant_level=1)
|
||||
dbus.Array([dbus.Byte(6), dbus.Byte(96)], signature=dbus.Signature('y'), variant_level=1)
|
||||
dbus.Array([dbus.Byte(6), dbus.Byte(97)], signature=dbus.Signature('y'), variant_level=1)
|
||||
dbus.Array([dbus.Byte(6), dbus.Byte(98)], signature=dbus.Signature('y'), variant_level=1)
|
||||
dbus.Array([dbus.Byte(6), dbus.Byte(99)], signature=dbus.Signature('y'), variant_level=1)
|
||||
|
||||
Subscribe to notifications: Off
|
||||
Success: blehr example test passed
|
||||
|
||||
exiting from test...
|
||||
disconnecting device...
|
||||
device disconnected
|
||||
powering off adapter...
|
||||
bluetooth adapter powered off
|
||||
```
|
||||
@@ -0,0 +1,153 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from __future__ import print_function
|
||||
import os
|
||||
import re
|
||||
import threading
|
||||
import traceback
|
||||
import subprocess
|
||||
|
||||
from tiny_test_fw import Utility
|
||||
import ttfw_idf
|
||||
from ble import lib_ble_client
|
||||
|
||||
try:
|
||||
import Queue
|
||||
except ImportError:
|
||||
import queue as Queue
|
||||
|
||||
# When running on local machine execute the following before running this script
|
||||
# > make app bootloader
|
||||
# > make print_flash_cmd | tail -n 1 > build/download.config
|
||||
|
||||
|
||||
def blehr_client_task(hr_obj, dut_addr):
|
||||
interface = 'hci0'
|
||||
ble_devname = 'blehr_sensor_1.0'
|
||||
hr_srv_uuid = '180d'
|
||||
hr_char_uuid = '2a37'
|
||||
|
||||
# Get BLE client module
|
||||
ble_client_obj = lib_ble_client.BLE_Bluez_Client(interface, devname=ble_devname, devaddr=dut_addr)
|
||||
if not ble_client_obj:
|
||||
raise RuntimeError("Failed to get DBus-Bluez object")
|
||||
|
||||
# Discover Bluetooth Adapter and power on
|
||||
is_adapter_set = ble_client_obj.set_adapter()
|
||||
if not is_adapter_set:
|
||||
raise RuntimeError("Adapter Power On failed !!")
|
||||
|
||||
# Connect BLE Device
|
||||
is_connected = ble_client_obj.connect()
|
||||
if not is_connected:
|
||||
# Call disconnect to perform cleanup operations before exiting application
|
||||
ble_client_obj.disconnect()
|
||||
raise RuntimeError("Connection to device " + str(ble_devname) + " failed !!")
|
||||
|
||||
# Read Services
|
||||
services_ret = ble_client_obj.get_services()
|
||||
if services_ret:
|
||||
Utility.console_log("\nServices\n")
|
||||
Utility.console_log(str(services_ret))
|
||||
else:
|
||||
ble_client_obj.disconnect()
|
||||
raise RuntimeError("Failure: Read Services failed")
|
||||
|
||||
'''
|
||||
Blehr application run:
|
||||
Start Notifications
|
||||
Retrieve updated value
|
||||
Stop Notifications
|
||||
'''
|
||||
blehr_ret = ble_client_obj.hr_update_simulation(hr_srv_uuid, hr_char_uuid)
|
||||
if blehr_ret:
|
||||
Utility.console_log("Success: blehr example test passed")
|
||||
else:
|
||||
raise RuntimeError("Failure: blehr example test failed")
|
||||
|
||||
# Call disconnect to perform cleanup operations before exiting application
|
||||
ble_client_obj.disconnect()
|
||||
|
||||
|
||||
class BleHRThread(threading.Thread):
|
||||
def __init__(self, dut_addr, exceptions_queue):
|
||||
threading.Thread.__init__(self)
|
||||
self.dut_addr = dut_addr
|
||||
self.exceptions_queue = exceptions_queue
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
blehr_client_task(self, self.dut_addr)
|
||||
except Exception:
|
||||
self.exceptions_queue.put(traceback.format_exc(), block=False)
|
||||
|
||||
|
||||
@ttfw_idf.idf_example_test(env_tag="Example_WIFI_BT")
|
||||
def test_example_app_ble_hr(env, extra_data):
|
||||
"""
|
||||
Steps:
|
||||
1. Discover Bluetooth Adapter and Power On
|
||||
2. Connect BLE Device
|
||||
3. Start Notifications
|
||||
4. Updated value is retrieved
|
||||
5. Stop Notifications
|
||||
"""
|
||||
subprocess.check_output(['rm','-rf','/var/lib/bluetooth/*'])
|
||||
subprocess.check_output(['hciconfig','hci0','reset'])
|
||||
|
||||
# Acquire DUT
|
||||
dut = env.get_dut("blehr", "examples/bluetooth/nimble/blehr", dut_class=ttfw_idf.ESP32DUT)
|
||||
|
||||
# Get binary file
|
||||
binary_file = os.path.join(dut.app.binary_path, "blehr.bin")
|
||||
bin_size = os.path.getsize(binary_file)
|
||||
ttfw_idf.log_performance("blehr_bin_size", "{}KB".format(bin_size // 1024))
|
||||
ttfw_idf.check_performance("blehr_bin_size", bin_size // 1024, dut.TARGET)
|
||||
|
||||
# Upload binary and start testing
|
||||
Utility.console_log("Starting blehr simple example test app")
|
||||
dut.start_app()
|
||||
dut.reset()
|
||||
|
||||
# Get device address from dut
|
||||
dut_addr = dut.expect(re.compile(r"Device Address: ([a-fA-F0-9:]+)"), timeout=30)[0]
|
||||
exceptions_queue = Queue.Queue()
|
||||
# Starting a py-client in a separate thread
|
||||
blehr_thread_obj = BleHRThread(dut_addr, exceptions_queue)
|
||||
blehr_thread_obj.start()
|
||||
blehr_thread_obj.join()
|
||||
|
||||
exception_msg = None
|
||||
while True:
|
||||
try:
|
||||
exception_msg = exceptions_queue.get(block=False)
|
||||
except Queue.Empty:
|
||||
break
|
||||
else:
|
||||
Utility.console_log("\n" + exception_msg)
|
||||
|
||||
if exception_msg:
|
||||
raise Exception("Thread did not run successfully")
|
||||
|
||||
# Check dut responses
|
||||
dut.expect("subscribe event; cur_notify=1", timeout=30)
|
||||
dut.expect("subscribe event; cur_notify=0", timeout=30)
|
||||
dut.expect("disconnect;", timeout=30)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_example_app_ble_hr()
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "main.c" "gatt_svr.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
#ifndef H_BLEHR_SENSOR_
|
||||
#define H_BLEHR_SENSOR_
|
||||
|
||||
#include "nimble/ble.h"
|
||||
#include "modlog/modlog.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Heart-rate configuration */
|
||||
#define GATT_HRS_UUID 0x180D
|
||||
#define GATT_HRS_MEASUREMENT_UUID 0x2A37
|
||||
#define GATT_HRS_BODY_SENSOR_LOC_UUID 0x2A38
|
||||
#define GATT_DEVICE_INFO_UUID 0x180A
|
||||
#define GATT_MANUFACTURER_NAME_UUID 0x2A29
|
||||
#define GATT_MODEL_NUMBER_UUID 0x2A24
|
||||
|
||||
extern uint16_t hrs_hrm_handle;
|
||||
|
||||
struct ble_hs_cfg;
|
||||
struct ble_gatt_register_ctxt;
|
||||
|
||||
void gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg);
|
||||
int gatt_svr_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,4 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "host/ble_hs.h"
|
||||
#include "host/ble_uuid.h"
|
||||
#include "services/gap/ble_svc_gap.h"
|
||||
#include "services/gatt/ble_svc_gatt.h"
|
||||
#include "blehr_sens.h"
|
||||
|
||||
static const char *manuf_name = "Apache Mynewt ESP32 devkitC";
|
||||
static const char *model_num = "Mynewt HR Sensor demo";
|
||||
uint16_t hrs_hrm_handle;
|
||||
|
||||
static int
|
||||
gatt_svr_chr_access_heart_rate(uint16_t conn_handle, uint16_t attr_handle,
|
||||
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||
|
||||
static int
|
||||
gatt_svr_chr_access_device_info(uint16_t conn_handle, uint16_t attr_handle,
|
||||
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||
|
||||
static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
|
||||
{
|
||||
/* Service: Heart-rate */
|
||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||
.uuid = BLE_UUID16_DECLARE(GATT_HRS_UUID),
|
||||
.characteristics = (struct ble_gatt_chr_def[])
|
||||
{ {
|
||||
/* Characteristic: Heart-rate measurement */
|
||||
.uuid = BLE_UUID16_DECLARE(GATT_HRS_MEASUREMENT_UUID),
|
||||
.access_cb = gatt_svr_chr_access_heart_rate,
|
||||
.val_handle = &hrs_hrm_handle,
|
||||
.flags = BLE_GATT_CHR_F_NOTIFY,
|
||||
}, {
|
||||
/* Characteristic: Body sensor location */
|
||||
.uuid = BLE_UUID16_DECLARE(GATT_HRS_BODY_SENSOR_LOC_UUID),
|
||||
.access_cb = gatt_svr_chr_access_heart_rate,
|
||||
.flags = BLE_GATT_CHR_F_READ,
|
||||
}, {
|
||||
0, /* No more characteristics in this service */
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
/* Service: Device Information */
|
||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||
.uuid = BLE_UUID16_DECLARE(GATT_DEVICE_INFO_UUID),
|
||||
.characteristics = (struct ble_gatt_chr_def[])
|
||||
{ {
|
||||
/* Characteristic: * Manufacturer name */
|
||||
.uuid = BLE_UUID16_DECLARE(GATT_MANUFACTURER_NAME_UUID),
|
||||
.access_cb = gatt_svr_chr_access_device_info,
|
||||
.flags = BLE_GATT_CHR_F_READ,
|
||||
}, {
|
||||
/* Characteristic: Model number string */
|
||||
.uuid = BLE_UUID16_DECLARE(GATT_MODEL_NUMBER_UUID),
|
||||
.access_cb = gatt_svr_chr_access_device_info,
|
||||
.flags = BLE_GATT_CHR_F_READ,
|
||||
}, {
|
||||
0, /* No more characteristics in this service */
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
0, /* No more services */
|
||||
},
|
||||
};
|
||||
|
||||
static int
|
||||
gatt_svr_chr_access_heart_rate(uint16_t conn_handle, uint16_t attr_handle,
|
||||
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||
{
|
||||
/* Sensor location, set to "Chest" */
|
||||
static uint8_t body_sens_loc = 0x01;
|
||||
uint16_t uuid;
|
||||
int rc;
|
||||
|
||||
uuid = ble_uuid_u16(ctxt->chr->uuid);
|
||||
|
||||
if (uuid == GATT_HRS_BODY_SENSOR_LOC_UUID) {
|
||||
rc = os_mbuf_append(ctxt->om, &body_sens_loc, sizeof(body_sens_loc));
|
||||
|
||||
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||
}
|
||||
|
||||
assert(0);
|
||||
return BLE_ATT_ERR_UNLIKELY;
|
||||
}
|
||||
|
||||
static int
|
||||
gatt_svr_chr_access_device_info(uint16_t conn_handle, uint16_t attr_handle,
|
||||
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||
{
|
||||
uint16_t uuid;
|
||||
int rc;
|
||||
|
||||
uuid = ble_uuid_u16(ctxt->chr->uuid);
|
||||
|
||||
if (uuid == GATT_MODEL_NUMBER_UUID) {
|
||||
rc = os_mbuf_append(ctxt->om, model_num, strlen(model_num));
|
||||
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||
}
|
||||
|
||||
if (uuid == GATT_MANUFACTURER_NAME_UUID) {
|
||||
rc = os_mbuf_append(ctxt->om, manuf_name, strlen(manuf_name));
|
||||
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||
}
|
||||
|
||||
assert(0);
|
||||
return BLE_ATT_ERR_UNLIKELY;
|
||||
}
|
||||
|
||||
void
|
||||
gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
|
||||
{
|
||||
char buf[BLE_UUID_STR_LEN];
|
||||
|
||||
switch (ctxt->op) {
|
||||
case BLE_GATT_REGISTER_OP_SVC:
|
||||
MODLOG_DFLT(DEBUG, "registered service %s with handle=%d\n",
|
||||
ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
|
||||
ctxt->svc.handle);
|
||||
break;
|
||||
|
||||
case BLE_GATT_REGISTER_OP_CHR:
|
||||
MODLOG_DFLT(DEBUG, "registering characteristic %s with "
|
||||
"def_handle=%d val_handle=%d\n",
|
||||
ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
|
||||
ctxt->chr.def_handle,
|
||||
ctxt->chr.val_handle);
|
||||
break;
|
||||
|
||||
case BLE_GATT_REGISTER_OP_DSC:
|
||||
MODLOG_DFLT(DEBUG, "registering descriptor %s with handle=%d\n",
|
||||
ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
|
||||
ctxt->dsc.handle);
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
gatt_svr_init(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
ble_svc_gap_init();
|
||||
ble_svc_gatt_init();
|
||||
|
||||
rc = ble_gatts_count_cfg(gatt_svr_svcs);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = ble_gatts_add_svcs(gatt_svr_svcs);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,304 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "freertos/FreeRTOSConfig.h"
|
||||
/* BLE */
|
||||
#include "esp_nimble_hci.h"
|
||||
#include "nimble/nimble_port.h"
|
||||
#include "nimble/nimble_port_freertos.h"
|
||||
#include "host/ble_hs.h"
|
||||
#include "host/util/util.h"
|
||||
#include "console/console.h"
|
||||
#include "services/gap/ble_svc_gap.h"
|
||||
#include "blehr_sens.h"
|
||||
|
||||
static const char *tag = "NimBLE_BLE_HeartRate";
|
||||
|
||||
static xTimerHandle blehr_tx_timer;
|
||||
|
||||
static bool notify_state;
|
||||
|
||||
static uint16_t conn_handle;
|
||||
|
||||
static const char *device_name = "blehr_sensor_1.0";
|
||||
|
||||
static int blehr_gap_event(struct ble_gap_event *event, void *arg);
|
||||
|
||||
static uint8_t blehr_addr_type;
|
||||
|
||||
/* Variable to simulate heart beats */
|
||||
static uint8_t heartrate = 90;
|
||||
|
||||
/**
|
||||
* Utility function to log an array of bytes.
|
||||
*/
|
||||
void
|
||||
print_bytes(const uint8_t *bytes, int len)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
MODLOG_DFLT(INFO, "%s0x%02x", i != 0 ? ":" : "", bytes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
print_addr(const void *addr)
|
||||
{
|
||||
const uint8_t *u8p;
|
||||
|
||||
u8p = addr;
|
||||
MODLOG_DFLT(INFO, "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Enables advertising with parameters:
|
||||
* o General discoverable mode
|
||||
* o Undirected connectable mode
|
||||
*/
|
||||
static void
|
||||
blehr_advertise(void)
|
||||
{
|
||||
struct ble_gap_adv_params adv_params;
|
||||
struct ble_hs_adv_fields fields;
|
||||
int rc;
|
||||
|
||||
/*
|
||||
* Set the advertisement data included in our advertisements:
|
||||
* o Flags (indicates advertisement type and other general info)
|
||||
* o Advertising tx power
|
||||
* o Device name
|
||||
*/
|
||||
memset(&fields, 0, sizeof(fields));
|
||||
|
||||
/*
|
||||
* Advertise two flags:
|
||||
* o Discoverability in forthcoming advertisement (general)
|
||||
* o BLE-only (BR/EDR unsupported)
|
||||
*/
|
||||
fields.flags = BLE_HS_ADV_F_DISC_GEN |
|
||||
BLE_HS_ADV_F_BREDR_UNSUP;
|
||||
|
||||
/*
|
||||
* Indicate that the TX power level field should be included; have the
|
||||
* stack fill this value automatically. This is done by assigning the
|
||||
* special value BLE_HS_ADV_TX_PWR_LVL_AUTO.
|
||||
*/
|
||||
fields.tx_pwr_lvl_is_present = 1;
|
||||
fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
|
||||
|
||||
fields.name = (uint8_t *)device_name;
|
||||
fields.name_len = strlen(device_name);
|
||||
fields.name_is_complete = 1;
|
||||
|
||||
rc = ble_gap_adv_set_fields(&fields);
|
||||
if (rc != 0) {
|
||||
MODLOG_DFLT(ERROR, "error setting advertisement data; rc=%d\n", rc);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Begin advertising */
|
||||
memset(&adv_params, 0, sizeof(adv_params));
|
||||
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
|
||||
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
|
||||
rc = ble_gap_adv_start(blehr_addr_type, NULL, BLE_HS_FOREVER,
|
||||
&adv_params, blehr_gap_event, NULL);
|
||||
if (rc != 0) {
|
||||
MODLOG_DFLT(ERROR, "error enabling advertisement; rc=%d\n", rc);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
blehr_tx_hrate_stop(void)
|
||||
{
|
||||
xTimerStop( blehr_tx_timer, 1000 / portTICK_PERIOD_MS );
|
||||
}
|
||||
|
||||
/* Reset heart rate measurement */
|
||||
static void
|
||||
blehr_tx_hrate_reset(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
if (xTimerReset(blehr_tx_timer, 1000 / portTICK_PERIOD_MS ) == pdPASS) {
|
||||
rc = 0;
|
||||
} else {
|
||||
rc = 1;
|
||||
}
|
||||
|
||||
assert(rc == 0);
|
||||
|
||||
}
|
||||
|
||||
/* This function simulates heart beat and notifies it to the client */
|
||||
static void
|
||||
blehr_tx_hrate(xTimerHandle ev)
|
||||
{
|
||||
static uint8_t hrm[2];
|
||||
int rc;
|
||||
struct os_mbuf *om;
|
||||
|
||||
if (!notify_state) {
|
||||
blehr_tx_hrate_stop();
|
||||
heartrate = 90;
|
||||
return;
|
||||
}
|
||||
|
||||
hrm[0] = 0x06; /* contact of a sensor */
|
||||
hrm[1] = heartrate; /* storing dummy data */
|
||||
|
||||
/* Simulation of heart beats */
|
||||
heartrate++;
|
||||
if (heartrate == 160) {
|
||||
heartrate = 90;
|
||||
}
|
||||
|
||||
om = ble_hs_mbuf_from_flat(hrm, sizeof(hrm));
|
||||
rc = ble_gattc_notify_custom(conn_handle, hrs_hrm_handle, om);
|
||||
|
||||
assert(rc == 0);
|
||||
|
||||
blehr_tx_hrate_reset();
|
||||
}
|
||||
|
||||
static int
|
||||
blehr_gap_event(struct ble_gap_event *event, void *arg)
|
||||
{
|
||||
switch (event->type) {
|
||||
case BLE_GAP_EVENT_CONNECT:
|
||||
/* A new connection was established or a connection attempt failed */
|
||||
MODLOG_DFLT(INFO, "connection %s; status=%d\n",
|
||||
event->connect.status == 0 ? "established" : "failed",
|
||||
event->connect.status);
|
||||
|
||||
if (event->connect.status != 0) {
|
||||
/* Connection failed; resume advertising */
|
||||
blehr_advertise();
|
||||
}
|
||||
conn_handle = event->connect.conn_handle;
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_DISCONNECT:
|
||||
MODLOG_DFLT(INFO, "disconnect; reason=%d\n", event->disconnect.reason);
|
||||
|
||||
/* Connection terminated; resume advertising */
|
||||
blehr_advertise();
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_ADV_COMPLETE:
|
||||
MODLOG_DFLT(INFO, "adv complete\n");
|
||||
blehr_advertise();
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_SUBSCRIBE:
|
||||
MODLOG_DFLT(INFO, "subscribe event; cur_notify=%d\n value handle; "
|
||||
"val_handle=%d\n",
|
||||
event->subscribe.cur_notify, hrs_hrm_handle);
|
||||
if (event->subscribe.attr_handle == hrs_hrm_handle) {
|
||||
notify_state = event->subscribe.cur_notify;
|
||||
blehr_tx_hrate_reset();
|
||||
} else if (event->subscribe.attr_handle != hrs_hrm_handle) {
|
||||
notify_state = event->subscribe.cur_notify;
|
||||
blehr_tx_hrate_stop();
|
||||
}
|
||||
ESP_LOGI("BLE_GAP_SUBSCRIBE_EVENT", "conn_handle from subscribe=%d", conn_handle);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_MTU:
|
||||
MODLOG_DFLT(INFO, "mtu update event; conn_handle=%d mtu=%d\n",
|
||||
event->mtu.conn_handle,
|
||||
event->mtu.value);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
blehr_on_sync(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = ble_hs_id_infer_auto(0, &blehr_addr_type);
|
||||
assert(rc == 0);
|
||||
|
||||
uint8_t addr_val[6] = {0};
|
||||
rc = ble_hs_id_copy_addr(blehr_addr_type, addr_val, NULL);
|
||||
|
||||
MODLOG_DFLT(INFO, "Device Address: ");
|
||||
print_addr(addr_val);
|
||||
MODLOG_DFLT(INFO, "\n");
|
||||
|
||||
/* Begin advertising */
|
||||
blehr_advertise();
|
||||
}
|
||||
|
||||
static void
|
||||
blehr_on_reset(int reason)
|
||||
{
|
||||
MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
|
||||
}
|
||||
|
||||
void blehr_host_task(void *param)
|
||||
{
|
||||
ESP_LOGI(tag, "BLE Host Task Started");
|
||||
/* This function will return only when nimble_port_stop() is executed */
|
||||
nimble_port_run();
|
||||
|
||||
nimble_port_freertos_deinit();
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
/* Initialize NVS — it is used to store PHY calibration data */
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
|
||||
|
||||
nimble_port_init();
|
||||
/* Initialize the NimBLE host configuration */
|
||||
ble_hs_cfg.sync_cb = blehr_on_sync;
|
||||
ble_hs_cfg.reset_cb = blehr_on_reset;
|
||||
|
||||
/* name, period/time, auto reload, timer ID, callback */
|
||||
blehr_tx_timer = xTimerCreate("blehr_tx_timer", pdMS_TO_TICKS(1000), pdTRUE, (void *)0, blehr_tx_hrate);
|
||||
|
||||
rc = gatt_svr_init();
|
||||
assert(rc == 0);
|
||||
|
||||
/* Set the default device name */
|
||||
rc = ble_svc_gap_device_name_set(device_name);
|
||||
assert(rc == 0);
|
||||
|
||||
/* Start the task */
|
||||
nimble_port_freertos_init(blehr_host_task);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
CONFIG_FREERTOS_UNICORE=y
|
||||
CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY=y
|
||||
CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_IRAM_8BIT=y
|
||||
@@ -0,0 +1,12 @@
|
||||
# Override some defaults so BT stack is enabled
|
||||
# in this example
|
||||
|
||||
#
|
||||
# BT config
|
||||
#
|
||||
CONFIG_BT_ENABLED=y
|
||||
CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y
|
||||
CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n
|
||||
CONFIG_BTDM_CTRL_MODE_BTDM=n
|
||||
CONFIG_BT_BLUEDROID_ENABLED=n
|
||||
CONFIG_BT_NIMBLE_ENABLED=y
|
||||
Reference in New Issue
Block a user