mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-17 20:42:21 +02:00
Added C API example
This commit is contained in:
@ -460,6 +460,10 @@ extern "C" void app_main(void)
|
||||
}, timeout),);
|
||||
});
|
||||
|
||||
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));
|
||||
});
|
||||
// start console REPL
|
||||
ESP_ERROR_CHECK(esp_console_start_repl(s_repl));
|
||||
ESP_LOGE(TAG, "Exit console!!!");
|
||||
|
@ -12,124 +12,17 @@
|
||||
#include "esp_netif.h"
|
||||
#include "esp_netif_ppp.h"
|
||||
#include "mqtt_client.h"
|
||||
#include "esp_modem.h"
|
||||
#include "esp_modem_netif.h"
|
||||
#include "esp_modem_api.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#if defined(CONFIG_EXAMPLE_MODEM_LEGACY_API)
|
||||
#include "sim800.h"
|
||||
#include "bg96.h"
|
||||
#include "sim7600.h"
|
||||
#endif
|
||||
|
||||
#define BROKER_URL "mqtt://mqtt.eclipse.org"
|
||||
#define BROKER_URL "mqtt://mqtt.eclipseprojects.io"
|
||||
|
||||
static const char *TAG = "pppos_example";
|
||||
static EventGroupHandle_t event_group = NULL;
|
||||
static const int CONNECT_BIT = BIT0;
|
||||
static const int STOP_BIT = BIT1;
|
||||
//static const int STOP_BIT = BIT1;
|
||||
static const int GOT_DATA_BIT = BIT2;
|
||||
|
||||
#if CONFIG_EXAMPLE_SEND_MSG
|
||||
/**
|
||||
* @brief This example will also show how to send short message using the infrastructure provided by esp modem library.
|
||||
* @note Not all modem support SMG.
|
||||
*
|
||||
*/
|
||||
static esp_err_t example_default_handle(esp_modem_dce_t *dce, const char *line)
|
||||
{
|
||||
esp_err_t err = ESP_FAIL;
|
||||
if (strstr(line, MODEM_RESULT_CODE_SUCCESS)) {
|
||||
err = esp_modem_process_command_done(dce, MODEM_STATE_SUCCESS);
|
||||
} else if (strstr(line, MODEM_RESULT_CODE_ERROR)) {
|
||||
err = esp_modem_process_command_done(dce, MODEM_STATE_FAIL);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
static esp_err_t example_handle_cmgs(esp_modem_dce_t *dce, const char *line)
|
||||
{
|
||||
esp_err_t err = ESP_FAIL;
|
||||
if (strstr(line, MODEM_RESULT_CODE_SUCCESS)) {
|
||||
err = esp_modem_process_command_done(dce, MODEM_STATE_SUCCESS);
|
||||
} else if (strstr(line, MODEM_RESULT_CODE_ERROR)) {
|
||||
err = esp_modem_process_command_done(dce, MODEM_STATE_FAIL);
|
||||
} else if (!strncmp(line, "+CMGS", strlen("+CMGS"))) {
|
||||
err = ESP_OK;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
#define MODEM_SMS_MAX_LENGTH (128)
|
||||
#define MODEM_COMMAND_TIMEOUT_SMS_MS (120000)
|
||||
#define MODEM_PROMPT_TIMEOUT_MS (10)
|
||||
|
||||
static esp_err_t example_send_message_text(modem_dce_t *user_dce, const char *phone_num, const char *text)
|
||||
{
|
||||
esp_modem_dce_t *dce = &user_dce->parent;
|
||||
modem_dte_t *dte = dce->dte;
|
||||
dce->handle_line = example_default_handle;
|
||||
/* Set text mode */
|
||||
if (dte->send_cmd(dte, "AT+CMGF=1\r", MODEM_COMMAND_TIMEOUT_DEFAULT) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "send command failed");
|
||||
goto err;
|
||||
}
|
||||
if (dce->state != MODEM_STATE_SUCCESS) {
|
||||
ESP_LOGE(TAG, "set message format failed");
|
||||
goto err;
|
||||
}
|
||||
ESP_LOGD(TAG, "set message format ok");
|
||||
/* Specify character set */
|
||||
dce->handle_line = example_default_handle;
|
||||
if (dte->send_cmd(dte, "AT+CSCS=\"GSM\"\r", MODEM_COMMAND_TIMEOUT_DEFAULT) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "send command failed");
|
||||
goto err;
|
||||
}
|
||||
if (dce->state != MODEM_STATE_SUCCESS) {
|
||||
ESP_LOGE(TAG, "set character set failed");
|
||||
goto err;
|
||||
}
|
||||
ESP_LOGD(TAG, "set character set ok");
|
||||
/* send message */
|
||||
char command[MODEM_SMS_MAX_LENGTH] = {0};
|
||||
int length = snprintf(command, MODEM_SMS_MAX_LENGTH, "AT+CMGS=\"%s\"\r", phone_num);
|
||||
/* set phone number and wait for "> " */
|
||||
dte->send_wait(dte, command, length, "\r\n> ", MODEM_PROMPT_TIMEOUT_MS);
|
||||
/* end with CTRL+Z */
|
||||
snprintf(command, MODEM_SMS_MAX_LENGTH, "%s\x1A", text);
|
||||
dce->handle_line = example_handle_cmgs;
|
||||
if (dte->send_cmd(dte, command, MODEM_COMMAND_TIMEOUT_SMS_MS) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "send command failed");
|
||||
goto err;
|
||||
}
|
||||
if (dce->state != MODEM_STATE_SUCCESS) {
|
||||
ESP_LOGE(TAG, "send message failed");
|
||||
goto err;
|
||||
}
|
||||
ESP_LOGD(TAG, "send message ok");
|
||||
return ESP_OK;
|
||||
err:
|
||||
return ESP_FAIL;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void modem_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
||||
{
|
||||
switch (event_id) {
|
||||
case ESP_MODEM_EVENT_PPP_START:
|
||||
ESP_LOGI(TAG, "Modem PPP Started");
|
||||
break;
|
||||
case ESP_MODEM_EVENT_PPP_STOP:
|
||||
ESP_LOGI(TAG, "Modem PPP Stopped");
|
||||
xEventGroupSetBits(event_group, STOP_BIT);
|
||||
break;
|
||||
case ESP_MODEM_EVENT_UNKNOWN:
|
||||
ESP_LOGW(TAG, "Unknow line received: %s", (char *)event_data);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
|
||||
{
|
||||
@ -217,8 +110,6 @@ static void on_ip_event(void *arg, esp_event_base_t event_base,
|
||||
}
|
||||
|
||||
|
||||
static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_config_t *dce_config, esp_netif_config_t *ppp_config);
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
|
||||
@ -252,171 +143,47 @@ void app_main(void)
|
||||
esp_netif_config_t netif_ppp_config = ESP_NETIF_DEFAULT_PPP();
|
||||
|
||||
/* Run the modem demo app */
|
||||
return modem_test_app(&dte_config, &dce_config,&netif_ppp_config);
|
||||
}
|
||||
|
||||
#if !defined(CONFIG_EXAMPLE_MODEM_LEGACY_API)
|
||||
static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_config_t *dce_config, esp_netif_config_t *ppp_config)
|
||||
{
|
||||
/* create dte object */
|
||||
esp_modem_dte_t *dte = esp_modem_dte_new(dte_config);
|
||||
assert(dte != NULL);
|
||||
|
||||
/* create dce object */
|
||||
#if CONFIG_EXAMPLE_MODEM_DEVICE_SIM800
|
||||
dce_config->device = ESP_MODEM_DEVICE_SIM800;
|
||||
#elif CONFIG_EXAMPLE_MODEM_DEVICE_BG96
|
||||
dce_config->device = ESP_MODEM_DEVICE_BG96;
|
||||
#elif CONFIG_EXAMPLE_MODEM_DEVICE_SIM7600
|
||||
dce_config->device = ESP_MODEM_DEVICE_SIM7600;
|
||||
#else
|
||||
#error "Unsupported DCE"
|
||||
#endif
|
||||
esp_modem_dce_t *dce = esp_modem_dce_new(dce_config);
|
||||
assert(dce != NULL);
|
||||
|
||||
/* create netif object */
|
||||
esp_netif_t *esp_netif = esp_netif_new(ppp_config);
|
||||
assert(esp_netif);
|
||||
#if !defined(CONFIG_EXAMPLE_MODEM_PPP_AUTH_NONE) && (defined(CONFIG_LWIP_PPP_PAP_SUPPORT) || defined(CONFIG_LWIP_PPP_CHAP_SUPPORT))
|
||||
#if CONFIG_LWIP_PPP_PAP_SUPPORT
|
||||
esp_netif_auth_type_t auth_type = NETIF_PPP_AUTHTYPE_PAP;
|
||||
#elif CONFIG_LWIP_PPP_CHAP_SUPPORT
|
||||
esp_netif_auth_type_t auth_type = NETIF_PPP_AUTHTYPE_CHAP;
|
||||
#else
|
||||
#error "Unsupported AUTH Negotiation"
|
||||
#endif
|
||||
esp_netif_ppp_set_auth(esp_netif, auth_type, CONFIG_EXAMPLE_MODEM_PPP_AUTH_USERNAME, CONFIG_EXAMPLE_MODEM_PPP_AUTH_PASSWORD);
|
||||
#endif
|
||||
/* Register event handler */
|
||||
ESP_ERROR_CHECK(esp_modem_set_event_handler(dte, modem_event_handler, ESP_EVENT_ANY_ID, NULL));
|
||||
|
||||
/* attach the DCE, DTE, netif to initialize the modem */
|
||||
ESP_ERROR_CHECK(esp_modem_default_attach(dte, dce, esp_netif));
|
||||
|
||||
while (1) {
|
||||
ESP_ERROR_CHECK(esp_modem_default_start(dte));
|
||||
/* Start PPP mode */
|
||||
ESP_ERROR_CHECK(esp_modem_start_ppp(dte));
|
||||
/* Wait for IP address */
|
||||
xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
|
||||
/* Config MQTT */
|
||||
esp_mqtt_client_config_t mqtt_config = {
|
||||
.uri = BROKER_URL,
|
||||
.event_handle = mqtt_event_handler,
|
||||
};
|
||||
esp_mqtt_client_handle_t mqtt_client = esp_mqtt_client_init(&mqtt_config);
|
||||
esp_mqtt_client_start(mqtt_client);
|
||||
xEventGroupWaitBits(event_group, GOT_DATA_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
esp_mqtt_client_destroy(mqtt_client);
|
||||
|
||||
/* Exit PPP mode */
|
||||
ESP_ERROR_CHECK(esp_modem_stop_ppp(dte));
|
||||
|
||||
xEventGroupWaitBits(event_group, STOP_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
ESP_LOGI(TAG, "Restart after 60 seconds");
|
||||
vTaskDelay(pdMS_TO_TICKS(60000));
|
||||
}
|
||||
|
||||
/* Default destroy all modem sub-units attached to it (DTE, DCE, netif) */
|
||||
ESP_ERROR_CHECK(esp_modem_default_destroy(dte));
|
||||
}
|
||||
#else // defined(CONFIG_EXAMPLE_MODEM_LEGACY_API)
|
||||
|
||||
static void modem_test_app(esp_modem_dte_config_t *dte_config, esp_modem_dce_config_t *dce_config, esp_netif_config_t *ppp_config)
|
||||
{
|
||||
/* create dte object */
|
||||
modem_dte_t *dte = esp_modem_dte_init(dte_config);
|
||||
/* Register event handler */
|
||||
ESP_ERROR_CHECK(esp_modem_set_event_handler(dte, modem_event_handler, ESP_EVENT_ANY_ID, NULL));
|
||||
|
||||
// Init netif object
|
||||
esp_netif_t *esp_netif = esp_netif_new(ppp_config);
|
||||
esp_netif_t *esp_netif = esp_netif_new(&netif_ppp_config);
|
||||
assert(esp_netif);
|
||||
esp_modem_dce_t *dce = esp_modem_new(&dte_config, &dce_config, esp_netif);
|
||||
int rssi, ber;
|
||||
esp_err_t err = esp_modem_get_signal_quality(dce, &rssi, &ber);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_modem_get_signal_quality failed with %d", err);
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(TAG, "Signal quality: rssi=%d, ber=%d", rssi, ber);
|
||||
|
||||
void *modem_netif_adapter = esp_modem_netif_setup(dte);
|
||||
esp_modem_netif_set_default_handlers(modem_netif_adapter, esp_netif);
|
||||
|
||||
while (1) {
|
||||
modem_dce_t *dce = NULL;
|
||||
/* create dce object */
|
||||
#if CONFIG_EXAMPLE_MODEM_DEVICE_SIM800
|
||||
dce = sim800_init(dte);
|
||||
#elif CONFIG_EXAMPLE_MODEM_DEVICE_BG96
|
||||
dce = bg96_init(dte);
|
||||
#elif CONFIG_EXAMPLE_MODEM_DEVICE_SIM7600
|
||||
dce = sim7600_init(dte);
|
||||
#else
|
||||
#error "Unsupported DCE"
|
||||
#endif
|
||||
assert(dce != NULL);
|
||||
ESP_ERROR_CHECK(dce->set_flow_ctrl(dce, ESP_MODEM_FLOW_CONTROL_NONE));
|
||||
ESP_ERROR_CHECK(dce->store_profile(dce));
|
||||
/* Print Module ID, Operator, IMEI, IMSI */
|
||||
ESP_LOGI(TAG, "Module: %s", dce->name);
|
||||
ESP_LOGI(TAG, "Operator: %s", dce->oper);
|
||||
ESP_LOGI(TAG, "IMEI: %s", dce->imei);
|
||||
ESP_LOGI(TAG, "IMSI: %s", dce->imsi);
|
||||
|
||||
/* Get signal quality */
|
||||
uint32_t rssi = 0, ber = 0;
|
||||
ESP_ERROR_CHECK(dce->get_signal_quality(dce, &rssi, &ber));
|
||||
ESP_LOGI(TAG, "rssi: %d, ber: %d", rssi, ber);
|
||||
/* Get battery voltage */
|
||||
uint32_t voltage = 0, bcs = 0, bcl = 0;
|
||||
ESP_ERROR_CHECK(dce->get_battery_status(dce, &bcs, &bcl, &voltage));
|
||||
ESP_LOGI(TAG, "Battery voltage: %d mV", voltage);
|
||||
/* setup PPPoS network parameters */
|
||||
#if !defined(CONFIG_EXAMPLE_MODEM_PPP_AUTH_NONE) && (defined(CONFIG_LWIP_PPP_PAP_SUPPORT) || defined(CONFIG_LWIP_PPP_CHAP_SUPPORT))
|
||||
#if CONFIG_LWIP_PPP_PAP_SUPPORT
|
||||
esp_netif_auth_type_t auth_type = NETIF_PPP_AUTHTYPE_PAP;
|
||||
#elif CONFIG_LWIP_PPP_CHAP_SUPPORT
|
||||
esp_netif_auth_type_t auth_type = NETIF_PPP_AUTHTYPE_CHAP;
|
||||
#else
|
||||
#error "Unsupported AUTH Negotiation"
|
||||
#endif
|
||||
esp_netif_ppp_set_auth(esp_netif, auth_type, CONFIG_EXAMPLE_MODEM_PPP_AUTH_USERNAME, CONFIG_EXAMPLE_MODEM_PPP_AUTH_PASSWORD);
|
||||
#endif
|
||||
/* attach the modem to the network interface */
|
||||
esp_netif_attach(esp_netif, modem_netif_adapter);
|
||||
/* Wait for IP address */
|
||||
xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
|
||||
/* Config MQTT */
|
||||
esp_mqtt_client_config_t mqtt_config = {
|
||||
err = esp_modem_set_mode(dce, ESP_MODEM_MODE_DATA);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_DATA) failed with %d", err);
|
||||
return;
|
||||
}
|
||||
/* Wait for IP address */
|
||||
xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
/* Config MQTT */
|
||||
esp_mqtt_client_config_t mqtt_config = {
|
||||
.uri = BROKER_URL,
|
||||
.event_handle = mqtt_event_handler,
|
||||
};
|
||||
esp_mqtt_client_handle_t mqtt_client = esp_mqtt_client_init(&mqtt_config);
|
||||
esp_mqtt_client_start(mqtt_client);
|
||||
xEventGroupWaitBits(event_group, GOT_DATA_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
esp_mqtt_client_destroy(mqtt_client);
|
||||
|
||||
/* Exit PPP mode */
|
||||
ESP_ERROR_CHECK(esp_modem_stop_ppp(dte));
|
||||
|
||||
xEventGroupWaitBits(event_group, STOP_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
#if CONFIG_EXAMPLE_SEND_MSG
|
||||
const char *message = "Welcome to ESP32!";
|
||||
ESP_ERROR_CHECK(example_send_message_text(dce, CONFIG_EXAMPLE_SEND_MSG_PEER_PHONE_NUMBER, message));
|
||||
ESP_LOGI(TAG, "Send send message [%s] ok", message);
|
||||
#endif
|
||||
/* Power down module */
|
||||
ESP_ERROR_CHECK(dce->power_down(dce));
|
||||
ESP_LOGI(TAG, "Power down");
|
||||
ESP_ERROR_CHECK(dce->deinit(dce));
|
||||
|
||||
ESP_LOGI(TAG, "Restart after 60 seconds");
|
||||
vTaskDelay(pdMS_TO_TICKS(60000));
|
||||
};
|
||||
esp_mqtt_client_handle_t mqtt_client = esp_mqtt_client_init(&mqtt_config);
|
||||
esp_mqtt_client_start(mqtt_client);
|
||||
xEventGroupWaitBits(event_group, GOT_DATA_BIT, pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
esp_mqtt_client_destroy(mqtt_client);
|
||||
err = esp_modem_set_mode(dce, ESP_MODEM_MODE_COMMAND);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_COMMAND) failed with %d", err);
|
||||
return;
|
||||
}
|
||||
char imsi[32];
|
||||
err = esp_modem_get_imsi(dce, imsi);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_modem_get_imsi failed with %d", err);
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(TAG, "IMSI=%s", imsi);
|
||||
|
||||
/* Unregister events, destroy the netif adapter and destroy its esp-netif instance */
|
||||
esp_modem_netif_clear_default_handlers(modem_netif_adapter);
|
||||
esp_modem_netif_teardown(modem_netif_adapter);
|
||||
esp_modem_destroy(dce);
|
||||
esp_netif_destroy(esp_netif);
|
||||
|
||||
ESP_ERROR_CHECK(dte->deinit(dte));
|
||||
}
|
||||
|
||||
#endif // CONFIG_EXAMPLE_MODEM_LEGACY_API
|
||||
|
@ -16,25 +16,12 @@
|
||||
#define _ESP_MODEM_API_H_
|
||||
|
||||
#include "generate/esp_modem_command_declare.inc"
|
||||
#include "esp_modem_c_api_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct esp_modem_dce_wrap esp_modem_dce_t;
|
||||
typedef struct esp_modem_dte_config esp_modem_dte_config_t;
|
||||
struct PdpContext;
|
||||
typedef enum esp_modem_dce_mode
|
||||
{
|
||||
ESP_MODEM_MODE_COMMAND,
|
||||
ESP_MODEM_MODE_DATA,
|
||||
} esp_modem_dce_mode_t;
|
||||
|
||||
esp_modem_dce_t *esp_modem_new(const esp_modem_dte_config_t *dte_config, const esp_modem_dce_config_t *dce_config, esp_netif_t *netif);
|
||||
|
||||
void esp_modem_destroy(esp_modem_dce_t * dce);
|
||||
esp_err_t esp_modem_set_mode(esp_modem_dce_t * dce, esp_modem_dce_mode_t mode);
|
||||
|
||||
|
||||
#define ESP_MODEM_DECLARE_DCE_COMMAND(name, return_type, TEMPLATE_ARG, MUX_ARG, ...) \
|
||||
esp_err_t esp_modem_ ## name(esp_modem_dce_t *dce, ##__VA_ARGS__);
|
||||
|
43
esp_modem/include/esp_modem_c_api_types.h
Normal file
43
esp_modem/include/esp_modem_c_api_types.h
Normal file
@ -0,0 +1,43 @@
|
||||
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _ESP_MODEM_C_API_TYPES_H_
|
||||
#define _ESP_MODEM_C_API_TYPES_H_
|
||||
|
||||
#include "esp_modem_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct esp_modem_dce_wrap esp_modem_dce_t;
|
||||
|
||||
struct PdpContext;
|
||||
typedef enum esp_modem_dce_mode
|
||||
{
|
||||
ESP_MODEM_MODE_COMMAND,
|
||||
ESP_MODEM_MODE_DATA,
|
||||
} esp_modem_dce_mode_t;
|
||||
|
||||
esp_modem_dce_t *esp_modem_new(const esp_modem_dte_config_t *dte_config, const esp_modem_dce_config_t *dce_config, esp_netif_t *netif);
|
||||
|
||||
void esp_modem_destroy(esp_modem_dce_t * dce);
|
||||
esp_err_t esp_modem_set_mode(esp_modem_dce_t * dce, esp_modem_dce_mode_t mode);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //_ESP_MODEM_C_API_TYPES_H_
|
@ -21,12 +21,15 @@
|
||||
#define STRING_OUT(x) std::string& x
|
||||
#define BOOL_IN(x) const bool x
|
||||
#define BOOL_OUT(x) bool& x
|
||||
#define INT_OUT(x) int& x
|
||||
|
||||
#define STRUCT_OUT(struct_name, x) struct_name& x
|
||||
#else
|
||||
#define STRING_IN(x) const char* x
|
||||
#define STRING_OUT(x) char* x
|
||||
#define BOOL_IN(x) const bool x
|
||||
#define BOOL_OUT(x) bool* x
|
||||
#define INT_OUT(x) int* x
|
||||
#define STRUCT_OUT(struct_name, x) struct struct_name* x
|
||||
#endif
|
||||
|
||||
@ -74,7 +77,13 @@ ESP_MODEM_DECLARE_DCE_COMMAND(get_module_name, command_result, 1, MUX_ARG, STRIN
|
||||
* @brief Sets the modem to data mode
|
||||
*
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(set_data_mode, command_result, 0, MUX_ARG)
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(set_data_mode, command_result, 0, MUX_ARG) \
|
||||
\
|
||||
/**
|
||||
* @brief Get Signal quality
|
||||
*
|
||||
*/ \
|
||||
ESP_MODEM_DECLARE_DCE_COMMAND(get_signal_quality, command_result, 2, MUX_ARG, INT_OUT(x), INT_OUT(y))
|
||||
|
||||
// --- DCE command documentation starts here ---
|
||||
#ifdef GENERATE_DOCS
|
||||
|
@ -17,9 +17,10 @@
|
||||
#include "esp_log.h"
|
||||
#include "cxx_include/esp_modem_api.hpp"
|
||||
#include "cxx_include/esp_modem_dce_factory.hpp"
|
||||
#include "esp_modem_api.h"
|
||||
#include "esp_modem_c_api_types.h"
|
||||
#include "esp_modem_config.h"
|
||||
#include "exception_stub.hpp"
|
||||
#include "cstring"
|
||||
|
||||
|
||||
namespace esp_modem {
|
||||
@ -59,7 +60,6 @@ std::unique_ptr<DCE> create_BG96_dce(const dce_config *config, std::shared_ptr<D
|
||||
|
||||
//
|
||||
// C API definitions
|
||||
struct PdpContext;
|
||||
using namespace esp_modem;
|
||||
|
||||
struct esp_modem_dce_wrap // need to mimic the polymorphic dispatch as CPP uses templated dispatch
|
||||
@ -108,7 +108,7 @@ extern "C" esp_err_t esp_modem_set_mode(esp_modem_dce_t * dce, esp_modem_dce_mod
|
||||
if (mode == ESP_MODEM_MODE_DATA) {
|
||||
dce_sim7600->set_data();
|
||||
} else if (mode == ESP_MODEM_MODE_COMMAND) {
|
||||
dce_sim7600->set_data();
|
||||
dce_sim7600->exit_data();
|
||||
} else {
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
@ -122,3 +122,21 @@ extern "C" esp_err_t esp_modem_read_pin(esp_modem_dce_t * dce, bool &x)
|
||||
return command_response_to_esp_err(dce_sim7600->read_pin(x));
|
||||
}
|
||||
|
||||
extern "C" esp_err_t esp_modem_get_signal_quality(esp_modem_dce_t * dce, int *rssi, int *ber)
|
||||
{
|
||||
assert(dce->modem_type == esp_modem_dce_wrap::MODEM_SIM7600);
|
||||
auto dce_sim7600 = static_cast<DCE*>(dce->dce_ptr);
|
||||
return command_response_to_esp_err(dce_sim7600->get_signal_quality(*rssi, *ber));
|
||||
}
|
||||
|
||||
extern "C" esp_err_t esp_modem_get_imsi(esp_modem_dce_t * dce, char *p_imsi)
|
||||
{
|
||||
assert(dce->modem_type == esp_modem_dce_wrap::MODEM_SIM7600);
|
||||
auto dce_sim7600 = static_cast<DCE*>(dce->dce_ptr);
|
||||
std::string imsi;
|
||||
auto ret = command_response_to_esp_err(dce_sim7600->get_imsi(imsi));
|
||||
if (ret == ESP_OK && !imsi.empty()) {
|
||||
strcpy(p_imsi, imsi.c_str());
|
||||
}
|
||||
return ret;
|
||||
}
|
@ -42,7 +42,6 @@ static inline command_result generic_command(CommandableIf* t, const std::string
|
||||
|
||||
static inline command_result generic_get_string(CommandableIf* t, const std::string& command, std::string& output, uint32_t timeout_ms)
|
||||
{
|
||||
std::cout << command << std::endl;
|
||||
return t->command(command, [&](uint8_t *data, size_t len) {
|
||||
size_t pos = 0;
|
||||
std::string response((char*)data, len);
|
||||
@ -51,7 +50,6 @@ static inline command_result generic_get_string(CommandableIf* t, const std::str
|
||||
for (auto i = 0; i<2; ++i) // trip trailing \n\r of last two chars
|
||||
if (pos >= 1 && (token[pos-1] == '\r' || token[pos-1] == '\n'))
|
||||
token.pop_back();
|
||||
std::cout << "###" << token << "#### " << std::endl;
|
||||
|
||||
if (token.find("OK") != std::string::npos) {
|
||||
return command_result::OK;
|
||||
@ -164,4 +162,29 @@ command_result set_pin(CommandableIf* t, const std::string& pin)
|
||||
return generic_command_common(t, set_pin_command);
|
||||
}
|
||||
|
||||
command_result get_signal_quality(CommandableIf* t, int &rssi, int &ber)
|
||||
{
|
||||
std::cout << "get_signal_quality" << std::endl;
|
||||
return t->command("AT+CSQ\r", [&](uint8_t *data, size_t len) {
|
||||
size_t pos = 0;
|
||||
std::string response((char*)data, len);
|
||||
while ((pos = response.find('\n')) != std::string::npos) {
|
||||
std::string token = response.substr(0, pos);
|
||||
for (auto i = 0; i<2; ++i) // trip trailing \n\r of last two chars
|
||||
if (pos >= 1 && (token[pos-1] == '\r' || token[pos-1] == '\n'))
|
||||
token.pop_back();
|
||||
|
||||
if (token.find("OK") != std::string::npos) {
|
||||
return command_result::OK;
|
||||
} else if (token.find("ERROR") != std::string::npos) {
|
||||
return command_result::FAIL;
|
||||
} else if (token.find("+CSQ") != std::string::npos) {
|
||||
sscanf(token.c_str(), "%*s%d,%d", &rssi, &ber);
|
||||
}
|
||||
response = response.substr(pos+1);
|
||||
}
|
||||
return command_result::TIMEOUT;
|
||||
}, 500);
|
||||
}
|
||||
|
||||
} // esp_modem::dce_commands
|
@ -13,7 +13,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
#include "cxx_include/esp_modem_dte.hpp"
|
||||
#include <string.h>
|
||||
#include <cstring>
|
||||
#include "esp_log.h"
|
||||
|
||||
using namespace esp_modem;
|
||||
@ -27,7 +27,6 @@ DTE::DTE(std::unique_ptr<Terminal> terminal):
|
||||
|
||||
command_result DTE::command(const std::string &command, got_line_cb got_line, uint32_t time_ms)
|
||||
{
|
||||
// assert(term_id < term->max_virtual_terms());
|
||||
Scoped<Lock> l(lock);
|
||||
command_result res = command_result::TIMEOUT;
|
||||
term->write((uint8_t *)command.c_str(), command.length());
|
||||
@ -37,7 +36,6 @@ command_result DTE::command(const std::string &command, got_line_cb got_line, ui
|
||||
auto actual_len = term->read(data, data_to_read);
|
||||
consumed += actual_len;
|
||||
if (memchr(data, '\n', actual_len)) {
|
||||
// ESP_LOGE("GOT", "LINE");
|
||||
res = got_line(buffer.get(), consumed);
|
||||
if (res == command_result::OK || res == command_result::FAIL) {
|
||||
signal.set(GOT_LINE);
|
||||
|
@ -24,6 +24,7 @@ GenericModule::GenericModule(std::shared_ptr<DTE> dte, const dce_config *config)
|
||||
|
||||
#define ARGS0
|
||||
#define ARGS1 , x
|
||||
#define ARGS2 , x , y
|
||||
#define _ARGS(x) ARGS ## x
|
||||
#define ARGS(x) _ARGS(x)
|
||||
#define TEMPLATE_ARG
|
||||
|
@ -26,7 +26,6 @@ namespace esp_modem {
|
||||
void Netif::on_ppp_changed(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data) {
|
||||
auto *ppp = static_cast<Netif *>(arg);
|
||||
ESP_LOGW("TAG", "PPP state changed event %d", event_id);
|
||||
if (event_id < NETIF_PP_PHASE_OFFSET) {
|
||||
ESP_LOGI("TAG", "PPP state changed event %d", event_id);
|
||||
// only notify the modem on state/error events, ignoring phase transitions
|
||||
@ -54,7 +53,7 @@ esp_err_t Netif::esp_modem_post_attach(esp_netif_t *esp_netif, void *args) {
|
||||
// check if PPP error events are enabled, if not, do enable the error occurred/state changed
|
||||
// to notify the modem layer when switching modes
|
||||
esp_netif_ppp_config_t ppp_config;
|
||||
// esp_netif_ppp_get_params(esp_netif, &ppp_config);
|
||||
esp_netif_ppp_get_params(esp_netif, &ppp_config);
|
||||
if (!ppp_config.ppp_error_event_enabled) {
|
||||
ppp_config.ppp_error_event_enabled = true;
|
||||
esp_netif_ppp_set_params(esp_netif, &ppp_config);
|
||||
@ -88,7 +87,7 @@ void Netif::start() {
|
||||
receive(data, actual_len);
|
||||
return false;
|
||||
});
|
||||
esp_netif_action_start(driver.base.netif, 0, 0, 0);
|
||||
esp_netif_action_start(driver.base.netif, nullptr, 0, nullptr);
|
||||
signal.set(PPP_STARTED);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user