1 # Copyright 2023 Google LLC
2 #
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 #     https://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 """Floss bluetooth test server."""
15 
16 import asyncio
17 import logging
18 
19 from floss.pandora.server import a2dp
20 from floss.pandora.server import bluetooth as bluetooth_module
21 from floss.pandora.server import gatt
22 from floss.pandora.server import hfp
23 from floss.pandora.server import hid
24 from floss.pandora.server import host
25 from floss.pandora.server import l2cap
26 from floss.pandora.server import modem
27 from floss.pandora.server import rfcomm
28 from floss.pandora.server import security
29 import grpc
30 from pandora import a2dp_grpc_aio
31 from pandora import host_grpc_aio
32 from pandora import security_grpc_aio
33 from pandora_experimental import gatt_grpc_aio
34 from pandora_experimental import hfp_grpc_aio
35 from pandora_experimental import hid_grpc_aio
36 from pandora_experimental import l2cap_grpc_aio
37 from pandora_experimental import modem_grpc_aio
38 from pandora_experimental import rfcomm_grpc_aio
39 
40 
41 async def serve(port):
42     """Start serving the Floss bluetooth test server."""
43 
44     logging.basicConfig(filename='/var/log/grpc_server_log', filemode='a', level=logging.DEBUG)
45 
46     try:
47         while True:
48             bluetooth = bluetooth_module.Bluetooth()
49             bluetooth.reset()
50             logging.info('bluetooth initialized')
51 
52             server = grpc.aio.server()
53             security_service = security.SecurityService(bluetooth)
54             security_grpc_aio.add_SecurityServicer_to_server(security_service, server)
55 
56             host_service = host.HostService(server, bluetooth, security_service)
57             host_grpc_aio.add_HostServicer_to_server(host_service, server)
58 
59             security_storage_service = security.SecurityStorageService(bluetooth)
60             security_grpc_aio.add_SecurityStorageServicer_to_server(security_storage_service, server)
61 
62             gatt_service = gatt.GATTService(bluetooth)
63             gatt_grpc_aio.add_GATTServicer_to_server(gatt_service, server)
64 
65             modem_service = modem.Modem(bluetooth)
66             modem_grpc_aio.add_ModemServicer_to_server(modem_service, server)
67 
68             hfp_service = hfp.HFPService(bluetooth)
69             hfp_grpc_aio.add_HFPServicer_to_server(hfp_service, server)
70 
71             hid_service = hid.HIDService(bluetooth)
72             hid_grpc_aio.add_HIDServicer_to_server(hid_service, server)
73 
74             l2cap_service = l2cap.L2CAPService(bluetooth)
75             l2cap_grpc_aio.add_L2CAPServicer_to_server(l2cap_service, server)
76 
77             rfcomm_service = rfcomm.RFCOMMService(bluetooth)
78             rfcomm_grpc_aio.add_RFCOMMServicer_to_server(rfcomm_service, server)
79 
80             a2dp_service = a2dp.A2DPService(bluetooth)
81             a2dp_grpc_aio.add_A2DPServicer_to_server(a2dp_service, server)
82 
83             server.add_insecure_port(f'[::]:{port}')
84 
85             await server.start()
86             logging.info('server started')
87 
88             await server.wait_for_termination()
89             bluetooth.cleanup()
90             del bluetooth
91     finally:
92         await server.stop(None)
93         bluetooth.cleanup()
94         del bluetooth
95 
96 
97 if __name__ == '__main__':
98     asyncio.run(serve(8999))
99