diff --git a/nuttx/configs/stm32f777zit6-meadow/src/espcp/espcp_coprocessor.c b/nuttx/configs/stm32f777zit6-meadow/src/espcp/espcp_coprocessor.c index d7bef8af500..af84f8554f2 100644 --- a/nuttx/configs/stm32f777zit6-meadow/src/espcp/espcp_coprocessor.c +++ b/nuttx/configs/stm32f777zit6-meadow/src/espcp/espcp_coprocessor.c @@ -54,6 +54,7 @@ #include #include #include +#include #include #include diff --git a/nuttx/configs/stm32f777zit6-meadow/src/espcp/espcp_event_handlers.c b/nuttx/configs/stm32f777zit6-meadow/src/espcp/espcp_event_handlers.c index 047155410d7..bb16505ef29 100644 --- a/nuttx/configs/stm32f777zit6-meadow/src/espcp/espcp_event_handlers.c +++ b/nuttx/configs/stm32f777zit6-meadow/src/espcp/espcp_event_handlers.c @@ -36,6 +36,8 @@ #include +#include + #include #include #include @@ -78,6 +80,7 @@ static void espcp_network_got_ip_event_handler(espcp_message_t *); static void espcp_system_get_configuration_event_handler(espcp_message_t *); static void espcp_system_error_event_handler(espcp_message_t *); +static void espcp_send_logging_configuration(void); /**************************************************************************** * Private Data @@ -431,6 +434,87 @@ void espcp_dispatch_event(espcp_message_t *message) } } +/**************************************************************************** + * Name: espcp_send_logging_configuration + * + * Description: + * Build the current logging configuration and send it to the ESP32. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + * Assumptions/Limitations: + * None. + * + ****************************************************************************/ +static void espcp_send_logging_configuration(void) +{ + hcom_nx_config_lock(); + meadow_configuration_t *config = hcom_nx_config_get_pointer(); + espcp_logging_configuration_t logging_config; + logging_config.interface = config->esp_log_destination; + logging_config.udp_port = config->esp_log_udp_port; + // + // Note that we do not need to strdup the string as it will be copied + // when the payload is encoded. Also, the config is still locked at + // this point. + // + logging_config.components_to_log = config->esp_log_components; + int payload_length = espcp_encoded_logging_configuration_buffer_size(&logging_config); + uint8_t *payload = (uint8_t *) zalloc(payload_length); + if (payload != NULL) + { + espcp_encode_logging_configuration(&logging_config, payload); + } + hcom_nx_config_unlock(); + + if (payload != NULL) + { + espcp_message_t *logging_configuration_message = espcp_create_message_on_heap(espcp_message_types_header, espcp_esp32_interfaces_system, + espcp_system_function_logging_configuration, espcp_status_codes_completed_ok, + espcp_get_next_message_id(), payload, payload_length); + if (logging_configuration_message != NULL) + { + espcp_queue_message(logging_configuration_message, false); + } + else + { + free(payload); + } + } +} + +/**************************************************************************** + * Name: espcp_send_wifi_country_code + * + * Description: + * Send the WiFi country to the ESP in case it needs changing. + * + * Input Parameters: + * None. + * + ****************************************************************************/ +static void espcp_send_wifi_country_code(void) +{ + char *country_code = NULL; + + hcom_nx_config_lock(); + meadow_configuration_t *config = hcom_nx_config_get_pointer(); + if (config != NULL) + { + country_code = kmm_strdup(config->country_code); + } + hcom_nx_config_unlock(); + if (country_code != NULL) + { + hcom_nx_config_set_esp_string_value(espcp_configuration_items_country_code, country_code); + kmm_free(country_code); + } +} + /**************************************************************************** * Name: espcp_system_get_configuration_event_handler * @@ -469,39 +553,8 @@ void espcp_system_get_configuration_event_handler(espcp_message_t *message) // // Send logging configuration to the ESP32. // - hcom_nx_config_lock(); - config = hcom_nx_config_get_pointer(); - espcp_logging_configuration_t logging_config; - logging_config.interface = config->esp_log_destination; - logging_config.udp_port = config->esp_log_udp_port; - // - // Note that we do not need to strdup the string as it will be copied - // when the payload is encoded. Also, the config is still locked at - // this point. - // - logging_config.components_to_log = config->esp_log_components; - int payload_length = espcp_encoded_logging_configuration_buffer_size(&logging_config); - uint8_t *payload = (uint8_t *) zalloc(payload_length); - if (payload != NULL) - { - espcp_encode_logging_configuration(&logging_config, payload); - } - hcom_nx_config_unlock(); - - if (payload != NULL) - { - espcp_message_t *logging_configuration_message = espcp_create_message_on_heap(espcp_message_types_header, espcp_esp32_interfaces_system, - espcp_system_function_logging_configuration, espcp_status_codes_completed_ok, - espcp_get_next_message_id(), payload, payload_length); - if (logging_configuration_message != NULL) - { - espcp_queue_message(logging_configuration_message, false); - } - else - { - free(payload); - } - } + espcp_send_logging_configuration(); + espcp_send_wifi_country_code(); } } espcp_delete_message_and_payload(message); diff --git a/nuttx/configs/stm32f777zit6-meadow/src/espcp/espcp_shared_enums.h b/nuttx/configs/stm32f777zit6-meadow/src/espcp/espcp_shared_enums.h index 85b72ef2712..70702f4bb82 100644 --- a/nuttx/configs/stm32f777zit6-meadow/src/espcp/espcp_shared_enums.h +++ b/nuttx/configs/stm32f777zit6-meadow/src/espcp/espcp_shared_enums.h @@ -128,6 +128,11 @@ enum espcp_status_codes * @brief Invalid request. */ espcp_status_codes_invalid_request = 61, + + /** + * @brief Invalid country code. + */ + espcp_status_codes_invalid_country_code = 62, }; typedef enum espcp_status_codes espcp_status_codes_t; @@ -343,6 +348,7 @@ enum espcp_configuration_items espcp_configuration_items_soft_ap_mac_address = 14, espcp_configuration_items_subnet_mask = 15, espcp_configuration_items_bluetooth_mac_address = 16, + espcp_configuration_items_country_code = 17, }; typedef enum espcp_configuration_items espcp_configuration_items_t; diff --git a/nuttx/configs/stm32f777zit6-meadow/src/hcom_nx/hcom_nx_config_manager.c b/nuttx/configs/stm32f777zit6-meadow/src/hcom_nx/hcom_nx_config_manager.c index 7b5ac91bcf9..99bcbe803c7 100644 --- a/nuttx/configs/stm32f777zit6-meadow/src/hcom_nx/hcom_nx_config_manager.c +++ b/nuttx/configs/stm32f777zit6-meadow/src/hcom_nx/hcom_nx_config_manager.c @@ -209,6 +209,11 @@ static char *esp_log_component_names[] = { "coredump", // Core dump information }; +/** + * @brief Default country code to use for WiFi if not specified in the config file. + */ +static char *default_country_code = "01"; + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -1740,6 +1745,18 @@ static void hcom_nx_config_process_network_section(yaml_network_t *network_confi } } } + config->country_code = default_country_code; + if ((network_config->country_code != NULL) && (strlen(network_config->country_code) > 0)) + { + if (strlen(network_config->country_code) != 2) + { + meadow_os_raise_simple_exception(espcp_status_codes_invalid_country_code); + } + else + { + config->country_code = kmm_strdup(network_config->country_code); + } + } if (create_default_dns_resolver_file) { hcom_nx_config_setup_default_dns_servers(); @@ -1767,7 +1784,22 @@ static void hcom_nx_config_process_network_section(yaml_network_t *network_confi config->default_interface = hcom_nx_config_find_interface_by_name(MEADOW_IFT_ESP32_NAME); } config->default_interface->gateway_changed = false; - } + // + // Now check if the country code has been specified. + // + config->country_code = default_country_code; + if ((network_config->country_code != NULL) && (strlen(network_config->country_code) > 0)) + { + if (strlen(network_config->country_code) != 2) + { + meadow_os_raise_simple_exception(espcp_status_codes_invalid_country_code); + } + else + { + config->country_code = kmm_strdup(network_config->country_code); + } + } + } else { hcom_nx_config_setup_default_ntp_servers(config); diff --git a/nuttx/configs/stm32f777zit6-meadow/src/hcom_nx/hcom_nx_config_manager.h b/nuttx/configs/stm32f777zit6-meadow/src/hcom_nx/hcom_nx_config_manager.h index dc7094d2170..7ae1eb893ca 100644 --- a/nuttx/configs/stm32f777zit6-meadow/src/hcom_nx/hcom_nx_config_manager.h +++ b/nuttx/configs/stm32f777zit6-meadow/src/hcom_nx/hcom_nx_config_manager.h @@ -136,6 +136,7 @@ void hcom_nx_config_process_cell_config_file(void); void hcom_nx_config_turn_on_the_cell_module(void); void hcom_nx_config_refresh_mono_version(meadow_configuration_t *); int hcom_nx_config_set_esp_integer_value(espcp_configuration_items_t, uint32_t); +int hcom_nx_config_set_esp_string_value(espcp_configuration_items_t, const char *); void hcom_nx_config_set_time_to_os_build_time(void); void hcom_nx_config_add_default_gateway_dns_file(meadow_configuration_t *, uint32_t); void hcom_nx_config_add_dns_address_into_file(uint32_t); @@ -143,5 +144,6 @@ void hcom_nx_config_update_network_interface(meadow_configuration_t *, uint32_t, void hcom_nx_config_update_dns_address(meadow_configuration_t *, uint32_t); void hcom_nx_config_clear_network_interface(meadow_configuration_t *); unit_tests_configuration_t *process_unit_tests_configuration_file(void); +int hcom_nx_config_set_esp_string_value(espcp_configuration_items_t, const char *); #endif // __CONFIGS_MEADOW_SRC_HCOM_NX_CONFIG_MANAGER__H diff --git a/nuttx/configs/stm32f777zit6-meadow/src/hcom_nx/hcom_nx_config_manager_yaml.h b/nuttx/configs/stm32f777zit6-meadow/src/hcom_nx/hcom_nx_config_manager_yaml.h index 1fbd7a758dd..2dc3b256eb5 100644 --- a/nuttx/configs/stm32f777zit6-meadow/src/hcom_nx/hcom_nx_config_manager_yaml.h +++ b/nuttx/configs/stm32f777zit6-meadow/src/hcom_nx/hcom_nx_config_manager_yaml.h @@ -254,6 +254,11 @@ struct yaml_network_s */ char *default_interface; + /** + * @brief Country code for WiFi configuration. + */ + char *country_code; + /** * @brief Collection of network interfaces. */ @@ -274,6 +279,7 @@ static const cyaml_schema_field_t configuration_network_section_schema[] = CYAML_FIELD_SEQUENCE("NtpServers", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, yaml_network_t, ntp_servers, &string_ptr_schema, 0, CYAML_UNLIMITED), CYAML_FIELD_SEQUENCE("DnsServers", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, yaml_network_t, dns_servers, &string_ptr_schema, 0, CYAML_UNLIMITED), CYAML_FIELD_STRING_PTR("DefaultInterface", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, yaml_network_t, default_interface, 0, CYAML_UNLIMITED), + CYAML_FIELD_STRING_PTR("CountryCode", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, yaml_network_t, country_code, 0, CYAML_UNLIMITED), CYAML_FIELD_SEQUENCE("Interfaces", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, yaml_network_t, interfaces, &interface_schema_value, 0, CYAML_UNLIMITED), CYAML_FIELD_END }; diff --git a/nuttx/include/meadow/hcom_shared_common.h b/nuttx/include/meadow/hcom_shared_common.h index 10bae6c8a35..4ea686977a7 100644 --- a/nuttx/include/meadow/hcom_shared_common.h +++ b/nuttx/include/meadow/hcom_shared_common.h @@ -785,6 +785,11 @@ struct meadow_configuration_s * and returns an error code. */ uint32_t maximum_retry_count; + + /** + * @brief Country code for the network interface. + */ + char *country_code; }; typedef struct meadow_configuration_s meadow_configuration_t;