2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://floralicense.org/license/
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.
24 #include <bluetooth-api.h>
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"
33 static const char *schema[] =
42 "ftp://anonymous:anonymous@",
73 static uint8_t *bt_addr = NULL;
76 static const char *log_tag = LOG_CLIENT_TAG;
78 const char *net_nfc_get_log_tag()
83 void net_nfc_change_log_tag()
85 log_tag = LOG_SERVER_TAG;
88 API void __net_nfc_util_free_mem(void **mem, char *filename, unsigned int line)
92 SECURE_LOGD("FILE: %s, LINE:%d, Invalid parameter in mem free util, mem is NULL",
99 SECURE_LOGD("FILE: %s, LINE:%d, Invalid Parameter in mem free util, *mem is NULL",
108 API void __net_nfc_util_alloc_mem(void **mem, int size, char *filename,
111 if (NULL == mem || size <= 0)
113 SECURE_LOGD("FILE: %s, LINE:%d, Invalid parameter in mem alloc util",
114 "mem [%p], size [%d]", filename, line, mem, size);
120 SECURE_LOGD("FILE: %s, LINE:%d, WARNING: Pointer is not NULL, mem [%p]",
121 filename, line, *mem);
124 *mem = g_malloc0(size);
128 SECURE_LOGD("FILE: %s, LINE:%d, Allocation is failed, size [%d]",
129 filename, line, size);
133 API void __net_nfc_util_strdup(char **output, const char *origin, char *filename,
136 if (NULL == output || NULL == origin)
138 SECURE_LOGD("FILE: %s, LINE:%d, Invalid parameter in strdup",
139 "output [%p], origin [%p]", filename, line, output, origin);
145 SECURE_LOGD("FILE: %s, LINE:%d, WARNING: Pointer is not NULL, mem [%p]",
146 filename, line, *output);
149 *output = g_strdup(origin);
152 SECURE_LOGD("FILE: %s, LINE:%d, strdup failed", filename, line);
155 API bool net_nfc_util_alloc_data(data_s *data, uint32_t length)
157 RETV_IF(0 == length, false);
158 RETV_IF(NULL == data, false);
160 _net_nfc_util_alloc_mem(data->buffer, length);
161 if (NULL == data->buffer)
164 data->length = length;
169 API void net_nfc_util_free_data(data_s *data)
171 RET_IF(NULL == data);
172 RET_IF(NULL == data->buffer);
174 _net_nfc_util_free_mem(data->buffer);
178 net_nfc_conn_handover_carrier_state_e net_nfc_util_get_cps(
179 net_nfc_conn_handover_carrier_type_e carrier_type)
181 net_nfc_conn_handover_carrier_state_e cps = NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE;
183 if (NET_NFC_CONN_HANDOVER_CARRIER_BT== carrier_type)
185 int ret = bluetooth_check_adapter();
189 case BLUETOOTH_ADAPTER_ENABLED :
190 cps = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
193 case BLUETOOTH_ADAPTER_CHANGING_ENABLE :
194 cps = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATING;
197 case BLUETOOTH_ADAPTER_DISABLED :
198 case BLUETOOTH_ADAPTER_CHANGING_DISABLE :
199 case BLUETOOTH_ERROR_NO_RESOURCES :
201 cps = NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE;
205 else if (NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS == carrier_type)
209 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
213 case VCONFKEY_WIFI_UNCONNECTED :
214 case VCONFKEY_WIFI_CONNECTED :
215 case VCONFKEY_WIFI_TRANSFER :
216 cps = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
219 case VCONFKEY_WIFI_OFF :
221 cps = NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE;
229 uint8_t *net_nfc_util_get_local_bt_address()
236 _net_nfc_util_alloc_mem(bt_addr, BLUETOOTH_ADDRESS_LENGTH);
239 net_nfc_conn_handover_carrier_state_e ret;
241 ret = net_nfc_util_get_cps(NET_NFC_CONN_HANDOVER_CARRIER_BT);
243 if (ret != NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE)
245 // bt power is off. so get bt address from configuration file.
248 if ((fp = fopen(HIDDEN_BT_ADDR_FILE, "r")) != NULL)
253 unsigned char temp[BLUETOOTH_ADDRESS_LENGTH * 2] = { 0, };
255 while ((ch = fgetc(fp)) != EOF && count < BLUETOOTH_ADDRESS_LENGTH * 2)
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;
265 for (; i < BLUETOOTH_ADDRESS_LENGTH; i++)
267 bt_addr[i] = temp[i * 2] << 4 | temp[i * 2 + 1];
275 bluetooth_device_address_t local_address;
277 memset(&local_address, 0x00, sizeof(bluetooth_device_address_t));
279 bluetooth_get_local_address(&local_address);
281 memcpy(bt_addr, &local_address.addr, BLUETOOTH_ADDRESS_LENGTH);
288 void net_nfc_util_enable_bluetooth(void)
290 bluetooth_enable_adapter();
293 bool net_nfc_util_strip_string(char *buffer, int buffer_length)
299 _net_nfc_util_alloc_mem(temp, buffer_length);
303 for (; i < buffer_length; i++)
305 if (buffer[i] != ' ' && buffer[i] != '\t')
309 if (i < buffer_length)
311 memcpy(temp, &buffer[i], buffer_length - i);
312 memset(buffer, 0x00, buffer_length);
313 memcpy(buffer, temp, buffer_length - i);
322 _net_nfc_util_free_mem(temp);
327 static uint16_t _net_nfc_util_update_CRC(uint8_t ch, uint16_t *lpwCrc)
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);
336 void net_nfc_util_compute_CRC(CRC_type_e CRC_type, uint8_t *buffer,
340 uint8_t *temp = buffer;
341 int msg_length = length - 2;
344 uint16_t wCrc = 0xFFFF; /* ISO/IEC 13239 (formerly ISO/IEC 3309) */
359 _net_nfc_util_update_CRC(chBlock, &wCrc);
360 } while (--msg_length > 0);
362 if (CRC_B == CRC_type)
363 wCrc = ~wCrc; /* ISO/IEC 13239 (formerly ISO/IEC 3309) */
365 temp[length - 2] = (uint8_t)(wCrc & 0xFF);
366 temp[length - 1] = (uint8_t)((wCrc >> 8) & 0xFF);
369 const char *net_nfc_util_get_schema_string(int index)
371 RETV_IF(0 == index, NULL);
372 RETV_IF(index >= NET_NFC_SCHEMA_MAX, NULL);
374 return schema[index];