1 /*
2  * Copyright (c) 2014 The Android Open Source Project
3  * Copyright (C) 2012 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef COM_ANDROID_BLUETOOTH_H
19 #define COM_ANDROID_BLUETOOTH_H
20 
21 #include "hardware/bluetooth.h"
22 #include "hardware/hardware.h"
23 #include "jni.h"
24 #include "jni_logging.h"
25 #include "nativehelper/ScopedLocalRef.h"
26 #include "utils/Log.h"
27 
28 namespace android {
29 
30 JNIEnv* getCallbackEnv();
31 bool isCallbackThread();
32 
33 class CallbackEnv {
34 public:
CallbackEnv(const char * methodName)35     CallbackEnv(const char *methodName) : mName(methodName) {
36         mCallbackEnv = getCallbackEnv();
37     }
38 
~CallbackEnv()39     ~CallbackEnv() {
40       if (mCallbackEnv && mCallbackEnv->ExceptionCheck()) {
41           ALOGE("An exception was thrown by callback '%s'.", mName);
42           LOGE_EX(mCallbackEnv);
43           mCallbackEnv->ExceptionClear();
44       }
45     }
46 
valid()47     bool valid() const {
48       if (!mCallbackEnv || !isCallbackThread()) {
49         ALOGE("%s: Callback env fail", mName);
50         return false;
51       }
52       return true;
53     }
54 
55     // stolen from art/runtime/jni/check_jni.cc
isValidUtf(const char * bytes)56     bool isValidUtf(const char* bytes) const {
57       while (*bytes != '\0') {
58         const uint8_t* utf8 = reinterpret_cast<const uint8_t*>(bytes++);
59         // Switch on the high four bits.
60         switch (*utf8 >> 4) {
61           case 0x00:
62           case 0x01:
63           case 0x02:
64           case 0x03:
65           case 0x04:
66           case 0x05:
67           case 0x06:
68           case 0x07:
69             // Bit pattern 0xxx. No need for any extra bytes.
70             break;
71           case 0x08:
72           case 0x09:
73           case 0x0a:
74           case 0x0b:
75             // Bit patterns 10xx, which are illegal start bytes.
76             return false;
77           case 0x0f:
78             // Bit pattern 1111, which might be the start of a 4 byte sequence.
79             if ((*utf8 & 0x08) == 0) {
80               // Bit pattern 1111 0xxx, which is the start of a 4 byte sequence.
81               // We consume one continuation byte here, and fall through to
82               // consume two more.
83               utf8 = reinterpret_cast<const uint8_t*>(bytes++);
84               if ((*utf8 & 0xc0) != 0x80) {
85                 return false;
86               }
87             } else {
88               return false;
89             }
90             // Fall through to the cases below to consume two more continuation
91             // bytes.
92             FALLTHROUGH_INTENDED;
93           case 0x0e:
94             // Bit pattern 1110, so there are two additional bytes.
95             utf8 = reinterpret_cast<const uint8_t*>(bytes++);
96             if ((*utf8 & 0xc0) != 0x80) {
97               return false;
98             }
99             // Fall through to consume one more continuation byte.
100             FALLTHROUGH_INTENDED;
101           case 0x0c:
102           case 0x0d:
103             // Bit pattern 110x, so there is one additional byte.
104             utf8 = reinterpret_cast<const uint8_t*>(bytes++);
105             if ((*utf8 & 0xc0) != 0x80) {
106               return false;
107             }
108             break;
109         }
110       }
111       return true;
112     }
113 
114     JNIEnv *operator-> () const {
115         return mCallbackEnv;
116     }
117 
get()118     JNIEnv *get() const {
119         return mCallbackEnv;
120     }
121 
122 private:
123     JNIEnv *mCallbackEnv;
124     const char *mName;
125 
126     DISALLOW_COPY_AND_ASSIGN(CallbackEnv);
127 };
128 
129 const bt_interface_t* getBluetoothInterface();
130 
131 int register_com_android_bluetooth_hfp(JNIEnv* env);
132 
133 int register_com_android_bluetooth_hfpclient(JNIEnv* env);
134 
135 int register_com_android_bluetooth_a2dp(JNIEnv* env);
136 
137 int register_com_android_bluetooth_a2dp_sink(JNIEnv* env);
138 
139 int register_com_android_bluetooth_avrcp(JNIEnv* env);
140 
141 int register_com_android_bluetooth_avrcp_target(JNIEnv* env);
142 
143 int register_com_android_bluetooth_avrcp_controller(JNIEnv* env);
144 
145 int register_com_android_bluetooth_hid_host(JNIEnv* env);
146 
147 int register_com_android_bluetooth_hid_device(JNIEnv* env);
148 
149 int register_com_android_bluetooth_pan(JNIEnv* env);
150 
151 int register_com_android_bluetooth_gatt (JNIEnv* env);
152 
153 int register_com_android_bluetooth_sdp (JNIEnv* env);
154 
155 int register_com_android_bluetooth_hearing_aid(JNIEnv* env);
156 
157 int register_com_android_bluetooth_btservice_BluetoothKeystore(JNIEnv* env);
158 }
159 
160 #endif /* COM_ANDROID_BLUETOOTH_H */
161