f834b783477a9b5af24f97914f3ae5474fd226de
[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         if (nfc_ndef_message_create(&handle) != NFC_ERROR_NONE) {
58                 handle = NULL;
59                 ThrowMsg(PlatformException, "Can't create Ndef Message");
60         }
61
62         for (int i = 0 ; i < static_cast<int>(ndefRcords.size()); i++) {
63                 nfc_ndef_record_h appendRecord;
64                 if (!_copyNDEFRecord((nfc_ndef_record_h*)&ndefRcords[i], &appendRecord)) {
65                         LogDebug("copyNDEFRecord fail!");
66                         ThrowMsg(PlatformException, "Can't copy Record");
67                 }
68
69                 if (nfc_ndef_message_append_record(handle, appendRecord) != NFC_ERROR_NONE)
70                         ThrowMsg(PlatformException, "Can't create Ndef Message");
71         }
72 }
73
74 NdefMessage::NdefMessage(const std::vector<unsigned char> rawdata)
75 {
76         LogDebug("entered");
77
78         NFCUtil Util;
79         unsigned char *Urawdata = Util.toCharPtr(rawdata);
80         if (nfc_ndef_message_create_from_rawdata(&handle, Urawdata, rawdata.size()) != NFC_ERROR_NONE) {
81                 handle = NULL;
82                 free(Urawdata);
83                 ThrowMsg(PlatformException, "Can't create Ndef Message");
84         }
85
86         free(Urawdata);
87 }
88
89 NdefMessage::~NdefMessage()
90 {
91         LogDebug("entered");
92         if (handle != NULL)
93                 nfc_ndef_message_destroy(handle);
94 }
95
96 bool NdefMessage::_copyNDEFRecord(nfc_ndef_record_h *srcHandle, nfc_ndef_record_h *destHandle) {
97         nfc_record_tnf_e tnf;
98         unsigned char *typeName, *id, *payload;
99         int typeSize, idSize, payloadSize;
100
101         if (nfc_ndef_record_get_tnf(*srcHandle, &tnf) != NFC_ERROR_NONE)
102                 return false;
103
104         if (nfc_ndef_record_get_type(*srcHandle, &typeName, &typeSize) != NFC_ERROR_NONE)
105                 return false;
106
107         if (nfc_ndef_record_get_id(*srcHandle, &id, &idSize) != NFC_ERROR_NONE)
108                 return false;
109
110         if (nfc_ndef_record_get_payload(*srcHandle, &payload, &payloadSize) != NFC_ERROR_NONE)
111                 return false;
112
113         if (nfc_ndef_record_create(destHandle, tnf, typeName, typeSize, id, idSize, payload, payloadSize) != NFC_ERROR_NONE)
114                 return false;
115
116         return true;
117 }
118
119 void *NdefMessage::getHandle() {
120         return (void *)handle;
121 }
122
123 long NdefMessage::getRecordCount() {
124         int count;
125         if (nfc_ndef_message_get_record_count(handle, &count) != NFC_ERROR_NONE)
126                 ThrowMsg(PlatformException, "Can't get Record Count");
127
128         LogDebug("record Count : " << count);
129         return static_cast<long>(count);
130 }
131
132 std::vector<unsigned char> NdefMessage::toByte() {
133         unsigned char *UrawData;
134         int size;
135
136         if (nfc_ndef_message_get_rawdata(handle, &UrawData, &size) != NFC_ERROR_NONE)
137                 ThrowMsg(PlatformException, "Can't get serial bytes of NDEF message");
138
139         LogDebug(UrawData);
140         NFCUtil Util;
141         return Util.toVector(UrawData, size);
142 }
143
144 NdefRecordData NdefMessage::getNDEFRecord(const long index) {
145         nfc_ndef_record_h recordHandle;
146         if (nfc_ndef_message_get_record(handle, static_cast<int>(index), &recordHandle) != NFC_ERROR_NONE)
147                 ThrowMsg(PlatformException, "Can't get Ndef Record");
148
149         NdefRecordData recordData;
150
151         nfc_record_tnf_e tnf;
152         unsigned char *typeName, *id, *payload;
153         int typeSize, idSize, payloadSize;
154         NFCUtil Util;
155
156         if (nfc_ndef_record_get_tnf(recordHandle, &tnf) != NFC_ERROR_NONE)
157                 ThrowMsg(PlatformException, "Can't get record's tnf");
158
159         if (nfc_ndef_record_get_type(recordHandle, &typeName, &typeSize) != NFC_ERROR_NONE)
160                 ThrowMsg(PlatformException, "Can't get record's type");
161
162         if (nfc_ndef_record_get_id(recordHandle, &id, &idSize) != NFC_ERROR_NONE)
163                 ThrowMsg(PlatformException, "Can't get record's id");
164
165         if (nfc_ndef_record_get_payload(recordHandle, &payload, &payloadSize) != NFC_ERROR_NONE)
166                 ThrowMsg(PlatformException, "Can't get record's payload");
167
168         LogDebug("tnf : " <<tnf);
169         LogDebug("typeName : " << Util.byteToString(typeName, typeSize));
170         LogDebug("payload : " << Util.byteToString(payload, payloadSize));
171
172         recordData.properties.tnf = Util.convertTonfcTNF(static_cast<unsigned short>(tnf));
173         recordData.properties.typeName = Util.toVector(typeName, typeSize);
174         recordData.properties.id = Util.toVector(id, idSize);
175         recordData.payload = Util.toVector(payload, payloadSize);
176
177         return recordData;
178 }
179
180 bool NdefMessage::insertNDEFRecord(const long index, const void *recordHandle) {
181         nfc_ndef_record_h insertRecord;
182         if (!_copyNDEFRecord((nfc_ndef_record_h *)&recordHandle, &insertRecord)) {
183                 LogDebug("copyNDEFRecord fail!");
184                 ThrowMsg(PlatformException, "Can't copy Record");
185         }
186
187         if (nfc_ndef_message_insert_record(handle, static_cast<int>(index), insertRecord) != NFC_ERROR_NONE)
188                 ThrowMsg(PlatformException, "Can't insert ndef Record");
189
190         return TRUE;
191 }
192
193 bool NdefMessage::appendNDEFRecord(const void *recordHandle) {
194         nfc_ndef_record_h appendRecord;
195         if (!_copyNDEFRecord((nfc_ndef_record_h *)&recordHandle, &appendRecord)) {
196                 LogDebug("copyNDEFRecord fail!");
197                 ThrowMsg(PlatformException, "Can't copy Record");
198         }
199
200         if (nfc_ndef_message_append_record(handle, appendRecord) != NFC_ERROR_NONE)
201                 ThrowMsg(PlatformException, "Can't append ndef Record");
202
203         return TRUE;
204 }
205
206 bool NdefMessage::removeNDEFRecord(const long index) {
207         if (nfc_ndef_message_remove_record(handle, static_cast<int>(index)) != NFC_ERROR_NONE)
208                 ThrowMsg(PlatformException, "Can't remove ndef Record");
209
210         return TRUE;
211 }
212
213 }
214 }
215 }