Skip to content

Commit

Permalink
refactor: reduce boot time
Browse files Browse the repository at this point in the history
* chore: remove unused header

* refactor: reduce boot time
  • Loading branch information
iChizer0 committed Nov 9, 2023
1 parent df759cb commit bc8a4ba
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 53 deletions.
3 changes: 0 additions & 3 deletions porting/himax/we2/drivers/drv_ov5647.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions sscma/callback/action.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ void set_action(const std::vector<std::string>& 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\": ",
Expand Down
7 changes: 5 additions & 2 deletions sscma/callback/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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\": ",
Expand All @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion sscma/callback/mqtt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ void set_mqtt_server(const std::vector<std::string>& 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;
if (ret != EL_OK) [[unlikely]]
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);
Expand All @@ -47,6 +50,10 @@ void set_mqtt_server(const std::vector<std::string>& 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],
Expand Down
10 changes: 10 additions & 0 deletions sscma/callback/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ void set_wireless_network(const std::vector<std::string>& 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);
Expand All @@ -53,6 +59,10 @@ void set_wireless_network(const std::vector<std::string>& 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],
Expand Down
7 changes: 5 additions & 2 deletions sscma/callback/sensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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\": ",
Expand All @@ -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);
Expand Down
104 changes: 59 additions & 45 deletions sscma/main_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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<std::string> 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<std::string> 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<std::string> 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
Expand Down
4 changes: 4 additions & 0 deletions sscma/static_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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\": ",
Expand Down

0 comments on commit bc8a4ba

Please sign in to comment.