+/**
+ * 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);
+}