Remove unnecessary setting
[platform/core/connectivity/nfc-manager-neard.git] / common / net_nfc_util.c
1 /*
2  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *                               http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // libc header
18 #include <stdlib.h>
19 #include <string.h>
20 #include <pthread.h>
21 #include <fcntl.h>
22
23 // platform header
24 #include <vconf.h>
25 #include <bluetooth.h>
26 #include <bluetooth_internal.h>
27
28 // nfc-manager header
29 #include "net_nfc_util_internal.h"
30 #include "net_nfc_debug_internal.h"
31 #include "net_nfc_oem_controller.h"
32 #include "net_nfc_util_defines.h"
33
34 static const char *schema[] =
35 {
36         "",
37         "http://www.",
38         "https://www.",
39         "http://",
40         "https://",
41         "tel:",
42         "mailto:",
43         "ftp://anonymous:anonymous@",
44         "ftp://ftp.",
45         "ftps://",
46         "sftp://",
47         "smb://",
48         "nfs://",
49         "ftp://",
50         "dav://",
51         "news:",
52         "telnet://",
53         "imap:",
54         "rtsp://",
55         "urn:",
56         "pop:",
57         "sip:",
58         "sips:",
59         "tftp:",
60         "btspp://",
61         "btl2cap://",
62         "btgoep://",
63         "tcpobex://",
64         "irdaobex://",
65         "file://",
66         "urn:epc:id:",
67         "urn:epc:tag:",
68         "urn:epc:pat:",
69         "urn:epc:raw:",
70         "urn:epc:",
71         "urn:epc:nfc:",
72 };
73
74 static uint8_t *bt_addr = NULL;
75
76 /* for log tag */
77 static const char *log_tag = LOG_CLIENT_TAG;
78
79 const char *net_nfc_get_log_tag()
80 {
81         return log_tag;
82 }
83
84 void net_nfc_change_log_tag()
85 {
86         log_tag = LOG_SERVER_TAG;
87 }
88
89 API void __net_nfc_util_free_mem(void **mem, char *filename, unsigned int line)
90 {
91         if (NULL == mem)
92         {
93                 SECURE_LOGD("FILE: %s, LINE:%d, Invalid parameter in mem free util, mem is NULL",
94                                 filename, line);
95                 return;
96         }
97
98         if (NULL == *mem)
99         {
100                 SECURE_LOGD("FILE: %s, LINE:%d, Invalid Parameter in mem free util, *mem is NULL",
101                                 filename, line);
102                 return;
103         }
104
105         g_free(*mem);
106         *mem = NULL;
107 }
108
109 API void __net_nfc_util_alloc_mem(void **mem, int size, char *filename,
110                 unsigned int line)
111 {
112         if (NULL == mem || size <= 0)
113         {
114                 SECURE_LOGD("FILE: %s, LINE:%d, Invalid parameter in mem alloc util",
115                         "mem [%p], size [%d]", filename, line, mem, size);
116                 return;
117         }
118
119         if (*mem != NULL)
120         {
121                 SECURE_LOGD("FILE: %s, LINE:%d, WARNING: Pointer is not NULL, mem [%p]",
122                                 filename, line, *mem);
123         }
124
125         *mem = g_malloc0(size);
126
127         if (NULL == *mem)
128         {
129                 SECURE_LOGD("FILE: %s, LINE:%d, Allocation is failed, size [%d]",
130                                 filename, line, size);
131         }
132 }
133
134 API void __net_nfc_util_strdup(char **output, const char *origin, char *filename,
135                 unsigned int line)
136 {
137         if (NULL == output || NULL == origin)
138         {
139                 SECURE_LOGD("FILE: %s, LINE:%d, Invalid parameter in strdup",
140                                 "output [%p], origin [%p]", filename, line, output, origin);
141                 return;
142         }
143
144         if (*output != NULL)
145         {
146                 SECURE_LOGD("FILE: %s, LINE:%d, WARNING: Pointer is not NULL, mem [%p]",
147                                 filename, line, *output);
148         }
149
150         *output = g_strdup(origin);
151
152         if (NULL == *output)
153                 SECURE_LOGD("FILE: %s, LINE:%d, strdup failed", filename, line);
154 }
155
156 API bool net_nfc_util_alloc_data(data_s *data, uint32_t length)
157 {
158         RETV_IF(0 == length, false);
159         RETV_IF(NULL == data, false);
160
161         _net_nfc_util_alloc_mem(data->buffer, length);
162         if (NULL == data->buffer)
163                 return false;
164
165         data->length = length;
166
167         return true;
168 }
169
170 API void net_nfc_util_free_data(data_s *data)
171 {
172         RET_IF(NULL == data);
173         RET_IF(NULL == data->buffer);
174
175         _net_nfc_util_free_mem(data->buffer);
176         data->length = 0;
177 }
178
179 net_nfc_conn_handover_carrier_state_e net_nfc_util_get_cps(
180                 net_nfc_conn_handover_carrier_type_e carrier_type)
181 {
182         net_nfc_conn_handover_carrier_state_e cps = NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE;
183         bt_adapter_state_e state = BT_ADAPTER_DISABLED;
184
185         if (NET_NFC_CONN_HANDOVER_CARRIER_BT== carrier_type)
186         {
187                 bt_adapter_get_state(&state);
188
189                 switch (state)
190                 {
191                 case BT_ADAPTER_ENABLED :
192                         cps = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
193                         break;
194                 /*
195                 case BT_ADAPTER_CHANGING_ENABLE :
196                         cps = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATING;
197                         break;
198                 */
199                 case BT_ADAPTER_DISABLED :
200                 //case BT_ADAPTER_CHANGING_DISABLE :
201                 default :
202                         cps = NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE;
203                         break;
204                 }
205         }
206         else if (NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS == carrier_type)
207         {
208                 int wifi_state = 0;
209
210                 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
211
212                 switch (wifi_state)
213                 {
214                 case VCONFKEY_WIFI_UNCONNECTED :
215                 case VCONFKEY_WIFI_CONNECTED :
216                 case VCONFKEY_WIFI_TRANSFER :
217                         cps = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
218                         break;
219
220                 case VCONFKEY_WIFI_OFF :
221                 default :
222                         cps = NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE;
223                         break;
224                 }
225         }
226
227         return cps;
228 }
229
230 uint8_t *net_nfc_util_get_local_bt_address()
231 {
232         if (bt_addr != NULL)
233         {
234                 return bt_addr;
235         }
236
237         _net_nfc_util_alloc_mem(bt_addr, BLUETOOTH_ADDRESS_LENGTH);
238         if (bt_addr != NULL)
239         {
240                 net_nfc_conn_handover_carrier_state_e ret;
241
242                 ret = net_nfc_util_get_cps(NET_NFC_CONN_HANDOVER_CARRIER_BT);
243
244                 if (ret != NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE)
245                 {
246                         // bt power is off. so get bt address from configuration file.
247                         FILE *fp = NULL;
248
249                         if ((fp = fopen(HIDDEN_BT_ADDR_FILE, "r")) != NULL)
250                         {
251                                 int ch;
252                                 int i = 0;
253                                 int count = 0;
254                                 unsigned char temp[BLUETOOTH_ADDRESS_LENGTH * 2] = { 0, };
255
256                                 while ((ch = fgetc(fp)) != EOF && count < BLUETOOTH_ADDRESS_LENGTH * 2)
257                                 {
258                                         if (((ch >= '0') && (ch <= '9')))
259                                                 temp[count++] = ch - '0';
260                                         else if (((ch >= 'a') && (ch <= 'z')))
261                                                 temp[count++] = ch - 'a' + 10;
262                                         else if (((ch >= 'A') && (ch <= 'Z')))
263                                                 temp[count++] = ch - 'A' + 10;
264                                 }
265
266                                 for (; i < BLUETOOTH_ADDRESS_LENGTH; i++)
267                                 {
268                                         bt_addr[i] = temp[i * 2] << 4 | temp[i * 2 + 1];
269                                 }
270
271                                 fclose(fp);
272                         }
273                 }
274                 else
275                 {
276                         char *local_address = NULL;
277
278                         bt_adapter_get_address(&local_address);
279
280                         memcpy(bt_addr, local_address, BLUETOOTH_ADDRESS_LENGTH);
281                 }
282         }
283
284         return bt_addr;
285 }
286
287 void net_nfc_util_enable_bluetooth(void)
288 {
289         bt_adapter_enable();
290 }
291
292 bool net_nfc_util_strip_string(char *buffer, int buffer_length)
293 {
294         int i = 0;
295         char *temp = NULL;
296         bool result = false;
297
298         _net_nfc_util_alloc_mem(temp, buffer_length);
299         if (NULL == temp)
300                 return result;
301
302         for (; i < buffer_length; i++)
303         {
304                 if (buffer[i] != ' ' && buffer[i] != '\t')
305                         break;
306         }
307
308         if (i < buffer_length)
309         {
310                 memcpy(temp, &buffer[i], buffer_length - i);
311                 memset(buffer, 0x00, buffer_length);
312                 memcpy(buffer, temp, buffer_length - i);
313
314                 result = true;
315         }
316         else
317         {
318                 result = false;
319         }
320
321         _net_nfc_util_free_mem(temp);
322
323         return true;
324 }
325
326 static uint16_t _net_nfc_util_update_CRC(uint8_t ch, uint16_t *lpwCrc)
327 {
328         ch = (ch ^ (uint8_t)((*lpwCrc) & 0x00FF));
329         ch = (ch ^ (ch << 4));
330         *lpwCrc = (*lpwCrc >> 8) ^ ((uint16_t)ch << 8) ^
331                         ((uint16_t)ch << 3) ^ ((uint16_t)ch >> 4);
332         return (*lpwCrc);
333 }
334
335 void net_nfc_util_compute_CRC(CRC_type_e CRC_type, uint8_t *buffer,
336         uint32_t length)
337 {
338         uint8_t chBlock = 0;
339         uint8_t *temp = buffer;
340         int msg_length = length - 2;
341
342         // default is CRC_B
343         uint16_t wCrc = 0xFFFF; /* ISO/IEC 13239 (formerly ISO/IEC 3309) */
344
345         switch (CRC_type)
346         {
347         case CRC_A :
348                 wCrc = 0x6363;
349                 break;
350
351         case CRC_B :
352                 wCrc = 0xFFFF;
353                 break;
354         }
355
356         do{
357                 chBlock = *buffer++;
358                 _net_nfc_util_update_CRC(chBlock, &wCrc);
359         } while (--msg_length > 0);
360
361         if (CRC_B == CRC_type)
362                 wCrc = ~wCrc; /* ISO/IEC 13239 (formerly ISO/IEC 3309) */
363
364         temp[length - 2] = (uint8_t)(wCrc & 0xFF);
365         temp[length - 1] = (uint8_t)((wCrc >> 8) & 0xFF);
366 }
367
368 const char *net_nfc_util_get_schema_string(int index)
369 {
370         RETV_IF(0 == index, NULL);
371         RETV_IF(index >= NET_NFC_SCHEMA_MAX, NULL);
372
373         return schema[index];
374 }
375