sim7600.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. * @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 Handle response from AT+CBC
  27. */
  28. static esp_err_t sim7600_handle_cbc(modem_dce_t *dce, const char *line)
  29. {
  30. esp_err_t err = ESP_FAIL;
  31. bg96_modem_dce_t *bg96_dce = __containerof(dce, bg96_modem_dce_t, parent);
  32. if (strstr(line, MODEM_RESULT_CODE_SUCCESS)) {
  33. err = esp_modem_process_command_done(dce, MODEM_STATE_SUCCESS);
  34. } else if (strstr(line, MODEM_RESULT_CODE_ERROR)) {
  35. err = esp_modem_process_command_done(dce, MODEM_STATE_FAIL);
  36. } else if (!strncmp(line, "+CBC", strlen("+CBC"))) {
  37. /* store value of bcs, bcl, voltage */
  38. int32_t **cbc = bg96_dce->priv_resource;
  39. int32_t volts = 0, fraction = 0;
  40. /* +CBC: <voltage in Volts> V*/
  41. sscanf(line, "+CBC: %d.%dV", &volts, &fraction);
  42. /* Since the "read_battery_status()" API (besides voltage) returns also values for BCS, BCL (charge status),
  43. * which are not applicable to this modem, we return -1 to indicate invalid value
  44. */
  45. *cbc[0] = -1; // BCS
  46. *cbc[1] = -1; // BCL
  47. *cbc[2] = volts*1000 + fraction;
  48. err = ESP_OK;
  49. }
  50. return err;
  51. }
  52. /**
  53. * @brief Get battery status
  54. *
  55. * @param dce Modem DCE object
  56. * @param bcs Battery charge status
  57. * @param bcl Battery connection level
  58. * @param voltage Battery voltage
  59. * @return esp_err_t
  60. * - ESP_OK on success
  61. * - ESP_FAIL on error
  62. */
  63. static esp_err_t sim7600_get_battery_status(modem_dce_t *dce, uint32_t *bcs, uint32_t *bcl, uint32_t *voltage)
  64. {
  65. modem_dte_t *dte = dce->dte;
  66. bg96_modem_dce_t *bg96_dce = __containerof(dce, bg96_modem_dce_t, parent);
  67. uint32_t *resource[3] = {bcs, bcl, voltage};
  68. bg96_dce->priv_resource = resource;
  69. dce->handle_line = sim7600_handle_cbc;
  70. DCE_CHECK(dte->send_cmd(dte, "AT+CBC\r", MODEM_COMMAND_TIMEOUT_DEFAULT) == ESP_OK, "send command failed", err);
  71. DCE_CHECK(dce->state == MODEM_STATE_SUCCESS, "inquire battery status failed", err);
  72. ESP_LOGD(DCE_TAG, "inquire battery status ok");
  73. return ESP_OK;
  74. err:
  75. return ESP_FAIL;
  76. }
  77. /**
  78. * @brief Create and initialize SIM7600 object
  79. *
  80. */
  81. modem_dce_t *sim7600_init(modem_dte_t *dte)
  82. {
  83. modem_dce_t *dce = bg96_init(dte);
  84. dte->dce->get_battery_status = sim7600_get_battery_status;
  85. return dce;
  86. }