Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 025c3102 authored by Srinu Jella's avatar Srinu Jella Committed by Linux Build Service Account
Browse files

Bluetooth: Add Get/Set socket option to the Blueooth Socket

Add Get/Set socket option for the Bluetooth socket.These APIs
will be called from the frameworks.

CRs-Fixed: 557180
Change-Id: I5ceef23d49e382089f479e8d618eec1dbc6ed5fb
Conflicts:
jni/com_android_bluetooth_btservice_AdapterService.cpp
parent f3feef17
Loading
Loading
Loading
Loading
+63 −0
Original line number Diff line number Diff line
@@ -1071,6 +1071,67 @@ static jboolean setDevicePropertyNative(JNIEnv *env, jobject obj, jbyteArray add
    return result;
}

static int getSocketOptNative(JNIEnv *env, jobject obj, jint type, jint channel, jint optionName,
                                        jbyteArray optionVal) {
    ALOGV("%s:",__FUNCTION__);

    jbyte *option_val = NULL;
    int option_len;
    bt_status_t status;

    if (!sBluetoothSocketInterface) return -1;

    option_val = env->GetByteArrayElements(optionVal, NULL);
    if (option_val == NULL) {
        ALOGE("getSocketOptNative :jniThrowIOException ");
        jniThrowIOException(env, EINVAL);
        return -1;
    }

    if ( (status = sBluetoothSocketInterface->get_sock_opt((btsock_type_t)type, channel,
         (btsock_option_type_t) optionName, (void *) option_val, &option_len)) !=
                                                           BT_STATUS_SUCCESS) {
        ALOGE("get_sock_opt failed: %d", status);
        goto Fail;
    }
    env->ReleaseByteArrayElements(optionVal, option_val, 0);

    return option_len;
Fail:
    env->ReleaseByteArrayElements(optionVal, option_val, 0);
    return -1;
}

static int setSocketOptNative(JNIEnv *env, jobject obj, jint type, jint channel, jint optionName,
                                        jbyteArray optionVal, jint optionLen) {
    ALOGV("%s:",__FUNCTION__);

    jbyte *option_val = NULL;
    bt_status_t status;

    if (!sBluetoothSocketInterface) return -1;

    option_val = env->GetByteArrayElements(optionVal, NULL);
    if (option_val == NULL) {
        ALOGE("setSocketOptNative:jniThrowIOException ");
        jniThrowIOException(env, EINVAL);
        return -1;
    }

    if ( (status = sBluetoothSocketInterface->set_sock_opt((btsock_type_t)type, channel,
         (btsock_option_type_t) optionName, (void *) option_val, optionLen)) !=
                                                         BT_STATUS_SUCCESS) {
        ALOGE("set_sock_opt failed: %d", status);
        goto Fail;
    }
    env->ReleaseByteArrayElements(optionVal, option_val, 0);

    return 0;
Fail:
    env->ReleaseByteArrayElements(optionVal, option_val, 0);
    return -1;
}

static jboolean getRemoteServicesNative(JNIEnv *env, jobject obj, jbyteArray address) {
    ALOGV("%s:",__FUNCTION__);

@@ -1242,6 +1303,8 @@ static JNINativeMethod sMethods[] = {
    {"configHciSnoopLogNative", "(Z)Z", (void*) configHciSnoopLogNative},
    {"alarmFiredNative", "()V", (void *) alarmFiredNative},
    {"readEnergyInfo", "()I", (void*) readEnergyInfo},
    {"getSocketOptNative", "(III[B)I", (void*) getSocketOptNative},
    {"setSocketOptNative", "(III[BI)I", (void*) setSocketOptNative}
};

int register_com_android_bluetooth_btservice_AdapterService(JNIEnv* env)
+42 −0
Original line number Diff line number Diff line
@@ -1044,6 +1044,29 @@ public class AdapterService extends Service {
            return service.createSocketChannel(type, serviceName, uuid, port, flag);
        }

        public int setSocketOpt(int type, int channel, int optionName, byte [] optionVal,
                                                    int optionLen) {
            if (!Utils.checkCaller()) {
                Log.w(TAG,"setSocketOpt(): not allowed for non-active user");
                return -1;
            }

            AdapterService service = getService();
            if (service == null) return -1;
            return service.setSocketOpt(type, channel, optionName, optionVal, optionLen);
        }

        public int getSocketOpt(int type, int channel, int optionName, byte [] optionVal) {
            if (!Utils.checkCaller()) {
                Log.w(TAG,"getSocketOpt(): not allowed for non-active user");
                return -1;
            }

            AdapterService service = getService();
            if (service == null) return -1;
            return service.getSocketOpt(type, channel, optionName, optionVal);
        }

        public boolean configHciSnoopLog(boolean enable) {
            if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
                (!Utils.checkCaller())) {
@@ -1614,6 +1637,19 @@ public class AdapterService extends Service {
        return ParcelFileDescriptor.adoptFd(fd);
    }

     int setSocketOpt(int type, int channel, int optionName, byte [] optionVal,
             int optionLen) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");

        return setSocketOptNative(type, channel, optionName, optionVal, optionLen);
     }

     int getSocketOpt(int type, int channel, int optionName, byte [] optionVal) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");

        return getSocketOptNative(type, channel, optionName, optionVal);
     }

    boolean configHciSnoopLog(boolean enable) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        return configHciSnoopLogNative(enable);
@@ -1844,6 +1880,12 @@ public class AdapterService extends Service {
    private native int createSocketChannelNative(int type, String serviceName,
                                                 byte[] uuid, int port, int flag);

    private native int setSocketOptNative(int fd, int type, int optionName,
                                byte [] optionVal, int optionLen);

    private native int  getSocketOptNative(int fd, int type, int optionName,
                                byte [] optionVal);

    /*package*/ native boolean configHciSnoopLogNative(boolean enable);

    private native void alarmFiredNative();