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