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.
17 #include <Elementary.h>
28 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
29 pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
36 Eina_Bool timeout_handler(void *data){
37 if( timeout_counter == 0 ){
39 printf("Fail, timeout!\n");
50 int print_result(const char * func, int error )
52 printf("%s ret = %x\n", func , error);
58 char *byteToString(unsigned char* buffer, int size){
59 static char localbuffer[255];
60 memset(localbuffer, 0, 255);
61 memcpy(localbuffer, buffer, size);
65 void print_ndef_message(nfc_ndef_message_h message){
67 "NFC_RECORD_TNF_EMPTY",
68 "NFC_RECORD_TNF_WELL_KNOWN",
69 "NFC_RECORD_TNF_MIME_MEDIA",
71 "NFC_RECORD_TNF_EXTERNAL_RTD",
72 "NFC_RECORD_TNF_UNKNOWN",
73 "NFC_RECORD_TNF_UNCHAGNED"
78 unsigned char *payload;
80 nfc_ndef_record_h record;
83 ret = nfc_ndef_message_get_record(message, 0 , &record);
85 printf("can't found NDEF Record\n");
90 nfc_ndef_record_get_tnf(record, &tnf);
91 nfc_ndef_record_get_type(record, &type, &type_size);
92 nfc_ndef_record_get_payload(record, &payload, &payload_size);
94 printf("tnf \t: %s\n", tnf_tbl[tnf]);
95 printf("type \t: %s\n", byteToString(type, type_size));
96 printf("payload : %s\n", byteToString(payload, payload_size) );
98 if( tnf == NFC_RECORD_TNF_WELL_KNOWN && type[0] == 'U' ){
100 nfc_ndef_record_get_uri(record, &uri);
101 printf("uri \t: %s\n", uri);
105 if( tnf == NFC_RECORD_TNF_WELL_KNOWN && type[0] == 'T' ){
107 nfc_ndef_record_get_text(record, &text);
108 printf("text \t: %s\n", text);
110 nfc_ndef_record_get_langcode(record , &text);
111 printf("langcode : %s\n", text);
118 void ndef_record_create_test(nfc_error_e error, void *user_data){
121 "NFC_RECORD_TNF_EMPTY",
122 "NFC_RECORD_TNF_WELL_KNOWN",
123 "NFC_RECORD_TNF_MIME_MEDIA",
124 "NFC_RECORD_TNF_URI",
125 "NFC_RECORD_TNF_EXTERNAL_RTD",
126 "NFC_RECORD_TNF_UNKNOWN",
127 "NFC_RECORD_TNF_UNCHAGNED"
129 nfc_ndef_record_h record1;
130 nfc_record_tnf_e tnf;
135 unsigned char *payload;
142 printf("---------------------------------------------------\n");
143 printf(" NEF Record Create Test\n");
145 ret = nfc_ndef_record_create(&record1, NFC_RECORD_TNF_WELL_KNOWN, NFC_RECORD_SMART_POSTER_TYPE, sizeof(NFC_RECORD_SMART_POSTER_TYPE), (unsigned char*)"id", strlen("id"), (unsigned char*)"testpayload", strlen("testpayload"));
146 print_result("nfc_ndef_record_create" , ret);
148 nfc_ndef_record_get_tnf(record1, &tnf);
149 nfc_ndef_record_get_type(record1, &type, &type_size);
150 nfc_ndef_record_get_id(record1, &id, &id_size);
151 nfc_ndef_record_get_payload(record1, &payload, &payload_size);
153 printf("tnf \t: %s\n", tnf_tbl[tnf]);
154 printf("type \t: %s\n", byteToString(type, type_size));
155 printf("id \t: %s\n" , byteToString(id, id_size) );
156 printf("payload : %s\n", byteToString(payload, payload_size) );
158 nfc_ndef_record_destroy(record1);
163 ret = nfc_ndef_record_create_mime(&record1, "text/plain", (unsigned char*)"the text record", sizeof("the text record"));
164 print_result("nfc_ndef_record_create_mime" , ret);
166 nfc_ndef_record_get_tnf(record1, &tnf);
167 nfc_ndef_record_get_mime_type(record1 , &strp);
168 nfc_ndef_record_get_type(record1, &type, &type_size);
169 nfc_ndef_record_get_payload(record1, &payload, &payload_size);
171 printf("tnf \t: %s\n", tnf_tbl[tnf]);
172 printf("mimetype: %s\n", strp);
173 printf("type \t: %s\n", byteToString(type, type_size));
174 printf("payload : %s\n", byteToString(payload, payload_size) );
177 nfc_ndef_record_destroy(record1);
182 ret = nfc_ndef_record_create_text(&record1, "the text record", "us-en", NFC_ENCODE_UTF_8);
183 print_result("nfc_ndef_record_create_text" , ret);
185 nfc_ndef_record_get_tnf(record1, &tnf);
186 nfc_ndef_record_get_type(record1, &type, &type_size);
187 nfc_ndef_record_get_payload(record1, &payload, &payload_size);
188 nfc_ndef_record_get_text(record1, &strp);
189 nfc_ndef_record_get_langcode(record1, &strp2);
191 printf("tnf \t: %s\n", tnf_tbl[tnf]);
192 printf("type \t: %s\n", byteToString(type, type_size));
193 printf("payload : %s\n", byteToString(payload, payload_size) );
194 printf("text \t: %s\n", strp);
195 printf("langcode: %s\n", strp2);
196 nfc_ndef_record_destroy(record1);
202 ret = nfc_ndef_record_create_uri(&record1,"http://samsung.com");
203 print_result("nfc_ndef_record_create_uri" , ret);
206 nfc_ndef_record_get_tnf(record1, &tnf);
207 nfc_ndef_record_get_type(record1, &type, &type_size);
208 nfc_ndef_record_get_payload(record1, &payload, &payload_size);
209 nfc_ndef_record_get_uri(record1, &strp);
211 printf("tnf \t: %s\n", tnf_tbl[tnf]);
212 printf("type \t: %s\n", byteToString(type, type_size));
213 printf("payload : %s\n", byteToString(payload, payload_size) );
214 printf("uri \t: %s\n", strp);
216 nfc_ndef_record_destroy(record1);
222 ret = nfc_ndef_record_create_uri(&record1,"http://samsung.com");
223 print_result("nfc_ndef_record_create_uri" , ret);
225 ret = nfc_ndef_record_get_mime_type(record1, &strp);
226 print_result("nfc_ndef_record_get_mime_type" , ret);
227 ret = nfc_ndef_record_get_text(record1,&strp);
228 print_result("nfc_ndef_record_get_text" , ret);
229 ret = nfc_ndef_record_get_langcode(record1,&strp);
230 print_result("nfc_ndef_record_get_langcode" , ret);
231 ret = nfc_ndef_record_get_uri(record1,&strp);
232 print_result("nfc_ndef_record_get_uri" , ret);
235 nfc_ndef_record_destroy(record1);
237 printf("---------------------------------------------------\n");
246 void _ndef_discovered_cb(nfc_ndef_message_h message, void * user_data){
247 printf("Discovered NDEF Message!\n");
248 print_ndef_message(message);
253 void read_ndef_from_tag_using_ndef_discovered(nfc_error_e error, void *user_data){
257 timeout_counter = 30;
259 ret = nfc_manager_set_ndef_discovered_cb(_ndef_discovered_cb , NULL);
260 print_result("nfc_manager_set_ndef_discovered_cb", ret);
261 printf("Now, Bring the tag closer.\n");
262 ecore_timer_add(1, timeout_handler, NULL);
266 void _write_completed_cb(nfc_error_e result , void * user_data){
268 printf("write completed!result %s\n" , result == 0 ? "Success": "Fail");
274 void _tag_discovered_cb(nfc_discovered_type_e type, nfc_tag_h tag, void * user_data){
275 if( type == NFC_DISCOVERED_TYPE_ATTACHED ){
277 printf("Discovered Tag!\n");
279 nfc_ndef_record_h record;
280 nfc_ndef_message_h message;
281 nfc_ndef_record_create_uri(&record, "http://samsung.com");
282 nfc_ndef_message_create(&message);
283 nfc_ndef_message_append_record(message, record);
284 printf("Write request!\n");
285 timeout_counter = 30;
286 nfc_tag_write_ndef(tag, message , _write_completed_cb , NULL);
287 nfc_ndef_message_destroy(message);
292 void write_ndef_to_tag(nfc_error_e error, void *user_data){
297 printf("write_ndef_to_tag\n");
298 ret = nfc_manager_get_connected_tag(&tag);
299 printf("nfc_manager_get_connected_tag ret(0x%08x) !\n",ret);
302 if ( ret == NFC_ERROR_NONE)
304 printf("tag alread attached !\n");
306 nfc_ndef_record_h record;
307 nfc_ndef_message_h message;
308 nfc_ndef_record_create_uri(&record, "http://samsung.com");
309 nfc_ndef_message_create(&message);
310 nfc_ndef_message_append_record(message, record);
311 printf("Write request!\n");
312 timeout_counter = 30;
313 nfc_tag_write_ndef(tag, message , _write_completed_cb , NULL);
314 nfc_ndef_message_destroy(message);
322 timeout_counter = 30;
324 ret = nfc_manager_set_tag_discovered_cb( _tag_discovered_cb , NULL);
325 print_result("nfc_manager_set_tag_discovered_cb", ret);
327 printf("Now, Bring the tag closer. Will be writen a new NDEF Message\n");
328 ecore_timer_add(1, timeout_handler, NULL);
334 void _send_completed_cb(nfc_error_e result , void *user_data){
335 printf("send completed!result %s\n" , result == 0 ? "Success": "Fail");
342 void _handover_completed_cb(nfc_error_e result, nfc_ac_type_e carrior, void * ac_data, int ac_data_size , void *user_data){
344 char * address =(char *) (strdup(ac_data));
346 printf("handover completed!result %d AC type is [%d] \n" , result,carrior);
347 printf("address [%s] address size[%d]\n" , address, ac_data_size);
354 Eina_Bool send_test(void *data){
356 nfc_p2p_target_h target = (nfc_p2p_target_h)data;
357 nfc_ndef_message_h message;
358 nfc_ndef_record_h record;
360 nfc_ndef_record_create_uri(&record, "http://samsung.com");
361 nfc_ndef_message_create(&message);
362 nfc_ndef_message_append_record(message, record);
363 printf("Send Request!\n");
365 nfc_p2p_send(target, message , _send_completed_cb, NULL);
366 nfc_ndef_message_destroy(message);
371 Eina_Bool handover_test(void *data){
373 nfc_p2p_target_h target = (nfc_p2p_target_h)data;
374 nfc_ac_type_e type = NFC_AC_TYPE_BT;
376 if(nfc_p2p_is_supported_ac_type(type))
377 printf("NFC_AC_TYPE_BT supported\n");
379 nfc_p2p_connection_handover(target, type , _handover_completed_cb, NULL);
384 void _target_discovered_cb(nfc_discovered_type_e type, nfc_p2p_target_h target, void * user_data){
385 if( type == NFC_DISCOVERED_TYPE_ATTACHED){
386 printf("Discovered new target!\n");
387 ecore_idler_add(send_test, target);
391 void _target_discovered_for_connect_handover_cb(nfc_discovered_type_e type, nfc_p2p_target_h target, void * user_data){
392 if( type == NFC_DISCOVERED_TYPE_ATTACHED){
393 printf("Discovered new target!\n");
394 ecore_idler_add(handover_test, target);
398 void send_ndef_to_peer(nfc_error_e error, void *user_data){
400 nfc_p2p_target_h target;
403 printf("send_ndef_to_peer\n");
405 ret = nfc_manager_get_connected_target(&target);
406 if ( ret == NFC_ERROR_NONE)
408 printf("target already attached!\n");
409 ecore_idler_add(send_test, target);
417 ret = nfc_manager_set_p2p_target_discovered_cb(_target_discovered_cb , NULL);
418 printf("Now, Bring the target closer. Will be sent a new NDEF Message\n");
419 ecore_timer_add(1, timeout_handler, NULL);
423 void connect_handover_to_peer(nfc_error_e error, void *user_data){
427 ret = nfc_manager_set_p2p_target_discovered_cb(_target_discovered_for_connect_handover_cb , NULL);
428 printf("Now, Bring the target closer. Will be tried to connect handover\n");
429 ecore_timer_add(1, timeout_handler, NULL);
432 void _p2p_recv_cb(nfc_p2p_target_h target , nfc_ndef_message_h message, void *user_data){
434 printf("recevie a new message!\n");
435 print_ndef_message(message);
442 void _target_discovered_cb2(nfc_discovered_type_e type, nfc_p2p_target_h target, void * user_data){
443 if( type == NFC_DISCOVERED_TYPE_ATTACHED){
444 printf("Discovered new target!\n");
445 printf("I wait a new Message.......\n");
446 timeout_counter = 30;
447 nfc_p2p_set_data_received_cb(target, _p2p_recv_cb,NULL);
449 printf("Peer target is detached!!!!\n");
455 void recv_ndef_from_peer(nfc_error_e error, void *user_data){
458 timeout_counter = 30;
459 ret = nfc_manager_set_p2p_target_discovered_cb(_target_discovered_cb2 , NULL);
460 printf("Now, Bring the target closer. and send a new NDEF Message to the this target\n");
461 ecore_timer_add(1, timeout_handler, NULL);
466 void _activation_changed_cb(bool activated , void *user_data){
467 if( activated == true){
468 printf("NET_NFC_MESSAGE_INIT received\n");
469 }else if( activated == false){
470 printf("NET_NFC_MESSAGE_DEINIT received\n");
475 void on_activation_changed_cb_test(nfc_error_e error, void *user_data){
478 timeout_counter = 30;
479 ret = nfc_manager_set_activation_changed_cb(_activation_changed_cb , NULL);
480 printf("Now, wait NET_NFC_MESSAGE_INIT or NET_NFC_MESSAGE_DEINIT\n");
481 ecore_timer_add(1, timeout_handler, NULL);
487 Eina_Bool check_terminate(void *data){
489 nfc_manager_deinitialize();
498 "------------------\n"
501 " 2. ON/OFF on_activation_changed_cb check\n"
502 "------------------\n"
504 " a. ndef record test\n"
505 " b. ndef discoverd cb test\n"
506 " c. write ndef to tag \n"
507 " d. send ndef to peer \n"
508 " e. recv ndef from peer\n"
509 " f. connect handover to peer\n"
510 "------------------\n"
512 " g. ON nfc_manager_enable_system_handler\n"
513 " h. OFF nfc_manager_enable_system_handler\n"
517 "------------------\n"
520 void set_activation_completed_cb(nfc_error_e error, void *user_data)
522 printf("set_activation_completed_cb ret(%d) \n", error);
526 int main(int argc, char ** argv)
528 elm_init(argc, argv);
540 ret = nfc_manager_set_activation(0, set_activation_completed_cb, NULL);
541 printf("nfc_manager_set_activation set off ret(0x%08x) \n", ret);
543 if (ret == NFC_ERROR_NOT_SUPPORTED)
545 printf("NFC_ERROR_NOT_SUPPORTED \n");
547 else if (ret == NFC_ERROR_ALREADY_ACTIVATED)
549 printf("NFC_ERROR_ALREADY_ACTIVATED \n");
552 else if (ret == NFC_ERROR_ALREADY_DEACTIVATED)
554 printf("NFC_ERROR_ALREADY_DEACTIVATED \n");
557 else if (ret == NFC_ERROR_OPERATION_FAILED)
559 printf("NFC_ERROR_OPERATION_FAILED \n");
566 ret = nfc_manager_set_activation(1, set_activation_completed_cb, NULL);
567 printf("nfc_manager_set_activation set off ret(0x%08x) \n", ret);
569 if (ret == NFC_ERROR_NOT_SUPPORTED)
571 printf("NFC_ERROR_NOT_SUPPORTED \n");
573 else if (ret == NFC_ERROR_ALREADY_ACTIVATED)
575 printf("NFC_ERROR_ALREADY_ACTIVATED \n");
578 else if (ret == NFC_ERROR_ALREADY_DEACTIVATED)
580 printf("NFC_ERROR_ALREADY_DEACTIVATED \n");
583 else if (ret == NFC_ERROR_OPERATION_FAILED)
585 printf("NFC_ERROR_OPERATION_FAILED \n");
592 ret = nfc_manager_initialize(on_activation_changed_cb_test,NULL);
593 print_result("on_activation_changed_cb", ret);
597 ret = nfc_manager_initialize(ndef_record_create_test,NULL);
598 print_result("nfc_manager_initialize", ret);
601 ret = nfc_manager_initialize(read_ndef_from_tag_using_ndef_discovered,NULL);
602 print_result("nfc_manager_initialize", ret);
605 ret = nfc_manager_initialize(write_ndef_to_tag,NULL);
606 print_result("nfc_manager_initialize", ret);
609 ret = nfc_manager_initialize(send_ndef_to_peer,NULL);
610 print_result("nfc_manager_initialize", ret);
613 ret = nfc_manager_initialize(recv_ndef_from_peer,NULL);
614 print_result("nfc_manager_initialize", ret);
617 ret = nfc_manager_initialize(connect_handover_to_peer,NULL);
618 print_result("nfc_manager_initialize", ret);
623 ret = nfc_manager_set_system_handler_enable(true);
625 printf("nfc_manager_set_system_handler_enable(true) ret [%d] current [%d]\n", ret,nfc_manager_is_system_handler_enabled());
629 ret = nfc_manager_set_system_handler_enable(false);
630 printf("nfc_manager_set_system_handler_enable(false) ret [%d] current [%d]\n", ret,nfc_manager_is_system_handler_enabled());
637 printf("wrong selection!\n");
643 ecore_timer_add(1, check_terminate, NULL);