ml307_board.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "ml307_board.h"
  2. #include "application.h"
  3. #include "display.h"
  4. #include "font_awesome_symbols.h"
  5. #include "assets/lang_config.h"
  6. #include <esp_log.h>
  7. #include <esp_timer.h>
  8. #include <ml307_http.h>
  9. #include <ml307_ssl_transport.h>
  10. #include <web_socket.h>
  11. #include <ml307_mqtt.h>
  12. #include <ml307_udp.h>
  13. #include <opus_encoder.h>
  14. static const char *TAG = "Ml307Board";
  15. Ml307Board::Ml307Board(gpio_num_t tx_pin, gpio_num_t rx_pin, size_t rx_buffer_size) : modem_(tx_pin, rx_pin, rx_buffer_size) {
  16. }
  17. std::string Ml307Board::GetBoardType() {
  18. return "ml307";
  19. }
  20. void Ml307Board::StartNetwork() {
  21. auto display = Board::GetInstance().GetDisplay();
  22. display->SetStatus(Lang::Strings::DETECTING_MODULE);
  23. modem_.SetDebug(false);
  24. // modem_.SetBaudRate(921600);
  25. modem_.SetBaudRate(230400);
  26. auto& application = Application::GetInstance();
  27. // If low power, the material ready event will be triggered by the modem because of a reset
  28. modem_.OnMaterialReady([this, &application]() {
  29. ESP_LOGI(TAG, "ML307 material ready");
  30. application.Schedule([this, &application]() {
  31. application.SetDeviceState(kDeviceStateIdle);
  32. WaitForNetworkReady();
  33. });
  34. });
  35. WaitForNetworkReady();
  36. }
  37. void Ml307Board::WaitForNetworkReady() {
  38. auto& application = Application::GetInstance();
  39. auto display = Board::GetInstance().GetDisplay();
  40. display->SetStatus(Lang::Strings::REGISTERING_NETWORK);
  41. int result = modem_.WaitForNetworkReady();
  42. if (result == -1) {
  43. application.Alert(Lang::Strings::ERROR, Lang::Strings::PIN_ERROR, "sad", Lang::Sounds::P3_ERR_PIN);
  44. return;
  45. } else if (result == -2) {
  46. application.Alert(Lang::Strings::ERROR, Lang::Strings::REG_ERROR, "sad", Lang::Sounds::P3_ERR_REG);
  47. return;
  48. }
  49. // Print the ML307 modem information
  50. std::string module_name = modem_.GetModuleName();
  51. std::string imei = modem_.GetImei();
  52. std::string iccid = modem_.GetIccid();
  53. ESP_LOGI(TAG, "ML307 Module: %s", module_name.c_str());
  54. ESP_LOGI(TAG, "ML307 IMEI: %s", imei.c_str());
  55. ESP_LOGI(TAG, "ML307 ICCID: %s", iccid.c_str());
  56. // Close all previous connections
  57. modem_.ResetConnections();
  58. }
  59. Http* Ml307Board::CreateHttp() {
  60. return new Ml307Http(modem_);
  61. }
  62. WebSocket* Ml307Board::CreateWebSocket() {
  63. return new WebSocket(new Ml307SslTransport(modem_, 0));
  64. }
  65. Mqtt* Ml307Board::CreateMqtt() {
  66. return new Ml307Mqtt(modem_, 0);
  67. }
  68. Udp* Ml307Board::CreateUdp() {
  69. return new Ml307Udp(modem_, 0);
  70. }
  71. const char* Ml307Board::GetNetworkStateIcon() {
  72. if (!modem_.network_ready()) {
  73. return FONT_AWESOME_SIGNAL_OFF;
  74. }
  75. int csq = modem_.GetCsq();
  76. if (csq == -1) {
  77. return FONT_AWESOME_SIGNAL_OFF;
  78. } else if (csq >= 0 && csq <= 14) {
  79. return FONT_AWESOME_SIGNAL_1;
  80. } else if (csq >= 15 && csq <= 19) {
  81. return FONT_AWESOME_SIGNAL_2;
  82. } else if (csq >= 20 && csq <= 24) {
  83. return FONT_AWESOME_SIGNAL_3;
  84. } else if (csq >= 25 && csq <= 31) {
  85. return FONT_AWESOME_SIGNAL_4;
  86. }
  87. ESP_LOGW(TAG, "Invalid CSQ: %d", csq);
  88. return FONT_AWESOME_SIGNAL_OFF;
  89. }
  90. std::string Ml307Board::GetBoardJson() {
  91. // Set the board type for OTA
  92. std::string board_json = std::string("{\"type\":\"" BOARD_TYPE "\",");
  93. board_json += "\"name\":\"" BOARD_NAME "\",";
  94. board_json += "\"revision\":\"" + modem_.GetModuleName() + "\",";
  95. board_json += "\"carrier\":\"" + modem_.GetCarrierName() + "\",";
  96. board_json += "\"csq\":\"" + std::to_string(modem_.GetCsq()) + "\",";
  97. board_json += "\"imei\":\"" + modem_.GetImei() + "\",";
  98. board_json += "\"iccid\":\"" + modem_.GetIccid() + "\"}";
  99. return board_json;
  100. }
  101. void Ml307Board::SetPowerSaveMode(bool enabled) {
  102. // TODO: Implement power save mode for ML307
  103. }