Sync code with Tizen 3.0 branch
[platform/core/api/nfc.git] / src / net_nfc_client_ndef_record.c
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #include "net_nfc_ndef_record.h"
18 #include "net_nfc_ndef_message.h"
19 #include "net_nfc_data.h"
20 #include "net_nfc_debug_internal.h"
21 #include "net_nfc_util_internal.h"
22 #include "net_nfc_util_ndef_record.h"
23
24 #ifndef NET_NFC_EXPORT_API
25 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
26 #endif
27
28 /* LCOV_EXCL_START */
29
30 NET_NFC_EXPORT_API
31 net_nfc_error_e net_nfc_create_record(ndef_record_h *record,
32         net_nfc_record_tnf_e tnf, data_h typeName, data_h id, data_h payload)
33 {
34         return net_nfc_util_create_record(tnf, (data_s *)typeName,
35                 (data_s *)id, (data_s *)payload, (ndef_record_s **)record);
36 }
37
38 NET_NFC_EXPORT_API
39 net_nfc_error_e net_nfc_create_text_type_record(ndef_record_h *record,
40         const char *text, const char *language_code_str,
41         net_nfc_encode_type_e encode)
42 {
43         return net_nfc_util_create_text_type_record(text, language_code_str,
44                 encode, (ndef_record_s **)record);
45 }
46
47 NET_NFC_EXPORT_API
48 net_nfc_error_e net_nfc_create_uri_type_record(ndef_record_h *record,
49         const char *uri, net_nfc_schema_type_e protocol_schema)
50 {
51         return net_nfc_util_create_uri_type_record(uri, protocol_schema,
52                 (ndef_record_s **)record);
53 }
54
55 NET_NFC_EXPORT_API
56 net_nfc_error_e net_nfc_free_record(ndef_record_h record)
57 {
58         return net_nfc_util_free_record((ndef_record_s *)record);
59 }
60
61 NET_NFC_EXPORT_API
62 net_nfc_error_e net_nfc_get_record_payload(ndef_record_h record,
63         data_h *payload)
64 {
65         ndef_record_s *struct_record = (ndef_record_s *)record;
66
67         if (record == NULL || payload == NULL)
68                 return NET_NFC_NULL_PARAMETER;
69
70         *payload = (data_h)&(struct_record->payload_s);
71
72         return NET_NFC_OK;
73 }
74
75 NET_NFC_EXPORT_API
76 net_nfc_error_e net_nfc_get_record_type(ndef_record_h record, data_h *type)
77 {
78         ndef_record_s *struct_record = (ndef_record_s *)record;
79
80         if (record == NULL || type == NULL)
81                 return NET_NFC_NULL_PARAMETER;
82
83         *type = (data_h)&(struct_record->type_s);
84
85         return NET_NFC_OK;
86 }
87
88 NET_NFC_EXPORT_API
89 net_nfc_error_e net_nfc_get_record_id(ndef_record_h record, data_h *id)
90 {
91         ndef_record_s *struct_record = (ndef_record_s *)record;
92
93         if (record == NULL || id == NULL)
94                 return NET_NFC_NULL_PARAMETER;
95
96         *id = (data_h)&(struct_record->id_s);
97
98         return NET_NFC_OK;
99 }
100
101 NET_NFC_EXPORT_API
102 net_nfc_error_e net_nfc_get_record_tnf(ndef_record_h record,
103         net_nfc_record_tnf_e *TNF)
104 {
105         ndef_record_s *struct_record = (ndef_record_s *)record;
106
107         if (record == NULL || TNF == NULL)
108                 return NET_NFC_NULL_PARAMETER;
109
110         *TNF = (net_nfc_record_tnf_e)struct_record->TNF;
111
112         return NET_NFC_OK;
113 }
114
115 NET_NFC_EXPORT_API
116 net_nfc_error_e net_nfc_set_record_id(ndef_record_h record, data_h id)
117 {
118         data_s *tmp_id = (data_s *)id;
119
120         if (record == NULL || tmp_id == NULL)
121                 return NET_NFC_NULL_PARAMETER;
122
123         return net_nfc_util_set_record_id((ndef_record_s *)record,
124                 tmp_id->buffer, tmp_id->length);
125 }
126
127 NET_NFC_EXPORT_API
128 net_nfc_error_e net_nfc_get_record_flags(ndef_record_h record, uint8_t *flag)
129 {
130         ndef_record_s *struct_record = (ndef_record_s *)record;
131
132         if (record == NULL || flag == NULL)
133                 return NET_NFC_NULL_PARAMETER;
134
135         *flag = struct_record->MB;
136         *flag <<= 1;
137         *flag += struct_record->ME;
138         *flag <<= 1;
139         *flag += struct_record->CF;
140         *flag <<= 1;
141         *flag += struct_record->SR;
142         *flag <<= 1;
143         *flag += struct_record->IL;
144         *flag <<= 3;
145         *flag += struct_record->TNF;
146
147         return NET_NFC_OK;
148 }
149
150 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_mb(uint8_t flag)
151 {
152         return ((flag >> 7) & 0x01);
153 }
154
155 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_me(uint8_t flag)
156 {
157         return ((flag >> 6) & 0x01);
158 }
159
160 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_cf(uint8_t flag)
161 {
162         return ((flag >> 5) & 0x01);
163 }
164
165 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_sr(uint8_t flag)
166 {
167         return ((flag >> 4) & 0x01);
168 }
169
170 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_il(uint8_t flag)
171 {
172         return ((flag >> 3) & 0x01);
173 }
174
175 static bool _is_text_record(ndef_record_h record)
176 {
177         bool result = false;
178         data_h type;
179
180         if ((net_nfc_get_record_type(record, &type) == NET_NFC_OK) &&
181                 (strncmp((char *)net_nfc_get_data_buffer(type),
182                         TEXT_RECORD_TYPE,
183                         net_nfc_get_data_length(type)) == 0))
184                 result = true;
185
186         return result;
187 }
188
189 NET_NFC_EXPORT_API
190 net_nfc_error_e net_nfc_create_text_string_from_text_record(
191         ndef_record_h record, char **buffer)
192 {
193         net_nfc_error_e result;
194         data_h payload;
195
196         if (record == NULL || buffer == NULL)
197                 return NET_NFC_NULL_PARAMETER;
198
199         *buffer = NULL;
200
201         if (_is_text_record(record) == false) {
202                 DEBUG_ERR_MSG("record type is not matched");
203
204                 return NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE;
205         }
206
207         result = net_nfc_get_record_payload(record, &payload);
208         if (result == NET_NFC_OK) {
209                 uint8_t *buffer_temp = net_nfc_get_data_buffer(payload);
210                 uint32_t buffer_length = net_nfc_get_data_length(payload);
211
212                 int controllbyte = buffer_temp[0];
213                 int lang_code_length = controllbyte & 0x3F;
214                 int index = lang_code_length + 1;
215                 int text_length = buffer_length - (lang_code_length + 1);
216
217                 char *temp = NULL;
218
219                 _net_nfc_util_alloc_mem(temp, text_length + 1);
220                 if (temp != NULL) {
221                         memcpy(temp, &(buffer_temp[index]), text_length);
222
223                         DEBUG_CLIENT_MSG("text = [%s]", temp);
224
225                         *buffer = temp;
226                 } else {
227                         result = NET_NFC_ALLOC_FAIL;
228                 }
229         }
230
231         return result;
232 }
233
234 NET_NFC_EXPORT_API
235 net_nfc_error_e net_nfc_get_languange_code_string_from_text_record(
236         ndef_record_h record, char **lang_code_str)
237 {
238         net_nfc_error_e result;
239         data_h payload;
240
241         if (record == NULL || lang_code_str == NULL)
242                 return NET_NFC_NULL_PARAMETER;
243
244         *lang_code_str = NULL;
245
246         if (_is_text_record(record) == false) {
247                 DEBUG_ERR_MSG("record type is not matched");
248
249                 return NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE;
250         }
251
252         result = net_nfc_get_record_payload(record, &payload);
253         if (result == NET_NFC_OK) {
254                 uint8_t *buffer_temp = net_nfc_get_data_buffer(payload);
255                 char *buffer = NULL;
256
257                 int controllbyte = buffer_temp[0];
258                 int lang_code_length = controllbyte & 0x3F;
259                 int index = 1;
260
261                 _net_nfc_util_alloc_mem(buffer, lang_code_length + 1);
262                 if (buffer != NULL) {
263                         memcpy(buffer, &(buffer_temp[index]), lang_code_length);
264
265                         DEBUG_CLIENT_MSG("language code = [%s]", buffer);
266
267                         *lang_code_str = buffer;
268                 } else {
269                         result = NET_NFC_ALLOC_FAIL;
270                 }
271         }
272
273         return result;
274 }
275
276 NET_NFC_EXPORT_API
277 net_nfc_error_e net_nfc_get_encoding_type_from_text_record(ndef_record_h record,
278         net_nfc_encode_type_e *encoding)
279 {
280         net_nfc_error_e result;
281         data_h payload;
282
283         if (record == NULL || encoding == NULL)
284                 return NET_NFC_NULL_PARAMETER;
285
286         *encoding = NET_NFC_ENCODE_UTF_8;
287
288         if (_is_text_record(record) == false) {
289                 DEBUG_ERR_MSG("record type is not matched");
290
291                 return NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE;
292         }
293
294         result = net_nfc_get_record_payload(record, &payload);
295         if (result == NET_NFC_OK) {
296                 uint8_t *buffer_temp = net_nfc_get_data_buffer(payload);
297
298                 int controllbyte = buffer_temp[0];
299
300                 if ((controllbyte & 0x80) == 0x80)
301                         *encoding = NET_NFC_ENCODE_UTF_16;
302         }
303
304         return result;
305 }
306
307 NET_NFC_EXPORT_API
308 net_nfc_error_e net_nfc_create_uri_string_from_uri_record(ndef_record_h record,
309         char **uri)
310 {
311         return net_nfc_util_create_uri_string_from_uri_record(
312                 (ndef_record_s *)record, uri);
313 }
314
315 /* LCOV_EXCL_STOP */
316