Add sample application[phone]
[staging/soundmanager.git] / sample / phone / telephony-binding / gdbus / ofono_manager.c
1 /*
2  * Copyright (C) 2017 Konsulko Group
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 #define _GNU_SOURCE
18
19 #include <string.h>
20
21 #include <afb/afb-binding.h>
22
23 #include "ofono_manager.h"
24 #include "ofono_manager_interface.h"
25
26 struct ofono_manager_modem
27 {
28         const gchar *path;
29         const gchar *name;
30         const gchar *type;
31         gboolean powered;
32         gboolean online;
33 };
34
35 static OrgOfonoManager *manager;
36 static struct ofono_manager_modem default_modem;
37 static const struct afb_binding_interface *interface;
38
39 int ofono_manager_init(const struct afb_binding_interface *iface)
40 {
41         GVariant *out_arg = NULL, *next, *value;
42         GError *error = NULL;
43         gchar *path, *key;
44         GVariantIter *iter, *iter2 = NULL;
45         int ret = 0;
46
47         interface = iface;
48
49         if (manager) {
50                 ERROR(interface, "Ofono Manager already initialized\n");
51                 return -1;
52         }
53
54         manager = org_ofono_manager_proxy_new_for_bus_sync(
55                 G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
56                 "org.ofono", "/", NULL, NULL);
57
58         if (!manager) {
59                 ERROR(interface, "Ofono Manager not initialized\n");
60                 return -1;
61         }
62
63         org_ofono_manager_call_get_modems_sync(manager, &out_arg, NULL, &error);
64         if (error == NULL) {
65                 g_variant_get(out_arg, "a(oa{sv})", &iter);
66                 next = g_variant_iter_next_value(iter);
67                 if (next) {
68                         g_variant_get(next, "(oa{sv})", &path, &iter2);
69                         default_modem.path = path;
70                         while (g_variant_iter_loop(iter2, "{sv}", &key, &value)) {
71                                 if (!strcmp(key, "Name"))
72                                         default_modem.name = g_variant_get_string(value, NULL);
73                                 else if (!strcmp(key, "Type"))
74                                         default_modem.type = g_variant_get_string(value, NULL);
75                                 else if (!strcmp(key, "Powered"))
76                                         default_modem.powered = g_variant_get_boolean(value);
77                                 else if (!strcmp(key, "Online"))
78                                         default_modem.online = g_variant_get_boolean(value);
79                         }
80                 } else {
81                         ret = -1;
82                 }
83         } else {
84                 ret = -1;
85         }
86
87         return ret;
88 }
89
90
91 const gchar *ofono_manager_get_default_modem_path(void)
92 {
93         if (!manager) {
94                 ERROR(interface, "Ofono Manager not initialized\n");
95         }
96
97         return default_modem.path;
98 }
99
100 const gchar *ofono_manager_get_default_modem_name(void)
101 {
102         if (!manager) {
103                 ERROR(interface, "Ofono Manager not initialized\n");
104         }
105
106         return default_modem.name;
107 }
108
109 const gchar *ofono_manager_get_default_modem_type(void)
110 {
111         if (!manager) {
112                 ERROR(interface, "Ofono Manager not initialized\n");
113         }
114
115         return default_modem.type;
116 }
117
118 gboolean ofono_manager_get_default_modem_powered(void)
119 {
120         if (!manager) {
121                 ERROR(interface, "Ofono Manager not initialized\n");
122         }
123
124         return default_modem.powered;
125 }
126
127 gboolean ofono_manager_get_default_modem_online(void)
128 {
129         if (!manager) {
130                 ERROR(interface, "Ofono Manager not initialized\n");
131         }
132
133         return default_modem.online;
134 }