/* 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 #include #include #include #include #include #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_ob
/*
 * Copyright (C) 2015, 2016 "IoT.bzh"
 * Author "Fulup Ar Foll"
 *
 * 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 <stdio.h>
#include <json-c/json.h>

#include <afb/afb-plugin.h>

// Dummy sample of Client Application Context
typedef struct {
  int  something;       
  void *whateveryouwant;
} MyClientApplicationHandle;


// This function is call when Client Session Context is removed
// Note: when freeCtxCB==NULL standard free/malloc is called
static void clientContextFree(void *context) {
    fprintf (stderr,"Plugin[token] Closing Session\n");
    free (context);
}

// Request Creation of new context if it does not exist
static void clientContextCreate (struct afb_req request)
{
    json_object *jresp;

    // add an application specific client context to session
    afb_req_context_set(request, malloc (sizeof (MyClientApplicationHandle)), clientContextFree);
    
    // Send response to UI
    jresp = json_object_new_object();               
    json_object_object_add(jresp, "token", json_object_new_string ("A New Token and Session Context Was Created"));

    afb_req_success(request, jresp, NULL);
}

// Before entering here token will be check and renew
static void clientContextRefresh (struct afb_req request) {
    json_object *jresp