• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "bluetooth_gatt_server_callback_stub.h"
17 #include "bluetooth_log.h"
18 #include "ipc_types.h"
19 #include "string_ex.h"
20 
21 namespace OHOS {
22 namespace Bluetooth {
BluetoothGattServerCallbackStub()23 BluetoothGattServerCallbackStub::BluetoothGattServerCallbackStub()
24 {
25     HILOGI("start.");
26     memberFuncMap_[static_cast<uint32_t>(
27         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_CHARACTERISTIC_READREQUEST)] =
28         &BluetoothGattServerCallbackStub::OnCharacteristicReadRequestInner;
29     memberFuncMap_[static_cast<uint32_t>(
30         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_CONNECTIONSTATE_CHANGED)] =
31         &BluetoothGattServerCallbackStub::OnConnectionStateChangedInner;
32     memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_ADD_SERVICE)] =
33         &BluetoothGattServerCallbackStub::OnAddServiceInner;
34     memberFuncMap_[static_cast<uint32_t>(
35         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_CHARACTERISTIC_WRITE_REQUEST)] =
36         &BluetoothGattServerCallbackStub::OnCharacteristicWriteRequestInner;
37     memberFuncMap_[static_cast<uint32_t>(
38         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_DESCRIPTOR_READ_REQUEST)] =
39         &BluetoothGattServerCallbackStub::OnDescriptorReadRequestInner;
40     memberFuncMap_[static_cast<uint32_t>(
41         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_DESCRIPTOR_WRITE_REQUEST)] =
42         &BluetoothGattServerCallbackStub::OnDescriptorWriteRequestInner;
43     memberFuncMap_[static_cast<uint32_t>(BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_MTU_CHANGED)] =
44         &BluetoothGattServerCallbackStub::OnMtuChangedInner;
45     memberFuncMap_[static_cast<uint32_t>(
46         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_NOTIFY_CONFIRM)] =
47         &BluetoothGattServerCallbackStub::OnNotifyConfirmInner;
48     memberFuncMap_[static_cast<uint32_t>(
49         BluetoothGattServerCallbackInterfaceCode::GATT_SERVER_CALLBACK_CONNECTION_PARAMETER_CHANGED)] =
50         &BluetoothGattServerCallbackStub::OnConnectionParameterChangedInner;
51     HILOGI("ends.");
52 }
53 
~BluetoothGattServerCallbackStub()54 BluetoothGattServerCallbackStub::~BluetoothGattServerCallbackStub()
55 {
56     HILOGI("start.");
57     memberFuncMap_.clear();
58 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)59 int BluetoothGattServerCallbackStub::OnRemoteRequest(
60     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
61 {
62     HILOGD("BluetoothGattServerCallbackStub::OnRemoteRequest, cmd = %{public}u, flags= %{public}d",
63         code, option.GetFlags());
64     if (BluetoothGattServerCallbackStub::GetDescriptor() != data.ReadInterfaceToken()) {
65         HILOGI("local descriptor is not equal to remote");
66         return ERR_INVALID_STATE;
67     }
68 
69     auto itFunc = memberFuncMap_.find(code);
70     if (itFunc != memberFuncMap_.end()) {
71         auto memberFunc = itFunc->second;
72         if (memberFunc != nullptr) {
73             return (this->*memberFunc)(data, reply);
74         }
75     }
76     HILOGW("BluetoothGattServerCallbackStub::OnRemoteRequest, default case, need check.");
77     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
78 };
OnCharacteristicReadRequestInner(MessageParcel & data,MessageParcel & reply)79 ErrCode BluetoothGattServerCallbackStub::OnCharacteristicReadRequestInner(MessageParcel &data, MessageParcel &reply)
80 {
81     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
82     if (!device) {
83         return TRANSACTION_ERR;
84     }
85     std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
86     if (!characteristic) {
87         return TRANSACTION_ERR;
88     }
89 
90     OnCharacteristicReadRequest(*device, *characteristic);
91 
92     return NO_ERROR;
93 }
OnConnectionStateChangedInner(MessageParcel & data,MessageParcel & reply)94 ErrCode BluetoothGattServerCallbackStub::OnConnectionStateChangedInner(MessageParcel &data, MessageParcel &reply)
95 {
96     HILOGD("BluetoothGattServerCallbackStub::OnConnectionStateChangedInner Triggered!");
97     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
98     if (!device) {
99         HILOGE("BluetoothGattServerCallbackStub::OnConnectionStateChangedInner device is null");
100         return TRANSACTION_ERR;
101     }
102 
103     int32_t ret = data.ReadInt32();
104     int32_t state = data.ReadInt32();
105     OnConnectionStateChanged(*device, ret, state);
106 
107     return NO_ERROR;
108 }
OnAddServiceInner(MessageParcel & data,MessageParcel & reply)109 ErrCode BluetoothGattServerCallbackStub::OnAddServiceInner(MessageParcel &data, MessageParcel &reply)
110 {
111     HILOGI("BluetoothGattServerCallbackStub::OnAddServiceInner Triggered!");
112     int32_t ret = data.ReadInt32();
113     std::shared_ptr<BluetoothGattService> service(data.ReadParcelable<BluetoothGattService>());
114     if (!service) {
115         return TRANSACTION_ERR;
116     }
117 
118     OnAddService(ret, *service);
119 
120     service = nullptr;
121 
122     return NO_ERROR;
123 }
OnCharacteristicWriteRequestInner(MessageParcel & data,MessageParcel & reply)124 ErrCode BluetoothGattServerCallbackStub::OnCharacteristicWriteRequestInner(MessageParcel &data, MessageParcel &reply)
125 {
126     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
127     if (!device) {
128         return TRANSACTION_ERR;
129     }
130     std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
131     if (!characteristic) {
132         return TRANSACTION_ERR;
133     }
134     bool needRespones = data.ReadBool();
135 
136     OnCharacteristicWriteRequest(*device, *characteristic, needRespones);
137 
138     return NO_ERROR;
139 }
OnDescriptorReadRequestInner(MessageParcel & data,MessageParcel & reply)140 ErrCode BluetoothGattServerCallbackStub::OnDescriptorReadRequestInner(MessageParcel &data, MessageParcel &reply)
141 {
142     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
143     if (!device) {
144         return TRANSACTION_ERR;
145     }
146     std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
147     if (!descriptor) {
148         return TRANSACTION_ERR;
149     }
150 
151     OnDescriptorReadRequest(*device, *descriptor);
152 
153     return NO_ERROR;
154 }
OnDescriptorWriteRequestInner(MessageParcel & data,MessageParcel & reply)155 ErrCode BluetoothGattServerCallbackStub::OnDescriptorWriteRequestInner(MessageParcel &data, MessageParcel &reply)
156 {
157     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
158     if (!device) {
159         return TRANSACTION_ERR;
160     }
161     std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
162     if (!descriptor) {
163         return TRANSACTION_ERR;
164     }
165 
166     OnDescriptorWriteRequest(*device, *descriptor);
167 
168     return NO_ERROR;
169 }
OnMtuChangedInner(MessageParcel & data,MessageParcel & reply)170 ErrCode BluetoothGattServerCallbackStub::OnMtuChangedInner(MessageParcel &data, MessageParcel &reply)
171 {
172     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
173     if (!device) {
174         return TRANSACTION_ERR;
175     }
176     int32_t mtu = data.ReadInt32();
177 
178     OnMtuChanged(*device, mtu);
179 
180     return NO_ERROR;
181 }
OnNotifyConfirmInner(MessageParcel & data,MessageParcel & reply)182 ErrCode BluetoothGattServerCallbackStub::OnNotifyConfirmInner(MessageParcel &data, MessageParcel &reply)
183 {
184     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
185     if (!device) {
186         return TRANSACTION_ERR;
187     }
188     std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
189     if (!characteristic) {
190         return TRANSACTION_ERR;
191     }
192     int32_t result = data.ReadInt32();
193 
194     OnNotifyConfirm(*device, *characteristic, result);
195 
196     return NO_ERROR;
197 }
OnConnectionParameterChangedInner(MessageParcel & data,MessageParcel & reply)198 ErrCode BluetoothGattServerCallbackStub::OnConnectionParameterChangedInner(MessageParcel &data, MessageParcel &reply)
199 {
200     std::shared_ptr<BluetoothGattDevice> device(data.ReadParcelable<BluetoothGattDevice>());
201     if (!device) {
202         return TRANSACTION_ERR;
203     }
204     int32_t interval = data.ReadInt32();
205     int32_t latency = data.ReadInt32();
206     int32_t timeout = data.ReadInt32();
207     int32_t status = data.ReadInt32();
208 
209     OnConnectionParameterChanged(*device, interval, latency, timeout, status);
210 
211     return NO_ERROR;
212 }
213 }  // namespace Bluetooth
214 }  // namespace OHOS