Beta merge 2
[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()
34 {
35         handle = NULL;
36 }
37
38 NdefRecord::NdefRecord(const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload)
39 {
40         LogDebug("entered");
41
42         NFCUtil Util;
43
44         unsigned char * recordPayload = Util.toCharPtr(payload);
45         unsigned char * typeName = Util.toCharPtr(ndefRecordProperties.typeName);
46         unsigned char * id = Util.toCharPtr(ndefRecordProperties.id);
47
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()) ;
51         
52         free(recordPayload);
53         free(typeName);
54         free(id);
55
56         if (result != NFC_ERROR_NONE) {
57                 handle = NULL;
58                 ThrowMsg(PlatformException, "Can't create Ndef Record");
59         }
60 }
61
62 NdefRecord::NdefRecord(const std::string &text, const std::string &langCode, const short encodeType)
63 {
64         LogDebug("entered");
65
66         if (nfc_ndef_record_create_text(&handle, text.c_str(), langCode.c_str(), _convertToEncodeType(static_cast<nfcTextEncodeUTF>(encodeType))) != NFC_ERROR_NONE) {
67                 handle = NULL;
68                 ThrowMsg(PlatformException, "Can't create Ndef Text Record");
69         }       
70 }
71
72 NdefRecord::NdefRecord(const std::string &uri)
73 {
74         LogDebug("entered");
75
76         if (nfc_ndef_record_create_uri(&handle, uri.c_str()) != NFC_ERROR_NONE) {
77                 handle = NULL;
78                 ThrowMsg(PlatformException, "Can't create Ndef Uri Record");
79         }
80 }
81
82 NdefRecord::NdefRecord(const std::string &mimeType, const std::vector<unsigned char> data)
83 {
84         LogDebug("entered");
85
86         NFCUtil Util;
87
88         if (nfc_ndef_record_create_mime(&handle, mimeType.c_str(), Util.toCharPtr(data),static_cast<int>(data.size())) != NFC_ERROR_NONE) {
89                 handle = NULL;
90                 ThrowMsg(PlatformException, "Can't create Ndef Mime Record");
91         }
92 }
93
94 NdefRecord::~NdefRecord()
95 {
96         LogDebug("entered");
97         if (handle != NULL)
98                 nfc_ndef_record_destroy(handle);
99 }
100
101 nfc_encode_type_e NdefRecord::_convertToEncodeType(const nfcTextEncodeUTF type) {
102         switch(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;
107         }
108 }
109
110 nfcTextEncodeUTF NdefRecord::_convertToNfcEncodeType(const nfc_encode_type_e type) {
111         switch(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;
116         }
117 }
118
119 void *NdefRecord::getHandle() {
120         return (void *)handle;
121 }
122
123 NdefRecordProperties NdefRecord::getNDEFRecordProperties() {
124         LogDebug("entered");
125         NdefRecordProperties props;
126         NFCUtil Util;
127
128         props.tnf = getTNF();
129         LogDebug("tnf : " << props.tnf);
130         props.typeName = getTypeName();
131         props.id = getID();
132         LogDebug("typeName : " << Util.byteToString(props.typeName));
133
134         return props;
135 }
136
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");
141
142         NFCUtil Util;
143         return Util.convertTonfcTNF(static_cast<unsigned short>(tnf));
144 }
145
146 std::vector<unsigned char> NdefRecord::getTypeName() {
147         unsigned char *typeName;
148         int typeSize;
149
150         if (nfc_ndef_record_get_type(handle, &typeName, &typeSize) != NFC_ERROR_NONE)
151                 ThrowMsg(PlatformException, "Can't get record's type");
152
153         NFCUtil Util;
154         return Util.toVector(typeName, typeSize);
155 }
156
157 std::vector<unsigned char> NdefRecord::getID() {
158         unsigned char *id;
159         int idSize;
160
161         if (nfc_ndef_record_get_id(handle, &id, &idSize) != NFC_ERROR_NONE)
162                 ThrowMsg(PlatformException, "Can't get record's id");
163
164         NFCUtil Util;
165         return Util.toVector(id, idSize);
166 }
167
168 bool NdefRecord::getText(char **text) {
169         LogDebug("entered");
170
171         int result = nfc_ndef_record_get_text(handle, text);
172         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
173                 return false;
174         if (result != NFC_ERROR_NONE)
175                 ThrowMsg(PlatformException, "Can't get text of record");
176
177         LogDebug("text : " << *text);
178         return true;
179 }
180
181 bool NdefRecord::getLangCode(char **landCode) {
182         LogDebug("entered");
183
184         int result = nfc_ndef_record_get_langcode(handle, landCode);
185         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
186                 return false;
187         if (result != NFC_ERROR_NONE)
188                 ThrowMsg(PlatformException, "Can't get langcode of record");
189
190         LogDebug("landCode : " << *landCode);
191         return true;
192 }
193 bool NdefRecord::getEncodeType(nfcTextEncodeUTF *encodeType) {
194         LogDebug("entered");
195
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)
199                 return false;
200         if (result != NFC_ERROR_NONE)
201                 ThrowMsg(PlatformException, "Can't get encode type of record");
202
203         *encodeType = _convertToNfcEncodeType(type);
204         LogDebug("encodeType : " << *encodeType);
205         return true;
206 }
207
208 bool NdefRecord::getUri(char **uri) {
209         LogDebug("entered");
210         int result = nfc_ndef_record_get_uri(handle, uri);
211         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
212                 return false;
213         if (result != NFC_ERROR_NONE)
214                 ThrowMsg(PlatformException, "Can't get uri of record");
215
216         LogDebug("uri : " << *uri);
217         return true;
218 }
219
220 bool NdefRecord::getMimeType(char **mimeType) {
221         LogDebug("entered");
222         int result = nfc_ndef_record_get_mime_type(handle, mimeType);
223         if (result == NFC_ERROR_INVALID_RECORD_TYPE)
224                 return false;
225         if (result != NFC_ERROR_NONE)
226                 ThrowMsg(PlatformException, "Can't get text of record");
227
228         LogDebug("mimeType : " << *mimeType);
229         return true;
230 }
231 std::vector<unsigned char> NdefRecord::getPayload() {
232         LogDebug("entered");
233         int size;
234         unsigned char *recordbuffer;
235
236         if (nfc_ndef_record_get_payload(handle, &recordbuffer, &size) != NFC_ERROR_NONE)
237                 ThrowMsg(PlatformException, "Can't get record's payload");
238
239         NFCUtil Util;
240         LogDebug("payload : " << Util.byteToString(recordbuffer, size));
241         return Util.toVector(recordbuffer, size);
242 }
243
244 }
245 }
246 }