summaryrefslogtreecommitdiffstats
path: root/src/com/android/bluetooth/map/BluetoothMnsObexSession.java
blob: 6d3c663eead06793096514940301093acd926ec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *        * Redistributions of source code must retain the above copyright
 *          notice, this list of conditions and the following disclaimer.
 *        * Redistributions in binary form must reproduce the above copyright
 *          notice, this list of conditions and the following disclaimer in the
 *          documentation and/or other materials provided with the distribution.
 *        * Neither the name of Code Aurora nor
 *          the names of its contributors may be used to endorse or promote
 *          products derived from this software without specific prior written
 *          permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT ARE DISCLAIMED.    IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.bluetooth.map;

import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.Process;
import android.util.Log;

import javax.obex.*;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.File;

import org.apache.http.Header;
/**
 * This class runs as an OBEX client
 */

// TBD - Do applications need to do anything for power management?

public class BluetoothMnsObexSession {

    private static final String TAG = "BtMns ObexClient";
    private static final boolean D = BluetoothMasService.DEBUG;
    private static final boolean V = BluetoothMasService.VERBOSE;

    public final static int MSG_SESSION_ERROR = 1;
    public final static int MSG_CONNECT_TIMEOUT = 2;

    private ObexTransport mTransport;

    private Context mContext;


    private volatile boolean mWaitingForRemote;

    private Handler mCallback;

    private static final String TYPE_EVENT = "x-bt/MAP-event-report";

    public BluetoothMnsObexSession(Context context, ObexTransport transport) {
        if (transport == null) {
            throw new NullPointerException("transport is null");
        }
        mContext = context;
        mTransport = transport;
    }


    private ClientSession mCs;


    private boolean mConnected = false;

    public void disconnect() {
        try {
            if (mCs != null) {
                mCs.disconnect(null);
            }
                mCs = null;
                if (D) Log.d(TAG, "OBEX session disconnected");
            } catch (IOException e) {
                Log.w(TAG, "OBEX session disconnect error" + e);
            }
            try {
                if (mCs != null) {
                    if (D) Log.d(TAG, "OBEX session close mCs");
                    mCs.close();
                    if (D) Log.d(TAG, "OBEX session closed");
                }
            } catch (IOException e) {
                Log.w(TAG, "OBEX session close error" + e);
        }
        if (mTransport != null) {
            try {
                    mTransport.close();
            } catch (IOException e) {
                    Log.e(TAG, "mTransport.close error");
            }

        }
    }

    private HeaderSet hsConnect = null;

    public void connect() {
        Log.d(TAG, "Create ClientSession with transport " + mTransport.toString());
        try {
            mCs = new ClientSession(mTransport);
            mConnected = true;
        } catch (IOException e1) {
            Log.e(TAG, "OBEX session create error");
        }
        if (mConnected) {
            mConnected = false;
            HeaderSet hs = new HeaderSet();
            // bb582b41-420c-11db-b0de-0800200c9a66
            byte[] mnsTarget = { (byte) 0xbb, (byte) 0x58, (byte) 0x2b, (byte) 0x41,
                                 (byte) 0x42, (byte) 0x0c, (byte) 0x11, (byte) 0xdb,
                                 (byte) 0xb0, (byte) 0xde, (byte) 0x08, (byte) 0x00,
                                 (byte) 0x20, (byte) 0x0c, (byte) 0x9a, (byte) 0x66 };
            hs.setHeader(HeaderSet.TARGET, mnsTarget);

            synchronized (this) {
                mWaitingForRemote = true;
            }
            try {
                hsConnect = mCs.connect(hs);
                if (D) Log.d(TAG, "OBEX session created");
                mConnected = true;
            } catch (IOException e) {
                Log.e(TAG, "OBEX session connect error");
            }
        }
            synchronized (this) {
                mWaitingForRemote = false;
        }
    }

