uart_interface.c 4.4 KB

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