Skip to content

Commit

Permalink
[Tizen] Allow to pass CHIP error to device event (#33744)
Browse files Browse the repository at this point in the history
* Create GATT service with short Bluetooth SIG UUID

* Do not initialize characteristics with dummy values

* Do not keep non-Matter UUIDs in BleUUID.h

* Pass CHIP_ERROR to device event

* Simplify ChipDeviceEvent initialization

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
arkq and restyled-commits authored Jun 5, 2024
1 parent 4a8dc73 commit e445e8a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 93 deletions.
1 change: 0 additions & 1 deletion src/ble/BleUUID.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ constexpr std::pair<bool, ChipBleUUID> StringToUUID(const char (&str)[N])
// BlueZ API https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/org.bluez.Device.rst
// describes ServiceData as "Keys are the UUIDs in string format" however no description
// on actual case required
inline constexpr char CHIP_BLE_DESC_SHORT_UUID_STR[] = "2902";
inline constexpr char CHIP_BLE_SERVICE_SHORT_UUID_STR[] = "fff6";
inline constexpr char CHIP_BLE_SERVICE_LONG_UUID_STR[] = "0000fff6-0000-1000-8000-00805f9b34fb";
inline constexpr char CHIP_BLE_CHAR_1_UUID_STR[] = "18ee2ef5-263d-4559-959f-4f9c429f9d11";
Expand Down
1 change: 1 addition & 0 deletions src/platform/Linux/CHIPDevicePlatformEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#pragma once

#include <lib/core/CHIPError.h>
#include <platform/CHIPDeviceEvent.h>
#include <system/SystemPacketBuffer.h>

Expand Down
139 changes: 62 additions & 77 deletions src/platform/Tizen/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ namespace Internal {

namespace {

// Bluetooth SIG defined UUID for Client Characteristic Configuration Descriptor
constexpr const char * kClientCharacteristicConfigurationUUID = "2902";

constexpr System::Clock::Timeout kNewConnectionScanTimeout = System::Clock::Seconds16(20);
constexpr System::Clock::Timeout kConnectTimeout = System::Clock::Seconds16(20);
constexpr System::Clock::Timeout kFastAdvertiseTimeout =
Expand Down Expand Up @@ -208,7 +211,10 @@ void BLEManagerImpl::ReadValueRequestedCb(const char * remoteAddress, int reques

ChipLogByteSpan(DeviceLayer, ByteSpan(Uint8::from_const_char(value.get()), len));

ret = bt_gatt_server_send_response(requestId, BT_GATT_REQUEST_TYPE_READ, offset, 0x00, value.get(), len);
char dummyValue[] = "";
// Tizen API does not like NULLs even for zero-length values.
char * valuePtr = value ? value.get() : dummyValue;
ret = bt_gatt_server_send_response(requestId, BT_GATT_REQUEST_TYPE_READ, offset, BT_ATT_ERROR_NONE, valuePtr, len);
VerifyOrReturn(ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "bt_gatt_server_send_response() failed: %s", get_error_message(ret)));
}
Expand All @@ -233,7 +239,7 @@ void BLEManagerImpl::WriteValueRequestedCb(const char * remoteAddress, int reque
ret = bt_gatt_set_value(gattHandle, value, len);
VerifyOrReturn(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_set_value() failed: %s", get_error_message(ret)));

ret = bt_gatt_server_send_response(requestId, BT_GATT_REQUEST_TYPE_WRITE, offset, 0x00, nullptr, 0);
ret = bt_gatt_server_send_response(requestId, BT_GATT_REQUEST_TYPE_WRITE, offset, BT_ATT_ERROR_NONE, nullptr, 0);
VerifyOrReturn(ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "bt_gatt_server_send_response() failed: %s", get_error_message(ret)));

Expand Down Expand Up @@ -265,7 +271,7 @@ void BLEManagerImpl::NotificationStateChangedCb(bool notify, bt_gatt_server_h se

ChipLogProgress(DeviceLayer, "Notification State Changed %d on %s: %s", notify, __ConvertAttTypeToStr(type),
StringOrNullMarker(uuid.get()));
NotifyBLESubscribed(notify ? true : false, conn);
NotifyBLESubscribed(conn, notify ? true : false);
}

void BLEManagerImpl::WriteCompletedCb(int result, bt_gatt_h gattHandle, void * userData)
Expand Down Expand Up @@ -314,7 +320,7 @@ void BLEManagerImpl::AdvertisingStateChangedCb(int result, bt_advertiser_h adver
if (advState == BT_ADAPTER_LE_ADVERTISING_STARTED)
{
mFlags.Set(Flags::kAdvertising);
NotifyBLEPeripheralAdvStartComplete(true, nullptr);
NotifyBLEPeripheralAdvStartComplete(CHIP_NO_ERROR);
DeviceLayer::SystemLayer().ScheduleLambda([this] {
// Start a timer to make sure that the fast advertising is stopped after specified timeout.
DeviceLayer::SystemLayer().StartTimer(kFastAdvertiseTimeout, HandleAdvertisingTimeout, this);
Expand All @@ -323,7 +329,7 @@ void BLEManagerImpl::AdvertisingStateChangedCb(int result, bt_advertiser_h adver
else
{
mFlags.Clear(Flags::kAdvertising);
NotifyBLEPeripheralAdvStopComplete(true, nullptr);
NotifyBLEPeripheralAdvStopComplete(CHIP_NO_ERROR);
DeviceLayer::SystemLayer().ScheduleLambda(
[this] { DeviceLayer::SystemLayer().CancelTimer(HandleAdvertisingTimeout, this); });
}
Expand All @@ -338,89 +344,73 @@ void BLEManagerImpl::AdvertisingStateChangedCb(int result, bt_advertiser_h adver
}

// ====== Private Functions.
void BLEManagerImpl::NotifyBLEPeripheralGATTServerRegisterComplete(bool aIsSuccess, void * apAppstate)

void BLEManagerImpl::NotifyBLEPeripheralGATTServerRegisterComplete(CHIP_ERROR error)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kPlatformTizenBLEPeripheralGATTServerRegisterComplete;
event.Platform.BLEPeripheralGATTServerRegisterComplete.mIsSuccess = aIsSuccess;
event.Platform.BLEPeripheralGATTServerRegisterComplete.mpAppstate = apAppstate;
ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformTizenBLEPeripheralGATTServerRegisterComplete,
.Platform = { .BLEPeripheralGATTServerRegisterComplete = { .mError = error } } };
PlatformMgr().PostEventOrDie(&event);
}

void BLEManagerImpl::NotifyBLEPeripheralAdvConfiguredComplete(bool aIsSuccess, void * apAppstate)
void BLEManagerImpl::NotifyBLEPeripheralAdvConfiguredComplete(CHIP_ERROR error)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kPlatformTizenBLEPeripheralAdvConfiguredComplete;
event.Platform.BLEPeripheralAdvConfiguredComplete.mIsSuccess = aIsSuccess;
event.Platform.BLEPeripheralAdvConfiguredComplete.mpAppstate = apAppstate;
ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformTizenBLEPeripheralAdvConfiguredComplete,
.Platform = { .BLEPeripheralAdvConfiguredComplete = { .mError = error } } };
PlatformMgr().PostEventOrDie(&event);
}

void BLEManagerImpl::NotifyBLEPeripheralAdvStartComplete(bool aIsSuccess, void * apAppstate)
void BLEManagerImpl::NotifyBLEPeripheralAdvStartComplete(CHIP_ERROR error)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kPlatformTizenBLEPeripheralAdvStartComplete;
event.Platform.BLEPeripheralAdvStartComplete.mIsSuccess = aIsSuccess;
event.Platform.BLEPeripheralAdvStartComplete.mpAppstate = apAppstate;
ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformTizenBLEPeripheralAdvStartComplete,
.Platform = { .BLEPeripheralAdvStartComplete = { .mError = error } } };
PlatformMgr().PostEventOrDie(&event);
}

void BLEManagerImpl::NotifyBLEPeripheralAdvStopComplete(bool aIsSuccess, void * apAppstate)
void BLEManagerImpl::NotifyBLEPeripheralAdvStopComplete(CHIP_ERROR error)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kPlatformTizenBLEPeripheralAdvStopComplete;
event.Platform.BLEPeripheralAdvStopComplete.mIsSuccess = aIsSuccess;
event.Platform.BLEPeripheralAdvStopComplete.mpAppstate = apAppstate;
ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformTizenBLEPeripheralAdvStopComplete,
.Platform = { .BLEPeripheralAdvStopComplete = { .mError = error } } };
PlatformMgr().PostEventOrDie(&event);
}

