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