add BLEUuid.cpp

since it got more complex
This commit is contained in:
hathach committed 2017-03-22 14:55:27 +07:00
1 parent 6f0e361b8f
commit 98402b1a50
2 files changed
+129 -63

No files matched your search

+116
View File
@@ -0,0 +1,116 @@
/**************************************************************************/
/*!
@file BLEUuid.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 "BLEUuid.h"
void BLEUuid::set(uint16_t uuid16)
{
_uuid.type = BLE_UUID_TYPE_BLE;
_uuid.uuid = uuid16;
_uuid128 = NULL;
}
void BLEUuid::set(uint8_t const uuid128[16])
{
_uuid.type = BLE_UUID_TYPE_UNKNOWN; _uuid.uuid = 0;
_uuid128 = uuid128;
}
void BLEUuid::get(uint16_t& uuid16 )
{
}
uint8_t BLEUuid::size (void) const
{
// uuid 16
if (_uuid.type == BLE_UUID_TYPE_BLE ) return 2;
if (_uuid128 != NULL ) return 16;
// unknown
return 0;
}
void BLEUuid::begin(void)
{
/* Add base uuid and decode to get uuid16
* This should cover the already added base uuid128 previously
*/
if (_uuid.type == BLE_UUID_TYPE_UNKNOWN && _uuid128 != NULL )
{
(void) sd_ble_uuid_vs_add( (ble_uuid128_t const*) _uuid128, &_uuid.type );
(void) sd_ble_uuid_decode(16, _uuid128, &_uuid);
}
}
bool BLEUuid::operator== (const BLEUuid& uuid) const
{
return (this->_uuid.type == uuid._uuid.type) && (this->_uuid.uuid == uuid._uuid.uuid);
}
bool BLEUuid::operator!= (const BLEUuid& uuid) const
{
return !(*this == uuid);
}
bool BLEUuid::operator== (const ble_uuid_t uuid) const
{
return (this->_uuid.type == uuid.type) && (this->_uuid.uuid == uuid.uuid);
}
bool BLEUuid::operator!= (const ble_uuid_t uuid) const
{
return !(*this == uuid);
}
// Overload copy operator to allow initialization from other type
BLEUuid& BLEUuid::operator=(const uint16_t uuid)
{
set(uuid);
return *this;
}
BLEUuid& BLEUuid::operator=(uint8_t const uuid128[16])
{
set(uuid128);
return *this;
}
BLEUuid& BLEUuid::operator=(ble_uuid_t uuid)
{
_uuid = uuid;
return *this;
}
+13 -63
View File
@@ -49,75 +49,25 @@ class BLEUuid
BLEUuid(uint16_t uuid16 ) { set(uuid16); }
BLEUuid(uint8_t const uuid128[16] ) { set(uuid128); }
void set(uint16_t uuid16)
{
_uuid.type = BLE_UUID_TYPE_BLE;
_uuid.uuid = uuid16;
_uuid128 = NULL;
}
void set(uint16_t uuid16);
void set(uint8_t const uuid128[16]);
void set(uint8_t const uuid128[16])
{
_uuid.type = BLE_UUID_TYPE_UNKNOWN; _uuid.uuid = 0;
_uuid128 = uuid128;
}
void get(uint16_t& uuid16);
uint8_t size (void)
{
// uuid 16
if (_uuid.type == BLE_UUID_TYPE_BLE ) return 2;
if (_uuid128 != NULL ) return 16;
uint8_t size (void) const;
// unknown
return 0;
}
// Add UUID128 if needed, in case of UUID16, no actions is required
void begin(void);
// Add UUID128 if any, in case of UUID16, no actions is required
// Application should call it anyway to be safe and consistent
void begin(void)
{
/* Add base uuid and decode to get uuid16
* This should cover the already added base uuid128 previously
*/
if (_uuid.type == BLE_UUID_TYPE_UNKNOWN && _uuid128 != NULL )
{
(void) sd_ble_uuid_vs_add( (ble_uuid128_t const*) _uuid128, &_uuid.type );
(void) sd_ble_uuid_decode(16, _uuid128, &_uuid);
}
}
bool operator==(const BLEUuid& uuid) const
{
return (this->_uuid.type == uuid._uuid.type) && (this->_uuid.uuid == uuid._uuid.uuid);
}
bool operator!=(const BLEUuid& uuid) const
{
return !(*this == uuid);
}
bool operator==(const ble_uuid_t uuid) const
{
return (this->_uuid.type == uuid.type) && (this->_uuid.uuid == uuid.uuid);
}
bool operator!=(const ble_uuid_t uuid) const
{
return !(*this == uuid);
}
bool operator==(const BLEUuid& uuid) const;
bool operator!=(const BLEUuid& uuid) const;
bool operator==(const ble_uuid_t uuid) const;
bool operator!=(const ble_uuid_t uuid) const;
// Overload copy operator to allow initialization from other type
BLEUuid& operator=(const uint16_t uuid)
{
set(uuid);
return *this;
}
BLEUuid& operator=(uint8_t const uuid128[16])
{
set(uuid128);
return *this;
}
BLEUuid& operator=(const uint16_t uuid);
BLEUuid& operator=(uint8_t const uuid128[16]);
BLEUuid& operator=(ble_uuid_t uuid);
};
// Service UUID