From e81d315f28af424b586b0f6fa7e265689090551f Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 15 Oct 2017 14:57:12 +0700 Subject: [PATCH] add more HAP services: HAPPairing, HAPLightBulb --- libraries/BLEHomekit/BLEHomekit.cpp | 41 +--------- libraries/BLEHomekit/BLEHomekit.h | 7 +- libraries/BLEHomekit/HAPCharacteristic.h | 2 +- libraries/BLEHomekit/service/HAPLightBulb.cpp | 59 ++++++++++++++ libraries/BLEHomekit/service/HAPLightBulb.h | 56 +++++++++++++ libraries/BLEHomekit/service/HAPPairing.cpp | 78 +++++++++++++++++++ libraries/BLEHomekit/service/HAPPairing.h | 58 ++++++++++++++ 7 files changed, 258 insertions(+), 43 deletions(-) create mode 100644 libraries/BLEHomekit/service/HAPLightBulb.cpp create mode 100644 libraries/BLEHomekit/service/HAPLightBulb.h create mode 100644 libraries/BLEHomekit/service/HAPPairing.cpp create mode 100644 libraries/BLEHomekit/service/HAPPairing.h diff --git a/libraries/BLEHomekit/BLEHomekit.cpp b/libraries/BLEHomekit/BLEHomekit.cpp index fd9d747a..cd509beb 100644 --- a/libraries/BLEHomekit/BLEHomekit.cpp +++ b/libraries/BLEHomekit/BLEHomekit.cpp @@ -43,10 +43,8 @@ uint16_t BLEHomekit::_gInstanceID = 1; BLEHomekit::BLEHomekit() - : _protocol_info(HAP_UUID_SVC_PROTOCOL_INFO), AccessoryInfo(), - _pairing(HAP_UUID_SVC_PAIRING), _pair_setup(HAP_UUID_CHR_PAIR_SETUP), _pair_verify(HAP_UUID_CHR_PAIR_VERIFY), - _pair_features(HAP_UUID_SVC_PAIR_FEATURE), _pair_pairing(HAP_UUID_SVC_PAIR_PAIRING), - _lightbulb(HAP_UUID_SVC_LIGHT_BULB), _on(HAP_UUID_CHR_ON) + : _protocol_info(HAP_UUID_SVC_PROTOCOL_INFO), AccessoryInfo(), _pairing(), + _lightbulb() { } @@ -74,7 +72,7 @@ err_t BLEHomekit::begin() /*------------- Protocol Info Service -------------*/ VERIFY_STATUS( _protocol_info.begin() ); { - HAPCharacteristic chr(HAP_UUID_CHR_VERSION); + HAPCharacteristic chr(HAP_UUID_CHR_VERSION, BLE_GATT_CPF_FORMAT_UTF8S); chr.setTempMemory(); // ready-only, not included in Gatt list chr.setProperties(CHR_PROPS_READ); @@ -86,41 +84,10 @@ err_t BLEHomekit::begin() } /*------------- Pairing Service -------------*/ - VERIFY_STATUS( _pairing.begin() ); - { - // TODO read, write using auth - _pair_setup.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); - _pair_setup.setPermission(SECMODE_OPEN, SECMODE_OPEN); - _pair_setup.setMaxLen(100); - VERIFY_STATUS( _pair_setup.begin() ); - - _pair_verify.setProperties(CHR_PROPS_READ); - _pair_verify.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); - _pair_verify.setMaxLen(100); - VERIFY_STATUS( _pair_verify.begin() ); - - _pair_features.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); - _pair_features.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); - _pair_features.setFixedLen(1); - VERIFY_STATUS( _pair_features.begin() ); - _pair_features.write( (uint8_t) 0x01); // support HAP pairing - - _pair_pairing.setProperties(CHR_PROPS_READ); - _pair_pairing.setPermission(SECMODE_OPEN, SECMODE_OPEN); - _pair_pairing.setMaxLen(100); - VERIFY_STATUS( _pair_pairing.begin() ); - } + VERIFY_STATUS ( _pairing.begin() ); /*------------- LightBulb Service -------------*/ VERIFY_STATUS( _lightbulb.begin() ) ; - { - // ON char - _on.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_INDICATE ); - _on.setPermission(SECMODE_OPEN, SECMODE_OPEN); - _on.setFixedLen(1); - VERIFY_STATUS( _on.begin() ); - _on.write( (uint8_t) 0x00 ); - } return ERROR_NONE; } diff --git a/libraries/BLEHomekit/BLEHomekit.h b/libraries/BLEHomekit/BLEHomekit.h index 8b89f61d..bce758bb 100644 --- a/libraries/BLEHomekit/BLEHomekit.h +++ b/libraries/BLEHomekit/BLEHomekit.h @@ -41,6 +41,7 @@ #include "HAPService.h" #include "service/HAPAccessoryInfo.h" +#include "service/HAPPairing.h" enum { @@ -74,18 +75,14 @@ class BLEHomekit : public Advertisable BLEHomekit(); err_t begin(); - HAPAccessoryInfo AccessoryInfo; private: // Mandatory services HAPService _protocol_info; - - HAPService _pairing; - HAPCharacteristic _pair_setup, _pair_verify, _pair_features, _pair_pairing; + HAPPairing _pairing; HAPService _lightbulb; - HAPCharacteristic _on; }; #endif /* BLEHOMEKIT_H_ */ diff --git a/libraries/BLEHomekit/HAPCharacteristic.h b/libraries/BLEHomekit/HAPCharacteristic.h index a143aad2..22028da2 100644 --- a/libraries/BLEHomekit/HAPCharacteristic.h +++ b/libraries/BLEHomekit/HAPCharacteristic.h @@ -43,7 +43,7 @@ class HAPCharacteristic : public BLECharacteristic public: static BLEUuid _g_uuid_cid; - HAPCharacteristic(BLEUuid bleuuid, uint8_t format, uint16_t unit); + HAPCharacteristic(BLEUuid bleuuid, uint8_t format, uint16_t unit = UUID16_UNIT_UNITLESS); virtual err_t begin(void); private: diff --git a/libraries/BLEHomekit/service/HAPLightBulb.cpp b/libraries/BLEHomekit/service/HAPLightBulb.cpp new file mode 100644 index 00000000..3befd98a --- /dev/null +++ b/libraries/BLEHomekit/service/HAPLightBulb.cpp @@ -0,0 +1,59 @@ +/**************************************************************************/ +/*! + @file HAPLightBulb.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 "HAPUuid.h" +#include "HAPLightBulb.h" + +HAPLightBulb::HAPLightBulb(void) + : HAPService(HAP_UUID_SVC_LIGHT_BULB), _on(HAP_UUID_CHR_ON) +{ + +} + +err_t HAPLightBulb::begin(void) +{ + VERIFY_STATUS( HAPService::begin() ); // Invoke base class begin() + + // ON char + _on.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_INDICATE ); + _on.setPermission(SECMODE_OPEN, SECMODE_OPEN); + _on.setFixedLen(1); + VERIFY_STATUS( _on.begin() ); + _on.write( (uint8_t) 0x00 ); + + return ERROR_NONE; +} diff --git a/libraries/BLEHomekit/service/HAPLightBulb.h b/libraries/BLEHomekit/service/HAPLightBulb.h new file mode 100644 index 00000000..15675b8e --- /dev/null +++ b/libraries/BLEHomekit/service/HAPLightBulb.h @@ -0,0 +1,56 @@ +/**************************************************************************/ +/*! + @file HAPLightBulb.h + @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. +*/ +/**************************************************************************/ +#ifndef HAPLIGHTBULB_H_ +#define HAPLIGHTBULB_H_ + +#include "HAPCharacteristic.h" +#include "HAPService.h" + +class HAPLightBulb : public HAPService +{ + public: + HAPLightBulb(void); + + virtual err_t begin(void); + + private: + HAPCharacteristic _on; +}; + + + + +#endif /* HAPLIGHTBULB_H_ */ diff --git a/libraries/BLEHomekit/service/HAPPairing.cpp b/libraries/BLEHomekit/service/HAPPairing.cpp new file mode 100644 index 00000000..05911ff7 --- /dev/null +++ b/libraries/BLEHomekit/service/HAPPairing.cpp @@ -0,0 +1,78 @@ +/**************************************************************************/ +/*! + @file HAPPairing.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 "HAPUuid.h" +#include "HAPPairing.h" + +HAPPairing::HAPPairing(void) + : HAPService(HAP_UUID_SVC_PAIRING), + _setup (HAP_UUID_CHR_PAIR_SETUP , BLE_GATT_CPF_FORMAT_STRUCT), + _verify (HAP_UUID_CHR_PAIR_VERIFY , BLE_GATT_CPF_FORMAT_STRUCT), + _features (HAP_UUID_SVC_PAIR_FEATURE , BLE_GATT_CPF_FORMAT_STRUCT), + _pairing (HAP_UUID_SVC_PAIR_PAIRING , BLE_GATT_CPF_FORMAT_STRUCT) +{ + +} + +err_t HAPPairing::begin(void) +{ + VERIFY_STATUS( HAPService::begin() ); // Invoke base class begin() + + // TODO read, write using auth + _setup.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + _setup.setPermission(SECMODE_OPEN, SECMODE_OPEN); + _setup.setMaxLen(100); + VERIFY_STATUS( _setup.begin() ); + + _verify.setProperties(CHR_PROPS_READ); + _verify.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + _verify.setMaxLen(100); + VERIFY_STATUS( _verify.begin() ); + + _features.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE); + _features.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); + _features.setFixedLen(1); + VERIFY_STATUS( _features.begin() ); + _features.write( (uint8_t) 0x01); // support HAP pairing + + _pairing.setProperties(CHR_PROPS_READ); + _pairing.setPermission(SECMODE_OPEN, SECMODE_OPEN); + _pairing.setMaxLen(100); + VERIFY_STATUS( _pairing.begin() ); + + return ERROR_NONE; +} diff --git a/libraries/BLEHomekit/service/HAPPairing.h b/libraries/BLEHomekit/service/HAPPairing.h new file mode 100644 index 00000000..2be84141 --- /dev/null +++ b/libraries/BLEHomekit/service/HAPPairing.h @@ -0,0 +1,58 @@ +/**************************************************************************/ +/*! + @file HAPPairing.h + @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. +*/ +/**************************************************************************/ +#ifndef HAPPAIRING_H_ +#define HAPPAIRING_H_ + +#include "HAPCharacteristic.h" +#include "HAPService.h" + +class HAPPairing : public HAPService +{ + public: + HAPPairing(void); + + virtual err_t begin(void); + + private: + HAPCharacteristic _setup; + HAPCharacteristic _verify; + HAPCharacteristic _features; + HAPCharacteristic _pairing; +}; + + + +#endif /* HAPPAIRING_H_ */