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.
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 UG_NFC_SHARE_DEBUG_ERR("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 UG_NFC_SHARE_DEBUG_ERR("nfc_ndef_message_destroy failed");
104 ug_data->current_ndef = ndef_msg;
108 return UG_NFC_SHARE_OK;
112 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)
114 ug_nfc_share_result_e result = UG_NFC_SHARE_ERROR;
115 char *extension = NULL;
117 UG_NFC_SHARE_BEGIN();
119 retv_if(path == NULL, result);
120 retv_if(type_data == NULL, result);
121 retv_if(type_size == NULL, result);
123 UG_NFC_SHARE_DEBUG("typedata = %p, typesize = %d", type_data, *type_size);
125 memset(type_data, 0, *type_size);
128 extension = strrchr(path, '.');
129 UG_NFC_SHARE_DEBUG("extension = %s", GET_SAFE_STRING(extension));
131 if (extension != NULL)
133 char *mime_str = NULL;
135 if (mime_type_get_mime_type(extension+1, &mime_str) == MIME_TYPE_ERROR_NONE)
137 UG_NFC_SHARE_DEBUG("mime_str[%s]", mime_str);
139 *type_size = strlen(mime_str);
140 memcpy(type_data, mime_str, *type_size);
141 result = UG_NFC_SHARE_OK;
145 UG_NFC_SHARE_DEBUG_ERR("ERROR :: mime_type_get_mime_type failed");
146 result = UG_NFC_SHARE_ERROR;
150 UG_NFC_SHARE_DEBUG("mime type : %s", GET_SAFE_STRING((char *)type_data));
157 ug_nfc_share_result_e ug_nfc_share_make_ndef_message_from_file(nfc_ndef_message_h *msg, const char *path)
159 int result = UG_NFC_SHARE_ERROR;
161 uint8_t type_buffer[50] = { 0, };
162 int type_size = sizeof(type_buffer);
163 nfc_ndef_record_h record = NULL;
165 char *file_name = NULL;
166 uint8_t *file_data = NULL;
167 long int file_len = 0;
170 UG_NFC_SHARE_BEGIN();
172 retv_if(msg == NULL, result);
173 retv_if(path == NULL, result);
174 /*Cause of Svace warning */
175 /*Name : TOCTTOU_SEQUENCE
177 1.use fstat inplace of stat
178 2.or using check-Use-Check Pattern
180 retv_if((stat(path, &st) == -1 && errno == ENOENT), result);
182 /* read file and make payload*/
183 file = fopen(path, "r");
186 long int read_count = 0, read_total = 0;
188 fseek(file, 0, SEEK_END);
189 file_len = ftell(file);
190 fseek(file, 0, SEEK_SET);
192 UG_NFC_SHARE_MEM_MALLOC(file_data, file_len, uint8_t);
193 if (file_data == NULL)
195 UG_NFC_SHARE_DEBUG_ERR("ERROR :: UG_NFC_SHARE_MEM_MALLOC failed");
204 read_count = fread(file_data + read_total, 1, file_len - read_total, file);
205 read_total += read_count;
207 while (read_count != 0 && read_total < file_len);
211 UG_NFC_SHARE_DEBUG("fread(%s) success, size %ld", path, file_len);
215 UG_NFC_SHARE_DEBUG_ERR("fopen(%s) error");
221 result = ug_nfc_share_make_mime_type_data_from_file_path(path, type_buffer, (uint32_t *)&type_size);
222 if (result != UG_NFC_SHARE_OK)
224 UG_NFC_SHARE_DEBUG_ERR("ERROR :: _make_mime_type_data_from_file_path failed [%d]", result);
229 /* get file name for id */
230 file_name = strrchr(path, '/');
231 if (file_name == NULL)
233 file_name = (char *)path;
240 UG_NFC_SHARE_DEBUG("file name : %s", file_name);
243 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);
244 if (result != NFC_ERROR_NONE)
246 UG_NFC_SHARE_DEBUG_ERR("nfc_ndef_record_create failed (%d)", result);
251 /* make file NDEF message */
252 result = nfc_ndef_message_create(msg);
253 if (result != NFC_ERROR_NONE)
255 UG_NFC_SHARE_DEBUG_ERR("nfc_ndef_message_create failed [%d]", result);
257 nfc_ndef_record_destroy(record);
262 /* append record to ndef message */
263 result = nfc_ndef_message_append_record(*msg, record);
264 if (result != NFC_ERROR_NONE)
266 UG_NFC_SHARE_DEBUG_ERR("nfc_ndef_message_append_record failed (%d)", result);
271 UG_NFC_SHARE_DEBUG("ug_nfc_share_make_ndef_message_from_file success");
279 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)
281 int result = UG_NFC_SHARE_ERROR;
283 uint8_t type_buffer[50] = { 0, };
284 int type_size = sizeof(type_buffer);
285 nfc_ndef_record_h record = NULL;
287 char *file_name = NULL;
288 uint8_t *file_data = NULL;
289 long int file_len = 0;
293 UG_NFC_SHARE_BEGIN();
295 retv_if(msg == NULL, result);
296 retv_if(path == NULL, result);
297 /*Cause of Svace warning */
298 /*Name : TOCTTOU_SEQUENCE
300 1.use fstat inplace of stat
301 2.or using check-Use-Check Pattern
304 for (index = 0; index < record_count; index++)
306 retv_if((stat(path[index], &st) == -1 && errno == ENOENT), result);
308 /* read file and make payload*/
309 file = fopen(path[index], "r");
313 long int read_count = 0, read_total = 0;
315 fseek(file, 0, SEEK_END);
316 file_len = ftell(file);
317 fseek(file, 0, SEEK_SET);
319 UG_NFC_SHARE_MEM_MALLOC(file_data, file_len, uint8_t);
320 if (file_data == NULL)
322 UG_NFC_SHARE_DEBUG_ERR("ERROR :: UG_NFC_SHARE_MEM_MALLOC failed");
331 read_count = fread(file_data + read_total, 1, file_len - read_total, file);
332 read_total += read_count;
334 while (read_count != 0 && read_total < file_len);
338 UG_NFC_SHARE_DEBUG("fread(%s) success, size %ld", path[index], file_len);
342 UG_NFC_SHARE_DEBUG_ERR("fopen(%s) error");
348 result = ug_nfc_share_make_mime_type_data_from_file_path(path[index], type_buffer, (uint32_t *)&type_size);
349 if (result != UG_NFC_SHARE_OK)
351 UG_NFC_SHARE_DEBUG_ERR("ERROR :: _make_mime_type_data_from_file_path failed [%d]", result);
356 /* get file name for id */
357 file_name = strrchr(path[index], '/');
358 if (file_name == NULL)
360 file_name = (char *)path[index];
367 UG_NFC_SHARE_DEBUG("file name : %s", file_name);
370 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);
371 if (result != NFC_ERROR_NONE)
373 UG_NFC_SHARE_DEBUG_ERR("nfc_ndef_record_create failed (%d)", result);
378 /* make file NDEF message */
379 result = nfc_ndef_message_create(msg);
380 if (result != NFC_ERROR_NONE)
382 UG_NFC_SHARE_DEBUG_ERR("nfc_ndef_message_create failed [%d]", result);
384 nfc_ndef_record_destroy(record);
389 /* append record to ndef message */
390 result = nfc_ndef_message_append_record(*msg, record);
391 if (result != NFC_ERROR_NONE)
393 UG_NFC_SHARE_DEBUG_ERR("nfc_ndef_message_append_record failed (%d)", result);
398 UG_NFC_SHARE_DEBUG("ug_nfc_share_make_ndef_message_from_file success");
405 void _ug_nfc_share_get_bt_addr_from_string(uint8_t *addr, char *addr_string)
409 if (addr == NULL || addr_string == NULL)
414 UG_NFC_SHARE_DEBUG("string : %s", addr_string);
416 UG_NFC_SHARE_MEM_STRDUP(temp, addr_string);
423 token = strtok(temp, ":");
427 value = strtol(token, NULL, 16);
429 addr[count++] = (uint8_t)value;
431 while ((token = strtok(NULL, ":")) != NULL);
433 UG_NFC_SHARE_MEM_FREE(temp);
436 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)
438 UG_NFC_SHARE_BEGIN();
440 ugdata_t* ug_data = (ugdata_t*)user_data;
442 /* To prevent write event during showing popup, unset response callback */
443 ug_nfc_unset_nfc_callback();
446 if(nfc_manager_deinitialize () != NFC_ERROR_NONE)
448 UG_NFC_SHARE_DEBUG_ERR("nfc_manager_deinitialize failed");
451 if(result == NFC_ERROR_NONE)
455 UG_NFC_SHARE_DEBUG("p2p_connection_handover is completed");
457 data = (char *)bundle_get_val(ug_data->bd, "request_data");
459 UG_NFC_SHARE_DEBUG("uri[%d] = %s", strlen(data), data);
461 if (_bt_ipc_send_obex_message((char *)ac_data, (uint8_t *)data, strlen(data) + 1) == 0)
463 _show_status_text(ug_data, IDS_SHARED);
467 UG_NFC_SHARE_DEBUG_ERR("_bt_ipc_send_obex_message failed");
469 _show_status_text(ug_data, IDS_UNABLE_TO_SHARE);
474 UG_NFC_SHARE_DEBUG_ERR("p2p_connection_handover failed");
476 _show_status_text(ug_data, IDS_UNABLE_TO_SHARE);
482 static void _p2p_send_completed_cb(nfc_error_e result, void *user_data)
484 UG_NFC_SHARE_BEGIN();
486 ugdata_t* ug_data = (ugdata_t*)user_data;
488 /* To prevent write event during showing popup, unset response callback */
489 ug_nfc_unset_nfc_callback();
492 if(nfc_manager_deinitialize () != NFC_ERROR_NONE)
494 UG_NFC_SHARE_DEBUG_ERR("nfc_manager_deinitialize failed");
497 if(result == NFC_ERROR_NONE)
499 UG_NFC_SHARE_DEBUG("_p2p_send_completed_cb is completed");
501 _show_status_text(ug_data, IDS_SHARED);
505 UG_NFC_SHARE_DEBUG_ERR("_p2p_send_completed_cb failed");
507 _show_status_text(ug_data, IDS_UNABLE_TO_SHARE);
513 static void _p2p_target_discovered_cb(nfc_discovered_type_e type, nfc_p2p_target_h target, void *user_data)
515 UG_NFC_SHARE_BEGIN();
517 ugdata_t* ug_data = (ugdata_t*)user_data;
519 if(type == NFC_DISCOVERED_TYPE_ATTACHED)
521 int result = NFC_ERROR_NONE;
523 UG_NFC_SHARE_DEBUG("NFC_DISCOVERED_TYPE_ATTACHED");
525 if (ug_nfc_share_get_tag_type() == UG_NFC_SHARE_TAG_HANDOVER)
527 UG_NFC_SHARE_DEBUG("UG_NFC_SHARE_TAG_HANDOVER");
529 /* The code below will be changed after capi is completed */
530 if ((result = nfc_p2p_connection_handover(target, NFC_AC_TYPE_UNKNOWN, _p2p_connection_handover_completed_cb, ug_data)) != NFC_ERROR_NONE)
532 UG_NFC_SHARE_DEBUG_ERR("nfc_p2p_connection_handover failed [%d]", result);
539 nfc_ndef_message_h msg = NULL;
541 msg = ug_nfc_share_get_current_ndef(ug_data);
544 UG_NFC_SHARE_DEBUG_ERR("nfc_ndef_message_h is NULL!!");
548 result = nfc_p2p_send(target, msg, _p2p_send_completed_cb, ug_data);
549 if(result != NFC_ERROR_NONE)
551 UG_NFC_SHARE_DEBUG_ERR("nfc_p2p_send failed[%d]", result);
558 UG_NFC_SHARE_DEBUG("NFC_DISCOVERED_TYPE_DETACHED");
564 void ug_nfc_set_nfc_callback(void *user_data)
566 UG_NFC_SHARE_BEGIN();
568 nfc_manager_set_p2p_target_discovered_cb(_p2p_target_discovered_cb, user_data);
573 void ug_nfc_unset_nfc_callback(void)
575 UG_NFC_SHARE_BEGIN();
577 nfc_manager_unset_p2p_target_discovered_cb();
582 int _bt_ipc_send_obex_message(char *address, const uint8_t *files, uint32_t length)
585 uint32_t i, count = 1;
586 E_DBus_Connection *conn = NULL;
588 UG_NFC_SHARE_BEGIN();
590 if (address == NULL || files == NULL)
592 UG_NFC_SHARE_DEBUG_ERR("invalid param [%p] [%p]", address, files);
597 for (i = 0; i < length; i++)
605 if (e_dbus_init() > 0)
607 if ((conn = e_dbus_bus_get(DBUS_BUS_SYSTEM)) != NULL)
609 DBusMessage *msg = NULL;
611 if (e_dbus_request_name(conn, "User.Bluetooth.UG", 0, NULL, NULL) != NULL)
613 if ((msg = dbus_message_new_signal("/org/projectx/connect_device", "User.Bluetooth.UG", "Send")) != NULL)
617 char *name = address;
618 uint8_t temp[6] = { 0, };
619 uint8_t *addr = temp;
621 _ug_nfc_share_get_bt_addr_from_string(temp, address);
623 UG_NFC_SHARE_DEBUG("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);
625 if (dbus_message_append_args(msg,
626 DBUS_TYPE_INT32, &reserved,
627 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &addr, 6,
628 DBUS_TYPE_INT32, &count,
629 DBUS_TYPE_STRING, &files,
630 DBUS_TYPE_STRING, &type,
631 DBUS_TYPE_STRING, &name,
634 e_dbus_message_send(conn, msg, NULL, -1, NULL);
636 UG_NFC_SHARE_DEBUG("Send success");
640 UG_NFC_SHARE_DEBUG_ERR("Connect sending failed");
645 dbus_message_unref(msg);
649 UG_NFC_SHARE_DEBUG_ERR("dbus_message_new_signal failed");
656 UG_NFC_SHARE_DEBUG_ERR("e_dbus_request_name failed");
663 UG_NFC_SHARE_DEBUG_ERR("e_dbus_bus_get failed");
672 UG_NFC_SHARE_DEBUG_ERR("e_dbus_init failed");