blob: 025bd7a35c03a98d23e490c037a7b2cea67ba4cb [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 }
157 }
158
159 public BluetoothMasObexClientSession(ObexTransport transport, Handler handler) {
160 mTransport = transport;
161 mSessionHandler = handler;
162 }
163
164 public void start() {
165 if (mClientThread == null) {
166 mClientThread = new ClientThread(mTransport);
167 mClientThread.start();
168 }
169
170 }
171
172 public void stop() {
173 if (mClientThread != null) {
174 mClientThread.interrupt();
175
176 (new Thread() {
177 @Override
178 public void run() {
179 try {
180 mClientThread.join();
181 mClientThread = null;
182 } catch (InterruptedException e) {
183 Log.w(TAG, "Interrupted while waiting for thread to join");
184 }
185 }
186 }).run();
187 }
188 }
189
190 public boolean makeRequest(BluetoothMasRequest request) {
191 if (mClientThread == null) {
192 return false;
193 }
194
Nitin Shivpuree5b6f5c2014-11-21 18:44:35 +0530195 if (((BluetoothMapTransport)mTransport).isSrmSupported()) {
196 Log.d(TAG, "Client is srm capable");
197 if (request instanceof BluetoothMasRequestGetFolderListing ||
198 request instanceof BluetoothMasRequestGetFolderListingSize ||
199 request instanceof BluetoothMasRequestGetMessagesListing ||
200 request instanceof BluetoothMasRequestGetMessage ||
201 request instanceof BluetoothMasRequestPushMessage ||
202 request instanceof BluetoothMasRequestGetMessagesListingSize) {
203 mClientThread.mSession.setLocalSrmStatus(true);
204 }
205
206 if (request instanceof BluetoothMasRequestSetMessageStatus ||
207 request instanceof BluetoothMasRequestUpdateInbox||
208 request instanceof BluetoothMasRequestSetNotificationRegistration) {
209 mClientThread.mSession.setLocalSrmStatus(false);
210 }
211 } else {
212 Log.d(TAG, "Client is not srm capable");
213 }
214
Hemant Gupta192d7932014-04-08 16:04:13 +0530215 return mClientThread.schedule(request);
216 }
217}