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