Skip to content
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

[Rendezvous] Add BLEEndPoint support to the iOS app #1460

Closed
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
111 changes: 111 additions & 0 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
// module header, comes first
#include <controller/CHIPDeviceController.h>

#if CONFIG_NETWORK_LAYER_BLE
#include <ble/BleLayer.h>
#endif // CONFIG_NETWORK_LAYER_BLE
#include <core/CHIPCore.h>
#include <core/CHIPEncoding.h>
#include <support/Base64.h>
Expand Down Expand Up @@ -62,6 +65,11 @@ ChipDeviceController::ChipDeviceController()
mDevicePort = CHIP_PORT;
mLocalDeviceId = 0;
memset(&mOnComplete, 0, sizeof(mOnComplete));
#if CONFIG_NETWORK_LAYER_BLE
mOnBleError = NULL;
mOnBleConnection = NULL;
mOnBleMessage = NULL;
#endif // CONFIG_NETWORK_LAYER_BLE
}

CHIP_ERROR ChipDeviceController::Init(NodeId localNodeId)
Expand Down Expand Up @@ -125,6 +133,18 @@ CHIP_ERROR ChipDeviceController::Shutdown()
mMessageNumber = 0;
mRemoteDeviceId.ClearValue();

#if CONFIG_NETWORK_LAYER_BLE
if (mBleEndPoint != NULL)
{
mBleEndPoint->Close();
mBleEndPoint = NULL;
}

mOnBleError = NULL;
mOnBleConnection = NULL;
mOnBleMessage = NULL;
#endif // CONFIG_NETWORK_LAYER_BLE

return err;
}

Expand Down Expand Up @@ -367,5 +387,96 @@ void ChipDeviceController::OnMessageReceived(const MessageHeader & header, Trans
}
}

#if CONFIG_NETWORK_LAYER_BLE

CHIP_ERROR ChipDeviceController::InitBle(BlePlatformDelegate * platformDelegate, BleApplicationDelegate * applicationDelegate)
{
CHIP_ERROR err = CHIP_NO_ERROR;

VerifyOrExit(mState == kState_Initialized, err = CHIP_ERROR_INCORRECT_STATE);

err = mBleLayer.Init(platformDelegate, applicationDelegate, mSystemLayer);
SuccessOrExit(err);

exit:
return err;
}

CHIP_ERROR ChipDeviceController::ConnectBle(BLE_CONNECTION_OBJECT connObj, BleNewConnectionHandler onBleConnection,
BleMessageReceiveHandler onBleMessage, BleErrorHandler onBleError)
{
CHIP_ERROR err = CHIP_NO_ERROR;
err = mBleLayer.NewBleEndPoint(&mBleEndPoint, connObj, kBleRole_Central, true);
SuccessOrExit(err);

// Set up callbacks we need to negotiate CHIPoBle connection.
mBleEndPoint->mAppState = this;
mBleEndPoint->OnConnectComplete = HandleBleConnectComplete;
mBleEndPoint->OnConnectionClosed = HandleBleConnectionClosed;
mBleEndPoint->OnMessageReceived = HandleBleMessageReceived;

// Initiate CHIP over BLE protocol connection.
err = mBleEndPoint->StartConnect();
SuccessOrExit(err);

mOnBleConnection = onBleConnection;
mOnBleMessage = onBleMessage;
mOnBleError = onBleError;

exit:
return err;
}

CHIP_ERROR ChipDeviceController::DisconnectBle(BLE_CONNECTION_OBJECT connObj)
{
CHIP_ERROR err = CHIP_NO_ERROR;
SuccessOrExit(err);

exit:
return err;
}

CHIP_ERROR ChipDeviceController::SendBleMessage(PacketBuffer * buffer)
{
CHIP_ERROR err = CHIP_NO_ERROR;

VerifyOrExit(mBleEndPoint, err = CHIP_ERROR_INCORRECT_STATE);

mBleEndPoint->Send(buffer);

exit:
return err;
}

