summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsaman <mahmoudi.saman1@gmail.com>2020-11-07 09:58:01 +0330
committersaman <mahmoudi.saman1@gmail.com>2020-11-19 09:08:28 +0330
commit56fec224dc4dd73a631ac2c15c1a240fd8e92e81 (patch)
tree64293bd46878d2ad7052b8387d4f6f21c04acf82
parent5762ff51fce596eadbd57522a6ab2978147cd830 (diff)
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 <mahmoudi.saman1@gmail.com> Change-Id: I9713cb186195a170909e6511991cfc32380e12a1
-rw-r--r--README.md1
-rw-r--r--binding/gdbus/ofono_manager.c77
-rw-r--r--binding/gdbus/ofono_manager.h4
-rw-r--r--binding/telephony-binding.c22
4 files changed, 104 insertions, 0 deletions
diff --git a/README.md b/README.md
index 839d6a7..ac3f623 100644
--- a/README.md
+++ b/README.md
@@ -13,6 +13,7 @@ Telephony service allows respective clients access to the Handsfree Profile via
| dial | dial respective number | *Request:* {"value": "15035551212"} |
| hangup | hangup an active call or reject incoming call | |
| answer | answer incoming call | |
+| get_battery_level | getting battery level of connected phone device | |
## Events
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 <afb/afb-binding.h>
+#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");
@@ -354,6 +372,10 @@ static const afb_verb_t verbs[]= {
.callback = answer,
},
{
+ .verb = "get_battery_level",
+ .callback = get_battery_level,
+ },
+ {
.verb = "subscribe",
.callback = subscribe,
},