084fd520432898e401d430b9c71857c4a81c6e32
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / NFC / NdefRecord.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18
19
20 #include <dpl/log/log.h>
21 #include <Commons/Exception.h>
22
23 #include "NdefRecord.h"
24 #include "NFCUtil.h"
25
26 namespace TizenApis {
27 namespace Platform {
28 namespace NFC {
29
30 using namespace Api::NFC;
31 using namespace WrtDeviceApis::Commons;
32
33 NdefRecord::NdefRecord(const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload)
34 {
35         LogDebug("entered");
36
37         NFCUtil Util;
38
39         unsigned char * recordPayload = Util.toCharPtr(payload);
40         unsigned char * typeName = Util.toCharPtr(ndefRecordProperties.typeName);
41         unsigned char * id = Util.toCharPtr(ndefRecordProperties.id);
42
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()) ;
46         
47         free(recordPayload);
48         free(typeName);
49         free(id);
50
51         if (result != NFC_ERROR_NONE) {
52                 handle = NULL;
53                 ThrowMsg(PlatformException, "Can't create Ndef Record");
54         }
55 }
56
57 NdefRecord::NdefRecord(void *recordhandle)
58 {
59         LogDebug("entered");
60
61         handle = (nfc_ndef_record_h)recordhandle;
62
63         if (recordhandle == NULL)
64                 ThrowMsg(PlatformException, "Record Handler is Null Pointer.");
65 }
66
67 NdefRecord::NdefRecord(const std::string &text, const std::string &langCode, const short encodeType)
68 {
69         LogDebug("entered");
70
71         if (nfc_ndef_record_create_text(&handle, text.c_str(), langCode.c_str(), _convertToEncodeType(static_cast<nfcTextEncodeUTF>(encodeType))) != NFC_ERROR_NONE) {
72                 handle = NULL;
73                 ThrowMsg(PlatformException, "Can't create Ndef Text Record");
74         }       
75 }
76
77 NdefRecord::NdefRecord(const std::string &uri)
78 {
79         LogDebug("entered");
80
81         if (nfc_ndef_record_create_uri(&handle, uri.c_str()) != NFC_ERROR_NONE) {
82                 handle = NULL;
83                 ThrowMsg(PlatformException, "Can't create Ndef Uri Record");
84         }
85 }
86
87 NdefRecord::NdefRecord(const std::string &mimeType, const std::vector<unsigned char> data)
88 {
89         LogDebug("entered");
90
91         NFCUtil Util;
92
93         if (nfc_ndef_record_create_mime(&handle, mimeType.c_str(), Util.toCharPtr(data),static_cast<int>(data.size())) != NFC_ERROR_NONE) {
94                 handle = NULL;
95                 ThrowMsg(PlatformException, "Can't create Ndef Mime Record");
96         }
97 }
98
99 NdefRecord::~NdefRecord()
100 {
101         LogDebug("entered");
102         if (handle != NULL)
103                 nfc_ndef_record_destroy(handle);
104 }
105
106 nfc_encode_type_e NdefRecord::_convertToEncodeType(const nfcTextEncodeUTF type) {
107         switch(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;
112         }
113 }
114
115 nfcTextEncodeUTF NdefRecord::_convertToNfcEncodeType(const nfc_encode_type_e type) {
116         switch(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;
121         }
122 }
123
124 void *NdefRecord::getHandle() {
125         return (void *)handle;
126 }
127
128 NdefRecordProperties NdefRecord::getNDEFRecordProperties() {
129         LogDebug("entered");
130         NdefRecordProperties props;
131         nfc_record_tnf_e tnf;
132         unsigned char *typeName, *id;
133         int typeSize, idSize;
134         NFCUtil Util;
135
136         if (nfc_ndef_record_get_tnf(handle, &tnf) != NFC_ERROR_NONE)
137                 ThrowMsg(PlatformException, "Can't get record's tnf");
138
139         if (nfc_ndef_record_get_type(handle, &typeName, &typeSize) != NFC_ERROR_NONE)
140                 ThrowMsg(PlatformException, "Can't get record's type");
141
142         if (nfc_ndef_record_get_id(handle, &id, &idSize) != NFC_ERROR_NONE)
143                 ThrowMsg(PlatformException, "Can't get record's id");
144
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));
150
151         return props;
152 }
153
154 bool NdefRecord::getText(char **text) {
155         LogDebug("entered");
156
157         int result = nfc_ndef_record_get_text(handle, text);
158         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
159                 return false;
160         if (result != NFC_ERROR_NONE)
161                 ThrowMsg(PlatformException, "Can't get text of record");
162
163         LogDebug("text : " << *text);
164         return true;
165 }
166
167 bool NdefRecord::getLangCode(char **landCode) {
168         LogDebug("entered");
169
170         int result = nfc_ndef_record_get_langcode(handle, landCode);
171         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
172                 return false;
173         if (result != NFC_ERROR_NONE)
174                 ThrowMsg(PlatformException, "Can't get langcode of record");
175
176         LogDebug("landCode : " << *landCode);
177         return true;
178 }
179 bool NdefRecord::getEncodeType(nfcTextEncodeUTF *encodeType) {
180         LogDebug("entered");
181
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)
185                 return false;
186         if (result != NFC_ERROR_NONE)
187                 ThrowMsg(PlatformException, "Can't get encode type of record");
188
189         *encodeType = _convertToNfcEncodeType(type);
190         LogDebug("encodeType : " << *encodeType);
191         return true;
192 }
193
194 bool NdefRecord::getUri(char **uri) {
195         LogDebug("entered");
196         int result = nfc_ndef_record_get_uri(handle, uri);
197         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
198                 return false;
199         if (result != NFC_ERROR_NONE)
200                 ThrowMsg(PlatformException, "Can't get uri of record");
201
202         LogDebug("uri : " << *uri);
203         return true;
204 }
205
206 bool NdefRecord::getMimeType(char **mimeType) {
207         LogDebug("entered");
208         int result = nfc_ndef_record_get_mime_type(handle, mimeType);
209         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
210                 return false;
211         if (result != NFC_ERROR_NONE)
212                 ThrowMsg(PlatformException, "Can't get text of record");
213
214         LogDebug("mimeType : " << *mimeType);
215         return true;
216 }
217 std::vector<unsigned char> NdefRecord::getPayload() {
218         LogDebug("entered");
219         int size;
220         unsigned char *recordbuffer;
221
222         if (nfc_ndef_record_get_payload(handle, &recordbuffer, &size) != NFC_ERROR_NONE)
223                 ThrowMsg(PlatformException, "Can't get record's payload");
224
225         NFCUtil Util;
226         LogDebug("payload : " << Util.byteToString(recordbuffer, size));
227         return Util.toVector(recordbuffer, size);
228 }
229
230 }
231 }
232 }