uart_interface.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * IdeasX UART Interface
  3. * Author: Tyler Berezowsky
  4. * Date: 20170518
  5. */
  6. #include "interface/uart_interface.h"
  7. #include "interface/light_interface.h"
  8. #include "hal/io.h"
  9. static const char* TAG = "uart_interface.c";
  10. static void show_WiFi_current(void)
  11. {
  12. struct station_config stationConfig;
  13. wifi_station_get_config(&stationConfig);
  14. os_printf("SSID: %s\nBSSID: "MACSTR"\n", stationConfig.ssid, MAC2STR(stationConfig.bssid)); // fix MAX2STR error
  15. }
  16. static void show_WiFi(void)
  17. {
  18. struct station_config stationConfig[5];
  19. uint8_t num = wifi_station_get_ap_info(stationConfig);
  20. os_printf("\n--------Stored Access Points---------\n");
  21. uint32_t i;
  22. for (i=0;i<num;i++)
  23. {
  24. os_printf("AP: %d/%d\n", i, (num-1));
  25. os_printf("\tSSID: %s\n", stationConfig[i].ssid);
  26. os_printf("\tPASSWORD: %s\n", stationConfig[i].password);
  27. if (stationConfig[i].bssid_set)
  28. os_printf("\tBSSID: "MACSTR"\n", MAC2STR(stationConfig[i].bssid)); // fix MAC2STR
  29. }
  30. }
  31. static void show_IP(void)
  32. {
  33. struct ip_info ipconfig;
  34. wifi_get_ip_info(STATION_IF, &ipconfig);
  35. if (wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0) {
  36. os_printf("IP: %d.%d.%d.%d, MASK: %d.%d.%d.%d, GW: %d.%d.%d.%d\n",
  37. IP2STR(&ipconfig.ip), IP2STR(&ipconfig.netmask), IP2STR(&ipconfig.gw));
  38. } else {
  39. os_printf("Network Status: %d\n", wifi_station_get_connect_status());
  40. }
  41. }
  42. static void select_ap(uint8_t* str)
  43. {
  44. uint8_t* token;
  45. uint8_t num;
  46. token = strtok(str, "=");
  47. token = strtok(NULL, ",");
  48. num = atoi(token);
  49. ESP_LOGD(TAG,"Setting to Station: %d\n", num);
  50. if (wifi_station_ap_change(num))
  51. os_printf("OK\n");
  52. else
  53. os_printf("FAIL\n");
  54. }
  55. static void add_ap(uint8_t* str)
  56. {
  57. //strip command and seperate into SSID / password
  58. uint8_t* token;
  59. struct station_config stationConfig;
  60. token = strtok(str, "="); // disregard first field
  61. token = strtok(NULL, ",");
  62. if (token != NULL) // set ssid
  63. {
  64. ESP_LOGD(TAG, "\rSSID: %s\n", token);
  65. os_sprintf(stationConfig.ssid, "%s", token);
  66. }
  67. token = strtok(NULL, ",");
  68. if (token != NULL) // set password field
  69. {
  70. ESP_LOGD(TAG, "Password: %s\n", token);
  71. os_sprintf(stationConfig.password, "%s", token);
  72. }
  73. stationConfig.bssid_set = 0;
  74. if (wifi_station_set_config(&stationConfig))
  75. os_printf("OK\n");
  76. else
  77. os_printf("FAIL\n");
  78. }
  79. static void show_info(void)
  80. {
  81. os_printf("\n--------System Information--------\n");
  82. os_printf("SDK: v%s\n", system_get_sdk_version());
  83. os_printf("Firmware Build Date:" BUILD_TIME"\n");
  84. os_printf("Free Heap: %d\n", system_get_free_heap_size());
  85. os_printf("CPU Frequency: %d MHz\n", system_get_cpu_freq());
  86. os_printf("System Chip ID: 0x%x\n", system_get_chip_id());
  87. os_printf("SPI Flash ID: 0x%x\n", spi_flash_get_id());
  88. os_printf("SPI Flash Size: %d\n", (1 << ((spi_flash_get_id() >> 16) & 0xff)));
  89. os_printf("--------Network Status------------\n");
  90. show_IP();
  91. show_WiFi_current();
  92. // print broker information
  93. }
  94. void process_command(uint8_t *str)
  95. {
  96. if (!strcmp(str, "AT+RST")) // reset
  97. {
  98. ESP_LOGI(TAG, "Manual Restarting...\n");
  99. system_restart();
  100. }
  101. else if (!strcmp(str, "AT+SD")) // shutdown
  102. {
  103. ESP_LOGI(TAG, "Manual Shutdown...\n");
  104. StatusLED_Shutdown();
  105. IO_Shutdown();
  106. }
  107. else if (!strcmp(str, "AT+INFO")) // print system information
  108. {
  109. show_info();
  110. }
  111. else if (!strcmp(str, "AT+WL")) // list all stored APs
  112. {
  113. show_WiFi();
  114. }
  115. else if (!strncmp(str, "AT+WA", 5)) // add ap
  116. {
  117. add_ap(str);
  118. }
  119. else if (!strncmp(str, "AT+WS", 5)) // manually select ap
  120. {
  121. select_ap(str);
  122. }
  123. else if (!strcmp(str, "AT+WD")) // delete stored AP
  124. {
  125. ESP_LOGI(TAG, "N/A\n");
  126. }
  127. else if (!strcmp(str, "AT+OTA")) // manually force OTA update
  128. {
  129. ESP_LOGI(TAG, "N/A\n");
  130. }
  131. else if (!strcmp(str, "AT+BS")) // store MQTT broker
  132. {
  133. ESP_LOGI(TAG, "N/A\n");
  134. }
  135. }