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