fixed compile errors
[platform/framework/web/wrt-plugins-tizen.git] / src / NFC / NdefMessage.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include <Commons/Exception.h>
19 #include "NdefMessage.h"
20 #include "NFCUtil.h"
21 #include <Logger.h>
22
23 namespace DeviceAPI {
24 namespace NFC {
25
26 using namespace WrtDeviceApis::Commons;
27
28 NdefMessage::NdefMessage()
29 {
30         LoggerD("entered");
31 }
32
33 NdefMessage::~NdefMessage()
34 {
35         LoggerD("entered");
36 }
37
38 void *NdefMessage::makeNdefMessageHandle(std::vector<void *> &ndefRcords) {
39         NFCUtil util;
40         if (ndefRcords.size() == 0)
41                 return NULL;
42
43         nfc_ndef_message_h message = NULL;
44         int result = nfc_ndef_message_create(&message);
45
46         if (result != NFC_ERROR_NONE) {
47                 LoggerE(util.getNFCErrorMessage(result));
48                 if (message)
49                         nfc_ndef_message_destroy(message);
50                 util.throwNFCException(result, "Can't create Ndef Message");
51         } else if (!message) {
52                 LoggerE("No Message Handle");
53                 ThrowMsg(PlatformException, "Can't create Ndef Message");
54         }
55
56         for (int i = 0 ; i < static_cast<int>(ndefRcords.size()); i++) {
57                 if ((message == NULL) && (ndefRcords[i] != NULL))
58                         nfc_ndef_record_destroy((nfc_ndef_record_h)ndefRcords[i]);
59                 result = nfc_ndef_message_append_record(message, (nfc_ndef_record_h)ndefRcords[i]);
60
61                 if (result != NFC_ERROR_NONE) {
62                         LoggerE(i << " record can't be inserted. " << ndefRcords[i] << " : " << message);
63                         if (message)
64                                 nfc_ndef_message_destroy(message);
65                         message = NULL;
66                 }
67         }
68
69         return (void *)message;
70 }
71
72 std::vector<unsigned char> NdefMessage::toByte(std::vector<void *> &ndefRcords) {
73         NFCUtil util;
74         if (ndefRcords.size() == 0)
75                 return util.toVector(NULL, 0);
76
77         nfc_ndef_message_h message = (nfc_ndef_message_h)makeNdefMessageHandle(ndefRcords);
78         if (message == NULL)
79                 ThrowMsg(ConversionException, "Invalid NDEF Message");
80         unsigned char *rawdata = NULL;
81         unsigned int size = 0;
82         int result = nfc_ndef_message_get_rawdata(message, &rawdata, &size);
83
84         std::vector<unsigned char> byteData = util.toVector(rawdata, size);
85
86         if (message)
87                 nfc_ndef_message_destroy(message);
88         if (rawdata)
89                 free(rawdata);
90
91         if (result != NFC_ERROR_NONE)
92                 util.throwNFCException(result, "Can't get serial bytes of NDEF message");
93
94         return byteData;
95 }
96
97 std::vector<NdefRecordData> NdefMessage::toNDEFRecords(const std::vector<unsigned char> &rawdata) {
98         LoggerD("entered");
99         NFCUtil util;
100         unsigned char *messageRawdata = util.toCharPtr(rawdata);
101         nfc_ndef_message_h message = NULL;
102
103         try {
104                 int result = nfc_ndef_message_create_from_rawdata(&message, messageRawdata, rawdata.size());
105                 if (messageRawdata)
106                         free(messageRawdata);
107
108                 if (result != NFC_ERROR_NONE)
109                         util.throwNFCException(result, "Can't create Ndef Message");
110                 else if (!message)
111                         ThrowMsg(PlatformException, "Can't create Ndef Message");
112         } catch (const WrtDeviceApis::Commons::Exception& err) {
113                 LoggerE(err.GetClassName() << ":"<<err.GetMessage());
114         }
115
116         return toNDEFRecords((void *)message);
117 }
118
119 std::vector<NdefRecordData> NdefMessage::toNDEFRecords(void *message) {
120         LoggerD("entered");
121         NFCUtil util;
122         std::vector<NdefRecordData> records;
123
124         if (message == NULL)
125                 return records;
126
127         try {
128                 int count = 0;
129                 int result = nfc_ndef_message_get_record_count((nfc_ndef_message_h)message, &count);
130                 if (result != NFC_ERROR_NONE)
131                         util.throwNFCException(result, "Can't get a record count of message");
132                 for (int i=0; i<count; i++) {
133                         NdefRecordData record;
134                         try {
135                                 record = _getNDEFRecord((nfc_ndef_message_h)message, i);
136                         } catch(const WrtDeviceApis::Commons::Exception& err) {
137                                 LoggerE(err.GetClassName() << ":"<<err.GetMessage());
138                         }
139                         records.push_back(record);
140                 }
141         } catch (const WrtDeviceApis::Commons::Exception& err) {
142                 LoggerE(err.GetClassName() << ":"<<err.GetMessage());
143         }
144
145
146         if (message)
147                 nfc_ndef_message_destroy((nfc_ndef_message_h)message);
148         message = NULL;
149
150         return records;
151 }
152
153
154 NdefRecordData NdefMessage::_getNDEFRecord(nfc_ndef_message_h handle, const long index) {
155         nfc_ndef_record_h recordHandle;
156         int result = nfc_ndef_message_get_record(handle, static_cast<int>(index), &recordHandle);
157
158         NFCUtil util;
159         if (result != NFC_ERROR_NONE)
160                 util.throwNFCException(result, "Can't get Ndef Record");
161
162         NdefRecordData recordData;
163
164         return util.getNDEFRecordData(recordHandle);
165 }
166
167 }
168 }