Skip to content

Commit 0b15350

Browse files
Factor out connObj bridge casts into helper functions
1 parent cb43215 commit 0b15350

5 files changed

+62
-25
lines changed

src/platform/Darwin/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ static_library("Darwin") {
130130
"BleConnectionDelegateImpl.mm",
131131
"BlePlatformDelegateImpl.h",
132132
"BlePlatformDelegateImpl.mm",
133+
"BleUtils.h",
133134
"MTRUUIDHelper.h",
134135
"MTRUUIDHelperImpl.mm",
135136
]

src/platform/Darwin/BleApplicationDelegateImpl.mm

-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
#include <ble/Ble.h>
2828
#include <platform/Darwin/BleApplicationDelegateImpl.h>
2929

30-
using namespace ::chip;
31-
using namespace ::chip::Ble;
32-
3330
namespace chip {
3431
namespace DeviceLayer {
3532
namespace Internal {

src/platform/Darwin/BleConnectionDelegateImpl.mm

+16-14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <platform/CHIPDeviceLayer.h>
3131
#include <platform/Darwin/BleConnectionDelegateImpl.h>
3232
#include <platform/Darwin/BleScannerDelegate.h>
33+
#include <platform/Darwin/BleUtils.h>
3334
#include <platform/LockTracker.h>
3435
#include <setup_payload/SetupPayload.h>
3536
#include <tracing/metric_event.h>
@@ -39,6 +40,7 @@
3940

4041
using namespace chip::Ble;
4142
using namespace chip::DeviceLayer;
43+
using namespace chip::DeviceLayer::Internal;
4244
using namespace chip::Tracing::DarwinPlatform;
4345

4446
constexpr uint64_t kScanningWithDiscriminatorTimeoutInSeconds = 60;
@@ -125,7 +127,7 @@ - (void)removePeripheralsFromCache;
125127
assertChipStackLockedByCurrentThread();
126128

127129
ChipLogProgress(Ble, "ConnectionDelegate NewConnection with conn obj: %p", connObj);
128-
CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj;
130+
CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj);
129131

130132
// The BLE_CONNECTION_OBJECT represent a CBPeripheral object. In order for it to be valid the central
131133
// manager needs to still be running.
@@ -296,7 +298,7 @@ - (void)dispatchConnectionError:(CHIP_ERROR)error
296298
- (void)dispatchConnectionComplete:(CBPeripheral *)peripheral
297299
{
298300
if (self.onConnectionComplete != nil) {
299-
self.onConnectionComplete(self.appState, (__bridge void *) peripheral);
301+
self.onConnectionComplete(self.appState, BleConnObjectFromCBPeripheral(peripheral));
300302
}
301303
}
302304

@@ -465,12 +467,12 @@ - (void)peripheral:(CBPeripheral *)peripheral
465467
chip::Ble::ChipBleUUID svcId;
466468
chip::Ble::ChipBleUUID charId;
467469
[BleConnection fillServiceWithCharacteristicUuids:characteristic svcId:&svcId charId:&charId];
468-
_mBleLayer->HandleWriteConfirmation((__bridge void *) peripheral, &svcId, &charId);
470+
_mBleLayer->HandleWriteConfirmation(BleConnObjectFromCBPeripheral(peripheral), &svcId, &charId);
469471
} else {
470472
ChipLogError(
471473
Ble, "BLE:Error writing Characteristics in Chip service on the device: [%s]", [error.localizedDescription UTF8String]);
472474
MATTER_LOG_METRIC(kMetricBLEWriteChrValueFailed, BLE_ERROR_GATT_WRITE_FAILED);
473-
_mBleLayer->HandleConnectionError((__bridge void *) peripheral, BLE_ERROR_GATT_WRITE_FAILED);
475+
_mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), BLE_ERROR_GATT_WRITE_FAILED);
474476
}
475477
}
476478

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

