From 56fec224dc4dd73a631ac2c15c1a240fd8e92e81 Mon Sep 17 00:00:00 2001 From: saman Date: Sat, 7 Nov 2020 09:58:01 +0330 Subject: Getting battery level of connected phone device Adding "get_battery_level" verb for getting battery level of connected phone device. Is used ofono service and handsfree interface. Bug-AGL: SPEC-3687 Signed-off-by: saman Change-Id: I9713cb186195a170909e6511991cfc32380e12a1 --- binding/gdbus/ofono_manager.c | 77 +++++++++++++++++++++++++++++++++++++++++++ binding/gdbus/ofono_manager.h | 4 +++ binding/telephony-binding.c | 22 +++++++++++++ 3 files changed, 103 insertions(+) (limited to 'binding') diff --git a/binding/gdbus/ofono_manager.c b/binding/gdbus/ofono_manager.c index c0000dc..0eb63ea 100644 --- a/binding/gdbus/ofono_manager.c +++ b/binding/gdbus/ofono_manager.c @@ -182,3 +182,80 @@ gboolean ofono_manager_get_default_modem_valid(void) return default_modem.valid; } + +static json_object *gvariant_to_json(GVariant *value) +{ + const gchar *type = g_variant_get_type_string(value); + json_object *res = NULL; + json_object *obj = NULL; + switch (*type) { + case 'o': + case 's': + res = json_object_new_string(g_variant_get_string(value, NULL)); + break; + case 'b': + res = json_object_new_int(g_variant_get_boolean(value)); + break; + case 'u': + res = json_object_new_int64(g_variant_get_uint32(value)); + break; + case 'y': + res = json_object_new_int((int)g_variant_get_byte(value)); + break; + case 'a': + if(g_strcmp0(type, "as")) + break; + const gchar *arr_str; + GVariantIter i; + obj = json_object_new_array(); + g_variant_iter_init(&i, value); + while (g_variant_iter_next(&i, "s", &arr_str)) + json_object_array_add(obj, json_object_new_string(arr_str)); + res = obj; + default: + break; + } + return res; +} + +json_object * ofono_get_properties(GDBusConnection *conn, const char *path, const char *interface, GError **error) +{ + GVariant *reply = NULL; + GVariantIter *array; + GVariant *var = NULL; + const gchar *key = NULL; + json_object *jprop = NULL; + + reply = g_dbus_connection_call_sync(conn, + OFONO_SERVICE, path, interface, "GetProperties", + NULL, NULL, G_DBUS_CALL_FLAGS_NONE, + (120 * 1000), NULL, error); + if (!reply) + return NULL; + + g_variant_get(reply, "(a{sv})", &array); + jprop = json_object_new_object(); + while (g_variant_iter_loop(array, "{sv}", &key, &var)) { + json_object_object_add(jprop,key,gvariant_to_json(var)); + } + g_variant_iter_free(array); + g_variant_unref(reply); + return jprop; +} + +json_object *ofono_get_property(const char *path, const char *interface, const char *name, GError **error) +{ + GDBusConnection *conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, error); + json_object *jprop; + json_object *jret = json_object_new_object(); + jprop = ofono_get_properties(conn,path,interface,error); + json_object_object_foreach(jprop, key, jval) { + if (name) { + if (!g_strcmp0(key, name)) { + return jval; + } + } + json_object_object_add(jret,key,jval); + } + return jret; +} \ No newline at end of file diff --git a/binding/gdbus/ofono_manager.h b/binding/gdbus/ofono_manager.h index 0556d4f..c7333d0 100644 --- a/binding/gdbus/ofono_manager.h +++ b/binding/gdbus/ofono_manager.h @@ -21,6 +21,9 @@ #define AFB_BINDING_VERSION 3 #include +#define OFONO_SERVICE "org.ofono" +#define OFONO_HANDSFREE_INTERFACE OFONO_SERVICE ".Handsfree" + int ofono_manager_init(void); void ofono_manager_invalidate_default_modem(void); int ofono_manager_set_default_modem(const char *); @@ -31,3 +34,4 @@ const gchar *ofono_manager_get_default_modem_type(void); gboolean ofono_manager_get_default_modem_powered(void); gboolean ofono_manager_get_default_modem_online(void); gboolean ofono_manager_get_default_modem_valid(void); +json_object *ofono_get_property(const char *path, const char *interface, const char *name, GError **error); \ No newline at end of file diff --git a/binding/telephony-binding.c b/binding/telephony-binding.c index c84af2c..dce0ef9 100644 --- a/binding/telephony-binding.c +++ b/binding/telephony-binding.c @@ -106,6 +106,24 @@ static void answer(afb_req_t request) } } +static void get_battery_level(afb_req_t request) +{ + const gchar *device; + + device = ofono_manager_get_default_modem_path(); + if (!device) { + afb_req_fail(request, "failed", "No path find"); + return; + } + + json_object *result = ofono_get_property(device, OFONO_HANDSFREE_INTERFACE, "BatteryChargeLevel", NULL); + if (!result) + afb_req_fail(request, "failed", "Can not find Battery object"); + else + afb_req_success(request, result, "OK"); + return; +} + static void subscribe(afb_req_t request) { const char *value = afb_req_value(request, "value"); @@ -353,6 +371,10 @@ static const afb_verb_t verbs[]= { .verb = "answer", .callback = answer, }, + { + .verb = "get_battery_level", + .callback = get_battery_level, + }, { .verb = "subscribe", .callback = subscribe, -- cgit 1.2.3-korg