예제 #1
0
void CAEDRNativeSocketClose(JNIEnv *env, const char *address)
{
    VERIFY_NON_NULL_VOID(address, TAG, "address is null");

    jclass jni_cid_BTSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
    if (!jni_cid_BTSocket)
    {
        OIC_LOG(ERROR, TAG, "jni_cid_BTSocket is null");
        return;
    }

    jmethodID jni_mid_close = (*env)->GetMethodID(env, jni_cid_BTSocket, "close", "()V");
    if (!jni_mid_close)
    {
        OIC_LOG(ERROR, TAG, "jni_mid_close is null");
        return;
    }

    jobject jni_obj_socket = CAEDRNativeGetDeviceSocketBaseAddr(env, address);
    if (!jni_obj_socket)
    {
        OIC_LOG(ERROR, TAG, "jni_obj_socket is not available");
        return;
    }

    (*env)->CallVoidMethod(env, jni_obj_socket, jni_mid_close);

    if ((*env)->ExceptionCheck(env))
    {
        OIC_LOG(ERROR, TAG, "close is Failed!!!");
        (*env)->ExceptionDescribe(env);
        (*env)->ExceptionClear(env);
        return;
    }

    // remove socket to list
    CAEDRNativeRemoveDeviceSocket(env, jni_obj_socket);

    // update state
    ca_mutex_lock(g_mutexStateList);
    CAEDRUpdateDeviceState(STATE_DISCONNECTED, address);
    ca_mutex_unlock(g_mutexStateList);

    OIC_LOG_V(DEBUG, TAG, "disconnected with [%s]", address);
}
예제 #2
0
void CAEDRNativeSocketClose(JNIEnv *env, const char *address, uint32_t id)
{

    jclass jni_cid_BTSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
    if(!jni_cid_BTSocket)
    {
        OIC_LOG(ERROR, TAG, "[EDR][Native] close: jni_cid_BTSocket is null");
        return;
    }

    jmethodID jni_mid_close = (*env)->GetMethodID(env, jni_cid_BTSocket, "close", "()V");
    if(!jni_mid_close)
    {
        OIC_LOG(ERROR, TAG, "[EDR][Native] close: jni_mid_close is null");
        return;
    }

    jobject jni_obj_socket = CAEDRNativeGetDeviceSocket(id);
    if(!jni_obj_socket)
    {
        OIC_LOG(ERROR, TAG, "[EDR][Native] close: jni_obj_socket is not available");
        return;
    }

    (*env)->CallVoidMethod(env, jni_obj_socket, jni_mid_close);

    if((*env)->ExceptionCheck(env))
    {
        OIC_LOG(DEBUG, TAG, "[EDR][Native] close: close is Failed!!!");
        (*env)->ExceptionDescribe(env);
        (*env)->ExceptionClear(env);
        return;
    }

    // remove socket to list
    CAEDRNativeRemoveDeviceSocket(env, jni_obj_socket);

    // update state
    CAEDRUpdateDeviceState(STATE_DISCONNECTED, address);

    OIC_LOG(DEBUG, TAG, "[EDR][Native] close: disconnected");
}
CAResult_t CAEDRNativeReadData(JNIEnv *env, uint32_t id)
{
    if ((*env)->ExceptionCheck(env))
    {
        (*env)->ExceptionDescribe(env);
        (*env)->ExceptionClear(env);
        OIC_LOG(ERROR, TAG, "env error!!");
        return CA_STATUS_FAILED;
    }

    // check whether this socket object is connected or not.
    jobject jni_obj_socket = CAEDRNativeGetDeviceSocket(id);
    if (!jni_obj_socket)
    {
        return CA_STATUS_INVALID_PARAM;
    }

    // check it whether is still connected or not through google api
    jboolean ret = CAEDRIsConnectedForSocket(env, jni_obj_socket);
    if (!ret)
    {
        OIC_LOG(ERROR, TAG, "it is not connected yet.");

        // remove socket to list
        CAEDRNativeRemoveDeviceSocket(env, jni_obj_socket);
        return CA_STATUS_FAILED;
    }

    // start to read through InputStream
    jclass jni_cid_BTsocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
    if (!jni_cid_BTsocket)
    {
        OIC_LOG(ERROR, TAG, "jni_cid_BTsocket is null");
        return CA_STATUS_FAILED;
    }
    jmethodID jni_mid_getInputStream = (*env)->GetMethodID(env, jni_cid_BTsocket,
                                                           "getInputStream",
                                                           "()Ljava/io/InputStream;");

    jobject jni_obj_inputStream = (*env)->CallObjectMethod(env, jni_obj_socket,
                                                           jni_mid_getInputStream);
    if (!jni_obj_inputStream)
    {
        OIC_LOG(ERROR, TAG, "jni_obj_inputStream is null");
        (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
        return CA_STATUS_FAILED;
    }

    jclass jni_cid_InputStream = (*env)->FindClass(env, "java/io/InputStream");
    if (!jni_cid_InputStream)
    {
        OIC_LOG(ERROR, TAG, "jni_cid_InputStream is null");
        (*env)->DeleteLocalRef(env, jni_obj_inputStream);
        (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
        return CA_STATUS_FAILED;
    }

    jmethodID jni_mid_available = (*env)->GetMethodID(env, jni_cid_InputStream,
                                                      "available", "()I");
    if (!jni_mid_available)
    {
        OIC_LOG(ERROR, TAG, "jni_mid_available is null");
        goto exit;
    }

    jint available = (*env)->CallIntMethod(env, jni_obj_inputStream, jni_mid_available);

    CAConnectedDeviceInfo_t *deviceInfo = NULL;
    if (0 < available)
    {
        jstring jni_str_address = CAEDRNativeGetAddressFromDeviceSocket(env, jni_obj_socket);
        if (!jni_str_address)
        {
            OIC_LOG(ERROR, TAG, "jni_str_address is null");
            goto exit;
        }

        const char* address = (*env)->GetStringUTFChars(env, jni_str_address, NULL);
        if (!address)
        {
            OIC_LOG(ERROR, TAG, "address is null");
            (*env)->DeleteLocalRef(env, jni_str_address);
            goto exit;
        }

        OIC_LOG_V(DEBUG, TAG, "get InputStream..%d, %s", id, address);
        jmethodID jni_mid_read = (*env)->GetMethodID(env, jni_cid_InputStream,
                                                     "read", "([BII)I");
        if (!jni_mid_read)
        {
            OIC_LOG(ERROR, TAG, "jni_mid_read is null");
            (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
            (*env)->DeleteLocalRef(env, jni_str_address);
            goto exit;
        }

        deviceInfo = (CAConnectedDeviceInfo_t *) CAEDRGetDeviceInfoFromAddress(address);
        if (!deviceInfo)
        {
            OIC_LOG(ERROR, TAG, "failed to get device info from list");
            (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
            (*env)->DeleteLocalRef(env, jni_str_address);
            goto exit;
        }

        jint bufSize = (deviceInfo->totalDataLen == 0) ?
                EDR_MAX_HEADER_LEN : deviceInfo->totalDataLen;
        if (!deviceInfo->recvData)
        {
            deviceInfo->recvData = OICCalloc(1, bufSize);
            if (!deviceInfo->recvData)
            {
                OIC_LOG(ERROR, TAG, "out of memory");
                (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
                (*env)->DeleteLocalRef(env, jni_str_address);
                goto exit;
            }
        }

        jbyteArray jbuf = (*env)->NewByteArray(env, (jint) bufSize - deviceInfo->recvDataLen);
        if (!jbuf)
        {
            OIC_LOG(ERROR, TAG, "jbuf is null");
            (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
            (*env)->DeleteLocalRef(env, jni_str_address);
            goto exit;
        }

        jint recvLen = (*env)->CallIntMethod(env, jni_obj_inputStream, jni_mid_read,
                                             jbuf, (jint) 0,
                                             (jint) bufSize - deviceInfo->recvDataLen);
        if (-1 == recvLen)
        {
            OIC_LOG(ERROR, TAG, "recvLen is -1");
            (*env)->DeleteLocalRef(env, jbuf);
            (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
            (*env)->DeleteLocalRef(env, jni_str_address);
            goto exit;
        }

        jbyte* buf = (*env)->GetByteArrayElements(env, jbuf, NULL);
        if (!buf)
        {
            OIC_LOG(ERROR, TAG, "buf is null");
            (*env)->DeleteLocalRef(env, jbuf);
            (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
            (*env)->DeleteLocalRef(env, jni_str_address);
            goto exit;
        }
        memcpy(deviceInfo->recvData + deviceInfo->recvDataLen, (const char*) buf, recvLen);
        deviceInfo->recvDataLen += recvLen;

        OIC_LOG(DEBUG, TAG, "read something from InputStream");

        if (!deviceInfo->totalDataLen)
        {
            coap_transport_type transport = coap_get_tcp_header_type_from_initbyte(
                    ((unsigned char *) deviceInfo->recvData)[0] >> 4);
            size_t headerLen = coap_get_tcp_header_length_for_transport(transport);
            if (deviceInfo->recvData && deviceInfo->recvDataLen >= headerLen)
            {
                deviceInfo->totalDataLen = coap_get_total_message_length(deviceInfo->recvData,
                                                                         deviceInfo->recvDataLen);
                OIC_LOG_V(DEBUG, TAG, "total data length [%d] bytes", deviceInfo->totalDataLen);

                uint8_t *newBuf = OICRealloc(deviceInfo->recvData, deviceInfo->totalDataLen);
                if (!newBuf)
                {
                    OIC_LOG(ERROR, TAG, "out of memory");
                    (*env)->ReleaseByteArrayElements(env, jbuf, buf, 0);
                    (*env)->DeleteLocalRef(env, jbuf);
                    (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
                    (*env)->DeleteLocalRef(env, jni_str_address);
                    goto exit;
                }
                deviceInfo->recvData = newBuf;
            }
        }
예제 #4
0
/**
 * EDR Method
 */
void CAEDRNativeSendData(JNIEnv *env, const char *address, const char *data, uint32_t id)
{
    OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData logic start");

    if(STATE_DISCONNECTED == CAEDRIsConnectedDevice(address))
    {
        // connect before send data
        OIC_LOG(DEBUG, TAG, "[EDR][Native] connect before send data");

        if(NULL == address)
        {
            OIC_LOG(ERROR, TAG, "[EDR][Native] remote address is empty");
            return;
        }
        else
        {
            CAEDRNativeConnect(env, address, id);
        }
    }

    if(STATE_CONNECTED == CAEDRIsConnectedDevice(address))
    {
        if(!((*env)->ExceptionCheck(env)))
        {
            jclass jni_cid_BTsocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
            if(!jni_cid_BTsocket)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_cid_BTsocket is null");
                return;
            }

            jmethodID jni_mid_getOutputStream = (*env)->GetMethodID(env,
                    jni_cid_BTsocket, "getOutputStream",
                    "()Ljava/io/OutputStream;");
            if(!jni_mid_getOutputStream)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_mid_getOutputStream is null");
                return;
            }
            OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btSendData: Get MethodID for i/o stream..%d", id);

            jobject jni_obj_socket = CAEDRNativeGetDeviceSocket(id);
            if(!jni_obj_socket)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_socket is not available");
                return;
            }

            jobject jni_obj_outputStream = (*env)->CallObjectMethod(env,
                    jni_obj_socket, jni_mid_getOutputStream);
            if(!jni_obj_outputStream)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_obj_outputStream is null");
                return;
            }

            OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: ready outputStream..");

            jclass jni_cid_OutputStream = (*env)->FindClass(env, "java/io/OutputStream");
            if(!jni_cid_OutputStream)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_cid_OutputStream is null");
                return;
            }

            jmethodID jni_mid_write = (*env)->GetMethodID(env,
                    jni_cid_OutputStream, "write", "([BII)V");
            if(!jni_mid_write)
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: jni_mid_write is null");
                return;
            }

            jbyteArray jbuf;
            int length = strlen(data);
            jbuf = (*env)->NewByteArray(env, length);
            (*env)->SetByteArrayRegion(env, jbuf, 0, length, (jbyte*)data);

            (*env)->CallVoidMethod(env, jni_obj_outputStream, jni_mid_write,
                    jbuf, (jint) 0, (jint) length);

            if((*env)->ExceptionCheck(env))
            {
                OIC_LOG(ERROR, TAG, "[EDR][Native] btSendData: Write Error!!!");
                (*env)->ExceptionDescribe(env);
                (*env)->ExceptionClear(env);
                return;
            }

            OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: Write Success");

            // remove socket to list
            CAEDRNativeRemoveDeviceSocket(env, jni_obj_socket);

            // update state
            CAEDRUpdateDeviceState(STATE_DISCONNECTED, address);
        }
        else
        {
            (*env)->ExceptionDescribe(env);
            (*env)->ExceptionClear(env);
            OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: error!!");
            return;
        }
    }
    else
    {
        OIC_LOG(DEBUG, TAG, "[EDR][Native] btSendData: BT connection is not completed!!");
    }
}