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

Commit c6eefaf9 authored by Srinu Jella's avatar Srinu Jella
Browse files

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

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

Change-Id: I5ceef23d49e382089f479e8d618eec1dbc6ed5fb
parent 002b5864
Loading
Loading
Loading
Loading
+64 −1
Original line number Diff line number Diff line
@@ -898,6 +898,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__);

@@ -1039,7 +1100,9 @@ static JNINativeMethod sMethods[] = {
    {"getRemoteMasInstancesNative", "([B)Z", (void*) getRemoteMasInstancesNative},
    {"connectSocketNative", "([BI[BII)I", (void*) connectSocketNative},
    {"createSocketChannelNative", "(ILjava/lang/String;[BII)I",
     (void*) createSocketChannelNative}
     (void*) createSocketChannelNative},
    {"getSocketOptNative", "(III[B)I", (void*) getSocketOptNative},
    {"setSocketOptNative", "(III[BI)I", (void*) setSocketOptNative}
};

int register_com_android_bluetooth_btservice_AdapterService(JNIEnv* env)
+41 −0
Original line number Diff line number Diff line
@@ -913,6 +913,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 void registerCallback(IBluetoothCallback cb) {
            AdapterService service = getService();
            if (service == null) return ;
@@ -1410,6 +1433,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);
     }

     void registerCallback(IBluetoothCallback cb) {
         mCallbacks.register(cb);
      }
@@ -1484,6 +1520,11 @@ public class AdapterService extends Service {
                                           byte[] uuid, int port, int flag);
    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);

    protected void finalize() {
        cleanup();