[Fix 64bit Build Error] Correctly type casting unsigned int to pointer using GLIB...
[platform/core/api/nfc.git] / src / nfc_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 #include "nfc_common.h"
18
19 /**
20  * @brief RTD(Record type definition) Type - Smart Poster type.
21  * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
22  */
23 const unsigned char NFC_RECORD_SMART_POSTER_TYPE[2] = { 'S', 'p' };
24
25 /**
26  * @brief  RTD(Record type definition) Type - Text type.
27  * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
28  */
29 const unsigned char NFC_RECORD_TEXT_TYPE[1] = { 'T' };
30 /**
31  * @brief  RTD(Record type definition) Type - URI type.
32  * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
33  */
34 const unsigned char NFC_RECORD_URI_TYPE[1] = { 'U' };
35 /**
36  * @brief  RTD(Record type definition) Type - Alternative Carrier type.
37  * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
38  */
39 const unsigned char NFC_RECORD_ALTERNATIVE_CARRIER_TYPE[2] = { 'a','c' };
40 /**
41  * @brief  RTD(Record type definition) Type - Handover Carrier type.
42  * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
43  */
44 const unsigned char NFC_RECORD_HANDOVER_CARRIER_TYPE[2] = { 'H','c' };
45 /**
46  * @brief  RTD(Record type definition) Type - Handover Request type.
47  * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
48  */
49 const unsigned char NFC_RECORD_HANDOVER_REQUEST_TYPE[2] = { 'H','r' };
50 /**
51  * @brief  RTD(Record type definition) Type - Handover Select type.
52  * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
53  */
54 const unsigned char NFC_RECORD_HANDOVER_SELECT_TYPE[2] = { 'H','s' };
55
56 int nfc_ndef_record_create(nfc_ndef_record_h *record,
57         nfc_record_tnf_e tnf,
58         const unsigned char *type,
59         int type_size,
60         const unsigned char *id,
61         int id_size,
62         const unsigned char *payload,
63         unsigned int payload_size)
64 {
65         int ret;
66         data_h type_data = NULL;
67         data_h id_data = NULL;
68         data_h payload_data = NULL;
69
70         LOG_BEGIN();
71
72         CHECK_SUPPORTED(NFC_FEATURE);
73
74         /* LCOV_EXCL_START */
75         CHECK_INIT();
76         CHECK_INVALID(record == NULL);
77
78         net_nfc_create_data(&type_data, type, type_size);
79
80         if (id != NULL && id_size > 0)
81                 net_nfc_create_data(&id_data, id, id_size);
82
83         if (payload != NULL && payload_size > 0)
84                 net_nfc_create_data(&payload_data, payload, payload_size);
85
86         ret = net_nfc_create_record(
87                 (ndef_record_h *)record,
88                 tnf,
89                 type_data,
90                 id_data,
91                 payload_data);
92
93         if (payload_data != NULL)
94                 net_nfc_free_data(payload_data);
95
96         if (id_data != NULL)
97                 net_nfc_free_data(id_data);
98
99         net_nfc_free_data(type_data);
100
101         return nfc_common_convert_error_code(__func__, ret);
102         /* LCOV_EXCL_STOP */
103 }
104
105
106 int nfc_ndef_record_create_text(nfc_ndef_record_h *record,
107         const char *text,
108         const char *lang_code,
109         nfc_encode_type_e encode)
110 {
111         int ret;
112
113         LOG_BEGIN();
114
115         CHECK_SUPPORTED(NFC_FEATURE);
116
117         /* LCOV_EXCL_START */
118         CHECK_INIT();
119         CHECK_INVALID(record == NULL);
120         CHECK_INVALID(text == NULL);
121         CHECK_INVALID(lang_code == NULL);
122
123         ret = net_nfc_create_text_type_record(
124                 (ndef_record_h *)record,
125                 text,
126                 lang_code,
127                 encode);
128
129         return nfc_common_convert_error_code(__func__, ret);
130         /* LCOV_EXCL_STOP */
131 }
132
133 int nfc_ndef_record_create_uri(nfc_ndef_record_h *record,
134         const char *uri)
135 {
136         int ret;
137
138         LOG_BEGIN();
139
140         CHECK_SUPPORTED(NFC_FEATURE);
141
142         /* LCOV_EXCL_START */
143         CHECK_INIT();
144         CHECK_INVALID(record == NULL);
145         CHECK_INVALID(uri == NULL);
146
147         ret = net_nfc_create_uri_type_record(
148                 (ndef_record_h *)record,
149                 uri,
150                 NET_NFC_SCHEMA_FULL_URI);
151
152         return nfc_common_convert_error_code(__func__, ret);
153         /* LCOV_EXCL_STOP */
154 }
155
156 int nfc_ndef_record_create_mime(nfc_ndef_record_h *record,
157         const char *mime_type,
158         const unsigned char *data,
159         unsigned int data_size)
160 {
161         int ret;
162
163         LOG_BEGIN();
164
165         CHECK_SUPPORTED(NFC_FEATURE);
166
167         /* LCOV_EXCL_START */
168         CHECK_INIT();
169         CHECK_INVALID(record == NULL);
170         CHECK_INVALID(mime_type == NULL);
171         CHECK_INVALID(data == NULL);
172         CHECK_INVALID(data_size <= 0);
173
174         ret = nfc_ndef_record_create(record,
175                 NFC_RECORD_TNF_MIME_MEDIA,
176                 (unsigned char *)mime_type,
177                 strlen(mime_type),
178                 NULL,
179                 0,
180                 data,
181                 data_size);
182
183         return nfc_common_convert_error_code(__func__, ret);
184         /* LCOV_EXCL_STOP */
185 }
186
187 int nfc_ndef_record_get_mime_type(nfc_ndef_record_h record,
188         char **mime_type)
189 {
190         int ret;
191         nfc_record_tnf_e tnf;
192         unsigned char *typename;
193         int length;
194
195         LOG_BEGIN();
196
197         CHECK_SUPPORTED(NFC_FEATURE);
198
199         /* LCOV_EXCL_START */
200         CHECK_INIT();
201         CHECK_INVALID(record == NULL);
202         CHECK_INVALID(mime_type == NULL);
203
204         if (nfc_ndef_record_get_tnf(record, &tnf) != NET_NFC_OK ||
205                 tnf != NFC_RECORD_TNF_MIME_MEDIA) {
206                 return NFC_ERROR_INVALID_RECORD_TYPE;
207         }
208
209         ret = nfc_ndef_record_get_type(record, &typename, &length);
210         if (ret == NET_NFC_OK) {
211                 *mime_type = calloc(1, length + 1);
212                 if (*mime_type != NULL)
213                         memcpy(*mime_type, typename, length);
214                 else
215                         ret = NET_NFC_ALLOC_FAIL;
216         }
217
218         return nfc_common_convert_error_code(__func__, ret);
219         /* LCOV_EXCL_STOP */
220 }
221
222 int nfc_ndef_record_destroy(nfc_ndef_record_h record)
223 {
224         int ret;
225
226         LOG_BEGIN();
227
228         CHECK_SUPPORTED(NFC_FEATURE);
229
230         /* LCOV_EXCL_START */
231         CHECK_INIT();
232         CHECK_INVALID(record == NULL);
233
234         ret = net_nfc_free_record(record);
235
236         return nfc_common_convert_error_code(__func__, ret);
237         /* LCOV_EXCL_STOP */
238 }
239
240 int nfc_ndef_record_set_id(nfc_ndef_record_h record,
241         unsigned char *id,
242         int id_size)
243 {
244         int ret;
245         data_h id_data = NULL;
246
247         LOG_BEGIN();
248
249         CHECK_SUPPORTED(NFC_FEATURE);
250
251         /* LCOV_EXCL_START */
252         CHECK_INIT();
253         CHECK_INVALID(record == NULL);
254         CHECK_INVALID(id == NULL);
255
256         ret = net_nfc_create_data(&id_data, id, id_size);
257         if (ret == NET_NFC_OK) {
258                 ret = net_nfc_set_record_id(record, id_data);
259
260                 net_nfc_free_data(id_data);
261         } else {
262                 LOG_ERR("net_nfc_create_data failed, [%d]", ret);
263         }
264
265         return nfc_common_convert_error_code(__func__, ret);
266         /* LCOV_EXCL_STOP */
267 }
268
269 int nfc_ndef_record_get_payload(nfc_ndef_record_h record,
270         unsigned char **payload,
271         unsigned int *size)
272 {
273         int ret;
274         data_h payload_data;
275
276         LOG_BEGIN();
277
278         CHECK_SUPPORTED(NFC_FEATURE);
279
280         /* LCOV_EXCL_START */
281         CHECK_INIT();
282         CHECK_INVALID(record == NULL);
283         CHECK_INVALID(payload == NULL);
284         CHECK_INVALID(size == NULL);
285
286         *payload = NULL;
287         *size = 0;
288
289         ret = net_nfc_get_record_payload(record, &payload_data);
290         if (ret == NET_NFC_OK) {
291                 *payload = net_nfc_get_data_buffer(payload_data);
292                 *size = net_nfc_get_data_length(payload_data);
293         } else {
294                 *payload = NULL;
295                 *size = 0;
296         }
297
298         return nfc_common_convert_error_code(__func__, ret);
299         /* LCOV_EXCL_STOP */
300 }
301
302 int nfc_ndef_record_get_type(nfc_ndef_record_h record,
303         unsigned char **type,
304         int *size)
305 {
306         int ret;
307         data_h type_data;
308
309         LOG_BEGIN();
310
311         CHECK_SUPPORTED(NFC_FEATURE);
312
313         /* LCOV_EXCL_START */
314         CHECK_INIT();
315         CHECK_INVALID(record == NULL);
316         CHECK_INVALID(type == NULL);
317         CHECK_INVALID(size == NULL);
318
319         *type = NULL;
320         *size = 0;
321
322         ret = net_nfc_get_record_type(record, &type_data);
323         if (ret == NET_NFC_OK) {
324                 *type = net_nfc_get_data_buffer(type_data);
325                 *size = net_nfc_get_data_length(type_data);
326         } else {
327                 *type = NULL;
328                 *size = 0;
329         }
330
331         return nfc_common_convert_error_code(__func__, ret);
332         /* LCOV_EXCL_STOP */
333 }
334
335 int nfc_ndef_record_get_id(nfc_ndef_record_h record,
336         unsigned char **id,
337         int *size)
338 {
339         int ret;
340         data_h id_data;
341
342         LOG_BEGIN();
343
344         CHECK_SUPPORTED(NFC_FEATURE);
345
346         /* LCOV_EXCL_START */
347         CHECK_INIT();
348         CHECK_INVALID(record == NULL);
349         CHECK_INVALID(id == NULL);
350         CHECK_INVALID(size == NULL);
351
352         *id = NULL;
353         *size = 0;
354
355         ret = net_nfc_get_record_id(record, &id_data);
356         if (ret == NET_NFC_OK) {
357                 *id = net_nfc_get_data_buffer(id_data);
358                 *size = net_nfc_get_data_length(id_data);
359         } else {
360                 *id = NULL;
361                 *size = 0;
362         }
363
364         return nfc_common_convert_error_code(__func__, ret);
365         /* LCOV_EXCL_STOP */
366 }
367
368 int nfc_ndef_record_get_tnf(nfc_ndef_record_h record, nfc_record_tnf_e *tnf)
369 {
370         int ret;
371
372         LOG_BEGIN();
373
374         CHECK_SUPPORTED(NFC_FEATURE);
375
376         /* LCOV_EXCL_START */
377         CHECK_INIT();
378         CHECK_INVALID(record == NULL);
379         CHECK_INVALID(tnf == NULL);
380
381         ret = net_nfc_get_record_tnf(record, (net_nfc_record_tnf_e *)tnf);
382
383         return nfc_common_convert_error_code(__func__, ret);
384         /* LCOV_EXCL_STOP */
385 }
386
387 int nfc_ndef_record_get_text(nfc_ndef_record_h record, char **buffer)
388 {
389         int ret;
390         unsigned char* record_type = NULL;
391         int type_size = 0;
392
393         LOG_BEGIN();
394
395         CHECK_SUPPORTED(NFC_FEATURE);
396
397         /* LCOV_EXCL_START */
398         CHECK_INIT();
399         CHECK_INVALID(record == NULL);
400         CHECK_INVALID(buffer == NULL);
401
402         ret = nfc_ndef_record_get_type(record, &record_type, &type_size);
403
404         if (ret == NFC_ERROR_NONE && record_type != NULL && type_size != 0 &&
405                                 !strcmp((char*)record_type, "T")) {
406                 LOG_ERR("record type is T");
407                 ret = net_nfc_create_text_string_from_text_record(record, buffer);
408         } else {
409                 LOG_ERR("record type is not T");
410                 return NFC_ERROR_INVALID_RECORD_TYPE;
411         }
412
413         return nfc_common_convert_error_code(__func__, ret);
414         /* LCOV_EXCL_STOP */
415 }
416
417 int nfc_ndef_record_get_langcode(nfc_ndef_record_h record, char **lang_code)
418 {
419         int ret;
420
421         LOG_BEGIN();
422
423         CHECK_SUPPORTED(NFC_FEATURE);
424
425         /* LCOV_EXCL_START */
426         CHECK_INIT();
427         CHECK_INVALID(record == NULL);
428         CHECK_INVALID(lang_code == NULL);
429
430         ret = net_nfc_get_languange_code_string_from_text_record(record, lang_code);
431
432         return nfc_common_convert_error_code(__func__, ret);
433         /* LCOV_EXCL_STOP */
434 }
435
436 int nfc_ndef_record_get_encode_type(
437         nfc_ndef_record_h record,
438         nfc_encode_type_e *encode)
439 {
440         int ret;
441
442         LOG_BEGIN();
443
444         CHECK_SUPPORTED(NFC_FEATURE);
445
446         /* LCOV_EXCL_START */
447         CHECK_INIT();
448         CHECK_INVALID(record == NULL);
449         CHECK_INVALID(encode == NULL);
450
451         ret = net_nfc_get_encoding_type_from_text_record(
452                 record,
453                 (net_nfc_encode_type_e *)encode);
454
455         /*      if( ret == NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE)
456          LOGE("%s reord type is not text type");*/
457
458         return nfc_common_convert_error_code(__func__, ret);
459         /* LCOV_EXCL_STOP */
460 }
461
462 int nfc_ndef_record_get_uri(nfc_ndef_record_h record, char **uri)
463 {
464         int ret;
465
466         LOG_BEGIN();
467
468         CHECK_SUPPORTED(NFC_FEATURE);
469
470         /* LCOV_EXCL_START */
471         CHECK_INIT();
472         CHECK_INVALID(record == NULL);
473         CHECK_INVALID(uri == NULL);
474
475         ret = net_nfc_create_uri_string_from_uri_record(record, uri);
476
477         return nfc_common_convert_error_code(__func__, ret);
478         /* LCOV_EXCL_STOP */
479 }
480
481 int nfc_ndef_message_create(nfc_ndef_message_h *ndef_message)
482 {
483         int ret;
484
485         LOG_BEGIN();
486
487         CHECK_SUPPORTED(NFC_FEATURE);
488
489         /* LCOV_EXCL_START */
490         CHECK_INIT();
491         CHECK_INVALID(ndef_message == NULL);
492
493         ret = net_nfc_create_ndef_message(ndef_message);
494
495         return nfc_common_convert_error_code(__func__, ret);
496         /* LCOV_EXCL_STOP */
497 }
498
499 int nfc_ndef_message_create_from_rawdata(
500         nfc_ndef_message_h *ndef_message,
501         const unsigned char *rawdata,
502         unsigned int rawdata_size)
503 {
504         int ret;
505         data_h rawdata_data = NULL;
506
507         LOG_BEGIN();
508
509         CHECK_SUPPORTED(NFC_FEATURE);
510
511         /* LCOV_EXCL_START */
512         CHECK_INIT();
513         CHECK_INVALID(ndef_message == NULL);
514         CHECK_INVALID(rawdata == NULL);
515         CHECK_INVALID(rawdata_size <= 0);
516
517         ret = net_nfc_create_data(&rawdata_data, rawdata, rawdata_size);
518         if (ret == NET_NFC_OK) {
519                 ret = net_nfc_create_ndef_message_from_rawdata(
520                         (ndef_message_h *)ndef_message,
521                         rawdata_data);
522
523                 net_nfc_free_data(rawdata_data);
524         } else {
525                 LOG_ERR("net_nfc_create_data failed, [%d]", ret);
526         }
527
528         return nfc_common_convert_error_code(__func__, ret);
529         /* LCOV_EXCL_STOP */
530 }
531
532 int nfc_ndef_message_destroy(nfc_ndef_message_h ndef_message)
533 {
534         int ret;
535
536         LOG_BEGIN();
537
538         CHECK_SUPPORTED(NFC_FEATURE);
539
540         /* LCOV_EXCL_START */
541         CHECK_INIT();
542         CHECK_INVALID(ndef_message == NULL);
543
544         ret = net_nfc_free_ndef_message(ndef_message);
545
546         return nfc_common_convert_error_code(__func__, ret);
547         /* LCOV_EXCL_STOP */
548 }
549
550 int nfc_ndef_message_get_record_count(nfc_ndef_message_h ndef_message,
551         int *count)
552 {
553         int ret;
554
555         LOG_BEGIN();
556
557         CHECK_SUPPORTED(NFC_FEATURE);
558
559         /* LCOV_EXCL_START */
560         CHECK_INIT();
561         CHECK_INVALID(ndef_message == NULL);
562         CHECK_INVALID(count == NULL);
563
564         ret = net_nfc_get_ndef_message_record_count(ndef_message, count);
565
566         return nfc_common_convert_error_code(__func__, ret);
567         /* LCOV_EXCL_STOP */
568 }
569
570 int nfc_ndef_message_get_rawdata(nfc_ndef_message_h ndef_message,
571         unsigned char **rawdata,
572         unsigned int *rawdata_size)
573 {
574         int ret;
575         data_h rawdata_data;
576
577         LOG_BEGIN();
578
579         CHECK_SUPPORTED(NFC_FEATURE);
580
581         /* LCOV_EXCL_START */
582         CHECK_INIT();
583         CHECK_INVALID(ndef_message == NULL);
584         CHECK_INVALID(rawdata == NULL);
585         CHECK_INVALID(rawdata_size == NULL);
586
587         *rawdata = NULL;
588         *rawdata_size = 0;
589
590         ret = net_nfc_create_rawdata_from_ndef_message(ndef_message,
591                 &rawdata_data);
592         if (ret == NET_NFC_OK) {
593                 uint8_t *buffer;
594                 uint32_t length;
595
596                 buffer = net_nfc_get_data_buffer(rawdata_data);
597                 length = net_nfc_get_data_length(rawdata_data);
598
599                 *rawdata = calloc(1, length);
600                 if (*rawdata != NULL) {
601                         memcpy(*rawdata, buffer, length);
602                         *rawdata_size = length;
603                 } else {
604                         ret = NET_NFC_ALLOC_FAIL;
605                 }
606
607                 net_nfc_free_data(rawdata_data);
608         }
609
610         return nfc_common_convert_error_code(__func__, ret);
611         /* LCOV_EXCL_STOP */
612 }
613
614 int nfc_ndef_message_append_record(nfc_ndef_message_h ndef_message,
615         nfc_ndef_record_h record)
616 {
617         int ret;
618
619         LOG_BEGIN();
620
621         CHECK_SUPPORTED(NFC_FEATURE);
622
623         /* LCOV_EXCL_START */
624         CHECK_INIT();
625         CHECK_INVALID(ndef_message == NULL);
626         CHECK_INVALID(record == NULL);
627
628         ret = net_nfc_append_record_to_ndef_message(ndef_message, record);
629
630         return nfc_common_convert_error_code(__func__, ret);
631         /* LCOV_EXCL_STOP */
632 }
633
634 int nfc_ndef_message_insert_record(nfc_ndef_message_h ndef_message,
635         int index,
636         nfc_ndef_record_h record)
637 {
638         int ret;
639
640         LOG_BEGIN();
641
642         CHECK_SUPPORTED(NFC_FEATURE);
643
644         /* LCOV_EXCL_START */
645         CHECK_INIT();
646         CHECK_INVALID(ndef_message == NULL);
647         CHECK_INVALID(record == NULL);
648
649         ret = net_nfc_append_record_by_index(ndef_message, index, record);
650
651         return nfc_common_convert_error_code(__func__, ret);
652         /* LCOV_EXCL_STOP */
653 }
654
655 int nfc_ndef_message_remove_record(nfc_ndef_message_h ndef_message,
656         int index)
657 {
658         int ret;
659
660         LOG_BEGIN();
661
662         CHECK_SUPPORTED(NFC_FEATURE);
663
664         /* LCOV_EXCL_START */
665         CHECK_INIT();
666         CHECK_INVALID(ndef_message == NULL);
667
668         ret = net_nfc_remove_record_by_index(ndef_message, index);
669
670         return nfc_common_convert_error_code(__func__, ret);
671         /* LCOV_EXCL_STOP */
672 }
673
674 int nfc_ndef_message_get_record(nfc_ndef_message_h ndef_message,
675         int index,
676         nfc_ndef_record_h *record)
677 {
678         int ret;
679
680         LOG_BEGIN();
681
682         CHECK_SUPPORTED(NFC_FEATURE);
683
684         /* LCOV_EXCL_START */
685         CHECK_INIT();
686         CHECK_INVALID(ndef_message == NULL);
687         CHECK_INVALID(record == NULL);
688
689         ret = net_nfc_get_record_by_index(ndef_message,
690                 index,
691                 (ndef_record_h*)record);
692
693         return nfc_common_convert_error_code(__func__, ret);
694         /* LCOV_EXCL_STOP */
695 }
696