Applied astyle code formatting

This commit is contained in:
David Cermak
2021-06-01 10:21:51 +02:00
parent dc64f862c4
commit a61e9e2d40
43 changed files with 865 additions and 662 deletions

View File

@ -32,13 +32,13 @@ using NetDCE = DCE_T<NetModule>;
class NetDCE_Factory: public Factory {
public:
template <typename Module, typename ...Args>
static DCE_T<Module>* create(const config *cfg, Args&&... args)
static DCE_T<Module> *create(const config *cfg, Args &&... args)
{
return build_generic_DCE<Module>(cfg, std::forward<Args>(args)...);
}
template <typename Module, typename ...Args>
static std::shared_ptr<Module> create_module(const config *cfg, Args&&... args)
static std::shared_ptr<Module> create_module(const config *cfg, Args &&... args)
{
return build_shared_module<Module>(cfg, std::forward<Args>(args)...);
}
@ -53,44 +53,48 @@ public:
class NetModule: public ModuleIf {
public:
explicit NetModule(std::shared_ptr<DTE> dte, const esp_modem_dce_config *cfg):
dte(std::move(dte)), apn(std::string(cfg->apn)) {}
dte(std::move(dte)), apn(std::string(cfg->apn)) {}
[[nodiscard]] bool setup_data_mode() override
{
PdpContext pdp(apn);
if (set_pdp_context(pdp) != command_result::OK)
if (set_pdp_context(pdp) != command_result::OK) {
return false;
}
return true;
}
bool set_mode(modem_mode mode) override
{
switch (mode) {
case esp_modem::modem_mode::DATA_MODE:
if (set_data_mode() != command_result::OK) {
return resume_data_mode() == command_result::OK;
}
return true;
case esp_modem::modem_mode::COMMAND_MODE:
return set_command_mode() == command_result::OK;
default:
return false;
case esp_modem::modem_mode::DATA_MODE:
if (set_data_mode() != command_result::OK) {
return resume_data_mode() == command_result::OK;
}
return true;
case esp_modem::modem_mode::COMMAND_MODE:
return set_command_mode() == command_result::OK;
default:
return false;
}
}
[[maybe_unused]] bool init_sim(const std::string& pin)
[[maybe_unused]] bool init_sim(const std::string &pin)
{
// switch to command mode (in case we were in PPP mode)
static_cast<void>(set_command_mode()); // ignore the potential failure, as we might be in command mode after startup
bool is_pin_ok;
if (read_pin(is_pin_ok) != command_result::OK)
if (read_pin(is_pin_ok) != command_result::OK) {
return false;
}
if (!is_pin_ok) {
if (set_pin(pin) != command_result::OK)
if (set_pin(pin) != command_result::OK) {
return false;
}
vTaskDelay(pdMS_TO_TICKS(1000));
if (read_pin(is_pin_ok) != command_result::OK || !is_pin_ok)
if (read_pin(is_pin_ok) != command_result::OK || !is_pin_ok) {
return false;
}
}
return true;
}
@ -99,12 +103,30 @@ private:
std::shared_ptr<DTE> dte;
std::string apn;
[[nodiscard]] command_result set_pdp_context(PdpContext& pdp) { return dce_commands::set_pdp_context(dte.get(),pdp); }
[[nodiscard]] command_result set_pin(const std::string &pin) { return dce_commands::set_pin(dte.get(), pin); }
[[nodiscard]] command_result read_pin(bool& pin_ok) { return dce_commands::read_pin(dte.get(), pin_ok); }
[[nodiscard]] command_result set_data_mode() { return dce_commands::set_data_mode(dte.get()); }
[[nodiscard]] command_result resume_data_mode() { return dce_commands::resume_data_mode(dte.get()); }
[[nodiscard]] command_result set_command_mode() { return dce_commands::set_command_mode(dte.get()); }
[[nodiscard]] command_result set_pdp_context(PdpContext &pdp)
{
return dce_commands::set_pdp_context(dte.get(), pdp);
}
[[nodiscard]] command_result set_pin(const std::string &pin)
{
return dce_commands::set_pin(dte.get(), pin);
}
[[nodiscard]] command_result read_pin(bool &pin_ok)
{
return dce_commands::read_pin(dte.get(), pin_ok);
}
[[nodiscard]] command_result set_data_mode()
{
return dce_commands::set_data_mode(dte.get());
}
[[nodiscard]] command_result resume_data_mode()
{
return dce_commands::resume_data_mode(dte.get());
}
[[nodiscard]] command_result set_command_mode()
{
return dce_commands::set_command_mode(dte.get());
}
};
@ -132,13 +154,15 @@ extern "C" esp_err_t modem_init_network(esp_netif_t *netif)
// create the specific device (and initialize it)
auto dev = NetDCE_Factory::create_module<NetModule>(&dce_config, uart_dte, netif);
#if CONFIG_EXAMPLE_NEED_SIM_PIN == 1
if (!dev->init_sim(CONFIG_EXAMPLE_SIM_PIN))
if (!dev->init_sim(CONFIG_EXAMPLE_SIM_PIN)) {
return ESP_FAIL;
}
#endif
// now create the DCE from our already existent device
dce = NetDCE_Factory::create<NetModule>(&dce_config, uart_dte, netif, dev);
if (dce == nullptr)
if (dce == nullptr) {
return ESP_FAIL;
}
return ESP_OK;
}