void BLEManagerImpl::NotifyBLEWriteReceived(System::PacketBufferHandle & buf, BLE_CONNECTION_OBJECT conId)
void BLEManagerImpl::NotifyBLEWriteReceived(BLE_CONNECTION_OBJECT conId, System::PacketBufferHandle & buf)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kCHIPoBLEWriteReceived;
event.CHIPoBLEWriteReceived.ConId = conId;
event.CHIPoBLEWriteReceived.Data = std::move(buf).UnsafeRelease();
ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoBLEWriteReceived,
.CHIPoBLEWriteReceived = { .ConId = conId, .Data = std::move(buf).UnsafeRelease() } };
PlatformMgr().PostEventOrDie(&event);
}

void BLEManagerImpl::NotifyBLENotificationReceived(System::PacketBufferHandle & buf, BLE_CONNECTION_OBJECT conId)
void BLEManagerImpl::NotifyBLENotificationReceived(BLE_CONNECTION_OBJECT conId, System::PacketBufferHandle & buf)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kPlatformTizenBLEIndicationReceived;
event.Platform.BLEIndicationReceived.mConnection = conId;
event.Platform.BLEIndicationReceived.mData = std::move(buf).UnsafeRelease();
ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformTizenBLEIndicationReceived,
.Platform = {
.BLEIndicationReceived = { .mConnection = conId, .mData = std::move(buf).UnsafeRelease() } } };
PlatformMgr().PostEventOrDie(&event);
}

