Skip to content

V4.2 Porting #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 17 additions & 23 deletions libraries/CurieBLE/src/BLEDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,25 @@ BLEDevice::BLEDevice()
_conn_param.interval_max = BT_GAP_INIT_CONN_INT_MAX;
_conn_param.interval_min = BT_GAP_INIT_CONN_INT_MIN;
_conn_param.latency = 0;
_conn_param.timeout = 400;
_conn_param.timeout = 500;
}

/*
BLEDevice::BLEDevice(String bleaddress)
{
BLEUtils::macAddressString2BT(bleaddress.c_str(), _bt_addr);
}

BLEDevice::BLEDevice(const char* bleaddress)
{
BLEUtils::macAddressString2BT(bleaddress, _bt_addr);
}

*/

BLEDevice::BLEDevice(const bt_addr_le_t* bleaddress):
BLEDevice()
{
memcpy(&_bt_addr, bleaddress, sizeof(_bt_addr));
bt_addr_le_copy(&_bt_addr, bleaddress);
BLEDeviceManager::instance()->getConnectionInterval(this, &_conn_param);
}

BLEDevice::BLEDevice(const BLEDevice* bledevice)
{
memcpy(&_bt_addr, bledevice->bt_le_address(), sizeof(_bt_addr));
bt_addr_le_copy(&_bt_addr, bledevice->bt_le_address());
memcpy(&_conn_param, &bledevice->_conn_param, sizeof (_conn_param));
}

BLEDevice::BLEDevice(const BLEDevice& bledevice)
{
memcpy(&_bt_addr, bledevice.bt_le_address(), sizeof(_bt_addr));
bt_addr_le_copy(&_bt_addr, bledevice.bt_le_address());
memcpy(&_conn_param, &bledevice._conn_param, sizeof (_conn_param));
}

Expand Down Expand Up @@ -117,7 +104,7 @@ String BLEDevice::address() const

void BLEDevice::setAddress(const bt_addr_le_t& addr)
{
memcpy(&_bt_addr, &addr, sizeof(_bt_addr));
bt_addr_le_copy(&_bt_addr, &addr);
}

void BLEDevice::setAdvertisedServiceUuid(const char* advertisedServiceUuid)
Expand Down Expand Up @@ -229,7 +216,11 @@ void BLEDevice::setDeviceName(const char* deviceName)

void BLEDevice::setAppearance(unsigned short appearance)
{
BLEDeviceManager::instance()->setAppearance(appearance);
if (BLEUtils::isLocalBLE(*this))
{
// Only local device can set the appearance
BLEDeviceManager::instance()->setAppearance(appearance);
}
}

int BLEDevice::addService(BLEService& attribute)
Expand Down Expand Up @@ -280,12 +271,14 @@ BLEDevice& BLEDevice::operator=(const BLEDevice& device)

bool BLEDevice::operator==(const BLEDevice& device) const
{
return (memcmp(this->_bt_addr.val, device._bt_addr.val, 6) == 0);
return (bt_addr_le_cmp(&this->_bt_addr, &device._bt_addr) == 0);
//return (memcmp(this->_bt_addr.a.val, device._bt_addr.a.val, 6) == 0);
}

bool BLEDevice::operator!=(const BLEDevice& device) const
{
return (memcmp(this->_bt_addr.val, device._bt_addr.val, 6) != 0);
return (bt_addr_le_cmp(&this->_bt_addr, &device._bt_addr) != 0);
//return (memcmp(this->_bt_addr.a.val, device._bt_addr.a.val, 6) != 0);
}


Expand Down Expand Up @@ -383,9 +376,10 @@ bool BLEDevice::connect()
return BLEDeviceManager::instance()->connect(*this);
}

bool BLEDevice::discoverAttributes()
bool BLEDevice::discoverAttributes(bool discoverGapGatt)
{
return BLEProfileManager::instance()->discoverAttributes(this);
return BLEProfileManager::instance()->discoverAllAttributes(this,
discoverGapGatt);
}

bool BLEDevice::discoverAttributesByService(const char* svc_uuid)
Expand Down
4 changes: 2 additions & 2 deletions libraries/CurieBLE/src/BLEDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ class BLEDevice
int rssi() const; // returns the RSSI of the peripheral at discovery

bool connect(); // connect to the peripheral
bool discoverAttributes(); // discover the peripheral's attributes
bool discoverAttributes(bool discoverGapGatt = false); // discover the peripheral's attributes
bool discoverAttributesByService(const char* svc_uuid);