View File

@ -22,15 +22,15 @@ int main()
{
// init the DTE
esp_modem_dte_config_t dte_config = {
.dte_buffer_size = 512,
.task_stack_size = 1024,
.task_priority = 10,
.vfs_config = {}
.dte_buffer_size = 512,
.task_stack_size = 1024,
.task_priority = 10,
.vfs_config = {}
};
#if CONFIG_USE_VFS_UART == 1
struct esp_modem_vfs_uart_creator uart_config = {
.dev_name = "/dev/ttyUSB0",
.uart = {}
.dev_name = "/dev/ttyUSB0",
.uart = {}
};
assert(vfs_create_uart(&uart_config, &dte_config.vfs_config) == true);
#else
@ -39,16 +39,16 @@ int main()
* socat TCP-L:2222 GOPEN:/dev/ttyS0,ispeed=115200,ospeed=1152000,b115200,raw,echo=0
*/
struct esp_modem_vfs_socket_creator socket_config = {
.host_name = "raspberrypi.local",
.port = 2222
.host_name = "raspberrypi.local",
.port = 2222
};
assert(vfs_create_socket(&socket_config, &dte_config.vfs_config) == true);
#endif
auto dte = create_vfs_dte(&dte_config);
esp_netif_config_t netif_config = {
.dev_name = "/dev/net/tun",
.if_name = "tun0"
.dev_name = "/dev/net/tun",
.if_name = "tun0"
};
esp_netif_t *tun_netif = esp_netif_new(&netif_config);
@ -67,12 +67,13 @@ int main()
std::string str;
// dce->set_mode(esp_modem::modem_mode::CMUX_MODE);
dce->get_imsi(str);
ESP_LOGI(TAG, "Modem IMSI number: %s",str.c_str());
ESP_LOGI(TAG, "Modem IMSI number: %s", str.c_str());
dce->get_imei(str);
ESP_LOGI(TAG, "Modem IMEI number: %s",str.c_str());
while (command_result::OK != dce->get_operator_name(str))
{ printf(".\n"); }
ESP_LOGI(TAG, "Operator name: %s",str.c_str());
ESP_LOGI(TAG, "Modem IMEI number: %s", str.c_str());
while (command_result::OK != dce->get_operator_name(str)) {
printf(".\n");
}
ESP_LOGI(TAG, "Operator name: %s", str.c_str());
dce->set_mode(esp_modem::modem_mode::DATA_MODE);

View File

@ -12,35 +12,35 @@
static const char *TAG = "modem_console_helper";
ConsoleCommand::ConsoleCommand(const char* command, const char* help, const std::vector<CommandArgs>& args, std::function<bool(ConsoleCommand *)> f):
func(std::move(f))
ConsoleCommand::ConsoleCommand(const char *command, const char *help, const std::vector<CommandArgs> &args, std::function<bool(ConsoleCommand *)> f):
func(std::move(f))
{
RegisterCommand(command, help, args);
}
void ConsoleCommand::RegisterCommand(const char* command, const char* help, const std::vector<CommandArgs>& args)
void ConsoleCommand::RegisterCommand(const char *command, const char *help, const std::vector<CommandArgs> &args)
{
assert(last_command <= MAX_REPEAT_NR);
arg_type common_arg = { };
for (auto& it: args) {
switch(it.type) {
case ARG_END:
break;
case STR0:
common_arg.str = arg_str0(it.shortopts, it.longopts, it.datatype, it.glossary);
break;
case STR1:
common_arg.str = arg_str1(it.shortopts, it.longopts, it.datatype, it.glossary);
break;
case INT0:
common_arg.intx = arg_int0(it.shortopts, it.longopts, it.datatype, it.glossary);
break;
case INT1:
common_arg.intx = arg_int1(it.shortopts, it.longopts, it.datatype, it.glossary);
break;
case LIT0:
common_arg.lit = arg_lit0(it.shortopts, it.longopts, it.glossary);
break;
for (auto &it : args) {
switch (it.type) {
case ARG_END:
break;
case STR0:
common_arg.str = arg_str0(it.shortopts, it.longopts, it.datatype, it.glossary);
break;
case STR1:
common_arg.str = arg_str1(it.shortopts, it.longopts, it.datatype, it.glossary);
break;
case INT0:
common_arg.intx = arg_int0(it.shortopts, it.longopts, it.datatype, it.glossary);
break;
case INT1:
common_arg.intx = arg_int1(it.shortopts, it.longopts, it.datatype, it.glossary);
break;
case LIT0:
common_arg.lit = arg_lit0(it.shortopts, it.longopts, it.glossary);
break;
}
if (common_arg.is_null()) {
arg_table.emplace_back(common_arg);
@ -53,11 +53,11 @@ void ConsoleCommand::RegisterCommand(const char* command, const char* help, cons
arg_type end = { .end = arg_end(1) };
arg_table.emplace_back(end);
const esp_console_cmd_t command_def = {
.command = command,
.help = help,
.hint = nullptr,
.func = command_func_pts[last_command],
.argtable = &arg_table[0]
.command = command,
.help = help,
.hint = nullptr,
.func = command_func_pts[last_command],
.argtable = &arg_table[0]
};
ESP_ERROR_CHECK(esp_console_cmd_register(&command_def));
last_command++;
@ -86,8 +86,9 @@ int ConsoleCommand::get_int(int index)
}
int ConsoleCommand::command_func(int argc, char **argv) {
arg_type* plain_arg_array = &arg_table[0];
int ConsoleCommand::command_func(int argc, char **argv)
{
arg_type *plain_arg_array = &arg_table[0];
int nerrors = arg_parse(argc, argv, (void **)plain_arg_array);
if (nerrors != 0) {
arg_print_errors(stderr, arg_table.back().end, argv[0]);
@ -122,7 +123,7 @@ const esp_console_cmd_func_t ConsoleCommand::command_func_pts[] = {
#define ITEM_TO_REPEAT(index) StaticCommands::command_func_ ## index ,
_DO_REPEAT_ITEM()
_DO_REPEAT_ITEM()
#undef ITEM_TO_REPEAT
};
@ -130,5 +131,5 @@ const esp_console_cmd_func_t ConsoleCommand::command_func_pts[] = {
/**
* @brief Static members defined for ConsoleCommand
*/
std::vector<ConsoleCommand*> ConsoleCommand::console_commands;
std::vector<ConsoleCommand *> ConsoleCommand::console_commands;
int ConsoleCommand::last_command = 0;

View File

@ -34,10 +34,10 @@ enum arg_type {
* Command argument struct definition for list of arguments of one command
*/
struct CommandArgs {
CommandArgs(arg_type t, const char * shopts, const char * lopts, const char * data, const char * glos):
CommandArgs(arg_type t, const char *shopts, const char *lopts, const char *data, const char *glos):
type(t), shortopts(shopts), longopts(lopts), datatype(data), glossary(glos) {}
CommandArgs(arg_type t, const char * shopts, const char * lopts, const char * glos):
type(t), shortopts(shopts), longopts(lopts), datatype(nullptr), glossary(glos) {}
CommandArgs(arg_type t, const char *shopts, const char *lopts, const char *glos):
type(t), shortopts(shopts), longopts(lopts), datatype(nullptr), glossary(glos) {}
arg_type type;
const char *shortopts;
@ -56,14 +56,16 @@ class ConsoleCommand {
* @brief Common argument types to be stored internally for parsing later
*/
using arg_type =
union {
struct arg_int *intx;
struct arg_str *str;
struct arg_lit *lit;
struct arg_end *end;
void *__raw_ptr;
bool is_null() const { return __raw_ptr; }
};
union {
struct arg_int *intx;
struct arg_str *str;
struct arg_lit *lit;
struct arg_end *end;
void *__raw_ptr;
bool is_null() const {
return __raw_ptr;
}
};
friend class StaticCommands;
public:
@ -75,8 +77,8 @@ public:
* @param srg_struct_size Size of the argument struct
* @param f Function callback for the command
*/
template<typename T> explicit ConsoleCommand(const char* command, const char* help, const T *arg_struct, size_t srg_struct_size,
std::function<bool(ConsoleCommand *)> f): func(std::move(f))
template<typename T> explicit ConsoleCommand(const char *command, const char *help, const T *arg_struct, size_t srg_struct_size,
std::function<bool(ConsoleCommand *)> f): func(std::move(f))
{
size_t args_plain_size = srg_struct_size / sizeof(CommandArgs);
auto first_arg = reinterpret_cast<const CommandArgs *>(arg_struct);
@ -87,27 +89,38 @@ public:
/**
* @brief Another method of Console command definitions using vector arg struct
*/
explicit ConsoleCommand(const char* command, const char* help, const std::vector<CommandArgs>& args, std::function<bool(ConsoleCommand *)> f);
explicit ConsoleCommand(const char *command, const char *help, const std::vector<CommandArgs> &args, std::function<bool(ConsoleCommand *)> f);
/**
* @brief Utility getters of various params from the argument list
*/
template<typename T> int get_count_of(CommandArgs T::*member) { return get_count(index_arg(member)); }
template<typename T> std::string get_string_of(CommandArgs T::*member) { return get_string(index_arg(member)); }
template<typename T> int get_int_of(CommandArgs T::*member) { return get_int(index_arg(member)); }
template<typename T> int get_count_of(CommandArgs T::*member)
{
return get_count(index_arg(member));
}
template<typename T> std::string get_string_of(CommandArgs T::*member)
{
return get_string(index_arg(member));
}
template<typename T> int get_int_of(CommandArgs T::*member)
{
return get_int(index_arg(member));
}
std::string get_string(int index);
int get_int(int index);
private:
int get_count(int index);
void RegisterCommand(const char* command, const char* help, const std::vector<CommandArgs>& args);
void RegisterCommand(const char *command, const char *help, const std::vector<CommandArgs> &args);
template<typename T> static constexpr size_t index_arg(CommandArgs T::*member)
{ return ((uint8_t *)&((T*)nullptr->*member) - (uint8_t *)nullptr)/sizeof(CommandArgs); }
{
return ((uint8_t *) & ((T *)nullptr->*member) - (uint8_t *)nullptr) / sizeof(CommandArgs);
}
std::vector<arg_type> arg_table;
int command_func(int argc, char **argv);
static int last_command;
static std::vector<ConsoleCommand*> console_commands;
static std::vector<ConsoleCommand *> console_commands;
std::function<bool(ConsoleCommand *)> func;
const static esp_console_cmd_func_t command_func_pts[];
};

View File

@ -73,7 +73,7 @@ extern "C" void app_main(void)
SetModeArgs(): mode(STR1, nullptr, nullptr, "<mode>", "PPP, CMD or CMUX") {}
CommandArgs mode;
} set_mode_args;
const ConsoleCommand SetModeParser("set_mode", "sets modem mode", &set_mode_args, sizeof(set_mode_args), [&](ConsoleCommand *c){
const ConsoleCommand SetModeParser("set_mode", "sets modem mode", &set_mode_args, sizeof(set_mode_args), [&](ConsoleCommand * c) {
if (c->get_count_of(&SetModeArgs::mode)) {
auto mode = c->get_string_of(&SetModeArgs::mode);
modem_mode dev_mode;
@ -101,7 +101,7 @@ extern "C" void app_main(void)
SetPinArgs(): pin(STR1, nullptr, nullptr, "<pin>", "PIN") {}
CommandArgs pin;
} set_pin_args;
const ConsoleCommand SetPinParser("set_pin", "sets SIM card PIN", &set_pin_args, sizeof(set_pin_args), [&](ConsoleCommand *c){
const ConsoleCommand SetPinParser("set_pin", "sets SIM card PIN", &set_pin_args, sizeof(set_pin_args), [&](ConsoleCommand * c) {
if (c->get_count_of(&SetPinArgs::pin)) {
auto pin = c->get_string_of(&SetPinArgs::pin);
ESP_LOGI(TAG, "Setting pin=%s...", pin.c_str());
@ -109,7 +109,7 @@ extern "C" void app_main(void)
if (err == command_result::OK) {
ESP_LOGI(TAG, "OK");
} else {
ESP_LOGE(TAG, "Failed %s", err == command_result::TIMEOUT ? "TIMEOUT":"");
ESP_LOGE(TAG, "Failed %s", err == command_result::TIMEOUT ? "TIMEOUT" : "");
return 1;
}
}
@ -117,26 +117,26 @@ extern "C" void app_main(void)
});
const std::vector<CommandArgs> no_args;
const ConsoleCommand ReadPinArgs("read_pin", "checks if SIM is unlocked", no_args, [&](ConsoleCommand *c){
const ConsoleCommand ReadPinArgs("read_pin", "checks if SIM is unlocked", no_args, [&](ConsoleCommand * c) {
bool pin_ok;
ESP_LOGI(TAG, "Checking pin...");
auto err = dce->read_pin(pin_ok);
if (err == command_result::OK) {
ESP_LOGI(TAG, "OK. Pin status: %s", pin_ok ? "true": "false");
ESP_LOGI(TAG, "OK. Pin status: %s", pin_ok ? "true" : "false");
} else {
ESP_LOGE(TAG, "Failed %s", err == command_result::TIMEOUT ? "TIMEOUT":"");
ESP_LOGE(TAG, "Failed %s", err == command_result::TIMEOUT ? "TIMEOUT" : "");
return 1;
}
return 0;
});
const ConsoleCommand GetModuleName("get_module_name", "reads the module name", no_args, [&](ConsoleCommand *c){
const ConsoleCommand GetModuleName("get_module_name", "reads the module name", no_args, [&](ConsoleCommand * c) {
std::string module_name;
ESP_LOGI(TAG, "Reading module name...");
CHECK_ERR(dce->get_module_name(module_name), ESP_LOGI(TAG, "OK. Module name: %s", module_name.c_str()));
});
const ConsoleCommand GetOperatorName("get_operator_name", "reads the operator name", no_args, [&](ConsoleCommand *c){
const ConsoleCommand GetOperatorName("get_operator_name", "reads the operator name", no_args, [&](ConsoleCommand * c) {
std::string operator_name;
ESP_LOGI(TAG, "Reading operator name...");
CHECK_ERR(dce->get_operator_name(operator_name), ESP_LOGI(TAG, "OK. Operator name: %s", operator_name.c_str()));
@ -153,10 +153,10 @@ extern "C" void app_main(void)
CommandArgs pattern;
CommandArgs no_cr;
} send_cmd_args;
const ConsoleCommand SendCommand("cmd", "sends generic AT command, no_args", &send_cmd_args, sizeof(send_cmd_args), [&](ConsoleCommand *c) {
const ConsoleCommand SendCommand("cmd", "sends generic AT command, no_args", &send_cmd_args, sizeof(send_cmd_args), [&](ConsoleCommand * c) {
auto cmd = c->get_string_of(&GenericCommandArgs::cmd);
auto timeout = c->get_count_of(&GenericCommandArgs::timeout) ? c->get_int_of(&GenericCommandArgs::timeout)
: 1000;
: 1000;
ESP_LOGI(TAG, "Sending command %s with timeout %d", cmd.c_str(), timeout);
auto pattern = c->get_string_of(&GenericCommandArgs::pattern);
if (c->get_count_of(&GenericCommandArgs::no_cr) == 0) {
@ -165,27 +165,29 @@ extern "C" void app_main(void)
CHECK_ERR(dce->command(cmd, [&](uint8_t *data, size_t len) {
std::string response((char *) data, len);
ESP_LOGI(TAG, "%s", response.c_str());
if (pattern.empty() || response.find(pattern) != std::string::npos)
if (pattern.empty() || response.find(pattern) != std::string::npos) {
return command_result::OK;
if (response.find(pattern) != std::string::npos)
}
if (response.find(pattern) != std::string::npos) {
return command_result::OK;
}
return command_result::TIMEOUT;
}, timeout),);
});
const ConsoleCommand GetSignalQuality("get_signal_quality", "Gets signal quality", no_args, [&](ConsoleCommand *c){
const ConsoleCommand GetSignalQuality("get_signal_quality", "Gets signal quality", no_args, [&](ConsoleCommand * c) {
int rssi, ber;
CHECK_ERR(dce->get_signal_quality(rssi, ber), ESP_LOGI(TAG, "OK. rssi=%d, ber=%d", rssi, ber));
});
const ConsoleCommand GetBatteryStatus("get_battery_status", "Reads voltage/battery status", no_args, [&](ConsoleCommand *c){
const ConsoleCommand GetBatteryStatus("get_battery_status", "Reads voltage/battery status", no_args, [&](ConsoleCommand * c) {
int volt, bcl, bcs;
CHECK_ERR(dce->get_battery_status(volt, bcl, bcs), ESP_LOGI(TAG, "OK. volt=%d, bcl=%d, bcs=%d", volt, bcl, bcs));
});
const ConsoleCommand PowerDown("power_down", "power down the module", no_args, [&](ConsoleCommand *c){
const ConsoleCommand PowerDown("power_down", "power down the module", no_args, [&](ConsoleCommand * c) {
ESP_LOGI(TAG, "Power down the module...");
CHECK_ERR(dce->power_down(), ESP_LOGI(TAG, "OK"));
});
const ConsoleCommand Reset("reset", "reset the module", no_args, [&](ConsoleCommand *c){
const ConsoleCommand Reset("reset", "reset the module", no_args, [&](ConsoleCommand * c) {
ESP_LOGI(TAG, "Resetting the module...");
CHECK_ERR(dce->reset(), ESP_LOGI(TAG, "OK"));
});
@ -193,7 +195,7 @@ extern "C" void app_main(void)
SetApn(): apn(STR1, nullptr, nullptr, "<apn>", "APN (Access Point Name)") {}
CommandArgs apn;
} set_apn;
const ConsoleCommand SetApnParser("set_apn", "sets APN", &set_apn, sizeof(set_apn), [&](ConsoleCommand *c){
const ConsoleCommand SetApnParser("set_apn", "sets APN", &set_apn, sizeof(set_apn), [&](ConsoleCommand * c) {
if (c->get_count_of(&SetApn::apn)) {
auto apn = c->get_string_of(&SetApn::apn);
ESP_LOGI(TAG, "Setting the APN=%s...", apn.c_str());
@ -205,7 +207,7 @@ extern "C" void app_main(void)
});
SignalGroup exit_signal;
const ConsoleCommand ExitConsole("exit", "exit the console application", no_args, [&](ConsoleCommand *c){
const ConsoleCommand ExitConsole("exit", "exit the console application", no_args, [&](ConsoleCommand * c) {
ESP_LOGI(TAG, "Exiting...");
exit_signal.set(1);
s_repl->del(s_repl);

View File

@ -20,7 +20,7 @@
class MyShinyModem: public esp_modem::GenericModule {
using GenericModule::GenericModule;
public:
esp_modem::command_result get_module_name(std::string& name) override
esp_modem::command_result get_module_name(std::string &name) override
{
name = "Custom Shiny Module";
return esp_modem::command_result::OK;
@ -32,8 +32,8 @@ public:
* @return unique pointer of the resultant DCE
*/
std::unique_ptr<esp_modem::DCE> create_shiny_dce(const esp_modem::dce_config *config,
std::shared_ptr<esp_modem::DTE> dte,
esp_netif_t *netif)
std::shared_ptr<esp_modem::DTE> dte,
esp_netif_t *netif)
{
return esp_modem::dce_factory::Factory::build_unique<MyShinyModem>(config, std::move(dte), netif);
}

View File

@ -122,10 +122,10 @@ extern "C" void app_main(void)
event_handler.listen_to(MqttClient::get_event(MqttClient::Event::DATA));
auto reg = loop->register_event(MqttClient::get_event(MqttClient::Event::DATA),
[&mqtt](const ESPEvent &event, void *data) {
std::cout << " TOPIC:" << mqtt.get_topic(data) << std::endl;
std::cout << " DATA:" << mqtt.get_data(data) << std::endl;
});
[&mqtt](const ESPEvent & event, void *data) {
std::cout << " TOPIC:" << mqtt.get_topic(data) << std::endl;
std::cout << " DATA:" << mqtt.get_data(data) << std::endl;
});
mqtt.connect();
while (true) {
result = event_handler.wait_event_for(std::chrono::milliseconds(60000));

View File

@ -22,9 +22,8 @@ ESP_EVENT_DECLARE_BASE(MQTT_EVENTS);
/**
* Thin wrapper around C mqtt_client
*/
struct MqttClientHandle
{
explicit MqttClientHandle(const std::string & uri)
struct MqttClientHandle {
explicit MqttClientHandle(const std::string &uri)
{
esp_mqtt_client_config_t config = { };
config.uri = uri.c_str();
@ -49,9 +48,9 @@ struct MqttClientHandle
/**
* @brief Definitions of MqttClient methods
*/
MqttClient::MqttClient(const std::string & uri):
MqttClient::MqttClient(const std::string &uri):
h(std::unique_ptr<MqttClientHandle>(new MqttClientHandle(uri)))
{}
{}
void MqttClient::connect()
{
@ -61,11 +60,11 @@ void MqttClient::connect()
idf::event::ESPEvent MqttClient::get_event(MqttClient::Event ev)
{
switch (ev) {
case Event::CONNECT: {
return { MQTT_EVENTS, ESPEventID(MQTT_EVENT_CONNECTED) };
}
case Event::DATA:
return { MQTT_EVENTS, ESPEventID(MQTT_EVENT_DATA) };
case Event::CONNECT: {
return { MQTT_EVENTS, ESPEventID(MQTT_EVENT_CONNECTED) };
}
case Event::DATA:
return { MQTT_EVENTS, ESPEventID(MQTT_EVENT_DATA) };
}
return { };
}
@ -80,7 +79,7 @@ int MqttClient::subscribe(const std::string &topic, int qos)
return esp_mqtt_client_subscribe(h->client, topic.c_str(), qos);
}
std::string MqttClient::get_topic(void * event_data)
std::string MqttClient::get_topic(void *event_data)
{
auto event = (esp_mqtt_event_handle_t)event_data;
if (event == nullptr || event->client != h->client)
@ -89,7 +88,7 @@ std::string MqttClient::get_topic(void * event_data)
return std::string(event->topic, event->topic_len);
}
std::string MqttClient::get_data(void * event_data)
std::string MqttClient::get_data(void *event_data)
{
auto event = (esp_mqtt_event_handle_t)event_data;
if (event == nullptr || event->client != h->client)

View File

@ -24,7 +24,7 @@ public:
DATA,
};
explicit MqttClient(const std::string & uri);
explicit MqttClient(const std::string &uri);
~MqttClient();
/**
@ -39,7 +39,7 @@ public:
* @param qos QoS (0 by default)
* @return message id
*/
int publish(const std::string & topic, const std::string & data, int qos = 0);
int publish(const std::string &topic, const std::string &data, int qos = 0);
/**
* @brief Subscribe to a topic
@ -47,7 +47,7 @@ public:
* @param qos QoS (0 by default)
* @return message id
*/
int subscribe(const std::string & topic, int qos = 0);
int subscribe(const std::string &topic, int qos = 0);
/**
* @brief Get topic from event data