esp_modem: Allow error handler register in C

C++ API offers interface to register error callback.
C API isn't flexible enough, so we offer conversion function between C++ and C enums.
This commit is contained in:
Tomas Rezucha
2022-09-23 08:20:47 +02:00
parent 04c711f757
commit 62cb235aa9
3 changed files with 85 additions and 13 deletions

View File

@ -12,6 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @file c_api_wrapper.hpp
* @brief Collection of C API wrappers
*
* This file is located in include/esp_private because it is not intended for users,
* but rather for esp_modem C extension developers.
*
* The C extension API must provide a 'factory function' that returns initialized pointer to esp_modem_dce_wrap.
* Helper functions provided below, can be used for conversion between C++ enum classes and standard C enums.
*/
#pragma once
#include "cxx_include/esp_modem_dce_factory.hpp"
@ -23,6 +34,8 @@ struct esp_modem_dce_wrap { // need to mimic the polymorphic dispatch as CPP use
enum class modem_wrap_dte_type { UART, VFS, USB } dte_type;
dce_factory::ModemType modem_type;
DCE *dce;
std::shared_ptr<DTE> dte;
esp_modem_dce_wrap() : dce(nullptr), dte(nullptr) {}
};
inline dce_factory::ModemType convert_modem_enum(esp_modem_dce_device_t module)
@ -43,3 +56,32 @@ inline dce_factory::ModemType convert_modem_enum(esp_modem_dce_device_t module)
return esp_modem::dce_factory::ModemType::GenericModule;
}
}
inline esp_modem_terminal_error_t convert_terminal_error_enum(terminal_error err)
{
switch (err) {
case terminal_error::BUFFER_OVERFLOW:
return ESP_MODEM_TERMINAL_BUFFER_OVERFLOW;
case terminal_error::CHECKSUM_ERROR:
return ESP_MODEM_TERMINAL_CHECKSUM_ERROR;
case terminal_error::UNEXPECTED_CONTROL_FLOW:
return ESP_MODEM_TERMINAL_UNEXPECTED_CONTROL_FLOW;
case terminal_error::DEVICE_GONE:
return ESP_MODEM_TERMINAL_DEVICE_GONE;
default:
return ESP_MODEM_TERMINAL_UNKNOWN_ERROR;
}
}
inline esp_err_t command_response_to_esp_err(command_result res)
{
switch (res) {
case command_result::OK:
return ESP_OK;
case command_result::FAIL:
return ESP_FAIL;
case command_result::TIMEOUT:
return ESP_ERR_TIMEOUT;
}
return ESP_ERR_INVALID_ARG;
}