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 "esp_modem_dce_service.h"
18 #include "bg96.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 Macro defined for error checking
29  *
30  */
31 #define DCE_CHECK(a, str, goto_tag, ...)                                              \
32     do                                                                                \
33     {                                                                                 \
34         if (!(a))                                                                     \
35         {                                                                             \
36             ESP_LOGE(DCE_TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
37             goto goto_tag;                                                            \
38         }                                                                             \
39     } while (0)
40 
41 /**
42  * @brief Handle response from AT+CBC
43  */
sim7600_handle_cbc(modem_dce_t * dce,const char * line)44 static esp_err_t sim7600_handle_cbc(modem_dce_t *dce, const char *line)
45 {
46     esp_err_t err = ESP_FAIL;
47     esp_modem_dce_t *esp_modem_dce = __containerof(dce, esp_modem_dce_t, parent);
48     if (strstr(line, MODEM_RESULT_CODE_SUCCESS)) {
49         err = esp_modem_process_command_done(dce, MODEM_STATE_SUCCESS);
50     } else if (strstr(line, MODEM_RESULT_CODE_ERROR)) {
51         err = esp_modem_process_command_done(dce, MODEM_STATE_FAIL);
52     } else if (!strncmp(line, "+CBC", strlen("+CBC"))) {
53         /* store value of bcs, bcl, voltage */
54         int32_t **cbc = esp_modem_dce->priv_resource;
55         int32_t volts = 0, fraction = 0;
56         /* +CBC: <voltage in Volts> V*/
57         sscanf(line, "+CBC: %d.%dV", &volts, &fraction);
58         /* Since the "read_battery_status()" API (besides voltage) returns also values for BCS, BCL (charge status),
59          * which are not applicable to this modem, we return -1 to indicate invalid value
60          */
61         *cbc[0] = -1; // BCS
62         *cbc[1] = -1; // BCL
63         *cbc[2] = volts*1000 + fraction;
64         err = ESP_OK;
65     }
66     return err;
67 }
68 
69 /**
70  * @brief Handle response from AT+CPOF
71  */
sim7600_handle_cpof(modem_dce_t * dce,const char * line)72 static esp_err_t sim7600_handle_cpof(modem_dce_t *dce, const char *line)
73 {
74     esp_err_t err = ESP_OK;
75     if (strstr(line, MODEM_RESULT_CODE_SUCCESS)) {
76         err = esp_modem_process_command_done(dce, MODEM_STATE_SUCCESS);
77     } else if (strstr(line, MODEM_RESULT_CODE_NO_CARRIER)) {
78         err = ESP_OK;
79     } else if (strstr(line, MODEM_RESULT_CODE_ERROR)) {
80         err = esp_modem_process_command_done(dce, MODEM_STATE_FAIL);
81     }
82     return err;
83 }
84 
85 /**
86  * @brief Get battery status
87  *
88  * @param dce Modem DCE object
89  * @param bcs Battery charge status
90  * @param bcl Battery connection level
91  * @param voltage Battery voltage
92  * @return esp_err_t
93  *      - ESP_OK on success
94  *      - ESP_FAIL on error
95  */
sim7600_get_battery_status(modem_dce_t * dce,uint32_t * bcs,uint32_t * bcl,uint32_t * voltage)96 static esp_err_t sim7600_get_battery_status(modem_dce_t *dce, uint32_t *bcs, uint32_t *bcl, uint32_t *voltage)
97 {
98     modem_dte_t *dte = dce->dte;
99     esp_modem_dce_t *esp_modem_dce = __containerof(dce, esp_modem_dce_t, parent);
100     uint32_t *resource[3] = {bcs, bcl, voltage};
101     esp_modem_dce->priv_resource = resource;
102     dce->handle_line = sim7600_handle_cbc;
103     DCE_CHECK(dte->send_cmd(dte, "AT+CBC\r", MODEM_COMMAND_TIMEOUT_DEFAULT) == ESP_OK, "send command failed", err);
104     DCE_CHECK(dce->state == MODEM_STATE_SUCCESS, "inquire battery status failed", err);
105     ESP_LOGD(DCE_TAG, "inquire battery status ok");
106     return ESP_OK;
107 err:
108     return ESP_FAIL;
109 }
110 
111 /**
112  * @brief Set the SIM7600 device to power down mode
113  *
114  * @param dce common modem dce object (modem_dce_t)
115  * @return esp_err_t
116  *      - ESP_OK on success
117  *      - ESP_FAIL on error
118  */
sim7600_power_down(modem_dce_t * dce)119 static esp_err_t sim7600_power_down(modem_dce_t *dce)
120 {
121     modem_dte_t *dte = dce->dte;
122     dce->handle_line = sim7600_handle_cpof;
123     DCE_CHECK(dte->send_cmd(dte, "AT+CPOF\r", MODEM_COMMAND_TIMEOUT_POWEROFF) == ESP_OK, "send command failed", err);
124     DCE_CHECK(dce->state == MODEM_STATE_SUCCESS, "power down failed", err);
125     ESP_LOGD(DCE_TAG, "power down ok");
126     return ESP_OK;
127 err:
128     return ESP_FAIL;
129 }
130 
131 /**
132  * @brief Create and initialize SIM7600 object
133  *
134  */
sim7600_init(modem_dte_t * dte)135 modem_dce_t *sim7600_init(modem_dte_t *dte)
136 {
137     modem_dce_t *dce = bg96_init(dte);
138     dte->dce->get_battery_status = sim7600_get_battery_status;
139     dte->dce->power_down = sim7600_power_down;
140     return dce;
141 }
142