diff --git a/porting/himax/we2/drivers/drv_ov5647.h b/porting/himax/we2/drivers/drv_ov5647.h index a1fada2d..bc58775a 100644 --- a/porting/himax/we2/drivers/drv_ov5647.h +++ b/porting/himax/we2/drivers/drv_ov5647.h @@ -66,9 +66,6 @@ #endif #endif -/* el */ -#include "core/el_common.h" - #define DEAULT_XHSUTDOWN_PIN AON_GPIO2 #define OV5647_MAX_WIDTH 640 #define OV5647_MAX_HEIGHT 480 diff --git a/sscma/callback/action.hpp b/sscma/callback/action.hpp index a67a489b..b74788f2 100644 --- a/sscma/callback/action.hpp +++ b/sscma/callback/action.hpp @@ -69,6 +69,10 @@ void set_action(const std::vector& argv) { } ActionReply: +#if CONFIG_EL_DEBUG == 0 + if (!static_resource->is_ready.load()) return; +#endif + const auto& ss{concat_strings("\r{\"type\": 0, \"name\": \"", argv[0], "\", \"code\": ", diff --git a/sscma/callback/model.hpp b/sscma/callback/model.hpp index c124ce2c..a7ba5cdb 100644 --- a/sscma/callback/model.hpp +++ b/sscma/callback/model.hpp @@ -55,7 +55,11 @@ void set_model(const std::string& cmd, uint8_t model_id) { ModelError: static_resource->current_model_id = 0; -ModelReply: { +ModelReply: +#if CONFIG_EL_DEBUG == 0 + if (!static_resource->is_ready.load()) return; +#endif + const auto& ss{concat_strings("\r{\"type\": 0, \"name\": \"", cmd, "\", \"code\": ", @@ -65,7 +69,6 @@ ModelReply: { "}}\n")}; static_resource->transport->send_bytes(ss.c_str(), ss.size()); } -} void get_model_info(const std::string& cmd) { const auto& model_info = static_resource->models->get_model_info(static_resource->current_model_id); diff --git a/sscma/callback/mqtt.hpp b/sscma/callback/mqtt.hpp index 0f9b2e1f..b3e3ffb2 100644 --- a/sscma/callback/mqtt.hpp +++ b/sscma/callback/mqtt.hpp @@ -31,7 +31,7 @@ void set_mqtt_server(const std::vector& argv) { std::strncpy(config.address, argv[2].c_str(), sizeof(config.address) - 1); std::strncpy(config.username, argv[3].c_str(), sizeof(config.username) - 1); std::strncpy(config.password, argv[4].c_str(), sizeof(config.password) - 1); - config.use_ssl = std::atoi(argv[5].c_str()) != 0; // TODO: add SSL config support + config.use_ssl = std::atoi(argv[5].c_str()) != 0; // TODO: driver add SSL config support if (static_resource->is_ready.load()) [[likely]] { ret = static_resource->storage->emplace(el_make_storage_kv_from_type(config)) ? EL_OK : EL_EIO; @@ -39,6 +39,9 @@ void set_mqtt_server(const std::vector& argv) { goto Reply; } + if (!argv[1].c_str() || !argv[2].c_str()) // TODO: driver add disconnect support + goto Reply; + while (--retry_cnt && static_resource->network->status() != NETWORK_CONNECTED) { ret = static_resource->network->connect( config.address, config.username, config.password, sscma::callback::mqtt_recv_cb); @@ -47,6 +50,10 @@ void set_mqtt_server(const std::vector& argv) { } Reply: +#if CONFIG_EL_DEBUG == 0 + if (!static_resource->is_ready.load()) return; +#endif + connected = retry_cnt && static_resource->network->status() == NETWORK_CONNECTED; const auto& ss{concat_strings("\r{\"type\": 0, \"name\": \"", argv[0], diff --git a/sscma/callback/network.hpp b/sscma/callback/network.hpp index f8bc3da7..e6958b8c 100644 --- a/sscma/callback/network.hpp +++ b/sscma/callback/network.hpp @@ -45,6 +45,12 @@ void set_wireless_network(const std::vector& argv) { goto Reply; } + if (!argv[1].c_str()) { + ret = static_resource->network->quit(); + el_sleep(SSCMA_WIRELESS_NETWORK_CONN_DELAY_MS); + goto Reply; + } + retry_cnt = SSCMA_WIRELESS_NETWORK_CONN_RETRY; while (--retry_cnt && static_resource->network->status() != NETWORK_JOINED) { ret = static_resource->network->join(config.name, config.passwd); @@ -53,6 +59,10 @@ void set_wireless_network(const std::vector& argv) { } Reply: +#if CONFIG_EL_DEBUG == 0 + if (!static_resource->is_ready.load()) return; +#endif + joined = retry_cnt && static_resource->network->status() == NETWORK_JOINED; const auto& ss{concat_strings("\r{\"type\": 0, \"name\": \"", argv[0], diff --git a/sscma/callback/sensor.hpp b/sscma/callback/sensor.hpp index a6ed07d7..591c64c2 100644 --- a/sscma/callback/sensor.hpp +++ b/sscma/callback/sensor.hpp @@ -66,7 +66,11 @@ void set_sensor(const std::string& cmd, uint8_t sensor_id, bool enable) { SensorError: static_resource->current_sensor_id = 0; -SensorReply: { +SensorReply: +#if CONFIG_EL_DEBUG == 0 + if (!static_resource->is_ready.load()) return; +#endif + const auto& ss{concat_strings("\r{\"type\": 0, \"name\": \"", cmd, "\", \"code\": ", @@ -76,7 +80,6 @@ SensorReply: { "}}\n")}; static_resource->transport->send_bytes(ss.c_str(), ss.size()); } -} void get_sensor_info(const std::string& cmd) { const auto& sensor_info = static_resource->device->get_sensor_info(static_resource->current_sensor_id); diff --git a/sscma/main_task.hpp b/sscma/main_task.hpp index ab5dd4f7..c893dd5c 100644 --- a/sscma/main_task.hpp +++ b/sscma/main_task.hpp @@ -265,57 +265,71 @@ void run() { return EL_OK; }); - // init commands (TODO: call init functions directly) - { // set model - if (static_resource->current_model_id) [[likely]] - static_resource->instance->exec( - concat_strings("AT+MODEL=", std::to_string(static_resource->current_model_id))); + // init commands + // set model + if (static_resource->current_model_id) [[likely]] + set_model("set_model", static_resource->current_model_id); + + // set sensor + if (static_resource->current_sensor_id) [[likely]] + set_sensor("set_sensor", static_resource->current_sensor_id, true); + + // set action + if (static_resource->storage->contains(SSCMA_STORAGE_KEY_ACTION)) [[likely]] { + char action[CONFIG_SSCMA_CMD_MAX_LENGTH]{}; + *static_resource->storage >> el_make_storage_kv(SSCMA_STORAGE_KEY_ACTION, action); + std::vector argv{"set_action", action}; + set_action(argv); } - { // set sensor - if (static_resource->current_sensor_id) [[likely]] - static_resource->instance->exec( - concat_strings("AT+SENSOR=", std::to_string(static_resource->current_sensor_id), ",1")); - } - { // set action - if (static_resource->storage->contains(SSCMA_STORAGE_KEY_ACTION)) [[likely]] { - char action[CONFIG_SSCMA_CMD_MAX_LENGTH]{}; - *static_resource->storage >> el_make_storage_kv(SSCMA_STORAGE_KEY_ACTION, action); - static_resource->instance->exec(concat_strings("AT+ACTION=", quoted(action))); - } - } - { // connect to wireless network + + // connect to wireless network and setup MQTT + { auto config = wireless_network_config_t{}; auto kv = el_make_storage_kv_from_type(config); - if (static_resource->storage->contains(kv.key)) [[likely]] { + if (!static_resource->storage->contains(kv.key)) [[unlikely]] + goto Finish; + { *static_resource->storage >> kv; - static_resource->instance->exec(concat_strings( - "AT+WIFI=", quoted(config.name), std::to_string(config.security_type), quoted(config.passwd))); + std::vector argv{ + "set_wireless_network", quoted(config.name), std::to_string(config.security_type), quoted(config.passwd)}; + set_wireless_network(argv); } - } - { // connect to MQTT server - auto config = mqtt_server_config_t{}; - auto kv = el_make_storage_kv_from_type(config); - if (static_resource->storage->contains(kv.key)) [[likely]] { - *static_resource->storage >> kv; - static_resource->instance->exec(concat_strings("AT+MQTTSERVER=", - quoted(config.client_id), - quoted(config.address), - quoted(config.username), - quoted(config.password), - std::to_string(config.use_ssl ? 1 : 0))); - } - } - { // set MQTT publish, subcribe topic - auto config = mqtt_pubsub_config_t{}; - auto kv = el_make_storage_kv_from_type(config); - if (static_resource->storage->contains(kv.key)) [[likely]] { - *static_resource->storage >> kv; - static_resource->instance->exec(concat_strings("AT+MQTTPUBSUB=", - quoted(config.pub_topic), - std::to_string(config.pub_qos), - quoted(config.sub_topic), - std::to_string(config.sub_qos))); + if (static_resource->network->status() != NETWORK_JOINED) goto Finish; + { + auto config = mqtt_server_config_t{}; + auto kv = el_make_storage_kv_from_type(config); + if (static_resource->storage->contains(kv.key)) [[unlikely]] + goto Finish; + { + *static_resource->storage >> kv; + std::vector argv{"set_mqtt_server", + quoted(config.client_id), + quoted(config.address), + quoted(config.username), + quoted(config.password), + std::to_string(config.use_ssl ? 1 : 0)}; + set_mqtt_server(argv); + } + if (static_resource->network->status() != NETWORK_CONNECTED) goto Finish; + { + auto config = mqtt_pubsub_config_t{}; + auto kv = el_make_storage_kv_from_type(config); + if (static_resource->storage->contains(kv.key)) [[unlikely]] + goto Finish; + { + *static_resource->storage >> kv; + *static_resource->storage >> kv; + std::vector argv{"set_mqtt_pubsub", + quoted(config.pub_topic), + std::to_string(config.pub_qos), + quoted(config.sub_topic), + std::to_string(config.sub_qos)}; + set_mqtt_pubsub(argv); + } + } } + + Finish:; } // mark the system status as ready diff --git a/sscma/static_resource.hpp b/sscma/static_resource.hpp index c3c02dca..df2511df 100644 --- a/sscma/static_resource.hpp +++ b/sscma/static_resource.hpp @@ -106,6 +106,9 @@ class StaticResource final { serial = device->get_serial(); network = device->get_network(); + static auto v_transport{MuxTransport()}; + transport = &v_transport; + static auto v_instance{Server()}; instance = &v_instance; @@ -196,6 +199,7 @@ class StaticResource final { inline void init_frontend() { instance->init([this](el_err_code_t ret, std::string msg) { if (ret != EL_OK) [[unlikely]] { + msg.erase(std::remove_if(msg.begin(), msg.end(), [](char c) { return std::iscntrl(c); }), msg.end()); const auto& ss{concat_strings("\r{\"type\": 2, \"name\": \"AT\", \"code\": ", std::to_string(ret), ", \"data\": ",