Cleanup config parsing.

This commit is contained in:
iabdalkader 2018-06-19 23:54:20 +02:00
parent f135c12cd9
commit 925f662786
3 changed files with 30 additions and 49 deletions

View File

@ -276,11 +276,9 @@ void NORETURN __stack_chk_fail(void)
} }
#endif #endif
typedef struct apply_settings_user typedef struct openmv_config {
{ wifi_dbg_config_t wifi_dbg_config;
wifi_dbg_settings_t wifi_dbg_settings; } openmv_config_t;
}
apply_settings_user_t;
extern char *strncpy(char *dst, const char *src, size_t n); extern char *strncpy(char *dst, const char *src, size_t n);
@ -298,7 +296,7 @@ static bool ini_handler_callback_is_true(const char *value)
int ini_handler_callback(void *user, const char *section, const char *name, const char *value) int ini_handler_callback(void *user, const char *section, const char *name, const char *value)
{ {
apply_settings_user_t *apply_settings_user = (apply_settings_user_t *) user; openmv_config_t *openmv_config = (openmv_config_t *) user;
#define MATCH(s, n) ((strcmp(section, (s)) == 0) && (strcmp(name, (n)) == 0)) #define MATCH(s, n) ((strcmp(section, (s)) == 0) && (strcmp(name, (n)) == 0))
@ -312,21 +310,21 @@ int ini_handler_callback(void *user, const char *section, const char *name, cons
MP_STATE_PORT(pyb_stdio_uart) = pyb_uart_type.make_new((mp_obj_t) &pyb_uart_type, MP_ARRAY_SIZE(args), 0, args); MP_STATE_PORT(pyb_stdio_uart) = pyb_uart_type.make_new((mp_obj_t) &pyb_uart_type, MP_ARRAY_SIZE(args), 0, args);
} }
} else if (MATCH("BootSettings", "WiFiMode")) { } else if (MATCH("BootSettings", "WiFiMode")) {
apply_settings_user->wifi_dbg_settings.wifi_mode = ini_atoi(value); openmv_config->wifi_dbg_config.wifi_mode = ini_atoi(value);
} else if (MATCH("BootSettings", "ClientModeSSID")) { } else if (MATCH("BootSettings", "ClientModeSSID")) {
strncpy(apply_settings_user->wifi_dbg_settings.wifi_client_ssid, name, SSID_MAX); strncpy(openmv_config->wifi_dbg_config.wifi_client_ssid, name, SSID_MAX);
} else if (MATCH("BootSettings", "ClientModePass")) { } else if (MATCH("BootSettings", "ClientModePass")) {
strncpy(apply_settings_user->wifi_dbg_settings.wifi_client_pass, name, PASS_MAX); strncpy(openmv_config->wifi_dbg_config.wifi_client_pass, name, PASS_MAX);
} else if (MATCH("BootSettings", "ClientModeType")) { } else if (MATCH("BootSettings", "ClientModeType")) {
apply_settings_user->wifi_dbg_settings.wifi_client_type = ini_atoi(value); openmv_config->wifi_dbg_config.wifi_client_type = ini_atoi(value);
} else if (MATCH("BootSettings", "AccessPointModeSSID")) { } else if (MATCH("BootSettings", "AccessPointModeSSID")) {
strncpy(apply_settings_user->wifi_dbg_settings.wifi_ap_ssid, name, SSID_MAX); strncpy(openmv_config->wifi_dbg_config.wifi_ap_ssid, name, SSID_MAX);
} else if (MATCH("BootSettings", "AccessPointModePass")) { } else if (MATCH("BootSettings", "AccessPointModePass")) {
strncpy(apply_settings_user->wifi_dbg_settings.wifi_ap_pass, name, SSID_MAX); strncpy(openmv_config->wifi_dbg_config.wifi_ap_pass, name, SSID_MAX);
} else if (MATCH("BootSettings", "AccessPointModeType")) { } else if (MATCH("BootSettings", "AccessPointModeType")) {
apply_settings_user->wifi_dbg_settings.wifi_ap_type = ini_atoi(value); openmv_config->wifi_dbg_config.wifi_ap_type = ini_atoi(value);
} else if (MATCH("BootSettings", "BoardName")) { } else if (MATCH("BootSettings", "BoardName")) {
strncpy(apply_settings_user->wifi_dbg_settings.wifi_board_name, name, NAME_MAX); strncpy(openmv_config->wifi_dbg_config.wifi_board_name, name, NAME_MAX);
} else { } else {
return 0; return 0;
} }
@ -336,23 +334,6 @@ int ini_handler_callback(void *user, const char *section, const char *name, cons
#undef MATCH #undef MATCH
} }
FRESULT apply_settings(const char *path)
{
nlr_buf_t nlr;
FRESULT f_res = f_stat(&vfs_fat->fatfs, path, NULL);
if (f_res == FR_OK) {
if (nlr_push(&nlr) == 0) {
apply_settings_user_t apply_settings_user = {};
ini_parse(&vfs_fat->fatfs, path, ini_handler_callback, &apply_settings_user);
wifi_dbg_apply_settings(&apply_settings_user.wifi_dbg_settings);
nlr_pop();
}
}
return f_res;
}
FRESULT exec_boot_script(const char *path, bool selftest, bool interruptible) FRESULT exec_boot_script(const char *path, bool selftest, bool interruptible)
{ {
nlr_buf_t nlr; nlr_buf_t nlr;
@ -551,6 +532,13 @@ soft_reset:
MP_STATE_VM(vfs_mount_table) = vfs; MP_STATE_VM(vfs_mount_table) = vfs;
MP_STATE_PORT(vfs_cur) = vfs; MP_STATE_PORT(vfs_cur) = vfs;
// Parse OpenMV configuration file.
if (first_soft_reset) {
openmv_config_t openmv_config = {0};
ini_parse(&vfs_fat->fatfs, "/openmv.config", ini_handler_callback, &openmv_config);
wifi_dbg_apply_config(&openmv_config.wifi_dbg_config);
}
// Init USB device to default setting if it was not already configured // Init USB device to default setting if it was not already configured
pyb_usb_dev_init(USBD_VID, USBD_PID_CDC_MSC, USBD_MODE_CDC_MSC, NULL); pyb_usb_dev_init(USBD_VID, USBD_PID_CDC_MSC, USBD_MODE_CDC_MSC, NULL);
@ -569,7 +557,6 @@ soft_reset:
// Run boot script(s) // Run boot script(s)
if (first_soft_reset) { if (first_soft_reset) {
exec_boot_script("/selftest.py", true, false); exec_boot_script("/selftest.py", true, false);
apply_settings("/openmv.config");
exec_boot_script("/main.py", false, true); exec_boot_script("/main.py", false, true);
} }

View File

@ -23,9 +23,9 @@ void wifi_dbg_init()
// Also, reset regular USB CDC DBG access if things were setup before. // Also, reset regular USB CDC DBG access if things were setup before.
} }
void wifi_dbg_apply_settings(wifi_dbg_settings_t *settings) void wifi_dbg_apply_config(wifi_dbg_config_t *config)
{ {
switch(settings->wifi_mode) { switch(config->wifi_mode) {
case WIFI_DBG_DISABLED: // Don't init anything. case WIFI_DBG_DISABLED: // Don't init anything.
break; break;
case WIFI_DBG_ENABLED_CLIENT_MODE: case WIFI_DBG_ENABLED_CLIENT_MODE:
@ -33,7 +33,7 @@ void wifi_dbg_apply_settings(wifi_dbg_settings_t *settings)
// Additionally, make network.WINC() return a python object of the setup wifi module and make // Additionally, make network.WINC() return a python object of the setup wifi module and make
// winc.connect()/winc.disconnect() do nothing. This should allow the user to use the wifi module // winc.connect()/winc.disconnect() do nothing. This should allow the user to use the wifi module
// normally in their scripts in debug mode. // normally in their scripts in debug mode.
// You should have all the settings you need to init the wifi module in the settings struct. // You should have all the settings you need to init the wifi module in the config struct.
// Disable regular USB CDC DBG if this works... two folks can't share the interface. // Disable regular USB CDC DBG if this works... two folks can't share the interface.
// For data transfer create a TCP server after setting up the wifi. You will then UDP broadcast // For data transfer create a TCP server after setting up the wifi. You will then UDP broadcast
// your IP address along with the port you chose for the TCP server. OpenMV IDE will connect to this. // your IP address along with the port you chose for the TCP server. OpenMV IDE will connect to this.
@ -43,7 +43,7 @@ void wifi_dbg_apply_settings(wifi_dbg_settings_t *settings)
// Additionally, make network.WINC() return a python object of the setup wifi module and make // Additionally, make network.WINC() return a python object of the setup wifi module and make
// winc.connect()/winc.disconnect() do nothing. This should allow the user to use the wifi module // winc.connect()/winc.disconnect() do nothing. This should allow the user to use the wifi module
// normally in their scripts in debug mode. // normally in their scripts in debug mode.
// You should have all the settings you need to init the wifi module in the settings struct. // You should have all the settings you need to init the wifi module in the config struct.
// Disable regular USB CDC DBG if this works... two folks can't share the interface. // Disable regular USB CDC DBG if this works... two folks can't share the interface.
// For data transfer create a TCP server after setting up the wifi. You will then UDP broadcast // For data transfer create a TCP server after setting up the wifi. You will then UDP broadcast
// your IP address along with the port you chose for the TCP server. OpenMV IDE will connect to this. // your IP address along with the port you chose for the TCP server. OpenMV IDE will connect to this.

View File

@ -11,24 +11,19 @@
#define OPENMVCAM_BROADCAST_PORT 0xABD1 #define OPENMVCAM_BROADCAST_PORT 0xABD1
typedef enum wifi_dbg_wifi_mode typedef enum wifi_dbg_wifi_mode {
{
WIFI_DBG_DISABLED = 0, WIFI_DBG_DISABLED = 0,
WIFI_DBG_ENABLED_CLIENT_MODE = 1, WIFI_DBG_ENABLED_CLIENT_MODE = 1,
WIFI_DBG_ENABLED_AP_MODE = 2 WIFI_DBG_ENABLED_AP_MODE = 2
} } wifi_dbg_wifi_mode_t;
wifi_dbg_wifi_mode_t;
typedef enum wifi_dbg_wifi_type typedef enum wifi_dbg_wifi_type {
{
WIFI_DBG_OPEN, WIFI_DBG_OPEN,
WIFI_DBG_WPA, WIFI_DBG_WPA,
WIFI_DBG_WEP WIFI_DBG_WEP
} } wifi_dbg_wifi_type_t;
wifi_dbg_wifi_type_t;
typedef struct wifi_dbg_settings typedef struct wifi_dbg_config {
{
wifi_dbg_wifi_mode_t wifi_mode; wifi_dbg_wifi_mode_t wifi_mode;
char wifi_client_ssid[SSID_MAX+1]; char wifi_client_ssid[SSID_MAX+1];
char wifi_client_pass[PASS_MAX+1]; char wifi_client_pass[PASS_MAX+1];
@ -37,11 +32,10 @@ typedef struct wifi_dbg_settings
char wifi_ap_pass[PASS_MAX+1]; char wifi_ap_pass[PASS_MAX+1];
wifi_dbg_wifi_type_t wifi_ap_type; wifi_dbg_wifi_type_t wifi_ap_type;
char wifi_board_name[NAME_MAX+1]; char wifi_board_name[NAME_MAX+1];
} } wifi_dbg_config_t;
wifi_dbg_settings_t;
void wifi_dbg_init(); void wifi_dbg_init();
void wifi_dbg_apply_settings(wifi_dbg_settings_t *settings); void wifi_dbg_apply_config(wifi_dbg_config_t *config);
void wifi_dbg_beacon(); void wifi_dbg_beacon();
#endif /* __WIFI_DBG_H__ */ #endif /* __WIFI_DBG_H__ */