1 /* 2 * Copyright (C) 2016 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.bluetooth.mapclient; 18 19 import android.bluetooth.BluetoothDevice; 20 import android.bluetooth.BluetoothServerSocket; 21 import android.bluetooth.BluetoothSocket; 22 import android.os.Handler; 23 import android.util.Log; 24 25 import com.android.bluetooth.BluetoothObexTransport; 26 import com.android.bluetooth.IObexConnectionHandler; 27 import com.android.bluetooth.ObexServerSockets; 28 import com.android.bluetooth.sdp.SdpManager; 29 30 import java.io.IOException; 31 32 import javax.obex.ServerSession; 33 34 /** 35 * Message Notification Server implementation 36 */ 37 public class MnsService { 38 static final int MSG_EVENT = 1; 39 /* for Client */ 40 static final int EVENT_REPORT = 1001; 41 private static final String TAG = "MnsService"; 42 private static final Boolean DBG = MapClientService.DBG; 43 private static final Boolean VDBG = MapClientService.VDBG; 44 /* MAP version 1.1 */ 45 private static final int MNS_VERSION = 0x0101; 46 /* these are shared across instances */ 47 private static SocketAcceptor sAcceptThread = null; 48 private static Handler sSessionHandler = null; 49 private static BluetoothServerSocket sServerSocket = null; 50 private static ObexServerSockets sServerSockets = null; 51 52 private static MapClientService sContext; 53 private volatile boolean mShutdown = false; // Used to interrupt socket accept thread 54 private int mSdpHandle = -1; 55 MnsService(MapClientService context)56 MnsService(MapClientService context) { 57 if (VDBG) { 58 Log.v(TAG, "MnsService()"); 59 } 60 sContext = context; 61 sAcceptThread = new SocketAcceptor(); 62 sServerSockets = ObexServerSockets.create(sAcceptThread); 63 SdpManager sdpManager = SdpManager.getDefaultManager(); 64 if (sdpManager == null) { 65 Log.e(TAG, "SdpManager is null"); 66 return; 67 } 68 mSdpHandle = sdpManager.createMapMnsRecord("MAP Message Notification Service", 69 sServerSockets.getRfcommChannel(), -1, MNS_VERSION, 70 MasClient.MAP_SUPPORTED_FEATURES); 71 } 72 stop()73 void stop() { 74 if (VDBG) { 75 Log.v(TAG, "stop()"); 76 } 77 mShutdown = true; 78 cleanUpSdpRecord(); 79 if (sServerSockets != null) { 80 sServerSockets.shutdown(false); 81 sServerSockets = null; 82 } 83 } 84 cleanUpSdpRecord()85 private void cleanUpSdpRecord() { 86 if (mSdpHandle < 0) { 87 Log.e(TAG, "cleanUpSdpRecord, SDP record never created"); 88 return; 89 } 90 int sdpHandle = mSdpHandle; 91 mSdpHandle = -1; 92 SdpManager sdpManager = SdpManager.getDefaultManager(); 93 if (sdpManager == null) { 94 Log.e(TAG, "cleanUpSdpRecord failed, sdpManager is null, sdpHandle=" + sdpHandle); 95 return; 96 } 97 Log.i(TAG, "cleanUpSdpRecord, mSdpHandle=" + sdpHandle); 98 if (!sdpManager.removeSdpRecord(sdpHandle)) { 99 Log.e(TAG, "cleanUpSdpRecord, removeSdpRecord failed, sdpHandle=" + sdpHandle); 100 } 101 } 102 103 private class SocketAcceptor implements IObexConnectionHandler { 104 105 private boolean mInterrupted = false; 106 107 /** 108 * Called when an unrecoverable error occurred in an accept thread. 109 * Close down the server socket, and restart. 110 * TODO: Change to message, to call start in correct context. 111 */ 112 @Override onAcceptFailed()113 public synchronized void onAcceptFailed() { 114 Log.e(TAG, "OnAcceptFailed"); 115 sServerSockets = null; // Will cause a new to be created when calling start. 116 if (mShutdown) { 117 Log.e(TAG, "Failed to accept incomming connection - " + "shutdown"); 118 } 119 } 120 121 @Override onConnect(BluetoothDevice device, BluetoothSocket socket)122 public synchronized boolean onConnect(BluetoothDevice device, BluetoothSocket socket) { 123 if (DBG) { 124 Log.d(TAG, "onConnect" + device + " SOCKET: " + socket); 125 } 126 /* Signal to the service that we have received an incoming connection.*/ 127 MceStateMachine stateMachine = sContext.getMceStateMachineForDevice(device); 128 if (stateMachine == null) { 129 Log.e(TAG, "Error: NO statemachine for device: " + device.getAddress() 130 + " (name: " + device.getName()); 131 return false; 132 } 133 MnsObexServer srv = new MnsObexServer(stateMachine, sServerSockets); 134 BluetoothObexTransport transport = new BluetoothObexTransport(socket); 135 try { 136 new ServerSession(transport, srv, null); 137 return true; 138 } catch (IOException e) { 139 Log.e(TAG, e.toString()); 140 return false; 141 } 142 } 143 } 144 } 145