• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <nativehelper/JNIHelp.h>
22 #include "android_runtime/AndroidRuntime.h"
23 #include "android_runtime/Log.h"
24 #include "hardware/bluetooth.h"
25 #include "hardware/hardware.h"
26 #include "jni.h"
27 #include "nativehelper/ScopedLocalRef.h"
28 #include "utils/Log.h"
29 
30 namespace android {
31 
32 JNIEnv* getCallbackEnv();
33 
34 class CallbackEnv {
35 public:
CallbackEnv(const char * methodName)36     CallbackEnv(const char *methodName) : mName(methodName) {
37         mCallbackEnv = getCallbackEnv();
38     }
39 
~CallbackEnv()40     ~CallbackEnv() {
41       if (mCallbackEnv && mCallbackEnv->ExceptionCheck()) {
42           ALOGE("An exception was thrown by callback '%s'.", mName);
43           LOGE_EX(mCallbackEnv);
44           mCallbackEnv->ExceptionClear();
45       }
46     }
47 
valid()48     bool valid() const {
49       JNIEnv *env = AndroidRuntime::getJNIEnv();
50       if (!mCallbackEnv || (mCallbackEnv != env)) {
51           ALOGE("%s: Callback env fail: env: %p, callback: %p", mName, env, mCallbackEnv);
52           return false;
53       }
54       return true;
55     }
56 
57     // stolen from art/runtime/jni/check_jni.cc
isValidUtf(const char * bytes)58     bool isValidUtf(const char* bytes) const {
59       while (*bytes != '\0') {
60         const uint8_t* utf8 = reinterpret_cast<const uint8_t*>(bytes++);
61         // Switch on the high four bits.
62         switch (*utf8 >> 4) {
63           case 0x00:
64           case 0x01:
65           case 0x02:
66           case 0x03:
67           case 0x04:
68           case 0x05:
69           case 0x06:
70           case 0x07:
71             // Bit pattern 0xxx. No need for any extra bytes.
72             break;
73           case 0x08:
74           case 0x09:
75           case 0x0a:
76           case 0x0b:
77             // Bit patterns 10xx, which are illegal start bytes.
78             return false;
79           case 0x0f:
80             // Bit pattern 1111, which might be the start of a 4 byte sequence.
81             if ((*utf8 & 0x08) == 0) {
82               // Bit pattern 1111 0xxx, which is the start of a 4 byte sequence.
83               // We consume one continuation byte here, and fall through to
84               // consume two more.
85               utf8 = reinterpret_cast<const uint8_t*>(bytes++);
86               if ((*utf8 & 0xc0) != 0x80) {
87                 return false;
88               }
89             } else {
90               return false;
91             }
92             // Fall through to the cases below to consume two more continuation
93             // bytes.
94             FALLTHROUGH_INTENDED;
95           case 0x0e:
96             // Bit pattern 1110, so there are two additional bytes.
97             utf8 = reinterpret_cast<const uint8_t*>(bytes++);
98             if ((*utf8 & 0xc0) != 0x80) {
99               return false;
100             }
101             // Fall through to consume one more continuation byte.
102             FALLTHROUGH_INTENDED;
103           case 0x0c:
104           case 0x0d:
105             // Bit pattern 110x, so there is one additional byte.
106             utf8 = reinterpret_cast<const uint8_t*>(bytes++);
107             if ((*utf8 & 0xc0) != 0x80) {
108               return false;
109             }
110             break;
111         }
112       }
113       return true;
114     }
115 
116     JNIEnv *operator-> () const {
117         return mCallbackEnv;
118     }
119 
get()120     JNIEnv *get() const {
121         return mCallbackEnv;
122     }
123 
124 private:
125     JNIEnv *mCallbackEnv;
126     const char *mName;
127 
128     DISALLOW_COPY_AND_ASSIGN(CallbackEnv);
129 };
130 
131 const bt_interface_t* getBluetoothInterface();
132 
133 int register_com_android_bluetooth_hfp(JNIEnv* env);
134 
135 int register_com_android_bluetooth_hfpclient(JNIEnv* env);
136 
137 int register_com_android_bluetooth_a2dp(JNIEnv* env);
138 
139 int register_com_android_bluetooth_a2dp_sink(JNIEnv* env);
140 
141 int register_com_android_bluetooth_avrcp(JNIEnv* env);
142 
143 int register_com_android_bluetooth_avrcp_target(JNIEnv* env);
144 
145 int register_com_android_bluetooth_avrcp_controller(JNIEnv* env);
146 
147 int register_com_android_bluetooth_hid_host(JNIEnv* env);
148 
149 int register_com_android_bluetooth_hid_device(JNIEnv* env);
150 
151 int register_com_android_bluetooth_pan(JNIEnv* env);
152 
153 int register_com_android_bluetooth_gatt (JNIEnv* env);
154 
155 int register_com_android_bluetooth_sdp (JNIEnv* env);
156 
157 int register_com_android_bluetooth_hearing_aid(JNIEnv* env);
158 }
159 
160 #endif /* COM_ANDROID_BLUETOOTH_H */
161