void ChipDeviceController::HandleBleConnectComplete(BLEEndPoint * endPoint, BLE_ERROR err)
{
ChipDeviceController * dc = (ChipDeviceController *) endPoint->mAppState;
if (dc && dc->mOnBleConnection != NULL)
{
dc->mOnBleConnection(dc);
}
}

void ChipDeviceController::HandleBleConnectionClosed(BLEEndPoint * endPoint, BLE_ERROR err)
{
ChipDeviceController * dc = (ChipDeviceController *) endPoint->mAppState;
if (dc && dc->mOnBleError != NULL)
{
dc->mOnBleError(dc, err);
}
}

void ChipDeviceController::HandleBleMessageReceived(BLEEndPoint * endPoint, PacketBuffer * data)
{

ChipDeviceController * dc = (ChipDeviceController *) endPoint->mAppState;
if (dc && dc->mOnBleMessage != NULL)
{
dc->mOnBleMessage(dc, data);
}
}

#endif // CONFIG_NETWORK_LAYER_BLE

} // namespace DeviceController
} // namespace chip
25 changes: 25 additions & 0 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ typedef void (*CompleteHandler)(ChipDeviceController * deviceController, void *
typedef void (*ErrorHandler)(ChipDeviceController * deviceController, void * appReqState, CHIP_ERROR err,
const IPPacketInfo * pktInfo);
typedef void (*MessageReceiveHandler)(ChipDeviceController * deviceController, void * appReqState, System::PacketBuffer * payload);
#if CONFIG_NETWORK_LAYER_BLE
typedef void (*BleMessageReceiveHandler)(ChipDeviceController * deviceController, System::PacketBuffer * payload);
typedef void (*BleNewConnectionHandler)(ChipDeviceController * deviceController);
typedef void (*BleErrorHandler)(ChipDeviceController * deviceController, BLE_ERROR err);
#endif // CONFIG_NETWORK_LAYER_BLE
};

class DLL_EXPORT ChipDeviceController : public SecureSessionMgrCallback
Expand Down Expand Up @@ -161,6 +166,14 @@ class DLL_EXPORT ChipDeviceController : public SecureSessionMgrCallback

virtual void OnNewConnection(Transport::PeerConnectionState * state, SecureSessionMgr * mgr);

#if CONFIG_NETWORK_LAYER_BLE
CHIP_ERROR InitBle(BlePlatformDelegate * platformDelegate, BleApplicationDelegate * applicationDelegate);
CHIP_ERROR ConnectBle(BLE_CONNECTION_OBJECT connObj, BleNewConnectionHandler onBleConnection,
BleMessageReceiveHandler onBleMessage, BleErrorHandler onBleError);
CHIP_ERROR DisconnectBle(BLE_CONNECTION_OBJECT connObj);
CHIP_ERROR SendBleMessage(PacketBuffer * buffer);
#endif // CONFIG_NETWORK_LAYER_BLE

