ml307_board.cc 3.7 KB

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