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.map; 18 19import android.os.Handler; 20import android.util.Log; 21 22import android.bluetooth.client.map.utils.ObexAppParameters; 23 24import java.io.IOException; 25import java.util.Arrays; 26 27import javax.obex.HeaderSet; 28import javax.obex.Operation; 29import javax.obex.ResponseCodes; 30import javax.obex.ServerRequestHandler; 31 32class BluetoothMnsObexServer extends ServerRequestHandler { 33 34 private final static String TAG = "BluetoothMnsObexServer"; 35 36 private static final byte[] MNS_TARGET = new byte[] { 37 (byte) 0xbb, 0x58, 0x2b, 0x41, 0x42, 0x0c, 0x11, (byte) 0xdb, (byte) 0xb0, (byte) 0xde, 38 0x08, 0x00, 0x20, 0x0c, (byte) 0x9a, 0x66 39 }; 40 41 private final static String TYPE = "x-bt/MAP-event-report"; 42 43 private final Handler mCallback; 44 45 public BluetoothMnsObexServer(Handler callback) { 46 super(); 47 48 mCallback = callback; 49 } 50 51 @Override 52 public int onConnect(final HeaderSet request, HeaderSet reply) { 53 Log.v(TAG, "onConnect"); 54 55 try { 56 byte[] uuid = (byte[]) request.getHeader(HeaderSet.TARGET); 57 58 if (!Arrays.equals(uuid, MNS_TARGET)) { 59 return ResponseCodes.OBEX_HTTP_NOT_ACCEPTABLE; 60 } 61 62 } catch (IOException e) { 63 // this should never happen since getHeader won't throw exception it 64 // declares to throw 65 return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR; 66 } 67 68 reply.setHeader(HeaderSet.WHO, MNS_TARGET); 69 return ResponseCodes.OBEX_HTTP_OK; 70 } 71 72 @Override 73 public void onDisconnect(final HeaderSet request, HeaderSet reply) { 74 Log.v(TAG, "onDisconnect"); 75 } 76 77 @Override 78 public int onGet(final Operation op) { 79 Log.v(TAG, "onGet"); 80 81 return ResponseCodes.OBEX_HTTP_BAD_REQUEST; 82 } 83 84 @Override 85 public int onPut(final Operation op) { 86 Log.v(TAG, "onPut"); 87 88 try { 89 HeaderSet headerset; 90 headerset = op.getReceivedHeader(); 91 92 String type = (String) headerset.getHeader(HeaderSet.TYPE); 93 ObexAppParameters oap = ObexAppParameters.fromHeaderSet(headerset); 94 95 if (!TYPE.equals(type) || !oap.exists(BluetoothMasRequest.OAP_TAGID_MAS_INSTANCE_ID)) { 96 return ResponseCodes.OBEX_HTTP_BAD_REQUEST; 97 } 98 99 Byte inst = oap.getByte(BluetoothMasRequest.OAP_TAGID_MAS_INSTANCE_ID); 100 101 BluetoothMapEventReport ev = BluetoothMapEventReport.fromStream(op 102 .openDataInputStream()); 103 104 op.close(); 105 106 mCallback.obtainMessage(BluetoothMnsService.MSG_EVENT, inst, 0, ev).sendToTarget(); 107 } catch (IOException e) { 108 Log.e(TAG, "I/O exception when handling PUT request", e); 109 return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR; 110 } 111 112 return ResponseCodes.OBEX_HTTP_OK; 113 } 114 115 @Override 116 public int onAbort(final HeaderSet request, HeaderSet reply) { 117 Log.v(TAG, "onAbort"); 118 119 return ResponseCodes.OBEX_HTTP_NOT_IMPLEMENTED; 120 } 121 122 @Override 123 public int onSetPath(final HeaderSet request, HeaderSet reply, 124 final boolean backup, final boolean create) { 125 Log.v(TAG, "onSetPath"); 126 127 return ResponseCodes.OBEX_HTTP_BAD_REQUEST; 128 } 129 130 @Override 131 public void onClose() { 132 Log.v(TAG, "onClose"); 133 134 // TODO: call session handler so it can disconnect 135 } 136} 137