1#!/usr/bin/env python3
2#
3#   Copyright 2022 - The Android Open Source Project
4#
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
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
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.
16
17import asyncio
18import grpc
19
20from blueberry.facade.topshim import facade_pb2
21from blueberry.facade.topshim import facade_pb2_grpc
22from blueberry.tests.topshim.lib.async_closable import AsyncClosable
23from blueberry.tests.topshim.lib.async_closable import asyncSafeClose
24
25from google.protobuf import empty_pb2 as empty_proto
26
27
28class GattClient(AsyncClosable):
29    """
30    Wrapper gRPC interface to the GATT Service
31    """
32    # Timeout for async wait
33    DEFAULT_TIMEOUT = 2
34    __task_list = []
35    __channel = None
36    __gatt_stub = None
37    __adapter_event_stream = None
38
39    def __init__(self, port=8999):
40        self.__channel = grpc.aio.insecure_channel("localhost:%d" % port)
41        self.__gatt_stub = facade_pb2_grpc.GattServiceStub(self.__channel)
42        #self.__gatt_event_stream = self.__gatt_stub.FetchEvents(facade_pb2.FetchEventsRequest())
43
44    async def close(self):
45        """
46        Terminate the current tasks
47        """
48        for task in self.__task_list:
49            task.cancel()
50            task = None
51        self.__task_list.clear()
52        await self.__channel.close()
53
54    async def register_advertiser(self):
55        """
56        """
57        await self.__gatt_stub.RegisterAdvertiser(empty_proto.Empty())
58
59    async def unregister_advertiser(self, advertiser_id):
60        """
61        Stop advertising for advertiser id
62        """
63        # TODO(optedoblivion): make message to pass advertiser id
64        await self.__gatt_stub.UnregisterAdvertiser(empty_proto.Empty())
65
66    async def get_own_address(self):
67        """
68        """
69        await self.__gatt_stub.GetOwnAddress(empty_proto.Empty())
70
71    async def set_parameters(self):
72        """
73        """
74        await self.__gatt_stub.SetParameters(empty_proto.Empty())
75
76    async def set_data(self):
77        """
78        """
79        await self.__gatt_stub.SetData(empty_proto.Empty())
80
81    async def advertising_enable(self):
82        """
83        """
84        await self.__gatt_stub.AdvertisingEnable(empty_proto.Empty())
85
86    async def advertising_disable(self):
87        """
88        """
89        await self.__gatt_stub.AdvertisingDisable(empty_proto.Empty())
90
91    async def set_periodic_advertising_parameters(self):
92        """
93        """
94        await self.__gatt_stub.SetPeriodicAdvertisingParameters(empty_proto.Empty())
95
96    async def set_periodic_advertising_data(self):
97        """
98        """
99        await self.__gatt_stub.SetPeriodicAdvertisingData(empty_proto.Empty())
100
101    async def set_periodic_advertising_enable(self):
102        """
103        """
104        await self.__gatt_stub.SetPeriodicAdvertisingEnable(empty_proto.Empty())
105
106    async def start_advertising(self):
107        """
108        """
109        await self.__gatt_stub.StartAdvertising(empty_proto.Empty())
110
111    async def start_advertising_set(self):
112        """
113        Start advertising with the given parameters
114        """
115        await self.__gatt_stub.StartAdvertisingSet(empty_proto.Empty())
116
117    async def register_scanner(self):
118        """
119        """
120        await self.__gatt_stub.RegisterScanner(empty_proto.Empty())
121
122    async def unregister_scanner(self):
123        """
124        """
125        await self.__gatt_stub.UnregisterScanner(empty_proto.Empty())
126
127    async def start_scan(self):
128        """
129        """
130        await self.__gatt_stub.StartScan(empty_proto.Empty())
131
132    async def stop_scan(self):
133        """
134        """
135        await self.__gatt_stub.StopScan(empty_proto.Empty())
136
137    async def scan_filter_setup(self):
138        """
139        """
140        await self.__gatt_stub.ScanFilterSetup(empty_proto.Empty())
141
142    async def scan_filter_add(self):
143        """
144        """
145        await self.__gatt_stub.ScanFilterAdd(empty_proto.Empty())
146
147    async def scan_filter_clear(self):
148        """
149        """
150        await self.__gatt_stub.ScanFilterClear(empty_proto.Empty())
151
152    async def scan_filter_enable(self):
153        """
154        """
155        await self.__gatt_stub.ScanFilterEnable(empty_proto.Empty())
156
157    async def scan_filter_disable(self):
158        """
159        """
160        await self.__gatt_stub.ScanFilterDisable(empty_proto.Empty())
161
162    async def set_scan_parameters(self):
163        """
164        """
165        await self.__gatt_stub.SetScanParameters(empty_proto.Empty())
166
167    async def batch_scan_config_storage(self):
168        """
169        """
170        await self.__gatt_stub.BatchScanConfigStorage(empty_proto.Empty())
171
172    async def batch_scan_enable(self):
173        """
174        """
175        await self.__gatt_stub.BatchScanEnable(empty_proto.Empty())
176
177    async def batch_scan_disable(self):
178        """
179        """
180        await self.__gatt_stub.BatchScanDisable(empty_proto.Empty())
181
182    async def batch_scan_read_reports(self):
183        """
184        """
185        await self.__gatt_stub.BatchScanReadReports(empty_proto.Empty())
186
187    async def start_sync(self):
188        """
189        """
190        await self.__gatt_stub.StartSync(empty_proto.Empty())
191
192    async def stop_sync(self):
193        """
194        """
195        await self.__gatt_stub.StopSync(empty_proto.Empty())
196
197    async def cancel_create_sync(self):
198        """
199        """
200        await self.__gatt_stub.CancelCreateSync(empty_proto.Empty())
201
202    async def transfer_sync(self):
203        """
204        """
205        await self.__gatt_stub.TransferSync(empty_proto.Empty())
206
207    async def transfer_set_info(self):
208        """
209        """
210        await self.__gatt_stub.TransferSetInfo(empty_proto.Empty())
211
212    async def sync_tx_parameters(self):
213        """
214        """
215        await self.__gatt_stub.SyncTxParameters(empty_proto.Empty())
216
217    async def register_client(self):
218        """
219        """
220        await self.__gatt_stub.RegisterClient(empty_proto.Empty())
221
222    async def unregister_client(self):
223        """
224        """
225        await self.__gatt_stub.UnregisterClient(empty_proto.Empty())
226
227    async def connect(self):
228        """
229        """
230        await self.__gatt_stub.Connect(empty_proto.Empty())
231
232    async def disconnect(self):
233        """
234        """
235        await self.__gatt_stub.Disconnect(empty_proto.Empty())
236
237    async def refresh(self):
238        """
239        """
240        await self.__gatt_stub.Refresh(empty_proto.Empty())
241
242    async def search_service(self):
243        """
244        """
245        await self.__gatt_stub.SearchService(empty_proto.Empty())
246
247    async def btif_gattc_discover_service_by_uuid(self):
248        """
249        """
250        await self.__gatt_stub.BtifGattcDiscoverServiceByUuid(empty_proto.Empty())
251
252    async def read_characteristic(self):
253        """
254        """
255        await self.__gatt_stub.ReadCharacteristic(empty_proto.Empty())
256
257    async def read_using_characteristic_uuid(self):
258        """
259        """
260        await self.__gatt_stub.ReadUsingCharacteristicUuid(empty_proto.Empty())
261
262    async def write_characteristic(self):
263        """
264        """
265        await self.__gatt_stub.WriteCharacteristic(empty_proto.Empty())
266
267    async def read_descriptor(self):
268        """
269        """
270        await self.__gatt_stub.ReadDescriptor(empty_proto.Empty())
271
272    async def write_descriptor(self):
273        """
274        """
275        await self.__gatt_stub.WriteDescriptor(empty_proto.Empty())
276
277    async def execute_write(self):
278        """
279        """
280        await self.__gatt_stub.ExecuteWrite(empty_proto.Empty())
281
282    async def register_for_notification(self):
283        """
284        """
285        await self.__gatt_stub.RegisterForNotification(empty_proto.Empty())
286
287    async def deregister_for_notification(self):
288        """
289        """
290        await self.__gatt_stub.DeregisterForNotification(empty_proto.Empty())
291
292    async def read_remote_rssi(self):
293        """
294        """
295        await self.__gatt_stub.ReadRemoteRssi(empty_proto.Empty())
296
297    async def get_device_type(self):
298        """
299        """
300        await self.__gatt_stub.GetDeviceType(empty_proto.Empty())
301
302    async def configure_mtu(self):
303        """
304        """
305        await self.__gatt_stub.ConfigureMtu(empty_proto.Empty())
306
307    async def conn_parameter_update(self):
308        """
309        """
310        await self.__gatt_stub.ConnParameterUpdate(empty_proto.Empty())
311
312    async def set_preferred_phy(self):
313        """
314        """
315        await self.__gatt_stub.SetPreferredPhy(empty_proto.Empty())
316
317    async def read_phy(self):
318        """
319        """
320        await self.__gatt_stub.ReadPhy(empty_proto.Empty())
321
322    async def test_command(self):
323        """
324        """
325        await self.__gatt_stub.TestCommand(empty_proto.Empty())
326
327    async def get_gatt_db(self):
328        """
329        """
330        await self.__gatt_stub.GetGattDb(empty_proto.Empty())
331
332    async def register_server(self):
333        """
334        """
335        await self.__gatt_stub.RegisterServer(empty_proto.Empty())
336
337    async def unregister_server(self):
338        """
339        """
340        await self.__gatt_stub.UnregisterServer(empty_proto.Empty())
341
342    async def connect(self):
343        """
344        """
345        await self.__gatt_stub.Connect(empty_proto.Empty())
346
347    async def disconnect(self):
348        """
349        """
350        await self.__gatt_stub.Disconnect(empty_proto.Empty())
351
352    async def add_service(self):
353        """
354        """
355        await self.__gatt_stub.AddService(empty_proto.Empty())
356
357    async def stop_service(self):
358        """
359        """
360        await self.__gatt_stub.StopService(empty_proto.Empty())
361
362    async def delete_service(self):
363        """
364        """
365        await self.__gatt_stub.DeleteService(empty_proto.Empty())
366
367    async def send_indication(self):
368        """
369        """
370        await self.__gatt_stub.SendIndication(empty_proto.Empty())
371
372    async def send_response(self):
373        """
374        """
375        await self.__gatt_stub.SendResponse(empty_proto.Empty())
376
377    async def set_preferred_phy(self):
378        """
379        """
380        await self.__gatt_stub.SetPreferredPhy(empty_proto.Empty())
381
382    async def read_phy(self):
383        """
384        """
385        await self.__gatt_stub.ReadPhy(empty_proto.Empty())
386