summaryrefslogtreecommitdiffstats
path: root/binding-wifi
diff options
context:
space:
mode:
authorMatt Ranostay <matt.ranostay@konsulko.com>2017-06-29 19:01:55 -0700
committerMatt Ranostay <matt.ranostay@konsulko.com>2017-06-30 11:57:16 -0700
commitdfd034fb6016ad056131b17b30a0457789b791b9 (patch)
treec3b0e1ae6d892a44a011f080f978da37e3365473 /binding-wifi
parentca66ba0579e6c1e9a6d484f18633fdbd223a5bb5 (diff)
binding: wifi: make system wide service
Allow WiFi binding to be build separately and installed as a system wide service Bug-AGL: SPEC-661 SPEC-715 Change-Id: Id9eb9d9efae3f0bc3ab00641eb26cd1b5dae9d53 Signed-off-by: Matt Ranostay <matt.ranostay@konsulko.com>
Diffstat (limited to 'binding-wifi')
-rw-r--r--binding-wifi/agent.c292
-rw-r--r--binding-wifi/binding-wifi.pro11
-rw-r--r--binding-wifi/binding.pri6
-rw-r--r--binding-wifi/export.map1
-rw-r--r--binding-wifi/wifi-api.c789
-rw-r--r--binding-wifi/wifi-api.h38
-rw-r--r--binding-wifi/wifi-connman.c396
-rw-r--r--binding-wifi/wifi-connman.h148
8 files changed, 1681 insertions, 0 deletions
diff --git a/binding-wifi/agent.c b/binding-wifi/agent.c
new file mode 100644
index 0000000..6b0f235
--- /dev/null
+++ b/binding-wifi/agent.c
@@ -0,0 +1,292 @@
+/* Copyright 2016 ALPS ELECTRIC CO., LTD.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#include <stdio.h>
+#include <errno.h>
+
+#include <gio/gio.h>
+#include "wifi-connman.h"
+
+static GMainLoop *loop = NULL;
+
+static GDBusNodeInfo *introspection_data = NULL;
+
+GDBusConnection *connectionAgent;
+
+GDBusMethodInvocation *invocation_passkey = NULL;
+
+/* Introspection data for the agent service */
+static const gchar introspection_xml[] = "<node>"
+ " <interface name='net.connman.Agent'>"
+ " <method name='RequestInput'>"
+ " <arg type='o' name='service' direction='in'/>"
+ " <arg type='a{sv}' name='fields' direction='in'/>"
+ " <arg type='a{sv}' name='fields' direction='out'/>"
+ " </method>"
+ " <method name='ReportError'>"
+ " <arg type='o' name='service' direction='in'/>"
+ " <arg type='s' name='error' direction='in'/>"
+ " </method>"
+ " </interface>"
+ "</node>";
+
+callback password_callback;
+callback wifiListChanged_callback;
+
+static void handle_method_call(GDBusConnection *connection, const gchar *sender,
+ const gchar *object_path, const gchar *interface_name,
+ const gchar *method_name, GVariant *parameters,
+ GDBusMethodInvocation *invocation, gpointer user_data) {
+ //MyObject *myobj = user_data;
+
+ if (g_strcmp0(method_name, "RequestInput") == 0) {
+ DEBUG(afbitf,"RequestInput method on Agent interface has been called\n");
+
+ invocation_passkey = invocation;
+
+ GVariantIter *array;
+ gchar * object_path;
+ g_variant_get(parameters, "(oa{sv})", &object_path, &array);
+ //TODO: get only object path for now, complete parameters are
+
+ /*
+ object path "/net/connman/service/wifi_d85d4c880b1a_4c656e6f766f204b3520506c7573_managed_psk"
+ array [
+ dict entry(
+ string "Passphrase"
+ variant array [
+ dict entry(
+ string "Type"
+ variant string "psk"
+ )
+ dict entry(
+ string "Requirement"
+ variant string "mandatory"
+ )
+ ]
+ )
+ ]
+ */
+ NOTICE(afbitf,"Passphrase requested for network : %s\n", object_path);
+ (*password_callback)(0, object_path);
+
+
+ }
+
+ if (g_strcmp0(method_name, "ReportError") == 0) {
+ ERROR(afbitf,"ReportError method on Agent interface has een called\n");
+
+ gchar *error_string; // = NULL;
+
+ gchar * object_path;
+ g_variant_get(parameters, "(os)", &object_path, &error_string);
+
+ ERROR(afbitf, "Error %s for %s\n", error_string, object_path);
+
+ if (g_strcmp0(error_string, "invalid-key") == 0) {
+
+ WARNING(afbitf, "Passkey is not correct.\n");
+ (*password_callback)(1, "invalid-key");
+
+ }
+
+ }
+}
+
+GError* sendPasskey(gchar *passkey) {
+
+ GVariantBuilder *builder;
+ GVariant *value = NULL;
+
+ NOTICE(afbitf, "Passkey to send: %s\n", passkey);
+
+ builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+
+ g_variant_builder_add(builder, "{sv}", "Passphrase",
+ g_variant_new_string(passkey));
+
+ value = g_variant_new("(a{sv})", builder);
+
+ g_dbus_method_invocation_return_value(invocation_passkey, value);
+
+ return NULL;
+
+}
+
+static const GDBusInterfaceVTable interface_vtable = { handle_method_call, NULL,
+ NULL };
+
+static void on_bus_acquired(GDBusConnection *connection, const gchar *name,
+ gpointer user_data) {
+ //MyObject *myobj = user_data;
+ guint registration_id;
+
+ registration_id = g_dbus_connection_register_object(connection,
+ "/net/connman/Agent", introspection_data->interfaces[0],
+ &interface_vtable, NULL, NULL, /* user_data_free_func */
+ NULL); /* GError** */
+ //TODO: make some proper error message rather than exiting
+ //g_assert(registration_id > 0);
+
+ return NULL;
+}
+
+static void test_signal_handler (GDBusConnection *connection, const gchar *sender_name, const gchar *object_path, const gchar *interface_name, const gchar *signal_name, GVariant *parameters, gpointer user_data) {
+
+ //do not parse, just check what has changed and make callback -
+ // we need to refresh completelist anyway..
+ if (g_strcmp0(signal_name, "PropertiesChanged") == 0) {
+
+ (*wifiListChanged_callback)(1, "PropertiesChanged");
+ }
+ else if (g_strcmp0(signal_name, "BSSRemoved") == 0) {
+ (*wifiListChanged_callback)(2, "BSSRemoved");
+ }
+
+ else if (g_strcmp0(signal_name, "BSSAdded") == 0) {
+ (*wifiListChanged_callback)(2, "BSSAdded");
+ }
+ else WARNING(afbitf,"unhandled signal %s %s %s, %s", sender_name, object_path, interface_name, signal_name);
+
+
+}
+
+void* register_agent(void *data) {
+
+ guint owner_id;
+
+ guint networkChangedID;
+
+ introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
+ g_assert(introspection_data != NULL);
+
+ //myobj = g_object_new(my_object_get_type(), NULL);
+
+// owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, "org.agent",
+// G_BUS_NAME_OWNER_FLAGS_NONE, on_bus_acquired, on_name_acquired,
+// on_name_lost, myobj,
+// NULL);
+
+//FIXME: ALLOW_REPLACEMENT for now, make proper deinitialization
+ owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, AGENT_SERVICE,
+ G_BUS_NAME_OWNER_FLAGS_REPLACE, on_bus_acquired, NULL, NULL, NULL,
+ NULL);
+ //G_BUS_NAME_OWNER_FLAGS_NONE G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT
+
+
+ //"net.connman.Manager", "ServicesChanged",
+ networkChangedID = g_dbus_connection_signal_subscribe(connectionAgent, NULL, "fi.w1.wpa_supplicant1.Interface", NULL, NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE, test_signal_handler, NULL, NULL);
+
+ g_assert(networkChangedID !=0);
+
+ loop = g_main_loop_new(NULL, FALSE);
+
+ //sleep(10);
+ g_main_loop_run(loop);
+
+ g_bus_unown_name(owner_id);
+
+ g_dbus_node_info_unref(introspection_data);
+
+ //g_object_unref(myobj);
+
+ return NULL;
+
+}
+
+GError* create_agent(GDBusConnection *connection) {
+
+ int err = -1;
+ pthread_t tid[1];
+
+
+ connectionAgent = connection;
+
+ err = pthread_create((&tid[0]), NULL, register_agent, NULL);
+
+ if (err != 0) {
+ ERROR(afbitf,"\ncan't create thread :[%d]", err);
+ ERROR(afbitf,"Fatal error!\n\n");
+ return NULL;
+ }
+
+ GVariant *message = NULL;
+ GError *error = NULL;
+
+ GVariant *params = NULL;
+
+ char *agent_path = AGENT_PATH;
+
+ params = g_variant_new("(o)", agent_path);
+
+ message = g_dbus_connection_call_sync(connection, CONNMAN_SERVICE,
+ CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE, "RegisterAgent", params,
+ NULL, G_DBUS_CALL_FLAGS_NONE,
+ DBUS_REPLY_TIMEOUT, NULL, &error);
+
+ if (error) {
+ ERROR(afbitf,"error: %d:%s\n", error->code, error->message);
+
+ return error;
+
+ } else {
+ INFO(afbitf,"Agent registered\n");
+ return NULL;
+ }
+
+}
+
+GError* stop_agent(GDBusConnection *connection) {
+
+ GVariant *message = NULL;
+ GError *error = NULL;
+
+
+ GVariant *params = NULL;
+
+ char *agent_path = AGENT_PATH;
+
+
+ params = g_variant_new("(o)", agent_path);
+
+ message = g_dbus_connection_call_sync(connection, CONNMAN_SERVICE,
+ CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE, "UnregisterAgent", params,
+ NULL, G_DBUS_CALL_FLAGS_NONE,
+ DBUS_REPLY_TIMEOUT, NULL, &error);
+
+ if (error) {
+ ERROR(afbitf, "error: %d:%s\n", error->code, error->message);
+ return error;
+
+ } else {
+ DEBUG(afbitf,"Agent unregistered\n");
+ return NULL;
+ }
+
+}
+
+void register_callbackSecurity(callback callback_function) {
+
+ password_callback = callback_function;
+
+}
+
+void register_callbackWiFiList(callback callback_function) {
+
+ wifiListChanged_callback = callback_function;
+
+}
+
+
diff --git a/binding-wifi/binding-wifi.pro b/binding-wifi/binding-wifi.pro
new file mode 100644
index 0000000..11538e1
--- /dev/null
+++ b/binding-wifi/binding-wifi.pro
@@ -0,0 +1,11 @@
+TARGET = agl-wifi-binding
+
+HEADERS = wifi-api.h wifi-connman.h
+SOURCES = agent.c wifi-api.c wifi-connman.c
+
+LIBS += -Wl,--version-script=$$PWD/export.map
+
+CONFIG += link_pkgconfig
+PKGCONFIG += json-c afb-daemon glib-2.0 gio-2.0 gobject-2.0 zlib
+
+include(binding.pri)
diff --git a/binding-wifi/binding.pri b/binding-wifi/binding.pri
new file mode 100644
index 0000000..3448a56
--- /dev/null
+++ b/binding-wifi/binding.pri
@@ -0,0 +1,6 @@
+TEMPLATE = lib
+CONFIG += plugin use_c_linker
+CONFIG -= qt
+QMAKE_CFLAGS += -Wextra -Wconversion -Wno-unused-parameter -Werror=maybe-uninitialized -Werror=implicit-function-declaration -ffunction-sections -fdata-sections -Wl,--as-needed -Wl,--gc-sections
+
+DESTDIR = $${OUT_PWD}/../package/root/lib
diff --git a/binding-wifi/export.map b/binding-wifi/export.map
new file mode 100644
index 0000000..0ef1ac7
--- /dev/null
+++ b/binding-wifi/export.map
@@ -0,0 +1 @@
+{ global: afbBindingV1Register; local: *; };
diff --git a/binding-wifi/wifi-api.c b/binding-wifi/wifi-api.c
new file mode 100644
index 0000000..30552e8
--- /dev/null
+++ b/binding-wifi/wifi-api.c
@@ -0,0 +1,789 @@
+/* Copyright 2016 ALPS ELECTRIC CO., LTD.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+/**
+ * \file
+ *
+ * \brief Implementation of WiFi Binder for AGL's App Framework
+ *
+ * \author ALPS Electric
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <json-c/json.h>
+#include <afb/afb-binding.h>
+
+#include "wifi-api.h"
+#include "wifi-connman.h"
+
+
+
+static int need_passkey_flag = 0;
+static int passkey_not_correct_flag = 0;
+
+char *passkey;
+callback ptr_my_callback;
+callback wifiListChanged_clbck;
+
+GSList *wifi_list = NULL;
+
+
+/**
+ * \brief Input the passkey for connection to AP.
+ *
+ * \param[in] passkey pasword for the network
+ *
+ * The user should first subscribe for the event 'passkey' and then provide passkey
+ * when this event has been received.
+ *
+ * */
+static void insertpasskey(struct afb_req request) {
+
+
+ const char *passkey_from_user;
+
+ /* retrieves the argument, expects passkey string */
+ passkey_from_user = afb_req_value(request, "passkey");
+
+ NOTICE(afbitf, "Passkey inserted: %s\n", passkey_from_user);
+
+ sendPasskey(passkey_from_user);
+
+
+ if (passkey != NULL) {
+
+ registerPasskey(passkey);
+ } else {
+ NOTICE(afbitf, "Please enter the passkey first\n");
+
+ }
+}
+
+
+
+struct event
+{
+ struct event *next;
+ struct afb_event event;
+ char tag[1];
+};
+
+static struct event *events = 0;
+
+/**
+ * \internal
+ * \brief Helper function that searches for a specific event.
+ *
+ * \param[in] tag of the event
+ */
+static struct event *event_get(const char *tag)
+{
+ struct event *e = events;
+ while(e && strcmp(e->tag, tag))
+ e = e->next;
+ return e;
+}
+
+/**
+ * \internal
+ * \brief Helper function that actually pushes the event.
+ *
+ * \param[in] tag of the event
+ * \param[in] *args json object that contains data for the event
+ *
+ */
+static int do_event_push(struct json_object *args, const char *tag)
+{
+ struct event *e;
+ e = event_get(tag);
+ return e ? afb_event_push(e->event, json_object_get(args)) : -1;
+}
+
+/**
+ * \internal
+ * \brief Pushes the event of 'tag' with the 'data
+ *
+ * \param[in] tag
+ * \param[in] data
+ *
+ */
+static void eventpush (struct afb_req request)
+{
+ const char *tag = afb_req_value(request, "tag");
+ const char *data = afb_req_value(request, "data");
+ json_object *object = data ? json_tokener_parse(data) : NULL;
+
+ if (tag == NULL)
+ afb_req_fail(request, "failed", "bad arguments");
+ else if (0 > do_event_push(object, tag))
+ afb_req_fail(request, "failed", "push error");
+ else
+ afb_req_success(request, NULL, NULL);
+}
+
+/**
+ *
+ * \brief Notify user that passkey is necessary.
+ *
+ * \param[in] number additional integer data produced by Agent
+ * \param[in] asciidata additional ascii data produced by Agent
+ *
+ * This function is called from the registered agent on RequestInput() call.
+ *
+ */
+void ask_for_passkey(int number, const char* asciidata) {
+ NOTICE(afbitf, "Passkey for %s network needed.", asciidata);
+ NOTICE(afbitf, "Sending event.");
+
+ json_object *jresp = json_object_new_object();
+
+ json_object *int1 = json_object_new_int(number);
+ json_object *string = json_object_new_string(asciidata);
+
+ json_object_object_add(jresp, "data1", int1);
+ json_object_object_add(jresp, "data2", string);
+
+
+ do_event_push(jresp, "passkey");
+}
+
+/**
+ * \internal
+ * \brief Notify GUI that wifi list has changed.
+ *
+ * \param[in] number number of event that caused the callback
+ * \param[in] asciidata additional data, e.g "BSSRemoved"
+ *
+ * User should first subscribe for the event 'networkList' and then wait for this event.
+ * When notification comes, update the list if networks by scan_result call.
+ *
+ */
+void wifiListChanged(int number, const char* asciidata) {
+
+ //WARNING(afbitf, "wifiListChanged, reason:%d, %s",number, asciidata );
+
+
+ json_object *jresp = json_object_new_object();
+
+ json_object *int1 = json_object_new_int(number);
+ json_object *string = json_object_new_string(asciidata);
+
+ json_object_object_add(jresp, "data1", int1);
+ json_object_object_add(jresp, "data2", string);
+
+ do_event_push(jresp, "networkList");
+
+
+
+}
+
+
+/**
+ * \brief Initializes the binder and activates the WiFi HW, should be called first.
+ *
+ * \param[in] request no specific data needed
+ *
+ * \return result of the request, either "success" or "failed" with description of error
+ *
+ * This will fail if
+ * - agent for handling passkey requests cannot be registered
+ * - some error is returned from connman
+ */
+static void activate(struct afb_req request) /*AFB_SESSION_CHECK*/
+{
+ json_object *jresp;
+ GError *error = NULL;
+
+ if (ptr_my_callback == NULL) {
+
+ ptr_my_callback = ask_for_passkey;
+ register_callbackSecurity(ptr_my_callback);
+
+ }
+
+ if (wifiListChanged_clbck == NULL) {
+
+ wifiListChanged_clbck = wifiListChanged;
+ register_callbackWiFiList(wifiListChanged_clbck);
+
+ }
+
+ jresp = json_object_new_object();
+ json_object_object_add(jresp, "activation", json_object_new_string("on"));
+
+ error = do_wifiActivate();
+
+ if (error == NULL) {
+
+ afb_req_success(request, jresp, "Wi-Fi - Activated");
+
+ } else
+
+ afb_req_fail(request, "failed", error->message);
+
+}
+
+/**
+ * \brief Deinitializes the binder and deactivates the WiFi HW.
+ *
+ * \param[in] request no specific data needed
+ *
+ * \return result of the request, either "success" or "failed" with description of error
+ *
+ * This will fail if
+ * - agent for handling passkey requests cannot be unregistered
+ * - some error is returned from connman
+ *
+ */
+static void deactivate(struct afb_req request) /*AFB_SESSION_CHECK*/
+{
+
+ json_object *jresp;
+ GError *error = NULL;
+
+ ptr_my_callback = NULL;
+
+ jresp = json_object_new_object();
+ json_object_object_add(jresp, "deactivation", json_object_new_string("on"));
+
+ error = do_wifiDeactivate();
+
+ if (error == NULL) {
+
+ afb_req_success(request, jresp, "Wi-Fi - Activated");
+
+ } else
+
+ afb_req_fail(request, "failed", error->message);
+}
+
+/**
+ * \brief Starts scan for access points.
+ *
+ * \param[in] request no specific data needed
+ *
+ * \return result of the request, either "success" or "failed" with description of error
+ *
+ * User should first subscribe for the event 'networkList' and then wait for event to come.
+ * When notification comes, update the list of networks by scan_result call.
+ */
+static void scan(struct afb_req request) /*AFB_SESSION_NONE*/
+{
+ GError *error = NULL;
+
+ error = do_wifiScan();
+
+ if (error == NULL) {
+
+ afb_req_success(request, NULL, "Wi-Fi - Scan success");
+
+ } else
+
+ afb_req_fail(request, "failed", error->message);
+
+}
+
+/**
+ * \brief Return network list.
+ *
+ * \param[in] request no specific data needed
+ *
+ * \return result of the request, either "success" or "failed" with description of error \n
+ * result is array of following json objects: Number, Strength, ESSID, Security, IPAddress, State.
+ * E.g. {"Number":0,"Strength":82,"ESSID":"wifidata02","Security":"ieee8021x","IPAddress":"unsigned","State":"idle"}, or \n
+ * {"Number":1,"Strength":51,"ESSID":"ALCZM","Security":"WPA-PSK","IPAddress":"192.168.1.124","State":"ready"}
+ */
+static void scan_result(struct afb_req request) /*AFB_SESSION_CHECK*/
+{
+ struct wifi_profile_info *wifiProfile = NULL;
+ GSList *list = NULL;
+ GSList *holdMe = NULL;
+ wifi_list = NULL;
+ char *essid = NULL;
+ char *address = NULL;
+ char *security = NULL;
+ char *state = NULL;
+ unsigned int strength = 0;
+ int number = 0;
+ GError *error = NULL;
+
+ error = do_displayScan(&wifi_list); /*Get wifi scan list*/
+ if (error == NULL) {
+ json_object *my_array = json_object_new_array();
+
+ for (list = wifi_list; list; list = list->next) { /*extract wifi scan result*/
+ wifiProfile = (struct wifi_profile_info *) list->data;
+ security = wifiProfile->Security.sec_type;
+ strength = wifiProfile->Strength;
+ //if (essid == NULL || security == NULL)
+ // continue;
+
+ essid = wifiProfile->ESSID == NULL ?
+ "HiddenSSID" : wifiProfile->ESSID;
+ address =
+ wifiProfile->wifiNetwork.IPaddress == NULL ?
+ "unsigned" : wifiProfile->wifiNetwork.IPaddress;
+ state = wifiProfile->state;
+ //TODO: is there a case when security is NULL?
+
+ json_object *int1 = json_object_new_int(number);
+ json_object *int2 = json_object_new_int(strength);
+ json_object *jstring1 = json_object_new_string(essid);
+ json_object *jstring2 = json_object_new_string(security);
+ json_object *jstring3 = json_object_new_string(address);
+ json_object *jstring4 = json_object_new_string(state);
+
+ json_object *jresp = json_object_new_object();
+ json_object_object_add(jresp, "Number", int1);
+ json_object_object_add(jresp, "Strength", int2);
+ json_object_object_add(jresp, "ESSID", jstring1);
+ json_object_object_add(jresp, "Security", jstring2);
+ json_object_object_add(jresp, "IPAddress", jstring3);
+ json_object_object_add(jresp, "State", jstring4);
+
+ DEBUG(afbitf, "The object json: %s\n", json_object_to_json_string(jresp));
+
+ /*input each scan result into my_array*/
+ json_object_array_add(my_array, jresp);
+ number += 1;
+
+ //set the HMI icon according to strength, only if "ready" or "online"
+
+ int error = 0;
+ struct wifiStatus *status;
+
+ status = g_try_malloc0(sizeof(struct wifiStatus));
+ error = wifi_state(status); /*get current status of power and connection*/
+ if (!error) {
+
+ if (status->connected == 1) {
+
+ if ((strcmp(state, "ready") == 0)
+ || ((strcmp(state, "online") == 0))) {
+
+ if (strength < 30)
+ setHMIStatus(BAR_1);
+ else if (strength < 50)
+ setHMIStatus(BAR_2);
+ else if (strength < 70)
+ setHMIStatus(BAR_3);
+ else
+ setHMIStatus(BAR_FULL);
+ }
+ } else
+ setHMIStatus(BAR_NO);
+ }
+
+ }
+ while (list != NULL) {
+ holdMe = list->next;
+ g_free(list);
+ list = holdMe;
+ }
+ afb_req_success(request, my_array, "Wi-Fi - Scan Result is Displayed");
+ } else
+ afb_req_fail(request, "failed", error->message);
+}
+
+/**
+ * \brief Connects to a specific network.
+ *
+ * \param[in] request Number of network to connect to.
+ *
+ * \return result of the request, either "success" or "failed" with description of error
+ *
+ * Specify number of network to connect to obtained by scan_result().
+ * User should first subscribe for the event 'passkey', if passkey
+ * is needed for connection this event is pushed.
+ *
+ */
+static void connect(struct afb_req request) {
+
+ struct wifi_profile_info *wifiProfileToConnect = NULL;
+
+ const char *network;
+ int network_index = 0;
+ GError *error = NULL;
+ GSList *item = NULL;
+
+ /* retrieves the argument, expects the network number */
+ network = afb_req_value(request, "network");
+
+ if (network == NULL)
+ //TODO:better error message
+ afb_req_fail(request, "failed",
+ "specify a network number to connect to");
+
+ else {
+ network_index = atoi(network);
+ NOTICE(afbitf,"Joining network number %d\n", network_index);
+
+
+ }
+
+ //get information about desired network
+ item = g_slist_nth_data(wifi_list, network_index);
+
+ if (item == NULL) {
+ //Index starts from 1
+ ERROR(afbitf, "Network with number %d not found.\n", network_index + 1);
+ //TODO:better error message
+ afb_req_fail(request, "failed", "bad arguments");
+ }
+
+ else {
+ wifiProfileToConnect = (struct wifi_profile_info *) item;
+ INFO(afbitf, "Name: %s, strength: %d, %s\n", wifiProfileToConnect->ESSID,
+ wifiProfileToConnect->Strength,
+ wifiProfileToConnect->NetworkPath);
+ }
+ error = do_connectNetwork(wifiProfileToConnect->NetworkPath);
+
+ if (error == NULL)
+ afb_req_success(request, NULL, NULL);
+
+ else if (passkey_not_correct_flag) {
+ need_passkey_flag = 0;
+ passkey_not_correct_flag = 0;
+ afb_req_fail(request, "passkey-incorrect", NULL);
+ } else if (need_passkey_flag) {
+ need_passkey_flag = 0;
+ afb_req_fail(request, "need-passkey", NULL);
+
+ } else
+ afb_req_fail(request, "failed", error->message);
+}
+
+/**
+ * \brief Disconnects from a network.
+ *
+ * \param[in] request number of network to disconnect from
+ *
+ * \return result of the request, either "success" or "failed" with description of error
+ *
+ * Specify number of network to disconnect from obtained by scan_result().
+ */
+static void disconnect(struct afb_req request) {
+
+ struct wifi_profile_info *wifiProfileToConnect = NULL;
+
+ const char *network;
+ int network_index = 0;
+ GError *error = NULL;
+ GSList *item = NULL;
+
+ /* retrieves the argument, expects the network number */
+ network = afb_req_value(request, "network");
+
+ if (network == NULL)
+ //TODO:better error message
+ afb_req_fail(request, "failed",
+ "specify a network number to disconnect from");
+
+ else {
+ network_index = atoi(network);
+ NOTICE(afbitf, "Joining network number %d\n", network_index);
+
+ }
+
+ //get information about desired network
+ item = g_slist_nth_data(wifi_list, network_index);
+
+ if (item == NULL) {
+ //Index starts from 1
+ ERROR(afbitf,"Network with number %d not found.\n", network_index + 1);
+ //TODO:better error message
+ afb_req_fail(request, "failed", "bad arguments");
+ }
+
+ else {
+ wifiProfileToConnect = (struct wifi_profile_info *) item;
+ INFO(afbitf, "Name: %s, strength: %d, %s\n", wifiProfileToConnect->ESSID,
+ wifiProfileToConnect->Strength,
+ wifiProfileToConnect->NetworkPath);
+ }
+ error = do_disconnectNetwork(wifiProfileToConnect->NetworkPath);
+
+ if (error == NULL)
+ afb_req_success(request, NULL, NULL);
+ else
+ afb_req_fail(request, "failed", error->message);
+}
+
+/**
+ * \brief Return current status of a connection.
+ *
+ * \param[in] request no specific data needed
+ *
+ * \return result of the request, either "success" with status or "failed" with description of error
+ */
+static void status(struct afb_req request) {
+ int error = 0;
+ wifi_list = NULL;
+ struct wifiStatus *status;
+ json_object *jresp = json_object_new_object();
+
+ status = g_try_malloc0(sizeof(struct wifiStatus));
+ error = wifi_state(status); /*get current status of power and connection*/
+ if (!error) {
+ if (status->state == 0) {/*Wi-Fi is OFF*/
+ json_object_object_add(jresp, "Power",
+ json_object_new_string("OFF"));
+ //json_object_object_add(jresp, "Connection", json_object_new_string("Disconnected"));
+ DEBUG(afbitf, "Wi-Fi OFF\n");
+ } else {/*Wi-Fi is ON*/
+ json_object_object_add(jresp, "Power",
+ json_object_new_string("ON"));
+ if (status->connected == 0) {/*disconnected*/
+ json_object_object_add(jresp, "Connection",
+ json_object_new_string("Disconnected"));
+ DEBUG(afbitf, "Wi-Fi ON - Disconnected \n");
+ } else {/*Connected*/
+ json_object_object_add(jresp, "Connection",
+ json_object_new_string("Connected"));
+ DEBUG(afbitf, "Wi-Fi ON - Connected \n");
+ }
+ }
+ afb_req_success(request, jresp, "Wi-Fi - Connection Status Checked");
+ } else {
+ afb_req_fail(request, "failed", "Wi-Fi - Connection Status unknown");
+ }
+}
+
+static void reconnect() {
+ /*TBD*/
+}
+
+
+
+/**
+ * \internal
+ * \brief Helper functions that actually delete the tag.
+ *
+ * \param[in] tag tag to delete
+ *
+ * \return result of the request, either "success" or "failed" with description of error
+ */
+static int event_del(const char *tag)
+{
+ struct event *e, **p;
+
+ /* check exists */
+ e = event_get(tag);
+ if (!e) return -1;
+
+ /* unlink */
+ p = &events;
+ while(*p != e) p = &(*p)->next;
+ *p = e->next;
+
+ /* destroys */
+ afb_event_drop(e->event);
+ free(e);
+ return 0;
+}
+
+/**
+ * \internal
+ * \brief Helper functions that actually creates event of the tag.
+ *
+ * \param[in] tag tag to add
+ * \param[in] name name to add
+ *
+ * \return result of the request, either "success" or "failed" with description of error
+ */
+static int event_add(const char *tag, const char *name)
+{
+ struct event *e;
+
+ /* check valid tag */
+ e = event_get(tag);
+ if (e) return -1;
+
+ /* creation */
+ e = malloc(strlen(tag) + sizeof *e);
+ if (!e) return -1;
+ strcpy(e->tag, tag);
+
+ /* make the event */
+ e->event = afb_daemon_make_event(afbitf->daemon, name);
+ if (!e->event.closure) { free(e); return -1; }
+
+ /* link */
+ e->next = events;
+ events = e;
+ return 0;
+}
+
+/**
+ * \brief Creates event of the tag.
+ *
+ * \param[in] request tag and name of the event
+ *
+ * \return result of the request, either "success" or "failed" with description of error
+ */
+static void eventadd (struct afb_req request)
+{
+ const char *tag = afb_req_value(request, "tag");
+ const char *name = afb_req_value(request, "name");
+
+ json_object *query = afb_req_json(request);
+
+ if (tag == NULL || name == NULL)
+ afb_req_fail(request, "failed", "bad arguments");
+ else if (0 != event_add(tag, name))
+ afb_req_fail(request, "failed", "creation error");
+ else
+ afb_req_success(request, NULL, NULL);
+}
+
+/**
+ * \brief Deletes the event of tag.
+ *
+ * \param[in] request tag to delete
+ *
+ * \return result of the request, either "success" or "failed" with description of error
+ */
+static void eventdel (struct afb_req request)
+{
+ const char *tag = afb_req_value(request, "tag");
+
+ if (tag == NULL)
+ afb_req_fail(request, "failed", "bad arguments");
+ else if (0 != event_del(tag))
+ afb_req_fail(request, "failed", "deletion error");
+ else
+ afb_req_success(request, NULL, NULL);
+}
+
+
+/**
+ * \internal
+ * \brief Helper functions to subscribe for the event of tag.
+ *
+ * \param[in] request tag to subscribe for
+ *
+ * \return result of the request, either "success" or "failed" with description of error
+ */
+static int event_subscribe(struct afb_req request, const char *tag)
+{
+ struct event *e;
+ e = event_get(tag);
+ return e ? afb_req_subscribe(request, e->event) : -1;
+}
+
+/**
+ * \internal
+ * \brief Helper functions to unsubscribe for the event of tag.
+ *
+ * \param[in] request tag to unsubscribe for
+ *
+ * \return result of the request, either "success" or "failed" with description of error
+ */
+static int event_unsubscribe(struct afb_req request, const char *tag)
+{
+ struct event *e;
+ e = event_get(tag);
+ return e ? afb_req_unsubscribe(request, e->event) : -1;
+}
+
+
+/**
+ * \brief Subscribes for the event of tag.
+ *
+ * \param[in] request tag to subscribe for
+ *
+ * \return result of the request, either "success" or "failed" with description of error
+ */
+static void eventsub (struct afb_req request)
+{
+ const char *tag = afb_req_value(request, "tag");
+
+ if (tag == NULL)
+ afb_req_fail(request, "failed", "bad arguments");
+ else if (0 != event_subscribe(request, tag))
+ afb_req_fail(request, "failed", "subscription error");
+ else
+ afb_req_success(request, NULL, NULL);
+}
+
+
+/**
+ * \brief Unsubscribes for the event of tag.
+ *
+ * \param[in] request tag to unsubscribe for
+ *
+ * \return result of the request, either "success" or "failed" with description of error
+ */
+static void eventunsub (struct afb_req request)
+{
+ const char *tag = afb_req_value(request, "tag");
+
+ if (tag == NULL)
+ afb_req_fail(request, "failed", "bad arguments");
+ else if (0 != event_unsubscribe(request, tag))
+ afb_req_fail(request, "failed", "unsubscription error");
+ else
+ afb_req_success(request, NULL, NULL);
+}
+
+
+/*
+ * array of the verbs exported to afb-daemon
+ */
+static const struct afb_verb_desc_v1 binding_verbs[] = {
+/* VERB'S NAME SESSION MANAGEMENT FUNCTION TO CALL SHORT DESCRIPTION */
+{ .name = "activate", .session = AFB_SESSION_NONE, .callback = activate, .info = "Activate Wi-Fi" },
+{ .name = "deactivate", .session = AFB_SESSION_NONE, .callback = deactivate, .info ="Deactivate Wi-Fi" },
+{ .name = "scan", .session = AFB_SESSION_NONE, .callback = scan, .info ="Scanning Wi-Fi" },
+{ .name = "scan_result", .session = AFB_SESSION_NONE, .callback = scan_result, .info = "Get scan result Wi-Fi" },
+{ .name = "connect", .session = AFB_SESSION_NONE, .callback = connect, .info ="Connecting to Access Point" },
+{ .name = "status", .session = AFB_SESSION_NONE, .callback = status, .info ="Check connection status" },
+{ .name = "disconnect", .session = AFB_SESSION_NONE, .callback = disconnect, .info ="Disconnecting connection" },
+{ .name = "reconnect", .session = AFB_SESSION_NONE, .callback = reconnect, .info ="Reconnecting to Access Point" },
+{ .name = "insertpasskey",.session = AFB_SESSION_NONE, .callback = insertpasskey, .info ="inputs the passkey after it has been requsted"},
+{ .name = "eventadd", .session = AFB_SESSION_NONE, .callback = eventadd, .info ="adds the event of 'name' for the 'tag'"},
+{ .name = "eventsub", .session = AFB_SESSION_NONE, .callback = eventsub, .info ="unsubscribes to the event of 'tag'"},
+{ .name = "eventpush", .session = AFB_SESSION_NONE, .callback = eventpush, .info ="pushes the event of 'tag' with the 'data'"},
+{ .name = "eventunsub", .session = AFB_SESSION_NONE, .callback = eventunsub, .info ="unsubscribes to the event of 'tag'"},
+{ .name = "eventdel", .session = AFB_SESSION_NONE, .callback = eventdel, .info ="deletes the event of 'tag'"},
+
+{ .name = NULL } /* marker for end of the array */
+};
+
+/*
+ * description of the binding for afb-daemon
+ */
+static const struct afb_binding binding_description = {
+/* description conforms to VERSION 1 */
+.type = AFB_BINDING_VERSION_1, .v1 = { /* fills the v1 field of the union when AFB_BINDING_VERSION_1 */
+.prefix = "wifi-manager", /* the API name (or binding name or prefix) */
+.info = "wifi API", /* short description of of the binding */
+.verbs = binding_verbs /* the array describing the verbs of the API */
+} };
+
+/*
+ * activation function for registering the binding called by afb-daemon
+ */
+const struct afb_binding *afbBindingV1Register(
+ const struct afb_binding_interface *itf) {
+ afbitf = itf; // records the interface for accessing afb-daemon
+ return &binding_description; // returns the description of the binding
+}
+
diff --git a/binding-wifi/wifi-api.h b/binding-wifi/wifi-api.h
new file mode 100644
index 0000000..429211b
--- /dev/null
+++ b/binding-wifi/wifi-api.h
@@ -0,0 +1,38 @@
+/* Copyright 2016 ALPS ELECTRIC CO., LTD.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#ifndef WIFI_API_H
+#define WIFI_API_H
+
+/* global plugin handle, should store everything we may need */
+typedef struct {
+ int devCount;
+} pluginHandleT;
+
+/* private client context [will be destroyed when client leaves] */
+typedef struct {
+ unsigned char activate;
+ unsigned char connected;
+} wifiCtxHandleT;
+
+
+struct scan_list_info {
+ int number;
+ char *SSID;
+ char *Security;
+ int Strength;
+};
+
+#endif /* AUDIO_API_H */
diff --git a/binding-wifi/wifi-connman.c b/binding-wifi/wifi-connman.c
new file mode 100644
index 0000000..924898e
--- /dev/null
+++ b/binding-wifi/wifi-connman.c
@@ -0,0 +1,396 @@
+/* Copyright 2016 ALPS ELECTRIC CO., LTD.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <glib.h>
+//#include <dbus/dbus.h>
+#include <gio/gio.h>
+#include <glib-object.h>
+
+#include "wifi-api.h"
+#include "wifi-connman.h"
+
+static __thread struct security_profile Security = { NULL, NULL, NULL, NULL, 0,
+ 0 };
+
+int extract_values(GVariantIter *content, struct wifi_profile_info* wifiProfile) {
+ GVariant *var = NULL;
+ GVariant *subvar = NULL;
+ GVariantIter *array;
+ const gchar *key = NULL;
+ const gchar *subkey = NULL;
+ const gchar *value_char = NULL;
+ GVariantIter *content_sub;
+ unsigned int value_int;
+ gsize length;
+
+ while (g_variant_iter_loop(content, "{sv}", &key, &var)) {
+ if (g_strcmp0(key, "Name") == 0) {
+ value_char = g_variant_get_string(var, &length);
+ wifiProfile->ESSID = (char *) value_char;
+ } else if (g_strcmp0(key, "Security") == 0) {
+ g_variant_get(var, "as", &content_sub);
+ while (g_variant_iter_loop(content_sub, "s", &value_char)) {
+ if (g_strcmp0(value_char, "none") == 0)
+ wifiProfile->Security.sec_type = "Open";
+ else if (g_strcmp0(value_char, "wep") == 0)
+ wifiProfile->Security.sec_type = "WEP";
+ else if (g_strcmp0(value_char, "psk") == 0)
+ wifiProfile->Security.sec_type = "WPA-PSK";
+ else if (g_strcmp0(value_char, "ieee8021x") == 0)
+ wifiProfile->Security.sec_type = "ieee8021x";
+ else if (g_strcmp0(value_char, "wpa") == 0)
+ wifiProfile->Security.sec_type = "WPA-PSK";
+ else if (g_strcmp0(value_char, "rsn") == 0)
+ wifiProfile->Security.sec_type = "WPA2-PSK";
+ else if (g_strcmp0(value_char, "wps") == 0)
+ wifiProfile->Security.wps_support = 1;
+ else
+ Security.sec_type = "Open";
+ }
+ } else if (g_strcmp0(key, "Strength") == 0) {
+ value_int = (unsigned int) g_variant_get_byte(var);
+ wifiProfile->Strength = value_int;
+ } else if (g_strcmp0(key, "State") == 0) {
+ value_char = g_variant_get_string(var, &length);
+ wifiProfile->state = (char *) value_char;
+ } else if (g_strcmp0(key, "IPv4") == 0) {
+ g_variant_get(var, "a{sv}", &array);
+ while (g_variant_iter_loop(array, "{sv}", &subkey, &subvar)) {
+ if (g_strcmp0(subkey, "Method") == 0) {
+ value_char = g_variant_get_string(subvar, &length);
+ if (g_strcmp0(value_char, "dhcp") == 0)
+ wifiProfile->wifiNetwork.method = "dhcp";
+ else if (g_strcmp0(value_char, "manual") == 0)
+ wifiProfile->wifiNetwork.method = "manual";
+ else if (g_strcmp0(value_char, "fixed") == 0)
+ wifiProfile->wifiNetwork.method = "fix";
+ else if (g_strcmp0(value_char, "off") == 0)
+ wifiProfile->wifiNetwork.method = "off";
+ } else if (g_strcmp0(subkey, "Address") == 0) {
+ value_char = g_variant_get_string(subvar, &length);
+ wifiProfile->wifiNetwork.IPaddress = (char *) value_char;
+ } else if (g_strcmp0(subkey, "Netmask") == 0) {
+ value_char = g_variant_get_string(subvar, &length);
+ wifiProfile->wifiNetwork.netmask = (char *) value_char;
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+int wifi_state(struct wifiStatus *status) {
+ GError *error = NULL;
+ GVariant *message = NULL;
+ GVariantIter *array;
+ GDBusConnection *connection;
+ GVariant *var = NULL;
+ const gchar *key = NULL;
+ gboolean value_bool;
+
+ connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+ if (connection == NULL) {
+ ERROR(afbitf,"Cannot connect to D-Bus, %s", error->message);
+ return -1;
+ }
+ message = g_dbus_connection_call_sync(connection, CONNMAN_SERVICE,
+ CONNMAN_TECHNOLOGY_PATH, CONNMAN_TECHNOLOGY_INTERFACE, "GetProperties",
+ NULL, NULL, G_DBUS_CALL_FLAGS_NONE,
+ DBUS_REPLY_TIMEOUT, NULL, &error);
+ if (message == NULL) {
+ ERROR(afbitf,"Error %s while calling GetProperties method", error->message);
+ return -1;
+ }
+ g_variant_get(message, "(a{sv})", &array);
+ while (g_variant_iter_loop(array, "{sv}", &key, &var)) {
+ if (g_strcmp0(key, "Powered") == 0) {
+ value_bool = g_variant_get_boolean(var);
+ if (value_bool)
+ status->state = 1;
+ else
+ status->state = 0;
+ } else if (g_strcmp0(key, "Connected") == 0) {
+ value_bool = g_variant_get_boolean(var);
+ if (value_bool)
+ status->connected = 1;
+ else
+ status->connected = 0;
+ }
+ }
+ g_variant_iter_free(array);
+ g_variant_unref(message);
+
+ return 0;
+}
+
+GError* do_wifiActivate() {
+ GVariant *params = NULL;
+ params = g_variant_new("(sv)", "Powered", g_variant_new_boolean(TRUE));
+ GDBusConnection *connection;
+ GError *error = NULL;
+
+ connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+
+ if (connection == NULL) {
+ ERROR(afbitf,"Cannot connect to D-Bus, %s", error->message);
+ return error;
+ }
+
+ //create the agent to handle security
+ error = create_agent(connection);
+
+ if (error)
+ //This is fatal error, without agent secured networks can not be handled
+ return error;
+
+ g_dbus_connection_call(connection, CONNMAN_SERVICE,
+ CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE, "SetProperty",
+ params, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, &error);
+
+ if (error) {
+ ERROR(afbitf,"Error %s while calling SetProperty method", error->message);
+
+ return error;
+ }
+
+ else {
+ NOTICE(afbitf,"Power ON succeeded\n");
+ return NULL;
+ }
+
+}
+
+GError* do_wifiDeactivate() {
+ GVariant *params = NULL;
+ params = g_variant_new("(sv)", "Powered", g_variant_new_boolean(FALSE));
+ GDBusConnection *connection;
+ GError *error = NULL;
+
+ /*connection = gdbus_conn->connection;*/
+ connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+ if (connection == NULL) {
+ ERROR(afbitf,"Cannot connect to D-Bus, %s", error->message);
+ return error;
+ }
+
+ //create the agent to handle security
+ error = stop_agent(connection);
+
+ if (error) {
+ WARNING(afbitf, "Error while unregistering the agent, ignoring.");
+
+ }
+
+ g_dbus_connection_call(connection, CONNMAN_SERVICE,
+ CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE, "SetProperty",
+ params, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, &error);
+
+ if (error) {
+ ERROR(afbitf,"Error %s while calling SetProperty method", error->message);
+
+ return error;
+ }
+
+ else {
+ NOTICE(afbitf, "Power OFF succeeded\n");
+ return NULL;
+ }
+}
+
+GError* do_wifiScan() {
+ GDBusConnection *connection;
+ GError *error = NULL;
+
+ /*connection = gdbus_conn->connection;*/
+ connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+ if (connection == NULL) {
+ ERROR(afbitf,"Cannot connect to D-Bus, %s", error->message);
+ return error;
+ }
+
+ g_dbus_connection_call(connection, CONNMAN_SERVICE,
+ CONNMAN_WIFI_TECHNOLOGY_PREFIX, CONNMAN_TECHNOLOGY_INTERFACE, "Scan", NULL,
+ NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_REPLY_TIMEOUT, NULL, NULL, &error);
+ if (error) {
+ ERROR(afbitf,"Error %s while calling Scan method", error->message);
+
+ return error;
+ }
+
+ else {
+ INFO(afbitf, "Scan succeeded\n");
+ return NULL;
+ }
+}
+
+GError* do_displayScan(GSList **wifi_list) {
+ GError *error = NULL;
+ GVariant *message = NULL;
+ GVariantIter *array;
+ gchar *object;
+ GVariantIter *content = NULL;
+ GDBusConnection *connection;
+ struct wifi_profile_info *wifiProfile = NULL;
+
+ /*connection = gdbus_conn->connection;*/
+ connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+ if (connection == NULL) {
+ ERROR(afbitf,"Cannot connect to D-Bus, %s", error->message);
+ return error;
+ }
+ message = g_dbus_connection_call_sync(connection, CONNMAN_SERVICE,
+ CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE, "GetServices", NULL, NULL,
+ G_DBUS_CALL_FLAGS_NONE,
+ DBUS_REPLY_TIMEOUT, NULL, &error);
+ if (message == NULL) {
+ ERROR(afbitf,"Error %s while calling GetServices method", error->message);
+ return error;
+ }
+ g_variant_get(message, "(a(oa{sv}))", &array);
+ while (g_variant_iter_loop(array, "(oa{sv})", &object, &content)) {
+ if (g_str_has_prefix(object,
+ CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
+ wifiProfile = g_try_malloc0(sizeof(struct wifi_profile_info));
+
+ extract_values(content, wifiProfile);
+ wifiProfile->NetworkPath = g_try_malloc0(strlen(object));
+ strcpy(wifiProfile->NetworkPath, object);
+ DEBUG(afbitf,
+ "SSID= %s, security= %s, path= %s, Strength= %d, wps support= %d\n",
+ wifiProfile->ESSID, wifiProfile->Security.sec_type,
+ wifiProfile->NetworkPath, wifiProfile->Strength,
+ wifiProfile->Security.wps_support);
+ DEBUG(afbitf, "method= %s, ip address= %s, netmask= %s\n",
+ wifiProfile->wifiNetwork.method,
+ wifiProfile->wifiNetwork.IPaddress,
+ wifiProfile->wifiNetwork.netmask);
+ *wifi_list = g_slist_append(*wifi_list,
+ (struct wifi_profile_info *) wifiProfile);
+ }
+ }
+ g_variant_iter_free(array);
+
+ return NULL;
+}
+
+GError* do_connectNetwork(gchar *networkPath) {
+
+ NOTICE(afbitf, "Connecting to: %s\n", networkPath);
+
+ GVariant *message = NULL;
+ GError *error = NULL;
+ GDBusConnection *connection;
+
+ connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+
+ message = g_dbus_connection_call_sync(connection, CONNMAN_SERVICE,
+ networkPath, CONNMAN_SERVICE_INTERFACE, "Connect", NULL, NULL,
+ G_DBUS_CALL_FLAGS_NONE,
+ DBUS_REPLY_TIMEOUT_SHORT, NULL, &error);
+
+ //TODO: do we need retunrn value in message
+
+ if (error) {
+ ERROR(afbitf,"Error %s while calling Connect method", error->message);
+ return error;
+ } else {
+ INFO(afbitf,"Connection succeeded\n");
+ return NULL;
+ }
+
+}
+
+GError* do_disconnectNetwork(gchar *networkPath) {
+
+ NOTICE(afbitf, "Connecting to: %s\n", networkPath);
+
+ GVariant *message = NULL;
+ GError *error = NULL;
+ GDBusConnection *connection;
+
+ connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+
+ if (connection == NULL) {
+ ERROR(afbitf,"Cannot connect to D-Bus, %s", error->message);
+ return error;
+ }
+
+ message = g_dbus_connection_call_sync(connection, CONNMAN_SERVICE,
+ networkPath, CONNMAN_SERVICE_INTERFACE, "Disconnect", NULL, NULL,
+ G_DBUS_CALL_FLAGS_NONE,
+ DBUS_REPLY_TIMEOUT, NULL, &error);
+
+ //TODO: do we need return value in message
+
+ if (error) {
+ ERROR(afbitf,"Error %s while calling Disconnect method", error->message);
+ return error;
+ } else {
+ INFO(afbitf,"Disconnection succeeded\n");
+ return NULL;
+ }
+
+}
+
+void registerPasskey(gchar *passkey) {
+
+ INFO(afbitf,"Passkey: %s\n", passkey);
+ sendPasskey(passkey);
+
+
+}
+
+GError* setHMIStatus(enum wifiStates state) {
+
+ gchar *iconString = NULL;
+ GDBusConnection *connection;
+ GVariant *params = NULL;
+ GVariant *message = NULL;
+ GError *error = NULL;
+
+ if (state==BAR_NO) iconString = "qrc:/images/Status/HMI_Status_Wifi_NoBars-01.png";
+ else if (state==BAR_1) iconString = "qrc:/images/Status/HMI_Status_Wifi_1Bar-01.png";
+ else if (state==BAR_2) iconString = "qrc:/images/Status/HMI_Status_Wifi_2Bars-01.png";
+ else if (state==BAR_3) iconString = "qrc:/images/Status/HMI_Status_Wifi_3Bars-01.png";
+ else if (state==BAR_FULL) iconString = "qrc:/images/Status/HMI_Status_Wifi_Full-01.png";
+
+ connection = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
+
+ if (connection == NULL) {
+ ERROR(afbitf,"Cannot connect to D-Bus, %s", error->message);
+ return error;
+ }
+
+ params = g_variant_new("(is)", HOMESCREEN_WIFI_ICON_POSITION, iconString);
+
+ message = g_dbus_connection_call_sync(connection, HOMESCREEN_SERVICE,
+ HOMESCREEN_ICON_PATH, HOMESCREEN_ICON_INTERFACE, "setStatusIcon", params,
+ NULL, G_DBUS_CALL_FLAGS_NONE,
+ DBUS_REPLY_TIMEOUT, NULL, &error);
+
+ if (error) {
+ ERROR(afbitf,"Error %s while setting up the status icon", error->message);
+
+ return error;
+ } else {
+ return NULL;
+ }
+
+}
+
diff --git a/binding-wifi/wifi-connman.h b/binding-wifi/wifi-connman.h
new file mode 100644
index 0000000..dfc0d5b
--- /dev/null
+++ b/binding-wifi/wifi-connman.h
@@ -0,0 +1,148 @@
+/* Copyright 2016 ALPS ELECTRIC CO., LTD.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#define _GNU_SOURCE
+
+//#include <dlog.h>
+#include <glib.h>
+#include <stdlib.h>
+#include <gio/gio.h>
+#include <glib-object.h>
+
+#include <afb/afb-binding.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//
+
+
+/*
+ * the interface to afb-daemon
+ */
+const struct afb_binding_interface *afbitf;
+
+/** Maximum Profile Count */
+#define CONNMAN_MAX_BUFLEN 512
+
+#define CONNMAN_STATE_STRLEN 16
+
+#define CONNMAN_SERVICE "net.connman"
+#define CONNMAN_MANAGER_INTERFACE CONNMAN_SERVICE ".Manager"
+#define CONNMAN_TECHNOLOGY_INTERFACE CONNMAN_SERVICE ".Technology"
+#define CONNMAN_SERVICE_INTERFACE CONNMAN_SERVICE ".Service"
+#define CONNMAN_PROFILE_INTERFACE CONNMAN_SERVICE ".Profile"
+#define CONNMAN_COUNTER_INTERFACE CONNMAN_SERVICE ".Counter"
+#define CONNMAN_ERROR_INTERFACE CONNMAN_SERVICE ".Error"
+#define CONNMAN_AGENT_INTERFACE CONNMAN_SERVICE ".Agent"
+
+#define CONNMAN_MANAGER_PATH "/"
+#define CONNMAN_PATH "/net/connman"
+#define CONNMAN_TECHNOLOGY_PATH "/net/connman/technology/wifi"
+
+/** ConnMan technology and profile prefixes for ConnMan 0.78 */
+
+#define CONNMAN_WIFI_TECHNOLOGY_PREFIX CONNMAN_PATH "/technology/wifi"
+#define CONNMAN_WIFI_SERVICE_PROFILE_PREFIX CONNMAN_PATH "/service/wifi_"
+
+#define WIFI_ESSID_LEN 128
+#define WIFI_MAX_WPSPIN_LEN 8
+#define WIFI_BSSID_LEN 17
+#define WIFI_MAX_PSK_PASSPHRASE_LEN 65
+#define WIFI_MAX_WEP_KEY_LEN 26
+
+#define HOMESCREEN_SERVICE "org.agl.homescreen"
+#define HOMESCREEN_ICON_INTERFACE "org.agl.statusbar"
+#define HOMESCREEN_ICON_PATH "/StatusBar"
+#define HOMESCREEN_WIFI_ICON_POSITION 0
+
+
+#define AGENT_PATH "/net/connman/Agent"
+#define AGENT_SERVICE "org.agent"
+
+#define DBUS_REPLY_TIMEOUT (120 * 1000)
+#define DBUS_REPLY_TIMEOUT_SHORT (10 * 1000)
+
+
+struct gdbus_connection_data_s{
+ GDBusConnection *connection;
+ int conn_ref_count;
+ GCancellable *cancellable;
+ void *handle_libnetwork;
+};
+
+
+struct wifiStatus {
+ unsigned int state;
+ unsigned int connected;
+};
+
+struct security_profile{
+ char *sec_type;
+ char *enc_type;
+ char *wepKey;
+ char *pskKey;
+ unsigned int PassphraseRequired;
+ unsigned int wps_support;
+};
+
+struct wifi_net{
+ char *method;
+ char *IPaddress;
+ char *netmask;
+};
+
+struct wifi_profile_info{
+ char *ESSID;
+ char *NetworkPath;
+ char *state;
+ unsigned int Strength;
+ struct security_profile Security;
+ struct wifi_net wifiNetwork;
+};
+
+enum wifiStates {BAR_NO, BAR_1, BAR_2, BAR_3, BAR_FULL};
+
+typedef void(*callback)(int number, const char* asciidata);
+void register_callbackSecurity(callback ptr);
+void register_callbackWiFiList(callback ptr);
+
+
+
+int extract_values(GVariantIter *content, struct wifi_profile_info* wifiProfile);
+int wifi_state(struct wifiStatus *status);
+GError* do_wifiActivate();
+GError* do_wifiDeactivate();
+GError* do_wifiScan();
+GError* do_displayScan(GSList **wifi_list);
+GError* do_connectNetwork(gchar *object);
+GError* do_disconnectNetwork(gchar *object);
+
+GError* create_agent(GDBusConnection *connection);
+GError* stop_agent(GDBusConnection *connection);
+
+GError* setHMIStatus(enum wifiStates);
+
+void registerPasskey(gchar *object);
+GError* sendPasskey(gchar *object);
+
+GError* do_initialize();
+GError* do_initialize();
+
+
+
+
+