[Fix 64bit Build Error] Correctly type casting unsigned int to pointer using GLIB...
[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 NET_NFC_EXPORT_API
29 net_nfc_error_e net_nfc_create_record(ndef_record_h *record,
30         net_nfc_record_tnf_e tnf, data_h typeName, data_h id, data_h payload)
31 {
32         return net_nfc_util_create_record(tnf, (data_s *)typeName,
33                 (data_s *)id, (data_s *)payload, (ndef_record_s **)record);
34 }
35
36 NET_NFC_EXPORT_API
37 net_nfc_error_e net_nfc_create_text_type_record(ndef_record_h *record,
38         const char *text, const char *language_code_str,
39         net_nfc_encode_type_e encode)
40 {
41         return net_nfc_util_create_text_type_record(text, language_code_str,
42                 encode, (ndef_record_s **)record);
43 }
44
45 NET_NFC_EXPORT_API
46 net_nfc_error_e net_nfc_create_uri_type_record(ndef_record_h *record,
47         const char *uri, net_nfc_schema_type_e protocol_schema)
48 {
49         return net_nfc_util_create_uri_type_record(uri, protocol_schema,
50                 (ndef_record_s **)record);
51 }
52
53 NET_NFC_EXPORT_API
54 net_nfc_error_e net_nfc_free_record(ndef_record_h record)
55 {
56         return net_nfc_util_free_record((ndef_record_s *)record);
57 }
58
59 NET_NFC_EXPORT_API
60 net_nfc_error_e net_nfc_get_record_payload(ndef_record_h record,
61         data_h *payload)
62 {
63         ndef_record_s *struct_record = (ndef_record_s *)record;
64
65         if (record == NULL || payload == NULL)
66         {
67                 return NET_NFC_NULL_PARAMETER;
68         }
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         {
82                 return NET_NFC_NULL_PARAMETER;
83         }
84
85         *type = (data_h)&(struct_record->type_s);
86
87         return NET_NFC_OK;
88 }
89
90 NET_NFC_EXPORT_API
91 net_nfc_error_e net_nfc_get_record_id(ndef_record_h record, data_h *id)
92 {
93         ndef_record_s *struct_record = (ndef_record_s *)record;
94
95         if (record == NULL || id == NULL)
96         {
97                 return NET_NFC_NULL_PARAMETER;
98         }
99
100         *id = (data_h)&(struct_record->id_s);
101
102         return NET_NFC_OK;
103 }
104
105 NET_NFC_EXPORT_API
106 net_nfc_error_e net_nfc_get_record_tnf(ndef_record_h record,
107         net_nfc_record_tnf_e *TNF)
108 {
109         ndef_record_s *struct_record = (ndef_record_s *)record;
110
111         if (record == NULL || TNF == NULL)
112         {
113                 return NET_NFC_NULL_PARAMETER;
114         }
115
116         *TNF = (net_nfc_record_tnf_e)struct_record->TNF;
117
118         return NET_NFC_OK;
119 }
120
121 NET_NFC_EXPORT_API
122 net_nfc_error_e net_nfc_set_record_id(ndef_record_h record, data_h id)
123 {
124         data_s *tmp_id = (data_s *)id;
125
126         if (record == NULL || tmp_id == NULL)
127         {
128                 return NET_NFC_NULL_PARAMETER;
129         }
130
131         return net_nfc_util_set_record_id((ndef_record_s *)record,
132                 tmp_id->buffer, tmp_id->length);
133 }
134
135 NET_NFC_EXPORT_API
136 net_nfc_error_e net_nfc_get_record_flags(ndef_record_h record, uint8_t *flag)
137 {
138         ndef_record_s *struct_record = (ndef_record_s *)record;
139
140         if (record == NULL || flag == NULL)
141         {
142                 return NET_NFC_NULL_PARAMETER;
143         }
144
145         *flag = struct_record->MB;
146         *flag <<= 1;
147         *flag += struct_record->ME;
148         *flag <<= 1;
149         *flag += struct_record->CF;
150         *flag <<= 1;
151         *flag += struct_record->SR;
152         *flag <<= 1;
153         *flag += struct_record->IL;
154         *flag <<= 3;
155         *flag += struct_record->TNF;
156
157         return NET_NFC_OK;
158 }
159
160 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_mb(uint8_t flag)
161 {
162         return ((flag >> 7) & 0x01);
163 }
164
165 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_me(uint8_t flag)
166 {
167         return ((flag >> 6) & 0x01);
168 }
169
170 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_cf(uint8_t flag)
171 {
172         return ((flag >> 5) & 0x01);
173 }
174
175 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_sr(uint8_t flag)
176 {
177         return ((flag >> 4) & 0x01);
178 }
179
180 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_il(uint8_t flag)
181 {
182         return ((flag >> 3) & 0x01);
183 }
184
185 static bool _is_text_record(ndef_record_h record)
186 {
187         bool result = false;
188         data_h type;
189
190         if ((net_nfc_get_record_type(record, &type) == NET_NFC_OK) &&
191                 (strncmp((char *)net_nfc_get_data_buffer(type),
192                         TEXT_RECORD_TYPE,
193                         net_nfc_get_data_length(type)) == 0))
194                 result = true;
195
196         return result;
197 }
198
199 NET_NFC_EXPORT_API
200 net_nfc_error_e net_nfc_create_text_string_from_text_record(
201         ndef_record_h record, char **buffer)
202 {
203         net_nfc_error_e result;
204         data_h payload;
205
206         if (record == NULL || buffer == NULL)
207         {
208                 return NET_NFC_NULL_PARAMETER;
209         }
210
211         *buffer = NULL;
212
213         if (_is_text_record(record) == false)
214         {
215                 DEBUG_ERR_MSG("record type is not matched");
216
217                 return NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE;
218         }
219
220         result = net_nfc_get_record_payload(record, &payload);
221         if (result == NET_NFC_OK)
222         {
223                 uint8_t *buffer_temp = net_nfc_get_data_buffer(payload);
224                 uint32_t buffer_length = net_nfc_get_data_length(payload);
225
226                 int controllbyte = buffer_temp[0];
227                 int lang_code_length = controllbyte & 0x3F;
228                 int index = lang_code_length + 1;
229                 int text_length = buffer_length - (lang_code_length + 1);
230
231                 char *temp = NULL;
232
233                 _net_nfc_util_alloc_mem(temp, text_length + 1);
234                 if (temp != NULL)
235                 {
236                         memcpy(temp, &(buffer_temp[index]), text_length);
237
238                         DEBUG_CLIENT_MSG("text = [%s]", temp);
239
240                         *buffer = temp;
241                 }
242                 else
243                 {
244                         result = NET_NFC_ALLOC_FAIL;
245                 }
246         }
247
248         return result;
249 }
250
251 NET_NFC_EXPORT_API
252 net_nfc_error_e net_nfc_get_languange_code_string_from_text_record(
253         ndef_record_h record, char **lang_code_str)
254 {
255         net_nfc_error_e result;
256         data_h payload;
257
258         if (record == NULL || lang_code_str == NULL)
259         {
260                 return NET_NFC_NULL_PARAMETER;
261         }
262
263         *lang_code_str = NULL;
264
265         if (_is_text_record(record) == false)
266         {
267                 DEBUG_ERR_MSG("record type is not matched");
268
269                 return NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE;
270         }
271
272         result = net_nfc_get_record_payload(record, &payload);
273         if (result == NET_NFC_OK)
274         {
275                 uint8_t *buffer_temp = net_nfc_get_data_buffer(payload);
276                 char *buffer = NULL;
277
278                 int controllbyte = buffer_temp[0];
279                 int lang_code_length = controllbyte & 0x3F;
280                 int index = 1;
281
282                 _net_nfc_util_alloc_mem(buffer, lang_code_length + 1);
283                 if (buffer != NULL)
284                 {
285                         memcpy(buffer, &(buffer_temp[index]), lang_code_length);
286
287                         DEBUG_CLIENT_MSG("language code = [%s]", buffer);
288
289                         *lang_code_str = buffer;
290                 }
291                 else
292                 {
293                         result = NET_NFC_ALLOC_FAIL;
294                 }
295         }
296
297         return result;
298 }
299
300 NET_NFC_EXPORT_API
301 net_nfc_error_e net_nfc_get_encoding_type_from_text_record(ndef_record_h record,
302         net_nfc_encode_type_e *encoding)
303 {
304         net_nfc_error_e result;
305         data_h payload;
306
307         if (record == NULL || encoding == NULL)
308         {
309                 return NET_NFC_NULL_PARAMETER;
310         }
311
312         *encoding = NET_NFC_ENCODE_UTF_8;
313
314         if (_is_text_record(record) == false)
315         {
316                 DEBUG_ERR_MSG("record type is not matched");
317
318                 return NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE;
319         }
320
321         result = net_nfc_get_record_payload(record, &payload);
322         if (result == NET_NFC_OK)
323         {
324                 uint8_t *buffer_temp = net_nfc_get_data_buffer(payload);
325
326                 int controllbyte = buffer_temp[0];
327
328                 if ((controllbyte & 0x80) == 0x80)
329                 {
330                         *encoding = NET_NFC_ENCODE_UTF_16;
331                 }
332         }
333
334         return result;
335 }
336
337 NET_NFC_EXPORT_API
338 net_nfc_error_e net_nfc_create_uri_string_from_uri_record(ndef_record_h record,
339         char **uri)
340 {
341         return net_nfc_util_create_uri_string_from_uri_record(
342                 (ndef_record_s *)record, uri);
343 }