CommandLine.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "CommandLine.h"
  2. CommandLine::CommandLine() {
  3. }
  4. void CommandLine::RunSetup() {
  5. Serial.print("> ");
  6. }
  7. String CommandLine::getSerialInput() {
  8. String input = "";
  9. if (Serial.available() > 0)
  10. input = Serial.readStringUntil('\n');
  11. input.trim();
  12. return input;
  13. }
  14. void CommandLine::main(uint32_t currentTime) {
  15. String input = this->getSerialInput();
  16. this->runCommand(input);
  17. if (input != "")
  18. Serial.print("> ");
  19. }
  20. LinkedList<String> CommandLine::parseCommand(String input) {
  21. LinkedList<String> cmd_args;
  22. if (input != "") {
  23. char delim[] = " ";
  24. char fancy[input.length() + 1] = {};
  25. input.toCharArray(fancy, input.length() + 1);
  26. char* ptr = strtok(fancy, delim);
  27. while (ptr != NULL) {
  28. cmd_args.add(String(ptr));
  29. ptr = strtok(NULL, delim);
  30. }
  31. //memset(fancy, 0, sizeof(fancy));
  32. // Display the segmented cmd
  33. //for (int i = 0; i < cmd_args.size(); i++) {
  34. // Serial.println(cmd_args.get(i));
  35. //}
  36. }
  37. return cmd_args;
  38. }
  39. int CommandLine::argSearch(LinkedList<String>* cmd_args_list, String key) {
  40. for (int i = 0; i < cmd_args_list->size(); i++) {
  41. if (cmd_args_list->get(i) == key)
  42. return i;
  43. }
  44. return -1;
  45. }
  46. void CommandLine::runCommand(String input) {
  47. if (input != "")
  48. Serial.println("#" + input);
  49. else
  50. return;
  51. LinkedList<String> cmd_args = this->parseCommand(input);
  52. //// Admin commands
  53. // Stop Scan
  54. if (cmd_args.get(0) == STOPSCAN_CMD) {
  55. wifi_scan_obj.StartScan(WIFI_SCAN_OFF);
  56. // If we don't do this, the text and button coordinates will be off
  57. #ifdef HAS_SCREEN
  58. display_obj.tft.init();
  59. menu_function_obj.changeMenu(menu_function_obj.current_menu);
  60. #endif
  61. }
  62. // Channel command
  63. else if (cmd_args.get(0) == CH_CMD) {
  64. // Search for channel set arg
  65. int ch_set = this->argSearch(&cmd_args, "-s");
  66. if (cmd_args.size() == 1) {
  67. Serial.println("Current channel: " + (String)wifi_scan_obj.set_channel);
  68. }
  69. else if (ch_set != -1) {
  70. wifi_scan_obj.set_channel = cmd_args.get(ch_set + 1).toInt();
  71. wifi_scan_obj.changeChannel();
  72. Serial.println("Set channel: " + (String)wifi_scan_obj.set_channel);
  73. }
  74. }
  75. // Clear APs
  76. else if (cmd_args.get(0) == CLEARAP_CMD) {
  77. wifi_scan_obj.RunClearAPs();
  78. }
  79. //// WiFi Scan commands
  80. if (!wifi_scan_obj.scanning()) {
  81. // AP Scan
  82. if (cmd_args.get(0) == SCANAP_CMD) {
  83. #ifdef HAS_SCREEN
  84. display_obj.clearScreen();
  85. menu_function_obj.drawStatusBar();
  86. #endif
  87. wifi_scan_obj.StartScan(WIFI_SCAN_TARGET_AP, TFT_MAGENTA);
  88. }
  89. // Beacon sniff
  90. else if (cmd_args.get(0) == SNIFF_BEACON_CMD) {
  91. #ifdef HAS_SCREEN
  92. display_obj.clearScreen();
  93. menu_function_obj.drawStatusBar();
  94. #endif
  95. wifi_scan_obj.StartScan(WIFI_SCAN_AP, TFT_MAGENTA);
  96. }
  97. // Deauth sniff
  98. else if (cmd_args.get(0) == SNIFF_DEAUTH_CMD) {
  99. #ifdef HAS_SCREEN
  100. display_obj.clearScreen();
  101. menu_function_obj.drawStatusBar();
  102. #endif
  103. wifi_scan_obj.StartScan(WIFI_SCAN_DEAUTH, TFT_RED);
  104. }
  105. // PMKID sniff
  106. else if (cmd_args.get(0) == SNIFF_PMKID_CMD) {
  107. wifi_scan_obj.StartScan(WIFI_SCAN_EAPOL, TFT_VIOLET);
  108. }
  109. }
  110. }