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 int payloadSize = payload.size();
95 unsigned char * typeName = util.toCharPtr(ndefRecordProperties.typeName);
96 int typeSize = (static_cast<int>(ndefRecordProperties.typeName.size()) > 255) ? 255 : static_cast<int>(ndefRecordProperties.typeName.size());
97 unsigned char * id = util.toCharPtr(ndefRecordProperties.id);
98 int idSize = (static_cast<int>(ndefRecordProperties.id.size()) > 255) ? 255 : static_cast<int>(ndefRecordProperties.id.size());
100 int result = nfc_ndef_record_create(&handle, static_cast<nfc_record_tnf_e>(util.convertToTNF(ndefRecordProperties.tnf)), typeName, typeSize,
101 id, idSize, recordPayload, payloadSize) ;
109 if (result != NFC_ERROR_NONE) {
111 util.throwNFCException(result, "Can't create Ndef Record");
115 NdefRecord::NdefRecord(const std::string &text, const std::string &langCode, const short encodeType)
119 int result = nfc_ndef_record_create_text(&handle, text.c_str(), langCode.c_str(), _convertToEncodeType(static_cast<nfcTextEncodeUTF>(encodeType)));
122 if (result != NFC_ERROR_NONE) {
124 util.throwNFCException(result, "Can't create Ndef Text Record");
128 NdefRecord::NdefRecord(const std::string &uri)
132 int result = nfc_ndef_record_create_uri(&handle, uri.c_str());
135 if (result != NFC_ERROR_NONE) {
137 util.throwNFCException(result, "Can't create Ndef Uri Record");
141 NdefRecord::NdefRecord(const std::string &mimeType, const std::vector<unsigned char> data)
147 unsigned char *mimeData = util.toCharPtr(data);
148 int result = nfc_ndef_record_create_mime(&handle, mimeType.c_str(), mimeData, static_cast<int>(data.size()));
153 if (result != NFC_ERROR_NONE) {
155 util.throwNFCException(result, "Can't create Ndef Mime Record");
159 NdefRecord::~NdefRecord()
163 nfc_ndef_record_destroy(handle);
166 nfc_encode_type_e NdefRecord::_convertToEncodeType(const nfcTextEncodeUTF type) {
168 case NFC_TEXT_ENCODE_UTF_8:
169 return NFC_ENCODE_UTF_8;
170 case NFC_TEXT_ENCODE_UTF_16:
171 return NFC_ENCODE_UTF_16;
175 nfcTextEncodeUTF NdefRecord::_convertToNfcEncodeType(const nfc_encode_type_e type) {
177 case NFC_ENCODE_UTF_8:
178 return NFC_TEXT_ENCODE_UTF_8;
179 case NFC_ENCODE_UTF_16:
180 return NFC_TEXT_ENCODE_UTF_16;
184 void *NdefRecord::getHandle() {
185 return (void *)handle;
188 NdefRecordProperties NdefRecord::getNDEFRecordProperties() {
190 NdefRecordProperties props;
193 props.tnf = getTNF();
194 props.typeName = getTypeName();
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");
249 bool NdefRecord::getLangCode(char **langCode) {
252 int result = nfc_ndef_record_get_langcode(handle, langCode);
253 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
257 if (result != NFC_ERROR_NONE)
258 util.throwNFCException(result, "Can't get langcode of record");
262 bool NdefRecord::getEncodeType(nfcTextEncodeUTF *encodeType) {
265 nfc_encode_type_e type;
266 int result = nfc_ndef_record_get_encode_type(handle, &type);
267 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
271 if (result != NFC_ERROR_NONE)
272 util.throwNFCException(result, "Can't get encode type of record");
274 *encodeType = _convertToNfcEncodeType(type);
278 bool NdefRecord::getUri(char **uri) {
280 int result = nfc_ndef_record_get_uri(handle, uri);
281 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
285 if (result != NFC_ERROR_NONE)
286 util.throwNFCException(result, "Can't get uri of record");
291 bool NdefRecord::getMimeType(char **mimeType) {
293 int result = nfc_ndef_record_get_mime_type(handle, mimeType);
294 if (result == NFC_ERROR_INVALID_RECORD_TYPE)
298 if (result != NFC_ERROR_NONE)
299 util.throwNFCException(result, "Can't get mime type of record");
303 std::vector<unsigned char> NdefRecord::getPayload() {
306 unsigned char *recordbuffer;
307 int result = nfc_ndef_record_get_payload(handle, &recordbuffer, &size);
310 if (result != NFC_ERROR_NONE)
311 util.throwNFCException(result, "Can't get record's payload");
313 return util.toVector(recordbuffer, size);