String deviceName(); // read the device name attribute of the peripheral, and return String value
Expand Down Expand Up @@ -665,7 +665,7 @@ class BLEDevice
const void *data,
uint16_t length);
friend uint8_t profile_descriptor_read_rsp_process(bt_conn_t *conn,
int err,
uint8_t err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length);
Expand Down
2 changes: 1 addition & 1 deletion libraries/CurieBLE/src/BLEService.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class BLEService
friend class BLEServiceImp;
friend class BLEProfileManager;
friend uint8_t profile_service_read_rsp_process(bt_conn_t *conn,
int err,
uint8_t err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length);
Expand Down
55 changes: 32 additions & 23 deletions libraries/CurieBLE/src/internal/BLECallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
#include "BLEDeviceManager.h"
#include "BLEProfileManager.h"

#include "BLECallbacks.h"

#include <atomic.h>
#include "../src/services/ble/conn_internal.h"

// GATT Server Only
ssize_t profile_read_process(bt_conn_t *conn,
const bt_gatt_attr_t *attr,
Expand Down Expand Up @@ -56,26 +61,27 @@ ssize_t profile_read_process(bt_conn_t *conn,
ssize_t profile_write_process(bt_conn_t *conn,
const bt_gatt_attr_t *attr,
const void *buf, uint16_t len,
uint16_t offset)
uint16_t offset, uint8_t flags)
{
pr_info(LOG_MODULE_BLE, "%s1", __FUNCTION__);
BLEAttribute *bleattr = (BLEAttribute *)attr->user_data;
BLECharacteristicImp* blecharacteritic;
BLEAttributeType type = bleattr->type();
if ((BLETypeCharacteristic != type) || 0 != offset)
if (BLETypeCharacteristic != type)
{
return 0;
}

blecharacteritic = (BLECharacteristicImp*)bleattr;
blecharacteritic->setValue((const uint8_t *) buf, len);
blecharacteritic->setValue((const uint8_t *) buf, len, offset);
return len;
}

#ifdef TD_V3
ssize_t profile_longwrite_process(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
const void *buf, uint16_t len,
uint16_t offset)
uint16_t offset, uint8_t flags)
{
BLEAttribute *bleattr = (BLEAttribute *)attr->user_data;
BLEAttributeType type = bleattr->type();
Expand Down Expand Up @@ -116,21 +122,18 @@ int profile_longflush_process(struct bt_conn *conn,

return -EINVAL;
}

#endif

// GATT client only
uint8_t profile_notify_process (bt_conn_t *conn,
bt_gatt_subscribe_params_t *params,
const void *data, uint16_t length)
{
//BLEPeripheralHelper* peripheral = BLECentralRole::instance()->peripheral(conn);// Find peripheral by bt_conn
//BLEAttribute* notifyatt = peripheral->attribute(params); // Find attribute by params
BLECharacteristicImp* chrc = NULL;
BLEDevice bleDevice(bt_conn_get_dst(conn));
chrc = BLEProfileManager::instance()->characteristic(bleDevice, params->value_handle);

//assert(notifyatt->type() == BLETypeCharacteristic);
pr_debug(LOG_MODULE_APP, "%s1", __FUNCTION__);
pr_debug(LOG_MODULE_APP, "%s-%d", __FUNCTION__, __LINE__);
if (NULL != chrc)
{
chrc->setValue((const unsigned char *)data, length);
Expand All @@ -146,24 +149,20 @@ uint8_t profile_discover_process(bt_conn_t *conn,
uint8_t ret = BT_GATT_ITER_STOP;
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
ret = BLEProfileManager::instance()->discoverResponseProc(conn, attr, params);
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
return ret;
}

// GATT Client only
uint8_t profile_read_rsp_process(bt_conn_t *conn,
int err,
uint8_t err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length)
{
if (NULL == data && 0 != length)
{
return BT_GATT_ITER_STOP;
}
BLECharacteristicImp *chrc = NULL;
BLEDevice bleDevice(bt_conn_get_dst(conn));

pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
// Get characteristic by handle params->single.handle
chrc = BLEProfileManager::instance()->characteristic(bleDevice, params->single.handle);

Expand All @@ -174,7 +173,7 @@ uint8_t profile_read_rsp_process(bt_conn_t *conn,
}

uint8_t profile_descriptor_read_rsp_process(bt_conn_t *conn,
int err,
uint8_t err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length)
Expand All @@ -186,20 +185,19 @@ uint8_t profile_descriptor_read_rsp_process(bt_conn_t *conn,
BLEDescriptorImp *descriptor = NULL;
BLEDevice bleDevice(bt_conn_get_dst(conn));

pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
// Get characteristic by handle params->single.handle
descriptor = BLEProfileManager::instance()->descriptor(bleDevice, params->single.handle);

//pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
if (descriptor)
{
descriptor->writeValue((const unsigned char *)data, length, params->single.offset);
}
//pr_debug(LOG_MODULE_BLE, "%s-%d: desc len-%d", __FUNCTION__, __LINE__, descriptor->valueLength());
return BT_GATT_ITER_STOP;
}

uint8_t profile_service_read_rsp_process(bt_conn_t *conn,
int err,
uint8_t err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length)
Expand All @@ -210,7 +208,7 @@ uint8_t profile_service_read_rsp_process(bt_conn_t *conn,
}

uint8_t profile_characteristic_read_rsp_process(bt_conn_t *conn,
int err,
uint8_t err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length)
Expand All @@ -237,7 +235,6 @@ void bleConnectEventHandler(bt_conn_t *conn,
p->handleConnectEvent(conn, err);
}


void bleDisconnectEventHandler(bt_conn_t *conn,
uint8_t reason,
void *param)
Expand Down Expand Up @@ -268,13 +265,14 @@ void ble_central_device_found(const bt_addr_le_t *addr,
uint8_t len)
{
//char dev[BT_ADDR_LE_STR_LEN];

//bt_addr_le_to_str(addr, dev, sizeof(dev));
//pr_debug(LOG_MODULE_BLE, "[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i\n",
// dev, type, len, rssi);
//pr_debug(LOG_MODULE_BLE, " AD evt type %u, AD data len %u, RSSI %i\n",
// type, len, rssi);

BLEDeviceManager::instance()->handleDeviceFound(addr, rssi, type,
ad, len);
ad, len);
}

void ble_on_write_no_rsp_complete(struct bt_conn *conn, uint8_t err,
Expand All @@ -283,3 +281,14 @@ void ble_on_write_no_rsp_complete(struct bt_conn *conn, uint8_t err,
BLECharacteristicImp::writeResponseReceived(conn, err, data);
}

void prfile_cccd_cfg_changed(void *user_data, uint16_t value)
{
if (NULL == user_data)
return;
pr_debug(LOG_MODULE_BLE, "%s-%d: ccc userdata %p", __FUNCTION__, __LINE__, user_data);

BLECharacteristicImp *blecharacteritic = (BLECharacteristicImp *)user_data;
blecharacteritic->cccdValueChanged();
}


17 changes: 11 additions & 6 deletions libraries/CurieBLE/src/internal/BLECallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
uint8_t profile_notify_process (bt_conn_t *conn,
bt_gatt_subscribe_params_t *params,
const void *data, uint16_t length);
uint8_t profile_read_rsp_process(bt_conn_t *conn, int err,
uint8_t profile_read_rsp_process(bt_conn_t *conn,
uint8_t err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length);

uint8_t profile_descriptor_read_rsp_process(bt_conn_t *conn,
int err,
uint8_t err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length);
Expand All @@ -41,11 +42,13 @@ int profile_longflush_process(struct bt_conn *conn,
ssize_t profile_longwrite_process(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
const void *buf, uint16_t len,
uint16_t offset);
uint16_t offset,
uint8_t flags);
ssize_t profile_write_process(bt_conn_t *conn,
const bt_gatt_attr_t *attr,
const void *buf, uint16_t len,
uint16_t offset);
uint16_t offset,
uint8_t flags);
ssize_t profile_read_process(bt_conn_t *conn,
const bt_gatt_attr_t *attr,
void *buf, uint16_t len,
Expand Down Expand Up @@ -76,18 +79,20 @@ void ble_central_device_found(const bt_addr_le_t *addr,
uint8_t len);

uint8_t profile_service_read_rsp_process(bt_conn_t *conn,
int err,
uint8_t err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length);

void ble_on_write_no_rsp_complete(struct bt_conn *conn, uint8_t err,
const void *data);
uint8_t profile_characteristic_read_rsp_process(bt_conn_t *conn,
int err,
uint8_t err,
bt_gatt_read_params_t *params,
const void *data,
uint16_t length);

void prfile_cccd_cfg_changed(void *user_data, uint16_t value);

#endif

Loading