private:
enum
{
Expand Down Expand Up @@ -204,6 +217,18 @@ class DLL_EXPORT ChipDeviceController : public SecureSessionMgrCallback

void ClearRequestState();
void ClearOpState();

#if CONFIG_NETWORK_LAYER_BLE
BleLayer mBleLayer;
BLEEndPoint * mBleEndPoint;
BleErrorHandler mOnBleError;
BleNewConnectionHandler mOnBleConnection;
BleMessageReceiveHandler mOnBleMessage;

static void HandleBleConnectComplete(BLEEndPoint * endPoint, BLE_ERROR err);
static void HandleBleConnectionClosed(BLEEndPoint * endPoint, BLE_ERROR err);
static void HandleBleMessageReceived(BLEEndPoint * endPoint, PacketBuffer * data);
#endif // CONFIG_NETWORK_LAYER_BLE
};

} // namespace DeviceController
Expand Down
2 changes: 2 additions & 0 deletions src/darwin/CHIPTool/CHIPTool.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
0C47BE4324885B97005E97F6 /* CHIPViewControllerBase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CHIPViewControllerBase.m; sourceTree = "<group>"; };
0CA0E0CD248599BB009087B9 /* OnOffViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OnOffViewController.h; sourceTree = "<group>"; };
0CA0E0CE248599BB009087B9 /* OnOffViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OnOffViewController.m; sourceTree = "<group>"; };
1E42A3FF24B3679400E2BAD0 /* CoreBluetooth.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreBluetooth.framework; path = System/Library/Frameworks/CoreBluetooth.framework; sourceTree = SDKROOT; };
1E87C7B424A5E57100457C90 /* BLEConnectionController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BLEConnectionController.h; sourceTree = "<group>"; };
1E87C7B524A5E74E00457C90 /* BLEConnectionController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BLEConnectionController.m; sourceTree = "<group>"; };
515C401824868BF0004C4DB3 /* CHIPViewControllerBase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHIPViewControllerBase.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -123,6 +124,7 @@
B20252DE2459EC7600F97062 /* Frameworks */ = {
isa = PBXGroup;
children = (
1E42A3FF24B3679400E2BAD0 /* CoreBluetooth.framework */,
B2E0D7BE245B0C59003C5B48 /* CHIP.framework */,
);
name = Frameworks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#import "BLEConnectionController.h"
#import <CHIP/CHIP.h>

@interface BLEConnectionController ()

Expand Down Expand Up @@ -74,6 +75,14 @@ - (void)centralManager:(CBCentralManager *)central
}
}

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"Did connect to peripheral: %@", peripheral);

[peripheral setDelegate:[[CHIPDeviceController sharedController] mBleDelegate]];
[[CHIPDeviceController sharedController] connectBle:peripheral];
}