void BLEManagerImpl::NotifyBLESubscribed(bool indicationsEnabled, BLE_CONNECTION_OBJECT conId)
void BLEManagerImpl::NotifyBLESubscribed(BLE_CONNECTION_OBJECT conId, bool indicationsEnabled)
{
ChipDeviceEvent event;
event.Type = (indicationsEnabled) ? DeviceEventType::kCHIPoBLESubscribe : DeviceEventType::kCHIPoBLEUnsubscribe;
event.CHIPoBLESubscribe.ConId = conId;
ChipDeviceEvent event{ .Type = indicationsEnabled ? DeviceEventType::kCHIPoBLESubscribe : DeviceEventType::kCHIPoBLEUnsubscribe,
.CHIPoBLESubscribe = { .ConId = conId } };
PlatformMgr().PostEventOrDie(&event);
}

void BLEManagerImpl::NotifyBLEIndicationConfirmation(BLE_CONNECTION_OBJECT conId)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kCHIPoBLEIndicateConfirm;
event.CHIPoBLEIndicateConfirm.ConId = conId;
ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoBLEIndicateConfirm, .CHIPoBLEIndicateConfirm = { .ConId = conId } };
PlatformMgr().PostEventOrDie(&event);
}

void BLEManagerImpl::NotifyBLEConnectionEstablished(BLE_CONNECTION_OBJECT conId, CHIP_ERROR error)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kCHIPoBLEConnectionEstablished;
ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoBLEConnectionEstablished };
PlatformMgr().PostEventOrDie(&event);
}

void BLEManagerImpl::NotifyBLEDisconnection(BLE_CONNECTION_OBJECT conId, CHIP_ERROR error)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kCHIPoBLEConnectionError;
event.CHIPoBLEConnectionError.ConId = conId;
event.CHIPoBLEConnectionError.Reason = error;
ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoBLEConnectionError,
.CHIPoBLEConnectionError = { .ConId = conId, .Reason = error } };
PlatformMgr().PostEventOrDie(&event);
}

Expand All @@ -429,9 +419,8 @@ void BLEManagerImpl::NotifyHandleConnectFailed(CHIP_ERROR error)
ChipLogProgress(DeviceLayer, "Connection failed: %" CHIP_ERROR_FORMAT, error.Format());
if (mIsCentral)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kPlatformTizenBLECentralConnectFailed;
event.Platform.BLECentralConnectFailed.mError = error;
ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformTizenBLECentralConnectFailed,
.Platform = { .BLECentralConnectFailed = { .mError = error } } };
PlatformMgr().PostEventOrDie(&event);
}
}
Expand All @@ -440,27 +429,23 @@ void BLEManagerImpl::NotifyHandleNewConnection(BLE_CONNECTION_OBJECT conId)
{
if (mIsCentral)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kPlatformTizenBLECentralConnected;
event.Platform.BLECentralConnected.mConnection = conId;
ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformTizenBLECentralConnected,
.Platform = { .BLECentralConnected = { .mConnection = conId } } };
PlatformMgr().PostEventOrDie(&event);
}
}

