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(const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload)
39 unsigned char * recordPayload = Util.toCharPtr(payload);
40 unsigned char * typeName = Util.toCharPtr(ndefRecordProperties.typeName);
41 unsigned char * id = Util.toCharPtr(ndefRecordProperties.id);
43 LogDebug("typeName : " << Util.byteToString(typeName, ndefRecordProperties.typeName.size()));
44 int result = nfc_ndef_record_create(&handle, static_cast<nfc_record_tnf_e>(Util.convertToTNF(ndefRecordProperties.tnf)), typeName, ndefRecordProperties.typeName.size(),
45 id, ndefRecordProperties.id.size(), recordPayload, payload.size()) ;
51 if (result != NFC_ERROR_NONE) {
53 ThrowMsg(PlatformException, "Can't create Ndef Record");
57 NdefRecord::NdefRecord(void *recordhandle)
61 handle = (nfc_ndef_record_h)recordhandle;
63 if (recordhandle == NULL)
64 ThrowMsg(PlatformException, "Record Handler is Null Pointer.");
67 NdefRecord::NdefRecord(const std::string &text, const std::string &langCode, const short encodeType)
71 if (nfc_ndef_record_create_text(&handle, text.c_str(), langCode.c_str(), _convertToEncodeType(static_cast<nfcTextEncodeUTF>(encodeType))) != NFC_ERROR_NONE) {
73 ThrowMsg(PlatformException, "Can't create Ndef Text Record");
77 NdefRecord::NdefRecord(const std::string &uri)
81 if (nfc_ndef_record_create_uri(&handle, uri.c_str()) != NFC_ERROR_NONE) {
83 ThrowMsg(PlatformException, "Can't create Ndef Uri Record");
87 NdefRecord::NdefRecord(const std::string &mimeType, const std::vector<unsigned char> data)
93 if (nfc_ndef_record_create_mime(&handle, mimeType.c_str(), Util.toCharPtr(data),static_cast<int>(data.size())) != NFC_ERROR_NONE) {
95 ThrowMsg(PlatformException, "Can't create Ndef Mime Record");
99 NdefRecord::~NdefRecord()
103 nfc_ndef_record_destroy(handle);
106 nfc_encode_type_e NdefRecord::_convertToEncodeType(const nfcTextEncodeUTF type) {
108 case NFC_TEXT_ENCODE_UTF_8:
109 return NFC_ENCODE_UTF_8;
110 case NFC_TEXT_ENCODE_UTF_16:
111 return NFC_ENCODE_UTF_16;
115 nfcTextEncodeUTF NdefRecord::_convertToNfcEncodeType(const nfc_encode_type_e type) {
117 case NFC_ENCODE_UTF_8:
118 return NFC_TEXT_ENCODE_UTF_8;
119 case NFC_ENCODE_UTF_16:
120 return NFC_TEXT_ENCODE_UTF_16;
124 void *NdefRecord::getHandle() {
125 return (void *)handle;
128 NdefRecordProperties NdefRecord::getNDEFRecordProperties() {
130 NdefRecordProperties props;
131 nfc_record_tnf_e tnf;
132 unsigned char *typeName, *id;
133 int typeSize, idSize;
136 if (nfc_ndef_record_get_tnf(handle, &tnf) != NFC_ERROR_NONE)
137 ThrowMsg(PlatformException, "Can't get record's tnf");
139 if (nfc_ndef_record_get_type(handle, &typeName, &typeSize) != NFC_ERROR_NONE)
140 ThrowMsg(PlatformException, "Can't get record's type");
142 if (nfc_ndef_record_get_id(handle, &id, &idSize) != NFC_ERROR_NONE)
143 ThrowMsg(PlatformException, "Can't get record's id");
145 props.tnf = Util.convertTonfcTNF(static_cast<unsigned short>(tnf));
146 LogDebug("tnf : " << props.tnf);
147 props.typeName = Util.toVector(typeName, typeSize);
148 props.id = Util.toVector(id, idSize);
149 LogDebug("typeName : " << Util.byteToString(props.typeName));
154 bool NdefRecord::getText(char **text) {
157 int result = nfc_ndef_record_get_text(handle, text);
158 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
160 if (result != NFC_ERROR_NONE)
161 ThrowMsg(PlatformException, "Can't get text of record");
163 LogDebug("text : " << *text);
167 bool NdefRecord::getLangCode(char **landCode) {
170 int result = nfc_ndef_record_get_langcode(handle, landCode);
171 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
173 if (result != NFC_ERROR_NONE)
174 ThrowMsg(PlatformException, "Can't get langcode of record");
176 LogDebug("landCode : " << *landCode);
179 bool NdefRecord::getEncodeType(nfcTextEncodeUTF *encodeType) {
182 nfc_encode_type_e type;
183 int result = nfc_ndef_record_get_encode_type(handle, &type);
184 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
186 if (result != NFC_ERROR_NONE)
187 ThrowMsg(PlatformException, "Can't get encode type of record");
189 *encodeType = _convertToNfcEncodeType(type);
190 LogDebug("encodeType : " << *encodeType);
194 bool NdefRecord::getUri(char **uri) {
196 int result = nfc_ndef_record_get_uri(handle, uri);
197 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
199 if (result != NFC_ERROR_NONE)
200 ThrowMsg(PlatformException, "Can't get uri of record");
202 LogDebug("uri : " << *uri);
206 bool NdefRecord::getMimeType(char **mimeType) {
208 int result = nfc_ndef_record_get_mime_type(handle, mimeType);
209 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
211 if (result != NFC_ERROR_NONE)
212 ThrowMsg(PlatformException, "Can't get text of record");
214 LogDebug("mimeType : " << *mimeType);
217 std::vector<unsigned char> NdefRecord::getPayload() {
220 unsigned char *recordbuffer;
222 if (nfc_ndef_record_get_payload(handle, &recordbuffer, &size) != NFC_ERROR_NONE)
223 ThrowMsg(PlatformException, "Can't get record's payload");
226 LogDebug("payload : " << Util.byteToString(recordbuffer, size));
227 return Util.toVector(recordbuffer, size);