2 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <bluetooth.h>
26 #include <bluetooth_internal.h>
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"
34 static const char *schema[] =
43 "ftp://anonymous:anonymous@",
74 static uint8_t *bt_addr = NULL;
77 static const char *log_tag = LOG_CLIENT_TAG;
79 const char *net_nfc_get_log_tag()
84 void net_nfc_change_log_tag()
86 log_tag = LOG_SERVER_TAG;
89 API void __net_nfc_util_free_mem(void **mem, char *filename, unsigned int line)
93 SECURE_LOGD("FILE: %s, LINE:%d, Invalid parameter in mem free util, mem is NULL",
100 SECURE_LOGD("FILE: %s, LINE:%d, Invalid Parameter in mem free util, *mem is NULL",
109 API void __net_nfc_util_alloc_mem(void **mem, int size, char *filename,
112 if (NULL == mem || size <= 0)
114 SECURE_LOGD("FILE: %s, LINE:%d, Invalid parameter in mem alloc util",
115 "mem [%p], size [%d]", filename, line, mem, size);
121 SECURE_LOGD("FILE: %s, LINE:%d, WARNING: Pointer is not NULL, mem [%p]",
122 filename, line, *mem);
125 *mem = g_malloc0(size);
129 SECURE_LOGD("FILE: %s, LINE:%d, Allocation is failed, size [%d]",
130 filename, line, size);
134 API void __net_nfc_util_strdup(char **output, const char *origin, char *filename,
137 if (NULL == output || NULL == origin)
139 SECURE_LOGD("FILE: %s, LINE:%d, Invalid parameter in strdup",
140 "output [%p], origin [%p]", filename, line, output, origin);
146 SECURE_LOGD("FILE: %s, LINE:%d, WARNING: Pointer is not NULL, mem [%p]",
147 filename, line, *output);
150 *output = g_strdup(origin);
153 SECURE_LOGD("FILE: %s, LINE:%d, strdup failed", filename, line);
156 API bool net_nfc_util_alloc_data(data_s *data, uint32_t length)
158 RETV_IF(0 == length, false);
159 RETV_IF(NULL == data, false);
161 _net_nfc_util_alloc_mem(data->buffer, length);
162 if (NULL == data->buffer)
165 data->length = length;
170 API void net_nfc_util_free_data(data_s *data)
172 RET_IF(NULL == data);
173 RET_IF(NULL == data->buffer);
175 _net_nfc_util_free_mem(data->buffer);
179 net_nfc_conn_handover_carrier_state_e net_nfc_util_get_cps(
180 net_nfc_conn_handover_carrier_type_e carrier_type)
182 net_nfc_conn_handover_carrier_state_e cps = NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE;
183 bt_adapter_state_e state = BT_ADAPTER_DISABLED;
185 if (NET_NFC_CONN_HANDOVER_CARRIER_BT== carrier_type)
187 bt_adapter_get_state(&state);
191 case BT_ADAPTER_ENABLED :
192 cps = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
195 case BT_ADAPTER_CHANGING_ENABLE :
196 cps = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATING;
199 case BT_ADAPTER_DISABLED :
200 //case BT_ADAPTER_CHANGING_DISABLE :
202 cps = NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE;
206 else if (NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS == carrier_type)
210 vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
214 case VCONFKEY_WIFI_UNCONNECTED :
215 case VCONFKEY_WIFI_CONNECTED :
216 case VCONFKEY_WIFI_TRANSFER :
217 cps = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
220 case VCONFKEY_WIFI_OFF :
222 cps = NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE;
230 uint8_t *net_nfc_util_get_local_bt_address()
237 _net_nfc_util_alloc_mem(bt_addr, BLUETOOTH_ADDRESS_LENGTH);
240 net_nfc_conn_handover_carrier_state_e ret;
242 ret = net_nfc_util_get_cps(NET_NFC_CONN_HANDOVER_CARRIER_BT);
244 if (ret != NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE)
246 // bt power is off. so get bt address from configuration file.
249 if ((fp = fopen(HIDDEN_BT_ADDR_FILE, "r")) != NULL)
254 unsigned char temp[BLUETOOTH_ADDRESS_LENGTH * 2] = { 0, };
256 while ((ch = fgetc(fp)) != EOF && count < BLUETOOTH_ADDRESS_LENGTH * 2)
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;
266 for (; i < BLUETOOTH_ADDRESS_LENGTH; i++)
268 bt_addr[i] = temp[i * 2] << 4 | temp[i * 2 + 1];
276 char *local_address = NULL;
278 bt_adapter_get_address(&local_address);
280 memcpy(bt_addr, local_address, BLUETOOTH_ADDRESS_LENGTH);
287 void net_nfc_util_enable_bluetooth(void)
292 bool net_nfc_util_strip_string(char *buffer, int buffer_length)
298 _net_nfc_util_alloc_mem(temp, buffer_length);
302 for (; i < buffer_length; i++)
304 if (buffer[i] != ' ' && buffer[i] != '\t')
308 if (i < buffer_length)
310 memcpy(temp, &buffer[i], buffer_length - i);
311 memset(buffer, 0x00, buffer_length);
312 memcpy(buffer, temp, buffer_length - i);
321 _net_nfc_util_free_mem(temp);
326 static uint16_t _net_nfc_util_update_CRC(uint8_t ch, uint16_t *lpwCrc)
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);
335 void net_nfc_util_compute_CRC(CRC_type_e CRC_type, uint8_t *buffer,
339 uint8_t *temp = buffer;
340 int msg_length = length - 2;
343 uint16_t wCrc = 0xFFFF; /* ISO/IEC 13239 (formerly ISO/IEC 3309) */
358 _net_nfc_util_update_CRC(chBlock, &wCrc);
359 } while (--msg_length > 0);
361 if (CRC_B == CRC_type)
362 wCrc = ~wCrc; /* ISO/IEC 13239 (formerly ISO/IEC 3309) */
364 temp[length - 2] = (uint8_t)(wCrc & 0xFF);
365 temp[length - 1] = (uint8_t)((wCrc >> 8) & 0xFF);
368 const char *net_nfc_util_get_schema_string(int index)
370 RETV_IF(0 == index, NULL);
371 RETV_IF(index >= NET_NFC_SCHEMA_MAX, NULL);
373 return schema[index];