Skip to content

Commit 95b3389

Browse files
committed
Merge branch 'feature/pppos_sim7600' into 'master'
pppos-client: support for SIM7600 modem Closes IDFGH-3256 See merge request espressif/esp-idf!9333
2 parents fa9f025 + c226270 commit 95b3389

File tree

9 files changed

+188
-24
lines changed

9 files changed

+188
-24
lines changed

examples/protocols/pppos_client/components/modem/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ set(srcs "src/esp_modem.c"
33
"src/esp_modem_netif.c"
44
"src/esp_modem_compat.c"
55
"src/sim800.c"
6+
"src/sim7600.c"
67
"src/bg96.c")
78

89
idf_component_register(SRCS "${srcs}"
910
INCLUDE_DIRS include
11+
PRIV_INCLUDE_DIRS private_include
1012
REQUIRES driver)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
COMPONENT_ADD_INCLUDEDIRS := include
2-
2+
COMPONENT_PRIV_INCLUDEDIRS := private_include
33
COMPONENT_SRCDIRS := src
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
20+
#include "esp_modem_dce_service.h"
21+
#include "esp_modem.h"
22+
23+
/**
24+
* @brief Create and initialize SIM7600 object
25+
*
26+
* @param dte Modem DTE object
27+
* @return modem_dce_t* Modem DCE object
28+
*/
29+
modem_dce_t *sim7600_init(modem_dte_t *dte);
30+
31+
#ifdef __cplusplus
32+
}
33+
#endif
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
16+
/**
17+
* @brief Macro defined for error checking
18+
*
19+
*/
20+
#define DCE_CHECK(a, str, goto_tag, ...) \
21+
do \
22+
{ \
23+
if (!(a)) \
24+
{ \
25+
ESP_LOGE(DCE_TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
26+
goto goto_tag; \
27+
} \
28+
} while (0)
29+
30+
/**
31+
* @brief BG96 Modem
32+
*
33+
*/
34+
typedef struct {
35+
void *priv_resource; /*!< Private resource */
36+
modem_dce_t parent; /*!< DCE parent class */
37+
} bg96_modem_dce_t;

examples/protocols/pppos_client/components/modem/src/bg96.c

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,11 @@
1515
#include <string.h>
1616
#include "esp_log.h"
1717
#include "bg96.h"
18+
#include "bg96_private.h"
1819

1920
#define MODEM_RESULT_CODE_POWERDOWN "POWERED DOWN"
2021

21-
/**
22-
* @brief Macro defined for error checking
23-
*
24-
*/
2522
static const char *DCE_TAG = "bg96";
26-
#define DCE_CHECK(a, str, goto_tag, ...) \
27-
do \
28-
{ \
29-
if (!(a)) \
30-
{ \
31-
ESP_LOGE(DCE_TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
32-
goto goto_tag; \
33-
} \
34-
} while (0)
35-
36-
/**
37-
* @brief BG96 Modem
38-
*
39-
*/
40-
typedef struct {
41-
void *priv_resource; /*!< Private resource */
42-
modem_dce_t parent; /*!< DCE parent class */
43-
} bg96_modem_dce_t;
4423

4524
/**
4625
* @brief Handle response from AT+CSQ

examples/protocols/pppos_client/components/modem/src/esp_modem.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ typedef struct {
6262
int pattern_queue_size; /*!< UART pattern queue size */
6363
} esp_modem_dte_t;
6464

65+
/**
66+
* @brief Returns true if the supplied string contains only CR or LF
67+
*
68+
* @param str string to check
69+
* @param len length of string
70+
*/
71+
static inline bool is_only_cr_lf(const char *str, uint32_t len)
72+
{
73+
for (int i=0; i<len; ++i) {
74+
if (str[i] != '\r' && str[i] != '\n') {
75+
return false;
76+
}
77+
}
78+
return true;
79+
}
6580

6681
esp_err_t esp_modem_set_rx_cb(modem_dte_t *dte, esp_modem_on_receive receive_cb, void *receive_cb_ctx)
6782
{
@@ -85,8 +100,9 @@ static esp_err_t esp_dte_handle_line(esp_modem_dte_t *esp_dte)
85100
modem_dce_t *dce = esp_dte->parent.dce;
86101
MODEM_CHECK(dce, "DTE has not yet bind with DCE", err);
87102
const char *line = (const char *)(esp_dte->buffer);
103+
size_t len = strlen(line);
88104
/* Skip pure "\r\n" lines */
89-
if (strlen(line) > 2) {
105+
if (len > 2 && !is_only_cr_lf(line, len)) {
90106
MODEM_CHECK(dce->handle_line, "no handler for line", err_handle);
91107
MODEM_CHECK(dce->handle_line(dce, line) == ESP_OK, "handle line failed", err_handle);
92108
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2020 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#include <stdlib.h>
15+
#include <string.h>
16+
#include "esp_log.h"
17+
#include "bg96.h"
18+
#include "bg96_private.h"
19+
20+
/**
21+
* @brief This module supports SIM7600 module, which has a very similar interface
22+
* to the BG96, so it just references most of the handlers from BG96 and implements
23+
* only those that differ.
24+
*/
25+
static const char *DCE_TAG = "sim7600";
26+
27+
/**
28+
* @brief Handle response from AT+CBC
29+
*/
30+
static esp_err_t sim7600_handle_cbc(modem_dce_t *dce, const char *line)
31+
{
32+
esp_err_t err = ESP_FAIL;
33+
bg96_modem_dce_t *bg96_dce = __containerof(dce, bg96_modem_dce_t, parent);
34+
if (strstr(line, MODEM_RESULT_CODE_SUCCESS)) {
35+
err = esp_modem_process_command_done(dce, MODEM_STATE_SUCCESS);
36+
} else if (strstr(line, MODEM_RESULT_CODE_ERROR)) {
37+
err = esp_modem_process_command_done(dce, MODEM_STATE_FAIL);
38+
} else if (!strncmp(line, "+CBC", strlen("+CBC"))) {
39+
/* store value of bcs, bcl, voltage */
40+
int32_t **cbc = bg96_dce->priv_resource;
41+
int32_t volts = 0, fraction = 0;
42+
/* +CBC: <voltage in Volts> V*/
43+
sscanf(line, "+CBC: %d.%dV", &volts, &fraction);
44+
/* Since the "read_battery_status()" API (besides voltage) returns also values for BCS, BCL (charge status),
45+
* which are not applicable to this modem, we return -1 to indicate invalid value
46+
*/
47+
*cbc[0] = -1; // BCS
48+
*cbc[1] = -1; // BCL
49+
*cbc[2] = volts*1000 + fraction;
50+
err = ESP_OK;
51+
}
52+
return err;
53+
}
54+
55+
/**
56+
* @brief Get battery status
57+
*
58+
* @param dce Modem DCE object
59+
* @param bcs Battery charge status
60+
* @param bcl Battery connection level
61+
* @param voltage Battery voltage
62+
* @return esp_err_t
63+
* - ESP_OK on success
64+
* - ESP_FAIL on error
65+
*/
66+
static esp_err_t sim7600_get_battery_status(modem_dce_t *dce, uint32_t *bcs, uint32_t *bcl, uint32_t *voltage)
67+
{
68+
modem_dte_t *dte = dce->dte;
69+
bg96_modem_dce_t *bg96_dce = __containerof(dce, bg96_modem_dce_t, parent);
70+
uint32_t *resource[3] = {bcs, bcl, voltage};
71+
bg96_dce->priv_resource = resource;
72+
dce->handle_line = sim7600_handle_cbc;
73+
DCE_CHECK(dte->send_cmd(dte, "AT+CBC\r", MODEM_COMMAND_TIMEOUT_DEFAULT) == ESP_OK, "send command failed", err);
74+
DCE_CHECK(dce->state == MODEM_STATE_SUCCESS, "inquire battery status failed", err);
75+
ESP_LOGD(DCE_TAG, "inquire battery status ok");
76+
return ESP_OK;
77+
err:
78+
return ESP_FAIL;
79+
}
80+
81+
/**
82+
* @brief Create and initialize SIM7600 object
83+
*
84+
*/
85+
modem_dce_t *sim7600_init(modem_dte_t *dte)
86+
{
87+
modem_dce_t *dce = bg96_init(dte);
88+
dte->dce->get_battery_status = sim7600_get_battery_status;
89+
return dce;
90+
}

examples/protocols/pppos_client/main/Kconfig.projbuild

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ menu "Example Configuration"
1414
bool "BG96"
1515
help
1616
Quectel BG96 is a series of LTE Cat M1/Cat NB1/EGPRS module.
17+
config EXAMPLE_MODEM_DEVICE_SIM7600
18+
bool "SIM7600"
19+
help
20+
SIM7600 is Multi-Band LTE-TDD/LTE-FDD/HSPA+ and GSM/GPRS/EDGE module
1721
endchoice
1822

1923
config EXAMPLE_MODEM_PPP_AUTH_USERNAME

examples/protocols/pppos_client/main/pppos_client_main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "esp_log.h"
1818
#include "sim800.h"
1919
#include "bg96.h"
20+
#include "sim7600.h"
2021

2122
#define BROKER_URL "mqtt://mqtt.eclipse.org"
2223

@@ -261,6 +262,8 @@ void app_main(void)
261262
dce = sim800_init(dte);
262263
#elif CONFIG_EXAMPLE_MODEM_DEVICE_BG96
263264
dce = bg96_init(dte);
265+
#elif CONFIG_EXAMPLE_MODEM_DEVICE_SIM7600
266+
dce = sim7600_init(dte);
264267
#else
265268
#error "Unsupported DCE"
266269
#endif

0 commit comments

Comments
 (0)