2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include <Commons/Exception.h>
19 #include "NdefRecord.h"
26 using namespace WrtDeviceApis::Commons;
28 NdefRecord::NdefRecord()
33 NdefRecord::NdefRecord(std::vector<unsigned char> data)
37 nfc_ndef_message_h messageHandle;
39 unsigned char *rawdata = util.toCharPtr(data);
40 int result = nfc_ndef_message_create_from_rawdata(&messageHandle, rawdata, data.size());
42 if (result != NFC_ERROR_NONE) {
44 nfc_ndef_message_destroy(messageHandle);
50 util.throwNFCException(result, "Can't create Ndef Record");
54 result = nfc_ndef_message_get_record_count(messageHandle, &count);
55 if ((result != NFC_ERROR_NONE) || (count != 1)) {
56 nfc_ndef_message_destroy(messageHandle);
61 util.throwNFCException(result, "Can't create Ndef Record");
64 nfc_ndef_record_h recordHandle;
65 result = nfc_ndef_message_get_record(messageHandle, 0, &recordHandle);
66 if (result != NFC_ERROR_NONE) {
67 nfc_ndef_message_destroy(messageHandle);
72 util.throwNFCException(result, "Can't create Ndef Record");
75 if (!util.copyNDEFRecord((void **)&recordHandle, (void **)&handle)) {
76 nfc_ndef_message_destroy(messageHandle);
79 ThrowMsg(UnknownException, "Can't copy Ndef Record");
82 nfc_ndef_message_destroy(messageHandle);
87 NdefRecord::NdefRecord(const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload)
93 unsigned char * recordPayload = util.toCharPtr(payload);
94 unsigned char * typeName = util.toCharPtr(ndefRecordProperties.typeName);
95 unsigned char * id = util.toCharPtr(ndefRecordProperties.id);
97 LoggerD("typeName : " << util.byteToString(typeName, ndefRecordProperties.typeName.size()));
98 int result = nfc_ndef_record_create(&handle, static_cast<nfc_record_tnf_e>(util.convertToTNF(ndefRecordProperties.tnf)), typeName, ndefRecordProperties.typeName.size(),
99 id, ndefRecordProperties.id.size(), recordPayload, payload.size()) ;
107 if (result != NFC_ERROR_NONE) {
109 util.throwNFCException(result, "Can't create Ndef Record");
113 NdefRecord::NdefRecord(const std::string &text, const std::string &langCode, const short encodeType)
117 int result = nfc_ndef_record_create_text(&handle, text.c_str(), langCode.c_str(), _convertToEncodeType(static_cast<nfcTextEncodeUTF>(encodeType)));
120 if (result != NFC_ERROR_NONE) {
122 util.throwNFCException(result, "Can't create Ndef Text Record");
126 NdefRecord::NdefRecord(const std::string &uri)
130 int result = nfc_ndef_record_create_uri(&handle, uri.c_str());
133 if (result != NFC_ERROR_NONE) {
135 util.throwNFCException(result, "Can't create Ndef Uri Record");
139 NdefRecord::NdefRecord(const std::string &mimeType, const std::vector<unsigned char> data)
145 unsigned char *mimeData = util.toCharPtr(data);
146 int result = nfc_ndef_record_create_mime(&handle, mimeType.c_str(), mimeData, static_cast<int>(data.size()));
151 if (result != NFC_ERROR_NONE) {
153 util.throwNFCException(result, "Can't create Ndef Mime Record");
157 NdefRecord::~NdefRecord()
161 nfc_ndef_record_destroy(handle);
164 nfc_encode_type_e NdefRecord::_convertToEncodeType(const nfcTextEncodeUTF type) {
166 case NFC_TEXT_ENCODE_UTF_8:
167 return NFC_ENCODE_UTF_8;
168 case NFC_TEXT_ENCODE_UTF_16:
169 return NFC_ENCODE_UTF_16;
173 nfcTextEncodeUTF NdefRecord::_convertToNfcEncodeType(const nfc_encode_type_e type) {
175 case NFC_ENCODE_UTF_8:
176 return NFC_TEXT_ENCODE_UTF_8;
177 case NFC_ENCODE_UTF_16:
178 return NFC_TEXT_ENCODE_UTF_16;
182 void *NdefRecord::getHandle() {
183 return (void *)handle;
186 NdefRecordProperties NdefRecord::getNDEFRecordProperties() {
188 NdefRecordProperties props;
191 props.tnf = getTNF();
192 LoggerD("tnf : " << props.tnf);
193 props.typeName = getTypeName();
195 LoggerD("typeName : " << util.byteToString(&props.typeName));
200 nfcTNF NdefRecord::getTNF() {
201 nfc_record_tnf_e tnf;
202 int result = nfc_ndef_record_get_tnf(handle, &tnf);
205 if (result != NFC_ERROR_NONE)
206 util.throwNFCException(result, "Can't get record's tnf");
208 return util.convertTonfcTNF(static_cast<unsigned short>(tnf));
211 std::vector<unsigned char> NdefRecord::getTypeName() {
212 unsigned char *typeName;
214 int result = nfc_ndef_record_get_type(handle, &typeName, &typeSize);
217 if (result != NFC_ERROR_NONE)
218 util.throwNFCException(result, "Can't get record's type");
220 return util.toVector(typeName, typeSize);
223 std::vector<unsigned char> NdefRecord::getID() {
226 int result = nfc_ndef_record_get_id(handle, &id, &idSize);
229 if (result != NFC_ERROR_NONE)
230 util.throwNFCException(result, "Can't get record's id");
232 return util.toVector(id, idSize);
235 bool NdefRecord::getText(char **text) {
238 int result = nfc_ndef_record_get_text(handle, text);
239 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
243 if (result != NFC_ERROR_NONE)
244 util.throwNFCException(result, "Can't get text of record");
246 LoggerD("text : " << *text);
250 bool NdefRecord::getLangCode(char **langCode) {
253 int result = nfc_ndef_record_get_langcode(handle, langCode);
254 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
258 if (result != NFC_ERROR_NONE)
259 util.throwNFCException(result, "Can't get langcode of record");
261 LoggerD("langCode : " << *langCode);
264 bool NdefRecord::getEncodeType(nfcTextEncodeUTF *encodeType) {
267 nfc_encode_type_e type;
268 int result = nfc_ndef_record_get_encode_type(handle, &type);
269 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
273 if (result != NFC_ERROR_NONE)
274 util.throwNFCException(result, "Can't get encode type of record");
276 *encodeType = _convertToNfcEncodeType(type);
277 LoggerD("encodeType : " << *encodeType);
281 bool NdefRecord::getUri(char **uri) {
283 int result = nfc_ndef_record_get_uri(handle, uri);
284 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
288 if (result != NFC_ERROR_NONE)
289 util.throwNFCException(result, "Can't get uri of record");
291 LoggerD("uri : " << *uri);
295 bool NdefRecord::getMimeType(char **mimeType) {
297 int result = nfc_ndef_record_get_mime_type(handle, mimeType);
298 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
302 if (result != NFC_ERROR_NONE)
303 util.throwNFCException(result, "Can't get mime type of record");
305 LoggerD("mimeType : " << *mimeType);
308 std::vector<unsigned char> NdefRecord::getPayload() {
311 unsigned char *recordbuffer;
312 int result = nfc_ndef_record_get_payload(handle, &recordbuffer, &size);
315 if (result != NFC_ERROR_NONE)
316 util.throwNFCException(result, "Can't get record's payload");
318 LoggerD("payload : " << util.byteToString(recordbuffer, size));
319 return util.toVector(recordbuffer, size);