2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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.tizenopensource.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.
18 #include "ug-nfc-share-tag.h"
19 #include "ug-nfc-share-popup.h"
22 #include <Elementary.h>
27 #include <sys/types.h>
30 /* external library header */
31 #include <mime_type.h>
32 #include <notification.h>
34 #define NFC_POPUP_TIMEOUT 3.0
36 static ug_nfc_share_tag_type ug_nfc_share_tagType;
39 int _bt_ipc_send_obex_message(char *address, const uint8_t *files, uint32_t length);
41 /*-----------------------------------------------------------------------------------------------*/
43 static void _show_status_text(void *data, char *text)
45 ugdata_t *ug_data = (ugdata_t *)data;
49 ret_if(ug_data == NULL);
52 notification_status_message_post(text);
54 ug_destroy_me(ug_data->nfc_share_ug);
59 ug_nfc_share_tag_type ug_nfc_share_get_tag_type(void)
61 return ug_nfc_share_tagType;
64 void ug_nfc_share_set_tag_type(ug_nfc_share_tag_type tag_type)
66 if (tag_type < 0 || tag_type >= UG_NFC_SHARE_TAG_MAX)
69 ug_nfc_share_tagType = tag_type;
72 nfc_ndef_message_h ug_nfc_share_get_current_ndef(void *data)
74 ugdata_t *ug_data = (ugdata_t *)data;
78 retv_if(ug_data == NULL, NULL);
82 return ug_data->current_ndef;
85 ug_nfc_share_result_e ug_nfc_share_set_current_ndef(void *data, nfc_ndef_message_h ndef_msg)
89 ugdata_t *ug_data = (ugdata_t *)data;
92 LOGD("ug_data is null");
93 return UG_NFC_SHARE_ERROR;
96 if (ug_data->current_ndef != NULL)
98 if (nfc_ndef_message_destroy(ug_data->current_ndef) != NFC_ERROR_NONE)
100 LOGD("nfc_ndef_message_destroy failed");
104 ug_data->current_ndef = ndef_msg;
107 return UG_NFC_SHARE_OK;
111 static ug_nfc_share_result_e ug_nfc_share_make_mime_type_data_from_file_path(const char *path, uint8_t *type_data, uint32_t *type_size)
113 ug_nfc_share_result_e result = UG_NFC_SHARE_ERROR;
114 char *extension = NULL;
118 retv_if(path == NULL, result);
119 retv_if(type_data == NULL, result);
120 retv_if(type_size == NULL, result);
122 LOGD("typedata = %p, typesize = %d", type_data, *type_size);
124 memset(type_data, 0, *type_size);
127 extension = strrchr(path, '.');
128 LOGD("extension = %s\n", GET_SAFE_STRING(extension));
130 if (extension != NULL)
132 char *mime_str = NULL;
134 if (mime_type_get_mime_type(extension+1, &mime_str) == MIME_TYPE_ERROR_NONE)
136 LOGD("mime_str[%s]", mime_str);
138 *type_size = strlen(mime_str);
139 memcpy(type_data, mime_str, *type_size);
140 result = UG_NFC_SHARE_OK;
144 LOGD("ERROR :: mime_type_get_mime_type failed");
145 result = UG_NFC_SHARE_ERROR;
149 LOGD("mime type : %s", GET_SAFE_STRING((char *)type_data));
156 ug_nfc_share_result_e ug_nfc_share_make_ndef_message_from_file(nfc_ndef_message_h *msg, const char *path)
158 int result = UG_NFC_SHARE_ERROR;
160 uint8_t type_buffer[50] = { 0, };
161 int type_size = sizeof(type_buffer);
162 nfc_ndef_record_h record = NULL;
164 char *file_name = NULL;
165 uint8_t *file_data = NULL;
166 long int file_len = 0;
171 retv_if(msg == NULL, result);
172 retv_if(path == NULL, result);
173 /*Cause of Svace warning */
174 /*Name : TOCTTOU_SEQUENCE
176 1.use fstat inplace of stat
177 2.or using check-Use-Check Pattern
179 retv_if((stat(path, &st) == -1 && errno == ENOENT), result);
181 /* read file and make payload*/
182 file = fopen(path, "r");
185 long int read_count = 0, read_total = 0;
187 fseek(file, 0, SEEK_END);
188 file_len = ftell(file);
189 fseek(file, 0, SEEK_SET);
191 UG_NFC_SHARE_MEM_MALLOC(file_data, file_len, uint8_t);
192 if (file_data == NULL)
194 LOGD("ERROR :: UG_NFC_SHARE_MEM_MALLOC failed");
203 read_count = fread(file_data + read_total, 1, file_len - read_total, file);
204 read_total += read_count;
206 while (read_count != 0 && read_total < file_len);
210 LOGD("fread(%s) success, size %ld\n", path, file_len);
214 LOGD("fopen(%s) error\n");
220 result = ug_nfc_share_make_mime_type_data_from_file_path(path, type_buffer, (uint32_t *)&type_size);
221 if (result != UG_NFC_SHARE_OK)
223 LOGD("ERROR :: _make_mime_type_data_from_file_path failed [%d]", result);
228 /* get file name for id */
229 file_name = strrchr(path, '/');
230 if (file_name == NULL)
232 file_name = (char *)path;
239 LOGD("file name : %s", file_name);
242 result = nfc_ndef_record_create(&record, NFC_RECORD_TNF_MIME_MEDIA, type_buffer, type_size, (uint8_t *)file_name, strlen(file_name), file_data, file_len);
243 if (result != NFC_ERROR_NONE)
245 LOGD("nfc_ndef_record_create failed (%d)", result);
250 /* make file NDEF message */
251 result = nfc_ndef_message_create(msg);
252 if (result != NFC_ERROR_NONE)
254 LOGD("nfc_ndef_message_create failed [%d]\n", result);
256 nfc_ndef_record_destroy(record);
261 /* append record to ndef message */
262 result = nfc_ndef_message_append_record(*msg, record);
263 if (result != NFC_ERROR_NONE)
265 LOGD("nfc_ndef_message_append_record failed (%d)", result);
270 LOGD("ug_nfc_share_make_ndef_message_from_file success");
278 ug_nfc_share_result_e ug_nfc_share_make_ndef_message_from_multi_file(nfc_ndef_message_h *msg, const char *path[], int record_count)
280 int result = UG_NFC_SHARE_ERROR;
282 uint8_t type_buffer[50] = { 0, };
283 int type_size = sizeof(type_buffer);
284 nfc_ndef_record_h record = NULL;
286 char *file_name = NULL;
287 uint8_t *file_data = NULL;
288 long int file_len = 0;
294 retv_if(msg == NULL, result);
295 retv_if(path == NULL, result);
296 /*Cause of Svace warning */
297 /*Name : TOCTTOU_SEQUENCE
299 1.use fstat inplace of stat
300 2.or using check-Use-Check Pattern
303 for (index = 0; index < record_count; index++)
305 retv_if((stat(path[index], &st) == -1 && errno == ENOENT), result);
307 /* read file and make payload*/
308 file = fopen(path[index], "r");
312 long int read_count = 0, read_total = 0;
314 fseek(file, 0, SEEK_END);
315 file_len = ftell(file);
316 fseek(file, 0, SEEK_SET);
318 UG_NFC_SHARE_MEM_MALLOC(file_data, file_len, uint8_t);
319 if (file_data == NULL)
321 LOGD("ERROR :: UG_NFC_SHARE_MEM_MALLOC failed");
330 read_count = fread(file_data + read_total, 1, file_len - read_total, file);
331 read_total += read_count;
333 while (read_count != 0 && read_total < file_len);
337 LOGD("fread(%s) success, size %ld\n", path[index], file_len);
341 LOGD("fopen(%s) error\n");
347 result = ug_nfc_share_make_mime_type_data_from_file_path(path[index], type_buffer, (uint32_t *)&type_size);
348 if (result != UG_NFC_SHARE_OK)
350 LOGD("ERROR :: _make_mime_type_data_from_file_path failed [%d]", result);
355 /* get file name for id */
356 file_name = strrchr(path[index], '/');
357 if (file_name == NULL)
359 file_name = (char *)path[index];
366 LOGD("file name : %s", file_name);
369 result = nfc_ndef_record_create(&record, NFC_RECORD_TNF_MIME_MEDIA, type_buffer, type_size, (uint8_t *)file_name, strlen(file_name), file_data, file_len);
370 if (result != NFC_ERROR_NONE)
372 LOGD("nfc_ndef_record_create failed (%d)", result);
377 /* make file NDEF message */
378 result = nfc_ndef_message_create(msg);
379 if (result != NFC_ERROR_NONE)
381 LOGD("nfc_ndef_message_create failed [%d]\n", result);
383 nfc_ndef_record_destroy(record);
388 /* append record to ndef message */
389 result = nfc_ndef_message_append_record(*msg, record);
390 if (result != NFC_ERROR_NONE)
392 LOGD("nfc_ndef_message_append_record failed (%d)", result);
397 LOGD("ug_nfc_share_make_ndef_message_from_file success");
404 void _ug_nfc_share_get_bt_addr_from_string(uint8_t *addr, char *addr_string)
408 if (addr == NULL || addr_string == NULL)
413 LOGD("string : %s", addr_string);
415 UG_NFC_SHARE_MEM_STRNDUP(temp, addr_string, strlen(addr_string));
422 token = strtok(temp, ":");
426 value = strtol(token, NULL, 16);
428 addr[count++] = (uint8_t)value;
430 while ((token = strtok(NULL, ":")) != NULL);
432 UG_NFC_SHARE_MEM_FREE(temp);
435 static void _p2p_connection_handover_completed_cb(nfc_error_e result, nfc_ac_type_e carrior, void *ac_data, int ac_data_size, void *user_data)
439 ugdata_t* ug_data = (ugdata_t*)user_data;
441 /* To prevent write event during showing popup, unset response callback */
442 ug_nfc_unset_nfc_callback();
445 if(nfc_manager_deinitialize () != NFC_ERROR_NONE)
447 LOGD("nfc_manager_deinitialize failed");
450 if(result == NFC_ERROR_NONE)
454 LOGD("p2p_connection_handover is completed");
456 data = (char *)bundle_get_val(ug_data->bd, "request_data");
458 LOGD("uri[%d] = %s", strlen(data), data);
460 if (_bt_ipc_send_obex_message((char *)ac_data, (uint8_t *)data, strlen(data) + 1) == 0)
462 _show_status_text(ug_data, IDS_SHARED);
466 LOGD("_bt_ipc_send_obex_message failed");
468 _show_status_text(ug_data, IDS_UNABLE_TO_SHARE);
473 LOGD("p2p_connection_handover failed");
475 _show_status_text(ug_data, IDS_UNABLE_TO_SHARE);
481 static void _p2p_send_completed_cb(nfc_error_e result, void *user_data)
485 ugdata_t* ug_data = (ugdata_t*)user_data;
487 /* To prevent write event during showing popup, unset response callback */
488 ug_nfc_unset_nfc_callback();
491 if(nfc_manager_deinitialize () != NFC_ERROR_NONE)
493 LOGD("nfc_manager_deinitialize failed");
496 if(result == NFC_ERROR_NONE)
498 LOGD("_p2p_send_completed_cb is completed");
500 _show_status_text(ug_data, IDS_SHARED);
504 LOGD("_p2p_send_completed_cb failed");
506 _show_status_text(ug_data, IDS_UNABLE_TO_SHARE);
512 static void _p2p_target_discovered_cb(nfc_discovered_type_e type, nfc_p2p_target_h target, void *user_data)
516 ugdata_t* ug_data = (ugdata_t*)user_data;
518 if(type == NFC_DISCOVERED_TYPE_ATTACHED)
520 int result = NFC_ERROR_NONE;
522 LOGD("NFC_DISCOVERED_TYPE_ATTACHED");
524 if (ug_nfc_share_get_tag_type() == UG_NFC_SHARE_TAG_HANDOVER)
526 LOGD("UG_NFC_SHARE_TAG_HANDOVER\n");
528 /* The code below will be changed after capi is completed */
529 if ((result = nfc_p2p_connection_handover(target, NFC_AC_TYPE_UNKNOWN, _p2p_connection_handover_completed_cb, ug_data)) != NFC_ERROR_NONE)
531 LOGD("nfc_p2p_connection_handover failed [%d]", result);
538 nfc_ndef_message_h msg = NULL;
540 msg = ug_nfc_share_get_current_ndef(ug_data);
543 LOGD("nfc_ndef_message_h is NULL!!\n");
547 result = nfc_p2p_send(target, msg, _p2p_send_completed_cb, ug_data);
548 if(result != NFC_ERROR_NONE)
550 LOGD("nfc_p2p_send failed[%d]\n", result);
557 LOGD("NFC_DISCOVERED_TYPE_DETACHED");
563 void ug_nfc_set_nfc_callback(void *user_data)
567 nfc_manager_set_p2p_target_discovered_cb(_p2p_target_discovered_cb, user_data);
572 void ug_nfc_unset_nfc_callback(void)
576 nfc_manager_unset_p2p_target_discovered_cb();
581 int _bt_ipc_send_obex_message(char *address, const uint8_t *files, uint32_t length)
584 uint32_t i, count = 1;
585 E_DBus_Connection *conn = NULL;
589 if (address == NULL || files == NULL)
591 LOGD("invalid param [%p] [%p]", address, files);
596 for (i = 0; i < length; i++)
604 if (e_dbus_init() > 0)
606 if ((conn = e_dbus_bus_get(DBUS_BUS_SYSTEM)) != NULL)
608 DBusMessage *msg = NULL;
610 if (e_dbus_request_name(conn, "User.Bluetooth.UG", 0, NULL, NULL) != NULL)
612 if ((msg = dbus_message_new_signal("/org/projectx/connect_device", "User.Bluetooth.UG", "Send")) != NULL)
616 char *name = address;
617 uint8_t temp[6] = { 0, };
618 uint8_t *addr = temp;
620 _ug_nfc_share_get_bt_addr_from_string(temp, address);
622 LOGD("msg [%p], reserved [%d], address [%02X:%02X:%02X:%02X:%02X:%02X], count [%d], files [%s]", msg, reserved, addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], count, files);
624 if (dbus_message_append_args(msg,
625 DBUS_TYPE_INT32, &reserved,
626 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &addr, 6,
627 DBUS_TYPE_INT32, &count,
628 DBUS_TYPE_STRING, &files,
629 DBUS_TYPE_STRING, &type,
630 DBUS_TYPE_STRING, &name,
633 e_dbus_message_send(conn, msg, NULL, -1, NULL);
635 LOGD("Send success");
639 LOGE("Connect sending failed");
644 dbus_message_unref(msg);
648 LOGE("dbus_message_new_signal failed");
655 LOGE("e_dbus_request_name failed");
662 LOGE("e_dbus_bus_get failed");
671 LOGE("e_dbus_init failed");