16c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie/*
2aebc726105204f8a7b977eb3556c14b5ba18a5caHemant Gupta * Copyright (c) 2014 The Android Open Source Project
3ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * Copyright (C) 2012 The Android Open Source Project
4ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu *
5ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * Licensed under the Apache License, Version 2.0 (the "License");
6ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * you may not use this file except in compliance with the License.
7ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * You may obtain a copy of the License at
8ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu *
9ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu *      http://www.apache.org/licenses/LICENSE-2.0
10ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu *
11ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * Unless required by applicable law or agreed to in writing, software
12ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * distributed under the License is distributed on an "AS IS" BASIS,
13ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * See the License for the specific language governing permissions and
15ede67c26e7b2564ea35db6d9b3027a269c150e13Zhihai Xu * limitations under the License.
166c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie */
176c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie
186c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie#ifndef COM_ANDROID_BLUETOOTH_H
196c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie#define COM_ANDROID_BLUETOOTH_H
206c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie
216c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie#include "JNIHelp.h"
22fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen#include "android_runtime/AndroidRuntime.h"
23fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen#include "android_runtime/Log.h"
24479b34835a560d032e211c3c27e10384af69c3b9Marie Janssen#include "hardware/bluetooth.h"
25479b34835a560d032e211c3c27e10384af69c3b9Marie Janssen#include "hardware/hardware.h"
26479b34835a560d032e211c3c27e10384af69c3b9Marie Janssen#include "jni.h"
27479b34835a560d032e211c3c27e10384af69c3b9Marie Janssen#include "nativehelper/ScopedLocalRef.h"
28479b34835a560d032e211c3c27e10384af69c3b9Marie Janssen#include "utils/Log.h"
296c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie
306c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xienamespace android {
316c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie
32fe37710d689b7573ddeb901141db42c28b6cab39Marie JanssenJNIEnv* getCallbackEnv();
336c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie
34fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssenclass CallbackEnv {
35fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssenpublic:
36fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen    CallbackEnv(const char *methodName) : mName(methodName) {
37fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen        mCallbackEnv = getCallbackEnv();
38fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen    }
39fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen
40fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen    ~CallbackEnv() {
41fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen      if (mCallbackEnv && mCallbackEnv->ExceptionCheck()) {
42fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen          ALOGE("An exception was thrown by callback '%s'.", mName);
43fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen          LOGE_EX(mCallbackEnv);
44fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen          mCallbackEnv->ExceptionClear();
45fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen      }
46fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen    }
47fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen
48fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen    bool valid() const {
49fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen      JNIEnv *env = AndroidRuntime::getJNIEnv();
50fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen      if (!mCallbackEnv || (mCallbackEnv != env)) {
51fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen          ALOGE("%s: Callback env fail: env: %p, callback: %p", mName, env, mCallbackEnv);
52fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen          return false;
53fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen      }
54fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen      return true;
55fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen    }
56fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen
57fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen    JNIEnv *operator-> () const {
58fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen        return mCallbackEnv;
59fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen    }
60fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen
61fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen    JNIEnv *get() const {
62fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen        return mCallbackEnv;
63fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen    }
64fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen
652116dbf94fbc00da999ac629c78627983f599a26Andre Eisenbachprivate:
66fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen    JNIEnv *mCallbackEnv;
67fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen    const char *mName;
68fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen
69fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen    DISALLOW_COPY_AND_ASSIGN(CallbackEnv);
70fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssen};
716c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie
72fe37710d689b7573ddeb901141db42c28b6cab39Marie Janssenconst bt_interface_t* getBluetoothInterface();
736c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie
746c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xieint register_com_android_bluetooth_hfp(JNIEnv* env);
756c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie
76aebc726105204f8a7b977eb3556c14b5ba18a5caHemant Guptaint register_com_android_bluetooth_hfpclient(JNIEnv* env);
77aebc726105204f8a7b977eb3556c14b5ba18a5caHemant Gupta
786c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xieint register_com_android_bluetooth_a2dp(JNIEnv* env);
796c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie
80c030f08f419d596c4aa216c9cca9867e7b5486f0Mike Lockwoodint register_com_android_bluetooth_a2dp_sink(JNIEnv* env);
81c030f08f419d596c4aa216c9cca9867e7b5486f0Mike Lockwood
82c1c259c0ace7195240f1443c805995bfe8692a72Zhihai Xuint register_com_android_bluetooth_avrcp(JNIEnv* env);
83c1c259c0ace7195240f1443c805995bfe8692a72Zhihai Xu
84066ad9e16a548218b139424f758b92db7af34af2Mike Lockwoodint register_com_android_bluetooth_avrcp_controller(JNIEnv* env);
85066ad9e16a548218b139424f758b92db7af34af2Mike Lockwood
8635207a5638f61caca5b9abb31e5c6850a9478a52Matthew Xieint register_com_android_bluetooth_hid(JNIEnv* env);
8735207a5638f61caca5b9abb31e5c6850a9478a52Matthew Xie
88cf27f0ddf906a8f76a22b8a30ff4262526e33477Hemant Guptaint register_com_android_bluetooth_hidd(JNIEnv* env);
89cf27f0ddf906a8f76a22b8a30ff4262526e33477Hemant Gupta
90676cb1bdd1c14c7af56562bab51f168e7f8f6d62Matthew Xieint register_com_android_bluetooth_hdp(JNIEnv* env);
91676cb1bdd1c14c7af56562bab51f168e7f8f6d62Matthew Xie
926654f5c903de510a70f9e72cd5ad7837b615d93ffredcint register_com_android_bluetooth_pan(JNIEnv* env);
936654f5c903de510a70f9e72cd5ad7837b615d93ffredc
9403b8386de26ba6500af2d66687bff9b01f2cbbd7Ganesh Ganapathi Battaint register_com_android_bluetooth_gatt (JNIEnv* env);
9503b8386de26ba6500af2d66687bff9b01f2cbbd7Ganesh Ganapathi Batta
96bbb4110b455b3aa29106d5b4f0a37e1be8e09475Casper Bondeint register_com_android_bluetooth_sdp (JNIEnv* env);
97bbb4110b455b3aa29106d5b4f0a37e1be8e09475Casper Bonde
986c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie}
996c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie
1006c91bc0a163cc7600c40d7fb979777fd911d1ef1Matthew Xie#endif /* COM_ANDROID_BLUETOOTH_H */
101