    public int sendEvent(File file, byte masInstanceId) {

        boolean error = false;
        int responseCode = -1;
        HeaderSet request;
        byte[] val = new byte[1];
        val[0] =  masInstanceId;
        request = new HeaderSet();
        ApplicationParameter ap = new ApplicationParameter();
        ap.addAPPHeader((byte)BluetoothMasSpecParams.MAS_TAG_MAS_INSTANCE_ID,
                        (byte)BluetoothMasSpecParams.MAS_TAG_MAS_INSTANCE_ID_LEN,
                         val);
        request.setHeader(HeaderSet.TYPE, TYPE_EVENT);
        request.setHeader(HeaderSet.APPLICATION_PARAMETER, ap.getAPPparam());

        request.mConnectionID = new byte[4];
        System.arraycopy(hsConnect.mConnectionID, 0, request.mConnectionID, 0, 4);

        ClientOperation putOperation = null;
        OutputStream outputStream = null;
        InputStream inputStream = null;
        try {
            synchronized (this) {
            mWaitingForRemote = true;
            }
            // Send the header first and then the body
            try {
                if (V) Log.v(TAG, "Send headerset Event ");
                putOperation = (ClientOperation)mCs.put(request);
                // TODO - Should this be kept or Removed

             } catch (IOException e) {
                 Log.e(TAG, "Error when put HeaderSet ");
                 error = true;
             }
             synchronized (this) {
                 mWaitingForRemote = false;
             }
             if (!error) {
                 try {
                     if (V) Log.v(TAG, "Send headerset Event ");
                         outputStream = putOperation.openOutputStream();
                         inputStream = putOperation.openInputStream();
                     } catch (IOException e) {
                     Log.e(TAG, "Error when opening OutputStream");
                     error = true;
                 }
             }

             if (!error) {
                 int position = 0;
                 int readLength = 0;
                 boolean okToProceed = true;
                 long timestamp = 0;
                 int outputBufferSize = putOperation.getMaxPacketSize();
                 byte[] buffer = new byte[outputBufferSize];


                FileInputStream fileInputStream = new FileInputStream(file);
                BufferedInputStream a = new BufferedInputStream(fileInputStream, 0x4000);

                while (okToProceed && (position != file.length())) {
                    if (V) timestamp = System.currentTimeMillis();

                    readLength = a.read(buffer, 0, outputBufferSize);
                    outputStream.write(buffer, 0, readLength);

                    /* check remote abort */
                    responseCode = putOperation.getResponseCode();
                    responseCode = ResponseCodes.OBEX_HTTP_OK;

                    if (V) Log.v(TAG, "Response code is " + responseCode);
                    if (responseCode != ResponseCodes.OBEX_HTTP_CONTINUE
                        && responseCode != ResponseCodes.OBEX_HTTP_OK) {
                     /* abort happens */
                        okToProceed = false;
                    } else {
                        position += readLength;
                        if (V) {
                            Log.v(TAG, "Sending file position = " + position
                                    + " readLength " + readLength + " bytes took "
                                    + (System.currentTimeMillis() - timestamp) + " ms");
                        }
                    }
                }
                if (position == file.length()) {
                 Log.i(TAG, "SendFile finished send out file " + file.length()
                            + " length " + file.length());
                outputStream.close();
                } else {
                    error = true;
                    // TBD - Is Output stream close needed here
                    putOperation.abort();
                    Log.i(TAG, "SendFile interrupted when send out file "
                            + " at " + position + " of " + file.length());
                }
            }
        } catch (IOException e) {
            handleSendException(e.toString());
        } catch (NullPointerException e) {
            handleSendException(e.toString());
        } catch (IndexOutOfBoundsException e) {
            handleSendException(e.toString());
        } finally {
            try {
                if (!error) {
                    responseCode = putOperation.getResponseCode();
                    if (responseCode != -1) {
                        if (V) Log.v(TAG, "Get response code " + responseCode);
                        if (responseCode != ResponseCodes.OBEX_HTTP_OK) {
                            Log.i(TAG, "Response error code is " + responseCode);
                        }
                    }
                }
                if (inputStream != null) {
                    inputStream.close();
                }
                if (putOperation != null) {
                   putOperation.close();
                }
            } catch (IOException e) {
                Log.e(TAG, "Error when closing stream after send");
            }
        }

        return responseCode;
    }

    private void handleSendException(String exception) {
        Log.e(TAG, "Error when sending event: " + exception);
    }
}