1 //
2 //  Copyright (C) 2015 Google, Inc.
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 "service/common/bluetooth/binder/IBluetoothGattServerCallback.h"
18 
19 #include <base/logging.h>
20 #include <binder/Parcel.h>
21 
22 #include "service/common/bluetooth/binder/parcel_helpers.h"
23 
24 using android::IBinder;
25 using android::Parcel;
26 using android::sp;
27 using android::status_t;
28 
29 namespace ipc {
30 namespace binder {
31 
32 // static
33 const char IBluetoothGattServerCallback::kServiceName[] =
34     "bluetooth-gatt-server-callback-service";
35 
36 // BnBluetoothGattServerCallback (server) implementation
37 // ========================================================
38 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)39 status_t BnBluetoothGattServerCallback::onTransact(
40     uint32_t code,
41     const Parcel& data,
42     Parcel* reply,
43     uint32_t flags) {
44   VLOG(2) << "IBluetoothGattServerCallback: " << code;
45   if (!data.checkInterface(this))
46     return android::PERMISSION_DENIED;
47 
48   switch (code) {
49   case ON_SERVER_REGISTERED_TRANSACTION: {
50     int status = data.readInt32();
51     int server_if = data.readInt32();
52     OnServerRegistered(status, server_if);
53     return android::NO_ERROR;
54   }
55   case ON_SERVICE_ADDED_TRANSACTION: {
56     int status = data.readInt32();
57     auto gatt_id = CreateGattIdentifierFromParcel(data);
58     CHECK(gatt_id);
59     OnServiceAdded(status, *gatt_id);
60     return android::NO_ERROR;
61   }
62   case ON_CHARACTERISTIC_READ_REQUEST_TRANSACTION: {
63     std::string device_address = data.readCString();
64     int request_id = data.readInt32();
65     int offset = data.readInt32();
66     bool is_long = data.readInt32();
67     auto char_id = CreateGattIdentifierFromParcel(data);
68     CHECK(char_id);
69     OnCharacteristicReadRequest(device_address, request_id, offset, is_long,
70                                 *char_id);
71     return android::NO_ERROR;
72   }
73   case ON_DESCRIPTOR_READ_REQUEST_TRANSACTION: {
74     std::string device_address = data.readCString();
75     int request_id = data.readInt32();
76     int offset = data.readInt32();
77     bool is_long = data.readInt32();
78     auto desc_id = CreateGattIdentifierFromParcel(data);
79     CHECK(desc_id);
80     OnDescriptorReadRequest(device_address, request_id, offset, is_long,
81                             *desc_id);
82     return android::NO_ERROR;
83   }
84   case ON_CHARACTERISTIC_WRITE_REQUEST_TRANSACTION: {
85     std::string device_address = data.readCString();
86     int request_id = data.readInt32();
87     int offset = data.readInt32();
88     bool is_prep = data.readInt32();
89     bool need_rsp = data.readInt32();
90 
91     std::unique_ptr<std::vector<uint8_t>> value;
92     data.readByteVector(&value);
93     CHECK(value.get());
94 
95     auto char_id = CreateGattIdentifierFromParcel(data);
96     CHECK(char_id);
97 
98     OnCharacteristicWriteRequest(device_address, request_id, offset, is_prep,
99                                  need_rsp, *value, *char_id);
100     return android::NO_ERROR;
101   }
102   case ON_DESCRIPTOR_WRITE_REQUEST_TRANSACTION: {
103     std::string device_address = data.readCString();
104     int request_id = data.readInt32();
105     int offset = data.readInt32();
106     bool is_prep = data.readInt32();
107     bool need_rsp = data.readInt32();
108 
109     std::unique_ptr<std::vector<uint8_t>> value;
110     data.readByteVector(&value);
111     CHECK(value.get());
112 
113     auto desc_id = CreateGattIdentifierFromParcel(data);
114     CHECK(desc_id);
115 
116     OnDescriptorWriteRequest(device_address, request_id, offset, is_prep,
117                              need_rsp, *value, *desc_id);
118     return android::NO_ERROR;
119   }
120   case ON_EXECUTE_WRITE_REQUEST_TRANSACTION: {
121     std::string device_address = data.readCString();
122     int request_id = data.readInt32();
123     bool is_exec = data.readInt32();
124 
125     OnExecuteWriteRequest(device_address, request_id, is_exec);
126     return android::NO_ERROR;
127   }
128   case ON_NOTIFICATION_SENT_TRANSACTION: {
129     std::string device_address = data.readCString();
130     int status = data.readInt32();
131 
132     OnNotificationSent(device_address, status);
133     return android::NO_ERROR;
134   }
135   default:
136     return BBinder::onTransact(code, data, reply, flags);
137   }
138 }
139 
140 // BpBluetoothGattServerCallback (client) implementation
141 // ========================================================
142 
BpBluetoothGattServerCallback(const sp<IBinder> & impl)143 BpBluetoothGattServerCallback::BpBluetoothGattServerCallback(
144     const sp<IBinder>& impl)
145     : BpInterface<IBluetoothGattServerCallback>(impl) {
146 }
147 
OnServerRegistered(int status,int server_if)148 void BpBluetoothGattServerCallback::OnServerRegistered(
149     int status, int server_if) {
150   Parcel data, reply;
151 
152   data.writeInterfaceToken(
153       IBluetoothGattServerCallback::getInterfaceDescriptor());
154   data.writeInt32(status);
155   data.writeInt32(server_if);
156 
157   remote()->transact(
158       IBluetoothGattServerCallback::ON_SERVER_REGISTERED_TRANSACTION,
159       data, &reply,
160       IBinder::FLAG_ONEWAY);
161 }
162 
OnServiceAdded(int status,const bluetooth::GattIdentifier & service_id)163 void BpBluetoothGattServerCallback::OnServiceAdded(
164     int status,
165     const bluetooth::GattIdentifier& service_id) {
166   Parcel data, reply;
167 
168   data.writeInterfaceToken(
169       IBluetoothGattServerCallback::getInterfaceDescriptor());
170   data.writeInt32(status);
171   WriteGattIdentifierToParcel(service_id, &data);
172 
173   remote()->transact(IBluetoothGattServerCallback::ON_SERVICE_ADDED_TRANSACTION,
174                      data, &reply,
175                      IBinder::FLAG_ONEWAY);
176 }
177 
OnCharacteristicReadRequest(const std::string & device_address,int request_id,int offset,bool is_long,const bluetooth::GattIdentifier & characteristic_id)178 void BpBluetoothGattServerCallback::OnCharacteristicReadRequest(
179     const std::string& device_address,
180     int request_id, int offset, bool is_long,
181     const bluetooth::GattIdentifier& characteristic_id) {
182   Parcel data, reply;
183 
184   data.writeInterfaceToken(
185       IBluetoothGattServerCallback::getInterfaceDescriptor());
186   data.writeCString(device_address.c_str());
187   data.writeInt32(request_id);
188   data.writeInt32(offset);
189   data.writeInt32(is_long);
190   WriteGattIdentifierToParcel(characteristic_id, &data);
191 
192   remote()->transact(
193       IBluetoothGattServerCallback::ON_CHARACTERISTIC_READ_REQUEST_TRANSACTION,
194       data, &reply,
195       IBinder::FLAG_ONEWAY);
196 }
197 
OnDescriptorReadRequest(const std::string & device_address,int request_id,int offset,bool is_long,const bluetooth::GattIdentifier & descriptor_id)198 void BpBluetoothGattServerCallback::OnDescriptorReadRequest(
199     const std::string& device_address,
200     int request_id, int offset, bool is_long,
201     const bluetooth::GattIdentifier& descriptor_id) {
202   Parcel data, reply;
203 
204   data.writeInterfaceToken(
205       IBluetoothGattServerCallback::getInterfaceDescriptor());
206   data.writeCString(device_address.c_str());
207   data.writeInt32(request_id);
208   data.writeInt32(offset);
209   data.writeInt32(is_long);
210   WriteGattIdentifierToParcel(descriptor_id, &data);
211 
212   remote()->transact(
213       IBluetoothGattServerCallback::ON_DESCRIPTOR_READ_REQUEST_TRANSACTION,
214       data, &reply,
215       IBinder::FLAG_ONEWAY);
216 }
217 
OnCharacteristicWriteRequest(const std::string & device_address,int request_id,int offset,bool is_prepare_write,bool need_response,const std::vector<uint8_t> & value,const bluetooth::GattIdentifier & characteristic_id)218 void BpBluetoothGattServerCallback::OnCharacteristicWriteRequest(
219     const std::string& device_address,
220     int request_id, int offset, bool is_prepare_write, bool need_response,
221     const std::vector<uint8_t>& value,
222     const bluetooth::GattIdentifier& characteristic_id) {
223   Parcel data, reply;
224 
225   data.writeInterfaceToken(
226       IBluetoothGattServerCallback::getInterfaceDescriptor());
227   data.writeCString(device_address.c_str());
228   data.writeInt32(request_id);
229   data.writeInt32(offset);
230   data.writeInt32(is_prepare_write);
231   data.writeInt32(need_response);
232   data.writeByteVector(value);
233   WriteGattIdentifierToParcel(characteristic_id, &data);
234 
235   remote()->transact(
236       IBluetoothGattServerCallback::ON_CHARACTERISTIC_WRITE_REQUEST_TRANSACTION,
237       data, &reply,
238       IBinder::FLAG_ONEWAY);
239 }
240 
OnDescriptorWriteRequest(const std::string & device_address,int request_id,int offset,bool is_prepare_write,bool need_response,const std::vector<uint8_t> & value,const bluetooth::GattIdentifier & descriptor_id)241 void BpBluetoothGattServerCallback::OnDescriptorWriteRequest(
242     const std::string& device_address,
243     int request_id, int offset, bool is_prepare_write, bool need_response,
244     const std::vector<uint8_t>& value,
245     const bluetooth::GattIdentifier& descriptor_id) {
246   Parcel data, reply;
247 
248   data.writeInterfaceToken(
249       IBluetoothGattServerCallback::getInterfaceDescriptor());
250   data.writeCString(device_address.c_str());
251   data.writeInt32(request_id);
252   data.writeInt32(offset);
253   data.writeInt32(is_prepare_write);
254   data.writeInt32(need_response);
255   data.writeByteVector(value);
256   WriteGattIdentifierToParcel(descriptor_id, &data);
257 
258   remote()->transact(
259       IBluetoothGattServerCallback::ON_DESCRIPTOR_WRITE_REQUEST_TRANSACTION,
260       data, &reply,
261       IBinder::FLAG_ONEWAY);
262 }
263 
OnExecuteWriteRequest(const std::string & device_address,int request_id,bool is_execute)264 void BpBluetoothGattServerCallback::OnExecuteWriteRequest(
265     const std::string& device_address,
266     int request_id, bool is_execute) {
267   Parcel data, reply;
268 
269   data.writeInterfaceToken(
270       IBluetoothGattServerCallback::getInterfaceDescriptor());
271   data.writeCString(device_address.c_str());
272   data.writeInt32(request_id);
273   data.writeInt32(is_execute);
274 
275   remote()->transact(
276       IBluetoothGattServerCallback::ON_EXECUTE_WRITE_REQUEST_TRANSACTION,
277       data, &reply,
278       IBinder::FLAG_ONEWAY);
279 }
280 
OnNotificationSent(const std::string & device_address,int status)281 void BpBluetoothGattServerCallback::OnNotificationSent(
282     const std::string& device_address,
283     int status) {
284   Parcel data, reply;
285 
286   data.writeInterfaceToken(
287       IBluetoothGattServerCallback::getInterfaceDescriptor());
288   data.writeCString(device_address.c_str());
289   data.writeInt32(status);
290 
291   remote()->transact(
292       IBluetoothGattServerCallback::ON_NOTIFICATION_SENT_TRANSACTION,
293       data, &reply,
294       IBinder::FLAG_ONEWAY);
295 }
296 
297 IMPLEMENT_META_INTERFACE(BluetoothGattServerCallback,
298                          IBluetoothGattServerCallback::kServiceName);
299 
300 }  // namespace binder
301 }  // namespace ipc
302