2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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.
20 #include <dpl/log/log.h>
21 #include <Commons/Exception.h>
23 #include "NdefRecord.h"
30 using namespace Api::NFC;
31 using namespace WrtDeviceApis::Commons;
33 NdefRecord::NdefRecord()
38 NdefRecord::NdefRecord(const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload)
44 unsigned char * recordPayload = Util.toCharPtr(payload);
45 unsigned char * typeName = Util.toCharPtr(ndefRecordProperties.typeName);
46 unsigned char * id = Util.toCharPtr(ndefRecordProperties.id);
48 LogDebug("typeName : " << Util.byteToString(typeName, ndefRecordProperties.typeName.size()));
49 int result = nfc_ndef_record_create(&handle, static_cast<nfc_record_tnf_e>(Util.convertToTNF(ndefRecordProperties.tnf)), typeName, ndefRecordProperties.typeName.size(),
50 id, ndefRecordProperties.id.size(), recordPayload, payload.size()) ;
56 if (result != NFC_ERROR_NONE) {
58 ThrowMsg(PlatformException, "Can't create Ndef Record");
62 NdefRecord::NdefRecord(const std::string &text, const std::string &langCode, const short encodeType)
66 if (nfc_ndef_record_create_text(&handle, text.c_str(), langCode.c_str(), _convertToEncodeType(static_cast<nfcTextEncodeUTF>(encodeType))) != NFC_ERROR_NONE) {
68 ThrowMsg(PlatformException, "Can't create Ndef Text Record");
72 NdefRecord::NdefRecord(const std::string &uri)
76 if (nfc_ndef_record_create_uri(&handle, uri.c_str()) != NFC_ERROR_NONE) {
78 ThrowMsg(PlatformException, "Can't create Ndef Uri Record");
82 NdefRecord::NdefRecord(const std::string &mimeType, const std::vector<unsigned char> data)
88 if (nfc_ndef_record_create_mime(&handle, mimeType.c_str(), Util.toCharPtr(data),static_cast<int>(data.size())) != NFC_ERROR_NONE) {
90 ThrowMsg(PlatformException, "Can't create Ndef Mime Record");
94 NdefRecord::~NdefRecord()
98 nfc_ndef_record_destroy(handle);
101 nfc_encode_type_e NdefRecord::_convertToEncodeType(const nfcTextEncodeUTF type) {
103 case NFC_TEXT_ENCODE_UTF_8:
104 return NFC_ENCODE_UTF_8;
105 case NFC_TEXT_ENCODE_UTF_16:
106 return NFC_ENCODE_UTF_16;
110 nfcTextEncodeUTF NdefRecord::_convertToNfcEncodeType(const nfc_encode_type_e type) {
112 case NFC_ENCODE_UTF_8:
113 return NFC_TEXT_ENCODE_UTF_8;
114 case NFC_ENCODE_UTF_16:
115 return NFC_TEXT_ENCODE_UTF_16;
119 void *NdefRecord::getHandle() {
120 return (void *)handle;
123 NdefRecordProperties NdefRecord::getNDEFRecordProperties() {
125 NdefRecordProperties props;
128 props.tnf = getTNF();
129 LogDebug("tnf : " << props.tnf);
130 props.typeName = getTypeName();
132 LogDebug("typeName : " << Util.byteToString(props.typeName));
137 nfcTNF NdefRecord::getTNF() {
138 nfc_record_tnf_e tnf;
139 if (nfc_ndef_record_get_tnf(handle, &tnf) != NFC_ERROR_NONE)
140 ThrowMsg(PlatformException, "Can't get record's tnf");
143 return Util.convertTonfcTNF(static_cast<unsigned short>(tnf));
146 std::vector<unsigned char> NdefRecord::getTypeName() {
147 unsigned char *typeName;
150 if (nfc_ndef_record_get_type(handle, &typeName, &typeSize) != NFC_ERROR_NONE)
151 ThrowMsg(PlatformException, "Can't get record's type");
154 return Util.toVector(typeName, typeSize);
157 std::vector<unsigned char> NdefRecord::getID() {
161 if (nfc_ndef_record_get_id(handle, &id, &idSize) != NFC_ERROR_NONE)
162 ThrowMsg(PlatformException, "Can't get record's id");
165 return Util.toVector(id, idSize);
168 bool NdefRecord::getText(char **text) {
171 int result = nfc_ndef_record_get_text(handle, text);
172 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
174 if (result != NFC_ERROR_NONE)
175 ThrowMsg(PlatformException, "Can't get text of record");
177 LogDebug("text : " << *text);
181 bool NdefRecord::getLangCode(char **landCode) {
184 int result = nfc_ndef_record_get_langcode(handle, landCode);
185 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
187 if (result != NFC_ERROR_NONE)
188 ThrowMsg(PlatformException, "Can't get langcode of record");
190 LogDebug("landCode : " << *landCode);
193 bool NdefRecord::getEncodeType(nfcTextEncodeUTF *encodeType) {
196 nfc_encode_type_e type;
197 int result = nfc_ndef_record_get_encode_type(handle, &type);
198 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
200 if (result != NFC_ERROR_NONE)
201 ThrowMsg(PlatformException, "Can't get encode type of record");
203 *encodeType = _convertToNfcEncodeType(type);
204 LogDebug("encodeType : " << *encodeType);
208 bool NdefRecord::getUri(char **uri) {
210 int result = nfc_ndef_record_get_uri(handle, uri);
211 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
213 if (result != NFC_ERROR_NONE)
214 ThrowMsg(PlatformException, "Can't get uri of record");
216 LogDebug("uri : " << *uri);
220 bool NdefRecord::getMimeType(char **mimeType) {
222 int result = nfc_ndef_record_get_mime_type(handle, mimeType);
223 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
225 if (result != NFC_ERROR_NONE)
226 ThrowMsg(PlatformException, "Can't get text of record");
228 LogDebug("mimeType : " << *mimeType);
231 std::vector<unsigned char> NdefRecord::getPayload() {
234 unsigned char *recordbuffer;
236 if (nfc_ndef_record_get_payload(handle, &recordbuffer, &size) != NFC_ERROR_NONE)
237 ThrowMsg(PlatformException, "Can't get record's payload");
240 LogDebug("payload : " << Util.byteToString(recordbuffer, size));
241 return Util.toVector(recordbuffer, size);