c753b65b848405f9091ab6cf104d7f130d2c3882
[platform/upstream/connectedhomeip.git] / src / controller / python / ChipDeviceController-BlePlatformDelegate.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2014-2017 Nest Labs, Inc.
5  *    All rights reserved.
6  *
7  *    Licensed under the Apache License, Version 2.0 (the "License");
8  *    you may not use this file except in compliance with the License.
9  *    You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *    Unless required by applicable law or agreed to in writing, software
14  *    distributed under the License is distributed on an "AS IS" BASIS,
15  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *    See the License for the specific language governing permissions and
17  *    limitations under the License.
18  */
19
20 #include "ChipDeviceController-BlePlatformDelegate.h"
21 #include <ble/BlePlatformDelegate.h>
22
23 using namespace chip::Ble;
24
25 DeviceController_BlePlatformDelegate::DeviceController_BlePlatformDelegate(BleLayer * ble)
26 {
27     Ble         = ble;
28     writeCB     = NULL;
29     subscribeCB = NULL;
30     closeCB     = NULL;
31 }
32
33 bool DeviceController_BlePlatformDelegate::SubscribeCharacteristic(BLE_CONNECTION_OBJECT connObj,
34                                                                    const chip::Ble::ChipBleUUID * svcId,
35                                                                    const chip::Ble::ChipBleUUID * charId)
36 {
37     const bool subscribe = true;
38
39     if (subscribeCB && svcId && charId)
40     {
41         return subscribeCB(connObj, static_cast<const void *>(svcId->bytes), static_cast<const void *>(charId->bytes), subscribe);
42     }
43
44     return false;
45 }
46
47 bool DeviceController_BlePlatformDelegate::UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT connObj,
48                                                                      const chip::Ble::ChipBleUUID * svcId,
49                                                                      const chip::Ble::ChipBleUUID * charId)
50 {
51     const bool subscribe = true;
52
53     if (subscribeCB && svcId && charId)
54     {
55         return subscribeCB(connObj, static_cast<const void *>(svcId->bytes), static_cast<const void *>(charId->bytes), !subscribe);
56     }
57
58     return false;
59 }
60
61 bool DeviceController_BlePlatformDelegate::CloseConnection(BLE_CONNECTION_OBJECT connObj)
62 {
63     if (closeCB)
64     {
65         return closeCB(connObj);
66     }
67
68     return false;
69 }
70
71 uint16_t DeviceController_BlePlatformDelegate::GetMTU(BLE_CONNECTION_OBJECT connObj) const
72 {
73     // TODO Python queue-based implementation
74     return 0;
75 }
76
77 bool DeviceController_BlePlatformDelegate::SendIndication(BLE_CONNECTION_OBJECT connObj, const chip::Ble::ChipBleUUID * svcId,
78                                                           const chip::Ble::ChipBleUUID * charId,
79                                                           chip::System::PacketBufferHandle pBuf)
80 {
81     // TODO Python queue-based implementation
82
83     // Going out of scope releases delegate's reference to pBuf. pBuf will be freed when both platform delegate and Chip stack free
84     // their references to it.
85     return false;
86 }
87
88 bool DeviceController_BlePlatformDelegate::SendWriteRequest(BLE_CONNECTION_OBJECT connObj, const chip::Ble::ChipBleUUID * svcId,
89                                                             const chip::Ble::ChipBleUUID * charId,
90                                                             chip::System::PacketBufferHandle pBuf)
91 {
92     bool ret = false;
93
94     if (writeCB && svcId && charId && !pBuf.IsNull())
95     {
96         ret = writeCB(connObj, static_cast<const void *>(svcId->bytes), static_cast<const void *>(charId->bytes),
97                       static_cast<void *>(pBuf->Start()), pBuf->DataLength());
98     }
99
100     // Going out of scope releases delegate's reference to pBuf. pBuf will be freed when both platform delegate and Chip stack free
101     // their references to it. We release pBuf's reference here since its payload bytes were copied into a new NSData object by
102     // ChipBleMgr.py's writeCB, and in both the error and succees cases this code has no further use for the pBuf PacketBuffer.
103     return ret;
104 }
105
106 bool DeviceController_BlePlatformDelegate::SendReadRequest(BLE_CONNECTION_OBJECT connObj, const chip::Ble::ChipBleUUID * svcId,
107                                                            const chip::Ble::ChipBleUUID * charId,
108                                                            chip::System::PacketBufferHandle pBuf)
109 {
110     // TODO Python queue-based implementation
111     return false;
112 }
113
114 bool DeviceController_BlePlatformDelegate::SendReadResponse(BLE_CONNECTION_OBJECT connObj, BLE_READ_REQUEST_CONTEXT requestContext,
115                                                             const chip::Ble::ChipBleUUID * svcId,
116                                                             const chip::Ble::ChipBleUUID * charId)
117 {
118     // TODO Python queue-based implementation
119     return false;
120 }