Get the latest NFC state from neard
[platform/core/connectivity/nfc-manager-neard.git] / common / net_nfc_util_gdbus.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 // libc header
18
19 // platform header
20
21 // nfc-manager header
22 #include "net_nfc_debug_internal.h"
23 #include "net_nfc_util_internal.h"
24 #include "net_nfc_util_gdbus_internal.h"
25 #include "net_nfc_util_ndef_message.h"
26
27 void net_nfc_util_gdbus_variant_to_buffer(GVariant *variant, uint8_t **buffer,
28                 size_t *length)
29 {
30         guint size = 0;
31         GVariantIter *iter;
32         guint8 *buf = NULL;
33
34         RET_IF(NULL == variant);
35
36         g_variant_get(variant, "a(y)", &iter);
37
38         size = g_variant_iter_n_children(iter);
39         buf  = g_new0(guint8, size);
40
41         if (buf != NULL)
42         {
43                 guint i;
44                 guint8 element;
45
46                 i = 0;
47                 while (g_variant_iter_loop(iter, "(y)", &element))
48                 {
49                         *(buf + i) = element;
50                         i++;
51                 }
52
53                 g_variant_iter_free(iter);
54
55                 if (length)
56                         *length = size;
57
58                 if (buffer)
59                         *buffer = buf;
60                 else
61                         g_free(buf);
62         }
63 }
64
65 data_s *net_nfc_util_gdbus_variant_to_data(GVariant *variant)
66 {
67         guint size = 0;
68         GVariantIter *iter;
69         guint8 *buf = NULL;
70         data_s *result = NULL;
71
72         RETV_IF(NULL == variant, result);
73
74         g_variant_get(variant, "a(y)", &iter);
75
76         size = g_variant_iter_n_children(iter);
77         buf  = g_new0(guint8, size);
78         if (buf != NULL)
79         {
80                 guint i;
81                 guint8 element;
82
83                 i = 0;
84                 while (g_variant_iter_loop(iter, "(y)", &element))
85                 {
86                         *(buf + i) = element;
87                         i++;
88                 }
89
90                 g_variant_iter_free(iter);
91
92                 result = g_new0(data_s, 1);
93                 if (result != NULL)
94                 {
95                         result->buffer = buf;
96                         result->length = size;
97                 }
98                 else
99                 {
100                         g_free(buf);
101                 }
102         }
103
104         return result;
105 }
106
107 void net_nfc_util_gdbus_variant_to_data_s(GVariant *variant, data_s *data)
108 {
109         guint size = 0;
110         guint8 element;
111         GVariantIter *iter;
112         guint8 *buf = NULL;
113
114         RET_IF(NULL == data);
115         RET_IF(NULL == variant);
116
117         data->buffer = NULL;
118         data->length = 0;
119
120         g_variant_get(variant, "a(y)", &iter);
121
122         size = g_variant_iter_n_children(iter);
123         buf  = g_new0(guint8, size);
124
125         if (buf != NULL)
126         {
127                 guint i = 0;
128
129                 while (g_variant_iter_loop(iter, "(y)", &element))
130                 {
131                         *(buf + i) = element;
132                         i++;
133                 }
134
135                 g_variant_iter_free(iter);
136
137                 data->length = size;
138                 data->buffer = buf;
139         }
140 }
141
142 GVariant *net_nfc_util_gdbus_buffer_to_variant(const uint8_t *buffer,
143                 size_t length)
144 {
145         GVariantBuilder builder;
146
147         g_variant_builder_init(&builder, G_VARIANT_TYPE("a(y)"));
148
149         if (buffer && length > 0)
150         {
151                 int i;
152
153                 for(i = 0; i < length; i++)
154                         g_variant_builder_add(&builder, "(y)", *(buffer + i));
155         }
156
157         return g_variant_builder_end(&builder);
158 }
159
160 GVariant *net_nfc_util_gdbus_data_to_variant(const data_s *data)
161 {
162         if (data != NULL)
163                 return net_nfc_util_gdbus_buffer_to_variant(data->buffer, data->length);
164         else
165                 return net_nfc_util_gdbus_buffer_to_variant(NULL, 0);
166 }
167
168 ndef_message_s *net_nfc_util_gdbus_variant_to_ndef_message(GVariant *variant)
169 {
170         data_s data = { NULL, 0 };
171         ndef_message_s *temp = NULL;
172         ndef_message_s *message = NULL;
173
174         RETV_IF(NULL == variant, NULL);
175
176         net_nfc_util_gdbus_variant_to_data_s(variant, &data);
177
178         if (NULL == data.buffer || data.length <= 0)
179                 return NULL;
180
181         if (net_nfc_util_create_ndef_message(&temp) == NET_NFC_OK)
182         {
183                 if (net_nfc_util_convert_rawdata_to_ndef_message(&data, temp) == NET_NFC_OK)
184                 {
185                         message = temp;
186                 }
187                 else
188                 {
189                         NFC_ERR("net_nfc_create_ndef_message_from_rawdata failed");
190
191                         net_nfc_util_free_ndef_message(temp);
192                 }
193         }
194         else
195         {
196                 NFC_ERR("net_nfc_util_create_ndef_message failed");
197         }
198
199         net_nfc_util_free_data(&data);
200
201         return message;
202 }
203
204
205 GVariant *net_nfc_util_gdbus_ndef_message_to_variant(
206                 const ndef_message_s *message)
207 {
208         size_t length;
209         net_nfc_error_e ret;
210         data_s *data = NULL;
211         GVariant *variant = NULL;
212
213         length = net_nfc_util_get_ndef_message_length((ndef_message_s *)message);
214         if (length > 0)
215         {
216                 data_s temp = { NULL, 0 };
217
218                 if (net_nfc_util_alloc_data(&temp, length) == true)
219                 {
220                         ret = net_nfc_util_convert_ndef_message_to_rawdata((ndef_message_s *)message,
221                                                 &temp);
222                         if (NET_NFC_OK == ret)
223                         {
224                                 data = &temp;
225                         }
226                         else
227                         {
228                                 NFC_ERR("can not convert ndef_message to rawdata");
229
230                                 net_nfc_util_free_data(&temp);
231                         }
232                 }
233                 else
234                 {
235                         NFC_ERR("net_nfc_util_alloc_data failed");
236                 }
237         }
238         else
239         {
240                 NFC_ERR("message length is 0");
241         }
242
243         variant = net_nfc_util_gdbus_data_to_variant(data);
244
245         if (data != NULL)
246                 net_nfc_util_free_data(data);
247
248         return variant;
249 }