2 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <ble/BleConfig.h>
21 #if CONFIG_NETWORK_LAYER_BLE
23 #include "AndroidBlePlatformDelegate.h"
27 using namespace chip::Ble;
29 AndroidBlePlatformDelegate::AndroidBlePlatformDelegate() :
30 SendWriteRequestCb(NULL), SubscribeCharacteristicCb(NULL), UnsubscribeCharacteristicCb(NULL), CloseConnectionCb(NULL),
34 bool AndroidBlePlatformDelegate::SubscribeCharacteristic(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId,
35 const ChipBleUUID * charId)
38 if (SubscribeCharacteristicCb)
40 rc = SubscribeCharacteristicCb(connObj, static_cast<const uint8_t *>(svcId->bytes),
41 static_cast<const uint8_t *>(charId->bytes));
46 bool AndroidBlePlatformDelegate::UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId,
47 const ChipBleUUID * charId)
50 if (UnsubscribeCharacteristicCb)
52 rc = UnsubscribeCharacteristicCb(connObj, static_cast<const uint8_t *>(svcId->bytes),
53 static_cast<const uint8_t *>(charId->bytes));
58 uint16_t AndroidBlePlatformDelegate::GetMTU(BLE_CONNECTION_OBJECT connObj) const
63 mtu = GetMTUCb(connObj);
68 bool AndroidBlePlatformDelegate::CloseConnection(BLE_CONNECTION_OBJECT connObj)
71 if (CloseConnectionCb)
73 rc = CloseConnectionCb(connObj);
78 bool AndroidBlePlatformDelegate::SendIndication(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId,
79 const ChipBleUUID * charId, PacketBufferHandle pBuf)
81 // Going out of scope releases delegate's reference to pBuf. pBuf will be freed when both
82 // platform delegate and Chip stack free their references to it.
86 bool AndroidBlePlatformDelegate::SendWriteRequest(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId,
87 const ChipBleUUID * charId, PacketBufferHandle pBuf)
90 if (SendWriteRequestCb)
92 rc = SendWriteRequestCb(connObj, static_cast<const uint8_t *>(svcId->bytes), static_cast<const uint8_t *>(charId->bytes),
93 pBuf->Start(), pBuf->DataLength());
96 // Going out of scope releases delegate's reference to pBuf. pBuf will be freed when both
97 // platform delegate and Chip stack free their references to it.
98 // We release pBuf's reference here since its payload bytes were copied
99 // onto the Java heap by SendWriteRequestCb.
103 bool AndroidBlePlatformDelegate::SendReadRequest(BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId,
104 const ChipBleUUID * charId, PacketBufferHandle pBuf)
109 bool AndroidBlePlatformDelegate::SendReadResponse(BLE_CONNECTION_OBJECT connObj, BLE_READ_REQUEST_CONTEXT requestContext,
110 const ChipBleUUID * svcId, const ChipBleUUID * charId)
115 void AndroidBlePlatformDelegate::SetSendWriteRequestCallback(SendWriteRequestCallback cb)
117 SendWriteRequestCb = cb;
120 void AndroidBlePlatformDelegate::SetSubscribeCharacteristicCallback(SubscribeCharacteristicCallback cb)
122 SubscribeCharacteristicCb = cb;
125 void AndroidBlePlatformDelegate::SetUnsubscribeCharacteristicCallback(UnsubscribeCharacteristicCallback cb)
127 UnsubscribeCharacteristicCb = cb;
130 void AndroidBlePlatformDelegate::SetCloseConnectionCallback(CloseConnectionCallback cb)
132 CloseConnectionCb = cb;
135 void AndroidBlePlatformDelegate::SetGetMTUCallback(GetMTUCallback cb)
139 #endif /* CONFIG_NETWORK_LAYER_BLE */