delete config file if completed.
add each flag of configuration items because wifi has async return.
Signed-off-by: jiehwan.park <jiehwan.park@samsung.com>
SmackProcessLabel=System
Type=notify
ExecStart=/usr/bin/setup-adaptor
-Restart=on-failure
+Restart=no
RestartSec=0
[Install]
return TRUE;
}
-gboolean setup(gpointer data)
+gboolean setup_cb(gpointer data)
{
sa_error_e ret = SA_ERROR_NONE;
sa_config_s config = {0,};
sa_error_e file_read = SA_ERROR_NONE;
- _D("callback>>>setup start !!!");
+ _D("callback>>>setup_cb start !!!");
file_read = sa_inputfile_get_config_info(&config);
if (file_read == SA_ERROR_NONE) {
- if (config.systemData != NULL) {
+ _D("flag_completion(system=%d)(eth=%d)(wifi=%d)", sa_inputfile_get_completion_flag(SA_FILE_CONFIG_SYSTEM)
+ , sa_inputfile_get_completion_flag(SA_FILE_CONFIG_ETHERNET)
+ , sa_inputfile_get_completion_flag(SA_FILE_CONFIG_WIFI) );
+
+ if (config.systemData != NULL && !sa_inputfile_get_completion_flag(SA_FILE_CONFIG_SYSTEM) ) {
ret = sa_setup_system(config.systemData);
if(ret != SA_ERROR_NONE) {
_E("sa_setup_system return error(%d)", ret);
}
}
+ sa_inputfile_set_completion_flag(SA_FILE_CONFIG_SYSTEM);
+
if (config.networkData != NULL) {
ret = sa_setup_network(config.networkData);
_E("sa_setup_network return error(%d)", ret);
}
}
+ else {
+ sa_inputfile_set_completion_flag(SA_FILE_CONFIG_ETHERNET);
+ sa_inputfile_set_completion_flag(SA_FILE_CONFIG_WIFI);
+ }
}
sa_inputfile_release_resource(&config);
+
+ sa_inputfile_clear_completion_flag();
+ sa_inputfile_remove();
}
static GSourceFuncs SourceFuncs =
{
- prepare, // prepare function
- NULL, // function check
- sa_inputfile_thread, // callback start
+ prepare, // prepare function
+ NULL, // function check
+ sa_inputfile_thread, // main function to execute
NULL
};
// config for main thread & callback function
-static gpointer adder_thread_output (gpointer data)
+static gpointer main_thread (gpointer data)
{
GSource* src = NULL;
- _D("adder_thread_output ~~~");
+ _D("main_thread ~~~");
GMainLoop* main_loop;
GMainContext* context = NULL;
g_source_set_name (src, "setup-adaptor-func");
g_source_set_priority (src, G_PRIORITY_DEFAULT);
- g_source_set_callback(src, (GSourceFunc) setup, (gpointer) main_loop, NULL); // set callback function
+ g_source_set_callback(src, // GSourceFuncs prototype
+ (GSourceFunc) setup_cb, // callabck function
+ (gpointer) main_loop, // gpointer : set as GMainLoop handle to destroy in thread
+ NULL);
g_source_attach (src, context);
g_main_loop_run(main_loop);
if(data) {
g_main_loop_quit( (GMainLoop*)data );
}
+ _D("main_thread exit~~~");
return NULL;
}
sa_file_state_e file_state = SA_FILE_STATE_NOT_EXISTED;
sa_config_s config = {0,};
- _D("main function~~~");
// check if exists config file
file_state = sa_inputfile_get_config_state();
- _D("first check for config enable(%d)", file_state);
- if (SA_FILE_STATE_NOT_EXISTED == file_state) {
- _D("config file is not existed, disable setup-adaptor");
+ if (SA_FILE_STATE_EXIST != file_state) {
+ _D("Unavailable config file, disable setup-adaptor");
return 0;
}
+ _D("setup-adaptor started !!!");
+
GMainLoop *main_loop;
main_loop = g_main_loop_new(NULL, FALSE);
- g_thread_create((GThreadFunc) adder_thread_output, main_loop, TRUE, NULL);
+ g_thread_create((GThreadFunc) main_thread, main_loop, TRUE, NULL);
g_main_loop_run(main_loop);
g_main_loop_unref(main_loop);
#include <sys/inotify.h>
#include <fcntl.h>
#include <json.h>
+#include <errno.h>
#include "sa_common.h"
#include "sa_types.h"
#include "input_file.h"
sa_error_e ret = SA_ERROR_NONE;
// check file
if (access(CONFIG_FILE, 0) != 0) {
- return SA_ERROR_NOT_AVAILABLE;
+ return SA_ERROR_NOT_SUPPORTED;
}
// parsing and fill into struct
ret = __parse_config(CONFIG_FILE, config);
}
}
+#define FLAG_FILE_SYSTEM "/etc/setup-adaptor/system_executed"
+#define FLAG_FILE_ETH "/etc/setup-adaptor/ethernet_executed"
+#define FLAG_FILE_WIFI "/etc/setup-adaptor/wifi_executed"
-/* Sample Config */
-/*{"version":"0.1","wifi":{"ssid":"XXXX","password":"XXXXXX","enabled":true,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDNSServer":"127.0.0.1","secondaryDNSServer":"127.0.0.1"},"ethernet":{"enabled":false,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDnsServer":"127.0.0.1","secondaryDnsServer":"127.0.0.1"},"httpProxyHost":"127.0.0.1","httpProxyPort":8080}*/
\ No newline at end of file
+void sa_inputfile_remove(void)
+{
+ if (remove(CONFIG_FILE) != 0) {
+ _E("Can't remove file{%s}! %s", CONFIG_FILE, strerror(errno));
+ }
+ else {
+ _D("file removed successfully..{%s}", CONFIG_FILE);
+ }
+}
+
+void sa_inputfile_clear_completion_flag(void)
+{
+ if (remove(FLAG_FILE_SYSTEM) == 0) {
+ _D("file removed successfully..{%s}", FLAG_FILE_SYSTEM);
+ }
+ if (remove(FLAG_FILE_ETH) == 0) {
+ _D("file removed successfully..{%s}", FLAG_FILE_ETH);
+ }
+ if (remove(FLAG_FILE_WIFI) == 0) {
+ _D("file removed successfully..{%s}", FLAG_FILE_WIFI);
+ }
+}
+
+void sa_inputfile_set_completion_flag(sa_file_config_e config_type)
+{
+ int fd;
+
+ switch(config_type)
+ {
+ case SA_FILE_CONFIG_SYSTEM:
+ fd = creat(FLAG_FILE_SYSTEM, NULL);
+ break;
+ case SA_FILE_CONFIG_ETHERNET:
+ fd = creat(FLAG_FILE_ETH, NULL);
+ break;
+ case SA_FILE_CONFIG_WIFI:
+ fd = creat(FLAG_FILE_WIFI, NULL);
+ break;
+ default:
+ _E("unknown parameter (%d)", config_type);
+ break;
+ }
+
+ if(fd == -1) {
+ _E("Can't create file for (%d)! %s", config_type, strerror(errno));
+ }
+}
+
+// return TRUE if set
+gboolean sa_inputfile_get_completion_flag(sa_file_config_e config_type)
+{
+ int ret = 0;
+
+ switch(config_type)
+ {
+ case SA_FILE_CONFIG_SYSTEM:
+ ret = access(FLAG_FILE_SYSTEM, 0);
+ break;
+ case SA_FILE_CONFIG_ETHERNET:
+ ret = access(FLAG_FILE_ETH, 0);
+ break;
+ case SA_FILE_CONFIG_WIFI:
+ ret = access(FLAG_FILE_WIFI, 0);
+ break;
+ case SA_FILE_CONFIG_ALL:
+ ret = access(FLAG_FILE_SYSTEM, 0);
+ if(ret == 0) {
+ ret = access(FLAG_FILE_ETH, 0);
+ }
+ if(ret == 0) {
+ ret = access(FLAG_FILE_WIFI, 0);
+ }
+ break;
+ default:
+ _E("unknown parameter (%d)", config_type);
+ break;
+ }
+ if (ret == 0) {
+ return TRUE;
+ } else {
+ return FALSE;
+ }
+}
*/
gboolean sa_inputfile_thread(GSource *source, GSourceFunc callbackFuntion, gpointer user_data);
-
+/**
+ * @fn void sa_inputfile_release_resource
+ * @brief This function to free allocated resource (memory)
+ * @param sa_config_s
+ * @return void
+ */
void sa_inputfile_release_resource(sa_config_s *config);
+/**
+ * @fn void sa_inputfile_remove
+ * @brief This function to delete config file to prevent from execution next time.
+ * @param void
+ * @return void
+ */
+void sa_inputfile_remove(void);
+
+/**
+ * @fn void sa_inputfile_clear_completion_flag
+ * @brief This function to clear all file flag.
+ * @param void
+ * @return void
+ */
+void sa_inputfile_clear_completion_flag(void);
+
+/**
+ * @fn void sa_inputfile_set_completion_flag
+ * @brief This function to set completion flag to prevent from re-entrance.
+ * @param sa_file_config_e for each items
+ * @return void
+ */
+void sa_inputfile_set_completion_flag(sa_file_config_e config_type);
+
+/**
+ * @fn void sa_inputfile_get_completion_flag
+ * @brief This function to get the completion status.
+ * @param sa_file_config_e for each items
+ * @return TRUE (if completed), FALSE (need to config)
+ */
+gboolean sa_inputfile_get_completion_flag(sa_file_config_e config_type);
+
#endif/* __INPUT_FILE_H__ */
\ No newline at end of file
SA_FILE_STATE_CHANGED, /**< Changed */
} sa_file_state_e;
+
+typedef enum {
+ SA_FILE_CONFIG_SYSTEM = 0, /**< system configuration type */
+ SA_FILE_CONFIG_ETHERNET, /**< ethernet configuration type */
+ SA_FILE_CONFIG_WIFI, /**< wifi configuration type */
+ SA_FILE_CONFIG_ALL,
+ SA_FILE_CONFIG_UNKNOWN
+} sa_file_config_e;
+
/**
* @brief This type is definition of status file change callback
*
#include "sa_common.h"
#include "sa_types.h"
#include "setup_network.h"
+#include "input_file.h"
#include "net_connection.h"
#include "wifi-manager.h"
}
+extern void sa_inputfile_flag(void);
+
static sa_error_e __network_connect(sa_network_s *info)
{
sa_error_e ret = SA_ERROR_NONE;
return SA_ERROR_INVALID_PARAMETER;
}
+ if (info->eth != NULL) {
+ // decide whether it will be set according to policy
+ if ( (info->eth->enabled == TRUE) && !sa_inputfile_get_completion_flag(SA_FILE_CONFIG_ETHERNET) ){
+ retEth = __ethernet_connect_main(info->eth);
+ _D("return ethernet [%d]", retEth);
+ }
+ }
+ sa_inputfile_set_completion_flag(SA_FILE_CONFIG_ETHERNET);
+
if (info->wifi != NULL) {
- if (info->wifi->enabled == TRUE) {
+ if ( (info->wifi->enabled == TRUE) && !sa_inputfile_get_completion_flag(SA_FILE_CONFIG_WIFI) ) {
retWifi = __wifi_connect_main(info->wifi);
_D("return wifi [%d]", retWifi);
-
} else {
_D("wifi enabled flag is false");
}
}
-
- if (info->eth != NULL) {
- // decide whether it will be set according to policy
- if (info->eth->enabled == TRUE){
- retEth = __ethernet_connect_main(info->eth);
- _D("return ethernet [%d]", retEth);
- }
- }
+ sa_inputfile_set_completion_flag(SA_FILE_CONFIG_WIFI);
return ret;
}
-{"version":"0.1","wifi":{"ssid":"docker","password":"dockerdocker","enabled":true,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDNSServer":"127.0.0.1","secondaryDNSServer":"127.0.0.1"},"ethernet":{"enabled":false,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDnsServer":"127.0.0.1","secondaryDnsServer":"127.0.0.1"},"httpProxyHost":"127.0.0.1","httpProxyPort":8080}
\ No newline at end of file
+{
+"version":"0.1",
+"wifi":{"ssid":"docker","password":"dockerdocker","enabled":true,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDNSServer":"127.0.0.1","secondaryDNSServer":"127.0.0.1"},
+"ethernet":{"enabled":false,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDnsServer":"127.0.0.1","secondaryDnsServer":"127.0.0.1"},
+"httpProxyHost":"127.0.0.1",
+"httpProxyPort":8080
+}
\ No newline at end of file