Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / NFC / NdefMessage.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 #include <dpl/log/log.h>
19
20 #include <Commons/Exception.h>
21
22 #include "NdefMessage.h"
23 #include "NFCUtil.h"
24
25 namespace TizenApis {
26 namespace Platform {
27 namespace NFC {
28
29 using namespace Api::NFC;
30 using namespace WrtDeviceApis::Commons;
31
32 NdefMessage::NdefMessage()
33 {
34         LogDebug("entered");
35         if (nfc_ndef_message_create(&handle) != NFC_ERROR_NONE) {
36                 handle = NULL;
37                 ThrowMsg(PlatformException, "Can't create Ndef Message");
38         }
39 }
40
41 NdefMessage::NdefMessage(void *messageHandle)
42 {
43         LogDebug("entered");
44
45         if (messageHandle == NULL) {
46                 handle = NULL;
47                 ThrowMsg(PlatformException, "Message Handler is Null Pointer.");
48         }
49
50         handle = static_cast<nfc_ndef_message_h>(messageHandle);
51 }
52
53 NdefMessage::NdefMessage(const std::vector<void *> ndefRcords)
54 {
55         LogDebug("entered");
56
57         _makeMessage(ndefRcords);
58 }
59
60 NdefMessage::NdefMessage(const std::vector<unsigned char> rawdata)
61 {
62         LogDebug("entered");
63
64         NFCUtil Util;
65         unsigned char *Urawdata = Util.toCharPtr(rawdata);
66         if (nfc_ndef_message_create_from_rawdata(&handle, Urawdata, rawdata.size()) != NFC_ERROR_NONE) {
67                 handle = NULL;
68                 free(Urawdata);
69                 ThrowMsg(PlatformException, "Can't create Ndef Message");
70         }
71
72         free(Urawdata);
73 }
74
75 NdefMessage::~NdefMessage()
76 {
77         LogDebug("entered");
78         if (handle != NULL)
79                 nfc_ndef_message_destroy(handle);
80 }
81
82 void NdefMessage::_makeMessage(const std::vector<void *> ndefRcords) {
83         if (nfc_ndef_message_create(&handle) != NFC_ERROR_NONE) {
84                 handle = NULL;
85                 ThrowMsg(PlatformException, "Can't create Ndef Message");
86         }
87
88         for (int i = 0 ; i < static_cast<int>(ndefRcords.size()); i++) {
89                 nfc_ndef_record_h insertRecord;
90         
91                 if (!_copyNDEFRecord((nfc_ndef_record_h*)&ndefRcords[i], &insertRecord)) {
92                         LogDebug("copyNDEFRecord fail!");
93                         ThrowMsg(PlatformException, "Can't copy Record");
94                 }
95
96                 if (nfc_ndef_message_append_record(handle, insertRecord) != NFC_ERROR_NONE) {
97                         LogError(i << " record can't be inserted. " << insertRecord << " : " << handle);
98                         ThrowMsg(PlatformException, "Can't insert record in Ndef Message");
99                 }
100         }
101 }
102
103 bool NdefMessage::_copyNDEFRecord(nfc_ndef_record_h *srcHandle, nfc_ndef_record_h *destHandle) {
104         nfc_record_tnf_e tnf;
105         unsigned char *typeName, *id, *payload;
106         int typeSize, idSize, payloadSize;
107
108         if (nfc_ndef_record_get_tnf(*srcHandle, &tnf) != NFC_ERROR_NONE)
109                 return false;
110
111         if (nfc_ndef_record_get_type(*srcHandle, &typeName, &typeSize) != NFC_ERROR_NONE)
112                 return false;
113
114         if (nfc_ndef_record_get_id(*srcHandle, &id, &idSize) != NFC_ERROR_NONE)
115                 return false;
116
117         if (nfc_ndef_record_get_payload(*srcHandle, &payload, &payloadSize) != NFC_ERROR_NONE)
118                 return false;
119
120         if (nfc_ndef_record_create(destHandle, tnf, typeName, typeSize, id, idSize, payload, payloadSize) != NFC_ERROR_NONE)
121                 return false;
122
123         return true;
124 }
125
126 void *NdefMessage::getHandle() {
127         return (void *)handle;
128 }
129
130 void *NdefMessage::getRecordHandle(const long index) {
131         nfc_ndef_record_h recordHandle;
132         if (nfc_ndef_message_get_record(handle, static_cast<int>(index), &recordHandle) != NFC_ERROR_NONE)
133                 ThrowMsg(PlatformException, "Can't get Ndef Record");   
134         return (void *)recordHandle;
135 }
136
137 long NdefMessage::getRecordCount() {
138         int count;
139         if (nfc_ndef_message_get_record_count(handle, &count) != NFC_ERROR_NONE)
140                 ThrowMsg(PlatformException, "Can't get Record Count");
141
142         LogDebug("record Count : " << count);
143         return static_cast<long>(count);
144 }
145
146 std::vector<unsigned char> NdefMessage::toByte() {
147         unsigned char *UrawData;
148         int size;
149
150         if (nfc_ndef_message_get_rawdata(handle, &UrawData, &size) != NFC_ERROR_NONE)
151                 ThrowMsg(PlatformException, "Can't get serial bytes of NDEF message");
152
153         LogDebug(UrawData);
154         NFCUtil Util;
155         return Util.toVector(UrawData, size);
156 }
157
158 NdefRecordData NdefMessage::getNDEFRecord(const long index) {
159         nfc_ndef_record_h recordHandle;
160         if (nfc_ndef_message_get_record(handle, static_cast<int>(index), &recordHandle) != NFC_ERROR_NONE)
161                 ThrowMsg(PlatformException, "Can't get Ndef Record");
162
163         NdefRecordData recordData;
164
165         nfc_record_tnf_e tnf;
166         unsigned char *typeName, *id, *payload;
167         int typeSize, idSize, payloadSize;
168         NFCUtil Util;
169
170         if (nfc_ndef_record_get_tnf(recordHandle, &tnf) != NFC_ERROR_NONE)
171                 ThrowMsg(PlatformException, "Can't get record's tnf");
172
173         if (nfc_ndef_record_get_type(recordHandle, &typeName, &typeSize) != NFC_ERROR_NONE)
174                 ThrowMsg(PlatformException, "Can't get record's type");
175
176         if (nfc_ndef_record_get_id(recordHandle, &id, &idSize) != NFC_ERROR_NONE)
177                 ThrowMsg(PlatformException, "Can't get record's id");
178
179         if (nfc_ndef_record_get_payload(recordHandle, &payload, &payloadSize) != NFC_ERROR_NONE)
180                 ThrowMsg(PlatformException, "Can't get record's payload");
181
182         LogDebug("tnf : " <<tnf);
183         LogDebug("typeName : " << Util.byteToString(typeName, typeSize));
184         LogDebug("payload : " << Util.byteToString(payload, payloadSize));
185
186         recordData.properties.tnf = Util.convertTonfcTNF(static_cast<unsigned short>(tnf));
187         recordData.properties.typeName = Util.toVector(typeName, typeSize);
188         recordData.properties.id = Util.toVector(id, idSize);
189         recordData.payload = Util.toVector(payload, payloadSize);
190
191         return recordData;
192 }
193
194 bool NdefMessage::insertNDEFRecord(const long index, const void *recordHandle) {
195         nfc_ndef_record_h insertRecord;
196         if (!_copyNDEFRecord((nfc_ndef_record_h *)&recordHandle, &insertRecord)) {
197                 LogDebug("copyNDEFRecord fail!");
198                 ThrowMsg(PlatformException, "Can't copy Record");
199         }
200
201         if (nfc_ndef_message_insert_record(handle, static_cast<int>(index), insertRecord) != NFC_ERROR_NONE)
202                 ThrowMsg(PlatformException, "Can't insert ndef Record");
203
204         return TRUE;
205 }
206
207 bool NdefMessage::appendNDEFRecord(const void *recordHandle) {
208         nfc_ndef_record_h appendRecord;
209         if (!_copyNDEFRecord((nfc_ndef_record_h *)&recordHandle, &appendRecord)) {
210                 LogDebug("copyNDEFRecord fail!");
211                 ThrowMsg(PlatformException, "Can't copy Record");
212         }
213
214         if (nfc_ndef_message_append_record(handle, appendRecord) != NFC_ERROR_NONE)
215                 ThrowMsg(PlatformException, "Can't append ndef Record");
216
217         return TRUE;
218 }
219
220 bool NdefMessage::removeNDEFRecord(const long index) {
221         if (nfc_ndef_message_remove_record(handle, static_cast<int>(index)) != NFC_ERROR_NONE)
222                 ThrowMsg(PlatformException, "Can't remove ndef Record");
223
224         return TRUE;
225 }
226
227 void NdefMessage::changeAllRecords(const std::vector<void *> ndefRcords) {
228         nfc_ndef_message_h newHandle = handle;
229         handle = NULL;
230
231         _makeMessage(ndefRcords);
232         if (newHandle != NULL)
233                 nfc_ndef_message_destroy(newHandle);
234 }
235
236 }
237 }
238 }