netutils: save wapi config to file system

save and reload the wapi config from file system
This commit is contained in:
chao.an 2020-02-24 11:31:38 +08:00 committed by Ouss4
parent 86bfe13399
commit 52ad01f0bf
4 changed files with 416 additions and 18 deletions

View File

@ -90,9 +90,15 @@
*/
#ifdef CONFIG_NET_UDP
# define SOCK_WAPI SOCK_DGRAM
# define SOCK_WAPI SOCK_DGRAM
#else
# define SOCK_WAPI SOCK_STREAM
# define SOCK_WAPI SOCK_STREAM
#endif
#ifndef CONFIG_WIRELESS_WAPI_INITCONF
# define wapi_load_config(ifname, confname, conf) NULL
# define wapi_unload_config(load)
# define wapi_save_config(ifname, confname, conf) 0
#endif
/****************************************************************************
@ -664,6 +670,53 @@ int wapi_scan_stat(int sock, FAR const char *ifname);
int wapi_scan_coll(int sock, FAR const char *ifname,
FAR struct wapi_list_s *aps);
#ifdef CONFIG_WIRELESS_WAPI_INITCONF
/****************************************************************************
* Name: wapi_load_config
*
* Description:
*
* Input Parameters:
*
* Returned Value:
* Return a pointer to the hold the config resource, NULL On error.
*
****************************************************************************/
FAR void *wapi_load_config(FAR const char *ifname,
FAR const char *confname,
FAR struct wpa_wconfig_s *conf);
/****************************************************************************
* Name: wapi_unload_config
*
* Description:
*
* Input Parameters:
* load - Config resource handler, allocate by wapi_load_config()
*
* Returned Value:
*
****************************************************************************/
void wapi_unload_config(FAR void *load);
/****************************************************************************
* Name: wapi_save_config
*
* Description:
*
* Input Parameters:
*
* Returned Value:
*
****************************************************************************/
int wapi_save_config(FAR const char *ifname,
FAR const char *confname,
FAR const struct wpa_wconfig_s *conf);
#endif
/****************************************************************************
* Name: wpa_driver_wext_set_key_ext
*

View File

@ -59,27 +59,28 @@
int netinit_associate(FAR const char *ifname)
{
struct wpa_wconfig_s wconfig;
struct wpa_wconfig_s conf;
int ret;
FAR void *load;
/* Set up the network configuration */
load = wapi_load_config(ifname, NULL, &conf);
if (!load)
{
conf.ifname = ifname;
conf.sta_mode = CONFIG_NETINIT_WAPI_STAMODE;
conf.auth_wpa = CONFIG_NETINIT_WAPI_AUTHWPA;
conf.cipher_mode = CONFIG_NETINIT_WAPI_CIPHERMODE;
conf.alg = CONFIG_NETINIT_WAPI_ALG;
conf.ssid = CONFIG_NETINIT_WAPI_SSID;
conf.passphrase = CONFIG_NETINIT_WAPI_PASSPHRASE;
conf.ssidlen = strlen(conf.ssid);
conf.phraselen = strlen(conf.passphrase);
}
wconfig.sta_mode = CONFIG_NETINIT_WAPI_STAMODE;
wconfig.auth_wpa = CONFIG_NETINIT_WAPI_AUTHWPA;
wconfig.cipher_mode = CONFIG_NETINIT_WAPI_CIPHERMODE;
wconfig.alg = CONFIG_NETINIT_WAPI_ALG;
wconfig.ssid = CONFIG_NETINIT_WAPI_SSID;
wconfig.passphrase = CONFIG_NETINIT_WAPI_PASSPHRASE;
wconfig.ifname = ifname;
ret = wpa_driver_wext_associate(&conf);
wconfig.ssidlen = strlen(wconfig.ssid);
wconfig.phraselen = strlen(wconfig.passphrase);
wapi_unload_config(load);
/* Associate */
sleep(2);
ret = wpa_driver_wext_associate(&wconfig);
sleep(2);
return ret;
}

View File

@ -36,4 +36,17 @@ config WIRELESS_WAPI_PRIORITY
int "Command Priority"
default 100
config WIRELESS_WAPI_INITCONF
bool "Wireless Configure Initialization"
default n
depends on NETUTILS_CJSON
if WIRELESS_WAPI_INITCONF
config WIRELESS_WAPI_CONFIG_PATH
string "Wireless Wapi Configure File Path"
default "/data/wapi.conf"
endif
endif

View File

