2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <mdg_debug.h>
21 #include <mdg_private.h>
23 mdg_group_s *mdg_get_group_from_variant(GVariant *va)
25 char *device_id = NULL;
26 char *group_name = NULL;
27 mdg_group_type_e type = MDG_GROUP_TYPE_LOCAL;
28 GVariantIter *iter = NULL;
30 GVariant *key_value = NULL;
32 mdg_group_s *group = calloc(1, sizeof(mdg_group_s));
35 _ERR("Memory allocation failed");
40 g_variant_get(va, "a{sv}", &iter);
41 while (g_variant_iter_loop(iter, "{sv}", &key, &key_value)) {
42 if (g_strcmp0(key, "DeviceID") == 0)
43 device_id = (char *)g_variant_get_string(key_value, NULL);
44 else if (g_strcmp0(key, "GroupName") == 0)
45 group_name = (char *)g_variant_get_string(key_value, NULL);
46 else if (g_strcmp0(key, "Type") == 0)
47 type = g_variant_get_int32(key_value);
50 group->device_id = g_strdup(device_id);
51 group->group_name = g_strdup(group_name);
54 g_variant_iter_free(iter);
59 mdg_device_s *mdg_get_device_from_variant(GVariant *va)
61 char *device_id = NULL;
62 char *model_name = NULL;
63 char *device_name = NULL;
64 char *platform_ver = NULL;
65 char *vendor_id = NULL;
68 int type = MDG_DEVICE_TYPE_LOCAL;
69 GVariantIter *iter = NULL;
71 GVariant *key_value = NULL;
73 mdg_device_s *device = calloc(1, sizeof(mdg_device_s));
76 _ERR("Memory allocation failed");
81 g_variant_get(va, "a{sv}", &iter);
82 while (g_variant_iter_loop(iter, "{sv}", &key, &key_value)) {
83 if (g_strcmp0(key, "DeviceID") == 0)
84 device_id = (char *)g_variant_get_string(key_value, NULL);
85 else if (g_strcmp0(key, "ModelName") == 0)
86 model_name = (char *)g_variant_get_string(key_value, NULL);
87 else if (g_strcmp0(key, "DeviceName") == 0)
88 device_name = (char *)g_variant_get_string(key_value, NULL);
89 else if (g_strcmp0(key, "PlatformVer") == 0)
90 platform_ver = (char *)g_variant_get_string(key_value, NULL);
91 else if (g_strcmp0(key, "VendorID") == 0)
92 vendor_id = (char *)g_variant_get_string(key_value, NULL);
93 else if (g_strcmp0(key, "Profile") == 0)
94 profile = (char *)g_variant_get_string(key_value, NULL);
95 else if (g_strcmp0(key, "IsInvited") == 0)
96 is_invited = g_variant_get_int32(key_value);
97 else if (g_strcmp0(key, "Type") == 0)
98 type = g_variant_get_int32(key_value);
101 device->device_id = g_strdup(device_id);
102 device->model_name = g_strdup(model_name);
103 device->device_name = g_strdup(device_name);
104 device->platform_ver = g_strdup(platform_ver);
105 device->vendor_id = g_strdup(vendor_id);
106 device->profile = g_strdup(profile);
107 device->is_invited = is_invited;
110 g_variant_iter_free(iter);
115 void mdg_get_channel_from_variant(GVariant *va, char **device_id, char **channel_id)
117 GVariantIter *iter = NULL;
119 GVariant *key_value = NULL;
121 g_variant_get(va, "a{sv}", &iter);
122 while (g_variant_iter_loop(iter, "{sv}", &key, &key_value)) {
123 if (g_strcmp0(key, "DeviceID") == 0)
124 *device_id = (char *)g_variant_get_string(key_value, NULL);
125 else if (g_strcmp0(key, "ChannelID") == 0)
126 *channel_id = (char *)g_variant_get_string(key_value, NULL);
129 g_variant_iter_free(iter);
132 void mdg_get_data_from_variant(GVariant *va, char **device_id, char **channel_id,
133 unsigned char **data, int *data_len)
135 GVariantIter *iter = NULL;
137 GVariant *key_value = NULL;
139 GVariantIter *data_iter = NULL;
140 unsigned char byte_data;
142 g_variant_get(va, "a{sv}", &iter);
143 while (g_variant_iter_loop(iter, "{sv}", &key, &key_value)) {
144 if (g_strcmp0(key, "DeviceID") == 0)
145 *device_id = (char *)g_variant_get_string(key_value, NULL);
146 else if (g_strcmp0(key, "ChannelID") == 0)
147 *channel_id = (char *)g_variant_get_string(key_value, NULL);
148 else if (g_strcmp0(key, "Data") == 0)
149 data_va = g_variant_get_variant(key_value);
150 else if (g_strcmp0(key, "DataLength") == 0)
151 *data_len = g_variant_get_int32(key_value);
155 *data = (unsigned char *)g_try_malloc0(*data_len + 1);
157 g_variant_get(data_va, "a(y)", &data_iter);
158 while (g_variant_iter_loop(data_iter, "(y)", &byte_data))
159 (*data)[i++] = byte_data;
161 g_variant_iter_free(iter);
164 void mdg_get_progress_from_variant(GVariant *va, char **file_path,
165 long *send_size, long *total_size, int *percent)
167 GVariantIter *iter = NULL;
169 GVariant *key_value = NULL;
171 g_variant_get(va, "a{sv}", &iter);
172 while (g_variant_iter_loop(iter, "{sv}", &key, &key_value)) {
173 if (g_strcmp0(key, "FilePath") == 0)
174 *file_path = (char *)g_variant_get_string(key_value, NULL);
175 if (g_strcmp0(key, "SendSize") == 0)
176 *send_size = g_variant_get_int64(key_value);
177 if (g_strcmp0(key, "TotalSize") == 0)
178 *total_size = g_variant_get_int64(key_value);
179 if (g_strcmp0(key, "Percent") == 0)
180 *percent = g_variant_get_int32(key_value);
183 g_variant_iter_free(iter);
186 void mdg_get_receive_file_from_variant(GVariant *va, char **device_id, char **file_path)
188 GVariantIter *iter = NULL;
190 GVariant *key_value = NULL;
192 g_variant_get(va, "a{sv}", &iter);
193 while (g_variant_iter_loop(iter, "{sv}", &key, &key_value)) {
194 if (g_strcmp0(key, "DeviceID") == 0)
195 *device_id = (char *)g_variant_get_string(key_value, NULL);
196 if (g_strcmp0(key, "FilePath") == 0)
197 *file_path = (char *)g_variant_get_string(key_value, NULL);
200 g_variant_iter_free(iter);
203 GVariant *mdg_create_variant_device(mdg_device_s *device)
207 GVariantBuilder *builder;
209 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
211 g_variant_builder_add(builder, "{sv}", "DeviceID",
212 g_variant_new_string(device->device_id));
213 g_variant_builder_add(builder, "{sv}", "ModelName",
214 g_variant_new_string(device->model_name));
215 g_variant_builder_add(builder, "{sv}", "DeviceName",
216 g_variant_new_string(device->device_name));
217 g_variant_builder_add(builder, "{sv}", "PlatformVer",
218 g_variant_new_string(device->platform_ver));
219 g_variant_builder_add(builder, "{sv}", "VendorID",
220 g_variant_new_string(device->vendor_id));
221 g_variant_builder_add(builder, "{sv}", "Profile",
222 g_variant_new_string(device->profile));
223 g_variant_builder_add(builder, "{sv}", "IsInvited",
224 g_variant_new_int32(device->is_invited));
225 g_variant_builder_add(builder, "{sv}", "Type",
226 g_variant_new_int32(device->type));
228 va = g_variant_builder_end(builder);
230 g_variant_builder_unref(builder);