void BLEManagerImpl::NotifyHandleWriteComplete(BLE_CONNECTION_OBJECT conId)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kPlatformTizenBLEWriteComplete;
event.Platform.BLEWriteComplete.mConnection = conId;
ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformTizenBLEWriteComplete,
.Platform = { .BLEWriteComplete = { .mConnection = conId } } };
PlatformMgr().PostEventOrDie(&event);
}

void BLEManagerImpl::NotifySubscribeOpComplete(BLE_CONNECTION_OBJECT conId, bool isSubscribed)
{
ChipDeviceEvent event;
event.Type = DeviceEventType::kPlatformTizenBLESubscribeOpComplete;
event.Platform.BLESubscribeOpComplete.mConnection = conId;
event.Platform.BLESubscribeOpComplete.mIsSubscribed = isSubscribed;
ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformTizenBLESubscribeOpComplete,
.Platform = { .BLESubscribeOpComplete = { .mConnection = conId, .mIsSubscribed = isSubscribed } } };
PlatformMgr().PostEventOrDie(&event);
}

Expand Down Expand Up @@ -574,7 +559,7 @@ CHIP_ERROR BLEManagerImpl::RegisterGATTServer()
VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_server_create() failed: %s", get_error_message(ret)));

// Create Service (BTP Service)
ret = bt_gatt_service_create(Ble::CHIP_BLE_SERVICE_LONG_UUID_STR, BT_GATT_SERVICE_TYPE_PRIMARY, &service);
ret = bt_gatt_service_create(Ble::CHIP_BLE_SERVICE_SHORT_UUID_STR, BT_GATT_SERVICE_TYPE_PRIMARY, &service);
VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_service_create() failed: %s", get_error_message(ret)));

// Create 1st Characteristic (Client TX Buffer)
Expand All @@ -583,7 +568,7 @@ CHIP_ERROR BLEManagerImpl::RegisterGATTServer()
BT_GATT_PROPERTY_WRITE, // Write Request is not coming if we use WITHOUT_RESPONSE property. Let's use WRITE property and
// consider to use WITHOUT_RESPONSE property in the future according to the CHIP Spec 4.16.3.2. BTP
// GATT Service
"CHIPoBLE_C1", strlen("CHIPoBLE_C1"), &char1);
nullptr, 0, &char1);
VerifyOrExit(ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "bt_gatt_characteristic_create() failed: %s", get_error_message(ret)));

Expand All @@ -604,8 +589,7 @@ CHIP_ERROR BLEManagerImpl::RegisterGATTServer()

// Create 2nd Characteristic (Client RX Buffer)
ret = bt_gatt_characteristic_create(Ble::CHIP_BLE_CHAR_2_UUID_STR, BT_GATT_PERMISSION_READ,
BT_GATT_PROPERTY_READ | BT_GATT_PROPERTY_INDICATE, "CHIPoBLE_C2", strlen("CHIPoBLE_C2"),
&char2);
BT_GATT_PROPERTY_READ | BT_GATT_PROPERTY_INDICATE, nullptr, 0, &char2);
VerifyOrExit(ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "bt_gatt_characteristic_create() failed: %s", get_error_message(ret)));

Expand All @@ -618,6 +602,7 @@ CHIP_ERROR BLEManagerImpl::RegisterGATTServer()
this);
VerifyOrExit(ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "bt_gatt_server_set_read_value_requested_cb() failed: %s", get_error_message(ret)));

ret = bt_gatt_server_set_characteristic_notification_state_change_cb(
char2,
+[](bool notify, bt_gatt_server_h gattServer, bt_gatt_h charHandle, void * self) {
Expand All @@ -628,13 +613,14 @@ CHIP_ERROR BLEManagerImpl::RegisterGATTServer()
ChipLogError(DeviceLayer, "bt_gatt_server_set_characteristic_notification_state_change_cb() failed: %s",
get_error_message(ret)));

// Create CCC Descriptor
ret = bt_gatt_descriptor_create(Ble::CHIP_BLE_DESC_SHORT_UUID_STR, BT_GATT_PERMISSION_READ | BT_GATT_PERMISSION_WRITE,
ret = bt_gatt_descriptor_create(kClientCharacteristicConfigurationUUID, BT_GATT_PERMISSION_READ | BT_GATT_PERMISSION_WRITE,
desc_value, sizeof(desc_value), &desc);
VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_descriptor_create() failed: %s", get_error_message(ret)));

ret = bt_gatt_characteristic_add_descriptor(char2, desc);
VerifyOrExit(ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "bt_gatt_characteristic_add_descriptor() failed: %s", get_error_message(ret)));

ret = bt_gatt_service_add_characteristic(service, char2);
VerifyOrExit(ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "bt_gatt_service_add_characteristic() failed: %s", get_error_message(ret)));
Expand All @@ -648,17 +634,15 @@ CHIP_ERROR BLEManagerImpl::RegisterGATTServer()
ret = bt_gatt_server_start();
VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_gatt_server_start() failed: %s", get_error_message(ret)));

