bug fix
[framework/connectivity/nfc-manager.git] / test_clinet_app / nfc_client / main.c
1 /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   *
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
7
8   *     http://www.tizenopensource.org/license
9   *
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.
15   */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <net_nfc_typedef.h>
21 #include <net_nfc.h>
22 #include <net_nfc_exchanger.h>
23
24 #include <pthread.h>
25 #include "nfc_api_test.h"
26
27 #include <glib.h>
28
29 /* Function definition list*/
30 int nfcTestClientInit(uint8_t testNumber,void* arg_ptr2);
31 int nfcTestNdefParser(uint8_t testNumber,void* arg_ptr2);
32 int nfcTestWriteMode(uint8_t testNumber,void* arg_ptr2);
33 int nfcTestReaderMode(uint8_t testNumber,void* arg_ptr2);
34 int nfcTestTransceive(uint8_t testNumber,void* arg_ptr2);
35 int nfcTestAPIBasicTest1(uint8_t testNumber,void* arg_ptr2);
36 int nfcTestAPIBasicTest2(uint8_t testNumber,void* arg_ptr2);
37 int nfcTestAPIBasicTest3(uint8_t testNumber,void* arg_ptr2);
38 int nfcTestReadWriteMode(uint8_t testNumber,void* arg_ptr2);
39 int nfcTestAPIMultipleRequest(uint8_t testNumber,void* arg_ptr2);
40 int nfcTestLLCP(uint8_t testNumber,void* arg_ptr2);
41 int nfcTestStressLLCP(uint8_t testNumber,void* arg_ptr2);
42 int nfcTestExchanger(uint8_t testNumber,void* arg_ptr2);
43 int nfcConnHandover(uint8_t testNumber,void* arg_ptr2);
44 int nfcTestAPIException (uint8_t testNumber,void* arg_ptr);
45 int nfcTestAPIException_tagAPI (uint8_t testNumber,void* arg_ptr);
46 int nfcTestAPIException_targetInfo (uint8_t testNumber,void* arg_ptr);
47 int nfcConnHandoverMessageTest (uint8_t testNumber,void* arg_ptr);
48 int nfcTestFormatNdef(uint8_t testNumber,void* arg_ptr2);
49 int nfcTestInternalSe(uint8_t testNumber,void* arg_ptr2);
50 int nfcTestSnep(uint8_t testNumber,void* arg_ptr);
51
52
53 void print_test_result (char * str, net_nfc_test_result_e result);
54
55 #define NET_NFC_TAG_DISCOVERED                  1
56 #define NET_NFC_TAG_CONNECT                     (1 << 1)
57 #define NET_NFC_TAG_CHECK_NDEF          (1 << 2)
58 #define NET_NFC_TAG_NDEF_READ                   (1 << 3)
59 #define NET_NFC_TAG_NDEF_WRITE                  (1 << 4)
60 #define NET_NFC_TAG_NDEF_READ_BIG               (1 << 5)
61 #define NET_NFC_TAG_NDEF_WRITE_BIG      (1 << 6)
62 #define NET_NFC_TAG_DISCONNECT                  (1 << 7)
63 #define NET_NFC_MAX_TAG_TYPE    20
64
65 static nfcTestType nfcTestMatrix[] =
66 {
67 //      {"API STRESS_WRITE_READ TEST", nfcTestReadWriteMode, NET_NFC_TEST_NOT_YET}, // OK
68 //      {"Remove tag while writing", nfcTestWriteMode, NET_NFC_TEST_NOT_YET}, //OK
69 //      {"Remove tag while reading", nfcTestAPIBasicTest2, NET_NFC_TEST_NOT_YET}, // OK
70         //{"API BASIC TEST3", nfcTestAPIBasicTest3, NET_NFC_TEST_NOT_YET},
71 //      {"API MUTIPLE REQUEST", nfcTestAPIMultipleRequest, NET_NFC_TEST_NOT_YET}, // OK
72 //      {"API BASIC TEST1", nfcTestAPIBasicTest1, NET_NFC_TEST_NOT_YET}, // OK
73 //      {"Check NDEF message",          nfcTestNdefParser,              NET_NFC_TEST_NOT_YET}, // OK
74 //      {"write mode",                  nfcTestWriteMode,               NET_NFC_TEST_NOT_YET},
75 //      {"reader mode",                 nfcTestReaderMode,              NET_NFC_TEST_NOT_YET},
76 //      {"format ndef",                 nfcTestFormatNdef,              NET_NFC_TEST_NOT_YET},
77 //      {"internal_se_test",            nfcTestInternalSe,              NET_NFC_TEST_NOT_YET},
78 //      {"exchange mode",               nfcTestExchanger,               NET_NFC_TEST_NOT_YET},
79 //      {"Transceive Test",             nfcTestTransceive,              NET_NFC_TEST_NOT_YET},
80 //      {"LLCP Test",           nfcTestLLCP, NET_NFC_TEST_NOT_YET},
81 //      {"connection handover msg test",                nfcConnHandover, NET_NFC_TEST_NOT_YET},
82 //      {"API Exception Test",          nfcTestAPIException, NET_NFC_TEST_NOT_YET},
83 //      {"API Exception Test2",         nfcTestAPIException_tagAPI, NET_NFC_TEST_NOT_YET},
84 ///     {"API Exception Test3",         nfcTestAPIException_targetInfo, NET_NFC_TEST_NOT_YET},
85 //      {"LLCP Test",           nfcTestLLCP, NET_NFC_TEST_NOT_YET},
86 //      {"LLCP Test",           nfcTestStressLLCP, NET_NFC_TEST_NOT_YET},
87 //    {"Handover Message",      nfcConnHandoverMessageTest , NET_NFC_TEST_NOT_YET},
88         {"snep test",           nfcTestSnep,            NET_NFC_TEST_NOT_YET},
89         {NULL,                          NULL,                           NET_NFC_TEST_NOT_YET},
90 };
91
92 static uint32_t testDevType = 0;
93 static uint32_t testCardMaxNdefLength = 0;
94 static uint8_t tagTestResult[NET_NFC_MAX_TAG_TYPE];
95
96 static int read_count = 0;
97 static int write_count = 0;
98 static int detect_count = 0;
99
100 static net_nfc_target_handle_h tag_handle = NULL;
101 static int test_count = 0;
102
103
104
105 /* Below smart poster data can has problem or parser has some problem.
106         Need to check */
107 uint8_t  nfcTestSpHex[]= {0xD1,0x02,0x37,0x53,0x70,0x91,0x01,0x18,0x54,0x04,
108                         0x74,0x65,0x73,0x74,0x53,0x6D,0x61,0x72,0x74,0x70,
109                         0x6F,0x73,0x74,0x65,0x72,0x20,0x45,0x78,0x61,0x6D,
110                         0x70,0x6C,0x65,0x11,0x03,0x01,0x61,0x63,0x74,0x00,
111                         0x51,0x01,0x10,0x55,0x00,0x77,0x77,0x77,0x2E,0x73,
112                         0x61,0x6D,0x73,0x75,0x6E,0x67,0x2E,0x63,0x6F,0x6D};
113
114 /*
115 uint8_t  nfcTestSp[]={0xD1,0x02,0x24,0x53,0x70,0x91,0x01,0x14,0x54
116                                         ,0x00,0x53,0x6D,0x61,0x72,0x74,0x70,0x6F,0x73
117                                         ,0x74,0x65,0x72,0x20,0x45,0x78,0x61,0x6D,0x70
118                                         ,0x6C,0x65,0x11,0x03,0x01,0x61,0x63,0x74,0x00
119                                         ,0x51,0x01,0x01,0x55,0x00};
120 */
121
122 // test Text "Samsung Linux Platform NFC TEST"
123 uint8_t nfcTestTextHex[]= {0xD1,0x01,0x20,0x54,0x00,0x53,0x61,0x6D,0x73,0x75
124                                    ,0x6E,0x67,0x20,0x4C,0x69,0x6E,0x75,0x78,0x20,0x50
125                                    ,0x6C,0x61,0x74,0x66,0x6F,0x72,0x6D,0x20,0x4E,0x46
126                                    ,0x43,0x20,0x54,0x45,0x53,0x54};
127
128
129 // test URI "http://www.samsunglinuxplatform.nfctest.com"
130 uint8_t nfcTestUriHex[]={0xD1,0x01,0x2C,0x55,0x00,0x68,0x74,0x74,0x70,0x3A
131                                         ,0x2F,0x2F,0x77,0x77,0x77,0x2E,0x73,0x61,0x6D,0x73
132                                         ,0x75,0x6E,0x67,0x6C,0x69,0x6E,0x75,0x78,0x70,0x6C
133                                         ,0x61,0x74,0x66,0x6F,0x72,0x6D,0x2E,0x6E,0x66,0x63
134                                         ,0x74,0x65,0x73,0x74,0x2E,0x63,0x6F,0x6D};
135
136 uint8_t nfcTestText[] = "payload http://www.samsunglinuxplatform.nfctest.com";
137 uint8_t nfcTestUri[] = {0xD1,0x01,0x13,0x55,0x1,0x73,0x61,0x6D,0x73,0x75,0x6E,0x67,0x6D,0x6F,0x62,0x69,0x6C,0x65,0x2E,0x63,0x6F,0x6D,0x2F};
138
139
140
141 static pthread_cond_t pcond = PTHREAD_COND_INITIALIZER;
142 static pthread_mutex_t plock = PTHREAD_MUTEX_INITIALIZER;
143
144 static gboolean test_process_func(gpointer data)
145 {
146         uint8_t i, testNumber;
147         int count = 0;
148
149         while (nfcTestMatrix[count].testName != NULL){
150                 count ++;
151         }
152
153         for(i=0; i<count; i++)
154         {
155                 PRINT_INFO("%d. %s START\n", i, nfcTestMatrix[i].testName);
156
157                 if(nfcTestMatrix[i].testFn !=NULL){
158                         nfcTestMatrix[i].testResult = nfcTestMatrix[i].testFn(i, NULL);
159                         //print_test_result (nfcTestMatrix[i].testName, nfcTestMatrix[i].testResult);
160                 }
161         }
162
163         return false;
164 }
165
166 int main()
167 {
168
169         GMainLoop* loop = NULL;
170         loop = g_main_new(TRUE);
171
172         g_timeout_add_seconds(1, test_process_func, NULL); // 1secs
173         g_main_loop_run(loop);
174
175         return 0;
176 }
177
178 void print_test_result (char * str, net_nfc_test_result_e result)
179 {
180         if (result == NET_NFC_TEST_OK){
181                 PRINT_RESULT_SUCCESS("TEST [%s] is PASSED",str);
182         }
183         else if (result == NET_NFC_TEST_FAIL){
184                 PRINT_RESULT_FAIL("TEST [%s] is FAILED",str);
185         }
186         else {
187                 PRINT_INFO("TEST is being tested\n");
188         }
189 }
190
191
192 /*=================================================================================*/
193
194 static net_nfc_test_result_e test_case_result;
195
196 static void net_nfc_test_read_write_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data );
197 static void net_nfc_test_static_handover_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data);
198
199 static void net_nfc_test_client_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data )
200 {
201         // do nothing
202 }
203
204 int nfcTestClientInit(uint8_t testNumber,void* arg_ptr2)
205 {
206         net_nfc_error_e result;
207         result = net_nfc_initialize();
208         CHECK_RESULT(result);
209
210
211         result = net_nfc_set_response_callback (net_nfc_test_client_cb, NULL);
212         CHECK_RESULT(result);
213
214         result = net_nfc_unset_response_callback ();
215         CHECK_RESULT(result);
216
217         result = net_nfc_deinitialize ();
218         CHECK_RESULT(result);
219
220         return NET_NFC_TEST_OK;
221 }
222
223
224 net_nfc_exchanger_data_h net_nfc_exchanger_callback(net_nfc_exchanger_event_e event, void * user_param)
225 {
226         PRINT_INFO(" event = [%d] \n", event);
227         test_case_result = NET_NFC_TEST_OK;
228
229         switch(event)
230         {
231                 case NET_NFC_EXCHANGER_DATA_REQUESTED:
232                 {
233                         PRINT_INFO("exchange is requested");
234                         net_nfc_exchanger_data_h ex_data = NULL;
235                         net_nfc_error_e error = NET_NFC_OK;
236                         data_h payload = NULL;
237
238 /*
239                         uint8_t buffer[1024] = {0};
240
241                         int i = 0;
242                         for(; i< 1024; i++){
243                                 buffer[i] = 'a';
244                         }
245
246                         net_nfc_create_data(&payload, buffer, 1024);
247 */
248
249                         if((error = net_nfc_create_exchanger_data(&ex_data, (uint8_t *)"http://www.samsung.com")) == NET_NFC_OK)
250                         //if((error = net_nfc_create_exchanger_url_type_data(&ex_data, NET_NFC_SCHEMA_FULL_URI, (uint8_t *)"file://test.txt")) == NET_NFC_OK)
251                         //if((error = net_nfc_create_exchanger_raw_type_data(&ex_data, "text/plain", payload)) == NET_NFC_OK)
252                         {
253                                 return ex_data;
254                         }
255                         else
256                         {
257                                 PRINT_INFO("create exchanger data is failed = [%d]", error);
258                                 //pthread_cond_signal (&pcond);
259                                 return NULL;
260                         }
261                 }
262                 case NET_NFC_EXCHANGER_TRANSFER_FAILED:
263                 case NET_NFC_EXCHANGER_TRANSFER_COMPLETED:
264                 default:
265                 {
266                         //pthread_cond_signal (&pcond);
267
268                         if(event == NET_NFC_EXCHANGER_TRANSFER_COMPLETED)
269                         {
270                                 PRINT_INFO("transfer exchanger msg is completed");
271                         }
272                         else
273                         {
274                                 PRINT_INFO("transfer exchanger msg is failed");
275                         }
276
277                         return NULL;
278                 }
279         }
280
281 }
282
283 /*=================================================================================*/
284 static void net_nfc_test_reader_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
285 {
286         int user_context;
287
288         PRINT_INFO("user_param = [%d] trans_param = [%d]", user_param, trans_data);
289
290         switch(message)
291         {
292                 case NET_NFC_MESSAGE_TAG_DISCOVERED:{
293                         net_nfc_target_type_e type;
294                         net_nfc_target_handle_h id;
295                         bool is_ndef;
296                         net_nfc_target_info_h target_info = (net_nfc_target_info_h)data;
297
298                         net_nfc_get_tag_type (target_info, &type);
299                         net_nfc_get_tag_handle(target_info, &id);
300                         net_nfc_get_tag_ndef_support (target_info, &is_ndef);
301                         PRINT_INFO("target type: %d\n", type);
302                         PRINT_INFO("target id: %X\n", id);
303                         PRINT_INFO("Is NDEF supoort: %d\n", is_ndef);
304
305                         if (is_ndef){
306                                 int actual_size;
307                                 int max_size;
308                                 net_nfc_get_tag_actual_data_size (target_info ,&actual_size);
309                                 net_nfc_get_tag_max_data_size (target_info ,&max_size);
310                                 PRINT_INFO("\tmax data [%d]\tactual data [%d]\n", max_size,actual_size );
311                                 net_nfc_read_tag (id, (void *)2);
312                         }
313                         else{
314                                 PRINT_INSTRUCT("Please use NDEF formated tag!!");
315                                 test_case_result = NET_NFC_TEST_FAIL;
316                         }
317                         break;
318                 }
319                 case NET_NFC_MESSAGE_READ_NDEF:{
320                         if(data != NULL){
321                                 ndef_message_h ndef = (ndef_message_h)(data);
322                                 data_h rawdata;
323                                 net_nfc_create_rawdata_from_ndef_message (ndef ,&rawdata);
324                                 PRINT_INFO("read ndef message is ok, length is [%d]", net_nfc_get_data_length(rawdata));
325                                 //_//net_nfc_ndef_print_message (ndef);
326                                 /*
327
328                                 if (memcmp(net_nfc_get_data_buffer (rawdata),nfcTestSpHex, net_nfc_get_data_length (rawdata)) == 0){
329                                         test_case_result = NET_NFC_TEST_OK;
330                                 }
331                                 */
332                         }
333                         pthread_mutex_lock (&plock);
334                         pthread_cond_signal (&pcond);
335                         pthread_mutex_unlock (&plock);
336                         break;
337                 }
338                 default:
339                         break;
340         }
341 }
342
343
344 static void net_nfc_test_format_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
345 {
346         int user_context;
347
348         PRINT_INFO("user_param = [%d] trans_param = [%d]", user_param, trans_data);
349
350         switch(message)
351         {
352                 case NET_NFC_MESSAGE_TAG_DISCOVERED:{
353
354                         PRINT_INFO("NET_NFC_MESSAGE_TAG_DISCOVERED");
355
356                         net_nfc_target_type_e type;
357                         net_nfc_target_handle_h id;
358                         bool is_ndef;
359                         net_nfc_target_info_h target_info = (net_nfc_target_info_h)data;
360
361                         net_nfc_get_tag_type (target_info, &type);
362                         net_nfc_get_tag_handle(target_info, &id);
363                         net_nfc_get_tag_ndef_support (target_info, &is_ndef);
364                         PRINT_INFO("target type: %d\n", type);
365                         PRINT_INFO("target id: %X\n", id);
366                         PRINT_INFO("Is NDEF supoort: %d\n", is_ndef);
367
368                         data_h ndef_key = NULL;
369                         net_nfc_mifare_create_net_nfc_forum_key(&ndef_key);
370                         net_nfc_format_ndef(id, ndef_key, NULL);
371
372                 }
373                 break;
374
375                 case NET_NFC_MESSAGE_FORMAT_NDEF:{
376                         PRINT_INFO("NET_NFC_MESSAGE_FORMAT_NDEF");
377                         PRINT_INFO("result = [%d]", result);
378                 }
379                 break;
380                 case NET_NFC_MESSAGE_TAG_DETACHED:
381                 {
382                         PRINT_INFO("NET_NFC_MESSAGE_TAG_DETACHED");
383                 }
384                 break;
385                 default:
386                         break;
387         }
388 }
389
390 static void net_nfc_test_se_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
391 {
392         switch(message)
393         {
394                 case NET_NFC_MESSAGE_GET_SE :
395                 {
396                         PRINT_INFO("NET_NFC_MESSAGE_GET_SE result = [%d] se type = [%d]", result, *((uint8_t *)data));
397                         //net_nfc_set_secure_element_type(NET_NFC_SE_TYPE_ESE, NULL);
398                         net_nfc_set_secure_element_type(NET_NFC_SE_TYPE_UICC, NULL);
399                 }
400                 break;
401                 case NET_NFC_MESSAGE_SET_SE :
402                 {
403                         PRINT_INFO("NET_NFC_MESSAGE_SET_SE result = [%d] se type = [%d]", result, *((uint8_t *)data));
404                         net_nfc_open_internal_secure_element(NET_NFC_SE_TYPE_ESE, NULL);
405                         //net_nfc_open_internal_secure_element(NET_NFC_SE_TYPE_UICC, NULL);
406                 }
407                 break;
408                 case NET_NFC_MESSAGE_OPEN_INTERNAL_SE :
409                 {
410                         PRINT_INFO("NET_NFC_MESSAGE_OPEN_INTERNAL_SE result = [%d] and handle = [0x%x]", result, data);
411                         data_h apdu = NULL;
412                         uint8_t apdu_cmd[4] = {0x00, 0xA4, 0x00, 0x0C} ; // CLA 0-> use default channel and no secure message. 0xA4 -> select instruction
413                         net_nfc_create_data(&apdu, apdu_cmd, 4);
414                         net_nfc_send_apdu((net_nfc_target_handle_h)(data), apdu, data);
415
416                 }
417                 break;
418                 case NET_NFC_MESSAGE_SEND_APDU_SE:
419                 {
420                         PRINT_INFO("NET_NFC_MESSAGE_SEND_APDU_SE result = [%d]", result);
421
422                         if(data != NULL)
423                         {
424                                 uint8_t * r_buffer = net_nfc_get_data_buffer (data);
425                                 uint32_t r_buffer_length = net_nfc_get_data_length (data);
426
427                                 PRINT_INFO ("Received data [size:%d] \n", net_nfc_get_data_length (data));
428
429                                 int idx = 0;
430                                 for (idx = 0; idx < r_buffer_length; idx++){
431                                         printf (" %02X", r_buffer[idx]);
432                                 }printf ("\n");
433                         }
434
435                         net_nfc_close_internal_secure_element((net_nfc_target_handle_h)trans_data, NULL);
436                 }
437                 break;
438                 case NET_NFC_MESSAGE_CLOSE_INTERNAL_SE :
439                 {
440
441                         PRINT_INFO("NET_NFC_MESSAGE_CLOSE_INTERNAL_SE result = [%d]", result);
442                 }
443                 break;
444                 default:
445                         break;
446         }
447
448         PRINT_INFO("user_param = [%d] trans_param = [%d]", user_param, trans_data);
449 }
450
451 /*=================================================================================*/
452 static void net_nfc_test_static_handover_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
453 {
454         int user_context;
455         switch(message)
456         {
457                 case NET_NFC_MESSAGE_TAG_DISCOVERED:{
458                         net_nfc_target_type_e type;
459                         net_nfc_target_handle_h id;
460                         bool is_ndef;
461                         net_nfc_target_info_h target_info = (net_nfc_target_info_h)data;
462                         net_nfc_carrier_config_h * config;
463                         ndef_record_h record;
464
465                         net_nfc_get_tag_type (target_info, &type);
466                         net_nfc_get_tag_handle(target_info, &id);
467                         net_nfc_get_tag_ndef_support (target_info, &is_ndef);
468                         PRINT_INFO("target type: %d\n", type);
469                         PRINT_INFO("target id: %X\n", id);
470                         PRINT_INFO("Is NDEF supoort: %d\n", is_ndef);
471
472                         if (is_ndef){
473
474                                 ndef_message_h ndef = NULL;
475                                 data_h bt_config = NULL;
476                                 net_nfc_create_data_only(&bt_config);
477
478                                 // 2byte :: OOB length, 6byte :: bt addr
479                                 uint8_t temp[8] = {0x00, 0x08, 0x00, 0x02, 0x78, 0xDD, 0xC4, 0x8A};
480                                 net_nfc_create_carrier_config (&config,NET_NFC_CONN_HANDOVER_CARRIER_BT );
481                                 net_nfc_add_carrier_config_property (config ,NET_NFC_BT_ATTRIBUTE_ADDRESS ,8 ,temp);
482                                 net_nfc_create_ndef_record_with_carrier_config (&record, config);
483                                 net_nfc_create_handover_select_message (&ndef);
484                                 net_nfc_append_carrier_config_record (message,record,NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE);
485
486                                 net_nfc_write_ndef(id, ndef, NULL);
487
488                         }
489                         else{
490                                 PRINT_INSTRUCT("Please use NDEF formated tag!!");
491                                 test_case_result = NET_NFC_TEST_FAIL;
492                         }
493                         break;
494                 }
495                 case NET_NFC_MESSAGE_WRITE_NDEF:{
496
497                         if(result == NET_NFC_OK){
498                                 PRINT_INFO("write ndef message is ok");
499                                 test_case_result = NET_NFC_TEST_OK;
500                         }
501
502                         pthread_mutex_lock (&plock);
503                         pthread_cond_signal (&pcond);
504                         pthread_mutex_unlock (&plock);
505                         break;
506                 }
507                 default:
508                         break;
509         }
510 }
511
512 int nfcConnHandover(uint8_t testNumber,void* arg_ptr2)
513 {
514         int user_context = 0;
515         net_nfc_error_e result;
516 #if 1
517         test_case_result = NET_NFC_TEST_FAIL;
518
519         result = net_nfc_initialize();
520
521         net_nfc_state_activate(1);
522
523         CHECK_RESULT(result);
524         result = net_nfc_set_response_callback (net_nfc_test_static_handover_cb, NULL);
525         CHECK_RESULT(result);
526
527         PRINT_INSTRUCT("Please close a tag to device!!");
528
529         //pthread_cond_wait (&pcond,&plock );
530         /*
531         PRINT_INFO("operation is end");
532
533         result = net_nfc_unset_response_callback ();
534
535         CHECK_RESULT(result);
536         result = net_nfc_deinitialize ();
537         CHECK_RESULT(result);
538         */
539 #else
540
541 /*
542         ndef_message_h ndef = NULL;
543
544         if(net_nfc_retrieve_current_ndef_message(&ndef) == NET_NFC_OK){
545                 PRINT_INFO("retrieve is ok");
546         }
547         else{
548                 PRINT_INFO("retrieve is failed");
549                 return NET_NFC_TEST_FAIL;
550         }
551 */
552         ndef_message_h ndef = NULL;
553         data_h bt_config = NULL;
554         net_nfc_create_data_only(&bt_config);
555
556         // 2byte :: OOB length, 6byte :: bt addr
557         uint8_t temp[8] = {0x00, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
558
559         net_nfc_set_data(bt_config, temp, 8);
560         net_nfc_create_static_connection_handover_select_message(&ndef, bt_config, NULL);
561
562         net_nfc_conn_handover_info_h handover_info = NULL;
563
564         if(net_nfc_parse_connection_handover_ndef_message(ndef, &handover_info) == NET_NFC_OK){
565                 PRINT_INFO("parsing is ok");
566
567                 uint8_t version = 0;
568
569                 if((result = net_nfc_get_connection_handover_version(handover_info, &version)) == NET_NFC_OK){
570                         PRINT_INFO("version = [%d]", version);
571                 }
572                 else{
573                         PRINT_INFO("get version is failed = [%d]", result);
574                 }
575
576
577                 unsigned short random_number = 0;
578                 if((result = net_nfc_get_connection_handover_random_number(handover_info, &random_number)) == NET_NFC_OK)
579                 {
580                         PRINT_INFO("random_number = [%d]", random_number);
581                 }
582                 else
583                 {
584                         PRINT_INFO("get random_number is failed = [%d]", result);
585                 }
586
587                 data_h record_type = NULL;
588                 net_nfc_get_connection_handover_record_type(handover_info, &record_type);
589                 PRINT_INFO("record type = [%c] [%c]", (net_nfc_get_data_buffer(record_type))[0], (net_nfc_get_data_buffer(record_type))[1]);
590
591                 uint8_t carrier_count = 0;
592                 net_nfc_get_connection_handover_alternative_carrier_count(handover_info, &carrier_count);
593
594                 int i = 0;
595                 for(; i < carrier_count; i++)
596                 {
597                         net_nfc_conn_handover_carrier_info_h carrier_info = NULL;
598                         net_nfc_get_connection_handover_carrier_handle_by_index(handover_info, i, &carrier_info);
599
600                         if(carrier_info != NULL){
601                                 net_nfc_conn_handover_carrier_type_e carrier_type = 0;
602                                 net_nfc_get_carrier_type(carrier_info, &carrier_type);
603
604                                 if(carrier_type == NET_NFC_CONN_HANDOVER_CARRIER_BT)
605                                 {
606                                         PRINT_INFO("carrier is BT");
607                                 }
608                                 else if(carrier_type == NET_NFC_CONN_HANDOVER_CARRIER_WIFI)
609                                 {
610                                         PRINT_INFO("carrier is WIFI");
611                                 }
612                                 else
613                                 {
614                                         PRINT_INFO("carrier is unknown");
615                                 }
616
617                                 data_h id = NULL;
618                                 net_nfc_get_carrier_id(carrier_info, &id);
619
620                                 PRINT_INFO("id = [0x%x]", (net_nfc_get_data_buffer(id))[0]);
621
622                                 net_nfc_conn_handover_carrier_state_e carrier_state = 0;
623                                 net_nfc_get_carrier_power_state(carrier_info, &carrier_state);
624
625                                 PRINT_INFO("cps is = [0x%x]", carrier_state);
626
627                                 data_h config = NULL;
628                                 if((result = net_nfc_get_carrier_configuration(carrier_info, &config)) == NET_NFC_OK)
629                                 {
630                                         PRINT_INFO("good to get config");
631                                 }
632                                 else
633                                 {
634                                         PRINT_INFO("failed to get config = [%d]", result);
635                                 }
636
637                                 uint8_t* buffer = NULL;
638                                 if((buffer = net_nfc_get_data_buffer(config)) != NULL)
639                                 {
640                                         unsigned short size = ( buffer[0] << 8 ) |buffer[1];
641                                         PRINT_INFO("size is = [%d]", size);
642
643                                         if(size == 8)
644                                         {
645                                                 PRINT_INFO("carrier addr is [0x%x]", buffer[2]);
646                                                 PRINT_INFO("carrier addr is [0x%x]", buffer[3]);
647                                                 PRINT_INFO("carrier addr is [0x%x]", buffer[4]);
648                                                 PRINT_INFO("carrier addr is [0x%x]", buffer[5]);
649                                                 PRINT_INFO("carrier addr is [0x%x]", buffer[6]);
650                                                 PRINT_INFO("carrier addr is [0x%x]", buffer[7]);
651                                         }
652                                         else
653                                         {
654                                                 PRINT_INFO("more data -_-;; ");
655                                         }
656                                 }
657                                 else
658                                 {
659                                         PRINT_INFO("failed to buffer");
660                                 }
661                         }
662
663                 }
664         }
665         else{
666
667                 PRINT_INFO("parsing is failed");
668
669                 return NET_NFC_TEST_FAIL;
670         }
671
672
673         if(handover_info != NULL){
674                 if((net_nfc_free_connection_handover_info(handover_info)) == NET_NFC_OK){
675                         PRINT_INFO("free is good");
676                 }
677                 else{
678                         PRINT_INFO("free is failed");
679                 }
680         }
681 #endif
682
683         return test_case_result;
684 }
685
686 int nfcTestFormatNdef(uint8_t testNumber,void* arg_ptr2)
687 {
688         PRINT_INFO("%s is start", __func__);
689
690         int user_context = 0;
691         net_nfc_error_e result;
692         test_case_result = NET_NFC_TEST_FAIL;
693
694         result = net_nfc_initialize();
695
696         net_nfc_state_activate(1);
697
698         CHECK_RESULT(result);
699         result = net_nfc_set_response_callback (net_nfc_test_format_cb, (void *)1);
700         CHECK_RESULT(result);
701
702         PRINT_INSTRUCT("Please close a tag to device!!");
703
704         //pthread_cond_wait (&pcond,&plock );
705
706 /*
707         PRINT_INFO("operation is end");
708
709         result = net_nfc_unset_response_callback ();
710
711         CHECK_RESULT(result);
712         result = net_nfc_deinitialize ();
713         CHECK_RESULT(result);
714
715 */
716
717         return test_case_result;
718 }
719
720 int nfcTestInternalSe(uint8_t testNumber,void* arg_ptr2)
721 {
722         PRINT_INFO("%s is start", __func__);
723
724         int user_context = 0;
725         net_nfc_error_e result;
726         test_case_result = NET_NFC_TEST_FAIL;
727
728         result = net_nfc_initialize();
729
730         net_nfc_state_activate(1);
731
732         CHECK_RESULT(result);
733         result = net_nfc_set_response_callback (net_nfc_test_se_cb, (void *)1);
734         CHECK_RESULT(result);
735
736         net_nfc_get_secure_element_type(NULL);
737
738         PRINT_INSTRUCT("Please close a tag to device!!");
739
740         return test_case_result;
741 }
742
743
744
745 int nfcTestReaderMode(uint8_t testNumber,void* arg_ptr2)
746 {
747         PRINT_INFO("%s is start", __func__);
748
749         int user_context = 0;
750         net_nfc_error_e result;
751         test_case_result = NET_NFC_TEST_FAIL;
752
753         result = net_nfc_initialize();
754
755         net_nfc_state_activate(1);
756
757         CHECK_RESULT(result);
758         result = net_nfc_set_response_callback (net_nfc_test_reader_cb, (void *)1);
759         CHECK_RESULT(result);
760
761         PRINT_INSTRUCT("Please close a tag to device!!");
762
763         //pthread_cond_wait (&pcond,&plock );
764
765 /*
766         PRINT_INFO("operation is end");
767
768         result = net_nfc_unset_response_callback ();
769
770         CHECK_RESULT(result);
771         result = net_nfc_deinitialize ();
772         CHECK_RESULT(result);
773
774 */
775
776         return test_case_result;
777 }
778
779 int nfcTestExchanger(uint8_t testNumber,void* arg_ptr2)
780 {
781         PRINT_INFO("%s is start", __func__);
782         int user_context = 0;
783         net_nfc_error_e result;
784         test_case_result = NET_NFC_TEST_FAIL;
785
786         if((result = net_nfc_set_exchanger_cb(net_nfc_exchanger_callback, NULL)) != NET_NFC_OK){
787                 test_case_result = NET_NFC_TEST_FAIL;
788                 PRINT_INFO("failed to set exchanger_cb = [%d]", result);
789                 return test_case_result;
790         }
791         else{
792                 PRINT_INFO("setting exchange callback is ok = [%d]", result);
793         }
794
795 /*
796         if((result = net_nfc_initialize()) != NET_NFC_NOT_ALLOWED_OPERATION){
797                 test_case_result = NET_NFC_TEST_FAIL;
798                 PRINT_INFO("NET_NFC_NOT_ALLOWED_OPERATION 1 is not returned = [%d]", result);
799                 return test_case_result;
800         }else{
801                 PRINT_INFO("net_nfc_init is failed = [%d]", result);
802         }
803
804         if((result = net_nfc_unset_exchanger_cb()) != NET_NFC_OK){
805                 test_case_result = NET_NFC_TEST_FAIL;
806                 PRINT_INFO("unset exchanger cb is failed = [%d]", result);
807                 return test_case_result;
808         }else{
809                 PRINT_INFO("unset ex cb is ok = [%d]", result);
810         }
811
812         if((result = net_nfc_initialize()) != NET_NFC_OK){
813                 test_case_result = NET_NFC_TEST_FAIL;
814                 PRINT_INFO("net_nfc_init is failed = [%d]", result);
815                 return test_case_result;
816         }else{
817                 PRINT_INFO("net_nfc_init is ok = [%d]", result);
818         }
819
820         if((result = net_nfc_set_exchanger_cb(net_nfc_exchanger_callback, NULL)) != NET_NFC_NOT_ALLOWED_OPERATION){
821                 test_case_result = NET_NFC_TEST_FAIL;
822                 PRINT_INFO("NET_NFC_NOT_ALLOWED_OPERATION 2 is not returned = [%d]", result);
823                 return test_case_result;
824         }else{
825                 PRINT_INFO("set ex cb is failed = [%d]", result);
826         }
827
828         if((result = net_nfc_deinitialize()) != NET_NFC_OK){
829                 test_case_result = NET_NFC_TEST_FAIL;
830                 PRINT_INFO("nfc deinit is failed = [%d]", result);
831                 return test_case_result;
832         }else{
833                 PRINT_INFO("net_nfc_deinit is ok = [%d]", result);
834         }
835
836         if((result = net_nfc_set_exchanger_cb(net_nfc_exchanger_callback, NULL)) != NET_NFC_OK){
837                 PRINT_INFO("set exchanger cb is failed = [%d]", result);
838                 test_case_result = NET_NFC_TEST_FAIL;
839                 return test_case_result;
840         }else{
841                 PRINT_INFO("settting ex cb is ok = [%d]", result);
842         }
843 */
844
845         //pthread_cond_wait (&pcond,&plock );
846
847         //PRINT_INFO("operation is end");
848         //result = net_nfc_unset_exchanger_cb();
849         //CHECK_RESULT(result);
850
851         test_case_result = NET_NFC_TEST_OK;
852
853         return test_case_result;
854 }
855
856 /*=================================================================================*/
857
858 static void net_nfc_test_write_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
859 {
860         int user_context;
861
862         switch(message)
863         {
864                 case NET_NFC_MESSAGE_TAG_DISCOVERED:{
865                         net_nfc_target_type_e type;
866                         net_nfc_target_handle_h handle;
867                         bool is_ndef;
868                         net_nfc_target_info_h target_info = (net_nfc_target_info_h)data;
869
870                         net_nfc_get_tag_type (target_info, &type);
871                         net_nfc_get_tag_handle (target_info, &handle);
872                         net_nfc_get_tag_ndef_support (target_info, &is_ndef);
873                         PRINT_INFO("target type: %d\n", type);
874                         PRINT_INFO("target id: %X\n", handle);
875                         PRINT_INFO("Is NDEF supoort: %d\n", is_ndef);
876
877                         tag_handle = handle;
878
879                         if (is_ndef){
880
881                                 net_nfc_error_e error = NET_NFC_OK;
882                                 ndef_message_h msg = NULL;
883                                 ndef_record_h record = NULL;
884
885                                 if( (error = net_nfc_create_uri_type_record(&record, "http://www.naver.com", NET_NFC_SCHEMA_FULL_URI)) == NET_NFC_OK)
886                                 {
887                                         if( (error = net_nfc_create_ndef_message(&msg)) == NET_NFC_OK)
888                                         {
889                                                 if( (error = net_nfc_append_record_to_ndef_message(msg, record)) == NET_NFC_OK)
890                                                 {
891                                                         ////net_nfc_ndef_print_message(msg);
892                                                         net_nfc_write_ndef(tag_handle, msg, &user_context);
893                                                         net_nfc_free_ndef_message(msg);
894                                                 }
895                                                 else
896                                                 {
897                                                         PRINT_INFO("failed to append ndef message = [%d]", error);
898                                                         net_nfc_free_record(record);
899                                                         net_nfc_free_ndef_message(msg);
900
901                                                         pthread_mutex_lock (&plock);
902                                                         pthread_cond_signal (&pcond);
903                                                         pthread_mutex_unlock (&plock);
904                                                 }
905                                         }
906                                         else
907                                         {
908                                                 PRINT_INFO("failed to create ndef msg");
909                                                 net_nfc_free_record(record);
910
911                                                 pthread_mutex_lock (&plock);
912                                                 pthread_cond_signal (&pcond);
913                                                 pthread_mutex_unlock (&plock);
914                                         }
915                                 }
916                                 else
917                                 {
918                                         PRINT_INFO("failed to create text type record");
919
920                                         pthread_mutex_lock (&plock);
921                                         pthread_cond_signal (&pcond);
922                                         pthread_mutex_unlock (&plock);
923                                 }
924                         }
925                         else{
926                                 PRINT_INSTRUCT("Please use NDEF formated tag!!");
927                                 break;
928                         }
929
930                         break;
931                 }
932                 case NET_NFC_MESSAGE_TAG_DETACHED:
933                 {
934                         if (write_count++ >= 20) {
935                                 test_case_result = NET_NFC_TEST_OK;
936                                 pthread_mutex_lock (&plock);
937                                 pthread_cond_signal (&pcond);
938                                 pthread_mutex_unlock (&plock);
939                         }
940                         else {
941                                 PRINT_INSTRUCT("Please close a tag to device again!!");
942                         }
943                 }
944                 break;
945
946                 case NET_NFC_MESSAGE_WRITE_NDEF:
947                 {
948                         /*if (result == NET_NFC_OK) {
949                                 test_case_result = NET_NFC_TEST_OK;
950                         }
951                         else {
952                                 PRINT_INFO("received error: %d\n", result);
953                                 test_case_result = NET_NFC_TEST_FAIL;
954                         }
955                         */
956
957                 /*      pthread_mutex_lock (&plock);
958                         pthread_cond_signal (&pcond);
959                         pthread_mutex_unlock (&plock);
960                 */
961
962                         if(write_count++ == 20)
963                         {
964
965                                 PRINT_INSTRUCT("completed, please remove tag");
966                         }
967
968                         {
969
970                                 net_nfc_error_e error = NET_NFC_OK;
971                                 ndef_message_h msg = NULL;
972                                 ndef_record_h record = NULL;
973
974                                 if( (error = net_nfc_create_text_type_record(&record, "This is real NFC", "en-US", NET_NFC_ENCODE_UTF_8)) == NET_NFC_OK)
975                                 {
976                                         if( (error = net_nfc_create_ndef_message(&msg)) == NET_NFC_OK)
977                                         {
978                                                 if( (error = net_nfc_append_record_to_ndef_message(msg, record)) == NET_NFC_OK)
979                                                 {
980                                                         ////net_nfc_ndef_print_message(msg);
981                                                         net_nfc_write_ndef(tag_handle, msg, &user_context);
982                                                         net_nfc_free_ndef_message(msg);
983                                                 }
984                                                 else
985                                                 {
986                                                         PRINT_INFO("failed to append ndef message = [%d]", error);
987                                                         net_nfc_free_record(record);
988                                                         net_nfc_free_ndef_message(msg);
989                                                         test_case_result = NET_NFC_TEST_FAIL;
990
991                                                         pthread_mutex_lock (&plock);
992                                                         pthread_cond_signal (&pcond);
993                                                         pthread_mutex_unlock (&plock);
994                                                 }
995                                         }
996                                         else
997                                         {
998                                                 PRINT_INFO("failed to create ndef msg");
999                                                 net_nfc_free_record(record);
1000                                                 test_case_result = NET_NFC_TEST_FAIL;
1001
1002                                                 pthread_mutex_lock (&plock);
1003                                                 pthread_cond_signal (&pcond);
1004                                                 pthread_mutex_unlock (&plock);
1005                                         }
1006                                 }
1007                                 else
1008                                 {
1009                                         PRINT_INFO("failed to create text type record");
1010                                         test_case_result = NET_NFC_TEST_FAIL;
1011
1012                                         pthread_mutex_lock (&plock);
1013                                         pthread_cond_signal (&pcond);
1014                                         pthread_mutex_unlock (&plock);
1015
1016                                 }
1017                         }
1018
1019                         break;
1020                 }
1021                 default:
1022                         break;
1023         }
1024 }
1025
1026
1027 int nfcTestWriteMode(uint8_t testNumber,void* arg_ptr2)
1028 {
1029         int user_context = 0;
1030         net_nfc_error_e result;
1031         test_case_result = NET_NFC_TEST_FAIL;
1032
1033         result = net_nfc_initialize();
1034         CHECK_RESULT(result);
1035
1036         net_nfc_state_activate(1);
1037
1038         result = net_nfc_set_response_callback (net_nfc_test_write_cb, NULL);
1039         CHECK_RESULT(result);
1040
1041         write_count = 0;
1042         PRINT_INSTRUCT("TEST: remove tag while write");
1043         PRINT_INSTRUCT("Please close a tag to device!!");
1044
1045         //pthread_cond_wait (&pcond,&plock );
1046
1047 /*
1048         result = net_nfc_unset_response_callback ();
1049         CHECK_RESULT(result);
1050         result = net_nfc_deinitialize ();
1051         CHECK_RESULT(result);
1052 */
1053         return test_case_result;
1054
1055 }
1056
1057
1058 int nfcTestReadWriteMode(uint8_t testNumber,void* arg_ptr2)
1059 {
1060         int user_context = 0;
1061         net_nfc_error_e result;
1062         test_case_result = NET_NFC_TEST_FAIL;
1063
1064         read_count = 0;
1065         write_count = 0;
1066
1067         result = net_nfc_initialize();
1068         CHECK_RESULT(result);
1069
1070         net_nfc_state_activate(1);
1071
1072         result = net_nfc_set_response_callback (net_nfc_test_read_write_cb, NULL);
1073         CHECK_RESULT(result);
1074
1075         PRINT_INSTRUCT("Testing (50 each) read and write test");
1076         PRINT_INSTRUCT("Please close a tag to device!!");
1077
1078         //pthread_cond_wait (&pcond,&plock );
1079 /*
1080         result = net_nfc_unset_response_callback ();
1081         CHECK_RESULT(result);
1082         result = net_nfc_deinitialize ();
1083         CHECK_RESULT(result);
1084 */
1085
1086
1087         return test_case_result;
1088
1089 }
1090
1091 /*=================================================================================*/
1092
1093 static void net_nfc_test_transceive_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
1094 {
1095         int user_context;
1096
1097         printf ("callback is called with message %d\n", message);
1098         switch(message)
1099         {
1100                 case NET_NFC_MESSAGE_TAG_DISCOVERED:{
1101                         net_nfc_target_type_e type;
1102                         net_nfc_target_handle_h id;
1103                         bool is_ndef;
1104                         net_nfc_target_info_h target_info = (net_nfc_target_info_h)data;
1105                         net_nfc_error_e result = NET_NFC_OK;
1106
1107                         net_nfc_get_tag_type (target_info, &type);
1108                         net_nfc_get_tag_handle (target_info, &id);
1109                         net_nfc_get_tag_ndef_support (target_info, &is_ndef);
1110                         PRINT_INFO("target type: %d\n", type);
1111                         PRINT_INFO("target id: %X\n", id);
1112                         PRINT_INFO("Is NDEF supoort: %d\n", is_ndef);
1113
1114                         tag_handle = id;
1115
1116                         if(type == NET_NFC_MIFARE_ULTRA_PICC){
1117
1118                                 if((result = net_nfc_mifare_read(tag_handle, 0, NULL)) != NET_NFC_OK){
1119                                         PRINT_INFO("failed to read = [%d]", result);
1120                                 }
1121                         }
1122
1123                         else if(type >= NET_NFC_MIFARE_MINI_PICC && type <= NET_NFC_MIFARE_4K_PICC){
1124
1125                                 data_h mad_key = NULL;
1126                                 data_h net_nfc_forum_key = NULL;
1127                                 data_h default_key = NULL;
1128
1129                                 net_nfc_mifare_create_application_directory_key(&mad_key);
1130                                 net_nfc_mifare_create_net_nfc_forum_key(&net_nfc_forum_key);
1131                                 net_nfc_mifare_create_default_key(&default_key);
1132
1133                                 int sector = 1;
1134
1135                                 data_h key = NULL;
1136                                 test_count = 4;
1137
1138                                 if(sector == 0){
1139                                         key = mad_key;
1140                                 }
1141                                 else{
1142                                         key = default_key;
1143 //                                      key = net_nfc_forum_key;
1144                                 }
1145
1146 //                              if(net_nfc_mifare_authenticate_with_keyB(id, sector, key, NULL) != NET_NFC_OK){
1147                                 if(net_nfc_mifare_authenticate_with_keyA(id, sector, default_key, NULL) != NET_NFC_OK){
1148                                         PRINT_INFO("failed to authenticate sector");
1149
1150                                 }
1151                                 else{
1152
1153                                         int nBlocks = 0;
1154
1155                                         if(type == NET_NFC_MIFARE_4K_PICC && sector > 31){
1156
1157                                                 nBlocks = 16;
1158                                         }
1159                                         else{
1160                                                 nBlocks = 4;
1161                                         }
1162
1163
1164 /*
1165                                         int j = 0;
1166
1167                                         for(; j < nBlocks; j++){
1168                                                 if((result = net_nfc_mifare_read(tag_handle, sector * 4 + j, NULL)) != NET_NFC_OK){
1169                                                         PRINT_INFO("failed to read = [%d]", result);
1170                                                 }else{
1171                                                         PRINT_INFO("block [%d] is read", sector * 4 + j);
1172                                                 }
1173                                         }
1174 */
1175 /*
1176                                         data_h write_block = NULL;
1177                                         uint8_t buffer[16] = {0x00,};
1178                                         uint8_t* temp = buffer;
1179
1180                                         int value = 1000;
1181                                         int value_comp = 1 + ~value;
1182                                         uint8_t addr = 5;
1183                                         uint8_t addr_comp = 1 + ~addr;
1184
1185
1186                                         // read block 5 and write block 5 and read again
1187                                         if((result = net_nfc_mifare_read(tag_handle, addr, NULL)) != NET_NFC_OK){
1188                                                 PRINT_INFO("failed to read = [%d]", result);
1189                                         }else{
1190                                                 PRINT_INFO("block 5 is read");
1191                                         }
1192
1193                                         // read block 5 and write block 5 and read again
1194
1195                                         memcpy(temp, &value, sizeof(int));
1196                                         temp = temp + sizeof(int);
1197
1198                                         memcpy(temp, &value_comp, sizeof(int));
1199                                         temp = temp + sizeof(int);
1200
1201                                         memcpy(temp, &value, sizeof(int));
1202                                         temp = temp + sizeof(int);
1203
1204                                         *temp = addr;
1205                                         temp = temp + 1;
1206
1207                                         *temp = addr_comp;
1208                                         temp = temp + 1;
1209
1210                                         *temp = addr;
1211                                         temp = temp + 1;
1212
1213                                         *temp = addr_comp;
1214
1215                                         net_nfc_create_data(&write_block, buffer, 16);
1216
1217                                         if((result = net_nfc_mifare_write_block(tag_handle, addr, write_block,NULL)) != NET_NFC_OK){
1218                                                 PRINT_INFO("failed to write = [%d]", result);
1219                                         }else{
1220                                                 PRINT_INFO("block 5 is written");
1221                                         }
1222
1223                                         if((result = net_nfc_mifare_read(tag_handle, addr, NULL)) != NET_NFC_OK){
1224                                                 PRINT_INFO("failed to read = [%d]", result);
1225                                         }else{
1226                                                 PRINT_INFO("block 5 is read");
1227                                         }
1228 */
1229
1230                                         // read block 6 and decrease 6 and read again
1231
1232 /*
1233                                         if((result = net_nfc_mifare_read(tag_handle, 7, NULL)) != NET_NFC_OK){
1234                                                 PRINT_INFO("failed to read = [%d]", result);
1235                                         }else{
1236                                                 PRINT_INFO("read sector trailor of sector 1. it will be a block 7 th");
1237                                         }
1238 */
1239
1240 /*
1241                                         data_h write_block = NULL;
1242                                         uint8_t buffer[16] = {0x00, 0x00, 0x03, 0xE8, 0xFF, 0xFF, 0xFC, 0x18, 0x00, 0x00, 0x03, 0xE8, 0x05, 0xFB, 0x05, 0xFB};
1243
1244                                         net_nfc_create_data(&write_block, buffer, 16);
1245
1246                                         if((result = net_nfc_mifare_write_block(tag_handle, 5, write_block, NULL)) != NET_NFC_OK){
1247                                                 PRINT_INFO("failed to write = [%d]", result);
1248                                         }else{
1249                                                 PRINT_INFO("block 5 is written");
1250                                         }
1251
1252
1253                                         if((result = net_nfc_mifare_read(tag_handle, 5, NULL)) != NET_NFC_OK){
1254                                                 PRINT_INFO("failed to read = [%d]", result);
1255                                         }else{
1256                                                 PRINT_INFO("block 5 is read");
1257                                         }
1258 */
1259
1260
1261                                         if((result = net_nfc_mifare_read(tag_handle, 5, NULL)) != NET_NFC_OK){
1262                                                 PRINT_INFO("failed to read = [%d]", result);
1263                                         }
1264
1265
1266                                         int decrease_amount = 10; // 10 won
1267
1268                                         if((result = net_nfc_mifare_decrement(tag_handle, 5, decrease_amount, NULL)) != NET_NFC_OK){
1269                                                 PRINT_INFO("failed to decrease = [%d]", result);
1270                                         }else{
1271                                                 PRINT_INFO("block 5 is decreased");
1272                                         }
1273
1274                                         if((result = net_nfc_mifare_transfer(tag_handle, 5,  NULL)) != NET_NFC_OK){
1275                                                 PRINT_INFO("failed to transfer = [%d]", result);
1276                                         }else{
1277                                                 PRINT_INFO("internal register is transfered to block 5");
1278                                         }
1279
1280 /*
1281                                         if((result = net_nfc_mifare_read(tag_handle, 5, NULL)) != NET_NFC_OK){
1282                                                 PRINT_INFO("failed to read = [%d]", result);
1283                                         }
1284 */
1285
1286                                         net_nfc_free_data(mad_key);
1287                                         net_nfc_free_data(default_key);
1288                                         net_nfc_free_data(net_nfc_forum_key);
1289
1290                                 }
1291
1292
1293                         }
1294                         else if(type == NET_NFC_JEWEL_PICC){
1295
1296                                 if(net_nfc_jewel_read_id(id, NULL) != NET_NFC_OK){
1297                                         PRINT_INFO("failed to read ID");
1298                                 }
1299
1300                                 test_count = 0;
1301
1302                         }else if(type == NET_NFC_FELICA_PICC){
1303
1304                                 test_count = 4;
1305                                 net_nfc_error_e result = NET_NFC_OK;
1306
1307 /*
1308                                 if((result = net_nfc_felica_poll(id, NET_NFC_FELICA_POLL_COMM_SPEED_REQUEST, 0x0, NULL)) != NET_NFC_OK){
1309                                         PRINT_INFO("can not execute cmd felica poll");
1310                                 }
1311                                 else{
1312                                         PRINT_INFO("send poll req cmd is success");
1313                                 }
1314
1315                                 if((result = net_nfc_felica_request_system_code(id, NULL)) != NET_NFC_OK){
1316                                         PRINT_INFO("can not execute cmd felica request system code");
1317                                 }
1318                                 else{
1319                                         PRINT_INFO("send request system req cmd is success");
1320                                 }
1321
1322                                 if((result = net_nfc_felica_request_response(id, NULL)) != NET_NFC_OK){
1323                                         PRINT_INFO("can not execute cmd felica request response");
1324                                 }
1325                                 else{
1326                                         PRINT_INFO("send request response cmd is success");
1327                                 }
1328
1329                                 uint16_t service_code = 0xffff;
1330                                 if((result = net_nfc_felica_request_service(id, 1, &service_code, 1, NULL)) != NET_NFC_OK){
1331                                         PRINT_INFO("can not execute cmd felica request response");
1332                                 }
1333                                 else{
1334                                         PRINT_INFO("send request response cmd is success");
1335                                 }
1336 */
1337
1338                                 uint16_t service_code = 0xffff;
1339                                 uint8_t blocks = 0x2;
1340
1341                                 if((result = net_nfc_felica_read_without_encryption(id, 1, &service_code, 1, &blocks, NULL)) != NET_NFC_OK){
1342                                         PRINT_INFO("can not execute cmd felica request response");
1343                                 }
1344                                 else{
1345                                         PRINT_INFO("send request response cmd is success");
1346                                 }
1347                         }
1348
1349                         break;
1350                 }
1351                 case NET_NFC_MESSAGE_TRANSCEIVE:{
1352                         if (result == NET_NFC_OK) {
1353
1354                                 if(test_count == 0){
1355                                         int idx;
1356                                         data_h r_data = (data_h) data;
1357
1358                                         uint8_t * r_buffer = net_nfc_get_data_buffer (r_data);
1359                                         uint32_t r_buffer_length = net_nfc_get_data_length (r_data);
1360
1361                                         PRINT_INFO ("read uid is ok. format is = > [HeadRom0][HeadRom1][UID0][UID1][UID2][UID3]");
1362                                         PRINT_INFO ("Received data [size:%d] \n", net_nfc_get_data_length (r_data));
1363
1364                                         for (idx = 0; idx < r_buffer_length; idx++){
1365                                                 printf (" %02X", r_buffer[idx]);
1366                                         }printf ("\n");
1367
1368                                         PRINT_INFO("READ ALL DATA");
1369                                         if((result = net_nfc_jewel_read_all(tag_handle, NULL)) != NET_NFC_OK){
1370                                                 PRINT_INFO("failed to read all jewel tag = [%d]", result);
1371                                         }
1372
1373                                         test_count = 1;
1374                                 }
1375                                 else if(test_count == 1){
1376                                         int idx;
1377                                         data_h r_data = (data_h) data;
1378
1379                                         uint8_t * r_buffer = net_nfc_get_data_buffer (r_data);
1380                                         uint32_t r_buffer_length = net_nfc_get_data_length (r_data);
1381
1382                                         PRINT_INFO ("Received data [size:%d] \n", net_nfc_get_data_length (r_data));
1383
1384                                         for (idx = 0; idx < r_buffer_length; idx++){
1385                                                 printf (" %02X", r_buffer[idx]);
1386                                         }printf ("\n");
1387
1388                                         test_case_result = NET_NFC_TEST_OK;
1389
1390                                         // read UID0
1391
1392                                         PRINT_INFO("READ one byte. addr is the first byte of block 0");
1393                                         if((result = net_nfc_jewel_read_byte(tag_handle, 0, 0, NULL)) != NET_NFC_OK){
1394                                                 PRINT_INFO("failed to read all jewel tag = [%d]", result);
1395                                         }
1396
1397                                         test_count = 2;
1398
1399                                 }
1400                                 else if(test_count == 2){
1401                                         int idx;
1402                                         data_h r_data = (data_h) data;
1403
1404                                         uint8_t * r_buffer = net_nfc_get_data_buffer (r_data);
1405                                         uint32_t r_buffer_length = net_nfc_get_data_length (r_data);
1406
1407                                         PRINT_INFO ("Received data [size:%d] \n", net_nfc_get_data_length (r_data));
1408
1409                                         for (idx = 0; idx < r_buffer_length; idx++){
1410                                                 printf (" %02X", r_buffer[idx]);
1411                                         }printf ("\n");
1412
1413                                         test_case_result = NET_NFC_TEST_OK;
1414
1415                                         // read UID0
1416                                         PRINT_INFO("erase and write data 0xff . addr is the first byte of block 1");
1417                                         if((result = net_nfc_jewel_write_with_erase(tag_handle,  1, 0, 0xff, NULL)) != NET_NFC_OK){
1418                                                 PRINT_INFO("failed to read all jewel tag = [%d]", result);
1419                                         }
1420
1421                                         test_count = 3;
1422                                 }
1423                                 else if(test_count == 3){
1424                                         int idx;
1425                                         data_h r_data = (data_h) data;
1426
1427                                         uint8_t * r_buffer = net_nfc_get_data_buffer (r_data);
1428                                         uint32_t r_buffer_length = net_nfc_get_data_length (r_data);
1429
1430                                         PRINT_INFO ("Received data [size:%d] \n", net_nfc_get_data_length (r_data));
1431
1432                                         for (idx = 0; idx < r_buffer_length; idx++){
1433                                                 printf (" %02X", r_buffer[idx]);
1434                                         }printf ("\n");
1435
1436                                         test_case_result = NET_NFC_TEST_OK;
1437
1438                                         // read UID0
1439                                         PRINT_INFO("read one byte . addr is the first byte of block 1");
1440                                         if((result = net_nfc_jewel_read_byte(tag_handle, 1, 0, NULL)) != NET_NFC_OK){
1441                                                 PRINT_INFO("failed to read all jewel tag = [%d]", result);
1442                                         }
1443
1444                                         test_count = 4;
1445                                 }
1446                                 else if(test_count == 4){
1447
1448                                         int idx;
1449                                         data_h r_data = (data_h) data;
1450
1451                                         uint8_t * r_buffer = net_nfc_get_data_buffer (r_data);
1452                                         uint32_t r_buffer_length = net_nfc_get_data_length (r_data);
1453
1454                                         PRINT_INFO ("Received data [size:%d] \n", net_nfc_get_data_length (r_data));
1455
1456                                         for (idx = 0; idx < r_buffer_length; idx++){
1457                                                 printf (" %02X", r_buffer[idx]);
1458                                         }printf ("\n");
1459                                 }
1460                                 else if(test_count == 5){
1461                                         PRINT_INFO("auth key A is success = [%d]", result);
1462                                         test_case_result = NET_NFC_TEST_OK;
1463                                         net_nfc_error_e result = NET_NFC_OK;
1464                                         /*
1465                                         if((result = net_nfc_mifare_read(tag_handle, 0, NULL)) != NET_NFC_OK){
1466
1467                                                 PRINT_INFO("failed to read = [%d]", result);
1468                                         }else{
1469                                                 PRINT_INFO("calling read is ok");
1470                                         }
1471
1472                                         test_count = 4;
1473                                         */
1474                                 }
1475                         }
1476                         else {
1477
1478                                 PRINT_INFO("trancecive is failed with  %d\n", result);
1479                                 test_case_result = NET_NFC_TEST_FAIL;
1480
1481                                 //pthread_mutex_lock (&plock);
1482                                 //pthread_cond_signal (&pcond);
1483                                 //pthread_mutex_unlock (&plock);
1484                         }
1485                 }
1486
1487                 default:
1488                         break;
1489         }
1490 }
1491
1492
1493 #define NUM_OF_THREAD 10
1494 #define REQUEST_PER_THREAD 5
1495
1496 static number_of_read_completed = 0;
1497
1498 static void* net_nfc_read_ndef_test(void* handle)
1499 {
1500         net_nfc_target_handle_h target_handle = (net_nfc_target_handle_h)handle;
1501         int count = 0;
1502
1503         for (count = 0; count < REQUEST_PER_THREAD ; count ++)
1504         {
1505                 if(net_nfc_read_tag(target_handle, NULL) == NET_NFC_OK)
1506                 {
1507                         PRINT_INFO("send request is success");
1508                 }
1509                 else
1510                 {
1511                         PRINT_INFO("send request is failed");
1512                 }
1513         }
1514
1515         return (void *)NULL;
1516 }
1517 static void net_nfc_test_multiple_request_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
1518 {
1519         int user_context;
1520
1521         printf ("callback is called with message %d\n", message);
1522
1523         switch(message)
1524         {
1525                 case NET_NFC_MESSAGE_TAG_DISCOVERED:
1526                 {
1527                         net_nfc_target_type_e type;
1528                         net_nfc_target_handle_h handle;
1529                         bool is_ndef;
1530                         net_nfc_target_info_h target_info = (net_nfc_target_info_h)data;
1531
1532                         net_nfc_get_tag_type (target_info, &type);
1533                         net_nfc_get_tag_handle (target_info, &handle);
1534                         net_nfc_get_tag_ndef_support (target_info, &is_ndef);
1535                         PRINT_INFO("target type: %d\n", type);
1536                         PRINT_INFO("target handle: %X\n", handle);
1537                         PRINT_INFO("Is NDEF supoort: %d\n", is_ndef);
1538
1539                         test_case_result = NET_NFC_TEST_OK;
1540                         number_of_read_completed = 0;
1541
1542                         pthread_t read_thread[NUM_OF_THREAD];
1543                         pthread_attr_t attr;
1544                         pthread_attr_init(&attr);
1545                         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1546
1547                         int i =0;
1548                         for(; i < NUM_OF_THREAD; i++)
1549                         {
1550                                 pthread_create(&read_thread[i], &attr, net_nfc_read_ndef_test, handle);
1551                         }
1552
1553                 }
1554                 break;
1555
1556                 case NET_NFC_MESSAGE_READ_NDEF:
1557                 {
1558                         PRINT_INFO("NET_NFC_MESSAGE_READ_NDEF result = [%d]\n", result);
1559
1560                         if(data != NULL)
1561                         {
1562                                 PRINT_INFO("read ndef msg");
1563                                 number_of_read_completed ++;
1564
1565                                 ndef_message_h ndef = (ndef_message_h)(data);
1566
1567                                 ////net_nfc_ndef_print_message(ndef);
1568
1569                                 if (number_of_read_completed == NUM_OF_THREAD * REQUEST_PER_THREAD)
1570                                 {
1571                                         test_case_result = NET_NFC_TEST_OK;
1572                                         PRINT_INSTRUCT("Test is completed. please remove the tag !!");
1573                                 }
1574                         }
1575                 }
1576                 break;
1577
1578                 case NET_NFC_MESSAGE_TAG_DETACHED:
1579                 {
1580                         PRINT_INFO("NET_NFC_MESSAGE_TAG_DETACHED \n");
1581
1582                         pthread_mutex_lock (&plock);
1583                         pthread_cond_signal (&pcond);
1584                         pthread_mutex_unlock (&plock);
1585                 }
1586                 break;
1587
1588                 case NET_NFC_MESSAGE_NOTIFY:
1589                 {
1590                         PRINT_INFO("NET_NFC_MESSAGE_NOTIFY \n");
1591                 }
1592                 break;
1593
1594                 default:
1595                         break;
1596         }
1597 }
1598
1599 #define NUM_OF_DETECT_TRY 10
1600
1601 static void net_nfc_test_detected_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
1602 {
1603         int user_context;
1604
1605         printf ("callback is called with message %d\n", message);
1606
1607         switch(message)
1608         {
1609                 case NET_NFC_MESSAGE_TAG_DISCOVERED:
1610                 {
1611                         net_nfc_target_type_e type;
1612                         net_nfc_target_handle_h id;
1613                         bool is_ndef;
1614                         net_nfc_target_info_h target_info = (net_nfc_target_info_h)data;
1615
1616                         net_nfc_get_tag_type (target_info, &type);
1617                         net_nfc_get_tag_handle (target_info, &id);
1618                         net_nfc_get_tag_ndef_support (target_info, &is_ndef);
1619
1620                         PRINT_INFO("target type: %d\n", type);
1621                         PRINT_INFO("target handle: %X\n", id);
1622                         PRINT_INFO("Is NDEF supoort: %d\n", is_ndef);
1623
1624                         char** keys = NULL;
1625                         int nok = 0; // number of keys
1626                         net_nfc_error_e error = NET_NFC_OK;
1627
1628                         if((error = net_nfc_get_tag_info_keys(target_info, &keys, &nok)) == NET_NFC_OK)
1629                         {
1630                                 int i = 0;
1631                                 for(; i < nok; i++)
1632                                 {
1633                                         PRINT_INFO("key [%s]", keys[i]);
1634
1635                                         data_h value = NULL;
1636                                         if((error = net_nfc_get_tag_info_value(target_info, keys[i], &value)) == NET_NFC_OK)
1637                                         {
1638                                                 int index = 0;
1639                                                 uint32_t data_length = net_nfc_get_data_length(value);
1640                                                 uint8_t* data_buffer = net_nfc_get_data_buffer(value);
1641
1642                                                 PRINT_INFO("\n key >> ", keys[i]);
1643                                                 if(data_length > 1)
1644                                                 {
1645                                                         for(; index < data_length; index++)
1646                                                         {
1647                                                                 printf(" [0x%x] ", data_buffer[index]);
1648                                                         }
1649                                                 }
1650                                                 else
1651                                                 {
1652                                                         printf(" [0x%x] ", *data_buffer);
1653                                                 }
1654
1655
1656                                                 PRINT_INFO("<< \n");
1657                                         }
1658                                         else
1659                                         {
1660                                                 PRINT_INFO("get value is failed = [0x%x]", error);
1661                                         }
1662                                 }
1663                         }
1664                         else
1665                         {
1666                                 PRINT_INFO("failed to get keys = [%d]", error);
1667                         }
1668
1669                         free(keys);
1670
1671
1672                         detect_count++;
1673
1674                         PRINT_INFO("TAG is detected = [%d]", detect_count);
1675                         PRINT_INSTRUCT("please remove the tag !! Test left [%d] times", NUM_OF_DETECT_TRY - detect_count);
1676
1677                 }
1678                 break;
1679
1680                 case NET_NFC_MESSAGE_TAG_DETACHED:
1681                 {
1682                         PRINT_INSTRUCT("please close the tag again!!");
1683
1684                         //pthread_mutex_lock (&plock);
1685                         //pthread_cond_signal (&pcond);
1686                         //pthread_mutex_unlock (&plock);
1687
1688                         if(detect_count >= NUM_OF_DETECT_TRY)
1689                         {
1690                                 test_case_result = NET_NFC_TEST_OK;
1691                                 pthread_mutex_lock (&plock);
1692                                 pthread_cond_signal (&pcond);
1693                                 pthread_mutex_unlock (&plock);
1694                         }
1695                 }
1696                 break;
1697
1698                 case NET_NFC_MESSAGE_NOTIFY:
1699                 {
1700                         PRINT_INFO("NET_NFC_MESSAGE_NOTIFY \n");
1701                 }
1702                 break;
1703
1704                 default:
1705                         break;
1706         }
1707 }
1708
1709
1710 static void* net_nfc_read_stress_ndef_test(void* handle)
1711 {
1712         net_nfc_target_handle_h target_handle = (net_nfc_target_handle_h)handle;
1713         int count = 0;
1714
1715         for (count = 0; count < 200 ; count ++)
1716         {
1717                 if(net_nfc_read_tag(target_handle, NULL) == NET_NFC_OK)
1718                 {
1719                 }
1720                 else
1721                 {
1722                         PRINT_INFO("send request is failed");
1723                 }
1724         }
1725
1726         PRINT_INFO("send request is completed");
1727
1728         return (void *)NULL;
1729 }
1730
1731 static void net_nfc_test_read_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
1732 {
1733         int user_context;
1734
1735         switch(message)
1736         {
1737                 case NET_NFC_MESSAGE_TAG_DISCOVERED:
1738                 {
1739                         net_nfc_target_type_e type;
1740                         net_nfc_target_handle_h handle;
1741                         bool is_ndef;
1742                         net_nfc_target_info_h target_info = (net_nfc_target_info_h)data;
1743
1744                         net_nfc_get_tag_type (target_info, &type);
1745                         net_nfc_get_tag_handle (target_info, &handle);
1746                         net_nfc_get_tag_ndef_support (target_info, &is_ndef);
1747                         PRINT_INFO("target type: %d\n", type);
1748                         PRINT_INFO("target handle: %X\n", handle);
1749                         PRINT_INFO("Is NDEF supoort: %d\n", is_ndef);
1750
1751                         if(is_ndef)
1752                         {
1753                                 pthread_t read_thread;
1754                                 pthread_attr_t attr;
1755                                 pthread_attr_init(&attr);
1756                                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1757
1758                                 pthread_create(&read_thread, &attr, net_nfc_read_stress_ndef_test, handle);
1759                         }
1760                         else
1761                         {
1762                                 PRINT_INFO("non ndef tag.");
1763
1764                                 test_case_result = NET_NFC_TEST_FAIL;
1765
1766                                 pthread_mutex_lock (&plock);
1767                                 pthread_cond_signal (&pcond);
1768                                 pthread_mutex_unlock (&plock);
1769                         }
1770
1771                 }
1772                 break;
1773
1774                 case NET_NFC_MESSAGE_READ_NDEF:
1775                 {
1776                         if(data != NULL){
1777
1778                                 ndef_message_h ndef = (ndef_message_h)(data);
1779
1780                                 ////net_nfc_ndef_print_message(ndef);
1781
1782                                 test_case_result = NET_NFC_TEST_OK;
1783
1784                                 read_count++;
1785                         }
1786
1787                         if(read_count == 20)
1788                         {
1789                                 PRINT_INSTRUCT("please remove the tag !!");
1790                         }
1791
1792                         break;
1793                 }
1794                 break;
1795
1796                 case NET_NFC_MESSAGE_TAG_DETACHED:
1797                 {
1798                         PRINT_INFO("NET_NFC_MESSAGE_TAG_DETACHED \n");
1799
1800                         if (read_count >= 20 && read_count < 200){
1801                                 test_case_result = NET_NFC_TEST_OK;
1802                                         pthread_mutex_lock (&plock);
1803                                         pthread_cond_signal (&pcond);
1804                                         pthread_mutex_unlock (&plock);
1805                         }
1806                         else {
1807                                 PRINT_INSTRUCT("please close the tag again !!");
1808                         }
1809
1810
1811                 }
1812                 break;
1813
1814                 case NET_NFC_MESSAGE_NOTIFY:
1815                 {
1816                         PRINT_INFO("NET_NFC_MESSAGE_NOTIFY \n");
1817                 }
1818                 break;
1819
1820                 default:
1821                         break;
1822         }
1823 }
1824
1825
1826 static void net_nfc_test_read_write_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
1827 {
1828         int user_context;
1829
1830         printf ("callback is called with message %d\n", message);
1831
1832         switch(message)
1833         {
1834                 case NET_NFC_MESSAGE_TAG_DISCOVERED:
1835                 {
1836                         net_nfc_target_type_e type;
1837                         net_nfc_target_handle_h handle;
1838                         bool is_ndef;
1839                         net_nfc_target_info_h target_info = (net_nfc_target_info_h)data;
1840
1841                         net_nfc_get_tag_type (target_info, &type);
1842                         net_nfc_get_tag_handle (target_info, &handle);
1843                         net_nfc_get_tag_ndef_support (target_info, &is_ndef);
1844                         PRINT_INFO("target type: %d\n", type);
1845                         PRINT_INFO("target handle: %X\n", handle);
1846                         PRINT_INFO("Is NDEF supoort: %d\n", is_ndef);
1847
1848                         if(is_ndef)
1849                         {
1850                                 tag_handle = handle;
1851                                 if(net_nfc_read_tag(handle, user_param) == NET_NFC_OK)
1852                                 {
1853                                         PRINT_INFO("try to read ndef");
1854                                 }
1855                         }
1856                         else
1857                         {
1858                                 PRINT_INFO("non ndef tag.");
1859
1860                                 test_case_result = NET_NFC_TEST_FAIL;
1861
1862                                 pthread_mutex_lock (&plock);
1863                                 pthread_cond_signal (&pcond);
1864                                 pthread_mutex_unlock (&plock);
1865                         }
1866
1867                 }
1868                 break;
1869
1870                 case NET_NFC_MESSAGE_READ_NDEF:
1871                 {
1872                         if (write_count >= 50){
1873                                 PRINT_INSTRUCT("Test is finished. Please remove the tag from device!!");
1874                                 test_case_result = NET_NFC_TEST_OK;
1875                                 break;
1876                         }
1877                         if(data != NULL){
1878
1879                                 ndef_message_h ndef = (ndef_message_h)(data);
1880
1881                                 ////net_nfc_ndef_print_message(ndef);
1882
1883                                 test_case_result = NET_NFC_TEST_OK;
1884
1885                                 net_nfc_error_e error = NET_NFC_OK;
1886                                 ndef_message_h msg = NULL;
1887                                 ndef_record_h record = NULL;
1888
1889                                 if( (error = net_nfc_create_text_type_record(&record, "This is real NFC", "en-US", NET_NFC_ENCODE_UTF_8)) == NET_NFC_OK)
1890                                 {
1891                                         if( (error = net_nfc_create_ndef_message(&msg)) == NET_NFC_OK)
1892                                         {
1893                                                 if( (error = net_nfc_append_record_to_ndef_message(msg, record)) == NET_NFC_OK)
1894                                                 {
1895                                                         ////net_nfc_ndef_print_message(msg);
1896
1897                                                         if(net_nfc_write_ndef(tag_handle, msg, &user_context) == NET_NFC_OK)
1898                                                         {
1899                                                                 PRINT_INFO("write count = [%d] \n", write_count++);
1900                                                                 PRINT_INFO("write ndef msg");
1901                                                         }
1902                                                         net_nfc_free_ndef_message(msg);
1903                                                 }
1904                                                 else
1905                                                 {
1906                                                         PRINT_INFO("failed to append ndef message = [%d]", error);
1907                                                         net_nfc_free_record(record);
1908                                                         net_nfc_free_ndef_message(msg);
1909
1910                                                         pthread_mutex_lock (&plock);
1911                                                         pthread_cond_signal (&pcond);
1912                                                         pthread_mutex_unlock (&plock);
1913                                                 }
1914                                         }
1915                                         else
1916                                         {
1917                                                 PRINT_INFO("failed to create ndef msg");
1918                                                 net_nfc_free_record(record);
1919
1920                                                 pthread_mutex_lock (&plock);
1921                                                 pthread_cond_signal (&pcond);
1922                                                 pthread_mutex_unlock (&plock);
1923                                         }
1924                                 }
1925                                 else
1926                                 {
1927                                         PRINT_INFO("failed to create text type record");
1928
1929                                         pthread_mutex_lock (&plock);
1930                                         pthread_cond_signal (&pcond);
1931                                         pthread_mutex_unlock (&plock);
1932
1933                                 }
1934
1935                         }
1936                         else
1937                         {
1938                                 test_case_result = NET_NFC_TEST_FAIL;
1939
1940                                 pthread_mutex_lock (&plock);
1941                                 pthread_cond_signal (&pcond);
1942                                 pthread_mutex_unlock (&plock);
1943
1944                                 break;
1945                         }
1946
1947                         //net_nfc_read_ndef(tag_handle, user_param);
1948
1949                         break;
1950                 }
1951                 break;
1952
1953                 case NET_NFC_MESSAGE_WRITE_NDEF:
1954                 {
1955                         if (result == NET_NFC_OK) {
1956
1957                                 test_case_result = NET_NFC_TEST_OK;
1958
1959                                 if(net_nfc_read_tag(tag_handle, user_param) == NET_NFC_OK)
1960                                 {
1961                                         PRINT_INFO("read count = [%d] \n", read_count++);
1962                                         PRINT_INFO("try to read ndef");
1963                                 }
1964                         }
1965                         else {
1966                                 PRINT_INFO("received error: %d\n", result);
1967                                 test_case_result = NET_NFC_TEST_FAIL;
1968
1969                                 pthread_mutex_lock (&plock);
1970                                 pthread_cond_signal (&pcond);
1971                                 pthread_mutex_unlock (&plock);
1972                         }
1973                 }
1974                 break;
1975
1976                 case NET_NFC_MESSAGE_TAG_DETACHED:
1977                 {
1978                         PRINT_INFO("NET_NFC_MESSAGE_TAG_DETACHED \n");
1979
1980                         pthread_mutex_lock (&plock);
1981                         pthread_cond_signal (&pcond);
1982                         pthread_mutex_unlock (&plock);
1983                 }
1984                 break;
1985
1986                 case NET_NFC_MESSAGE_NOTIFY:
1987                 {
1988                         PRINT_INFO("NET_NFC_MESSAGE_NOTIFY \n");
1989                 }
1990                 break;
1991
1992                 default:
1993                         break;
1994         }
1995 }
1996
1997
1998 int nfcTestAPIMultipleRequest(uint8_t testNumber,void* arg_ptr2)
1999 {
2000         int user_context = 0;
2001         net_nfc_error_e result;
2002         test_case_result = NET_NFC_TEST_FAIL;
2003
2004         result = net_nfc_initialize();
2005         CHECK_RESULT(result);
2006
2007         net_nfc_state_activate(1);
2008
2009         result = net_nfc_set_response_callback (net_nfc_test_multiple_request_cb, NULL);
2010         CHECK_RESULT(result);
2011
2012         PRINT_INSTRUCT("Please close a tag to device for a while!!");
2013
2014         //pthread_cond_wait (&pcond,&plock );
2015 /*
2016         result = net_nfc_unset_response_callback ();
2017         CHECK_RESULT(result);
2018
2019         result = net_nfc_deinitialize ();
2020         CHECK_RESULT(result);
2021 */
2022         return test_case_result;
2023 }
2024
2025
2026 int nfcTestAPIBasicTest1(uint8_t testNumber,void* arg_ptr2)
2027 {
2028         int user_context = 0;
2029         net_nfc_error_e result;
2030         test_case_result = NET_NFC_TEST_FAIL;
2031
2032         int length = 0;
2033
2034 /*
2035         char** keys = NULL;
2036
2037         int i = 0;
2038
2039         for(i; i < NET_NFC_NFCIP1_INITIATOR; i++)
2040         {
2041                 if(net_nfc_get_tag_info_keys(i, &keys, &length) == NET_NFC_OK)
2042                 {
2043                         int index = 0;
2044
2045                         PRINT_INFO("############################\n") ;
2046                         for(; index < length; index++)
2047                         {
2048                                 PRINT_INFO("supported key = [%s] \n", keys[index]);
2049                         }
2050                         PRINT_INFO("############################\n")    ;
2051
2052                         test_case_result = NET_NFC_TEST_OK;
2053                 }
2054         }
2055 */
2056
2057         result = net_nfc_initialize();
2058         CHECK_RESULT(result);
2059
2060         net_nfc_state_activate(1);
2061
2062         result = net_nfc_set_response_callback (net_nfc_test_detected_cb, NULL);
2063
2064 //      net_nfc_set_tag_filter(NET_NFC_ALL_DISABLE);
2065 //      net_nfc_set_tag_filter(NET_NFC_ISO14443A_ENABLE);
2066 //      net_nfc_set_tag_filter(NET_NFC_ISO14443B_ENABLE);
2067 //      net_nfc_set_tag_filter(NET_NFC_ISO15693_ENABLE );
2068 //      net_nfc_set_tag_filter(NET_NFC_FELICA_ENABLE );
2069 //      net_nfc_set_tag_filter(NET_NFC_JEWEL_ENABLE );
2070 //      net_nfc_set_tag_filter(NET_NFC_ALL_ENABLE );
2071
2072
2073         CHECK_RESULT(result);
2074
2075         //PRINT_INSTRUCT("Please close a tag to device!!");
2076
2077         //pthread_cond_wait (&pcond,&plock );
2078
2079 /*
2080         result = net_nfc_unset_response_callback ();
2081         CHECK_RESULT(result);
2082
2083         result = net_nfc_deinitialize ();
2084
2085         sleep(1000);
2086
2087         CHECK_RESULT(result);
2088 */
2089
2090         return test_case_result;
2091 }
2092
2093 int nfcTestAPIBasicTest2(uint8_t testNumber,void* arg_ptr2)
2094 {
2095         int user_context = 0;
2096         net_nfc_error_e result;
2097         test_case_result = NET_NFC_TEST_FAIL;
2098
2099         result = net_nfc_initialize();
2100         CHECK_RESULT(result);
2101
2102         net_nfc_state_activate(1);
2103
2104         read_count = 0;
2105
2106         result = net_nfc_set_response_callback (net_nfc_test_read_cb, NULL);
2107         CHECK_RESULT(result);
2108
2109         PRINT_INSTRUCT("remove tag while reading operation !!");
2110         PRINT_INSTRUCT("Please close a tag to device!!");
2111
2112 /*
2113         pthread_cond_wait (&pcond,&plock );
2114
2115         result = net_nfc_unset_response_callback ();
2116         CHECK_RESULT(result);
2117
2118         result = net_nfc_deinitialize ();
2119         CHECK_RESULT(result);
2120 */
2121         return test_case_result;
2122 }
2123
2124 int nfcTestAPIBasicTest3(uint8_t testNumber,void* arg_ptr2)
2125 {
2126         int user_context = 0;
2127         net_nfc_error_e result;
2128         test_case_result = NET_NFC_TEST_FAIL;
2129
2130         ndef_message_h ndef_message = NULL;
2131
2132         if(net_nfc_retrieve_current_ndef_message(&ndef_message) == NET_NFC_OK)
2133         {
2134                 int count = 0;
2135                 if(net_nfc_get_ndef_message_record_count(ndef_message, &count) == NET_NFC_OK)
2136                 {
2137                         int i = 0;
2138                         for(; i < count; i++)
2139                         {
2140                                 ndef_record_h record = NULL;
2141                                 if(net_nfc_get_record_by_index(ndef_message, i, &record) == NET_NFC_OK)
2142                                 {
2143                                         net_nfc_record_tnf_e tnf;
2144                                         if(net_nfc_get_record_tnf(record, &tnf) == NET_NFC_OK)
2145                                         {
2146                                                 switch(tnf)
2147                                                 {
2148                                                         case NET_NFC_RECORD_WELL_KNOWN_TYPE:
2149                                                         {
2150                                                                 data_h type = NULL;
2151                                                                 data_h payload = NULL;
2152
2153                                                                 if(net_nfc_get_record_type(record, &type) == NET_NFC_OK && net_nfc_get_record_payload(record, &payload) == NET_NFC_OK)
2154                                                                 {
2155                                                                         if(strcmp(net_nfc_get_data_buffer(type), "Sp") == 0)
2156                                                                         {
2157
2158                                                                         }
2159                                                                         else if(strcmp(net_nfc_get_data_buffer(type), "T") == 0)
2160                                                                         {
2161
2162                                                                                 net_nfc_encode_type_e utf;
2163                                                                                 uint32_t utf_length = 0;
2164                                                                                 uint32_t lang_length = 0;
2165                                                                                 char* text = NULL;
2166                                                                                 uint32_t text_length = 0;
2167                                                                                 char* language_code_str = NULL;
2168
2169                                                                                 if(net_nfc_get_encoding_type_from_text_record(record, &utf) == NET_NFC_OK)
2170                                                                                 {
2171                                                                                         PRINT_INFO("utf = [%s]", utf);
2172                                                                                 }
2173                                                                                 else
2174                                                                                 {
2175                                                                                         PRINT_INFO("failed to get utf");
2176                                                                                 }
2177
2178                                                                                 if(net_nfc_get_languange_code_string_from_text_record(record, &language_code_str) == NET_NFC_OK)
2179                                                                                 {
2180                                                                                         PRINT_INFO("language_code = [%s]", language_code_str);
2181                                                                                 }
2182                                                                                 else
2183                                                                                 {
2184                                                                                         PRINT_INFO("failed to get language code");
2185                                                                                 }
2186
2187                                                                                 if(net_nfc_create_text_string_from_text_record(record, &text) == NET_NFC_OK)
2188                                                                                 {
2189                                                                                         PRINT_INFO("text = [%s]", text);
2190                                                                                 }
2191                                                                                 else
2192                                                                                 {
2193                                                                                         PRINT_INFO("failed to get text");
2194                                                                                 }
2195
2196                                                                                 if(text != NULL)
2197                                                                                         free(text);
2198 /*
2199                                                                                 uint8_t* utf = NULL;
2200                                                                                 uint8_t* language_code = NULL;
2201                                                                                 uint8_t* text = NULL;
2202                                                                                 int index = 0;
2203
2204                                                                                 uint8_t* buffer = net_nfc_get_data_buffer(payload);
2205                                                                                 uint32_t buffer_length = net_nfc_get_data_length(payload);
2206
2207                                                                                 PRINT_INFO("\n from file >>>");
2208
2209                                                                                 for(; index < buffer_length; index++)
2210                                                                                 {
2211                                                                                         printf(" [%d] ", buffer[index]);
2212                                                                                 }
2213
2214                                                                                 PRINT_INFO("\n from file <<<");
2215
2216                                                                                 index = 0;
2217
2218                                                                                 int controllbyte = buffer[0];
2219
2220                                                                                 if((controllbyte & 0x80)== 1)
2221                                                                                 {
2222                                                                                         PRINT_INFO("UTF-16");
2223                                                                                         utf = (uint8_t*)"UTF-16";
2224                                                                                 }
2225                                                                                 else
2226                                                                                 {
2227                                                                                         PRINT_INFO("UTF-8");
2228                                                                                         utf = (uint8_t*)"UTF-8";
2229                                                                                 }
2230
2231                                                                                 int lang_code_length = controllbyte & 0x3F;
2232
2233                                                                                 index = 1;
2234
2235                                                                                 language_code = (uint8_t *)malloc(lang_code_length + 1);
2236
2237                                                                                 memset(language_code, 0x00, lang_code_length + 1);
2238                                                                                 memcpy(language_code, &(buffer[index]), lang_code_length);
2239
2240                                                                                 PRINT_INFO("lang_code = [%s]", language_code);
2241
2242                                                                                 index = lang_code_length + 1;
2243
2244                                                                                 int text_length = buffer_length - (lang_code_length + 1); // payload length - (lang code length + control byte)
2245
2246                                                                                 PRINT_INFO("buffer length = [%d]", buffer_length);
2247                                                                                 PRINT_INFO("lang_code_length = [%d]", lang_code_length);
2248                                                                                 PRINT_INFO("text_length = [%d]", text_length);
2249
2250                                                                                 text = (uint8_t *)malloc(text_length + 1);
2251
2252                                                                                 memset(text, 0x00, text_length + 1);
2253                                                                                 memcpy(text, &(buffer[index]), text_length);
2254
2255                                                                                 PRINT_INFO("encoding type = [%s]", utf);
2256                                                                                 PRINT_INFO("lang_code = [%s]", language_code);
2257                                                                                 PRINT_INFO("text = [%s]", text);
2258                                                                                 test_case_result = NET_NFC_TEST_OK;
2259 */
2260                                                                         }
2261                                                                         else if(strcmp(net_nfc_get_data_buffer(type), "U") == 0)
2262                                                                         {
2263
2264                                                                         }
2265                                                                         else
2266                                                                         {
2267
2268                                                                         }
2269                                                                 }
2270
2271                                                         }
2272                                                         break;
2273
2274                                                         default:
2275                                                                 PRINT_INFO("unknow type");
2276                                                         break;
2277                                                 }
2278                                         }
2279                                 }
2280                         }
2281                 }
2282         }
2283
2284
2285         CHECK_RESULT(result);
2286
2287         return test_case_result;
2288 }
2289
2290 int nfcTestTransceive(uint8_t testNumber,void* arg_ptr2)
2291 {
2292         int user_context = 0;
2293         net_nfc_error_e result;
2294         test_case_result = NET_NFC_TEST_FAIL;
2295
2296         result = net_nfc_initialize();
2297         CHECK_RESULT(result);
2298
2299         net_nfc_state_activate(1);
2300
2301         result = net_nfc_set_response_callback (net_nfc_test_transceive_cb, NULL);
2302         CHECK_RESULT(result);
2303
2304         PRINT_INSTRUCT("Please close a tag to device!!");
2305
2306         //pthread_cond_wait (&pcond,&plock );
2307         /*
2308         result = net_nfc_unset_response_callback ();
2309         CHECK_RESULT(result);
2310         result = net_nfc_deinitialize ();
2311         CHECK_RESULT(result);
2312         */
2313
2314         return test_case_result;
2315 }
2316
2317 /*=================================================================================*/
2318
2319
2320 int nfcTestNdefParser(uint8_t testNumber,void* arg_ptr2)
2321 {
2322         net_nfc_error_e result;
2323
2324         ndef_message_h uriMsg = NULL;
2325         ndef_message_h spMsg = NULL;
2326         ndef_record_h  uriRecord  = NULL;
2327         ndef_record_h  spRecord = NULL;
2328         data_h type_data = NULL;
2329         data_h raw_data = NULL;
2330         char smart_poster_type[] = "Sp";
2331         ndef_record_h record = NULL;
2332         test_case_result = NET_NFC_TEST_OK;
2333
2334         result = net_nfc_create_ndef_message (&uriMsg);
2335         CHECK_RESULT(result);
2336
2337         result = net_nfc_create_uri_type_record (&record ,"http://www.naver.com", NET_NFC_SCHEMA_FULL_URI);
2338         CHECK_RESULT(result);
2339
2340         result = net_nfc_append_record_to_ndef_message (uriMsg, record);
2341         CHECK_RESULT(result);
2342
2343         uriRecord = record;
2344
2345         result = net_nfc_create_text_type_record (&record ,"This is the URI Test" ,"en-US" ,NET_NFC_ENCODE_UTF_8);
2346         CHECK_RESULT(result);
2347
2348         result = net_nfc_append_record_to_ndef_message (uriMsg, record);
2349         CHECK_RESULT(result);
2350
2351         result = net_nfc_create_text_type_record (&record ,"Hello World" ,"en-US" ,NET_NFC_ENCODE_UTF_8);
2352         CHECK_RESULT(result);
2353
2354         result = net_nfc_append_record_by_index (uriMsg,0 ,record);
2355         CHECK_RESULT(result);
2356
2357         result = net_nfc_create_text_type_record (&record ,"TEST1" ,"en-US" ,NET_NFC_ENCODE_UTF_8);
2358         CHECK_RESULT(result);
2359
2360         result = net_nfc_append_record_by_index (uriMsg,1 ,record);
2361         CHECK_RESULT(result);
2362
2363         result = net_nfc_create_text_type_record (&record ,"TEST2" ,"en-US" ,NET_NFC_ENCODE_UTF_8);
2364         CHECK_RESULT(result);
2365
2366         result = net_nfc_append_record_by_index (uriMsg,2 ,record);
2367         CHECK_RESULT(result);
2368
2369         result = net_nfc_create_text_type_record (&record ,"TEST3" ,"en-US" ,NET_NFC_ENCODE_UTF_8);
2370         CHECK_RESULT(result);
2371
2372         int position;
2373         result = net_nfc_get_ndef_message_record_count (uriMsg,&position);
2374         CHECK_RESULT(result);
2375
2376         result = net_nfc_append_record_by_index (uriMsg, position ,record);
2377         CHECK_RESULT(result);
2378
2379         //_//net_nfc_ndef_print_message (uriMsg);
2380
2381         result = net_nfc_create_data (&type_data ,"U", 1);
2382         CHECK_RESULT(result);
2383
2384         result = net_nfc_search_record_by_type (uriMsg ,NET_NFC_RECORD_WELL_KNOWN_TYPE ,type_data ,&record);
2385         CHECK_RESULT(result);
2386         if (record != uriRecord){
2387                 PRINT_RESULT_FAIL("Record search result does not matched");
2388                 return NET_NFC_TEST_FAIL;
2389         }
2390
2391         result = net_nfc_remove_record_by_index (uriMsg ,1);
2392         CHECK_RESULT(result);
2393
2394         result = net_nfc_remove_record_by_index (uriMsg ,0);
2395         CHECK_RESULT(result);
2396
2397         result = net_nfc_get_ndef_message_record_count (uriMsg,&position);
2398         CHECK_RESULT(result);
2399
2400         result = net_nfc_remove_record_by_index (uriMsg, position - 1);
2401         CHECK_RESULT(result);
2402
2403         result = net_nfc_remove_record_by_index (uriMsg ,2);
2404         CHECK_RESULT(result);
2405
2406         //_//net_nfc_ndef_print_message (uriMsg);
2407
2408         result = net_nfc_create_rawdata_from_ndef_message (uriMsg, &raw_data);
2409         CHECK_RESULT(result);
2410
2411         result = net_nfc_create_data (&type_data, smart_poster_type, strlen (smart_poster_type));
2412         CHECK_RESULT(result);
2413
2414         result = net_nfc_create_record (&spRecord, NET_NFC_RECORD_WELL_KNOWN_TYPE,type_data , NULL, raw_data);
2415         CHECK_RESULT(result);
2416
2417         result = net_nfc_create_ndef_message (&spMsg);
2418         CHECK_RESULT(result);
2419
2420         result = net_nfc_append_record_to_ndef_message (spMsg, spRecord);
2421         CHECK_RESULT(result);
2422
2423         net_nfc_free_data (type_data);
2424         net_nfc_free_data (raw_data);
2425
2426         return test_case_result;
2427 }
2428
2429 /*=================================================================================*/
2430
2431 net_nfc_llcp_socket_t server_socket;
2432 net_nfc_llcp_socket_t accepted_socket;
2433 net_nfc_llcp_socket_t client_socket;
2434
2435 static void net_nfc_client_socket_cb (net_nfc_llcp_message_e message, net_nfc_error_e result, void * data, void * user_data, void * trans_data)
2436 {
2437         PRINT_INFO ("\nCLIENT callback is called MESSAGE[%d]", message);
2438         int x;
2439         switch (message)
2440         {
2441                 case NET_NFC_MESSAGE_LLCP_LISTEN:
2442                 break;
2443                 case NET_NFC_MESSAGE_LLCP_ACCEPTED:
2444                 break;
2445                 case NET_NFC_MESSAGE_LLCP_CONNECT:
2446                 {
2447                         PRINT_INFO ("LLCP connect is completed with error code %d", result);
2448                         data_h data;
2449                         char * str = "Client message: Hello, server!";
2450                         net_nfc_create_data (&data, str ,strlen (str) + 1);
2451                         net_nfc_send_llcp (client_socket, data, NULL);
2452                 }
2453                 break;
2454                 case NET_NFC_MESSAGE_LLCP_CONNECT_SAP:
2455                 break;
2456                 case NET_NFC_MESSAGE_LLCP_SEND:
2457                 {
2458                         PRINT_INFO ("LLCP send is completed with error code %d", result);
2459                         net_nfc_receive_llcp (client_socket, 512 ,NULL);
2460                 }
2461                 break;
2462
2463                 case NET_NFC_MESSAGE_LLCP_RECEIVE:
2464                         PRINT_INFO ("LLCP receive is completed with error code %d", result);
2465                         data_h received_data = (data_h) data;
2466                         PRINT_INFO ("Server --> Client : %s" , net_nfc_get_data_buffer (received_data));
2467
2468                         net_nfc_disconnect_llcp (client_socket ,NULL);
2469                 break;
2470                 case NET_NFC_MESSAGE_LLCP_DISCONNECT:
2471                 {
2472                         PRINT_INFO ("LLCP disconnect is completed with error code %d", result);
2473                         net_nfc_close_llcp_socket (client_socket, NULL);
2474                 }
2475                 break;
2476                 case NET_NFC_MESSAGE_LLCP_ERROR:
2477                         PRINT_INFO ("LLCP socket error is completed with error code %d", result);
2478                         net_nfc_close_llcp_socket (client_socket, NULL);
2479                 break;
2480                 default:
2481                 break;
2482         }
2483
2484 }
2485
2486
2487 static void net_nfc_server_socket_cb (net_nfc_llcp_message_e message, net_nfc_error_e result, void * data, void * user_data, void * trans_data)
2488 {
2489         PRINT_INFO ("\nSERVER callback is called MESSAGE[%d]", message);
2490         switch (message)
2491         {
2492                 case NET_NFC_MESSAGE_LLCP_LISTEN:
2493                 {
2494                         PRINT_INFO ("LLCP Listen is completed with error code %d", result);
2495                 }
2496                 break;
2497
2498                 case NET_NFC_MESSAGE_LLCP_ACCEPTED:
2499                 {
2500                         PRINT_INFO ("LLCP accept is completed with error code %d", result);
2501                         accepted_socket = *(net_nfc_llcp_socket_t *)(data);
2502                         net_nfc_set_llcp_socket_callback (accepted_socket, net_nfc_server_socket_cb, NULL);
2503                         net_nfc_receive_llcp (accepted_socket, 512 ,NULL);
2504                 }
2505                 break;
2506                 case NET_NFC_MESSAGE_LLCP_CONNECT:
2507                 break;
2508                 case NET_NFC_MESSAGE_LLCP_CONNECT_SAP:
2509                 break;
2510                 case NET_NFC_MESSAGE_LLCP_SEND:
2511                 {
2512                         PRINT_INFO ("LLCP send is completed with error code %d", result);
2513                         net_nfc_disconnect_llcp (accepted_socket ,NULL);
2514                 }
2515                 break;
2516
2517                 case NET_NFC_MESSAGE_LLCP_RECEIVE:
2518                 {
2519                         PRINT_INFO ("LLCP receive is completed with error code %d", result);
2520                         data_h received_data = (data_h) data;
2521                         PRINT_INFO ("Server <-- Client : %s" , net_nfc_get_data_buffer (received_data));
2522
2523                         data_h data;
2524                         char * str = "Server message: Welcome NFC llcp world!";
2525                         net_nfc_create_data (&data, str ,strlen (str) + 1);
2526                         net_nfc_send_llcp (accepted_socket, data, NULL);
2527                 }
2528                 break;
2529
2530                 case NET_NFC_MESSAGE_LLCP_DISCONNECT:
2531                 {
2532                         PRINT_INFO ("LLCP disconnect is completed with error code %d", result);
2533                         net_nfc_close_llcp_socket (accepted_socket, NULL);
2534                 }
2535                 break;
2536                 case NET_NFC_MESSAGE_LLCP_ERROR:
2537                 {
2538                         PRINT_INFO ("LLCP socket error is received with code %d", result);
2539                         net_nfc_close_llcp_socket (accepted_socket, NULL);
2540                         net_nfc_close_llcp_socket (server_socket, NULL);
2541                 }
2542                 break;
2543                 default:
2544                 break;
2545         }
2546 }
2547
2548 net_nfc_target_handle_h snep_handle;
2549 net_nfc_exchanger_data_h snep_ex_data = NULL;
2550 int temp_count;
2551
2552 static void net_nfc_test_snep_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
2553 {
2554         PRINT_INFO ("client message received [%d]", message);
2555
2556         switch (message) {
2557 #if 0
2558                 case NET_NFC_MESSAGE_P2P_DISCOVERED:
2559                 {
2560                         snep_handle = (net_nfc_target_handle_h) data;
2561                         //= (net_nfc_target_handle_h) target_info->handle;
2562                         /* Fill the data to send. */
2563
2564                         if(NET_NFC_OK == net_nfc_create_exchanger_data(&snep_ex_data, nfcTestUri))
2565                         {
2566                                 if(net_nfc_send_exchanger_data(snep_ex_data,  snep_handle) == NET_NFC_OK)
2567                                 {
2568                                         PRINT_INFO ("exchanger data send success");
2569                                 }
2570                                 else
2571                                 {
2572                                         PRINT_INFO ("exchanger data send fail");
2573                                 }
2574                         }
2575                         else
2576                         {
2577                                 PRINT_INFO ("create exchanger data fail");
2578                         }
2579
2580                 }
2581                 break;
2582 #endif
2583                 case NET_NFC_MESSAGE_P2P_SEND:
2584                 {
2585                         PRINT_INFO ("NET_NFC_MESSAGE_P2P_SEND result [%d]", result);
2586
2587                 }
2588                 break;
2589
2590                 case NET_NFC_MESSAGE_P2P_DETACHED:
2591                 {
2592                         PRINT_INFO ("Target disconnected.");
2593                         temp_count = 0;
2594                 }
2595                 break;
2596
2597                 case NET_NFC_MESSAGE_P2P_RECEIVE:
2598                 {
2599 //                      int i;
2600 //                      data_h received_data = (data_h)data;
2601
2602 //                      PRINT_INFO ("NET_NFC_MESSAGE_P2P_RECEIVE [%s]", net_nfc_get_data_buffer(received_data));
2603 //                      PRINT_INFO ("   length [%d]", net_nfc_get_data_length(received_data));
2604                 }
2605                 break;
2606
2607                 default:
2608                 break;
2609         }
2610 }
2611
2612 static void net_nfc_test_llcp_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
2613 {
2614         // do nothing
2615         switch (message) {
2616                 case NET_NFC_MESSAGE_LLCP_DISCOVERED:
2617                 {
2618                         net_nfc_llcp_config_info_h config =  (net_nfc_llcp_config_info_h) data;
2619                         uint8_t lto, option;
2620                         uint16_t wks, miu;
2621                         net_nfc_get_llcp_configure_lto (config , &lto);
2622                         net_nfc_get_llcp_configure_wks (config , &wks);
2623                         net_nfc_get_llcp_configure_miu (config , &miu);
2624                         net_nfc_get_llcp_configure_option (config , &option);
2625
2626                         PRINT_INFO ("Remote Device llcp info:\n \tlto: %d, \twks: %d, \tmiu: %d, \toption: %d", lto, wks, miu, option);
2627
2628                         net_nfc_create_llcp_socket (&server_socket, NULL);
2629                         net_nfc_set_llcp_socket_callback (server_socket, net_nfc_server_socket_cb, NULL);
2630                         net_nfc_listen_llcp (server_socket, "urn:nfc:xsn:samsung.com:testllcp" ,16 ,NULL);
2631
2632                         net_nfc_create_llcp_socket (&client_socket, NULL);
2633                         net_nfc_set_llcp_socket_callback (client_socket, net_nfc_client_socket_cb, NULL);
2634                         net_nfc_connect_llcp (client_socket,"urn:nfc:xsn:samsung.com:testllcp",NULL);
2635                 }
2636                 break;
2637
2638                 case NET_NFC_MESSAGE_LLCP_CONFIG:
2639                 {
2640                         PRINT_INFO ("LLCP llcp local config is completed with error code %d", result);
2641                 }
2642                 break;
2643
2644                 case NET_NFC_MESSAGE_P2P_DETACHED:
2645                 {
2646                         PRINT_INFO ("LLCP NET_NFC_MESSAGE_P2P_DETACHED %d", result);
2647                 }
2648                 break;
2649
2650                 default:
2651                 break;
2652         }
2653 }
2654
2655 int nfcTestSnep(uint8_t testNumber,void* arg_ptr)
2656 {
2657         net_nfc_error_e result;
2658
2659         result = net_nfc_initialize();
2660         CHECK_RESULT(result);
2661         net_nfc_state_activate (1);
2662         result = net_nfc_set_response_callback (net_nfc_test_snep_cb, NULL);
2663
2664         PRINT_INSTRUCT("START SNEP test !!");
2665         return NET_NFC_TEST_OK;
2666 }
2667
2668 int nfcTestLLCP(uint8_t testNumber,void* arg_ptr2)
2669 {
2670         net_nfc_error_e result;
2671         net_nfc_llcp_config_info_h config;
2672         result = net_nfc_initialize();
2673         CHECK_RESULT(result);
2674         net_nfc_state_activate (1);
2675         result = net_nfc_set_response_callback (net_nfc_test_llcp_cb, NULL);
2676         CHECK_RESULT(result);
2677         result = net_nfc_create_llcp_configure_default (&config);
2678         CHECK_RESULT(result);
2679         result = net_nfc_set_llcp_local_configure (config ,NULL);
2680
2681         PRINT_INSTRUCT("Please start P2P communicate!!");
2682
2683 /*
2684         pthread_cond_wait (&pcond,&plock );
2685
2686         result = net_nfc_unset_response_callback ();
2687         CHECK_RESULT(result);
2688         result = net_nfc_deinitialize ();
2689         CHECK_RESULT(result);
2690 */
2691
2692         return NET_NFC_TEST_OK;
2693 }
2694
2695 /*=================================================================================*/
2696
2697 /* LLCP Stress Test */
2698
2699 net_nfc_llcp_socket_t server_socket;
2700 net_nfc_llcp_socket_t client_socket;
2701
2702 static void net_nfc_client_stress_socket_cb (net_nfc_llcp_message_e message, net_nfc_error_e result, void * data, void * user_data, void * trans_data)
2703 {
2704         PRINT_INFO ("\nCLIENT callback is called MESSAGE[%d]", message);
2705         int x;
2706         switch (message)
2707         {
2708                 case NET_NFC_MESSAGE_LLCP_LISTEN:
2709                 break;
2710                 case NET_NFC_MESSAGE_LLCP_ACCEPTED:
2711                 break;
2712                 case NET_NFC_MESSAGE_LLCP_CONNECT:
2713                 {
2714                         PRINT_INFO ("LLCP connect is completed with error code %d", result);
2715                         data_h data;
2716                         char * str = "Client message: Hello, server!";
2717                         net_nfc_create_data (&data, str ,strlen (str) + 1);
2718                         net_nfc_send_llcp (client_socket, data, NULL);
2719                 }
2720                 break;
2721                 case NET_NFC_MESSAGE_LLCP_CONNECT_SAP:
2722                 break;
2723                 case NET_NFC_MESSAGE_LLCP_SEND:
2724                 {
2725                         PRINT_INFO ("LLCP send is completed with error code %d", result);
2726                         net_nfc_receive_llcp (client_socket, 512 ,NULL);
2727                 }
2728                 break;
2729
2730                 case NET_NFC_MESSAGE_LLCP_RECEIVE:
2731                         PRINT_INFO ("LLCP receive is completed with error code %d", result);
2732                         data_h received_data = (data_h) data;
2733                         PRINT_INFO ("Server --> Client : %s" , net_nfc_get_data_buffer (received_data));
2734
2735                         net_nfc_disconnect_llcp (client_socket ,NULL);
2736                 break;
2737                 case NET_NFC_MESSAGE_LLCP_DISCONNECT:
2738                 {
2739                         PRINT_INFO ("LLCP disconnect is completed with error code %d", result);
2740                         net_nfc_close_llcp_socket (client_socket, NULL);
2741                 }
2742                 break;
2743                 case NET_NFC_MESSAGE_LLCP_ERROR:
2744                         PRINT_INFO ("LLCP socket error is completed with error code %d", result);
2745                         net_nfc_close_llcp_socket (client_socket, NULL);
2746                 break;
2747                 default:
2748                 break;
2749         }
2750
2751 }
2752
2753
2754 static void net_nfc_server_stress_socket_cb (net_nfc_llcp_message_e message, net_nfc_error_e result, void * data, void * user_data, void * trans_data)
2755 {
2756         PRINT_INFO ("\nSERVER callback is called MESSAGE[%d]", message);
2757         switch (message)
2758         {
2759                 case NET_NFC_MESSAGE_LLCP_LISTEN:
2760                 {
2761                         PRINT_INFO ("LLCP Listen is completed with error code %d", result);
2762                 }
2763                 break;
2764
2765                 case NET_NFC_MESSAGE_LLCP_ACCEPTED:
2766                 {
2767                         PRINT_INFO ("LLCP accept is completed with error code %d", result);
2768                         net_nfc_receive_llcp (server_socket, 512 ,NULL);
2769                 }
2770                 break;
2771                 case NET_NFC_MESSAGE_LLCP_CONNECT:
2772                 break;
2773                 case NET_NFC_MESSAGE_LLCP_CONNECT_SAP:
2774                 break;
2775                 case NET_NFC_MESSAGE_LLCP_SEND:
2776                 {
2777                         PRINT_INFO ("LLCP send is completed with error code %d", result);
2778                         net_nfc_disconnect_llcp (server_socket ,NULL);
2779                 }
2780                 break;
2781
2782                 case NET_NFC_MESSAGE_LLCP_RECEIVE:
2783                 {
2784                         PRINT_INFO ("LLCP receive is completed with error code %d", result);
2785                         data_h received_data = (data_h) data;
2786                         PRINT_INFO ("Server <-- Client : %s" , net_nfc_get_data_buffer (received_data));
2787
2788                         data_h data;
2789                         char * str = "Server message: Welcome NFC llcp world!";
2790                         net_nfc_create_data (&data, str ,strlen (str) + 1);
2791                         net_nfc_send_llcp (server_socket, data, NULL);
2792                 }
2793                 break;
2794
2795                 case NET_NFC_MESSAGE_LLCP_DISCONNECT:
2796                 {
2797                         PRINT_INFO ("LLCP disconnect is completed with error code %d", result);
2798                         net_nfc_close_llcp_socket (server_socket, NULL);
2799                 }
2800                 break;
2801
2802                 case NET_NFC_MESSAGE_LLCP_ERROR:
2803                 {
2804                         PRINT_INFO ("LLCP socket error is received with code %d", result);
2805                         net_nfc_close_llcp_socket (server_socket, NULL);
2806                 }
2807                 break;
2808                 default:
2809                 break;
2810         }
2811 }
2812
2813 static void net_nfc_test_llcp_stress_cb(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
2814 {
2815         // do nothing
2816         switch (message) {
2817                 case NET_NFC_MESSAGE_LLCP_DISCOVERED:
2818                 {
2819                         net_nfc_llcp_config_info_h config =  (net_nfc_llcp_config_info_h) data;
2820                         uint8_t lto, option;
2821                         uint16_t wks, miu;
2822                         net_nfc_get_llcp_configure_lto (config , &lto);
2823                         net_nfc_get_llcp_configure_wks (config , &wks);
2824                         net_nfc_get_llcp_configure_miu (config , &miu);
2825                         net_nfc_get_llcp_configure_option (config , &option);
2826
2827                         PRINT_INFO ("Remote Device llcp info:\n \tlto: %d, \twks: %d, \tmiu: %d, \toption: %d", lto, wks, miu, option);
2828
2829                         net_nfc_create_llcp_socket (&server_socket, NULL);
2830                         net_nfc_set_llcp_socket_callback (server_socket, net_nfc_server_stress_socket_cb, NULL);
2831                         net_nfc_listen_llcp (server_socket, "urn:nfc:xsn:samsung.com:testllcp" ,16 ,NULL);
2832
2833                         net_nfc_create_llcp_socket (&client_socket, NULL);
2834                         net_nfc_set_llcp_socket_callback (client_socket, net_nfc_client_stress_socket_cb, NULL);
2835                         net_nfc_connect_llcp (client_socket,"urn:nfc:xsn:samsung.com:testllcp",NULL);
2836                 }
2837                 break;
2838
2839                 case NET_NFC_MESSAGE_LLCP_CONFIG:
2840                 {
2841                         PRINT_INFO ("LLCP llcp local config is completed with error code %d", result);
2842                 }
2843                 break;
2844
2845                 case NET_NFC_MESSAGE_P2P_DETACHED:
2846                 {
2847                         PRINT_INFO ("LLCP NET_NFC_MESSAGE_P2P_DETACHED %d", result);
2848                 }
2849                 break;
2850
2851                 default:
2852                 break;
2853         }
2854 }
2855
2856 int nfcTestStressLLCP(uint8_t testNumber,void* arg_ptr2)
2857 {
2858         net_nfc_error_e result;
2859         net_nfc_llcp_config_info_h config;
2860         result = net_nfc_initialize();
2861         CHECK_RESULT(result);
2862         net_nfc_state_activate (1);
2863         result = net_nfc_set_response_callback (net_nfc_test_llcp_stress_cb, NULL);
2864         CHECK_RESULT(result);
2865         result = net_nfc_create_llcp_configure_default (&config);
2866         CHECK_RESULT(result);
2867         result = net_nfc_set_llcp_local_configure (config ,NULL);
2868
2869         PRINT_INSTRUCT("Please start P2P communicate!!");
2870 /*
2871         pthread_cond_wait (&pcond,&plock );
2872
2873         result = net_nfc_unset_response_callback ();
2874         CHECK_RESULT(result);
2875         result = net_nfc_deinitialize ();
2876         CHECK_RESULT(result);
2877 */
2878
2879         return NET_NFC_TEST_OK;
2880 }
2881
2882 /*=================================================================================*/
2883
2884
2885 static void net_nfc_test_API_exception_cb1(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
2886 {
2887         PRINT_INFO ("Message is received 1 %d", result);
2888         test_case_result = NET_NFC_TEST_FAIL;
2889         pthread_mutex_lock (&plock);
2890         pthread_cond_signal (&pcond);
2891         pthread_mutex_unlock (&plock);
2892 }
2893
2894 static void net_nfc_test_API_exception_cb2(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
2895 {
2896         PRINT_INFO ("Message is received 2 %d", result);
2897         test_case_result = NET_NFC_TEST_FAIL;
2898         pthread_mutex_lock (&plock);
2899         pthread_cond_signal (&pcond);
2900         pthread_mutex_unlock (&plock);
2901 }
2902
2903 static void net_nfc_test_API_exception_cb3(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
2904 {
2905         PRINT_INFO ("Message is received 3 %d", result);
2906         test_case_result = NET_NFC_TEST_OK;
2907         pthread_mutex_lock (&plock);
2908         pthread_cond_signal (&pcond);
2909         pthread_mutex_unlock (&plock);
2910 }
2911
2912
2913
2914
2915 int nfcTestAPIException (uint8_t testNumber,void* arg_ptr)
2916 {
2917         net_nfc_error_e result;
2918         test_case_result = NET_NFC_TEST_FAIL;
2919
2920         CHECK_ASSULT(net_nfc_initialize() == NET_NFC_OK);
2921         CHECK_ASSULT(net_nfc_initialize() == NET_NFC_ALREADY_INITIALIZED);
2922         CHECK_ASSULT(net_nfc_deinitialize () == NET_NFC_OK);
2923
2924         int count_try = 0;
2925         for (count_try = 0; count_try < 20; count_try ++)
2926         {
2927                 CHECK_ASSULT(net_nfc_initialize() == NET_NFC_OK);
2928                 CHECK_ASSULT(net_nfc_deinitialize () == NET_NFC_OK);
2929         }
2930
2931         CHECK_ASSULT(net_nfc_deinitialize () == NET_NFC_NOT_INITIALIZED);
2932         CHECK_ASSULT(net_nfc_deinitialize () == NET_NFC_NOT_INITIALIZED);
2933
2934
2935         CHECK_ASSULT(net_nfc_initialize() == NET_NFC_OK);
2936
2937         CHECK_ASSULT (NET_NFC_OK == net_nfc_set_response_callback (net_nfc_test_API_exception_cb1, NULL));
2938         CHECK_ASSULT (NET_NFC_OK == net_nfc_set_response_callback (net_nfc_test_API_exception_cb2, NULL));
2939         CHECK_ASSULT (NET_NFC_OK == net_nfc_set_response_callback (net_nfc_test_API_exception_cb3, NULL));
2940         net_nfc_state_activate (1);
2941
2942         PRINT_INSTRUCT("Please close a tag to device!!");
2943
2944         pthread_cond_wait (&pcond,&plock );
2945         if (!test_case_result) return test_case_result;
2946
2947         PRINT_INSTRUCT("Please remove the tag from device!!");
2948         pthread_cond_wait (&pcond,&plock );
2949
2950         CHECK_ASSULT (NET_NFC_OK == net_nfc_set_response_callback (net_nfc_test_API_exception_cb2, NULL));
2951         CHECK_ASSULT (NET_NFC_OK != net_nfc_set_response_callback (NULL, NULL));
2952
2953         CHECK_ASSULT (NET_NFC_OK == net_nfc_set_response_callback (net_nfc_test_API_exception_cb2, NULL));
2954         CHECK_ASSULT (NET_NFC_OK == net_nfc_unset_response_callback ());
2955
2956         PRINT_INSTRUCT("Please close a tag to device!! in 10 sec");
2957
2958         int idx = 0;
2959         for (idx = 10 ;idx > 0 ; idx --)
2960         {
2961                 PRINT_INSTRUCT("count down [%d]", idx);
2962                 sleep (1);
2963         }
2964         if (!test_case_result) return test_case_result;
2965
2966         PRINT_INSTRUCT("Please remove the tag from device!!");
2967         sleep (2);
2968
2969         CHECK_ASSULT (NET_NFC_OK == net_nfc_set_response_callback (net_nfc_test_API_exception_cb3, NULL));
2970         net_nfc_state_activate (1);
2971         net_nfc_state_activate (1);
2972         net_nfc_state_activate (1);
2973
2974         PRINT_INSTRUCT("Please close a tag to device!!");
2975
2976         pthread_cond_wait (&pcond,&plock );
2977         if (!test_case_result) return test_case_result;
2978         PRINT_INSTRUCT("Please remove the tag from device!!");
2979         pthread_cond_wait (&pcond,&plock );
2980
2981         net_nfc_state_deactivate ();
2982         PRINT_INSTRUCT("Please close a tag to device!! in 10 sec");
2983
2984         for (idx = 10 ;idx > 0 ; idx --)
2985         {
2986                 PRINT_INSTRUCT("count down [%d]", idx);
2987                 sleep (1);
2988         }
2989         if (!test_case_result) return test_case_result;
2990         PRINT_INSTRUCT("Please remove the tag from device!!");
2991         sleep (2);
2992
2993         net_nfc_state_activate (1);
2994         CHECK_ASSULT(net_nfc_deinitialize () == NET_NFC_OK);
2995
2996         PRINT_INSTRUCT("Please close a tag to device!! in 10 sec");
2997
2998
2999         for (idx = 10 ;idx > 0 ; idx --)
3000         {
3001                 PRINT_INSTRUCT("count down [%d]", idx);
3002                 sleep (1);
3003         }
3004         if (!test_case_result) return test_case_result;
3005
3006         PRINT_INSTRUCT("Please remove the tag from device!!");
3007         sleep (2);
3008
3009         CHECK_ASSULT(net_nfc_initialize() == NET_NFC_OK);
3010         CHECK_ASSULT (NET_NFC_OK == net_nfc_set_response_callback (net_nfc_test_API_exception_cb3, NULL));
3011         net_nfc_state_activate (1);
3012
3013         PRINT_INSTRUCT("Please close a tag to device!!");
3014
3015         pthread_cond_wait (&pcond,&plock );
3016         if (!test_case_result) return test_case_result;
3017
3018         return NET_NFC_TEST_OK;
3019 }
3020
3021 /*=================================================================================*/
3022
3023
3024 static void net_nfc_test_API_exception_tagAPI(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
3025 {
3026         PRINT_INFO ("Message is received 3 %d", result);
3027         test_case_result = NET_NFC_TEST_OK;
3028
3029         switch(message)
3030         {
3031                 case NET_NFC_MESSAGE_TAG_DISCOVERED:
3032                 {
3033                         switch (*(int*) user_param)
3034                         {
3035                                 case 0: // transceive
3036                                 {
3037                                         net_nfc_target_type_e type;
3038                                         net_nfc_target_handle_h id;
3039                                         bool is_ndef;
3040                                         net_nfc_target_info_h target_info = (net_nfc_target_info_h)data;
3041                                         net_nfc_error_e e_ret ;
3042
3043                                         net_nfc_get_tag_type (target_info, &type);
3044                                         net_nfc_get_tag_handle (target_info, &id);
3045                                         net_nfc_get_tag_ndef_support (target_info, &is_ndef);
3046                                         PRINT_INFO("target type: %d\n", type);
3047                                         PRINT_INFO("target id: %X\n", id);
3048                                         PRINT_INFO("Is NDEF supoort: %d\n", is_ndef);
3049
3050                                         net_nfc_deinitialize (); // Before calling transceive
3051
3052                                 }
3053                                 break;
3054                                 case 1:
3055                                 {
3056                                         net_nfc_target_info_h target_info = (net_nfc_target_info_h)data;
3057                                         net_nfc_target_handle_h id;
3058                                         net_nfc_get_tag_handle (target_info, &id);
3059                                         net_nfc_deinitialize();
3060                                         if (NET_NFC_OK == net_nfc_read_tag (id ,NULL)){
3061                                                 test_case_result = NET_NFC_TEST_FAIL;
3062                                         }
3063                                 }
3064                                 break;
3065                                 case 2:
3066                                 {
3067                                         net_nfc_target_info_h target_info = (net_nfc_target_info_h)data;
3068                                         net_nfc_target_handle_h id;
3069                                         ndef_message_h message = NULL;
3070                                         ndef_record_h record = NULL;
3071
3072                                         net_nfc_get_tag_handle (target_info, &id);
3073                                         net_nfc_deinitialize();
3074
3075                                         net_nfc_create_ndef_message (&message);
3076                                         net_nfc_create_text_type_record (&record, "abc" ,"en-US" ,NET_NFC_ENCODE_UTF_8);
3077                                         net_nfc_append_record_to_ndef_message (message ,record);
3078                                         if (NET_NFC_OK == net_nfc_write_ndef (id ,message ,NULL)){
3079                                                 test_case_result = NET_NFC_TEST_FAIL;
3080                                         }
3081
3082                                 }
3083                                 break;
3084                                 case 3:
3085                                 {
3086                                         net_nfc_target_info_h target_info = (net_nfc_target_info_h)data;
3087                                         net_nfc_target_handle_h id;
3088                                         data_h key;
3089                                         char data [] = {0xff,0xff,0xff,0xff,0xff,0xff};
3090                                         net_nfc_create_data (&key, data, 6);
3091                                         net_nfc_get_tag_handle (target_info, &id);
3092                                         net_nfc_deinitialize();
3093                                         if (NET_NFC_OK == net_nfc_format_ndef(id, key, NULL)){
3094                                                 test_case_result = NET_NFC_TEST_FAIL;
3095                                         }
3096                                 }
3097                                 break;
3098                         }
3099
3100                         pthread_mutex_lock (&plock);
3101                         pthread_cond_signal (&pcond);
3102                         pthread_mutex_unlock (&plock);
3103                 }
3104                 break;
3105         }
3106 }
3107
3108
3109 int nfcTestAPIException_tagAPI (uint8_t testNumber,void* arg_ptr)
3110 {
3111         int test_case = 0;
3112
3113         /* Call API before initailize */
3114
3115         data_h key;
3116         char data [] = {0xff,0xff,0xff,0xff,0xff,0xff};
3117         net_nfc_create_data (&key, data, 6);
3118         CHECK_ASSULT (NET_NFC_OK != net_nfc_format_ndef((net_nfc_target_handle_h) 0x302023, key, NULL));
3119         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_format_ndef(NULL, key, NULL));
3120         CHECK_ASSULT (NET_NFC_OK != net_nfc_format_ndef((net_nfc_target_handle_h) 0x302023, NULL, NULL));
3121
3122         CHECK_ASSULT (NET_NFC_OK != net_nfc_read_tag ((net_nfc_target_handle_h) 0x302023 ,NULL));
3123         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_read_tag (NULL ,NULL));
3124
3125         ndef_message_h message = NULL;
3126         ndef_record_h record = NULL;
3127         net_nfc_create_ndef_message (&message);
3128         net_nfc_create_text_type_record (&record, "abc" ,"en-US" ,NET_NFC_ENCODE_UTF_8);
3129         net_nfc_append_record_to_ndef_message (message ,record);
3130         CHECK_ASSULT (NET_NFC_OK != net_nfc_write_ndef ((net_nfc_target_handle_h)0x302023 ,message,NULL));
3131         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_write_ndef (NULL ,message,NULL));
3132         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_write_ndef ((net_nfc_target_handle_h)0x302023 ,NULL,NULL));
3133         net_nfc_free_ndef_message (message);
3134
3135
3136         for (test_case = 0 ; test_case < 4; test_case++){
3137                 CHECK_ASSULT(net_nfc_initialize() == NET_NFC_OK);
3138                 net_nfc_state_activate (1);
3139                 PRINT_INSTRUCT("Please close a tag to device!!");
3140                 CHECK_ASSULT (NET_NFC_OK == net_nfc_set_response_callback (net_nfc_test_API_exception_tagAPI, &test_case));
3141                 pthread_cond_wait (&pcond,&plock );
3142                 if (test_case_result == NET_NFC_TEST_FAIL) return test_case_result;
3143                 PRINT_INSTRUCT("Please remoe the tag from device!!");
3144                 sleep(2);
3145         }
3146
3147         CHECK_ASSULT(net_nfc_deinitialize () != NET_NFC_OK);
3148
3149         return NET_NFC_TEST_OK;
3150 }
3151 /*=================================================================================*/
3152
3153
3154 static void net_nfc_test_API_exception_targetInfo(net_nfc_message_e message, net_nfc_error_e result, void* data, void* user_param, void * trans_data)
3155 {
3156         PRINT_INFO ("Message is received 3 %d", result);
3157         test_case_result = NET_NFC_TEST_OK;
3158
3159         char **keys;
3160         int length;
3161         data_h value;
3162
3163         switch(message)
3164         {
3165                 case NET_NFC_MESSAGE_TAG_DISCOVERED:
3166                 {
3167
3168                         if (NET_NFC_OK != net_nfc_get_tag_info_keys((net_nfc_target_info_h)data, &keys, &length)){
3169                                 test_case_result = NET_NFC_TEST_FAIL;
3170                                 return;
3171                         }
3172
3173                         if (NET_NFC_OK == net_nfc_get_tag_info_value ((net_nfc_target_info_h)data, "abc", &value)){
3174                                 test_case_result = NET_NFC_TEST_FAIL;
3175                                 return;
3176                         }
3177                         PRINT_INSTRUCT("Please remove the tag from device!!");
3178
3179                 }
3180                 break;
3181                 case NET_NFC_MESSAGE_TAG_DETACHED:
3182
3183                         if (NET_NFC_OK == net_nfc_get_tag_info_keys((net_nfc_target_info_h)data, &keys, &length)){
3184                                 test_case_result = NET_NFC_TEST_FAIL;
3185                                 return;
3186                         }
3187
3188                         pthread_mutex_lock (&plock);
3189                         pthread_cond_signal (&pcond);
3190                         pthread_mutex_unlock (&plock);
3191
3192
3193                 break;
3194         }
3195 }
3196
3197 int nfcTestAPIException_targetInfo (uint8_t testNumber,void* arg_ptr)
3198 {
3199         net_nfc_target_type_e target_type;
3200         net_nfc_target_handle_h target_id;
3201         bool is_support;
3202         unsigned int size;
3203         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_type ((net_nfc_target_info_h)0x302023, NULL));
3204         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_type (NULL, &target_type));
3205
3206         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_handle((net_nfc_target_info_h)0x302023, NULL));
3207         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_handle(NULL, &target_id));
3208
3209         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_ndef_support ((net_nfc_target_info_h)0x302023, NULL));
3210         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_ndef_support (NULL, &is_support));
3211
3212         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_max_data_size ((net_nfc_target_info_h)0x302023, NULL));
3213         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_max_data_size (NULL, &size));
3214
3215         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_actual_data_size ((net_nfc_target_info_h)0x302023, NULL));
3216         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_actual_data_size (NULL, &size));
3217
3218         char **keys;
3219         int length;
3220         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_info_keys((net_nfc_target_info_h)0x302023, NULL, &length));
3221         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_info_keys((net_nfc_target_info_h)0x302023, &keys, NULL));
3222         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_info_keys(NULL, &keys, &length));
3223
3224         const char* key = "hello";
3225         data_h value;
3226
3227         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_info_value((net_nfc_target_info_h)0x302023, key , NULL));
3228         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_info_value((net_nfc_target_info_h)0x302023, NULL, &value));
3229         CHECK_ASSULT (NET_NFC_NULL_PARAMETER == net_nfc_get_tag_info_value(NULL, key, &value));
3230
3231         CHECK_ASSULT(net_nfc_initialize() == NET_NFC_OK);
3232         net_nfc_state_activate (1);
3233         CHECK_ASSULT (NET_NFC_OK == net_nfc_set_response_callback (net_nfc_test_API_exception_targetInfo, NULL));
3234         PRINT_INSTRUCT("Please close a tag to device!!");
3235
3236         pthread_cond_wait (&pcond,&plock );
3237
3238         CHECK_ASSULT(net_nfc_deinitialize() == NET_NFC_OK);
3239         return NET_NFC_TEST_OK;
3240 }
3241
3242 /*=================================================================================*/
3243
3244
3245 int nfcConnHandoverMessageTest (uint8_t testNumber,void* arg_ptr)
3246 {
3247         net_nfc_carrier_config_h carrier;
3248         net_nfc_property_group_h group;
3249         ndef_record_h carrier_record;
3250         ndef_message_h message;
3251         uint8_t buffer[256] = {0,};
3252         uint8_t * pdata;
3253         int length = 0;
3254         net_nfc_error_e result;
3255         char SSID[] = "HomeWLAN";
3256         char dev_name[] = "DeviceName";
3257         uint8_t btdev_addr[] = {0x01, 0x07, 0x80, 0x80, 0xBF, 0xA1};
3258
3259         result = net_nfc_create_carrier_config (&carrier ,NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS);
3260         CHECK_RESULT(result);
3261
3262         buffer[0] = 0x10;
3263         result = net_nfc_add_carrier_config_property (carrier, NET_NFC_WIFI_ATTRIBUTE_VERSION, 1, buffer);
3264         CHECK_RESULT(result);
3265         result = net_nfc_create_carrier_config_group (&group, NET_NFC_WIFI_ATTRIBUTE_CREDENTIAL);
3266         CHECK_RESULT(result);
3267
3268         buffer[0] = 0x1;
3269         result = net_nfc_add_carrier_config_group_property (group, NET_NFC_WIFI_ATTRIBUTE_NET_INDEX, 1, buffer);
3270         CHECK_RESULT(result);
3271
3272         length = sprintf (buffer, "%s", SSID);
3273         result = net_nfc_add_carrier_config_group_property (group, NET_NFC_WIFI_ATTRIBUTE_SSID, length, buffer);
3274         CHECK_RESULT(result);
3275
3276         *((uint16_t*) buffer ) = 0x20;
3277         result = net_nfc_add_carrier_config_group_property (group, NET_NFC_WIFI_ATTRIBUTE_AUTH_TYPE, 2, buffer);
3278         CHECK_RESULT(result);
3279
3280         *((uint16_t*) buffer ) = 0x0008;
3281         result = net_nfc_add_carrier_config_group_property (group, NET_NFC_WIFI_ATTRIBUTE_ENC_TYPE, 2, buffer);
3282         CHECK_RESULT(result);
3283
3284         length = sprintf (buffer, "MyPreSharedKey");
3285         result = net_nfc_add_carrier_config_group_property (group, NET_NFC_WIFI_ATTRIBUTE_NET_KEY, length, buffer);
3286         CHECK_RESULT(result);
3287
3288         buffer[0] = 0x00;
3289         buffer[1] = 0x07;
3290         buffer[2] = 0xE9;
3291         buffer[3] = 0x4C;
3292         buffer[4] = 0xA8;
3293         buffer[5] = 0x1C;
3294         result = net_nfc_add_carrier_config_group_property (group, NET_NFC_WIFI_ATTRIBUTE_MAC_ADDR, 6, buffer);
3295         CHECK_RESULT(result);
3296
3297
3298         *((uint16_t*) buffer ) = 0x0001;
3299         result = net_nfc_add_carrier_config_group_property (group, NET_NFC_WIFI_ATTRIBUTE_CHANNEL, 2, buffer);
3300         CHECK_RESULT(result);
3301
3302         buffer[0] = 0x00;
3303         buffer[1] = 0x37;
3304         buffer[2] = 0x2A;
3305         result = net_nfc_add_carrier_config_group_property (group, NET_NFC_WIFI_ATTRIBUTE_VEN_EXT, 3, buffer);
3306         CHECK_RESULT(result);
3307
3308         result = net_nfc_append_carrier_config_group (carrier, group);
3309         CHECK_RESULT(result);
3310
3311         buffer[0] = 0x20;
3312         result = net_nfc_add_carrier_config_property (carrier, NET_NFC_WIFI_ATTRIBUTE_VERSION2, 1, buffer);
3313         CHECK_RESULT(result);
3314
3315         result = net_nfc_create_ndef_record_with_carrier_config (&carrier_record ,carrier);
3316         CHECK_RESULT(result);
3317
3318         result = net_nfc_free_carrier_config (carrier); /* this free all data include group */
3319         CHECK_RESULT(result);
3320
3321         result = net_nfc_create_handover_request_message (&message);
3322         CHECK_RESULT(result);
3323
3324         //net_nfc_ndef_print_message (message);
3325
3326         result = net_nfc_append_carrier_config_record (message, carrier_record, NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE);
3327         CHECK_RESULT(result);
3328
3329         //net_nfc_ndef_print_message (message);
3330
3331
3332         // Create BT config
3333         result = net_nfc_create_carrier_config (&carrier ,NET_NFC_CONN_HANDOVER_CARRIER_BT);
3334         CHECK_RESULT(result);
3335
3336         buffer[0] = btdev_addr[0];
3337         buffer[1] = btdev_addr[1];
3338         buffer[2] = btdev_addr[2];
3339         buffer[3] = btdev_addr[3];
3340         buffer[4] = btdev_addr[4];
3341         buffer[5] = btdev_addr[5];
3342         result = net_nfc_add_carrier_config_property (carrier ,NET_NFC_BT_ATTRIBUTE_ADDRESS ,6 ,buffer);
3343         CHECK_RESULT(result);
3344
3345         buffer[0] = 0x08;
3346         buffer[1] = 0x06;
3347         buffer[2] = 0x20;
3348         result = net_nfc_add_carrier_config_property (carrier ,NET_NFC_BT_ATTRIBUTE_OOB_COD ,3 ,buffer);
3349         CHECK_RESULT(result);
3350
3351
3352         result = net_nfc_add_carrier_config_property (carrier ,NET_NFC_BT_ATTRIBUTE_OOB_HASH_C ,16 ,buffer);
3353         CHECK_RESULT(result);
3354
3355         result = net_nfc_add_carrier_config_property (carrier ,NET_NFC_BT_ATTRIBUTE_OOB_HASH_R ,16 ,buffer);
3356         CHECK_RESULT(result);
3357
3358         buffer[0] = 0x06;
3359         buffer[1] = 0x11;
3360         buffer[2] = 0x20;
3361         buffer[3] = 0x11;
3362         result = net_nfc_add_carrier_config_property (carrier ,NET_NFC_BT_ATTRIBUTE_UUID16 ,4 ,buffer);
3363         CHECK_RESULT(result);
3364
3365         length = sprintf (buffer, "%s", dev_name);
3366         result = net_nfc_add_carrier_config_property (carrier ,NET_NFC_BT_ATTRIBUTE_NAME ,length ,buffer);
3367         CHECK_RESULT(result);
3368
3369         result = net_nfc_create_ndef_record_with_carrier_config (&carrier_record ,carrier);
3370         CHECK_RESULT(result);
3371
3372         result = net_nfc_append_carrier_config_record (message, carrier_record, NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATING);
3373         CHECK_RESULT(result);
3374
3375         //net_nfc_ndef_print_message (message);
3376
3377         result = net_nfc_free_carrier_config (carrier);
3378         CHECK_RESULT(result);
3379
3380         result = net_nfc_get_carrier_config_record (message,0 ,&carrier_record);
3381         CHECK_RESULT(result);
3382
3383         result = net_nfc_create_carrier_config_from_config_record (&carrier ,carrier_record);
3384         CHECK_RESULT(result);
3385
3386         result = net_nfc_get_carrier_config_property (carrier ,NET_NFC_WIFI_ATTRIBUTE_VERSION , &length, &pdata);
3387         CHECK_RESULT(result);
3388         if (pdata[0] != 0x10){
3389                 PRINT_RESULT_FAIL("FILE:%s, LINE:%d, value %d",__FILE__,__LINE__, pdata[0]);
3390                 return NET_NFC_TEST_FAIL;
3391         }
3392
3393         result = net_nfc_get_carrier_config_group (carrier ,0 , &group);
3394         CHECK_RESULT(result);
3395
3396         result = net_nfc_get_carrier_config_group_property (group ,NET_NFC_WIFI_ATTRIBUTE_SSID, &length, &pdata);
3397         CHECK_RESULT(result);
3398         if (memcmp (pdata, SSID, length) != 0){
3399                 PRINT_RESULT_FAIL("FILE:%s, LINE:%d,",__FILE__,__LINE__);
3400                 return NET_NFC_TEST_FAIL;
3401         }
3402
3403         result = net_nfc_remove_carrier_config_group_property (group,NET_NFC_WIFI_ATTRIBUTE_VEN_EXT);
3404         CHECK_RESULT(result);
3405
3406         result = net_nfc_get_carrier_config_record (message,1 ,&carrier_record);
3407         CHECK_RESULT(result);
3408
3409         result = net_nfc_create_carrier_config_from_config_record (&carrier ,carrier_record);
3410         CHECK_RESULT(result);
3411
3412         result = net_nfc_get_carrier_config_property (carrier ,NET_NFC_BT_ATTRIBUTE_ADDRESS , &length, &pdata);
3413         CHECK_RESULT(result);
3414         if (memcmp (pdata, btdev_addr, length) != 0) {
3415                 PRINT_RESULT_FAIL("FILE:%s, LINE:%d,",__FILE__,__LINE__);
3416                 return NET_NFC_TEST_FAIL;
3417         }
3418
3419         result = net_nfc_get_carrier_config_property (carrier ,NET_NFC_BT_ATTRIBUTE_NAME , &length, &pdata);
3420         CHECK_RESULT(result);
3421         if (memcmp (pdata, dev_name, length) != 0){
3422                 PRINT_RESULT_FAIL("FILE:%s, LINE:%d,",__FILE__,__LINE__);
3423                 return NET_NFC_TEST_FAIL;
3424         }
3425
3426         result = net_nfc_get_alternative_carrier_record_count (message, &length);
3427         CHECK_RESULT(result);
3428
3429         if (length != 2) {
3430                 PRINT_RESULT_FAIL("FILE:%s, LINE:%d, count = %d",__FILE__,__LINE__, length);
3431                 return NET_NFC_TEST_FAIL;
3432         }
3433
3434         net_nfc_conn_handover_carrier_state_e power_state;
3435         result = net_nfc_get_alternative_carrier_power_status (message,0 ,&power_state);
3436         CHECK_RESULT(result);
3437
3438         if (power_state != NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE){
3439                 PRINT_RESULT_FAIL("FILE:%s, LINE:%d,",__FILE__,__LINE__);
3440                 return NET_NFC_TEST_FAIL;
3441         }
3442         result = net_nfc_set_alternative_carrier_power_status (message,1 ,NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE);
3443         CHECK_RESULT(result);
3444
3445         result = net_nfc_get_alternative_carrier_power_status (message,1 ,&power_state);
3446         CHECK_RESULT(result);
3447
3448         if (power_state != NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE){
3449                 PRINT_RESULT_FAIL("FILE:%s, LINE:%d, status = %d",__FILE__,__LINE__, power_state);
3450                 return NET_NFC_TEST_FAIL;
3451         }
3452
3453         net_nfc_conn_handover_carrier_type_e ctype;
3454         result = net_nfc_get_alternative_carrier_type (message, 0 , &ctype);
3455
3456         if (ctype != NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS){
3457                 PRINT_RESULT_FAIL("FILE:%s, LINE:%d,",__FILE__,__LINE__);
3458                 return NET_NFC_TEST_FAIL;
3459         }
3460
3461         unsigned short r_num;
3462         result = net_nfc_get_handover_random_number (message, &r_num);
3463
3464         result = net_nfc_get_carrier_config_record (message,0 ,&carrier_record);
3465         CHECK_RESULT(result);
3466
3467         result = net_nfc_remove_carrier_config_record (message, carrier_record);
3468         CHECK_RESULT(result);
3469
3470         result = net_nfc_get_carrier_config_record (message,0 ,&carrier_record);
3471         CHECK_RESULT(result);
3472
3473         result = net_nfc_create_carrier_config_from_config_record (&carrier ,carrier_record);
3474         CHECK_RESULT(result);
3475
3476         result = net_nfc_get_carrier_config_property (carrier ,NET_NFC_BT_ATTRIBUTE_ADDRESS , &length, &pdata);
3477         CHECK_RESULT(result);
3478         if (memcmp (pdata, btdev_addr, length) != 0) {
3479                 PRINT_RESULT_FAIL("FILE:%s, LINE:%d,",__FILE__,__LINE__);
3480                 return NET_NFC_TEST_FAIL;
3481         }
3482
3483         result = net_nfc_get_carrier_config_property (carrier ,NET_NFC_BT_ATTRIBUTE_NAME , &length, &pdata);
3484         CHECK_RESULT(result);
3485         if (memcmp (pdata, dev_name, length) != 0){
3486                 PRINT_RESULT_FAIL("FILE:%s, LINE:%d,",__FILE__,__LINE__);
3487                 return NET_NFC_TEST_FAIL;
3488         }
3489
3490         result = net_nfc_get_alternative_carrier_record_count (message, &length);
3491         CHECK_RESULT(result);
3492
3493         if (length != 1) {
3494                                 PRINT_RESULT_FAIL("FILE:%s, LINE:%d,",__FILE__,__LINE__);
3495                 return NET_NFC_TEST_FAIL;
3496         }
3497
3498         result = net_nfc_get_alternative_carrier_power_status (message,0 ,&power_state);
3499         CHECK_RESULT(result);
3500
3501         if (power_state != NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE){
3502                                 PRINT_RESULT_FAIL("FILE:%s, LINE:%d,",__FILE__,__LINE__);
3503                 return NET_NFC_TEST_FAIL;
3504         }
3505
3506         result = net_nfc_get_alternative_carrier_type (message, 0 , &ctype);
3507
3508         if (ctype != NET_NFC_CONN_HANDOVER_CARRIER_BT){
3509                 PRINT_RESULT_FAIL("FILE:%s, LINE:%d,",__FILE__,__LINE__);
3510                 return NET_NFC_TEST_FAIL;
3511         }
3512
3513         //net_nfc_ndef_print_message (message);
3514
3515         return NET_NFC_TEST_OK;
3516
3517 }
3518
3519 /*=================================================================================*/
3520
3521