Skip to content

Commit

Permalink
Factor out connObj bridge casts into helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ksperling-apple committed Mar 4, 2025
1 parent cb43215 commit 0b15350
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/platform/Darwin/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ static_library("Darwin") {
"BleConnectionDelegateImpl.mm",
"BlePlatformDelegateImpl.h",
"BlePlatformDelegateImpl.mm",
"BleUtils.h",
"MTRUUIDHelper.h",
"MTRUUIDHelperImpl.mm",
]
Expand Down
3 changes: 0 additions & 3 deletions src/platform/Darwin/BleApplicationDelegateImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
#include <ble/Ble.h>
#include <platform/Darwin/BleApplicationDelegateImpl.h>

using namespace ::chip;
using namespace ::chip::Ble;

namespace chip {
namespace DeviceLayer {
namespace Internal {
Expand Down
30 changes: 16 additions & 14 deletions src/platform/Darwin/BleConnectionDelegateImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <platform/CHIPDeviceLayer.h>
#include <platform/Darwin/BleConnectionDelegateImpl.h>
#include <platform/Darwin/BleScannerDelegate.h>
#include <platform/Darwin/BleUtils.h>
#include <platform/LockTracker.h>
#include <setup_payload/SetupPayload.h>
#include <tracing/metric_event.h>
Expand All @@ -39,6 +40,7 @@

using namespace chip::Ble;
using namespace chip::DeviceLayer;
using namespace chip::DeviceLayer::Internal;
using namespace chip::Tracing::DarwinPlatform;

constexpr uint64_t kScanningWithDiscriminatorTimeoutInSeconds = 60;
Expand Down Expand Up @@ -125,7 +127,7 @@ - (void)removePeripheralsFromCache;
assertChipStackLockedByCurrentThread();

ChipLogProgress(Ble, "ConnectionDelegate NewConnection with conn obj: %p", connObj);
CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj;
CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj);

// The BLE_CONNECTION_OBJECT represent a CBPeripheral object. In order for it to be valid the central
// manager needs to still be running.
Expand Down Expand Up @@ -296,7 +298,7 @@ - (void)dispatchConnectionError:(CHIP_ERROR)error
- (void)dispatchConnectionComplete:(CBPeripheral *)peripheral
{
if (self.onConnectionComplete != nil) {
self.onConnectionComplete(self.appState, (__bridge void *) peripheral);
self.onConnectionComplete(self.appState, BleConnObjectFromCBPeripheral(peripheral));
}
}

Expand Down Expand Up @@ -465,12 +467,12 @@ - (void)peripheral:(CBPeripheral *)peripheral
chip::Ble::ChipBleUUID svcId;
chip::Ble::ChipBleUUID charId;
[BleConnection fillServiceWithCharacteristicUuids:characteristic svcId:&svcId charId:&charId];
_mBleLayer->HandleWriteConfirmation((__bridge void *) peripheral, &svcId, &charId);
_mBleLayer->HandleWriteConfirmation(BleConnObjectFromCBPeripheral(peripheral), &svcId, &charId);
} else {
ChipLogError(
Ble, "BLE:Error writing Characteristics in Chip service on the device: [%s]", [error.localizedDescription UTF8String]);
MATTER_LOG_METRIC(kMetricBLEWriteChrValueFailed, BLE_ERROR_GATT_WRITE_FAILED);
_mBleLayer->HandleConnectionError((__bridge void *) peripheral, BLE_ERROR_GATT_WRITE_FAILED);
_mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), BLE_ERROR_GATT_WRITE_FAILED);
}
}

Expand All @@ -486,9 +488,9 @@ - (void)peripheral:(CBPeripheral *)peripheral
[BleConnection fillServiceWithCharacteristicUuids:characteristic svcId:&svcId charId:&charId];

