static ca_mutex g_gattObjectMutex = NULL;
static ca_mutex g_deviceStateListMutex = NULL;
+static ca_mutex g_scanMutex = NULL;
+
static CABLEDataReceivedCallback g_CABLEClientDataReceivedCallback = NULL;
//getting jvm
}
g_isStartedMulticastServer = false;
- g_isStartedScan = false;
+ CALEClientSetScanFlag(false);
CALEClientSetSendFinishFlag(false);
CALEClientTerminateGattMutexVariables();
else
{
OIC_LOG(DEBUG, TAG, "startLeScan is started");
- g_isStartedScan = true;
+ CALEClientSetScanFlag(true);
}
return CA_STATUS_OK;
else
{
OIC_LOG(DEBUG, TAG, "startLeScan With UUID is started");
- g_isStartedScan = true;
+ CALEClientSetScanFlag(true);
}
return CA_STATUS_OK;
}
else
{
- g_isStartedScan = false;
+ 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 (NULL == g_scanMutex)
+ {
+ g_scanMutex = ca_mutex_new();
+ if (NULL == g_scanMutex)
+ {
+ OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
+ return CA_STATUS_FAILED;
+ }
+ }
+
OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
}
ca_mutex_free(g_SendFinishMutex);
g_SendFinishMutex = NULL;
+ ca_mutex_free(g_scanMutex);
+ g_scanMutex = NULL;
+
OIC_LOG(DEBUG, TAG, "OUT");
}
goto error_exit;
}
}
- else if (GATT_SUCCESS == status && STATE_DISCONNECTED == newstate) // le disconnected
+ else // le disconnected
{
CAResult_t res = CALEClientStartScan();
if (CA_STATUS_OK != res)
{
OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
+ goto error_exit;
}
jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
if (!jni_address)
{
OIC_LOG(ERROR, TAG, "CALEClientGetAddressFromGattObj has failed");
+ goto error_exit;
}
const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
if (address)
{
- res = CALEClientUpdateDeviceState(address, STATE_DISCONNECTED,
- STATE_CHARACTER_NO_CHANGE,
- STATE_SEND_NONE);
+ res = CALEClientRemoveDeviceState(address);
if (CA_STATUS_OK != res)
{
- OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
+ OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceState has failed");
+ goto error_exit;
}
- (*env)->ReleaseStringUTFChars(env, jni_address, address);
- }
-
- res = CALEClientGattClose(env, gatt);
- if (CA_STATUS_OK != res)
- {
- OIC_LOG(ERROR, TAG, "CALEClientGattClose has failed");
- }
- }
- else // error
- {
- // update state
- jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
- if (!jni_address)
- {
- OIC_LOG(ERROR, TAG, "jni_address is null");
- goto error_exit;
- }
-
- const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
- if (address)
- {
- CAResult_t res = CALEClientUpdateDeviceState(address, STATE_DISCONNECTED,
- STATE_CHARACTER_NO_CHANGE,
- STATE_SEND_FAILED);
+ res = CALEClientRemoveGattObjForAddr(env, jni_address);
if (CA_STATUS_OK != res)
{
- OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
+ OIC_LOG(ERROR, TAG, "CALEClientRemoveGattObjForAddr has failed");
+ goto error_exit;
}
+
+ (*env)->ReleaseStringUTFChars(env, jni_address, address);
}
- (*env)->ReleaseStringUTFChars(env, jni_address, address);
- CAResult_t res = CALEClientGattClose(env, gatt);
+ res = CALEClientGattClose(env, gatt);
if (CA_STATUS_OK != res)
{
OIC_LOG(ERROR, TAG, "CALEClientGattClose has failed");
}
-
- goto error_exit;
}
return;
if (BT_STATE_ON == status) // STATE_ON:12
{
CANetworkStatus_t newStatus = CA_INTERFACE_UP;
+ CALEClientCreateDeviceList();
+ CALEServerCreateCachedDeviceList();
+
+ CAResult_t res = CALEClientStartScan();
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
+ }
+
+ res = CALEStartAdvertise();
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "CALEStartAdvertise has failed");
+ }
+
gCALEDeviceStateChangedCallback(newStatus);
}
else if (BT_STATE_OFF == status) // STATE_OFF:10
OIC_LOG(ERROR, TAG, "CALEClientRemoveAllScanDevices has failed");
}
+ res = CALEClientRemoveAllDeviceState();
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "CALEClientRemoveAllDeviceState has failed");
+ }
+
// remove obej for server
res = CALEServerRemoveAllDevices(env);
if (CA_STATUS_OK != res)
OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
}
+ CALEClientSetScanFlag(false);
+
CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
gCALEDeviceStateChangedCallback(newStatus);
}
return CA_STATUS_OK;
}
+CAResult_t CALEStartAdvertise()
+{
+ 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(ERROR, 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;
+ }
+
+ // start advertise
+ CAResult_t ret = CALEServerStartAdvertise(env, g_leAdvertiseCallback);
+ if (CA_STATUS_OK != ret)
+ {
+ OIC_LOG(ERROR, TAG, "CALEServerStartAdvertise has failed");
+ }
+
+ if (isAttached)
+ {
+ (*g_jvm)->DetachCurrentThread(g_jvm);
+ }
+ return ret;
+}
+
CAResult_t CALEServerStartAdvertise(JNIEnv *env, jobject advertiseCallback)
{
OIC_LOG(DEBUG, TAG, "IN - CALEServerStartAdvertise");
isAttached = true;
}
- CAResult_t ret = CALEServerStopMulticastServer(0);
+ CAResult_t ret = CALEServerGattClose(env, g_bluetoothGattServer);
+ if (CA_STATUS_OK != ret)
+ {
+ OIC_LOG(ERROR, TAG, "CALEServerGattClose has failed");
+ }
+
+ ret = CALEServerStopMulticastServer(0);
if (CA_STATUS_OK != ret)
{
OIC_LOG(ERROR, TAG, "CALEServerStopMulticastServer has failed");
else if (newState == jni_int_state_disconnected)
{
OIC_LOG(DEBUG, TAG, "LE DISCONNECTED");
- CAResult_t res = CALEServerGattClose(env, g_bluetoothGattServer);
- if (CA_STATUS_OK != res)
+
+ jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
+ CAResult_t ret = CALEServerRemoveDevice(env, jni_remoteAddress);
+ if (CA_STATUS_OK != ret)
+ {
+ OIC_LOG(ERROR, TAG, "CALEServerRemoveDevice has failed");
+ }
+
+ // start advertise
+ ret = CALEServerStartAdvertise(env, g_leAdvertiseCallback);
+ if (CA_STATUS_OK != ret)
{
- OIC_LOG(ERROR, TAG, "CALEServerGattClose has failed");
+ OIC_LOG(ERROR, TAG, "CALEServerStartAdvertise has failed");
}
}
else
VERIFY_NON_NULL_VOID(env, TAG, "env");
VERIFY_NON_NULL_VOID(obj, TAG, "obj");
- OIC_LOG_V(ERROR, TAG, "LE Advertise Start Failure Callback(%d)",
- errorCode);
+ OIC_LOG_V(INFO, TAG, "LE Advertise Start Failure Callback(%d)", errorCode);
}
/**