488490
if (isNotifying) {
489-
_mBleLayer->HandleSubscribeComplete((__bridge void *) peripheral, &svcId, &charId);
491+
_mBleLayer->HandleSubscribeComplete(BleConnObjectFromCBPeripheral(peripheral), &svcId, &charId);
490492
} else {
491-
_mBleLayer->HandleUnsubscribeComplete((__bridge void *) peripheral, &svcId, &charId);
493+
_mBleLayer->HandleUnsubscribeComplete(BleConnObjectFromCBPeripheral(peripheral), &svcId, &charId);
492494
}
493495
} else {
494496
ChipLogError(Ble, "BLE:Error subscribing/unsubcribing some characteristic on the device: [%s]",
@@ -497,11 +499,11 @@ - (void)peripheral:(CBPeripheral *)peripheral
497499
if (isNotifying) {
498500
MATTER_LOG_METRIC(kMetricBLEUpdateNotificationStateForChrFailed, BLE_ERROR_GATT_WRITE_FAILED);
499501
// we're still notifying, so we must failed the unsubscription
500-
_mBleLayer->HandleConnectionError((__bridge void *) peripheral, BLE_ERROR_GATT_UNSUBSCRIBE_FAILED);
502+
_mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), BLE_ERROR_GATT_UNSUBSCRIBE_FAILED);
501503
} else {
502504
// we're not notifying, so we must failed the subscription
503505
MATTER_LOG_METRIC(kMetricBLEUpdateNotificationStateForChrFailed, BLE_ERROR_GATT_SUBSCRIBE_FAILED);
504-
_mBleLayer->HandleConnectionError((__bridge void *) peripheral, BLE_ERROR_GATT_SUBSCRIBE_FAILED);
506+
_mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), BLE_ERROR_GATT_SUBSCRIBE_FAILED);
505507
}
506508
}
507509
}
@@ -522,8 +524,8 @@ - (void)peripheral:(CBPeripheral *)peripheral
522524
if (msgBuf.IsNull()) {
523525
ChipLogError(Ble, "Failed at allocating buffer for incoming BLE data");
524526
MATTER_LOG_METRIC(kMetricBLEUpdateValueForChrFailed, CHIP_ERROR_NO_MEMORY);
525-
_mBleLayer->HandleConnectionError((__bridge void *) peripheral, CHIP_ERROR_NO_MEMORY);
526-
} else if (!_mBleLayer->HandleIndicationReceived((__bridge void *) peripheral, &svcId, &charId, std::move(msgBuf))) {
527+
_mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), CHIP_ERROR_NO_MEMORY);
528+
} else if (!_mBleLayer->HandleIndicationReceived(BleConnObjectFromCBPeripheral(peripheral), &svcId, &charId, std::move(msgBuf))) {
527529
// since this error comes from device manager core
528530
// we assume it would do the right thing, like closing the connection
529531
ChipLogError(Ble, "Failed at handling incoming BLE data");
@@ -533,7 +535,7 @@ - (void)peripheral:(CBPeripheral *)peripheral
533535
ChipLogError(
534536
Ble, "BLE:Error receiving indication of Characteristics on the device: [%s]", [error.localizedDescription UTF8String]);
535537
MATTER_LOG_METRIC(kMetricBLEUpdateValueForChrFailed, BLE_ERROR_GATT_INDICATE_FAILED);
536-
_mBleLayer->HandleConnectionError((__bridge void *) peripheral, BLE_ERROR_GATT_INDICATE_FAILED);
538+
_mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), BLE_ERROR_GATT_INDICATE_FAILED);
537539
}
538540
}
539541

@@ -647,7 +649,7 @@ - (void)updateWithDelegate:(chip::DeviceLayer::BleScannerDelegate *)delegate pre
647649
NSData * serviceData = _cachedPeripherals[cachedPeripheral][@"data"];
648650
ChipBLEDeviceIdentificationInfo info;
649651
memcpy(&info, [serviceData bytes], sizeof(info));
650-
delegate->OnBleScanAdd((__bridge void *) cachedPeripheral, info);
652+
delegate->OnBleScanAdd(BleConnObjectFromCBPeripheral(cachedPeripheral), info);
651653
}
652654
_scannerDelegate = delegate;
653655
}
@@ -721,7 +723,7 @@ - (void)addPeripheralToCache:(CBPeripheral *)peripheral data:(NSData *)data
721723
ChipBLEDeviceIdentificationInfo info;
722724
auto bytes = (const uint8_t *) [data bytes];
723725
memcpy(&info, bytes, sizeof(info));
724-
delegate->OnBleScanAdd((__bridge void *) peripheral, info);
726+
delegate->OnBleScanAdd(BleConnObjectFromCBPeripheral(peripheral), info);
725727
}
726728

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

769771
auto delegate = _scannerDelegate;
770772
if (delegate) {
771-
delegate->OnBleScanRemove((__bridge void *) peripheral);
773+
delegate->OnBleScanRemove(BleConnObjectFromCBPeripheral(peripheral));
772774
}
773775
}
774776
}

src/platform/Darwin/BlePlatformDelegateImpl.mm

+8-8
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
#include <ble/Ble.h>
2929
#include <lib/support/logging/CHIPLogging.h>
3030
#include <platform/Darwin/BlePlatformDelegateImpl.h>
31+
#include <platform/Darwin/BleUtils.h>
3132

3233
#import "MTRUUIDHelper.h"
3334

34-
using namespace ::chip;
35-
using namespace ::chip::Ble;
36-
using ::chip::System::PacketBufferHandle;
35+
using namespace chip::Ble;
36+
using chip::System::PacketBufferHandle;
3737

3838
namespace chip {
3939
namespace DeviceLayer {
@@ -49,7 +49,7 @@
4949

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

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

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

9797
CHIP_ERROR BlePlatformDelegateImpl::CloseConnection(BLE_CONNECTION_OBJECT connObj)
9898
{
99-
CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj;
99+
CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj);
100100

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

109109
uint16_t BlePlatformDelegateImpl::GetMTU(BLE_CONNECTION_OBJECT connObj) const
110110
{
111-
CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj;
111+
CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj);
112112

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

139139
for (CBService * service in peripheral.services) {
140140
if ([service.UUID.data isEqualToData:serviceId.data]) {

src/platform/Darwin/BleUtils.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (c) 2025 Project CHIP Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <ble/Ble.h>
18+
19+
@class CBPeripheral;
20+
21+
namespace chip {
22+
namespace DeviceLayer {
23+
namespace Internal {
24+
25+
inline CBPeripheral * CBPeripheralFromBleConnObject(BLE_CONNECTION_OBJECT connObj)
26+
{
27+
return (__bridge CBPeripheral *) connObj;
28+
}
29+
30+
inline BLE_CONNECTION_OBJECT BleConnObjectFromCBPeripheral(CBPeripheral * peripheral)
31+
{
32+
return (__bridge void *) peripheral;
33+
}
34+
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)