blob: 4aa0cf4e880ccd656ff4dc3e40e6a9a17732b920 [file] [log] [blame]
Hemant Gupta192d7932014-04-08 16:04:13 +05301/*
2 * Copyright (C) 2014 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
17package android.bluetooth.client.map;
18
19import android.os.Handler;
20import android.os.Process;
21import android.util.Log;
22
23import java.io.IOException;
24
25import javax.obex.ClientSession;
26import javax.obex.HeaderSet;
27import javax.obex.ObexTransport;
Nitin Shivpuree5b6f5c2014-11-21 18:44:35 +053028import javax.obex.ObexHelper;
Hemant Gupta192d7932014-04-08 16:04:13 +053029import javax.obex.ResponseCodes;
30
31class BluetoothMasObexClientSession {
32 private static final String TAG = "BluetoothMasObexClientSession";
33
34 private static final byte[] MAS_TARGET = new byte[] {
35 (byte) 0xbb, 0x58, 0x2b, 0x40, 0x42, 0x0c, 0x11, (byte) 0xdb, (byte) 0xb0, (byte) 0xde,
36 0x08, 0x00, 0x20, 0x0c, (byte) 0x9a, 0x66
37 };
38
39 static final int MSG_OBEX_CONNECTED = 100;
40 static final int MSG_OBEX_DISCONNECTED = 101;
41 static final int MSG_REQUEST_COMPLETED = 102;
42
43 private final ObexTransport mTransport;
44
45 private final Handler mSessionHandler;
46
47 private ClientThread mClientThread;
48
49 private volatile boolean mInterrupted;
50
51 private class ClientThread extends Thread {
52 private final ObexTransport mTransport;
53
54 private ClientSession mSession;
55
56 private BluetoothMasRequest mRequest;
57
58 private boolean mConnected;
59
60 public ClientThread(ObexTransport transport) {
61 super("MAS ClientThread");
62
63 mTransport = transport;
64 mConnected = false;
65 }
66
67 @Override
68 public void run() {
69 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
70
71 connect();
72
73 if (mConnected) {
74 mSessionHandler.obtainMessage(MSG_OBEX_CONNECTED).sendToTarget();
75 } else {
76 mSessionHandler.obtainMessage(MSG_OBEX_DISCONNECTED).sendToTarget();
77 return;
78 }
79
80 while (!mInterrupted) {
81 synchronized (this) {
82 if (mRequest == null) {
83 try {
84 this.wait();
85 } catch (InterruptedException e) {
86 mInterrupted = true;
87 }
88 }
89 }
90
91 if (!mInterrupted && mRequest != null) {
92 try {
93 mRequest.execute(mSession);
94 } catch (IOException e) {
95 // this will "disconnect" to cleanup
96 mInterrupted = true;
97 }
98
99 BluetoothMasRequest oldReq = mRequest;
100 mRequest = null;
101
102 mSessionHandler.obtainMessage(MSG_REQUEST_COMPLETED, oldReq).sendToTarget();
103 }
104 }
105
106 disconnect();
107
108 mSessionHandler.obtainMessage(MSG_OBEX_DISCONNECTED).sendToTarget();
109 }
110
111 private void connect() {
112 try {
Nitin Shivpuree5b6f5c2014-11-21 18:44:35 +0530113 Log.w(TAG, "connect:");
Hemant Gupta192d7932014-04-08 16:04:13 +0530114 mSession = new ClientSession(mTransport);
115
116 HeaderSet headerset = new HeaderSet();
117 headerset.setHeader(HeaderSet.TARGET, MAS_TARGET);
118
119 headerset = mSession.connect(headerset);
120
121 if (headerset.getResponseCode() == ResponseCodes.OBEX_HTTP_OK) {
122 mConnected = true;
123 } else {
124 disconnect();
125 }
126 } catch (IOException e) {
Nitin Shivpuree5b6f5c2014-11-21 18:44:35 +0530127 Log.w(TAG, "handled connect exception: ", e);
Hemant Gupta192d7932014-04-08 16:04:13 +0530128 }
129 }
130
131 private void disconnect() {
Nitin Shivpuree5b6f5c2014-11-21 18:44:35 +0530132 Log.w(TAG, "disconnect: ");
Hemant Gupta192d7932014-04-08 16:04:13 +0530133 try {
134 mSession.disconnect(null);
135 } catch (IOException e) {
136 }
137
138 try {
139 mSession.close();
140 } catch (IOException e) {
Nitin Shivpuree5b6f5c2014-11-21 18:44:35 +0530141 Log.w(TAG, "handled disconnect exception:", e);
Hemant Gupta192d7932014-04-08 16:04:13 +0530142 }
143
144 mConnected = false;
145 }
146
147 public synchronized boolean schedule(BluetoothMasRequest request) {
148 if (mRequest != null) {
149 return false;
150 }
151
152 mRequest = request;
153 notify();
154
155 return true;
156 }
Nitin Shivpure614d2e02015-09-28 12:43:46 +0530157
158 private void shutdown() {
159 Log.w(TAG, "shutdown ");
160 mInterrupted = true;
161 interrupt();
162 }
Hemant Gupta192d7932014-04-08 16:04:13 +0530163 }
164
165 public BluetoothMasObexClientSession(ObexTransport transport, Handler handler) {
166 mTransport = transport;
167 mSessionHandler = handler;
168 }
169
170 public void start() {
171 if (mClientThread == null) {
172 mClientThread = new ClientThread(mTransport);
173 mClientThread.start();
174 }
175
176 }
177
178 public void stop() {
179 if (mClientThread != null) {
Nitin Shivpure614d2e02015-09-28 12:43:46 +0530180 mClientThread.shutdown();
Hemant Gupta192d7932014-04-08 16:04:13 +0530181
Nitin Shivpureee991e92015-11-03 17:29:41 +0530182 Thread t = new Thread(new Runnable() {
183 public void run () {
184 Log.d(TAG, "Spawning a new thread for stopping obex session");
Hemant Gupta192d7932014-04-08 16:04:13 +0530185 try {
186 mClientThread.join();
187 mClientThread = null;
188 } catch (InterruptedException e) {
189 Log.w(TAG, "Interrupted while waiting for thread to join");
190 }
191 }
Nitin Shivpureee991e92015-11-03 17:29:41 +0530192 });
193 t.start();
194 Log.d(TAG, "Exiting from the stopping thread");
Hemant Gupta192d7932014-04-08 16:04:13 +0530195 }
196 }
197
198 public boolean makeRequest(BluetoothMasRequest request) {
199 if (mClientThread == null) {
200 return false;
201 }
202
Nitin Shivpuree5b6f5c2014-11-21 18:44:35 +0530203 if (((BluetoothMapTransport)mTransport).isSrmSupported()) {
204 Log.d(TAG, "Client is srm capable");
205 if (request instanceof BluetoothMasRequestGetFolderListing ||
206 request instanceof BluetoothMasRequestGetFolderListingSize ||
207 request instanceof BluetoothMasRequestGetMessagesListing ||
208 request instanceof BluetoothMasRequestGetMessage ||
209 request instanceof BluetoothMasRequestPushMessage ||
210 request instanceof BluetoothMasRequestGetMessagesListingSize) {
211 mClientThread.mSession.setLocalSrmStatus(true);
212 }
213
214 if (request instanceof BluetoothMasRequestSetMessageStatus ||
215 request instanceof BluetoothMasRequestUpdateInbox||
216 request instanceof BluetoothMasRequestSetNotificationRegistration) {
217 mClientThread.mSession.setLocalSrmStatus(false);
218 }
219 } else {
220 Log.d(TAG, "Client is not srm capable");
221 }
222
Hemant Gupta192d7932014-04-08 16:04:13 +0530223 return mClientThread.schedule(request);
224 }
225}