Skip to content

Commit

Permalink
fix(modem): Working prototype of macro based perfect forward
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Nov 4, 2024
1 parent 20d92f3 commit 9b334e7
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ extern "C" void app_main(void)
});
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));
CHECK_ERR(dce->get_module()->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) {
ESP_LOGI(TAG, "Power down the module...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ using namespace esp_modem;
//
// Repeat all declarations and forward to the AT commands defined in esp_modem::dce_commands:: namespace
//
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, arg_nr, ...) \
#define NEED_TO_HANDLE_PARAMS
#undef _ARG
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, ...) \
return_type Shiny::DCE::name(__VA_ARGS__) { return esp_modem::dce_commands::name(this ARGS(arg_nr) ); }

//DECLARE_ALL_COMMAND_APIS(return_type name(...) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DCE : public esp_modem::DCE_T<GenericModule>, public CommandableIf {
return dte->on_read(on_data);
}

#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, num, ...) \
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, ...) \
esp_modem::return_type name(__VA_ARGS__);

// DECLARE_ALL_COMMAND_APIS(forwards name(...))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,30 @@
* @brief Sends the initial AT sequence to sync up with the device
* @return OK, FAIL or TIMEOUT
*/
ESP_MODEM_DECLARE_DCE_COMMAND(sync, command_result, 0)
ESP_MODEM_DECLARE_DCE_COMMAND(sync, command_result)

/**
* @brief Reads the operator name
* @param[out] name operator name
* @param[out] act access technology
* @return OK, FAIL or TIMEOUT
*/
ESP_MODEM_DECLARE_DCE_COMMAND(get_operator_name, command_result, 2, STRING_OUT(p1, name), INT_OUT(p2, act))
ESP_MODEM_DECLARE_DCE_COMMAND(get_operator_name, command_result, STRING_OUT(name), INT_OUT(act))

/**
* @brief Stores current user profile
* @return OK, FAIL or TIMEOUT
*/
ESP_MODEM_DECLARE_DCE_COMMAND(store_profile, command_result, 0)
ESP_MODEM_DECLARE_DCE_COMMAND(store_profile, command_result)

/**
* @brief Sets the supplied PIN code
* @param[in] pin Pin
* @return OK, FAIL or TIMEOUT
*/
ESP_MODEM_DECLARE_DCE_COMMAND(set_pin, command_result, 1, STRING_IN(p1, pin))
ESP_MODEM_DECLARE_DCE_COMMAND(set_pin, command_result, STRING_IN(pin))

#if 1
/**
* @brief Execute the supplied AT command in raw mode (doesn't append '\r' to command, returns everything)
* @param[in] cmd String command that's send to DTE
Expand Down Expand Up @@ -289,7 +290,7 @@ ESP_MODEM_DECLARE_DCE_COMMAND(set_gnss_power_mode, command_result, 1, INT_IN(p1,
*/
ESP_MODEM_DECLARE_DCE_COMMAND(get_gnss_power_mode, command_result, 1, INT_OUT(p1, mode))


#endif

#ifdef GENERATE_DOCS
// cat ../include/generate/esp_modem_command_declare.inc | clang++ -E -P -CC -xc++ -I../include -DGENERATE_DOCS - | sed -n '1,/DCE command documentation/!p'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@


// Parameters
// * handle different parameters for C++ and C API
// * make parameter unique names, so they could be easily referenced and forwarded
#define INT_IN(param, name) int _ARG(param, name)
#define INT_IN(name) int name
#ifdef __cplusplus
#define _ARG(param, name) param
#include <string>
#define STRING_IN(param, name) const std::string& _ARG(param, name)
#define STRING_OUT(param, name) std::string& _ARG(param, name)
#define BOOL_IN(param, name) const bool _ARG(param, name)
#define BOOL_OUT(param, name) bool& _ARG(param, name)
#define INT_OUT(param, name) int& _ARG(param, name)
#define INTEGER_LIST_IN(param, name) const int* _ARG(param, name)

#define STRUCT_OUT(struct_name, p1) struct_name& p1
#define STRING_IN(name) const std::string& name
#define STRING_OUT(name) std::string& name
#define BOOL_IN(name) const bool name
#define BOOL_OUT(name) bool& name
#define INT_OUT(name) int& name
#define INTEGER_LIST_IN(name) const int* name
#define STRUCT_OUT(struct_name, name) struct_name& name
#else
#define _ARG(param, name) name
#define STRING_IN(param, name) const char* _ARG(param, name)
#define STRING_OUT(param, name) char* _ARG(param, name)
#define BOOL_IN(param, name) const bool _ARG(param, name)
#define BOOL_OUT(param, name) bool* _ARG(param, name)
#define INT_OUT(param, name) int* _ARG(param, name)
#define INTEGER_LIST_IN(param, name) const int* _ARG(param, name)
#define STRUCT_OUT(struct_name, p1) esp_modem_ ## struct_name ## _t* p1
#define STRING_IN(name) const char* name
#define STRING_OUT(name) char* name
#define BOOL_IN(name) const bool name
#define BOOL_OUT(name) bool* name
#define INT_OUT(name) int* name
#define INTEGER_LIST_IN(name) const int* name
#define STRUCT_OUT(struct_name, name) esp_modem_ ## struct_name ## _t* name
#endif

