sim7600.c 5.1 KB

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