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.BluetoothDevice 20 import android.bluetooth.BluetoothManager 21 import android.content.Context 22 import android.provider.Telephony.* 23 import android.telephony.TelephonyManager 24 import android.util.Log 25 import com.google.protobuf.Empty 26 import io.grpc.stub.StreamObserver 27 import kotlinx.coroutines.CoroutineScope 28 import kotlinx.coroutines.Dispatchers 29 import pandora.OsGrpc.OsImplBase 30 import pandora.OsProto.* 31 32 private const val TAG = "PandoraOs" 33 34 @kotlinx.coroutines.ExperimentalCoroutinesApi 35 class Os(val context: Context) : OsImplBase() { 36 37 private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default.limitedParallelism(1)) 38 39 private val bluetoothManager = context.getSystemService(BluetoothManager::class.java)!! 40 private val bluetoothAdapter = bluetoothManager.adapter 41 private var telephonyManager = context.getSystemService(TelephonyManager::class.java)!! 42 private val DEFAULT_MESSAGE_LEN = 130 43 lognull44 override fun log(request: LogRequest, responseObserver: StreamObserver<LogResponse>) { 45 grpcUnary(scope, responseObserver) { 46 Log.i(TAG, request.text) 47 LogResponse.getDefaultInstance() 48 } 49 } 50 setAccessPermissionnull51 override fun setAccessPermission( 52 request: SetAccessPermissionRequest, 53 responseObserver: StreamObserver<Empty> 54 ) { 55 grpcUnary<Empty>(scope, responseObserver) { 56 val bluetoothDevice = request.address.toBluetoothDevice(bluetoothAdapter) 57 when (request.accessType!!) { 58 AccessType.ACCESS_MESSAGE -> 59 bluetoothDevice.setMessageAccessPermission(BluetoothDevice.ACCESS_ALLOWED) 60 AccessType.ACCESS_PHONEBOOK -> 61 bluetoothDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED) 62 AccessType.ACCESS_SIM -> 63 bluetoothDevice.setSimAccessPermission(BluetoothDevice.ACCESS_ALLOWED) 64 else -> {} 65 } 66 Empty.getDefaultInstance() 67 } 68 } 69 sendPingnull70 override fun sendPing(request: SendPingRequest, responseObserver: StreamObserver<Empty>) { 71 grpcUnary<Empty>(scope, responseObserver) { 72 val pingStatus = 73 Runtime.getRuntime().exec("ping -I bt-pan -c 1 ${request.ipAddress}").waitFor() 74 Empty.getDefaultInstance() 75 } 76 } 77 } 78