uart_interface.c 4.0 KB

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