ChipLogDetail(DeviceLayer, "NotifyBLEPeripheralGATTServerRegisterComplete Success");
BLEManagerImpl::NotifyBLEPeripheralGATTServerRegisterComplete(true, nullptr);
BLEManagerImpl::NotifyBLEPeripheralGATTServerRegisterComplete(CHIP_NO_ERROR);

// Save the Local Peripheral char1 & char2 handles
mGattCharC1Handle = char1;
mGattCharC2Handle = char2;
return CHIP_NO_ERROR;

exit:
ChipLogDetail(DeviceLayer, "NotifyBLEPeripheralGATTServerRegisterComplete Failed");
BLEManagerImpl::NotifyBLEPeripheralGATTServerRegisterComplete(false, nullptr);
BLEManagerImpl::NotifyBLEPeripheralGATTServerRegisterComplete(TizenToChipError(ret));
return TizenToChipError(ret);
}

Expand Down Expand Up @@ -727,7 +711,7 @@ CHIP_ERROR BLEManagerImpl::StartBLEAdvertising()
VerifyOrExit(ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "bt_adapter_le_set_advertising_device_name() failed: %s", get_error_message(ret)));

BLEManagerImpl::NotifyBLEPeripheralAdvConfiguredComplete(true, nullptr);
BLEManagerImpl::NotifyBLEPeripheralAdvConfiguredComplete(CHIP_NO_ERROR);

ret = bt_adapter_le_start_advertising_new(
mAdvertiser,
Expand All @@ -742,8 +726,9 @@ CHIP_ERROR BLEManagerImpl::StartBLEAdvertising()
return CHIP_NO_ERROR;

exit:
BLEManagerImpl::NotifyBLEPeripheralAdvStartComplete(false, nullptr);
return ret != BT_ERROR_NONE ? TizenToChipError(ret) : err;
err = ret != BT_ERROR_NONE ? TizenToChipError(ret) : err;
BLEManagerImpl::NotifyBLEPeripheralAdvStartComplete(err);
return err;
}

CHIP_ERROR BLEManagerImpl::StopBLEAdvertising()
Expand All @@ -758,7 +743,7 @@ CHIP_ERROR BLEManagerImpl::StopBLEAdvertising()
return CHIP_NO_ERROR;

exit:
BLEManagerImpl::NotifyBLEPeripheralAdvStopComplete(false, nullptr);
BLEManagerImpl::NotifyBLEPeripheralAdvStopComplete(TizenToChipError(ret));
return TizenToChipError(ret);
}

Expand Down Expand Up @@ -896,7 +881,7 @@ void BLEManagerImpl::HandleC1CharWriteEvent(BLE_CONNECTION_OBJECT conId, const u
// Copy the data to a packet buffer.
buf = System::PacketBufferHandle::NewWithData(value, len);
VerifyOrExit(!buf.IsNull(), err = CHIP_ERROR_NO_MEMORY);
NotifyBLEWriteReceived(buf, conId);
NotifyBLEWriteReceived(conId, buf);
return;
exit:
if (err != CHIP_NO_ERROR)
Expand All @@ -915,7 +900,7 @@ void BLEManagerImpl::HandleRXCharChanged(BLE_CONNECTION_OBJECT conId, const uint
// Copy the data to a packet buffer.
buf = System::PacketBufferHandle::NewWithData(value, len);
VerifyOrExit(!buf.IsNull(), err = CHIP_ERROR_NO_MEMORY);
NotifyBLENotificationReceived(buf, conId);
NotifyBLENotificationReceived(conId, buf);
return;
exit:
if (err != CHIP_NO_ERROR)
Expand Down
Loading

0 comments on commit e445e8a

Please sign in to comment.