if (isNotifying) {
_mBleLayer->HandleSubscribeComplete((__bridge void *) peripheral, &svcId, &charId);
_mBleLayer->HandleSubscribeComplete(BleConnObjectFromCBPeripheral(peripheral), &svcId, &charId);
} else {
_mBleLayer->HandleUnsubscribeComplete((__bridge void *) peripheral, &svcId, &charId);
_mBleLayer->HandleUnsubscribeComplete(BleConnObjectFromCBPeripheral(peripheral), &svcId, &charId);
}
} else {
ChipLogError(Ble, "BLE:Error subscribing/unsubcribing some characteristic on the device: [%s]",
Expand All @@ -497,11 +499,11 @@ - (void)peripheral:(CBPeripheral *)peripheral
if (isNotifying) {
MATTER_LOG_METRIC(kMetricBLEUpdateNotificationStateForChrFailed, BLE_ERROR_GATT_WRITE_FAILED);
// we're still notifying, so we must failed the unsubscription
_mBleLayer->HandleConnectionError((__bridge void *) peripheral, BLE_ERROR_GATT_UNSUBSCRIBE_FAILED);
_mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), BLE_ERROR_GATT_UNSUBSCRIBE_FAILED);
} else {
// we're not notifying, so we must failed the subscription
MATTER_LOG_METRIC(kMetricBLEUpdateNotificationStateForChrFailed, BLE_ERROR_GATT_SUBSCRIBE_FAILED);
_mBleLayer->HandleConnectionError((__bridge void *) peripheral, BLE_ERROR_GATT_SUBSCRIBE_FAILED);
_mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), BLE_ERROR_GATT_SUBSCRIBE_FAILED);
}
}
}
Expand All @@ -522,8 +524,8 @@ - (void)peripheral:(CBPeripheral *)peripheral
if (msgBuf.IsNull()) {
ChipLogError(Ble, "Failed at allocating buffer for incoming BLE data");
MATTER_LOG_METRIC(kMetricBLEUpdateValueForChrFailed, CHIP_ERROR_NO_MEMORY);
_mBleLayer->HandleConnectionError((__bridge void *) peripheral, CHIP_ERROR_NO_MEMORY);
} else if (!_mBleLayer->HandleIndicationReceived((__bridge void *) peripheral, &svcId, &charId, std::move(msgBuf))) {
_mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), CHIP_ERROR_NO_MEMORY);
} else if (!_mBleLayer->HandleIndicationReceived(BleConnObjectFromCBPeripheral(peripheral), &svcId, &charId, std::move(msgBuf))) {
// since this error comes from device manager core
// we assume it would do the right thing, like closing the connection
ChipLogError(Ble, "Failed at handling incoming BLE data");
Expand All @@ -533,7 +535,7 @@ - (void)peripheral:(CBPeripheral *)peripheral
ChipLogError(
Ble, "BLE:Error receiving indication of Characteristics on the device: [%s]", [error.localizedDescription UTF8String]);
MATTER_LOG_METRIC(kMetricBLEUpdateValueForChrFailed, BLE_ERROR_GATT_INDICATE_FAILED);
_mBleLayer->HandleConnectionError((__bridge void *) peripheral, BLE_ERROR_GATT_INDICATE_FAILED);
_mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), BLE_ERROR_GATT_INDICATE_FAILED);
}
}

Expand Down Expand Up @@ -647,7 +649,7 @@ - (void)updateWithDelegate:(chip::DeviceLayer::BleScannerDelegate *)delegate pre
NSData * serviceData = _cachedPeripherals[cachedPeripheral][@"data"];
ChipBLEDeviceIdentificationInfo info;
memcpy(&info, [serviceData bytes], sizeof(info));
delegate->OnBleScanAdd((__bridge void *) cachedPeripheral, info);
delegate->OnBleScanAdd(BleConnObjectFromCBPeripheral(cachedPeripheral), info);
}
_scannerDelegate = delegate;
}
Expand Down Expand Up @@ -721,7 +723,7 @@ - (void)addPeripheralToCache:(CBPeripheral *)peripheral data:(NSData *)data
ChipBLEDeviceIdentificationInfo info;
auto bytes = (const uint8_t *) [data bytes];
memcpy(&info, bytes, sizeof(info));
delegate->OnBleScanAdd((__bridge void *) peripheral, info);
delegate->OnBleScanAdd(BleConnObjectFromCBPeripheral(peripheral), info);
}

timeoutTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _workQueue);
Expand Down Expand Up @@ -768,7 +770,7 @@ - (void)removePeripheralFromCache:(CBPeripheral *)peripheral

auto delegate = _scannerDelegate;
if (delegate) {
delegate->OnBleScanRemove((__bridge void *) peripheral);
delegate->OnBleScanRemove(BleConnObjectFromCBPeripheral(peripheral));
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/platform/Darwin/BlePlatformDelegateImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
#include <ble/Ble.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/Darwin/BlePlatformDelegateImpl.h>
#include <platform/Darwin/BleUtils.h>

#import "MTRUUIDHelper.h"

using namespace ::chip;
using namespace ::chip::Ble;
using ::chip::System::PacketBufferHandle;
using namespace chip::Ble;
using chip::System::PacketBufferHandle;

namespace chip {
namespace DeviceLayer {
Expand All @@ -49,7 +49,7 @@

CBUUID * serviceId = [MTRUUIDHelper GetShortestServiceUUID:svcId];
CBUUID * characteristicId = [CBUUID UUIDWithData:[NSData dataWithBytes:charId->bytes length:sizeof(charId->bytes)]];
CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj;
CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj);

for (CBService * service in peripheral.services) {
if ([service.UUID.data isEqualToData:serviceId.data]) {
Expand Down Expand Up @@ -77,7 +77,7 @@
CBUUID * serviceId = [MTRUUIDHelper GetShortestServiceUUID:svcId];
CBUUID * characteristicId = characteristicId = [CBUUID UUIDWithData:[NSData dataWithBytes:charId->bytes
length:sizeof(charId->bytes)]];
CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj;
CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj);

for (CBService * service in peripheral.services) {
if ([service.UUID.data isEqualToData:serviceId.data]) {
Expand All @@ -96,7 +96,7 @@

CHIP_ERROR BlePlatformDelegateImpl::CloseConnection(BLE_CONNECTION_OBJECT connObj)
{
CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj;
CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj);

// CoreBluetooth API requires a CBCentralManager to close a connection which is a property of the peripheral.
CBCentralManager * manager = (CBCentralManager *) [peripheral valueForKey:@"manager"];
Expand All @@ -108,7 +108,7 @@

uint16_t BlePlatformDelegateImpl::GetMTU(BLE_CONNECTION_OBJECT connObj) const
{
CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj;
CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj);

// The negotiated mtu length is a property of the peripheral.
uint16_t mtuLength = [[peripheral valueForKey:@"mtuLength"] unsignedShortValue];
Expand All @@ -134,7 +134,7 @@
CBUUID * serviceId = [MTRUUIDHelper GetShortestServiceUUID:svcId];
CBUUID * characteristicId = [CBUUID UUIDWithData:[NSData dataWithBytes:charId->bytes length:sizeof(charId->bytes)]];
NSData * data = [NSData dataWithBytes:pBuf->Start() length:pBuf->DataLength()];
CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj;
CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj);

for (CBService * service in peripheral.services) {
if ([service.UUID.data isEqualToData:serviceId.data]) {
Expand Down
37 changes: 37 additions & 0 deletions src/platform/Darwin/BleUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) 2025 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <ble/Ble.h>

@class CBPeripheral;

namespace chip {
namespace DeviceLayer {
namespace Internal {

inline CBPeripheral * CBPeripheralFromBleConnObject(BLE_CONNECTION_OBJECT connObj)
{
return (__bridge CBPeripheral *) connObj;
}

inline BLE_CONNECTION_OBJECT BleConnObjectFromCBPeripheral(CBPeripheral * peripheral)
{
return (__bridge void *) peripheral;
}

}
}
}

0 comments on commit 0b15350

Please sign in to comment.