- (void)start
{
[self startScanning];
Expand Down
14 changes: 14 additions & 0 deletions src/darwin/Framework/CHIP.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
1E42A3FD24B3422E00E2BAD0 /* ChipBleDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1E42A3FC24B3422D00E2BAD0 /* ChipBleDelegate.mm */; };
1E42A40224B367C200E2BAD0 /* CoreBluetooth.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1E42A40124B367C200E2BAD0 /* CoreBluetooth.framework */; };
1E42A40524B369E800E2BAD0 /* ChipBleDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 1E42A3F524B33D1100E2BAD0 /* ChipBleDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; };
2C4DF09E248B2C60009307CB /* libmbedtls.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 2C4DF09D248B2C60009307CB /* libmbedtls.a */; };
515C401C2486BF43004C4DB3 /* CHIPOnOff.mm in Sources */ = {isa = PBXBuildFile; fileRef = 515C401A2486BF43004C4DB3 /* CHIPOnOff.mm */; };
515C401D2486BF43004C4DB3 /* CHIPOnOff.h in Headers */ = {isa = PBXBuildFile; fileRef = 515C401B2486BF43004C4DB3 /* CHIPOnOff.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -41,6 +44,10 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
1E42A3F524B33D1100E2BAD0 /* ChipBleDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ChipBleDelegate.h; sourceTree = "<group>"; };
1E42A3FC24B3422D00E2BAD0 /* ChipBleDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ChipBleDelegate.mm; sourceTree = "<group>"; };
1E42A3FE24B342FE00E2BAD0 /* ChipBleDelegate_Protected.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ChipBleDelegate_Protected.h; sourceTree = "<group>"; };
1E42A40124B367C200E2BAD0 /* CoreBluetooth.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreBluetooth.framework; path = System/Library/Frameworks/CoreBluetooth.framework; sourceTree = SDKROOT; };
2C4DF09D248B2C60009307CB /* libmbedtls.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libmbedtls.a; path = lib/libmbedtls.a; sourceTree = BUILT_PRODUCTS_DIR; };
515C401A2486BF43004C4DB3 /* CHIPOnOff.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CHIPOnOff.mm; sourceTree = "<group>"; };
515C401B2486BF43004C4DB3 /* CHIPOnOff.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHIPOnOff.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -72,6 +79,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
1E42A40224B367C200E2BAD0 /* CoreBluetooth.framework in Frameworks */,
BA09EB43247477BA00605257 /* libCHIP.a in Frameworks */,
2C4DF09E248B2C60009307CB /* libmbedtls.a in Frameworks */,
BA09EB44247477BC00605257 /* libSetupPayload.a in Frameworks */,
Expand Down Expand Up @@ -128,6 +136,9 @@
515C401B2486BF43004C4DB3 /* CHIPOnOff.h */,
515C401A2486BF43004C4DB3 /* CHIPOnOff.mm */,
B20252912459E34F00F97062 /* Info.plist */,
1E42A3F524B33D1100E2BAD0 /* ChipBleDelegate.h */,
1E42A3FC24B3422D00E2BAD0 /* ChipBleDelegate.mm */,
1E42A3FE24B342FE00E2BAD0 /* ChipBleDelegate_Protected.h */,
);
path = CHIP;
sourceTree = "<group>";
Expand All @@ -144,6 +155,7 @@
BA09EB3E2474762900605257 /* Frameworks */ = {
isa = PBXGroup;
children = (
1E42A40124B367C200E2BAD0 /* CoreBluetooth.framework */,
2C4DF09D248B2C60009307CB /* libmbedtls.a */,
BA09EB3F2474762900605257 /* libCHIP.a */,
BA09EB402474762900605257 /* libSetupPayload.a */,
Expand All @@ -163,6 +175,7 @@
B2E0D7B1245B0B5C003C5B48 /* CHIP.h in Headers */,
B2E0D7B8245B0B5C003C5B48 /* CHIPSetupPayload.h in Headers */,
B2E0D7B5245B0B5C003C5B48 /* CHIPQRCodeSetupPayloadParser.h in Headers */,
1E42A40524B369E800E2BAD0 /* ChipBleDelegate.h in Headers */,
515C401D2486BF43004C4DB3 /* CHIPOnOff.h in Headers */,
991DC08B247704DC00C13860 /* CHIPLogging.h in Headers */,
B2E0D7B4245B0B5C003C5B48 /* CHIPError.h in Headers */,
Expand Down Expand Up @@ -292,6 +305,7 @@
files = (
515C401C2486BF43004C4DB3 /* CHIPOnOff.mm in Sources */,
991DC0892475F47D00C13860 /* CHIPDeviceController.mm in Sources */,
1E42A3FD24B3422E00E2BAD0 /* ChipBleDelegate.mm in Sources */,
B2E0D7B7245B0B5C003C5B48 /* CHIPQRCodeSetupPayloadParser.mm in Sources */,
C4C222C02475A38700984173 /* CHIPLogging.mm in Sources */,
B2E0D7B3245B0B5C003C5B48 /* CHIPError.mm in Sources */,
Expand Down
1 change: 1 addition & 0 deletions src/darwin/Framework/CHIP/CHIP.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import <CHIP/CHIPOnOff.h>
#import <CHIP/CHIPQRCodeSetupPayloadParser.h>
#import <CHIP/CHIPSetupPayload.h>
#import <CHIP/ChipBleDelegate.h>

#import <Foundation/Foundation.h>
//! Project version number for CHIP.
Expand Down
9 changes: 9 additions & 0 deletions src/darwin/Framework/CHIP/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#define CHIP_DEVICE_CONTROLLER_H

#import "CHIPError.h"
#import "ChipBleDelegate.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN
Expand Down Expand Up @@ -47,6 +49,13 @@ typedef void (^ControllerOnErrorBlock)(NSError * error);
- (BOOL)disconnect:(NSError * __autoreleasing *)error;
- (BOOL)isConnected;

@property (strong) PreparationCompleteHandler BleConnectionPreparationCompleteHandler;
@property (readonly) CBPeripheral * blePeripheral;
@property (atomic, readonly) dispatch_queue_t WorkQueue;
@property (atomic, strong, readonly) ChipBleDelegate * mBleDelegate;

- (void)connectBle:(CBPeripheral *)peripheral;

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;

Expand Down
Loading