ym310_main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * YM310 MQTT Driver - Main Entry Point - REFACTORED v3.0 (ALL FIXES)
  3. *
  4. * Fixes:
  5. * - Fixed initialization sequence
  6. * - Added proper thread synchronization
  7. * - Enhanced error handling
  8. *
  9. * Version: 3.0.0 (All Fixes)
  10. */
  11. #define DBG_TAG "Y-MAIN"
  12. #define DBG_LVL DBG_LOG
  13. #include <rtdbg.h>
  14. #include "ym310_mqtt.h"
  15. /*=============================================================================
  16. * Main Entry Point
  17. *=============================================================================*/
  18. /**
  19. * @brief YM310 MQTT driver initialization entry point
  20. *
  21. * This function should be called from your application to start the
  22. * YM310 4G module driver and MQTT functionality.
  23. *
  24. * @return RT_EOK on success, -RT_ERROR on failure
  25. */
  26. int ym310_driver_init(void)
  27. {
  28. LOG_I("========================================");
  29. LOG_I("YM310 MQTT Driver v3.0.0 (All Fixes)");
  30. LOG_I("========================================");
  31. /* Start initialization thread */
  32. g_init_thread = ym310_start_init_thread();
  33. if (g_init_thread == RT_NULL)
  34. {
  35. LOG_E("Failed to start YM310 init thread!");
  36. return -RT_ERROR;
  37. }
  38. //LOG_I("YM310 driver initialization started");
  39. return RT_EOK;
  40. }
  41. /**
  42. * @brief YM310 driver deinitialization
  43. */
  44. void ym310_driver_deinit(void)
  45. {
  46. LOG_I("YM310 driver deinitializing...");
  47. ym310_init_thread_stop();
  48. ym310_maintain_thread_stop();
  49. rt_thread_mdelay(100);
  50. ym310_mqtt_deinit();
  51. ym310_at_core_deinit();
  52. LOG_I("YM310 driver deinitialized");
  53. }
  54. /*=============================================================================
  55. * RT-Thread Auto Initialization (optional)
  56. *=============================================================================*/
  57. #ifdef RT_USING_COMPONENTS_INIT
  58. /* Uncomment to enable auto-initialization */
  59. /* INIT_APP_EXPORT(ym310_driver_init); */
  60. #endif
  61. /*=============================================================================
  62. * Example Application Integration
  63. *=============================================================================*/
  64. /**
  65. * @brief Example: How to integrate YM310 driver in your application
  66. *
  67. * Add the following to your main() or application init function:
  68. *
  69. * ```c
  70. * #include "ym310_mqtt.h"
  71. *
  72. * int main(void)
  73. * {
  74. * // Initialize YM310 driver
  75. * if (ym310_driver_init() != RT_EOK) {
  76. * LOG_E("YM310 init failed!");
  77. * return -1;
  78. * }
  79. *
  80. * // Wait for network to be ready
  81. * rt_thread_mdelay(30000); // Wait 30 seconds for initialization
  82. *
  83. * // Register MQTT receive callbacks (optional)
  84. * // ym310_register_mqtt_callbacks();
  85. *
  86. * // Main application loop
  87. * while (1) {
  88. * // Check MQTT connection
  89. * if (ym310_mqtt_is_connected(0)) {
  90. * // Send data via primary MQTT
  91. * ym310_mqtt_send_primary("{\"status\":\"online\"}", 0);
  92. * }
  93. *
  94. * rt_thread_mdelay(60000); // Every minute
  95. * }
  96. *
  97. * return 0;
  98. * }
  99. * ```
  100. */
  101. /**
  102. * @brief Example: Custom MQTT receive callback
  103. *
  104. * ```c
  105. * void my_mqtt_callback(rt_uint8_t client_idx,
  106. * const char *topic,
  107. * const char *payload,
  108. * rt_uint16_t len)
  109. * {
  110. * LOG_I("Received on MQTT[%d]: %.*s", client_idx, len, payload);
  111. *
  112. * // Parse your protocol here
  113. * // ...
  114. * }
  115. *
  116. * // Register callback:
  117. * ym310_mqtt_set_recv_callback(0, my_mqtt_callback);
  118. * ```
  119. */