@ -39,14 +39,21 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include "wireless/wapi.h"
#include "util.h"
#ifdef CONFIG_WIRELESS_WAPI_INITCONF
#include "netutils/cJSON.h"
#endif /* CONFIG_WIRELESS_WAPI_INITCONF */
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@ -55,6 +62,99 @@
#define WAPI_IOCTL_COMMAND_NAMEBUFSIZ 24
/****************************************************************************
* Private Functions
****************************************************************************/
#ifdef CONFIG_WIRELESS_WAPI_INITCONF
static FAR void *wapi_json_load(FAR const char *confname)
{
FAR cJSON *root = NULL;
struct stat sb;
FAR char *buf;
int fd = -1;
if (stat(confname, &sb) < 0)
{
return NULL;
}
buf = malloc(sb.st_size);
if (!buf)
{
goto errout;
}
fd = open(confname, O_RDONLY);
if (fd < 0)
{
goto errout;
}
if (read(fd, buf, sb.st_size) != sb.st_size)
{
goto errout;
}
root = cJSON_Parse(buf);
errout:
if (buf)
{
free(buf);
}
if (fd > 0)
{
close(fd);
}
return root;
}
static bool wapi_json_update(FAR cJSON *root,
FAR const char *key,
FAR void *value,
bool integer)
{
intptr_t intval = (intptr_t)value;
FAR cJSON *item;
FAR cJSON *obj;
obj = cJSON_GetObjectItem(root, key);
if (obj)
{
if (integer)
{
if (intval == obj->valueint)
{
return false;
}
item = cJSON_CreateNumber(intval);
}
else
{
if (!strncmp(value, obj->valuestring, strlen(obj->valuestring)))
{
return false;
}
item = cJSON_CreateString(value);
}
cJSON_ReplaceItemInObject(root, key, item);
}
else
{
integer ? cJSON_AddNumberToObject(root, key, intval) :
cJSON_AddStringToObject(root, key, value);
}
return true;
}
#endif /* CONFIG_WIRELESS_WAPI_INITCONF */
/****************************************************************************
* Public Functions
****************************************************************************/
@ -159,3 +259,234 @@ FAR const char *wapi_ioctl_command_name(int cmd)
return g_ioctl_command_namebuf;
}
}
#ifdef CONFIG_WIRELESS_WAPI_INITCONF
/****************************************************************************
* Name: wapi_unload_config
*
* Description:
*
* Input Parameters:
* load - Config resource handler, allocate by wapi_load_config()
*
* Returned Value:
*
****************************************************************************/
void wapi_unload_config(FAR void *load)
{
if (load)
{
cJSON_Delete(load);
}
}
/****************************************************************************
* Name: wapi_load_config
*
* Description:
*
* Input Parameters:
*
* Returned Value:
* Return a pointer to the hold the config resource, NULL On error.
*
****************************************************************************/
FAR void *wapi_load_config(FAR const char *ifname,
FAR const char *confname,
FAR struct wpa_wconfig_s *conf)
{
FAR cJSON *ifobj;
FAR cJSON *root;
FAR cJSON *obj;
if (ifname == NULL ||
conf == NULL)
{
return NULL;
}
if (confname == NULL)
{
confname = CONFIG_WIRELESS_WAPI_CONFIG_PATH;
}
root = wapi_json_load(confname);
if (!root)
{
return NULL;
}
/* Set up the network configuration */
ifobj = cJSON_GetObjectItem(root, ifname);
if (!ifobj)
{
goto errout;
}
obj = cJSON_GetObjectItem(ifobj, "mode");
if (!obj)
{
goto errout;
}
conf->sta_mode = obj->valueint;
obj = cJSON_GetObjectItem(ifobj, "auth");
if (!obj)
{
goto errout;
}
conf->auth_wpa = obj->valueint;
obj = cJSON_GetObjectItem(ifobj, "cmode");
if (!obj)
{
goto errout;
}
conf->cipher_mode = obj->valueint;
obj = cJSON_GetObjectItem(ifobj, "alg");
if (!obj)
{
goto errout;
}
conf->alg = obj->valueint;
obj = cJSON_GetObjectItem(ifobj, "ssid");
if (!obj || !obj->valuestring)
{
goto errout;
}
conf->ssid = (FAR const char *)obj->valuestring;
obj = cJSON_GetObjectItem(ifobj, "psk");
if (!obj || !obj->valuestring)
{
goto errout;
}
conf->passphrase = (FAR const char *)obj->valuestring;
conf->ifname = ifname;
conf->ssidlen = strlen(conf->ssid);
conf->phraselen = strlen(conf->passphrase);
return root;
errout:
cJSON_Delete(root);
return NULL;
}
/****************************************************************************
* Name: wapi_save_config
*
* Description:
*
* Input Parameters:
*
* Returned Value:
*
****************************************************************************/
int wapi_save_config(FAR const char *ifname,
FAR const char *confname,
FAR const struct wpa_wconfig_s *conf)
{
FAR char *buf = NULL;
FAR cJSON *ifobj;
FAR cJSON *root;
int ret = -1;
int fd = -1;
bool update;
if (ifname == NULL || conf == NULL)
{
return ret;
}
if (confname == NULL)
{
confname = CONFIG_WIRELESS_WAPI_CONFIG_PATH;
}
root = wapi_json_load(confname);
if (!root)
{
root = cJSON_CreateObject();
if (root == NULL)
{
return ret;
}
}
ifobj = cJSON_GetObjectItem(root, ifname);
if (!ifobj)
{
ifobj = cJSON_CreateObject();
if (ifobj == NULL)
{
goto errout;
}
cJSON_AddItemToObject(root, ifname, ifobj);
}
update = wapi_json_update(ifobj, "mode",
(FAR void *)(intptr_t)conf->sta_mode, true);
update |= wapi_json_update(ifobj, "auth",
(FAR void *)(intptr_t)conf->auth_wpa, true);
update |= wapi_json_update(ifobj, "cmode",
(FAR void *)(intptr_t)conf->cipher_mode, true);
update |= wapi_json_update(ifobj, "alg",
(FAR void *)(intptr_t)conf->alg, true);
update |= wapi_json_update(ifobj, "ssid",
(FAR void *)conf->ssid, false);
update |= wapi_json_update(ifobj, "psk",
(FAR void *)conf->passphrase, false);
if (!update)
{
ret = OK;
goto errout;
}
buf = cJSON_PrintUnformatted(root);
if (!buf)
{
goto errout;
}
fd = open(confname, O_RDWR | O_CREAT | O_TRUNC);
if (fd < 0)
{
goto errout;
}
ret = write(fd, buf, strlen(buf));
errout:
if (buf)
{
free(buf);
}
if (fd > 0)
{
close(fd);
}
cJSON_Delete(root);
return ret < 0 ? ret : OK;
}
#endif /* CONFIG_WIRELESS_WAPI_INITCONF */