Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / resource / csdk / connectivity / util / src / camanager / android / caleautoconnector.c
1 /* ****************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include <jni.h>
22 #include "cacommon.h"
23 #include "caleclient.h"
24 #include "camanagerleutil.h"
25 #include "camanagerdevice.h"
26 #include "caleautoconnector.h"
27 #include "cacommonutil.h"
28 #include "logger.h"
29 #include "camutex.h"
30
31 #define TAG "OIC_CA_LE_AUTO_CONN"
32
33 static const size_t MAX_RETRY_COUNT = 5;
34 static const size_t TIMEOUT = 1000000;
35 static const size_t WAITING_TIME = 500000;
36
37 static ca_mutex g_connectRetryMutex = NULL;
38 static ca_cond g_connectRetryCond = NULL;
39
40 static ca_mutex g_recoveryMutex = NULL;
41 static ca_cond g_recoveryCond = NULL;
42
43 CAResult_t CAManagerInitLEAutoConnection()
44 {
45     if (NULL == g_connectRetryMutex)
46     {
47         g_connectRetryMutex = ca_mutex_new();
48         if (NULL == g_connectRetryMutex)
49         {
50             OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
51             return CA_STATUS_FAILED;
52         }
53     }
54
55     if (NULL == g_connectRetryCond)
56     {
57         g_connectRetryCond = ca_cond_new();
58         if (NULL == g_connectRetryCond)
59         {
60             OIC_LOG(ERROR, TAG, "ca_cond_new has failed");
61             return CA_STATUS_FAILED;
62         }
63     }
64
65     if (NULL == g_recoveryMutex)
66     {
67         g_recoveryMutex = ca_mutex_new();
68         if (NULL == g_recoveryMutex)
69         {
70             OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
71             return CA_STATUS_FAILED;
72         }
73     }
74
75     if (NULL == g_recoveryCond)
76     {
77         g_recoveryCond = ca_cond_new();
78         if (NULL == g_recoveryCond)
79         {
80             OIC_LOG(ERROR, TAG, "ca_cond_new has failed");
81             return CA_STATUS_FAILED;
82         }
83     }
84
85     return CA_STATUS_OK;
86 }
87
88 void CAManagerTerminateLEAutoConnection()
89 {
90     if (g_connectRetryCond)
91     {
92         ca_cond_signal(g_connectRetryCond);
93         ca_cond_free(g_connectRetryCond);
94         g_connectRetryCond = NULL;
95     }
96
97     if (g_connectRetryMutex)
98     {
99         ca_mutex_free(g_connectRetryMutex);
100         g_connectRetryMutex = NULL;
101     }
102
103     if (g_recoveryCond)
104     {
105         ca_cond_signal(g_recoveryCond);
106         ca_cond_free(g_recoveryCond);
107         g_recoveryCond = NULL;
108     }
109
110     if (g_recoveryMutex)
111     {
112         ca_mutex_free(g_recoveryMutex);
113         g_recoveryMutex = NULL;
114     }
115 }
116
117 CAResult_t CAManagerStartAutoConnection(JNIEnv *env, jstring remote_le_address)
118 {
119     VERIFY_NON_NULL(env, TAG, "env is null");
120     VERIFY_NON_NULL(remote_le_address, TAG, "remote_le_address is null");
121
122     OIC_LOG(DEBUG, TAG, "IN - CAManagerStartAutoConnection");
123
124     if (true == CAManagerGetAutoConnectionFlag(env, remote_le_address))
125     {
126         OIC_LOG(INFO, TAG, "auto connecting.");
127         return CA_STATUS_FAILED;
128     }
129
130     ca_mutex_lock(g_connectRetryMutex);
131
132     CAResult_t res = CA_STATUS_OK;
133     for (size_t retry_cnt = 0 ; retry_cnt < MAX_RETRY_COUNT ; retry_cnt++)
134     {
135         // there is retry logic 5 times when connectGatt call has failed
136         // because BT adapter might be not ready yet.
137         res = CAManagerConnectGatt(env, remote_le_address);
138         if (CA_STATUS_OK != res)
139         {
140             OIC_LOG_V(INFO, TAG, "retry will be started at least %d times after delay 1sec",
141                       MAX_RETRY_COUNT - retry_cnt - 1);
142             if (ca_cond_wait_for(g_connectRetryCond, g_connectRetryMutex, TIMEOUT) == 0)
143             {
144                 OIC_LOG(INFO, TAG, "request to connect gatt was canceled");
145                 ca_mutex_unlock(g_connectRetryMutex);
146                 return CA_STATUS_OK;
147             }
148             // time out. retry connection
149         }
150         else
151         {
152             OIC_LOG(INFO, TAG, "ConnectGatt has called successfully");
153             break;
154         }
155     }
156     ca_mutex_unlock(g_connectRetryMutex);
157     OIC_LOG(DEBUG, TAG, "OUT - CAManagerStartAutoConnection");
158     return res;
159 }
160
161 CAResult_t CAManagerConnectGatt(JNIEnv *env, jstring remote_le_address)
162 {
163     VERIFY_NON_NULL(env, TAG, "env");
164     VERIFY_NON_NULL(remote_le_address, TAG, "remote_le_address");
165
166     OIC_LOG(DEBUG, TAG, "IN - CAManagerConnectGatt");
167
168     jobject jni_bluetooth = CAManagerGetRemoteDevice(env, remote_le_address);
169     if (!jni_bluetooth)
170     {
171         OIC_LOG(ERROR, TAG, "jni_bluetooth is null");
172         return CA_STATUS_FAILED;
173     }
174
175     if (!CAManagerIsDeviceBonded(env, jni_bluetooth))
176     {
177         OIC_LOG(INFO, TAG, "device is BONDED_NONE");
178     }
179
180     // request to connection with AutoConnection Flag
181     OIC_LOG(INFO, TAG, "request to gatt connection for auto connection");
182     CAResult_t res = CALEClientDirectConnect(env, jni_bluetooth, JNI_TRUE);
183     if (CA_STATUS_OK != res)
184     {
185         OIC_LOG(INFO, TAG, "re-connection will be started");
186         return res;
187     }
188
189     // set flag auto connection is requested.
190     CAManagerSetAutoConnectionFlag(env, remote_le_address, true);
191
192     OIC_LOG(DEBUG, TAG, "OUT - CAManagerConnectGatt");
193     return CA_STATUS_OK;
194 }
195
196 CAResult_t CAManagerProcessRecovery(JNIEnv *env, uint16_t adapter_state)
197 {
198     VERIFY_NON_NULL(env, TAG, "env");
199     OIC_LOG(DEBUG, TAG, "IN - CAManagerProcessRecovery");
200
201     ca_mutex_lock(g_recoveryMutex);
202     CAResult_t res = CA_STATUS_OK;
203
204     switch(adapter_state)
205     {
206         case STATE_OFF:
207             // adapter will be enabled automatically after WAITING_TIME.
208             if (ca_cond_wait_for(g_recoveryCond, g_recoveryMutex, WAITING_TIME) == 0)
209             {
210                 OIC_LOG(INFO, TAG, "BT recovery was canceled");
211             }
212             else
213             {
214                 // enabled
215                 if (!CAManagerControlAdapter(env, true))
216                 {
217                     OIC_LOG(ERROR, TAG, "BT recovery(enable) failure");
218                     res = CA_STATUS_FAILED;
219                 }
220             }
221             CAManagerSetBTRecovery(false);
222             break;
223         case START_RECOVERY:
224             if (!CAManagerControlAdapter(env, false))
225             {
226                 OIC_LOG(ERROR, TAG, "BT recovery(disable) failure");
227                 res = CA_STATUS_FAILED;
228             }
229             CAManagerSetBTRecovery(true);
230             break;
231         default:
232             break;
233     }
234
235     ca_mutex_unlock(g_recoveryMutex);
236     OIC_LOG(DEBUG, TAG, "OUT - CAManagerProcessRecovery");
237
238     return res;
239 }