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 
MnsObexServer(MceStateMachine stateMachine, ObexServerSockets socketOriginator)61     MnsObexServer(MceStateMachine stateMachine, ObexServerSockets socketOriginator) {
62         super();
63         mStateMachineReference = new WeakReference<>(stateMachine);
64         mObexServerSockets = socketOriginator;
65     }
66 
67     @Override
onConnect(final HeaderSet request, HeaderSet reply)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
onDisconnect(final HeaderSet request, HeaderSet reply)89     public void onDisconnect(final HeaderSet request, HeaderSet reply) {
90         if (VDBG) {
91             Log.v(TAG, "onDisconnect");
92         }
93         MceStateMachine currentStateMachine = mStateMachineReference.get();
94         if (currentStateMachine != null) {
95             currentStateMachine.disconnect();
96         }
97     }
98 
99     @Override
onGet(final Operation op)100     public int onGet(final Operation op) {
101         if (VDBG) {
102             Log.v(TAG, "onGet");
103         }
104         return ResponseCodes.OBEX_HTTP_BAD_REQUEST;
105     }
106 
107     @Override
onPut(final Operation op)108     public int onPut(final Operation op) {
109         if (VDBG) {
110             Log.v(TAG, "onPut");
111         }
112 
113         try {
114             HeaderSet headerset;
115             headerset = op.getReceivedHeader();
116 
117             String type = (String) headerset.getHeader(HeaderSet.TYPE);
118             ObexAppParameters oap = ObexAppParameters.fromHeaderSet(headerset);
119             if (!TYPE.equals(type) || !oap.exists(Request.OAP_TAGID_MAS_INSTANCE_ID)) {
120                 return ResponseCodes.OBEX_HTTP_BAD_REQUEST;
121             }
122 
123             Byte inst = oap.getByte(Request.OAP_TAGID_MAS_INSTANCE_ID);
124             EventReport ev = EventReport.fromStream(op.openDataInputStream());
125             op.close();
126 
127             MceStateMachine currentStateMachine = mStateMachineReference.get();
128             if (currentStateMachine != null) {
129                 currentStateMachine.receiveEvent(ev);
130             }
131         } catch (IOException e) {
132             Log.e(TAG, "I/O exception when handling PUT request", e);
133             return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
134         }
135         return ResponseCodes.OBEX_HTTP_OK;
136     }
137 
138     @Override
onAbort(final HeaderSet request, HeaderSet reply)139     public int onAbort(final HeaderSet request, HeaderSet reply) {
140         if (VDBG) {
141             Log.v(TAG, "onAbort");
142         }
143         return ResponseCodes.OBEX_HTTP_NOT_IMPLEMENTED;
144     }
145 
146     @Override
onSetPath(final HeaderSet request, HeaderSet reply, final boolean backup, final boolean create)147     public int onSetPath(final HeaderSet request, HeaderSet reply, final boolean backup,
148             final boolean create) {
149         if (VDBG) {
150             Log.v(TAG, "onSetPath");
151         }
152         return ResponseCodes.OBEX_HTTP_BAD_REQUEST;
153     }
154 
155     @Override
onClose()156     public void onClose() {
157         if (VDBG) {
158             Log.v(TAG, "onClose");
159         }
160     }
161 }
162