2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "nfc_common.h"
20 * @brief RTD(Record type definition) Type - Smart Poster type.
21 * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
23 const unsigned char NFC_RECORD_SMART_POSTER_TYPE[2] = { 'S', 'p' };
26 * @brief RTD(Record type definition) Type - Text type.
27 * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
29 const unsigned char NFC_RECORD_TEXT_TYPE[1] = { 'T' };
31 * @brief RTD(Record type definition) Type - URI type.
32 * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
34 const unsigned char NFC_RECORD_URI_TYPE[1] = { 'U' };
36 * @brief RTD(Record type definition) Type - Alternative Carrier type.
37 * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
39 const unsigned char NFC_RECORD_ALTERNATIVE_CARRIER_TYPE[2] = { 'a', 'c' };
41 * @brief RTD(Record type definition) Type - Handover Carrier type.
42 * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
44 const unsigned char NFC_RECORD_HANDOVER_CARRIER_TYPE[2] = { 'H', 'c' };
46 * @brief RTD(Record type definition) Type - Handover Request type.
47 * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
49 const unsigned char NFC_RECORD_HANDOVER_REQUEST_TYPE[2] = { 'H', 'r' };
51 * @brief RTD(Record type definition) Type - Handover Select type.
52 * @ingroup CAPI_NETWORK_NFC_NDEF_RECORD_MODULE
54 const unsigned char NFC_RECORD_HANDOVER_SELECT_TYPE[2] = { 'H', 's' };
56 int nfc_ndef_record_create(nfc_ndef_record_h *record,
58 const unsigned char *type,
60 const unsigned char *id,
62 const unsigned char *payload,
63 unsigned int payload_size)
66 data_h type_data = NULL;
67 data_h id_data = NULL;
68 data_h payload_data = NULL;
72 CHECK_SUPPORTED(NFC_FEATURE);
76 CHECK_INVALID(record == NULL);
78 net_nfc_create_data(&type_data, type, type_size);
80 if (id != NULL && id_size > 0)
81 net_nfc_create_data(&id_data, id, id_size);
83 if (payload != NULL && payload_size > 0)
84 net_nfc_create_data(&payload_data, payload, payload_size);
86 ret = net_nfc_create_record(
87 (ndef_record_h *)record,
93 if (payload_data != NULL)
94 net_nfc_free_data(payload_data);
97 net_nfc_free_data(id_data);
99 net_nfc_free_data(type_data);
101 return nfc_common_convert_error_code(__func__, ret);
106 int nfc_ndef_record_create_text(nfc_ndef_record_h *record,
108 const char *lang_code,
109 nfc_encode_type_e encode)
115 CHECK_SUPPORTED(NFC_FEATURE);
117 /* LCOV_EXCL_START */
119 CHECK_INVALID(record == NULL);
120 CHECK_INVALID(text == NULL);
121 CHECK_INVALID(lang_code == NULL);
123 ret = net_nfc_create_text_type_record(
124 (ndef_record_h *)record,
129 return nfc_common_convert_error_code(__func__, ret);
133 int nfc_ndef_record_create_uri(nfc_ndef_record_h *record,
140 CHECK_SUPPORTED(NFC_FEATURE);
142 /* LCOV_EXCL_START */
144 CHECK_INVALID(record == NULL);
145 CHECK_INVALID(uri == NULL);
147 ret = net_nfc_create_uri_type_record(
148 (ndef_record_h *)record,
150 NET_NFC_SCHEMA_FULL_URI);
152 return nfc_common_convert_error_code(__func__, ret);
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)
165 CHECK_SUPPORTED(NFC_FEATURE);
167 /* LCOV_EXCL_START */
169 CHECK_INVALID(record == NULL);
170 CHECK_INVALID(mime_type == NULL);
171 CHECK_INVALID(data == NULL);
172 CHECK_INVALID(data_size <= 0);
174 ret = nfc_ndef_record_create(record,
175 NFC_RECORD_TNF_MIME_MEDIA,
176 (unsigned char *)mime_type,
183 return nfc_common_convert_error_code(__func__, ret);
187 int nfc_ndef_record_get_mime_type(nfc_ndef_record_h record,
191 nfc_record_tnf_e tnf;
192 unsigned char *typename;
197 CHECK_SUPPORTED(NFC_FEATURE);
199 /* LCOV_EXCL_START */
201 CHECK_INVALID(record == NULL);
202 CHECK_INVALID(mime_type == NULL);
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;
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);
215 ret = NET_NFC_ALLOC_FAIL;
218 return nfc_common_convert_error_code(__func__, ret);
222 int nfc_ndef_record_destroy(nfc_ndef_record_h record)
228 CHECK_SUPPORTED(NFC_FEATURE);
230 /* LCOV_EXCL_START */
232 CHECK_INVALID(record == NULL);
234 ret = net_nfc_free_record(record);
236 return nfc_common_convert_error_code(__func__, ret);
240 int nfc_ndef_record_set_id(nfc_ndef_record_h record,
245 data_h id_data = NULL;
249 CHECK_SUPPORTED(NFC_FEATURE);
251 /* LCOV_EXCL_START */
253 CHECK_INVALID(record == NULL);
254 CHECK_INVALID(id == NULL);
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);
260 net_nfc_free_data(id_data);
262 LOG_ERR("net_nfc_create_data failed, [%d]", ret);
265 return nfc_common_convert_error_code(__func__, ret);
269 int nfc_ndef_record_get_payload(nfc_ndef_record_h record,
270 unsigned char **payload,
278 CHECK_SUPPORTED(NFC_FEATURE);
280 /* LCOV_EXCL_START */
282 CHECK_INVALID(record == NULL);
283 CHECK_INVALID(payload == NULL);
284 CHECK_INVALID(size == NULL);
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);
298 return nfc_common_convert_error_code(__func__, ret);
302 int nfc_ndef_record_get_type(nfc_ndef_record_h record,
303 unsigned char **type,
311 CHECK_SUPPORTED(NFC_FEATURE);
313 /* LCOV_EXCL_START */
315 CHECK_INVALID(record == NULL);
316 CHECK_INVALID(type == NULL);
317 CHECK_INVALID(size == NULL);
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);
331 return nfc_common_convert_error_code(__func__, ret);
335 int nfc_ndef_record_get_id(nfc_ndef_record_h record,
344 CHECK_SUPPORTED(NFC_FEATURE);
346 /* LCOV_EXCL_START */
348 CHECK_INVALID(record == NULL);
349 CHECK_INVALID(id == NULL);
350 CHECK_INVALID(size == NULL);
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);
364 return nfc_common_convert_error_code(__func__, ret);
368 int nfc_ndef_record_get_tnf(nfc_ndef_record_h record, nfc_record_tnf_e *tnf)
374 CHECK_SUPPORTED(NFC_FEATURE);
376 /* LCOV_EXCL_START */
378 CHECK_INVALID(record == NULL);
379 CHECK_INVALID(tnf == NULL);
381 ret = net_nfc_get_record_tnf(record, (net_nfc_record_tnf_e *)tnf);
383 return nfc_common_convert_error_code(__func__, ret);
387 int nfc_ndef_record_get_text(nfc_ndef_record_h record, char **buffer)
390 unsigned char* record_type = NULL;
395 CHECK_SUPPORTED(NFC_FEATURE);
397 /* LCOV_EXCL_START */
399 CHECK_INVALID(record == NULL);
400 CHECK_INVALID(buffer == NULL);
402 ret = nfc_ndef_record_get_type(record, &record_type, &type_size);
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);
409 LOG_ERR("record type is not T");
410 return NFC_ERROR_INVALID_RECORD_TYPE;
413 return nfc_common_convert_error_code(__func__, ret);
417 int nfc_ndef_record_get_langcode(nfc_ndef_record_h record, char **lang_code)
423 CHECK_SUPPORTED(NFC_FEATURE);
425 /* LCOV_EXCL_START */
427 CHECK_INVALID(record == NULL);
428 CHECK_INVALID(lang_code == NULL);
430 ret = net_nfc_get_languange_code_string_from_text_record(record, lang_code);
432 return nfc_common_convert_error_code(__func__, ret);
436 int nfc_ndef_record_get_encode_type(
437 nfc_ndef_record_h record,
438 nfc_encode_type_e *encode)
444 CHECK_SUPPORTED(NFC_FEATURE);
446 /* LCOV_EXCL_START */
448 CHECK_INVALID(record == NULL);
449 CHECK_INVALID(encode == NULL);
451 ret = net_nfc_get_encoding_type_from_text_record(
453 (net_nfc_encode_type_e *)encode);
455 /* if( ret == NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE)
456 LOGE("%s reord type is not text type");*/
458 return nfc_common_convert_error_code(__func__, ret);
462 int nfc_ndef_record_get_uri(nfc_ndef_record_h record, char **uri)
468 CHECK_SUPPORTED(NFC_FEATURE);
470 /* LCOV_EXCL_START */
472 CHECK_INVALID(record == NULL);
473 CHECK_INVALID(uri == NULL);
475 ret = net_nfc_create_uri_string_from_uri_record(record, uri);
477 return nfc_common_convert_error_code(__func__, ret);
481 int nfc_ndef_message_create(nfc_ndef_message_h *ndef_message)
487 CHECK_SUPPORTED(NFC_FEATURE);
489 /* LCOV_EXCL_START */
491 CHECK_INVALID(ndef_message == NULL);
493 ret = net_nfc_create_ndef_message(ndef_message);
495 return nfc_common_convert_error_code(__func__, ret);
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)
505 data_h rawdata_data = NULL;
509 CHECK_SUPPORTED(NFC_FEATURE);
511 /* LCOV_EXCL_START */
513 CHECK_INVALID(ndef_message == NULL);
514 CHECK_INVALID(rawdata == NULL);
515 CHECK_INVALID(rawdata_size <= 0);
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,
523 net_nfc_free_data(rawdata_data);
525 LOG_ERR("net_nfc_create_data failed, [%d]", ret);
528 return nfc_common_convert_error_code(__func__, ret);
532 int nfc_ndef_message_destroy(nfc_ndef_message_h ndef_message)
538 CHECK_SUPPORTED(NFC_FEATURE);
540 /* LCOV_EXCL_START */
542 CHECK_INVALID(ndef_message == NULL);
544 ret = net_nfc_free_ndef_message(ndef_message);
546 return nfc_common_convert_error_code(__func__, ret);
550 int nfc_ndef_message_get_record_count(nfc_ndef_message_h ndef_message,
557 CHECK_SUPPORTED(NFC_FEATURE);
559 /* LCOV_EXCL_START */
561 CHECK_INVALID(ndef_message == NULL);
562 CHECK_INVALID(count == NULL);
564 ret = net_nfc_get_ndef_message_record_count(ndef_message, count);
566 return nfc_common_convert_error_code(__func__, ret);
570 int nfc_ndef_message_get_rawdata(nfc_ndef_message_h ndef_message,
571 unsigned char **rawdata,
572 unsigned int *rawdata_size)
579 CHECK_SUPPORTED(NFC_FEATURE);
581 /* LCOV_EXCL_START */
583 CHECK_INVALID(ndef_message == NULL);
584 CHECK_INVALID(rawdata == NULL);
585 CHECK_INVALID(rawdata_size == NULL);
590 ret = net_nfc_create_rawdata_from_ndef_message(ndef_message,
592 if (ret == NET_NFC_OK) {
596 buffer = net_nfc_get_data_buffer(rawdata_data);
597 length = net_nfc_get_data_length(rawdata_data);
599 *rawdata = calloc(1, length);
600 if (*rawdata != NULL) {
601 memcpy(*rawdata, buffer, length);
602 *rawdata_size = length;
604 ret = NET_NFC_ALLOC_FAIL;
607 net_nfc_free_data(rawdata_data);
610 return nfc_common_convert_error_code(__func__, ret);
614 int nfc_ndef_message_append_record(nfc_ndef_message_h ndef_message,
615 nfc_ndef_record_h record)
621 CHECK_SUPPORTED(NFC_FEATURE);
623 /* LCOV_EXCL_START */
625 CHECK_INVALID(ndef_message == NULL);
626 CHECK_INVALID(record == NULL);
628 ret = net_nfc_append_record_to_ndef_message(ndef_message, record);
630 return nfc_common_convert_error_code(__func__, ret);
634 int nfc_ndef_message_insert_record(nfc_ndef_message_h ndef_message,
636 nfc_ndef_record_h record)
642 CHECK_SUPPORTED(NFC_FEATURE);
644 /* LCOV_EXCL_START */
646 CHECK_INVALID(ndef_message == NULL);
647 CHECK_INVALID(record == NULL);
649 ret = net_nfc_append_record_by_index(ndef_message, index, record);
651 return nfc_common_convert_error_code(__func__, ret);
655 int nfc_ndef_message_remove_record(nfc_ndef_message_h ndef_message,
662 CHECK_SUPPORTED(NFC_FEATURE);
664 /* LCOV_EXCL_START */
666 CHECK_INVALID(ndef_message == NULL);
668 ret = net_nfc_remove_record_by_index(ndef_message, index);
670 return nfc_common_convert_error_code(__func__, ret);
674 int nfc_ndef_message_get_record(nfc_ndef_message_h ndef_message,
676 nfc_ndef_record_h *record)
682 CHECK_SUPPORTED(NFC_FEATURE);
684 /* LCOV_EXCL_START */
686 CHECK_INVALID(ndef_message == NULL);
687 CHECK_INVALID(record == NULL);
689 ret = net_nfc_get_record_by_index(ndef_message,
691 (ndef_record_h*)record);
693 return nfc_common_convert_error_code(__func__, ret);