• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.pbapclient;
18 
19 import android.util.Log;
20 
21 import java.io.IOException;
22 import java.io.InputStream;
23 
24 import javax.obex.ClientOperation;
25 import javax.obex.ClientSession;
26 import javax.obex.HeaderSet;
27 import javax.obex.ResponseCodes;
28 
29 abstract class BluetoothPbapRequest {
30 
31     private static final String TAG = "BluetoothPbapRequest";
32     private static final boolean DBG = Utils.DBG;
33 
34     protected static final byte OAP_TAGID_ORDER = 0x01;
35     protected static final byte OAP_TAGID_SEARCH_VALUE = 0x02;
36     protected static final byte OAP_TAGID_SEARCH_ATTRIBUTE = 0x03;
37     protected static final byte OAP_TAGID_MAX_LIST_COUNT = 0x04;
38     protected static final byte OAP_TAGID_LIST_START_OFFSET = 0x05;
39     protected static final byte OAP_TAGID_FILTER = 0x06;
40     protected static final byte OAP_TAGID_FORMAT = 0x07;
41     protected static final byte OAP_TAGID_PHONEBOOK_SIZE = 0x08;
42     protected static final byte OAP_TAGID_NEW_MISSED_CALLS = 0x09;
43     protected static final byte OAP_TAGID_PBAP_SUPPORTED_FEATURES = 0x10;
44 
45     protected HeaderSet mHeaderSet;
46 
47     protected int mResponseCode;
48 
49     private boolean mAborted = false;
50 
51     private ClientOperation mOp = null;
52 
BluetoothPbapRequest()53     BluetoothPbapRequest() {
54         mHeaderSet = new HeaderSet();
55     }
56 
isSuccess()57     public final boolean isSuccess() {
58         return (mResponseCode == ResponseCodes.OBEX_HTTP_OK);
59     }
60 
execute(ClientSession session)61     public void execute(ClientSession session) throws IOException {
62         if (DBG) Log.v(TAG, "execute");
63 
64         /* in case request is aborted before can be executed */
65         if (mAborted) {
66             mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
67             return;
68         }
69 
70         try {
71             mOp = (ClientOperation) session.get(mHeaderSet);
72 
73             /* make sure final flag for GET is used (PBAP spec 6.2.2) */
74             mOp.setGetFinalFlag(true);
75 
76             /*
77              * this will trigger ClientOperation to use non-buffered stream so
78              * we can abort operation
79              */
80             mOp.continueOperation(true, false);
81 
82             readResponseHeaders(mOp.getReceivedHeader());
83 
84             InputStream is = mOp.openInputStream();
85             readResponse(is);
86             is.close();
87 
88             mOp.close();
89 
90             mResponseCode = mOp.getResponseCode();
91 
92             if (DBG) Log.d(TAG, "mResponseCode=" + mResponseCode);
93 
94             checkResponseCode(mResponseCode);
95         } catch (IOException e) {
96             Log.e(TAG, "IOException occured when processing request", e);
97             mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
98 
99             throw e;
100         }
101     }
102 
abort()103     public void abort() {
104         mAborted = true;
105 
106         if (mOp != null) {
107             try {
108                 mOp.abort();
109             } catch (IOException e) {
110                 Log.e(TAG, "Exception occured when trying to abort", e);
111             }
112         }
113     }
114 
readResponse(InputStream stream)115     protected void readResponse(InputStream stream) throws IOException {
116         if (DBG) Log.v(TAG, "readResponse");
117 
118         /* nothing here by default */
119     }
120 
readResponseHeaders(HeaderSet headerset)121     protected void readResponseHeaders(HeaderSet headerset) {
122         if (DBG) Log.v(TAG, "readResponseHeaders");
123 
124         /* nothing here by dafault */
125     }
126 
checkResponseCode(int responseCode)127     protected void checkResponseCode(int responseCode) throws IOException {
128         if (DBG) Log.v(TAG, "checkResponseCode");
129 
130         /* nothing here by dafault */
131     }
132 }
133