#define FORWARD_INT_IN(name) name
#define FORWARD_STRING_IN(name) name
#define FORWARD_STRING_OUT(name) name
#define FORWARD_BOOL_IN(name) name
#define FORWARD_BOOL_OUT(name) name
#define FORWARD_INT_OUT(name) name
#define FORWARD_INTEGER_LIST_IN(name) name
#define FORWARD_STRUCT_OUT(struct_name, name) name
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

#pragma once

#include "esp_modem_dte.hpp"
#include "esp_modem_dce_module.hpp"
#include "esp_modem_types.hpp"
#include "cxx_include/esp_modem_dte.hpp"
#include "cxx_include/esp_modem_dce_module.hpp"
#include "cxx_include/esp_modem_types.hpp"
//#include "generate/esp_modem_command_declare.inc"

// --- ESP-MODEM command module starts here ---
namespace esp_modem {
namespace dce_commands {

Expand Down Expand Up @@ -38,7 +39,7 @@ command_result generic_command(CommandableIf *t, const std::string &command,
/**
* @brief Declaration of all commands is generated from esp_modem_command_declare.inc
*/
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, num, ...) \
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, ...) \
return_type name(CommandableIf *t, ## __VA_ARGS__);

// DECLARE_ALL_COMMAND_APIS(declare name(Commandable *p, ...);)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//#include "cxx_include/esp_modem_dce_template.hpp"
//#include "esp_modem_dce_template.hpp"


// --- ESP-MODEM command module starts here ---
namespace esp_modem {

/**
Expand All @@ -32,7 +32,7 @@ class DCE : public DCE_T<GenericModule> {
public:

using DCE_T<GenericModule>::DCE_T;
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, num, ...) \
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, ...) \
template <typename ...Agrs> \
return_type name(Agrs&&... args) \
{ \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "cxx_include/esp_modem_types.hpp"
#include "esp_modem_dce_config.h"


// --- ESP-MODEM command module starts here ---
namespace esp_modem {

/**
Expand Down Expand Up @@ -110,7 +112,7 @@ class GenericModule: public ModuleIf {
/**
* @brief Common DCE commands generated from the API AT list
*/
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, num, ...) \
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, ...) \
virtual return_type name(__VA_ARGS__);

// DECLARE_ALL_COMMAND_APIS(virtual return_type name(...); )
Expand Down
9 changes: 8 additions & 1 deletion components/esp_modem/scripts/generate.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@

for i in esp_modem_dce_generic.hpp esp_modem_dce_module.hpp esp_modem_command_library.hpp; do
cat ../include/generate/$i | clang++ -E -P -CC -xc++ -I../include - > ../include/commands/cxx_include/$i
out_file="../include/commands/cxx_include/$i";
in_file="../include/generate/$i";
echo "Processing $in_file"
# Process the header and includes -- just paste the content (without expanding)
cat $in_file | sed -n '1,/ESP-MODEM command module starts here/p' > $out_file;
# Now preprocess everything else to expand command prototypes or implementations
cat $in_file | clang++ -E -P -CC -xc++ -I../include - | sed -n '1,/ESP-MODEM command module starts here/!p' >> $out_file;
done
78 changes: 58 additions & 20 deletions components/esp_modem/src/esp_modem_modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,77 @@
#include "cxx_include/esp_modem_dte.hpp"
//#include "generate/esp_modem_command_declare.inc"

// --- ESP-MODEM command module starts here ---
namespace esp_modem {

GenericModule::GenericModule(std::shared_ptr<DTE> dte, const dce_config *config) :
dte(std::move(dte)), pdp(std::make_unique<PdpContext>(config->apn)) {}

//
// Define preprocessor's forwarding to dce_commands definitions
//
// Utility to count arguments (works for up to two parameters here)
#define GET_MACRO(_0, _1, _2, _3, NAME, ...) NAME
#define FOO(...) GET_MACRO(_0, ##__VA_ARGS__, FOO3, FOO2, FOO1, FOO0)(PARAM_, NO_COMMA, NO_COMMA, ##__VA_ARGS__)
#define GOO(...) GET_MACRO(_0, ##__VA_ARGS__, FOO3, FOO2, FOO1, FOO0)(FORWARD_, COMMA, NO_COMMA, ##__VA_ARGS__)
//#define GOO(...) GET_MACRO(_0, ##__VA_ARGS__, GOO3, GOO2, GOO1, GOO0)(__VA_ARGS__)

#define PARAM_STR_OUT(name) std::string& name
#define FORWARD_STR_OUT(name) name
#define PARAM_STR_IN(name) const std::string& name
#define FORWARD_STR_IN(name) name
#define PARAM_INT_OUT(name) int& name
#define FORWARD_INT_OUT(name) name
#define PARAM_INT_IN(name) int name
#define FORWARD_INT_IN(name) name
#define COMMA ,
#define NO_COMMA

#define EVAL(x) x
#define FOO_GENERIC(a, b) EVAL(a ## b)
#define FOO0(prefix, lead_comma, trail_comma)
#define FOO1(prefix, lead_comma, trail_comma, p1) lead_comma FOO_GENERIC(prefix, p1) trail_comma
#define FOO2(prefix, lead_comma, trail_comma, p1, p2) lead_comma FOO_GENERIC(prefix, p1), FOO_GENERIC(prefix, p2) trail_comma
#define FOO3(prefix, lead_comma, trail_comma, p1, p2, p3) lead_comma FOO_GENERIC(prefix, p1), FOO_GENERIC(prefix, p2), FOO_GENERIC(prefix, p3) trail_comma

#define GOO1(p1) , FOO_GENERIC(FORWARD_, p1)

//#define FOO1(p1) EVAL(PARAM_ ## p1)
//#define FOO2(p1, p2) EVAL(PARAM_ ## p1), EVAL(PARAM_ ## p2)
//#define FOO3(p1, p2, p3) EVAL(PARAM_ ## p1), \
// EVAL(PARAM_ ## p2), \
// EVAL(PARAM_ ## p3)

#define GOO0()
//#define GOO1(p1) , EVAL(FORWARD_ ## p1)
#define GOO2(p1, p2) , EVAL(FORWARD_ ## p1), EVAL(FORWARD_ ## p2)
#define GOO3(p1, p2, p3) , EVAL(FORWARD_ ## p1), EVAL(FORWARD_ ## p2), EVAL(FORWARD_ ## p3)


// Helper macros to handle multiple arguments of declared API
#define ARGS0
#define ARGS1 , p1
#define ARGS2 , p1 , p2
#define ARGS3 , p1 , p2 , p3
#define ARGS4 , p1 , p2 , p3, p4
#define ARGS5 , p1 , p2 , p3, p4, p5
#define ARGS6 , p1 , p2 , p3, p4, p5, p6
#define COMMAND(name, type, ...) \
type GenericModule::name(FOO(__VA_ARGS__)) { return esp_modem::dce_commands::name(dte.get() GOO(__VA_ARGS__)); }

#define _ARGS(x) ARGS ## x
#define ARGS(x) _ARGS(x)
COMMAND(sync, command_result)
COMMAND(set_pin, command_result, STR_IN(pin));
COMMAND(get_operator_name, command_result, STR_OUT(name), INT_OUT(act)) // Two arguments
COMMAND(at, command_result, STR_IN(cmd), STR_OUT(out), INT_IN(timeout)) // Three arguments


// Usage examples:
// Zero arguments

// Helper to apply the correct macro to each parameter

//
// Repeat all declarations and forward to the AT commands defined in esp_modem::dce_commands:: namespace
//
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, arg_nr, ...) \
return_type GenericModule::name(__VA_ARGS__) { return esp_modem::dce_commands::name(dte.get() ARGS(arg_nr) ); }

//DECLARE_ALL_COMMAND_APIS(return_type name(...) )
#include "generate/esp_modem_command_declare.inc"
//#define NEED_TO_HANDLE_PARAMS
//#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, ...) \
// return_type GenericModule::name(__VA_ARGS__) { return esp_modem::dce_commands::name(dte.get(), MAP_FORWARDS(__VA_ARGS__) ); }
//
////DECLARE_ALL_COMMAND_APIS(return_type name(...) )
//#include "generate/esp_modem_command_declare.inc"


#undef ESP_MODEM_DECLARE_DCE_COMMAND

#if 1
//
// Handle specific commands for specific supported modems
//
Expand Down Expand Up @@ -89,5 +127,5 @@ command_result BG96::set_pdp_context(esp_modem::PdpContext &pdp)
{
return dce_commands::set_pdp_context(dte.get(), pdp, 300);
}

#endif
}

0 comments on commit 9b334e7

Please sign in to comment.