1 /* 2 * Copyright (C) 2022 The Android Open Source Project 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 package com.android.pandora 18 19 import android.bluetooth.BluetoothManager 20 import android.bluetooth.BluetoothProfile 21 import android.content.Context 22 import android.util.Log 23 import io.grpc.BindableService 24 import io.grpc.Server as GrpcServer 25 import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder 26 import java.io.Closeable 27 28 @kotlinx.coroutines.ExperimentalCoroutinesApi 29 class Server(context: Context) { 30 31 private val TAG = "PandoraServer" 32 private val GRPC_PORT = 8999 33 34 private var grpcServer: GrpcServer 35 private var services: List<BindableService> 36 37 init { 38 val bluetoothAdapter = context.getSystemService(BluetoothManager::class.java)!!.adapter 39 40 val security = Security(context) 41 42 // If Bluetooth is turned off, the server will try to get the available profiles, 43 // but they will be null which will cause the server to crash. 44 // Filtering the nullable profiles will allow the user to 45 // perform a factory reset to turn the Bluetooth back on. 46 services = 47 listOf( 48 security, 49 Host(context, security, this), 50 L2cap(context), 51 MediaPlayer(context), 52 Rfcomm(context), 53 SecurityStorage(context), 54 Os(context), 55 ) + 56 mapOf( 57 BluetoothProfile.A2DP to ::A2dp, 58 BluetoothProfile.A2DP_SINK to ::A2dpSink, 59 BluetoothProfile.HEARING_AID to ::Asha, 60 BluetoothProfile.AVRCP to ::Avrcp, 61 BluetoothProfile.GATT to ::Gatt, 62 BluetoothProfile.HEADSET to ::Hfp, 63 BluetoothProfile.HEADSET_CLIENT to ::HfpHandsfree, 64 BluetoothProfile.HID_HOST to ::Hid, 65 BluetoothProfile.PAN to ::Pan, 66 BluetoothProfile.PBAP to ::Pbap, 67 BluetoothProfile.OPP to ::Opp, 68 BluetoothProfile.MAP to ::Map, 69 BluetoothProfile.LE_AUDIO to ::LeAudio, 70 ) <lambda>null71 .filter { bluetoothAdapter.isEnabled } <lambda>null72 .filter { bluetoothAdapter.getSupportedProfiles().contains(it.key) == true } <lambda>null73 .map { it.value(context) } 74 75 val grpcServerBuilder = NettyServerBuilder.forPort(GRPC_PORT) 76 <lambda>null77 services.forEach { grpcServerBuilder.addService(it) } 78 79 grpcServer = grpcServerBuilder.build() 80 81 Log.d(TAG, "Starting Pandora Server") 82 grpcServer.start() 83 Log.d(TAG, "Pandora Server started at $GRPC_PORT") 84 } 85 shutdownnull86 fun shutdown() = grpcServer.shutdown() 87 88 fun awaitTermination() = grpcServer.awaitTermination() 89 90 fun deinit() = services.forEach { if (it is Closeable) it.close() } 91 } 92