1 /* ****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
23 #include "caleclient.h"
24 #include "camanagerleutil.h"
25 #include "camanagerdevice.h"
26 #include "caleautoconnector.h"
27 #include "cacommonutil.h"
31 #define TAG "OIC_CA_LE_AUTO_CONN"
33 static const size_t MAX_RETRY_COUNT = 5;
34 static const size_t TIMEOUT = 1000000;
35 static const size_t WAITING_TIME = 500000;
37 static oc_mutex g_connectRetryMutex = NULL;
38 static oc_cond g_connectRetryCond = NULL;
40 static oc_mutex g_recoveryMutex = NULL;
41 static oc_cond g_recoveryCond = NULL;
43 CAResult_t CAManagerInitLEAutoConnection()
45 if (NULL == g_connectRetryMutex)
47 g_connectRetryMutex = oc_mutex_new();
48 if (NULL == g_connectRetryMutex)
50 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
51 return CA_STATUS_FAILED;
55 if (NULL == g_connectRetryCond)
57 g_connectRetryCond = oc_cond_new();
58 if (NULL == g_connectRetryCond)
60 OIC_LOG(ERROR, TAG, "oc_cond_new has failed");
61 return CA_STATUS_FAILED;
65 if (NULL == g_recoveryMutex)
67 g_recoveryMutex = oc_mutex_new();
68 if (NULL == g_recoveryMutex)
70 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
71 return CA_STATUS_FAILED;
75 if (NULL == g_recoveryCond)
77 g_recoveryCond = oc_cond_new();
78 if (NULL == g_recoveryCond)
80 OIC_LOG(ERROR, TAG, "oc_cond_new has failed");
81 return CA_STATUS_FAILED;
88 void CAManagerTerminateLEAutoConnection()
90 if (g_connectRetryCond)
92 oc_cond_signal(g_connectRetryCond);
93 oc_cond_free(g_connectRetryCond);
94 g_connectRetryCond = NULL;
97 if (g_connectRetryMutex)
99 oc_mutex_free(g_connectRetryMutex);
100 g_connectRetryMutex = NULL;
105 oc_cond_signal(g_recoveryCond);
106 oc_cond_free(g_recoveryCond);
107 g_recoveryCond = NULL;
112 oc_mutex_free(g_recoveryMutex);
113 g_recoveryMutex = NULL;
117 CAResult_t CAManagerStartAutoConnection(JNIEnv *env, jstring remote_le_address)
119 VERIFY_NON_NULL(env, TAG, "env is null");
120 VERIFY_NON_NULL(remote_le_address, TAG, "remote_le_address is null");
122 OIC_LOG(DEBUG, TAG, "IN - CAManagerStartAutoConnection");
123 oc_mutex_lock(g_connectRetryMutex);
125 bool isAutoConnecting = false;
126 if (CA_STATUS_OK != CAManagerGetAutoConnectingFlag(env, remote_le_address, &isAutoConnecting))
128 OIC_LOG(DEBUG, TAG, "CAManagerIsAutoConnecting has failed");
129 oc_mutex_unlock(g_connectRetryMutex);
130 return CA_STATUS_FAILED;
133 if (isAutoConnecting)
135 OIC_LOG(INFO, TAG, "connection has been already in progress or completed");
136 oc_mutex_unlock(g_connectRetryMutex);
137 return CA_STATUS_FAILED;
140 CAResult_t res = CA_STATUS_OK;
141 for (size_t retry_cnt = 0 ; retry_cnt < MAX_RETRY_COUNT ; retry_cnt++)
143 // there is retry logic 5 times when connectGatt call has failed
144 // because BT adapter might be not ready yet.
145 res = CAManagerConnectGatt(env, remote_le_address);
146 if (CA_STATUS_OK != res)
148 OIC_LOG_V(INFO, TAG, "retry will be started at least %d times after delay 1sec",
149 MAX_RETRY_COUNT - retry_cnt - 1);
150 if (oc_cond_wait_for(g_connectRetryCond, g_connectRetryMutex, TIMEOUT) == 0)
152 oc_mutex_unlock(g_connectRetryMutex);
153 OIC_LOG(INFO, TAG, "request to connect gatt was canceled");
156 // time out. retry connection
160 OIC_LOG(INFO, TAG, "ConnectGatt has called successfully");
164 oc_mutex_unlock(g_connectRetryMutex);
165 OIC_LOG(DEBUG, TAG, "OUT - CAManagerStartAutoConnection");
169 CAResult_t CAManagerConnectGatt(JNIEnv *env, jstring remote_le_address)
171 VERIFY_NON_NULL(env, TAG, "env");
172 VERIFY_NON_NULL(remote_le_address, TAG, "remote_le_address");
174 OIC_LOG(DEBUG, TAG, "IN - CAManagerConnectGatt");
176 jobject jni_bluetooth = CAManagerGetRemoteDevice(env, remote_le_address);
179 OIC_LOG(ERROR, TAG, "jni_bluetooth is null");
180 return CA_STATUS_FAILED;
183 if (!CAManagerIsDeviceBonded(env, jni_bluetooth))
185 OIC_LOG(INFO, TAG, "device is BONDED_NONE");
188 // request to connection with AutoConnection Flag
189 OIC_LOG(INFO, TAG, "request to gatt connection for auto connection");
190 CAResult_t res = CALEClientDirectConnect(env, jni_bluetooth, JNI_TRUE);
191 if (CA_STATUS_OK != res)
193 OIC_LOG(INFO, TAG, "re-connection will be started");
197 // set flag auto connection is requested.
198 CAManagerSetAutoConnectingFlag(env, remote_le_address, true);
200 OIC_LOG(DEBUG, TAG, "OUT - CAManagerConnectGatt");
204 CAResult_t CAManagerProcessRecovery(JNIEnv *env, uint16_t adapter_state)
206 VERIFY_NON_NULL(env, TAG, "env");
207 OIC_LOG(DEBUG, TAG, "IN - CAManagerProcessRecovery");
209 oc_mutex_lock(g_recoveryMutex);
210 CAResult_t res = CA_STATUS_OK;
212 switch(adapter_state)
215 // adapter will be enabled automatically after WAITING_TIME.
216 if (oc_cond_wait_for(g_recoveryCond, g_recoveryMutex, WAITING_TIME) == 0)
218 OIC_LOG(INFO, TAG, "BT recovery was canceled");
223 if (!CAManagerControlAdapter(env, true))
225 OIC_LOG(ERROR, TAG, "BT recovery(enable) failure");
226 res = CA_STATUS_FAILED;
229 CAManagerSetBTRecovery(false);
232 if (!CAManagerControlAdapter(env, false))
234 OIC_LOG(ERROR, TAG, "BT recovery(disable) failure");
235 res = CA_STATUS_FAILED;
237 CAManagerSetBTRecovery(true);
243 oc_mutex_unlock(g_recoveryMutex);
244 OIC_LOG(DEBUG, TAG, "OUT - CAManagerProcessRecovery");