Remove unnecessary setting
[platform/core/connectivity/nfc-manager-neard.git] / tests / net_nfc_test_ndef.c
1 /*
2  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *                               http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include "net_nfc_client_ndef.h"
19
20 #include "net_nfc_test_tag.h"
21 #include "net_nfc_test_ndef.h"
22 #include "net_nfc_test_util.h"
23 #include "net_nfc_ndef_message.h"
24 #include "net_nfc_target_info.h"
25 #include "net_nfc_ndef_record.h"
26 #include "net_nfc_test_util.h"
27
28 static gchar *ndef_str = NULL;
29 static gint ndef_count = 0;
30
31 static void run_next_callback(gpointer user_data)
32 {
33         if (user_data)
34         {
35                 GCallback callback;
36
37                 callback = (GCallback)(user_data);
38
39                 callback();
40         }
41 }
42
43 static void set_string(const gchar *str)
44 {
45         gint count = 0;
46
47         if (str == NULL)
48                 return;
49
50         sscanf(str, "Count: %d", &count);
51
52         if (count == 0)
53                 ndef_str = g_strdup(str);
54         else
55         {
56                 gchar *tmp;
57                 gchar *pos;
58
59                 pos = (gchar *)str;
60                 tmp = g_strdup_printf("Count: %d", count);
61                 if (strncmp(pos, tmp, strlen(tmp)) == 0)
62                 {
63                         if (*pos == ' ')
64                                 pos++;
65
66                         ndef_str = g_strdup(pos + strlen(tmp));
67                 }
68
69                 g_free(tmp);
70         }
71
72         ndef_count = count;
73 }
74
75 static void print_record_well_known_type(ndef_record_s *record)
76 {
77         gchar *uri = NULL;
78         gchar *lang = NULL;
79         gchar *text = NULL;
80
81         net_nfc_encode_type_e enc;
82
83         net_nfc_create_uri_string_from_uri_record(record, &uri);
84         if (uri)
85         {
86                 g_print("URI: %s\n", uri);
87                 g_free(uri);
88         }
89
90         net_nfc_create_text_string_from_text_record(record, &text);
91         if(text)
92         {
93                 g_print("Text:\n%s\n", text);
94                 set_string(text);
95                 g_free(text);
96
97         }
98
99         net_nfc_get_languange_code_string_from_text_record(record, &lang);
100         if(lang)
101         {
102                 g_print("Language: %s\n", lang);
103                 g_free(lang);
104         }
105
106         net_nfc_get_encoding_type_from_text_record(record, &enc);
107         switch (enc)
108         {
109         case NET_NFC_ENCODE_UTF_8:
110                 g_print("Encoding: UTF-8\n");
111                 break;
112         case NET_NFC_ENCODE_UTF_16:
113                 g_print("Encoding: UTF-16\n");
114                 break;
115         default:
116                 g_print("Encoding: Unknown\n");
117         }
118 }
119
120 static void print_record(ndef_record_s *record)
121 {
122         guint8 flag;
123         gchar *str = NULL;
124
125         gchar *tnf_str[] = {
126                 "Empty",
127                 "Well-known type",
128                 "Media-type",
129                 "Absolute URI",
130                 "External type",
131                 "Unknown",
132                 "Unchanged"
133         };
134
135         net_nfc_record_tnf_e tnf;
136
137         data_s *type = NULL;
138         data_s *id = NULL;
139         data_s *payload = NULL;
140
141         net_nfc_get_record_flags(record, &flag);
142         net_nfc_get_record_tnf(record, &tnf);
143
144         g_print("MB: %d ME: %d CF: %d SR: %d IL: %d TNF: %s\n",
145                         net_nfc_get_record_mb(flag),
146                         net_nfc_get_record_me(flag),
147                         net_nfc_get_record_cf(flag),
148                         net_nfc_get_record_sr(flag),
149                         net_nfc_get_record_il(flag),
150                         tnf_str[tnf]);
151
152         if (net_nfc_get_record_type(record, &type) == NET_NFC_OK)
153         {
154                 str = g_strndup((gchar *)net_nfc_get_data_buffer(type),
155                                 net_nfc_get_data_length(type));
156                 g_print("Type : %s\n", str);
157                 g_free(str);
158         }
159
160         net_nfc_get_record_id(record, &id);
161
162         str = g_strndup((gchar *)net_nfc_get_data_buffer(id),
163                         net_nfc_get_data_length(id));
164         g_print("ID : %s\n", str);
165         g_free(str);
166
167         if (net_nfc_get_record_payload(record, &payload) == NET_NFC_OK)
168         {
169                 print_received_data(payload);
170         }
171
172         switch(tnf)
173         {
174         case NET_NFC_RECORD_EMPTY:
175                 break;
176         case NET_NFC_RECORD_WELL_KNOWN_TYPE:
177                 print_record_well_known_type(record);
178                 break;
179         case NET_NFC_RECORD_MIME_TYPE:
180                 break;
181         case NET_NFC_RECORD_URI:
182                 break;
183         case NET_NFC_RECORD_EXTERNAL_RTD:
184                 break;
185         case NET_NFC_RECORD_UNKNOWN:
186                 break;
187         case NET_NFC_RECORD_UNCHAGNED:
188                 break;
189         default:
190                 g_print("TNF: unknown error\n");
191                 break;
192         }
193 }
194
195 static void print_ndef_message(ndef_message_s *message)
196 {
197         gint count = 0;
198         gint i;
199
200         if (message == NULL)
201         {
202                 g_print("Empty ndef message\n");
203                 return;
204         }
205
206         if (net_nfc_get_ndef_message_record_count(message,
207                                 &count) != NET_NFC_OK)
208         {
209                 g_print("can not get count of record\n");
210                 return;
211         }
212
213         for (i = 0; i < count; i++)
214         {
215                 ndef_record_s *record = NULL;
216
217                 g_print("Record count : %d\n", i+1);
218
219                 if (net_nfc_get_record_by_index(message, i, &record) != NET_NFC_OK)
220                 {
221                         g_print("can not get record from index %d\n", i);
222                         continue;
223                 }
224
225                 print_record(record);
226         }
227
228         g_print("\n");
229 }
230
231 static net_nfc_target_handle_s* ndef_get_handle(void)
232 {
233         net_nfc_target_info_s *info;
234         net_nfc_target_handle_s *handle;
235
236         bool is_ndef = false;
237
238         info = net_nfc_test_tag_get_target_info();
239         if (info == NULL)
240         {
241                 g_print("net_nfc_target_info_s *is NULL\n");
242                 return NULL;
243         }
244
245         net_nfc_get_tag_ndef_support(info, &is_ndef);
246
247         if (is_ndef == false)
248         {
249                 g_print("Tag does not support NDEF\n");
250                 return NULL;
251         }
252
253         net_nfc_get_tag_handle(info, &handle);
254
255         return handle;
256 }
257
258 static ndef_message_s *create_ndef_message_text(const gchar *str,
259                 const gchar *lang,
260                 net_nfc_encode_type_e encode)
261 {
262         ndef_record_s *record = NULL;
263         ndef_message_s *message = NULL;
264
265         if (net_nfc_create_ndef_message(&message) != NET_NFC_OK)
266         {
267                 g_printerr("Can not create ndef message\n");
268                 return NULL;
269         }
270
271         if (net_nfc_create_text_type_record(&record,
272                                 str,
273                                 lang,
274                                 encode) != NET_NFC_OK)
275         {
276                 g_printerr("Can not create text record(%s, %d): %s\n",
277                                 lang, encode, str);
278
279                 net_nfc_free_ndef_message(message);
280                 return NULL;
281         }
282
283         if (net_nfc_append_record_to_ndef_message(message,
284                                 record) != NET_NFC_OK)
285         {
286                 g_printerr("Can not append record to message\n");
287                 net_nfc_free_ndef_message(message);
288         }
289
290         return message;
291 }
292
293 static gchar *get_write_string(void)
294 {
295         gchar *str = NULL;
296
297         ndef_count++;
298
299         if (ndef_str == NULL)
300                 str = g_strdup_printf("Count: %d", ndef_count);
301         else
302                 str = g_strdup_printf("Count: %d %s", ndef_count, ndef_str);
303
304         return str;
305 }
306
307 static void ndef_read_cb(net_nfc_error_e result, ndef_message_s *message,
308                 void *user_data)
309 {
310         g_print("Read NDEF Completed %d\n", result);
311
312         print_ndef_message(message);
313
314         run_next_callback(user_data);
315 }
316
317 static void ndef_write_cb(net_nfc_error_e result, void *user_data)
318 {
319         g_print("Write NDEF Completed %d\n", result);
320
321         run_next_callback(user_data);
322 }
323
324
325 static void ndef_make_read_only_cb(net_nfc_error_e result, void *user_data)
326 {
327         g_print("Make Read only Completed %d\n", result);
328
329         run_next_callback(user_data);
330 }
331
332 static void ndef_format_cb(net_nfc_error_e result, void *user_data)
333
334 {
335         g_print("NDEF Format Completed: %d\n", result);
336
337         run_next_callback(user_data);
338 }
339
340
341
342 void net_nfc_test_ndef_read(gpointer data, gpointer user_data)
343 {
344         net_nfc_target_handle_s *handle;
345
346         handle = ndef_get_handle();
347         if (handle == NULL)
348         {
349                 g_printerr("Handle is NULL\n");
350
351                 run_next_callback(user_data);
352
353                 return;
354         }
355
356         g_print("Handle is %#x\n", GPOINTER_TO_UINT(handle));
357
358         net_nfc_client_ndef_read(handle, ndef_read_cb, user_data);
359 }
360
361 void net_nfc_test_ndef_write(gpointer data, gpointer user_data)
362 {
363         net_nfc_target_handle_s *handle;
364         ndef_message_s *message;
365
366         gchar *str = NULL;
367
368         handle = ndef_get_handle();
369         if (handle == NULL)
370         {
371                 g_printerr("Handle is NULL\n");
372
373                 run_next_callback(user_data);
374                 return;
375         }
376
377         g_print("Handle is %#x\n", GPOINTER_TO_UINT(handle));
378
379         str = get_write_string();
380         if(str == NULL)
381         {
382                 g_printerr("Can not get write string\n");
383
384                 run_next_callback(user_data);
385                 return;
386         }
387
388         message = create_ndef_message_text(str, "en-US", NET_NFC_ENCODE_UTF_8);
389
390         g_free(str);
391
392         if (message == NULL)
393         {
394                 g_printerr("message is NULL\n");
395
396                 run_next_callback(user_data);
397                 return;
398         }
399
400         net_nfc_client_ndef_write(handle, message, ndef_write_cb, user_data);
401
402 }
403
404 void net_nfc_test_ndef_make_read_only(gpointer data, gpointer user_data)
405 {
406         net_nfc_target_handle_s *handle;
407
408         handle = ndef_get_handle();
409         if (handle == NULL)
410         {
411                 g_printerr("Handle is NULL\n");
412
413                 run_next_callback(user_data);
414                 return;
415         }
416
417         g_print("Handle is %#x\n", GPOINTER_TO_UINT(handle));
418
419         net_nfc_client_ndef_make_read_only(handle,
420                         ndef_make_read_only_cb,
421                         user_data);
422 }
423
424 void net_nfc_test_ndef_format(gpointer data, gpointer user_data)
425 {
426         net_nfc_target_handle_s *handle;
427         data_s *key;
428         guint8 format_data[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
429
430         handle = ndef_get_handle();
431         if (handle == NULL)
432         {
433                 g_printerr("Handle is NULL\n");
434
435                 run_next_callback(user_data);
436                 return;
437         }
438
439         net_nfc_create_data(&key, format_data, 6);
440
441         net_nfc_client_ndef_format(handle, key, ndef_format_cb, user_data);
442
443         net_nfc_free_data(key);
444
445         return;
446 }
447
448 void net_nfc_test_ndef_read_sync(gpointer data, gpointer user_data)
449 {
450         net_nfc_target_handle_s *handle;
451         ndef_message_s *message = NULL;
452         net_nfc_error_e result;
453
454         handle = ndef_get_handle();
455         if (handle == NULL)
456         {
457                 g_printerr("Handle is NULL\n");
458
459                 run_next_callback(user_data);
460                 return;
461         }
462
463         g_print("Handle is %#x\n", GPOINTER_TO_UINT(handle));
464
465         result = net_nfc_client_ndef_read_sync(handle,
466                         &message);
467
468         g_print("Read Ndef: %d\n", result);
469
470         print_ndef_message(message);
471
472         net_nfc_free_ndef_message(message);
473
474         run_next_callback(user_data);
475 }
476
477 void net_nfc_test_ndef_write_sync(gpointer data, gpointer user_data)
478 {
479         gchar *str = NULL;
480
481         net_nfc_target_handle_s *handle;
482         ndef_message_s *message;
483         net_nfc_error_e result;
484
485         handle = ndef_get_handle();
486         if (handle == NULL)
487         {
488                 g_printerr("Handle is NULL\n");
489
490                 run_next_callback(user_data);
491                 return;
492         }
493
494         g_print("Handle is %#x\n", GPOINTER_TO_UINT(handle));
495
496         str = get_write_string();
497         if(str == NULL)
498         {
499                 g_printerr("Can not get write string\n");
500
501                 run_next_callback(user_data);
502                 return;
503         }
504
505         message = create_ndef_message_text(str, "en-US", NET_NFC_ENCODE_UTF_8);
506
507         g_free(str);
508
509         if (message == NULL)
510         {
511                 g_printerr("message is NULL\n");
512
513                 run_next_callback(user_data);
514
515                 return;
516         }
517
518         result = net_nfc_client_ndef_write_sync(handle, message);
519
520         g_print("Write Ndef: %d\n", result);
521
522         run_next_callback(user_data);
523 }
524
525 void net_nfc_test_ndef_make_read_only_sync(gpointer data, gpointer user_data)
526 {
527         net_nfc_target_handle_s *handle;
528         net_nfc_error_e result;
529
530         handle = ndef_get_handle();
531         if (handle == NULL)
532         {
533                 g_printerr("Handle is NULL\n");
534
535                 run_next_callback(user_data);
536
537                 return;
538         }
539
540         g_print("Handle is %#x\n", GPOINTER_TO_UINT(handle));
541
542         result = net_nfc_client_ndef_make_read_only_sync(handle);
543
544         g_print("Make Read Only: %d\n", result);
545
546         run_next_callback(user_data);
547 }
548
549 void net_nfc_test_ndef_format_sync(gpointer data, gpointer user_data)
550 {
551         net_nfc_target_handle_s *handle;
552         net_nfc_error_e result;
553         data_s *key;
554         guint8 format_data[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
555
556         handle = ndef_get_handle();
557         if (handle == NULL)
558         {
559                 g_printerr("Handle is NULL\n");
560
561                 run_next_callback(user_data);
562
563                 return;
564         }
565
566         net_nfc_create_data(&key, format_data, 6);
567
568         result = net_nfc_client_ndef_format_sync(handle, key);
569
570         net_nfc_free_data(key);
571
572         g_print("NDEF Format %d\n", result);
573
574         run_next_callback(user_data);
575 }