#define MICROSECS_PER_SEC 1000000
#define WAIT_TIME_WRITE_CHARACTERISTIC 10 * MICROSECS_PER_SEC
+#define WAIT_TIME_SCAN_INTERVAL_DEFAULT 10
+#define WAIT_TIME_SCANNED_CHECKING 30
+
+#define GATT_CONNECTION_PRIORITY_BALANCED 0
+#define GATT_FAILURE 257
+#define GATT_INSUFFICIENT_AUTHENTICATION 5
+#define GATT_INSUFFICIENT_ENCRYPTION 15
+#define GATT_INVALID_ATTRIBUTE_LENGTH 13
+#define GATT_INVALID_OFFSET 7
+#define GATT_READ_NOT_PERMITTED 2
+#define GATT_REQUEST_NOT_SUPPORTED 6
+#define GATT_WRITE_NOT_PERMITTED 3
static ca_thread_pool_t g_threadPoolHandle = NULL;
// it will be prevent to start send logic when adapter has stopped.
static bool g_isStartedLEClient = false;
-static bool g_isStartedMulticastServer = false;
-static bool g_isStartedScan = false;
static jbyteArray g_sendBuffer = NULL;
static uint32_t g_targetCnt = 0;
static ca_mutex g_gattObjectMutex = NULL;
static ca_mutex g_deviceStateListMutex = NULL;
-static ca_mutex g_scanMutex = NULL;
+static ca_mutex g_deviceScanRetryDelayMutex = NULL;
+static ca_cond g_deviceScanRetryDelayCond = NULL;
+
+static ca_mutex g_threadScanIntervalMutex = NULL;
+static ca_cond g_threadScanIntervalCond = NULL;
+
+static ca_mutex g_threadSendStateMutex = NULL;
+
+static int32_t g_scanIntervalTime = WAIT_TIME_SCAN_INTERVAL_DEFAULT;
+static int32_t g_scanIntervalTimePrev = WAIT_TIME_SCAN_INTERVAL_DEFAULT;
+static int32_t g_intervalCount = 0;
+static bool g_isWorkingScanThread = false;
+static CALEScanState_t g_scanningStep = BLE_SCAN_DISABLE;
static CABLEDataReceivedCallback g_CABLEClientDataReceivedCallback = NULL;
+/**
+ * check if retry logic for connection routine has to be stopped or not.
+ * in case of error value including this method, connection routine has to be stopped.
+ * since there is no retry logic for this error reason in this client.
+ * @param state constant value of bluetoothgatt.
+ * @return true - waiting for background connection in BT platform.
+ * false - connection routine has to be stopped.
+ */
+static bool CALECheckConnectionStateValue(jint state)
+{
+ switch(state)
+ {
+ case GATT_CONNECTION_PRIORITY_BALANCED:
+ case GATT_FAILURE:
+ case GATT_INSUFFICIENT_AUTHENTICATION:
+ case GATT_INSUFFICIENT_ENCRYPTION:
+ case GATT_INVALID_ATTRIBUTE_LENGTH:
+ case GATT_INVALID_OFFSET:
+ case GATT_READ_NOT_PERMITTED:
+ case GATT_REQUEST_NOT_SUPPORTED:
+ case GATT_WRITE_NOT_PERMITTED:
+ return true;
+ default:
+ return false;
+ }
+}
+
+void CALEClientSetScanInterval(int32_t intervalTime, int32_t workingCount)
+{
+ OIC_LOG_V(DEBUG, TAG, "CALEClientSetScanInterval : %d -> %d",
+ g_scanIntervalTime, intervalTime);
+
+ // previous time should be stored.
+ if (0 < workingCount)
+ {
+ g_scanIntervalTimePrev = g_scanIntervalTime;
+ }
+ g_scanIntervalTime = intervalTime;
+ g_intervalCount = workingCount;
+}
+
+void CALERestartScanWithInterval(int32_t intervalTime, int32_t workingCount)
+{
+ // restart scan with interval
+ CALEClientSetScanInterval(intervalTime, workingCount);
+ ca_cond_signal(g_threadScanIntervalCond);
+}
+
+static void CALEScanThread(void* object)
+{
+ (void)object;
+
+ bool isAttached = false;
+ JNIEnv* env = NULL;
+ jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
+ if (JNI_OK != res)
+ {
+ OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
+ res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
+
+ if (JNI_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
+ return;
+ }
+ isAttached = true;
+ }
+
+ ca_mutex_lock(g_threadScanIntervalMutex);
+ while(g_isWorkingScanThread)
+ {
+ OIC_LOG(DEBUG, TAG, "scan waiting time out");
+ if (BLE_SCAN_ENABLE == g_scanningStep)
+ {
+ //stop scan
+ CAResult_t ret = CALEClientStopScan();
+ if (CA_STATUS_OK != ret)
+ {
+ OIC_LOG(INFO, TAG, "CALEClientStopScan has failed");
+ }
+ }
+ else
+ {
+ //start scan
+ CAResult_t ret = CALEClientStartScan();
+ if (CA_STATUS_OK != ret)
+ {
+ OIC_LOG(INFO, TAG, "CALEClientStartScan has failed");
+ }
+ }
+
+ OIC_LOG_V(DEBUG, TAG, "wait for Scan Interval Time during %d sec", g_scanIntervalTime);
+ if (CA_WAIT_SUCCESS == ca_cond_wait_for(g_threadScanIntervalCond,
+ g_threadScanIntervalMutex,
+ g_scanIntervalTime * MICROSECS_PER_SEC))
+ {
+ // called signal scan thread will be terminated
+ OIC_LOG(DEBUG, TAG, "signal scanInterval waiting");
+ g_scanningStep = BLE_SCAN_DISABLE;
+ }
+ else
+ {
+ if (BLE_SCAN_ENABLE == g_scanningStep)
+ {
+ if (g_intervalCount > 0)
+ {
+ if (g_intervalCount == 1)
+ {
+ OIC_LOG(DEBUG, TAG, "reset default time");
+ CALEClientSetScanInterval(g_scanIntervalTimePrev, 0);
+ }
+ g_intervalCount--;
+ OIC_LOG_V(DEBUG, TAG, "interval count : %d", g_intervalCount);
+ }
+ g_scanningStep = BLE_SCAN_DISABLE;
+ }
+ else
+ {
+ g_scanningStep = BLE_SCAN_ENABLE;
+ }
+ }
+ }
+ ca_mutex_unlock(g_threadScanIntervalMutex);
+
+ if (isAttached)
+ {
+ (*g_jvm)->DetachCurrentThread(g_jvm);
+ }
+}
+
+CAResult_t CALEClientStartScanWithInterval()
+{
+ OIC_LOG(DEBUG, TAG, "IN - CALEClientStartScanWithInterval");
+
+ if (g_isWorkingScanThread)
+ {
+ OIC_LOG(DEBUG, TAG, "scan interval logic already running");
+ return CA_STATUS_OK;
+ }
+
+ // initialize scan flags
+ g_scanningStep = BLE_SCAN_DISABLE;
+ g_isWorkingScanThread = true;
+ g_intervalCount = 0;
+ g_scanIntervalTime = g_scanIntervalTimePrev;
+
+ if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle,
+ CALEScanThread, NULL))
+ {
+ OIC_LOG(ERROR, TAG, "Failed to create read thread!");
+ g_isWorkingScanThread = false;
+ return CA_STATUS_FAILED;
+ }
+
+ OIC_LOG(DEBUG, TAG, "OUT - CALEClientStartScanWithInterval");
+ return CA_STATUS_OK;
+}
+
+void CALEClientStopScanWithInterval()
+{
+ g_isWorkingScanThread = false;
+ ca_cond_signal(g_threadScanIntervalCond);
+}
+
//getting jvm
void CALEClientJniInit()
{
}
bool isAttached = false;
- JNIEnv* env;
+ JNIEnv* env = NULL;
jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
if (JNI_OK != res)
{
isAttached = true;
}
+ jmethodID mid_getApplicationContext = CAGetJNIMethodID(env, "android/content/Context",
+ "getApplicationContext",
+ "()Landroid/content/Context;");
+
+ if (!mid_getApplicationContext)
+ {
+ OIC_LOG(ERROR, TAG, "Could not get getApplicationContext method");
+ return CA_STATUS_FAILED;
+ }
+
+ jobject jApplicationContext = (*env)->CallObjectMethod(env, g_context,
+ mid_getApplicationContext);
+ if (!jApplicationContext)
+ {
+ OIC_LOG(ERROR, TAG, "Could not get application context");
+ return CA_STATUS_FAILED;
+ }
+
jclass jni_LEInterface = (*env)->FindClass(env, "org/iotivity/ca/CaLeClientInterface");
if (!jni_LEInterface)
{
goto error_exit;
}
- (*env)->NewObject(env, jni_LEInterface, LeInterfaceConstructorMethod, g_context);
+ (*env)->NewObject(env, jni_LEInterface, LeInterfaceConstructorMethod, jApplicationContext);
OIC_LOG(DEBUG, TAG, "Create instance for CaLeClientInterface");
if (isAttached)
}
bool isAttached = false;
- JNIEnv* env;
+ JNIEnv* env = NULL;
jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
if (JNI_OK != res)
{
// init mutex for send logic
g_threadCond = ca_cond_new();
g_threadWriteCharacteristicCond = ca_cond_new();
+ g_deviceScanRetryDelayCond = ca_cond_new();
+ g_threadScanIntervalCond = ca_cond_new();
CALEClientCreateDeviceList();
CALEClientJNISetContext();
}
bool isAttached = false;
- JNIEnv* env;
+ JNIEnv* env = NULL;
jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
if (JNI_OK != res)
{
if (g_leScanCallback)
{
(*env)->DeleteGlobalRef(env, g_leScanCallback);
+ g_leScanCallback = NULL;
}
if (g_leGattCallback)
{
(*env)->DeleteGlobalRef(env, g_leGattCallback);
+ g_leGattCallback = NULL;
}
if (g_sendBuffer)
{
(*env)->DeleteGlobalRef(env, g_sendBuffer);
+ g_sendBuffer = NULL;
}
if (g_uuidList)
{
(*env)->DeleteGlobalRef(env, g_uuidList);
+ g_uuidList = NULL;
}
CAResult_t ret = CALEClientRemoveAllDeviceState();
OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
}
- g_isStartedMulticastServer = false;
- CALEClientSetScanFlag(false);
- CALEClientSetSendFinishFlag(false);
+ CALEClientSetSendFinishFlag(true);
CALEClientTerminateGattMutexVariables();
CALEClientDestroyJniInterface();
ca_cond_free(g_deviceDescCond);
ca_cond_free(g_threadCond);
ca_cond_free(g_threadWriteCharacteristicCond);
+ ca_cond_free(g_deviceScanRetryDelayCond);
+ ca_cond_free(g_threadScanIntervalCond);
g_deviceDescCond = NULL;
g_threadCond = NULL;
g_threadWriteCharacteristicCond = NULL;
+ g_deviceScanRetryDelayCond = NULL;
+ g_threadScanIntervalCond = NULL;
+
g_isSignalSetFlag = false;
+ // stop scanning
+ CALEClientStopScanWithInterval();
+
if (isAttached)
{
(*g_jvm)->DetachCurrentThread(g_jvm);
}
bool isAttached = false;
- JNIEnv* env;
+ JNIEnv* env = NULL;
jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
if (JNI_OK != res)
{
}
bool isAttached = false;
- JNIEnv* env;
+ JNIEnv* env = NULL;
jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
if (JNI_OK != res)
{
CAResult_t CALEClientStartUnicastServer(const char* address)
{
+#ifndef TB_LOG
+ (void)address;
+#endif
OIC_LOG_V(DEBUG, TAG, "it is not needed in this platform (%s)", address);
return CA_NOT_SUPPORTED;
CAResult_t CALEClientStartMulticastServer()
{
- OIC_LOG(DEBUG, TAG, "CALEClientStartMulticastServer");
-
- if (g_isStartedMulticastServer)
- {
- OIC_LOG(ERROR, TAG, "server is already started..it will be skipped");
- return CA_STATUS_FAILED;
- }
-
- if (!g_jvm)
- {
- OIC_LOG(ERROR, TAG, "g_jvm is null");
- return CA_STATUS_FAILED;
- }
-
- bool isAttached = false;
- JNIEnv* env;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
- if (JNI_OK != res)
- {
- OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
- res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
-
- if (JNI_OK != res)
- {
- OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
- return CA_STATUS_FAILED;
- }
- isAttached = true;
- }
-
- g_isStartedMulticastServer = true;
- CAResult_t ret = CALEClientStartScan();
- if (CA_STATUS_OK != ret)
- {
- OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
- }
-
- if (isAttached)
- {
- (*g_jvm)->DetachCurrentThread(g_jvm);
- }
+ OIC_LOG(DEBUG, TAG, "it is not needed in this platform");
- return ret;
+ return CA_NOT_SUPPORTED;
}
void CALEClientStopUnicastServer()
void CALEClientStopMulticastServer()
{
OIC_LOG(DEBUG, TAG, "CALEClientStopMulticastServer");
- g_isStartedMulticastServer = false;
- CAResult_t res = CALEClientStopScan();
- if (CA_STATUS_OK != res)
- {
- OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
- return;
- }
}
void CALEClientSetCallback(CAPacketReceiveCallback callback)
g_clientErrorCallback = callback;
}
-CAResult_t CALEClientIsThereScannedDevices()
+CAResult_t CALEClientIsThereScannedDevices(JNIEnv *env, const char* address)
{
+ VERIFY_NON_NULL(env, TAG, "env");
+
if (!g_deviceList)
{
+ OIC_LOG(ERROR, TAG, "g_deviceList is not available");
return CA_STATUS_FAILED;
}
- if (0 == u_arraylist_length(g_deviceList))
+ if (0 == u_arraylist_length(g_deviceList) // multicast
+ || (address && !CALEClientIsDeviceInScanDeviceList(env, address))) // unicast
{
// Wait for LE peripherals to be discovered.
static uint64_t const TIMEOUT =
2 * MICROSECS_PER_SEC; // Microseconds
+ // set scan interval and start scan
+ CALERestartScanWithInterval(WAIT_TIME_SCANNED_CHECKING, 1);
+
bool devicesDiscovered = false;
- for (size_t i = 0;
- 0 == u_arraylist_length(g_deviceList) && i < RETRIES;
- ++i)
+ for (size_t i = 0; i < RETRIES; ++i)
{
+ OIC_LOG(DEBUG, TAG, "waiting for target device");
if (ca_cond_wait_for(g_deviceDescCond,
g_threadSendMutex,
- TIMEOUT) == 0)
+ TIMEOUT) == CA_WAIT_SUCCESS)
{
- devicesDiscovered = true;
- break;
+ ca_mutex_lock(g_deviceListMutex);
+ size_t scannedDeviceLen = u_arraylist_length(g_deviceList);
+ ca_mutex_unlock(g_deviceListMutex);
+
+ if (0 < scannedDeviceLen)
+ {
+ if (!address // multicast
+ || (address && CALEClientIsDeviceInScanDeviceList(env, address))) // unicast
+ {
+ devicesDiscovered = true;
+ break;
+ }
+ else
+ {
+ if (address)
+ {
+ OIC_LOG(INFO, TAG, "waiting..");
+
+ ca_mutex_lock(g_deviceScanRetryDelayMutex);
+ if (ca_cond_wait_for(g_deviceScanRetryDelayCond,
+ g_deviceScanRetryDelayMutex,
+ MICROSECS_PER_SEC) == CA_WAIT_SUCCESS)
+ {
+ OIC_LOG(INFO, TAG, "finish to waiting for target device");
+ ca_mutex_unlock(g_deviceScanRetryDelayMutex);
+ break;
+ }
+ ca_mutex_unlock(g_deviceScanRetryDelayMutex);
+ // time out
+
+ // checking whether a target device is found while waiting for time-out.
+ if (CALEClientIsDeviceInScanDeviceList(env, address))
+ {
+ devicesDiscovered = true;
+ break;
+ }
+ }
+ }
+ }
}
}
+ // reset scan interval time after checking scanned devices
+ CALERestartScanWithInterval(g_scanIntervalTimePrev, 0);
+
+ // time out for scanning devices
if (!devicesDiscovered)
{
return CA_STATUS_FAILED;
}
}
+ else
+ {
+ OIC_LOG(DEBUG, TAG, "there is a target device in the scanned devices");
+ }
return CA_STATUS_OK;
}
+
CAResult_t CALEClientSendUnicastMessageImpl(const char* address, const uint8_t* data,
- const uint32_t dataLen)
+ const uint32_t dataLen)
{
OIC_LOG_V(DEBUG, TAG, "CALEClientSendUnicastMessageImpl, address: %s, data: %p", address,
data);
}
bool isAttached = false;
- JNIEnv* env;
+ JNIEnv* env = NULL;
jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
if (JNI_OK != res)
{
CALEClientSetSendFinishFlag(false);
- CAResult_t ret = CALEClientIsThereScannedDevices();
+ CAResult_t ret = CALEClientIsThereScannedDevices(env, address);
if (CA_STATUS_OK != ret)
{
OIC_LOG(INFO, TAG, "there is no scanned device");
if (!strcmp(setAddress, address))
{
(*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
-
- // connect to gatt server
- ret = CALEClientStopScan();
- if (CA_STATUS_OK != ret)
- {
- OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
- goto error_exit;
- }
+ (*env)->DeleteLocalRef(env, jni_setAddress);
if (g_sendBuffer)
{
break;
}
(*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
+ (*env)->DeleteLocalRef(env, jni_setAddress);
}
}
// wait for finish to send data through "CALeGattServicesDiscoveredCallback"
// if there is no connection state.
+ ca_mutex_lock(g_threadMutex);
if (!g_isFinishedSendData)
{
- ca_mutex_lock(g_threadMutex);
+ OIC_LOG(DEBUG, TAG, "waiting send finish signal");
ca_cond_wait(g_threadCond, g_threadMutex);
OIC_LOG(DEBUG, TAG, "the data was sent");
- ca_mutex_unlock(g_threadMutex);
}
+ ca_mutex_unlock(g_threadMutex);
if (isAttached)
{
(*g_jvm)->DetachCurrentThread(g_jvm);
}
- // start LE Scan again
- ret = CALEClientStartScan();
- if (CA_STATUS_OK != ret)
+ ca_mutex_unlock(g_threadSendMutex);
+ OIC_LOG(INFO, TAG, "unicast - send logic has finished");
+ if (CALEClientIsValidState(address, CA_LE_SEND_STATE,
+ STATE_SEND_SUCCESS))
{
- OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
- ca_mutex_unlock(g_threadSendMutex);
- return res;
+ ret = CA_STATUS_OK;
+ }
+ else
+ {
+ ret = CA_SEND_FAILED;
}
- ca_mutex_unlock(g_threadSendMutex);
- OIC_LOG(INFO, TAG, "unicast - send success");
- return CA_STATUS_OK;
-
- // error label.
-error_exit:
-
- // start LE Scan again
- ret = CALEClientStartScan();
- if (CA_STATUS_OK != ret)
+ // reset send state
+ CAResult_t resetRet = CALEClientUpdateDeviceState(address, CA_LE_SEND_STATE,
+ STATE_SEND_NONE);
+ if (CA_STATUS_OK != resetRet)
{
- OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
- ca_mutex_unlock(g_threadSendMutex);
- if (isAttached)
- {
- (*g_jvm)->DetachCurrentThread(g_jvm);
- }
- return res;
+ OIC_LOG_V(ERROR, TAG, "CALEClientUpdateDeviceState has failed (%d)", resetRet);
+ ret = CA_SEND_FAILED;
}
+ return ret;
+
+ // error label.
+error_exit:
if (isAttached)
{
(*g_jvm)->DetachCurrentThread(g_jvm);
}
- if (g_clientErrorCallback)
- {
- g_clientErrorCallback(address, data, dataLen, CA_SEND_FAILED);
- }
ca_mutex_unlock(g_threadSendMutex);
return CA_SEND_FAILED;
}
g_sendBuffer = NULL;
}
- CAResult_t res = CALEClientIsThereScannedDevices();
+ CAResult_t res = CALEClientIsThereScannedDevices(env, NULL);
if (CA_STATUS_OK != res)
{
OIC_LOG(INFO, TAG, "there is no scanned device");
goto error_exit;
}
- // connect to gatt server
- res = CALEClientStopScan();
- if (CA_STATUS_OK != res)
- {
- OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
- ca_mutex_unlock(g_threadSendMutex);
- return res;
- }
uint32_t length = u_arraylist_length(g_deviceList);
g_targetCnt = length;
{
OIC_LOG(ERROR, TAG, "BT device - send has failed");
}
-
- jstring jni_address = CALEGetAddressFromBTDevice(env, jarrayObj);
- if (!jni_address)
- {
- OIC_LOG(ERROR, TAG, "CALEGetAddressFromBTDevice has failed");
- continue;
- }
-
- const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
- if (!address)
- {
- OIC_LOG(ERROR, TAG, "address is not available");
- continue;
- }
-
- (*env)->ReleaseStringUTFChars(env, jni_address, address);
}
OIC_LOG(DEBUG, TAG, "connection routine is finished for multicast");
// wait for finish to send data through "CALeGattServicesDiscoveredCallback"
+ ca_mutex_lock(g_threadMutex);
if (!g_isFinishedSendData)
{
- ca_mutex_lock(g_threadMutex);
+ OIC_LOG(DEBUG, TAG, "waiting send finish signal");
ca_cond_wait(g_threadCond, g_threadMutex);
- OIC_LOG(DEBUG, TAG, "the data was sent for All devices");
- ca_mutex_unlock(g_threadMutex);
- }
-
- // start LE Scan again
- res = CALEClientStartScan();
- if (CA_STATUS_OK != res)
- {
- OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
- ca_mutex_unlock(g_threadSendMutex);
- return res;
+ OIC_LOG(DEBUG, TAG, "the data was sent");
}
-
+ ca_mutex_unlock(g_threadMutex);
ca_mutex_unlock(g_threadSendMutex);
OIC_LOG(DEBUG, TAG, "OUT - CALEClientSendMulticastMessageImpl");
return CA_STATUS_OK;
error_exit:
- res = CALEClientStartScan();
- if (CA_STATUS_OK != res)
- {
- OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
- ca_mutex_unlock(g_threadSendMutex);
- return res;
- }
-
ca_mutex_unlock(g_threadSendMutex);
- OIC_LOG(DEBUG, TAG, "OUT - CALEClientSendMulticastMessageImpl");
+ OIC_LOG(ERROR, TAG, "OUT - CALEClientSendMulticastMessageImpl");
return CA_SEND_FAILED;
}
-CAResult_t CALECheckSendState(const char* address)
-{
- VERIFY_NON_NULL(address, TAG, "address is null");
-
- ca_mutex_lock(g_deviceStateListMutex);
- CALEState_t* state = CALEClientGetStateInfo(address);
- if (NULL == state)
- {
- OIC_LOG(ERROR, TAG, "state is null");
- ca_mutex_unlock(g_deviceStateListMutex);
- return CA_SEND_FAILED;
- }
-
- if (STATE_SEND_SUCCESS != state->sendState)
- {
- OIC_LOG(ERROR, TAG, "sendstate is not STATE_SEND_SUCCESS");
- ca_mutex_unlock(g_deviceStateListMutex);
- return CA_SEND_FAILED;
- }
- ca_mutex_unlock(g_deviceStateListMutex);
- return CA_STATUS_OK;
-}
-
CAResult_t CALEClientSendData(JNIEnv *env, jobject device)
{
OIC_LOG(DEBUG, TAG, "IN - CALEClientSendData");
ca_mutex_lock(g_deviceStateListMutex);
state = CALEClientGetStateInfo(address);
ca_mutex_unlock(g_deviceStateListMutex);
- (*env)->ReleaseStringUTFChars(env, jni_address, address);
}
if (!state)
// if there is gatt object in g_gattObjectList.
if (jni_address)
{
- address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
- if (!address)
- {
- OIC_LOG(ERROR, TAG, "address is not available");
- return CA_STATUS_FAILED;
- }
-
jobject gatt = CALEClientGetGattObjInList(env, address);
if (gatt)
{
}
// connection request
- jobject newGatt = CALEClientConnect(env, device, JNI_TRUE);
+ jobject newGatt = CALEClientConnect(env, device,
+ JNI_FALSE);
if (NULL == newGatt)
{
OIC_LOG(ERROR, TAG, "CALEClientConnect has failed");
}
else
{
- if (STATE_CONNECTED == state->connectedState)
+ if (CALEClientIsValidState(address, CA_LE_CONNECTION_STATE,
+ STATE_SERVICE_CONNECTED))
{
OIC_LOG(INFO, TAG, "GATT has already connected");
- if (!jni_address)
- {
- OIC_LOG(ERROR, TAG, "jni_address is not available");
- return CA_STATUS_FAILED;
- }
-
- address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
- if (!address)
- {
- OIC_LOG(ERROR, TAG, "address is not available");
- return CA_STATUS_FAILED;
- }
jobject gatt = CALEClientGetGattObjInList(env, address);
if (!gatt)
}
(*env)->ReleaseStringUTFChars(env, jni_address, address);
}
- else
+ else if(CALEClientIsValidState(address, CA_LE_CONNECTION_STATE,
+ STATE_CONNECTED))
+ {
+ OIC_LOG(INFO, TAG, "service connecting...");
+ }
+ else if(CALEClientIsValidState(address, CA_LE_CONNECTION_STATE,
+ STATE_DISCONNECTED))
{
+ OIC_LOG(INFO, TAG, "STATE_DISCONNECTED - start to connect LE");
+
+ // cancel previous connection request before connection
+ // if there is gatt object in g_gattObjectList.
+ if (jni_address)
+ {
+ jobject gatt = CALEClientGetGattObjInList(env, address);
+ if (gatt)
+ {
+ CAResult_t res = CALEClientDisconnect(env, gatt);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(INFO, TAG, "there is no gatt object");
+ }
+ }
+ (*env)->ReleaseStringUTFChars(env, jni_address, address);
+ }
+
OIC_LOG(DEBUG, TAG, "start to connect LE");
- jobject gatt = CALEClientConnect(env, device, JNI_TRUE);
+ jobject gatt = CALEClientConnect(env, device,
+ CALEClientGetFlagFromState(env, jni_address,
+ CA_LE_AUTO_CONNECT_FLAG));
+
if (NULL == gatt)
{
OIC_LOG(ERROR, TAG, "CALEClientConnect has failed");
VERIFY_NON_NULL_RET(gatt, TAG, "gatt is null", NULL);
VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
- jclass jni_cid_gattdevice_list = (*env)->FindClass(env, CLASSPATH_BT_GATT);
- if (!jni_cid_gattdevice_list)
- {
- OIC_LOG(ERROR, TAG, "jni_cid_gattdevice_list is null");
- return NULL;
- }
-
- jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice",
- "()Landroid/bluetooth/BluetoothDevice;");
+ jmethodID jni_mid_getDevice = CAGetJNIMethodID(env, CLASSPATH_BT_GATT, "getDevice",
+ "()Landroid/bluetooth/BluetoothDevice;");
if (!jni_mid_getDevice)
{
OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null");
VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
VERIFY_NON_NULL(env, TAG, "env is null");
- // get BluetoothGatt class
- OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
- jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
- if (!jni_cid_BluetoothGatt)
- {
- OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
- return CA_STATUS_FAILED;
- }
-
- jmethodID jni_mid_closeGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "close", "()V");
+ // get BluetoothGatt method
+ OIC_LOG(DEBUG, TAG, "get BluetoothGatt method");
+ jmethodID jni_mid_closeGatt = CAGetJNIMethodID(env, CLASSPATH_BT_GATT, "close", "()V");
if (!jni_mid_closeGatt)
{
OIC_LOG(ERROR, TAG, "jni_mid_closeGatt is null");
CAResult_t CALEClientStartScan()
{
- if (!g_isStartedMulticastServer)
- {
- OIC_LOG(ERROR, TAG, "server is not started yet..scan will be passed");
- return CA_STATUS_FAILED;
- }
-
if (!g_isStartedLEClient)
{
OIC_LOG(ERROR, TAG, "LE client is not started");
return CA_STATUS_FAILED;
}
- if (g_isStartedScan)
- {
- OIC_LOG(INFO, TAG, "scanning is already started");
- return CA_STATUS_OK;
- }
-
bool isAttached = false;
- JNIEnv* env;
+ JNIEnv* env = NULL;
jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
if (JNI_OK != res)
{
if (!jni_mid_getDefaultAdapter)
{
OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
return CA_STATUS_FAILED;
}
if (!jni_mid_startLeScan)
{
OIC_LOG(ERROR, TAG, "startLeScan: jni_mid_startLeScan is null");
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
return CA_STATUS_FAILED;
}
if (!jni_obj_BTAdapter)
{
OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_obj_BTAdapter is null");
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
return CA_STATUS_FAILED;
}
// call start le scan method
+ OIC_LOG(INFO, TAG, "CALL API - startLeScan");
jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter,
jni_mid_startLeScan, callback);
if (!jni_obj_startLeScan)
{
- OIC_LOG(ERROR, TAG, "startLeScan is failed");
- return CA_STATUS_FAILED;
+ OIC_LOG(INFO, TAG, "startLeScan has failed");
}
else
{
- OIC_LOG(DEBUG, TAG, "startLeScan is started");
- CALEClientSetScanFlag(true);
+ OIC_LOG(DEBUG, TAG, "LeScan has started");
}
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+ (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
return CA_STATUS_OK;
}
if (!jni_mid_getDefaultAdapter)
{
OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
return CA_STATUS_FAILED;
}
if (!jni_mid_startLeScan)
{
OIC_LOG(ERROR, TAG, "startLeScan: jni_mid_startLeScan is null");
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
return CA_STATUS_FAILED;
}
if (!jni_obj_BTAdapter)
{
OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_obj_BTAdapter is null");
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
return CA_STATUS_FAILED;
}
// call start le scan method
+ OIC_LOG(INFO, TAG, "CALL API - startLeScan (with UUID)");
jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter,
jni_mid_startLeScan, uuids, callback);
if (!jni_obj_startLeScan)
{
- OIC_LOG(ERROR, TAG, "startLeScan With UUID is failed");
- return CA_STATUS_FAILED;
+ OIC_LOG(INFO, TAG, "startLeScan has failed");
}
else
{
- OIC_LOG(DEBUG, TAG, "startLeScan With UUID is started");
- CALEClientSetScanFlag(true);
+ OIC_LOG(DEBUG, TAG, "LeScan has started");
}
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+ (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
return CA_STATUS_OK;
}
return CA_STATUS_FAILED;
}
- if (!g_isStartedScan)
- {
- OIC_LOG(INFO, TAG, "scanning is already stopped");
- return CA_STATUS_OK;
- }
-
bool isAttached = false;
- JNIEnv* env;
+ JNIEnv* env = NULL;
jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
if (JNI_OK != res)
{
OIC_LOG(ERROR, TAG, "CALEClientStopScanImpl has failed");
}
}
- else
- {
- CALEClientSetScanFlag(false);
- }
if (isAttached)
{
return ret;
}
-void CALEClientSetScanFlag(bool flag)
-{
- ca_mutex_lock(g_scanMutex);
- g_isStartedScan = flag;
- ca_mutex_unlock(g_scanMutex);
-}
-
CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback)
{
OIC_LOG(DEBUG, TAG, "CALEClientStopScanImpl");
if (!jni_mid_getDefaultAdapter)
{
OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
return CA_STATUS_FAILED;
}
if (!jni_mid_stopLeScan)
{
OIC_LOG(ERROR, TAG, "stopLeScan: jni_mid_stopLeScan is null");
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
return CA_STATUS_FAILED;
}
if (!jni_obj_BTAdapter)
{
OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
return CA_STATUS_FAILED;
}
- OIC_LOG(DEBUG, TAG, "CALL API - request to stop LE Scan");
+ OIC_LOG(INFO, TAG, "CALL API - stopLeScan");
// call start le scan method
(*env)->CallVoidMethod(env, jni_obj_BTAdapter, jni_mid_stopLeScan, callback);
if ((*env)->ExceptionCheck(env))
{
OIC_LOG(ERROR, TAG, "stopLeScan has failed");
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+ (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
return CA_STATUS_FAILED;
}
+ (*env)->DeleteLocalRef(env, jni_cid_BTAdapter);
+ (*env)->DeleteLocalRef(env, jni_obj_BTAdapter);
+ return CA_STATUS_OK;
+}
+
+CAResult_t CALEClientSetFlagToState(JNIEnv *env, jstring jni_address, jint state_idx, jboolean flag)
+{
+ OIC_LOG(DEBUG, TAG, "IN - CALEClientSetFlagToState");
+ VERIFY_NON_NULL(env, TAG, "env");
+ VERIFY_NON_NULL(jni_address, TAG, "jni_address");
+
+ ca_mutex_lock(g_deviceStateListMutex);
+
+ char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
+ if (!address)
+ {
+ OIC_LOG(ERROR, TAG, "address is not available");
+ return CA_STATUS_FAILED;
+ }
+
+ if (CALEClientIsDeviceInList(address))
+ {
+ CALEState_t* curState = CALEClientGetStateInfo(address);
+ if(!curState)
+ {
+ OIC_LOG(ERROR, TAG, "curState is null");
+ (*env)->ReleaseStringUTFChars(env, jni_address, address);
+ ca_mutex_unlock(g_deviceStateListMutex);
+ return CA_STATUS_FAILED;
+ }
+ OIC_LOG_V(INFO, TAG, "%d flag is set : %d", state_idx, flag);
+
+ switch(state_idx)
+ {
+ case CA_LE_AUTO_CONNECT_FLAG:
+ curState->autoConnectFlag = flag;
+ break;
+ case CA_LE_DESCRIPTOR_FOUND:
+ curState->isDescriptorFound = flag;
+ break;
+ default:
+ break;
+ }
+ }
+
+ (*env)->ReleaseStringUTFChars(env, jni_address, address);
+ ca_mutex_unlock(g_deviceStateListMutex);
+ OIC_LOG(DEBUG, TAG, "OUT - CALEClientSetFlagToState");
return CA_STATUS_OK;
}
+jboolean CALEClientGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx)
+{
+ OIC_LOG(DEBUG, TAG, "IN - CALEClientGetFlagFromState");
+ VERIFY_NON_NULL_RET(env, TAG, "env", false);
+ VERIFY_NON_NULL_RET(jni_address, TAG, "jni_address", false);
+
+ ca_mutex_lock(g_deviceStateListMutex);
+
+ char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
+ if (!address)
+ {
+ OIC_LOG(ERROR, TAG, "address is not available");
+ ca_mutex_unlock(g_deviceStateListMutex);
+ return JNI_FALSE;
+ }
+
+ CALEState_t* curState = CALEClientGetStateInfo(address);
+ (*env)->ReleaseStringUTFChars(env, jni_address, address);
+ if(!curState)
+ {
+ OIC_LOG(INFO, TAG, "there is no information. auto connect flag is false");
+ ca_mutex_unlock(g_deviceStateListMutex);
+ return JNI_FALSE;
+ }
+
+ jboolean ret = JNI_FALSE;
+ switch(state_idx)
+ {
+ case CA_LE_AUTO_CONNECT_FLAG:
+ ret = curState->autoConnectFlag;
+ break;
+ case CA_LE_DESCRIPTOR_FOUND:
+ ret = curState->isDescriptorFound;
+ break;
+ default:
+ break;
+ }
+ ca_mutex_unlock(g_deviceStateListMutex);
+
+ OIC_LOG_V(INFO, TAG, "%d flag is %d", state_idx, ret);
+ OIC_LOG(DEBUG, TAG, "OUT - CALEClientGetFlagFromState");
+ return ret;
+}
+
+CAResult_t CALEClientDirectConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect)
+{
+ OIC_LOG(DEBUG, TAG, "CALEClientDirectConnect");
+ VERIFY_NON_NULL(env, TAG, "env is null");
+ VERIFY_NON_NULL(bluetoothDevice, TAG, "bluetoothDevice is null");
+
+ ca_mutex_lock(g_threadSendMutex);
+
+ jstring jni_address = CALEGetAddressFromBTDevice(env, bluetoothDevice);
+ if (!jni_address)
+ {
+ OIC_LOG(ERROR, TAG, "jni_address is not available");
+ ca_mutex_unlock(g_threadSendMutex);
+ return CA_STATUS_FAILED;
+ }
+
+ const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
+ if (!address)
+ {
+ OIC_LOG(ERROR, TAG, "address is not available");
+ ca_mutex_unlock(g_threadSendMutex);
+ return CA_STATUS_FAILED;
+ }
+
+ CAResult_t res = CA_STATUS_OK;
+ if(CALEClientIsValidState(address, CA_LE_CONNECTION_STATE,
+ STATE_DISCONNECTED))
+ {
+ jobject newGatt = CALEClientConnect(env, bluetoothDevice, autoconnect);
+ if (NULL == newGatt)
+ {
+ OIC_LOG(INFO, TAG, "newGatt is not available");
+ res = CA_STATUS_FAILED;
+ }
+ }
+ ca_mutex_unlock(g_threadSendMutex);
+
+ return res;
+}
+
jobject CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect)
{
OIC_LOG(DEBUG, TAG, "CALEClientConnect");
VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
VERIFY_NON_NULL_RET(bluetoothDevice, TAG, "bluetoothDevice is null", NULL);
+ if (!g_leGattCallback)
+ {
+ OIC_LOG(INFO, TAG, "g_leGattCallback is null");
+ return NULL;
+ }
+
if (!CALEIsEnableBTAdapter(env))
{
OIC_LOG(INFO, TAG, "BT adapter is not enabled");
return NULL;
}
- // get BluetoothDevice class
- OIC_LOG(DEBUG, TAG, "get BluetoothDevice class");
- jclass jni_cid_BluetoothDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
- if (!jni_cid_BluetoothDevice)
- {
- OIC_LOG(ERROR, TAG, "bleConnect: jni_cid_BluetoothDevice is null");
- return NULL;
- }
-
- // get connectGatt method
- OIC_LOG(DEBUG, TAG, "get connectGatt method");
- jmethodID jni_mid_connectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothDevice, "connectGatt",
- "(Landroid/content/Context;ZLandroid/"
- "bluetooth/BluetoothGattCallback;)"
- "Landroid/bluetooth/BluetoothGatt;");
+ // get BluetoothDevice method
+ OIC_LOG(DEBUG, TAG, "get BluetoothDevice method");
+ jmethodID jni_mid_connectGatt = CAGetJNIMethodID(env, "android/bluetooth/BluetoothDevice",
+ "connectGatt",
+ "(Landroid/content/Context;ZLandroid/"
+ "bluetooth/BluetoothGattCallback;)"
+ "Landroid/bluetooth/BluetoothGatt;");
if (!jni_mid_connectGatt)
{
OIC_LOG(ERROR, TAG, "bleConnect: jni_mid_connectGatt is null");
VERIFY_NON_NULL(env, TAG, "env is null");
VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
- // get BluetoothGatt class
- jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
- if (!jni_cid_BluetoothGatt)
- {
- OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
- return CA_STATUS_FAILED;
- }
-
+ // get BluetoothGatt method
OIC_LOG(DEBUG, TAG, "get gatt disconnect method");
- jmethodID jni_mid_disconnectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
- "disconnect", "()V");
+ jmethodID jni_mid_disconnectGatt = CAGetJNIMethodID(env, CLASSPATH_BT_GATT,
+ "disconnect", "()V");
if (!jni_mid_disconnectGatt)
{
OIC_LOG(ERROR, TAG, "jni_mid_disconnectGatt is null");
}
// call disconnect gatt method
- OIC_LOG(DEBUG, TAG, "CALL API - request disconnect gatt");
+ OIC_LOG(INFO, TAG, "CALL API - disconnect");
(*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_disconnectGatt);
if ((*env)->ExceptionCheck(env))
{
return CA_ADAPTER_NOT_ENABLED;
}
- // get BluetoothGatt class
- OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
- jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
- if (!jni_cid_BluetoothGatt)
- {
- OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
- return CA_STATUS_FAILED;
- }
-
- OIC_LOG(DEBUG, TAG, "discovery gatt services method");
- jmethodID jni_mid_discoverServices = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
- "discoverServices", "()Z");
+ // get BluetoothGatt.discoverServices method
+ OIC_LOG(DEBUG, TAG, "get BluetoothGatt.discoverServices method");
+ jmethodID jni_mid_discoverServices = CAGetJNIMethodID(env, CLASSPATH_BT_GATT,
+ "discoverServices", "()Z");
if (!jni_mid_discoverServices)
{
OIC_LOG(ERROR, TAG, "jni_mid_discoverServices is null");
return CA_STATUS_FAILED;
}
+
// call disconnect gatt method
- OIC_LOG(DEBUG, TAG, "CALL API - request discovery gatt services");
+ OIC_LOG(INFO, TAG, "CALL API - discoverServices");
jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_discoverServices);
if (!ret)
{
VERIFY_NON_NULL_VOID(object, TAG, "object is null");
bool isAttached = false;
- JNIEnv* env;
+ JNIEnv* env = NULL;
jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
if (JNI_OK != res)
{
CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv* env, jobject gatt)
{
+ OIC_LOG(DEBUG, TAG, "CALESetValueAndWriteCharacteristic");
+
VERIFY_NON_NULL(gatt, TAG, "gatt is null");
VERIFY_NON_NULL(env, TAG, "env is null");
+ jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
+ if (!jni_address)
+ {
+ CALEClientSendFinish(env, gatt);
+ return CA_STATUS_FAILED;
+ }
+
+ const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
+ if (!address)
+ {
+ CALEClientSendFinish(env, gatt);
+ return CA_STATUS_FAILED;
+ }
+
+ ca_mutex_lock(g_threadSendStateMutex);
+
+ if (CALEClientIsValidState(address, CA_LE_SEND_STATE, STATE_SENDING))
+ {
+ OIC_LOG(INFO, TAG, "current state is SENDING");
+ (*env)->ReleaseStringUTFChars(env, jni_address, address);
+ ca_mutex_unlock(g_threadSendStateMutex);
+ return CA_STATUS_OK;
+ }
+
+ if (CA_STATUS_OK != CALEClientUpdateDeviceState(address, CA_LE_SEND_STATE,
+ STATE_SENDING))
+ {
+ OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
+ (*env)->ReleaseStringUTFChars(env, jni_address, address);
+ CALEClientSendFinish(env, gatt);
+ ca_mutex_unlock(g_threadSendStateMutex);
+ return CA_STATUS_FAILED;
+ }
+
+ (*env)->ReleaseStringUTFChars(env, jni_address, address);
+
+ ca_mutex_unlock(g_threadSendStateMutex);
+
// send data
jobject jni_obj_character = CALEClientCreateGattCharacteristic(env, gatt, g_sendBuffer);
if (!jni_obj_character)
g_isSignalSetFlag = false;
ca_mutex_unlock(g_threadWriteCharacteristicMutex);
+ CALEClientUpdateSendCnt(env);
+
OIC_LOG(INFO, TAG, "writeCharacteristic success!!");
return CA_STATUS_OK;
}
return CA_STATUS_FAILED;
}
- // get BluetoothGatt class
- OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
- jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
- if (!jni_cid_BluetoothGatt)
- {
- OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
- return CA_STATUS_FAILED;
- }
-
+ // get BluetoothGatt.write characteristic method
OIC_LOG(DEBUG, TAG, "write characteristic method");
- jmethodID jni_mid_writeCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
- "writeCharacteristic",
- "(Landroid/bluetooth/"
- "BluetoothGattCharacteristic;)Z");
+ jmethodID jni_mid_writeCharacteristic = CAGetJNIMethodID(env, CLASSPATH_BT_GATT,
+ "writeCharacteristic",
+ "(Landroid/bluetooth/"
+ "BluetoothGattCharacteristic;)Z");
if (!jni_mid_writeCharacteristic)
{
OIC_LOG(ERROR, TAG, "jni_mid_writeCharacteristic is null");
}
// call disconnect gatt method
- OIC_LOG(DEBUG, TAG, "CALL API - request to write gatt characteristic");
+ OIC_LOG(INFO, TAG, "CALL API - writeCharacteristic");
jboolean ret = (jboolean)(*env)->CallBooleanMethod(env, bluetoothGatt,
jni_mid_writeCharacteristic,
gattCharacteristic);
return CA_STATUS_FAILED;
}
- jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
- if (!jni_cid_BluetoothGatt)
- {
- OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
- return CA_STATUS_FAILED;
- }
-
jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
if (!jni_uuid)
{
}
OIC_LOG(DEBUG, TAG, "read characteristic method");
- jmethodID jni_mid_readCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
- "readCharacteristic",
- "(Landroid/bluetooth/"
- "BluetoothGattCharacteristic;)Z");
+ jmethodID jni_mid_readCharacteristic = CAGetJNIMethodID(env, CLASSPATH_BT_GATT,
+ "readCharacteristic",
+ "(Landroid/bluetooth/"
+ "BluetoothGattCharacteristic;)Z");
if (!jni_mid_readCharacteristic)
{
OIC_LOG(ERROR, TAG, "jni_mid_readCharacteristic is null");
}
// call disconnect gatt method
- OIC_LOG(DEBUG, TAG, "CALL API - request to read gatt characteristic");
+ OIC_LOG(INFO, TAG, "CALL API - readCharacteristic");
jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_readCharacteristic,
jni_obj_GattCharacteristic);
if (ret)
return CA_ADAPTER_NOT_ENABLED;
}
- // get BluetoothGatt class
+ // get BluetoothGatt.setCharacteristicNotification method
OIC_LOG(DEBUG, TAG, "CALEClientSetCharacteristicNotification");
- jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
- if (!jni_cid_BluetoothGatt)
- {
- OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
- return CA_STATUS_FAILED;
- }
-
- // set Characteristic Notification
- jmethodID jni_mid_setNotification = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
- "setCharacteristicNotification",
- "(Landroid/bluetooth/"
- "BluetoothGattCharacteristic;Z)Z");
+ jmethodID jni_mid_setNotification = CAGetJNIMethodID(env, CLASSPATH_BT_GATT,
+ "setCharacteristicNotification",
+ "(Landroid/bluetooth/"
+ "BluetoothGattCharacteristic;Z)Z");
if (!jni_mid_setNotification)
{
OIC_LOG(ERROR, TAG, "jni_mid_getService is null");
return CA_STATUS_FAILED;
}
+ OIC_LOG(INFO, TAG, "CALL API - setCharacteristicNotification");
jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_setNotification,
characteristic, JNI_TRUE);
if (JNI_TRUE == ret)
{
- OIC_LOG(DEBUG, TAG, "CALL API - setCharacteristicNotification success");
+ OIC_LOG(DEBUG, TAG, "setCharacteristicNotification success");
}
else
{
- OIC_LOG(ERROR, TAG, "CALL API - setCharacteristicNotification has failed");
+ OIC_LOG(ERROR, TAG, "setCharacteristicNotification has failed");
return CA_STATUS_FAILED;
}
return NULL;
}
- // get BluetoothGatt class
- OIC_LOG(DEBUG, TAG, "CALEClientGetGattService");
- jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
- if (!jni_cid_BluetoothGatt)
- {
- OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
- return NULL;
- }
-
- jmethodID jni_mid_getService = (*env)->GetMethodID(
- env, jni_cid_BluetoothGatt, "getService",
- "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattService;");
+ // get BluetoothGatt.getService method
+ OIC_LOG(DEBUG, TAG, "BluetoothGatt.getService");
+ jmethodID jni_mid_getService = CAGetJNIMethodID(env, CLASSPATH_BT_GATT,
+ "getService",
+ "(Ljava/util/UUID;)Landroid/bluetooth/"
+ "BluetoothGattService;");
if (!jni_mid_getService)
{
OIC_LOG(ERROR, TAG, "jni_mid_getService is null");
return NULL;
}
- // get bluetooth gatt service class
- jclass jni_cid_BluetoothGattService = (*env)->FindClass(
- env, "android/bluetooth/BluetoothGattService");
- if (!jni_cid_BluetoothGattService)
- {
- OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGattService is null");
- return NULL;
- }
-
- OIC_LOG(DEBUG, TAG, "get gatt getCharacteristic method");
- jmethodID jni_mid_getCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGattService,
- "getCharacteristic",
- "(Ljava/util/UUID;)"
- "Landroid/bluetooth/"
- "BluetoothGattCharacteristic;");
+ // get bluetooth gatt service method
+ jmethodID jni_mid_getCharacteristic = CAGetJNIMethodID(env, "android/bluetooth/"
+ "BluetoothGattService",
+ "getCharacteristic",
+ "(Ljava/util/UUID;)"
+ "Landroid/bluetooth/"
+ "BluetoothGattCharacteristic;");
if (!jni_mid_getCharacteristic)
{
OIC_LOG(ERROR, TAG, "jni_mid_getCharacteristic is null");
return NULL;
}
- jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
- "BluetoothGattCharacteristic");
- if (!jni_cid_BTGattCharacteristic)
- {
- OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
- return NULL;
- }
-
- OIC_LOG(DEBUG, TAG, "get value in Characteristic");
- jmethodID jni_mid_getValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "getValue",
- "()[B");
+ jmethodID jni_mid_getValue = CAGetJNIMethodID(env, "android/bluetooth/"
+ "BluetoothGattCharacteristic",
+ "getValue", "()[B");
if (!jni_mid_getValue)
{
OIC_LOG(ERROR, TAG, "jni_mid_getValue is null");
}
bool isAttached = false;
- JNIEnv* env;
+ JNIEnv* env = NULL;
jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
if (JNI_OK != res)
{
}
OIC_LOG(DEBUG, TAG, "CALEClientSetUUIDToDescriptor");
- jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
- "BluetoothGattCharacteristic");
- if (!jni_cid_BTGattCharacteristic)
- {
- OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
- return CA_STATUS_FAILED;
- }
-
- OIC_LOG(DEBUG, TAG, "set value in Characteristic");
- jmethodID jni_mid_getDescriptor = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic,
- "getDescriptor",
- "(Ljava/util/UUID;)Landroid/bluetooth/"
- "BluetoothGattDescriptor;");
+ jmethodID jni_mid_getDescriptor = CAGetJNIMethodID(env, "android/bluetooth/"
+ "BluetoothGattCharacteristic",
+ "getDescriptor",
+ "(Ljava/util/UUID;)Landroid/bluetooth/"
+ "BluetoothGattDescriptor;");
if (!jni_mid_getDescriptor)
{
OIC_LOG(ERROR, TAG, "jni_mid_getDescriptor is null");
return CA_STATUS_FAILED;
}
- jclass jni_cid_gatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGatt");
- if (!jni_cid_gatt)
- {
- OIC_LOG(ERROR, TAG, "jni_cid_gatt is null");
- return CA_STATUS_FAILED;
- }
-
- OIC_LOG(DEBUG, TAG, "write Descriptor in gatt object");
- jmethodID jni_mid_writeDescriptor = (*env)->GetMethodID(env, jni_cid_gatt, "writeDescriptor",
- "(Landroid/bluetooth/"
- "BluetoothGattDescriptor;)Z");
+ jmethodID jni_mid_writeDescriptor = CAGetJNIMethodID(env, "android/bluetooth/BluetoothGatt",
+ "writeDescriptor",
+ "(Landroid/bluetooth/"
+ "BluetoothGattDescriptor;)Z");
if (!jni_mid_writeDescriptor)
{
OIC_LOG(ERROR, TAG, "jni_mid_writeDescriptor is null");
return CA_STATUS_FAILED;
}
- OIC_LOG(DEBUG, TAG, "request to write descriptor");
+ OIC_LOG(INFO, TAG, "CALL API - writeDescriptor");
jboolean jni_ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_writeDescriptor,
jni_obj_descriptor);
if (jni_ret)
if (!remoteAddress)
{
OIC_LOG(ERROR, TAG, "remoteAddress is null");
+ (*env)->DeleteLocalRef(env, jni_remoteAddress);
ca_mutex_unlock(g_deviceListMutex);
return CA_STATUS_FAILED;
}
jobject gdevice = (*env)->NewGlobalRef(env, device);
u_arraylist_add(g_deviceList, gdevice);
ca_cond_signal(g_deviceDescCond);
- OIC_LOG_V(DEBUG, TAG, "Added this BT Device[%s] in the List", remoteAddress);
+ OIC_LOG_V(DEBUG, TAG, "Added a new BT Device in deviceList [%s]", remoteAddress);
}
(*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
+ (*env)->DeleteLocalRef(env, jni_remoteAddress);
ca_mutex_unlock(g_deviceListMutex);
bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char* remoteAddress)
{
- VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
+ VERIFY_NON_NULL_RET(env, TAG, "env is null", true);
VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", true);
if (!g_deviceList)
if (!setAddress)
{
OIC_LOG(ERROR, TAG, "setAddress is null");
+ (*env)->DeleteLocalRef(env, jni_setAddress);
return true;
}
if (!strcmp(remoteAddress, setAddress))
{
(*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
+ (*env)->DeleteLocalRef(env, jni_setAddress);
return true;
}
(*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
+ (*env)->DeleteLocalRef(env, jni_setAddress);
}
- OIC_LOG(DEBUG, TAG, "there are no the device in list. we can add");
+ OIC_LOG_V(DEBUG, TAG, "[%s] doesn't exist in scanned device list", remoteAddress);
return false;
}
continue;
}
(*env)->DeleteGlobalRef(env, jarrayObj);
+ jarrayObj = NULL;
}
OICFree(g_deviceList);
{
OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
(*env)->DeleteGlobalRef(env, jarrayObj);
+ jarrayObj = NULL;
(*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
(*env)->ReleaseStringUTFChars(env, address, remoteAddress);
CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt)
{
- OIC_LOG(INFO, TAG, "CALEClientAddGattobjToList");
+ OIC_LOG(DEBUG, TAG, "CALEClientAddGattobjToList");
VERIFY_NON_NULL(env, TAG, "env is null");
VERIFY_NON_NULL(gatt, TAG, "gatt is null");
return CA_STATUS_FAILED;
}
- OIC_LOG_V(INFO, TAG, "remote address : %s", remoteAddress);
+ OIC_LOG_V(DEBUG, TAG, "remote address : %s", remoteAddress);
if (!CALEClientIsGattObjInList(env, remoteAddress))
{
jobject newGatt = (*env)->NewGlobalRef(env, gatt);
u_arraylist_add(g_gattObjectList, newGatt);
- OIC_LOG(INFO, TAG, "Set GATT Object to Array as Element");
+ OIC_LOG(INFO, TAG, "added a newGatt object to gattObjectList");
}
(*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
continue;
}
(*env)->DeleteGlobalRef(env, jarrayObj);
+ jarrayObj = NULL;
}
OICFree(g_gattObjectList);
{
OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
(*env)->DeleteGlobalRef(env, jarrayObj);
+ jarrayObj = NULL;
(*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
(*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
{
OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
(*env)->DeleteGlobalRef(env, jarrayObj);
-
+ jarrayObj = NULL;
(*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
(*env)->ReleaseStringUTFChars(env, addr, remoteAddress);
if (NULL == u_arraylist_remove(g_gattObjectList, index))
jstring CALEClientGetLEAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice)
{
- OIC_LOG(DEBUG, TAG, "IN - CALEClientGetLEAddressFromBTDevice");
+ OIC_LOG(DEBUG, TAG, "CALEClientGetLEAddressFromBTDevice");
VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
VERIFY_NON_NULL_RET(bluetoothDevice, TAG, "bluetoothDevice", NULL);
}
// get method ID of getDevice()
- jclass jni_cid_gattdevice_list = (*env)->FindClass(env, CLASSPATH_BT_GATT);
- if (!jni_cid_gattdevice_list)
- {
- OIC_LOG(ERROR, TAG, "jni_cid_gattdevice_list is null");
- (*env)->ReleaseStringUTFChars(env, jni_btTargetAddress, targetAddress);
- return NULL;
- }
-
- jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice",
- METHODID_BT_DEVICE);
+ jmethodID jni_mid_getDevice = CAGetJNIMethodID(env, CLASSPATH_BT_GATT,
+ "getDevice", METHODID_BT_DEVICE);
if (!jni_mid_getDevice)
{
OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null");
return NULL;
}
+ ca_mutex_lock(g_gattObjectMutex);
+
size_t length = u_arraylist_length(g_gattObjectList);
+ OIC_LOG_V(DEBUG, TAG, "length of gattObjectList : %d", length);
+ OIC_LOG_V(DEBUG, TAG, "targetAddress : %s", targetAddress);
+
for (size_t index = 0; index < length; index++)
{
jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
if (!jarrayObj)
{
+ ca_mutex_unlock(g_gattObjectMutex);
OIC_LOG(ERROR, TAG, "jarrayObj is null");
(*env)->ReleaseStringUTFChars(env, jni_btTargetAddress, targetAddress);
- return CA_STATUS_FAILED;
+ return NULL;
}
- OIC_LOG(DEBUG, TAG, "CALL API - bluetoothGatt.getDevice()");
jobject jni_obj_device = (*env)->CallObjectMethod(env, jarrayObj, jni_mid_getDevice);
if (!jni_obj_device)
{
+ ca_mutex_unlock(g_gattObjectMutex);
OIC_LOG(ERROR, TAG, "jni_obj_device is null");
(*env)->ReleaseStringUTFChars(env, jni_btTargetAddress, targetAddress);
return NULL;
jstring jni_btAddress = CALEGetAddressFromBTDevice(env, jni_obj_device);
if (!jni_btAddress)
{
+ ca_mutex_unlock(g_gattObjectMutex);
OIC_LOG(ERROR, TAG, "CALEGetAddressFromBTDevice has failed");
(*env)->ReleaseStringUTFChars(env, jni_btTargetAddress, targetAddress);
return NULL;
const char* btAddress = (*env)->GetStringUTFChars(env, jni_btAddress, NULL);
if (!btAddress)
{
+ ca_mutex_unlock(g_gattObjectMutex);
OIC_LOG(ERROR, TAG, "btAddress is not available");
(*env)->ReleaseStringUTFChars(env, jni_btTargetAddress, targetAddress);
return NULL;
}
- OIC_LOG_V(DEBUG, TAG, "targetAddress : %s", targetAddress);
- OIC_LOG_V(DEBUG, TAG, "btAddress : %s", btAddress);
+ OIC_LOG_V(DEBUG, TAG, "btAddress : %s (idx: %d)", btAddress, index);
if (!strcmp(targetAddress, btAddress))
{
OIC_LOG(DEBUG, TAG, "Found Gatt object from BT device");
{
OIC_LOG(ERROR, TAG, "jni_LEAddress is null");
}
+ ca_mutex_unlock(g_gattObjectMutex);
(*env)->ReleaseStringUTFChars(env, jni_btTargetAddress, targetAddress);
(*env)->ReleaseStringUTFChars(env, jni_btAddress, btAddress);
(*env)->DeleteLocalRef(env, jni_btAddress);
(*env)->DeleteLocalRef(env, jni_btAddress);
(*env)->DeleteLocalRef(env, jni_obj_device);
}
+ ca_mutex_unlock(g_gattObjectMutex);
- OIC_LOG(DEBUG, TAG, "OUT - CALEClientGetLEAddressFromBTDevice");
+ (*env)->ReleaseStringUTFChars(env, jni_btTargetAddress, targetAddress);
+ OIC_LOG_V(DEBUG, TAG, "[%s] doesn't exist in gattObjectList", targetAddress);
return NULL;
}
* BT State List
*/
-CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
- uint16_t notificationState, uint16_t sendState)
+CAResult_t CALEClientUpdateDeviceState(const char* address, uint16_t state_type,
+ uint16_t target_state)
{
VERIFY_NON_NULL(address, TAG, "address is null");
-
- CALEState_t *newstate = (CALEState_t*) OICMalloc(sizeof(CALEState_t));
- if (!newstate)
- {
- OIC_LOG(ERROR, TAG, "out of memory");
- return CA_MEMORY_ALLOC_FAILED;
- }
-
- if (strlen(address) > CA_MACADDR_SIZE)
- {
- OIC_LOG(ERROR, TAG, "address is not proper");
- OICFree(newstate);
- return CA_STATUS_FAILED;
- }
-
- OICStrcpy(newstate->address, sizeof(newstate->address), address);
- newstate->connectedState = connectedState;
- newstate->notificationState = notificationState;
- newstate->sendState = sendState;
- return CALEClientAddDeviceStateToList(newstate);
-}
-
-CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state)
-{
- VERIFY_NON_NULL(state, TAG, "state is null");
-
- ca_mutex_lock(g_deviceStateListMutex);
+ VERIFY_NON_NULL(address, TAG, "state_type is null");
+ VERIFY_NON_NULL(address, TAG, "target_state is null");
if (!g_deviceStateList)
{
OIC_LOG(ERROR, TAG, "gdevice_list is null");
- ca_mutex_unlock(g_deviceStateListMutex);
return CA_STATUS_FAILED;
}
- if (CALEClientIsDeviceInList(state->address))
+ ca_mutex_lock(g_deviceStateListMutex);
+
+ if (CALEClientIsDeviceInList(address))
{
- CALEState_t* curState = CALEClientGetStateInfo(state->address);
+ CALEState_t* curState = CALEClientGetStateInfo(address);
if(!curState)
{
OIC_LOG(ERROR, TAG, "curState is null");
return CA_STATUS_FAILED;
}
- if (STATE_CHARACTER_NO_CHANGE == state->notificationState)
+ switch(state_type)
+ {
+ case CA_LE_CONNECTION_STATE:
+ curState->connectedState = target_state;
+ break;
+ case CA_LE_SEND_STATE:
+ curState->sendState = target_state;
+ break;
+ default:
+ break;
+ }
+ OIC_LOG_V(INFO, TAG, "update state - addr : %s, conn : %d, send : %d, ACFlag : %d",
+ curState->address, curState->connectedState, curState->sendState,
+ curState->autoConnectFlag);
+ }
+ else /** state is added newly **/
+ {
+ if (strlen(address) > CA_MACADDR_SIZE)
{
- state->notificationState = curState->notificationState;
+ OIC_LOG(ERROR, TAG, "address is not proper");
+ ca_mutex_unlock(g_deviceStateListMutex);
+ return CA_STATUS_INVALID_PARAM;
}
- // delete previous state for update new state
- CAResult_t res = CALEClientRemoveDeviceState(state->address);
- if (CA_STATUS_OK != res)
+ CALEState_t *newstate = (CALEState_t*) OICCalloc(1, sizeof(*newstate));
+ if (!newstate)
{
- OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceState has failed");
+ OIC_LOG(ERROR, TAG, "out of memory");
ca_mutex_unlock(g_deviceStateListMutex);
- return res;
+ return CA_MEMORY_ALLOC_FAILED;
}
- }
- u_arraylist_add(g_deviceStateList, state); // update new state
- OIC_LOG_V(INFO, TAG, "Set State Info to List : %d, %d, %s",
- state->connectedState, state->notificationState, state->address);
+ OICStrcpy(newstate->address, sizeof(newstate->address), address);
+
+ switch(state_type)
+ {
+ case CA_LE_CONNECTION_STATE:
+ newstate->connectedState = target_state;
+ newstate->sendState = STATE_SEND_NONE;
+ break;
+ case CA_LE_SEND_STATE:
+ newstate->connectedState = STATE_DISCONNECTED;
+ newstate->sendState = target_state;
+ break;
+ default:
+ break;
+ }
+ OIC_LOG_V(INFO, TAG, "add a new state to List - addr : %s, "
+ "conn : %d, send : %d, ACFlag : %d",
+ newstate->address, newstate->connectedState, newstate->sendState,
+ newstate->autoConnectFlag);
+ u_arraylist_add(g_deviceStateList, newstate); // update new state
+ }
ca_mutex_unlock(g_deviceStateListMutex);
+
return CA_STATUS_OK;
}
return CA_STATUS_OK;
}
-CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress)
+CAResult_t CALEClientResetDeviceStateForAll()
{
- OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceState");
- VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
+ OIC_LOG(DEBUG, TAG, "CALEClientResetDeviceStateForAll");
+ ca_mutex_lock(g_deviceStateListMutex);
if (!g_deviceStateList)
{
OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
+ ca_mutex_unlock(g_deviceStateListMutex);
return CA_STATUS_FAILED;
}
- uint32_t length = u_arraylist_length(g_deviceStateList);
- for (uint32_t index = 0; index < length; index++)
+ size_t length = u_arraylist_length(g_deviceStateList);
+ for (size_t index = 0; index < length; index++)
{
CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
if (!state)
{
- OIC_LOG(ERROR, TAG, "CALEState_t object is null");
+ OIC_LOG(ERROR, TAG, "jarrayObj is null");
continue;
}
- if (!strcmp(state->address, remoteAddress))
+ // autoConnectFlag value will be not changed,
+ // since it has reset only termination case.
+ state->connectedState = STATE_DISCONNECTED;
+ state->sendState = STATE_SEND_NONE;
+ }
+ ca_mutex_unlock(g_deviceStateListMutex);
+
+ return CA_STATUS_OK;
+}
+
+CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress)
+{
+ OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceState");
+ VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
+
+ if (!g_deviceStateList)
+ {
+ OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
+ return CA_STATUS_FAILED;
+ }
+
+ uint32_t length = u_arraylist_length(g_deviceStateList);
+ for (uint32_t index = 0; index < length; index++)
+ {
+ CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
+ if (!state)
+ {
+ OIC_LOG(ERROR, TAG, "CALEState_t object is null");
+ continue;
+ }
+
+ if (!strcmp(state->address, remoteAddress))
{
OIC_LOG_V(DEBUG, TAG, "remove state : %s", state->address);
CALEState_t* CALEClientGetStateInfo(const char* remoteAddress)
{
- OIC_LOG(DEBUG, TAG, "CALEClientGetStateInfo");
VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
if (!g_deviceStateList)
}
uint32_t length = u_arraylist_length(g_deviceStateList);
- OIC_LOG_V(DEBUG, TAG, "CALEClientGetStateInfo : %d", length);
+ OIC_LOG_V(DEBUG, TAG, "length of deviceStateList : %d", length);
+ OIC_LOG_V(DEBUG, TAG, "target address : %s", remoteAddress);
for (uint32_t index = 0; index < length; index++)
{
continue;
}
- OIC_LOG_V(DEBUG, TAG, "target address : %s", remoteAddress);
- OIC_LOG_V(DEBUG, TAG, "state address : %s", state->address);
+ OIC_LOG_V(DEBUG, TAG, "state address : %s (idx: %d)", state->address, index);
if (!strcmp(state->address, remoteAddress))
{
- OIC_LOG_V(DEBUG, TAG, "get state : %s", remoteAddress);
+ OIC_LOG(DEBUG, TAG, "found state");
return state;
}
}
+
+ OIC_LOG_V(DEBUG, TAG, "[%s] doesn't exist in deviceStateList", remoteAddress);
return NULL;
}
-bool CALEClientIsConnectedDevice(const char* remoteAddress)
+bool CALEClientIsValidState(const char* remoteAddress, uint16_t state_type,
+ uint16_t target_state)
{
- OIC_LOG(DEBUG, TAG, "CALEClientIsConnectedDevice");
+ OIC_LOG_V(DEBUG, TAG, "CALEClientIsValidState : type[%d], target state[%d]",
+ state_type, target_state);
VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
ca_mutex_lock(g_deviceStateListMutex);
return false;
}
- uint32_t length = u_arraylist_length(g_deviceStateList);
- for (uint32_t index = 0; index < length; index++)
+ CALEState_t* state = CALEClientGetStateInfo(remoteAddress);
+ if (NULL == state)
{
- CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
- if (!state)
- {
- OIC_LOG(ERROR, TAG, "CALEState_t object is null");
- continue;
- }
-
- if (!strcmp(state->address, remoteAddress))
- {
- OIC_LOG(DEBUG, TAG, "check whether it is connected or not");
-
- if (STATE_CONNECTED == state->connectedState)
- {
- ca_mutex_unlock(g_deviceStateListMutex);
- return true;
- }
- else
- {
- ca_mutex_unlock(g_deviceStateListMutex);
- return false;
- }
- }
+ OIC_LOG(ERROR, TAG, "state is null");
+ ca_mutex_unlock(g_deviceStateListMutex);
+ return false;
}
- ca_mutex_unlock(g_deviceStateListMutex);
- return false;
-}
-bool CALEClientIsSetCharacteristic(const char* remoteAddress)
-{
- OIC_LOG(DEBUG, TAG, "CALEClientIsSetCharacteristic");
- VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
+ uint16_t curValue = 0;
+ switch(state_type)
+ {
+ case CA_LE_CONNECTION_STATE:
+ curValue = state->connectedState;
+ break;
+ case CA_LE_SEND_STATE:
+ curValue = state->sendState;
+ break;
+ default:
+ break;
+ }
- ca_mutex_lock(g_deviceStateListMutex);
- if (!g_deviceStateList)
+ if (target_state == curValue)
{
- OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
ca_mutex_unlock(g_deviceStateListMutex);
- return false;
+ return true;
}
-
- uint32_t length = u_arraylist_length(g_deviceStateList);
- for (uint32_t index = 0; index < length; index++)
+ else
{
- CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
- if (!state)
- {
- OIC_LOG(ERROR, TAG, "CALEState_t object is null");
- continue;
- }
-
- if (!strcmp(state->address, remoteAddress))
- {
- OIC_LOG_V(DEBUG, TAG, "check whether it was set or not:%d", state->notificationState);
-
- if (STATE_CHARACTER_SET == state->notificationState)
- {
- ca_mutex_unlock(g_deviceStateListMutex);
- return true;
- }
- else
- {
- ca_mutex_unlock(g_deviceStateListMutex);
- return false;
- }
- }
+ ca_mutex_unlock(g_deviceStateListMutex);
+ return false;
}
ca_mutex_unlock(g_deviceStateListMutex);
*/
void CALEClientUpdateSendCnt(JNIEnv *env)
{
+ OIC_LOG(DEBUG, TAG, "CALEClientUpdateSendCnt");
+
VERIFY_NON_NULL_VOID(env, TAG, "env is null");
// mutex lock
ca_mutex_lock(g_threadMutex);
}
}
- if (NULL == g_scanMutex)
+ if (NULL == g_threadWriteCharacteristicMutex)
+ {
+ g_threadWriteCharacteristicMutex = ca_mutex_new();
+ if (NULL == g_threadWriteCharacteristicMutex)
+ {
+ OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
+ return CA_STATUS_FAILED;
+ }
+ }
+
+ if (NULL == g_deviceScanRetryDelayMutex)
{
- g_scanMutex = ca_mutex_new();
- if (NULL == g_scanMutex)
+ g_deviceScanRetryDelayMutex = ca_mutex_new();
+ if (NULL == g_deviceScanRetryDelayMutex)
{
OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
return CA_STATUS_FAILED;
}
}
- if (NULL == g_threadWriteCharacteristicMutex)
+ if (NULL == g_threadSendStateMutex)
{
- g_threadWriteCharacteristicMutex = ca_mutex_new();
- if (NULL == g_threadWriteCharacteristicMutex)
+ g_threadSendStateMutex = ca_mutex_new();
+ if (NULL == g_threadSendStateMutex)
+ {
+ OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
+ return CA_STATUS_FAILED;
+ }
+ }
+
+ if (NULL == g_threadScanIntervalMutex)
+ {
+ g_threadScanIntervalMutex = ca_mutex_new();
+ if (NULL == g_threadScanIntervalMutex)
{
OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
return CA_STATUS_FAILED;
ca_mutex_free(g_SendFinishMutex);
g_SendFinishMutex = NULL;
- ca_mutex_free(g_scanMutex);
- g_scanMutex = NULL;
-
ca_mutex_free(g_threadWriteCharacteristicMutex);
g_threadWriteCharacteristicMutex = NULL;
+
+ ca_mutex_free(g_deviceScanRetryDelayMutex);
+ g_deviceScanRetryDelayMutex = NULL;
+
+ ca_mutex_free(g_threadSendStateMutex);
+ g_threadSendStateMutex = NULL;
+
+ ca_mutex_free(g_threadScanIntervalMutex);
+ g_threadScanIntervalMutex = NULL;
}
void CALEClientSetSendFinishFlag(bool flag)
CAResult_t CAStartLEGattClient()
{
- CAResult_t res = CALEClientStartMulticastServer();
- if (CA_STATUS_OK != res)
+ // init mutex for send logic
+ if (!g_deviceDescCond)
{
- OIC_LOG(ERROR, TAG, "CALEClientStartMulticastServer has failed");
+ g_deviceDescCond = ca_cond_new();
}
- else
+
+ if (!g_threadCond)
{
- g_isStartedLEClient = true;
+ g_threadCond = ca_cond_new();
}
- return res;
+ if (!g_threadWriteCharacteristicCond)
+ {
+ g_threadWriteCharacteristicCond = ca_cond_new();
+ }
+
+ if (!g_threadScanIntervalCond)
+ {
+ g_threadScanIntervalCond = ca_cond_new();
+ }
+
+ CAResult_t ret = CALEClientStartScanWithInterval();
+ if (CA_STATUS_OK != ret)
+ {
+ OIC_LOG(ERROR, TAG, "CALEClientStartScanWithInterval has failed");
+ return ret;
+ }
+
+ g_isStartedLEClient = true;
+ return CA_STATUS_OK;
}
void CAStopLEGattClient()
}
bool isAttached = false;
- JNIEnv* env;
+ JNIEnv* env = NULL;
jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
if (JNI_OK != res)
{
OIC_LOG(ERROR, TAG, "CALEClientDisconnectAll has failed");
}
- ret = CALEClientStopScan();
- if(CA_STATUS_OK != ret)
- {
- OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
- }
+ CALEClientStopScanWithInterval();
ca_mutex_lock(g_threadMutex);
+ OIC_LOG(DEBUG, TAG, "signal - connection cond");
ca_cond_signal(g_threadCond);
+ CALEClientSetSendFinishFlag(true);
ca_mutex_unlock(g_threadMutex);
ca_mutex_lock(g_threadWriteCharacteristicMutex);
+ OIC_LOG(DEBUG, TAG, "signal - WriteCharacteristic cond");
ca_cond_signal(g_threadWriteCharacteristicCond);
ca_mutex_unlock(g_threadWriteCharacteristicMutex);
+ ca_mutex_lock(g_deviceScanRetryDelayMutex);
+ OIC_LOG(DEBUG, TAG, "signal - delay cond");
+ ca_cond_signal(g_deviceScanRetryDelayCond);
+ ca_mutex_unlock(g_deviceScanRetryDelayMutex);
+
+ ca_mutex_lock(g_threadScanIntervalMutex);
+ OIC_LOG(DEBUG, TAG, "signal - delay cond");
+ ca_cond_signal(g_threadScanIntervalCond);
+ ca_mutex_unlock(g_threadScanIntervalMutex);
+
+ ca_cond_free(g_deviceDescCond);
+ ca_cond_free(g_threadCond);
+ ca_cond_free(g_threadWriteCharacteristicCond);
+ ca_cond_free(g_deviceScanRetryDelayCond);
+ ca_cond_free(g_threadScanIntervalCond);
+
+ g_deviceDescCond = NULL;
+ g_threadCond = NULL;
+ g_threadWriteCharacteristicCond = NULL;
+ g_deviceScanRetryDelayCond = NULL;
+ g_threadScanIntervalCond = NULL;
+
if (isAttached)
{
(*g_jvm)->DetachCurrentThread(g_jvm);
CALEClientTerminate();
}
-CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, const uint8_t *data,
- uint32_t dataLen, CALETransferType_t type,
- int32_t position)
+CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, const uint8_t *data,
+ uint32_t dataLen, CALETransferType_t type,
+ int32_t position)
{
OIC_LOG(DEBUG, TAG, "call CALEClientSendUnicastMessage");
VERIFY_NON_NULL(data, TAG, "data is null");
*/
JNIEXPORT void JNICALL
Java_org_iotivity_ca_CaLeClientInterface_caLeGattConnectionStateChangeCallback(JNIEnv *env,
- jobject obj,
- jobject gatt,
- jint status,
- jint newstate)
+ jobject obj,
+ jobject gatt,
+ jint status,
+ jint newstate)
{
- OIC_LOG_V(DEBUG, TAG, "CALeGattConnectionStateChangeCallback - status %d, newstate %d", status,
- newstate);
+ OIC_LOG_V(INFO, TAG, "CALeGattConnectionStateChangeCallback - status %d, newstate %d", status,
+ newstate);
VERIFY_NON_NULL_VOID(env, TAG, "env is null");
VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
jint state_connected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE, "STATE_CONNECTED");
- jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE, "STATE_DISCONNECTED");
- jint gatt_success = CALEGetConstantsValue(env, CLASSPATH_BT_GATT, "GATT_SUCCESS");
- if (gatt_success == status && state_connected == newstate) // le connected
+ jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
+ if (!jni_address)
{
- jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
- if (!jni_address)
- {
- goto error_exit;
- }
+ OIC_LOG(ERROR, TAG, "CALEClientGetAddressFromGattObj has failed");
+ goto error_exit;
+ }
- const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
- if (address)
+ const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
+ if (!address)
+ {
+ OIC_LOG(ERROR, TAG, "address is null");
+ goto error_exit;
+ }
+ OIC_LOG_V(DEBUG, TAG, "CALeGattConnectionStateChangeCallback - address [%s]", address);
+
+ if (state_connected == newstate)
+ {
+ OIC_LOG(DEBUG, TAG, "LE is connected");
+ if (GATT_SUCCESS == status)
{
- CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED,
- STATE_CHARACTER_NO_CHANGE,
- STATE_SEND_NONE);
+ CAResult_t res = CALEClientUpdateDeviceState(address,
+ CA_LE_CONNECTION_STATE,
+ STATE_CONNECTED);
+ (*env)->ReleaseStringUTFChars(env, jni_address, address);
if (CA_STATUS_OK != res)
{
OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
- (*env)->ReleaseStringUTFChars(env, jni_address, address);
goto error_exit;
}
- OIC_LOG_V(INFO, TAG, "ConnectionStateCB - remote address : %s", address);
-
- (*env)->ReleaseStringUTFChars(env, jni_address, address);
- }
- CAResult_t res = CALEClientAddGattobjToList(env, gatt);
- if (CA_STATUS_OK != res)
- {
- OIC_LOG(ERROR, TAG, "CALEClientAddGattobjToList has failed");
- goto error_exit;
- }
-
- res = CALEClientDiscoverServices(env, gatt);
- if (CA_STATUS_OK != res)
- {
- OIC_LOG(ERROR, TAG, "CALEClientDiscoverServices has failed");
- goto error_exit;
- }
- }
- else if (GATT_ERROR == status && state_disconnected == newstate)
- {
- OIC_LOG(INFO, TAG, "Background connection running.. please wait");
- }
- else // le disconnected
- {
- CAResult_t res = CALEClientStartScan();
- if (CA_STATUS_OK != res)
- {
- if (CA_ADAPTER_NOT_ENABLED == res)
+ res = CALEClientAddGattobjToList(env, gatt);
+ if (CA_STATUS_OK != res)
{
- // scan will be started with start server when adapter is enabled
- OIC_LOG(INFO, TAG, "Adapter was disabled");
+ OIC_LOG(ERROR, TAG, "CALEClientAddGattobjToList has failed");
+ goto error_exit;
}
- else
+
+ res = CALEClientDiscoverServices(env, gatt);
+ if (CA_STATUS_OK != res)
{
- OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
+ OIC_LOG(ERROR, TAG, "CALEClientDiscoverServices has failed");
goto error_exit;
}
}
-
- jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
- if (!jni_address)
+ else
{
- OIC_LOG(ERROR, TAG, "CALEClientGetAddressFromGattObj has failed");
- goto error_exit;
+ OIC_LOG(INFO, TAG, "unknown status");
+ (*env)->ReleaseStringUTFChars(env, jni_address, address);
}
+ }
+ else // STATE_DISCONNECTED == newstate
+ {
+ OIC_LOG(DEBUG, TAG, "LE is disconnected");
- const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
- if (address)
+ CAResult_t res = CALEClientUpdateDeviceState(address,
+ CA_LE_CONNECTION_STATE,
+ STATE_DISCONNECTED);
+ (*env)->ReleaseStringUTFChars(env, jni_address, address);
+ if (CA_STATUS_OK != res)
{
- res = CALEClientRemoveDeviceState(address);
- if (CA_STATUS_OK != res)
- {
- OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceState has failed");
- goto error_exit;
- }
-
- (*env)->ReleaseStringUTFChars(env, jni_address, address);
+ OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
+ goto error_exit;
}
res = CALEClientGattClose(env, gatt);
OIC_LOG(ERROR, TAG, "CALEClientGattClose has failed");
}
+ if (CALECheckConnectionStateValue(status))
+ {
+ // this state is unexpected reason to disconnect
+ // if the reason is suitable, connection logic of the device will be destroyed.
+ OIC_LOG(INFO, TAG, "connection logic destroy");
+ goto error_exit;
+ }
+ else
+ {
+ // other reason except for gatt_success is expected to running
+ // background connection in BT platform.
+ OIC_LOG(INFO, TAG, "unknown status or manual disconnected state");
+ CALEClientUpdateSendCnt(env);
+ return;
+ }
+
if (g_sendBuffer)
{
(*env)->DeleteGlobalRef(env, g_sendBuffer);
// error label.
error_exit:
-
CALEClientSendFinish(env, gatt);
return;
}
*/
JNIEXPORT void JNICALL
Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIEnv *env,
- jobject obj,
- jobject gatt,
- jint status)
+ jobject obj,
+ jobject gatt,
+ jint status)
{
- OIC_LOG_V(DEBUG, TAG, "CALeGattServicesDiscoveredCallback - status %d: ", status);
+ OIC_LOG_V(INFO, TAG, "CALeGattServicesDiscoveredCallback - status %d", status);
VERIFY_NON_NULL_VOID(env, TAG, "env is null");
VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
- if (0 != status) // discovery error
+ if (GATT_SUCCESS != status) // discovery error
{
CALEClientSendFinish(env, gatt);
return;
return;
}
- if (!CALEClientIsSetCharacteristic(address))
+ jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
+ if (!jni_uuid)
{
- jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
- if (!jni_uuid)
- {
- OIC_LOG(ERROR, TAG, "jni_uuid is null");
- goto error_exit;
- }
+ OIC_LOG(ERROR, TAG, "jni_uuid is null");
+ goto error_exit;
+ }
+
+ jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, gatt, jni_uuid);
+ if (!jni_obj_GattCharacteristic)
+ {
+ OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
+ goto error_exit;
+ }
+
+ CAResult_t res = CALEClientSetCharacteristicNotification(env, gatt,
+ jni_obj_GattCharacteristic);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "CALEClientSetCharacteristicNotification has failed");
+ goto error_exit;
+ }
- jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, gatt, jni_uuid);
- if (!jni_obj_GattCharacteristic)
+ res = CALEClientSetUUIDToDescriptor(env, gatt, jni_obj_GattCharacteristic);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG_V(INFO, TAG, "Descriptor is not found : %d", res);
+
+ res = CALEClientSetFlagToState(env, jni_address, CA_LE_DESCRIPTOR_FOUND, JNI_FALSE);
+ if (CA_STATUS_OK != res)
{
- OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
+ OIC_LOG(ERROR, TAG, "CALEClientSetFlagToState has failed");
goto error_exit;
}
- CAResult_t res = CALEClientSetCharacteristicNotification(env, gatt,
- jni_obj_GattCharacteristic);
+ res = CALEClientUpdateDeviceState(address, CA_LE_CONNECTION_STATE,
+ STATE_SERVICE_CONNECTED);
if (CA_STATUS_OK != res)
{
- OIC_LOG(ERROR, TAG, "CALEClientSetCharacteristicNotification has failed");
+ OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
goto error_exit;
}
- res = CALEClientSetUUIDToDescriptor(env, gatt, jni_obj_GattCharacteristic);
- if (CA_STATUS_OK != res)
+ if (g_sendBuffer)
{
- OIC_LOG_V(INFO, TAG, "Descriptor is not found : %d", res);
- if (g_sendBuffer)
+ CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
+ if (CA_STATUS_OK != res)
{
- CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
- if (CA_STATUS_OK != res)
- {
- OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
- goto error_exit;
- }
+ OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
+ goto error_exit;
}
}
-
- res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
- STATE_SEND_NONE);
- if (CA_STATUS_OK != res)
- {
- OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
- goto error_exit;
- }
}
else
{
- CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
+ res = CALEClientSetFlagToState(env, jni_address, CA_LE_DESCRIPTOR_FOUND, JNI_TRUE);
if (CA_STATUS_OK != res)
{
- OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
+ OIC_LOG(ERROR, TAG, "CALEClientSetFlagToState has failed");
goto error_exit;
}
}
+
OIC_LOG(INFO, TAG, "ServicesDiscovery is successful");
(*env)->ReleaseStringUTFChars(env, jni_address, address);
return;
*/
JNIEXPORT void JNICALL
Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback(
- JNIEnv *env, jobject obj, jobject gatt, jbyteArray data,
- jint status)
+ JNIEnv *env, jobject obj, jobject gatt, jbyteArray data, jint status)
{
- OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - status : %d", status);
+ OIC_LOG_V(INFO, TAG, "CALeGattCharacteristicWriteCallback - status : %d", status);
VERIFY_NON_NULL_VOID(env, TAG, "env is null");
VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
- jboolean isCopy;
- char* wroteData = (char*) (*env)->GetByteArrayElements(env, data, &isCopy);
-
- OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - write data : %s", wroteData);
-
// send success & signal
jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
if (!jni_address)
goto error_exit;
}
- jint gatt_success = CALEGetConstantsValue(env, CLASSPATH_BT_GATT, "GATT_SUCCESS");
- if (gatt_success != status) // error case
+ if (GATT_SUCCESS != status) // error case
{
OIC_LOG(ERROR, TAG, "send failure");
ca_cond_signal(g_threadWriteCharacteristicCond);
ca_mutex_unlock(g_threadWriteCharacteristicMutex);
- CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED,
- STATE_CHARACTER_SET,
- STATE_SEND_FAILED);
+ CAResult_t res = CALEClientUpdateDeviceState(address, CA_LE_SEND_STATE,
+ STATE_SEND_FAIL);
if (CA_STATUS_OK != res)
{
OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
g_clientErrorCallback(address, data, length, CA_SEND_FAILED);
}
- CALEClientSendFinish(env, gatt);
+ (*env)->ReleaseStringUTFChars(env, jni_address, address);
goto error_exit;
}
}
else
{
OIC_LOG(DEBUG, TAG, "send success");
- CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
+ CAResult_t res = CALEClientUpdateDeviceState(address, CA_LE_SEND_STATE,
STATE_SEND_SUCCESS);
if (CA_STATUS_OK != res)
{
g_isSignalSetFlag = true;
ca_cond_signal(g_threadWriteCharacteristicCond);
ca_mutex_unlock(g_threadWriteCharacteristicMutex);
-
- CALEClientUpdateSendCnt(env);
}
(*env)->ReleaseStringUTFChars(env, jni_address, address);
Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicChangedCallback(
JNIEnv *env, jobject obj, jobject gatt, jbyteArray data)
{
- OIC_LOG(DEBUG, TAG, "CALeGattCharacteristicChangedCallback");
+ OIC_LOG(INFO, TAG, "CALeGattCharacteristicChangedCallback");
VERIFY_NON_NULL_VOID(env, TAG, "env is null");
VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - data. : %p, %d",
receivedData, length);
- ca_mutex_lock(g_bleServerBDAddressMutex);
uint32_t sentLength = 0;
- g_CABLEClientDataReceivedCallback(address, receivedData, length,
- &sentLength);
+ ca_mutex_lock(g_bleServerBDAddressMutex);
+ g_CABLEClientDataReceivedCallback(address, receivedData, length, &sentLength);
ca_mutex_unlock(g_bleServerBDAddressMutex);
(*env)->ReleaseStringUTFChars(env, jni_address, address);
jobject gatt,
jint status)
{
- OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorWriteCallback - status %d: ", status);
+ OIC_LOG_V(INFO, TAG, "CALeGattDescriptorWriteCallback - status %d", status);
VERIFY_NON_NULL_VOID(env, TAG, "env is null");
VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
- CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
+ if (GATT_SUCCESS != status) // error
+ {
+ goto error_exit;
+ }
+
+ jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
+ if (!jni_address)
+ {
+ goto error_exit;
+ }
+
+ const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
+ if (!address)
+ {
+ goto error_exit;
+ }
+
+ CAResult_t res = CALEClientUpdateDeviceState(address, CA_LE_CONNECTION_STATE,
+ STATE_SERVICE_CONNECTED);
+ (*env)->ReleaseStringUTFChars(env, jni_address, address);
if (CA_STATUS_OK != res)
{
- OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
+ OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
goto error_exit;
}
+
+ if (g_sendBuffer)
+ {
+ CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
+ goto error_exit;
+ }
+ }
return;
// error label.