From d2b00882def635e5a4270834956dff1473640659 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 11 Oct 2017 12:59:14 +0700 Subject: [PATCH] homekit advertising regconized --- changelog.md | 2 +- libraries/BLEHomekit/BLEHomekit.cpp | 89 +++++++++++++++++++ libraries/BLEHomekit/BLEHomekit.h | 31 +++++++ .../homekit_lightbulb/homekit_lightbulb.ino | 14 +-- .../Bluefruit52Lib/src/BLEAdvertising.cpp | 43 ++++++--- libraries/Bluefruit52Lib/src/BLEAdvertising.h | 8 +- libraries/Bluefruit52Lib/src/BLEUuid.h | 13 ++- .../Bluefruit52Lib/src/services/BLEBeacon.cpp | 2 +- 8 files changed, 180 insertions(+), 22 deletions(-) create mode 100644 libraries/BLEHomekit/BLEHomekit.cpp diff --git a/changelog.md b/changelog.md index 8f7544f8..3d191b7b 100644 --- a/changelog.md +++ b/changelog.md @@ -10,7 +10,7 @@ - Upgrade Bluetooth 5 with Softdevice to S132 v5.0.0 - Support max ATT MTU up to 247 (negotiable with peer) -- Added BLEAdvertising multiple services addition API +- Added BLEAdvertising multiple services aaddition and addManufacturerData() API - Fixed an discovery bug introduced in 0.7.5 ## 0.7.5 diff --git a/libraries/BLEHomekit/BLEHomekit.cpp b/libraries/BLEHomekit/BLEHomekit.cpp new file mode 100644 index 00000000..816e8bb9 --- /dev/null +++ b/libraries/BLEHomekit/BLEHomekit.cpp @@ -0,0 +1,89 @@ +/**************************************************************************/ +/*! + @file BLEHomekit.cpp + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + +#include +#include "BLEHomekit.h" + +bool BLEHomekit::setAdv(BLEAdvertisingData& adv_ref) +{ + VERIFY(&Bluefruit.Advertising == &adv_ref); + BLEAdvertising& adv = (BLEAdvertising&) adv_ref; + + // Calculate AIL field + uint16_t interval_ms = MS625TO1000( adv.getInterval() ); + uint8_t ail; + + if ( interval_ms > 2500 ) ail = 0xE0; + else if ( interval_ms > 1250 ) ail = 0xC0; + + else if ( interval_ms > 500 ) ail = 0xA0; + else if ( interval_ms > 300 ) ail = 0x80; + else if ( interval_ms > 100 ) ail = 0x60; + else if ( interval_ms > 25 ) ail = 0x40; + else ail = 0x20; + + ail |= 0x0D; + + struct ATTR_PACKED + { + uint16_t company_id; + uint8_t type; + uint8_t adv_int_len; + uint8_t status_flag; + uint8_t dev_id[6]; + uint16_t category; + uint16_t gsn; + uint8_t config_num; + uint8_t compatible_verion; + }data = + { + .company_id = UUID16_COMPANY_ID_APPLE, + .type = 0x06, + .adv_int_len = ail, + .status_flag = 1, // bit0 = HAP Pairing Status Flag (1 not paired) + .dev_id = { 0x0a, 0xbc, 0x12, 0x33, 0x56, 0x98 }, + .category = HAP_CAT_LIGHTBULB, + .gsn = 1, + .config_num = 1, + .compatible_verion = HOMEKIT_PROTOCOL_VERION + }; + + VERIFY_STATIC( sizeof(data) == 17); + + adv.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + return adv.addManufacturerData(&data, sizeof(data)); +} + diff --git a/libraries/BLEHomekit/BLEHomekit.h b/libraries/BLEHomekit/BLEHomekit.h index ec647293..76aacf29 100644 --- a/libraries/BLEHomekit/BLEHomekit.h +++ b/libraries/BLEHomekit/BLEHomekit.h @@ -39,6 +39,37 @@ #include "HapUuid.h" #include "HapAccessoryInfo.h" +enum +{ + HAP_CAT_OTHER = 1 , + HAP_CAT_BRIDGE , + HAP_CAT_FAN , + HAP_CAT_GARAGE , + HAP_CAT_LIGHTBULB , + HAP_CAT_DOOR_LOCK , + HAP_CAT_OUTLET , + HAP_CAT_SWITCH , + HAP_CAT_THERMOSTAT , + HAP_CAT_SENSOR , + HAP_CAT_SECURITY_SYSTEM , + HAP_CAT_DOOR , + HAP_CAT_WINDOWS , + HAP_CAT_WINDOWS_COVERING , + HAP_CAT_PROGRAMMABLE_SWITCH , + HAP_CAT_RANGE_EXTENDER , + HAP_CAT_IP_CAMERA , + HAP_CAT_VIDEO_DOOR_BELL , + HAP_CAT_AIR_PURFIER , +}; +#define HOMEKIT_PROTOCOL_VERION 2 + +class BLEHomekit : public Advertisable +{ + public: + virtual bool setAdv(BLEAdvertisingData& adv); + + private: +}; #endif /* BLEHOMEKIT_H_ */ diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/homekit_lightbulb/homekit_lightbulb.ino b/libraries/Bluefruit52Lib/examples/Peripheral/homekit_lightbulb/homekit_lightbulb.ino index 22c90186..a1414b50 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/homekit_lightbulb/homekit_lightbulb.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/homekit_lightbulb/homekit_lightbulb.ino @@ -15,6 +15,7 @@ #include #include +BLEHomekit homekit; HAPAccessoryInfo hap_info; void setup() @@ -39,11 +40,6 @@ void setup() void startAdv(void) { - // Advertising packet - Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); - Bluefruit.Advertising.addTxPower(); - Bluefruit.Advertising.addName(); - /* Start Advertising * - Enable auto advertising if disconnected * - Interval: fast mode = 20 ms, slow mode = 152.5 ms @@ -54,8 +50,14 @@ void startAdv(void) * https://developer.apple.com/library/content/qa/qa1931/_index.html */ Bluefruit.Advertising.restartOnDisconnect(true); - Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setInterval(32, 32); // in unit of 0.625 ms Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + + // Homekit include advertising interval as part of its data + // Therefor setData(homekit) must be after setInterval() + Bluefruit.Advertising.setData(homekit); + + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds } diff --git a/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp b/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp index d478c1ba..5fefcf30 100644 --- a/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp +++ b/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp @@ -209,6 +209,11 @@ bool BLEAdvertisingData::addAppearance(uint16_t appearance) return addData(BLE_GAP_AD_TYPE_APPEARANCE, &appearance, 2); } +bool BLEAdvertisingData::addManufacturerData(const void* data, uint8_t count) +{ + return addData(BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, data, count); +} + /*------------------------------------------------------------------*/ /* CUSTOM API *------------------------------------------------------------------*/ @@ -247,12 +252,13 @@ BLEAdvertising::BLEAdvertising(void) _start_if_disconnect = true; _runnning = false; - _fast_interval = BLE_ADV_INTERVAL_FAST_DFLT; - _slow_interval = BLE_ADV_INTERVAL_SLOW_DFLT; + _fast_interval = BLE_ADV_INTERVAL_FAST_DFLT; + _slow_interval = BLE_ADV_INTERVAL_SLOW_DFLT; + _active_interval = _fast_interval; - _fast_timeout = BLE_ADV_FAST_TIMEOUT_DFLT; - _stop_timeout = _left_timeout = 0; - _stop_cb = NULL; + _fast_timeout = BLE_ADV_FAST_TIMEOUT_DFLT; + _stop_timeout = _left_timeout = 0; + _stop_cb = NULL; } void BLEAdvertising::setFastTimeout(uint16_t sec) @@ -265,16 +271,32 @@ void BLEAdvertising::setType(uint8_t adv_type) _type = adv_type; } +/** + * Set Interval in unit of 0.625 ms + * @param fast Interval that is used in the first n seconds (configurable) + * @param slow Interval that is used after fast timeout + */ void BLEAdvertising::setInterval(uint16_t fast, uint16_t slow) { - _fast_interval = fast; - _slow_interval = slow; + _fast_interval = fast; + _slow_interval = slow; + + // default is fast since it will be advertising first + _active_interval = _fast_interval; } void BLEAdvertising::setIntervalMS(uint16_t fast, uint16_t slow) { - _fast_interval = MS1000TO625(fast); - _slow_interval = MS1000TO625(slow); + setInterval(MS1000TO625(fast), MS1000TO625(slow)); +} + +/** + * Get current active interval + * @return Either slow or fast interval in unit of 0.625 ms + */ +uint16_t BLEAdvertising::getInterval(void) +{ + return _active_interval; } void BLEAdvertising::setStopCallback(stop_callback_t fp) @@ -325,7 +347,8 @@ bool BLEAdvertising::_start(uint16_t interval, uint16_t timeout) #endif Bluefruit._startConnLed(); // start blinking - _runnning = true; + _runnning = true; + _active_interval = interval; _left_timeout -= min16(_left_timeout, timeout); diff --git a/libraries/Bluefruit52Lib/src/BLEAdvertising.h b/libraries/Bluefruit52Lib/src/BLEAdvertising.h index 6f393f9a..14c2ac08 100644 --- a/libraries/Bluefruit52Lib/src/BLEAdvertising.h +++ b/libraries/Bluefruit52Lib/src/BLEAdvertising.h @@ -84,6 +84,7 @@ public: bool addTxPower(void); bool addName(void); bool addAppearance(uint16_t appearance); + bool addManufacturerData(const void* data, uint8_t count); /*------------- UUID -------------*/ bool addUuid(BLEUuid bleuuid); @@ -124,6 +125,8 @@ public: void setInterval (uint16_t fast, uint16_t slow); void setIntervalMS(uint16_t fast, uint16_t slow); + uint16_t getInterval(void); + bool setBeacon(BLEBeacon& beacon); bool setBeacon(EddyStoneUrl& eddy_url); @@ -145,8 +148,9 @@ private: bool _start_if_disconnect; bool _runnning; - uint16_t _fast_interval; // in 0.625 ms - uint16_t _slow_interval; // in 0.625 ms + uint16_t _fast_interval; // in 0.625 ms + uint16_t _slow_interval; // in 0.625 ms + uint16_t _active_interval; // in 0.625 ms uint16_t _fast_timeout; // in second uint16_t _stop_timeout; // in second diff --git a/libraries/Bluefruit52Lib/src/BLEUuid.h b/libraries/Bluefruit52Lib/src/BLEUuid.h index c73e9176..3bbb9508 100644 --- a/libraries/Bluefruit52Lib/src/BLEUuid.h +++ b/libraries/Bluefruit52Lib/src/BLEUuid.h @@ -72,7 +72,9 @@ class BLEUuid BLEUuid& operator=(ble_uuid_t uuid); }; -// Service UUID +/*------------------------------------------------------------------*/ +/* Service UUID + *------------------------------------------------------------------*/ #define UUID16_SVC_ALERT_NOTIFICATION 0x1811 #define UUID16_SVC_BATTERY 0x180F #define UUID16_SVC_BLOOD_PRESSURE 0x1810 @@ -98,7 +100,9 @@ class BLEUuid #define UUID16_SVC_PLX 0x1822 #define UUID16_SVC_EDDYSTONE 0xFEAA -// Characteristic UUID +/*------------------------------------------------------------------*/ +/* Characteristic UUID + *------------------------------------------------------------------*/ #define UUID16_CHR_REMOVABLE 0x2A3A #define UUID16_CHR_SERVICE_REQUIRED 0x2A3B #define UUID16_CHR_ALERT_CATEGORY_ID 0x2A43 @@ -186,5 +190,10 @@ class BLEUuid #define UUID16_PLX_CONTINUOUS_MEAS 0x2A5F #define UUID16_PLX_FEATURES 0x2A60 +/*------------------------------------------------------------------*/ +/* Company UUID + *------------------------------------------------------------------*/ +#define UUID16_COMPANY_ID_APPLE 0x004C + #endif /* BLEUUID_H_ */ diff --git a/libraries/Bluefruit52Lib/src/services/BLEBeacon.cpp b/libraries/Bluefruit52Lib/src/services/BLEBeacon.cpp index 7a7db1c4..d99991ad 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEBeacon.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEBeacon.cpp @@ -38,7 +38,7 @@ void BLEBeacon::_init(void) { - _manufacturer_id = 0x004C; // default Apple --> iBeacon + _manufacturer_id = UUID16_COMPANY_ID_APPLE; // default is Apple _uuid128 = NULL; _major_be = _minor_be = 0;