Update filtered data
[platform/core/api/uwb.git] / src / uwb-util.c
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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 <glib.h>
18 #include <uwb.h>
19 #include <uwb-log.h>
20 #include <uwb-private.h>
21 #include <stdlib.h>
22
23 /* LCOV_EXCL_START */
24 void _node_free_func(gpointer data)
25 {
26         uwb_node_s *node = (uwb_node_s *)data;
27
28         free(node);
29         node = NULL;
30 }
31 /* LCOV_EXCL_STOP */
32
33 void uwb_util_destroy_network(uwb_network_s *network)
34 {
35         if (network == NULL)
36                 return;
37
38         g_slist_free_full(network->remote_node_list, _node_free_func);
39
40         g_free(network);
41         network = NULL;
42 }
43
44 uwb_node_s *uwb_util_get_node_from_variant(GVariant *va)
45 {
46         GVariantIter *iter = NULL;
47         const gchar *key;
48         GVariant *key_value = NULL;
49
50         uwb_node_s *node = calloc(1, sizeof(uwb_node_s));
51         if (NULL == node) {
52                 /* LCOV_EXCL_START */
53                 _ERR("Memory allocation failed");
54                 return NULL;
55                 /* LCOV_EXCL_STOP */
56         }
57
58         g_variant_get(va, "a{sv}", &iter);
59         while (g_variant_iter_loop(iter, "{sv}", &key, &key_value)) {
60                 if (g_strcmp0(key, "Distance") == 0)
61                         node->distance = g_variant_get_uint64(key_value);
62                 else if (g_strcmp0(key, "NodeID") == 0)
63                         node->node_id = g_variant_get_uint64(key_value);
64                 else if (g_strcmp0(key, "PanID") == 0)
65                         node->pan_id = g_variant_get_uint16(key_value);
66                 else if (g_strcmp0(key, "X") == 0)
67                         node->x = g_variant_get_int32(key_value);
68                 else if (g_strcmp0(key, "Y") == 0)
69                         node->y = g_variant_get_int32(key_value);
70                 else if (g_strcmp0(key, "Z") == 0)
71                         node->z = g_variant_get_int32(key_value);
72                 else if (g_strcmp0(key, "Range") == 0)
73                         node->range = g_variant_get_int32(key_value);
74                 else if (g_strcmp0(key, "Aoa") == 0)
75                         node->aoa = g_variant_get_int32(key_value);
76                 else if (g_strcmp0(key, "Pdoa") == 0)
77                         node->pdoa = g_variant_get_int32(key_value);
78                 else if (g_strcmp0(key, "FilteredRange") == 0)
79                         node->filtered_range = g_variant_get_int32(key_value);
80                 else if (g_strcmp0(key, "FilteredAoa") == 0)
81                         node->filtered_aoa = g_variant_get_int32(key_value);
82         }
83
84         g_variant_iter_free(iter);
85
86         return node;
87 }