uart_interface.c 4.2 KB

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