Exemplo n.º 1
0
void CAEDRNativeRemoveDeviceSocket(JNIEnv *env, jobject deviceSocket)
{
    OIC_LOG(DEBUG, TAG, "CAEDRNativeRemoveDeviceSocket");

    if (!g_deviceObjectList)
    {
        OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
        return;
    }

    jint index;
    jint length = u_arraylist_length(g_deviceStateList);
    for (index = 0; index < length; index++)
    {
        jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
        if (!jarrayObj)
        {
            OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
            continue;
        }

        jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
        if (!jni_setAddress)
        {
            OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_setAddress is null");
            continue;
        }

        jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
        if (!jni_remoteAddress)
        {
            OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_remoteAddress is null");
            continue;
        }

        const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
        const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);

        if (!strcmp(setAddress, remoteAddress))
        {
            OIC_LOG_V(DEBUG, TAG, "[EDR][Native] remove object : %s", remoteAddress);
            (*env)->DeleteGlobalRef(env, jarrayObj);
            (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
            (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);

            u_arraylist_remove(g_deviceObjectList, index);
            break;
        }
        (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
        (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
    }

    OIC_LOG(DEBUG, TAG, "[EDR][Native] there are no target object");
    return;
}
Exemplo n.º 2
0
void CAEDRNativeAddDeviceSocketToList(JNIEnv *env, jobject deviceSocket)
{
    OIC_LOG(DEBUG, TAG, "CANativeAddDeviceobjToList");

    if (!deviceSocket)
    {
        OIC_LOG(ERROR, TAG, "Device is null");
        return;
    }

    if (!g_deviceObjectList)
    {
        OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
        return;
    }

    jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
    if (!jni_remoteAddress)
    {
        OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
        return;
    }

    const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);

    if (!CAEDRNativeIsDeviceSocketInList(env, remoteAddress))
    {
        jobject gDeviceSocker = (*env)->NewGlobalRef(env, deviceSocket);
        u_arraylist_add(g_deviceObjectList, gDeviceSocker);
        OIC_LOG(DEBUG, TAG, "Set Socket Object to Array");
    }
    (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
    (*env)->DeleteLocalRef(env, jni_remoteAddress);
}
Exemplo n.º 3
0
bool CAEDRNativeIsDeviceSocketInList(JNIEnv *env, const char* remoteAddress)
{
    OIC_LOG(DEBUG, TAG, "CANativeIsDeviceObjInList");

    if (!remoteAddress)
    {
        OIC_LOG(ERROR, TAG, "remoteAddress is null");
        return false;
    }

    jint length = u_arraylist_length(g_deviceStateList);
    for (jint index = 0; index < length; index++)
    {

        jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
        if (!jarrayObj)
        {
            OIC_LOG(DEBUG, TAG, "jarrayObj is null");
            return false;
        }

        jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
        if (!jni_setAddress)
        {
            OIC_LOG(DEBUG, TAG, "jni_setAddress is null");
            return false;
        }

        const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
        if (!setAddress)
        {
            OIC_LOG(DEBUG, TAG, "setAddress is null");
            return false;
        }

        if (!strcmp(remoteAddress, setAddress))
        {
            OIC_LOG(DEBUG, TAG, "the device is already set");
            (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
            return true;
        }
        else
        {
            (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
            continue;
        }
    }

    OIC_LOG(DEBUG, TAG, "there are no the Device obejct in list. we can add");
    return false;
}
Exemplo n.º 4
0
jboolean CAEDRNativeIsDeviceSocketInList(JNIEnv *env, const char* remoteAddress)
{
    OIC_LOG(DEBUG, TAG, "[EDR][Native] CANativeIsDeviceObjInList");

    jint index;

    if (!remoteAddress) {
        OIC_LOG(ERROR, TAG, "[EDR][Native] remoteAddress is null");
        return JNI_TRUE;
    }
    for (index = 0; index < u_arraylist_length(g_deviceObjectList); index++)
    {

        jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
        if(!jarrayObj)
        {
            OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
            return JNI_TRUE;
        }

        jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
        if(!jni_setAddress)
        {
            OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_setAddress is null");
            return JNI_TRUE;
        }

        const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
        if(!setAddress)
        {
            OIC_LOG(DEBUG, TAG, "[EDR][Native] setAddress is null");
            return JNI_TRUE;
        }

        if(!strcmp(remoteAddress, setAddress))
        {
            OIC_LOG(DEBUG, TAG, "the device is already set");
            return JNI_TRUE;
        }
        else
        {
            continue;
        }
    }

    OIC_LOG(DEBUG, TAG, "there are no the Device obejct in list. we can add");
    return JNI_FALSE;
}
Exemplo n.º 5
0
jobject CAEDRNativeGetDeviceSocketBaseAddr(JNIEnv *env, const char* remoteAddress)
{
    OIC_LOG(DEBUG, TAG, "CAEDRNativeGetDeviceSocket");

    if (!g_deviceObjectList)
    {
        OIC_LOG(ERROR, TAG, "gdeviceObjectList is null");
        return NULL;
    }

    jint length = u_arraylist_length(g_deviceStateList);
    for (jint index = 0; index < length; index++)
    {
        jobject jarrayObj = (jobject) u_arraylist_get(g_deviceObjectList, index);
        if (!jarrayObj)
        {
            OIC_LOG(ERROR, TAG, "jarrayObj is null");
            continue;
        }

        jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
        if (!jni_setAddress)
        {
            OIC_LOG(ERROR, TAG, "jni_setAddress is null");
            continue;
        }
        const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);

        if (!strcmp(setAddress, remoteAddress))
        {
            OIC_LOG_V(ERROR, TAG, "remove object : %s", remoteAddress);
            (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
            (*env)->DeleteLocalRef(env, jni_setAddress);
            return jarrayObj;
        }
        (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
        (*env)->DeleteLocalRef(env, jni_setAddress);
    }

    return NULL;
}
Exemplo n.º 6
0
void CAEDRNativeAddDeviceSocketToList(JNIEnv *env, jobject deviceSocket)
{
    OIC_LOG(DEBUG, TAG, "[EDR][Native] CANativeAddDeviceobjToList");

    if(!deviceSocket)
    {
        OIC_LOG(ERROR, TAG, "[EDR][Native] Device is null");
        return;
    }

    if(!g_deviceObjectList)
    {
        OIC_LOG(ERROR, TAG, "[EDR][Native] gdeviceObjectList is null");
        return;
    }

    jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
    if(!jni_remoteAddress)
    {
        OIC_LOG(ERROR, TAG, "[EDR][Native] jni_remoteAddress is null");
        return;
    }

    u_mutex_lock(g_mutexSocketListManager);

    const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);

    if(!CAEDRNativeIsDeviceSocketInList(env, remoteAddress))
    {
        jobject gDeviceSocker = (*env)->NewGlobalRef(env, deviceSocket);
        u_arraylist_add(g_deviceObjectList, gDeviceSocker);
        OIC_LOG(DEBUG, TAG, "Set Socket Object to Array");
    }

    u_mutex_unlock(g_mutexSocketListManager);
}
Exemplo n.º 7
0
CAResult_t CAEDRNativeReadData(JNIEnv *env, uint32_t idx)
{
    if ((*env)->ExceptionCheck(env))
    {
        (*env)->ExceptionDescribe(env);
        (*env)->ExceptionClear(env);
        OIC_LOG(ERROR, TAG, "env error!!");
        return CA_STATUS_FAILED;
    }

    jobject jni_obj_inputStream = CAEDRNativeGetInputStream(idx);
    if (!jni_obj_inputStream)
    {
        OIC_LOG(ERROR, TAG, "jni_obj_inputStream is null");
        return CA_STATUS_FAILED;
    }

    jmethodID jni_mid_available = CAGetJNIMethodID(env, "java/io/InputStream", "available", "()I");
    if (!jni_mid_available)
    {
        OIC_LOG(ERROR, TAG, "jni_mid_available is null");
        return CA_STATUS_FAILED;
    }

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

    if (0 < available)
    {
        jobject jni_obj_socket = CAEDRNativeGetDeviceSocket(idx);
        if (!jni_obj_socket)
        {
            OIC_LOG(ERROR, TAG, "jni_obj_socket is null");
            return CA_STATUS_FAILED;
        }

        jstring jni_str_address = CAEDRNativeGetAddressFromDeviceSocket(env, jni_obj_socket);
        if (!jni_str_address)
        {
            OIC_LOG(ERROR, TAG, "jni_str_address is null");
            return CA_STATUS_FAILED;
        }

        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);
            return CA_STATUS_FAILED;
        }

        CAConnectedDeviceInfo_t *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);
            return CA_STATUS_FAILED;
        }

        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);
                return CA_STATUS_FAILED;
            }
        }

        jint remainSize = (jint) bufSize - deviceInfo->recvDataLen;
        if (0 >= remainSize)
        {
            OIC_LOG(ERROR, TAG, "remainSize value is invalid.");
            (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
            (*env)->DeleteLocalRef(env, jni_str_address);
            return CA_STATUS_FAILED;
        }

        jbyteArray jbuf = (*env)->NewByteArray(env, remainSize);
        if (!jbuf)
        {
            OIC_LOG(ERROR, TAG, "jbuf is null");
            (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
            (*env)->DeleteLocalRef(env, jni_str_address);
            return CA_STATUS_FAILED;
        }

        jmethodID jni_mid_read = CAGetJNIMethodID(env, "java/io/InputStream", "read", "([BII)I");
        if (!jni_mid_read)
        {
            OIC_LOG(ERROR, TAG, "jni_mid_read is null");
            (*env)->DeleteLocalRef(env, jbuf);
            (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
            (*env)->DeleteLocalRef(env, jni_str_address);
            return CA_STATUS_FAILED;
        }

        OIC_LOG_V(DEBUG, TAG, "read InputStream (idx:%d, addr:%s)", idx, address);
        jint recvLen = (*env)->CallIntMethod(env, jni_obj_inputStream, jni_mid_read,
                                             jbuf, (jint) 0, remainSize);
        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);
            return CA_STATUS_FAILED;
        }
        OIC_LOG_V(DEBUG, TAG, "read success (length: %d bytes)", recvLen);

        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);
            return CA_STATUS_FAILED;
        }
        memcpy(deviceInfo->recvData + deviceInfo->recvDataLen, (const char*) buf, recvLen);
        deviceInfo->recvDataLen += recvLen;

        (*env)->ReleaseByteArrayElements(env, jbuf, buf, 0);
        (*env)->DeleteLocalRef(env, jbuf);

        if (!deviceInfo->totalDataLen && deviceInfo->recvData)
        {
            coap_transport_t 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->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)->ReleaseStringUTFChars(env, jni_str_address, address);
                    (*env)->DeleteLocalRef(env, jni_str_address);
                    return CA_STATUS_FAILED;
                }
                deviceInfo->recvData = newBuf;
            }
        }
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;
            }
        }