wardriver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #include "wardriver.h"
  2. #include "wardriver_uart.h"
  3. #include <expansion/expansion.h>
  4. void save_file(Context* ctx) {
  5. Storage* storage = furi_record_open(RECORD_STORAGE);
  6. DateTime datetime;
  7. furi_hal_rtc_get_datetime(&datetime);
  8. FuriString* filename = furi_string_alloc();
  9. furi_string_printf(
  10. filename,
  11. "%s/%s_%d_%d_%d_%d_%d_%d.txt",
  12. FILE_PATH,
  13. "wigle",
  14. datetime.year,
  15. datetime.month,
  16. datetime.day,
  17. datetime.hour,
  18. datetime.minute,
  19. datetime.second);
  20. File* file = storage_file_alloc(storage);
  21. if(!storage_common_exists(storage, FILE_PATH)) {
  22. storage_common_mkdir(storage, FILE_PATH);
  23. }
  24. if(!storage_file_open(file, furi_string_get_cstr(filename), FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
  25. FURI_LOG_I(appname, "Failed to open file");
  26. storage_file_close(file);
  27. storage_file_free(file);
  28. furi_string_free(filename);
  29. furi_record_close(RECORD_STORAGE);
  30. furi_hal_light_blink_start(LightRed, 100, 100, 5000);
  31. return;
  32. }
  33. FuriString* string = furi_string_alloc();
  34. // WIGLE HEADERS DONT CHANGE THIS ITS IMPORTANT!
  35. furi_string_printf(
  36. string,
  37. "%s,%s,%s,%s,%s,%s,%s,%s\r\n",
  38. "WigleWifi-1.4",
  39. "appRelease=v2.0",
  40. "model=S33",
  41. "release=MomentumFW",
  42. "Flipper Zero",
  43. "",
  44. "Wardriver",
  45. "S33");
  46. furi_string_cat_printf(
  47. string,
  48. "%s,%s,%s,%s,%s,%s,%s\r\n",
  49. "MAC",
  50. "SSID",
  51. "FirstSeen",
  52. "Channel",
  53. "RSSI",
  54. "CurrentLatitude",
  55. "CurrentLongitude");
  56. if(!storage_file_write(
  57. file, furi_string_get_cstr(string), strlen(furi_string_get_cstr(string)))) {
  58. FURI_LOG_I(appname, "Failed to write header to file");
  59. }
  60. for(int i = 0; i < ctx->access_points_count; i++) {
  61. AccessPoint ap = ctx->access_points[i];
  62. furi_string_printf(
  63. string,
  64. "%s,%s,%04d-%02d-%02d %02d:%02d:%02d,%d,%d,%f,%f\r\n",
  65. ap.bssid,
  66. ap.ssid,
  67. ap.datetime.year,
  68. ap.datetime.month,
  69. ap.datetime.day,
  70. ap.datetime.hour,
  71. ap.datetime.minute,
  72. ap.datetime.second,
  73. ap.channel,
  74. ap.rssi,
  75. (double)ap.latitude,
  76. (double)ap.longitude);
  77. if(!storage_file_write(
  78. file, furi_string_get_cstr(string), strlen(furi_string_get_cstr(string)))) {
  79. FURI_LOG_I(appname, "Failed to write AP to file");
  80. }
  81. free(ap.ssid);
  82. free(ap.bssid);
  83. }
  84. furi_string_free(string);
  85. storage_file_close(file);
  86. storage_file_free(file);
  87. furi_string_free(filename);
  88. furi_record_close(RECORD_STORAGE);
  89. return;
  90. }
  91. static void tick_callback(void* ctx_q) {
  92. furi_assert(ctx_q);
  93. FuriMessageQueue* queue = ctx_q;
  94. Event event = {.type = EventTypeTick};
  95. furi_message_queue_put(queue, &event, 0);
  96. }
  97. static void input_callback(InputEvent* input_event, void* ctx) {
  98. FuriMessageQueue* queue = ctx;
  99. furi_assert(queue);
  100. Event event = {.type = EventTypeKey, .input = *input_event};
  101. furi_message_queue_put(queue, &event, FuriWaitForever);
  102. }
  103. static void draw_access_point(Canvas* canvas, Context* context) {
  104. Context* ctx = context;
  105. FuriString* string = furi_string_alloc();
  106. AccessPoint ap = ctx->active_access_point;
  107. canvas_draw_str_aligned(canvas, 62, 25, AlignCenter, AlignBottom, ap.ssid);
  108. canvas_set_font(canvas, FontSecondary);
  109. canvas_draw_str_aligned(canvas, 38, 12, AlignLeft, AlignBottom, ap.bssid);
  110. furi_string_printf(string, "Signal strength: %ddBm", ap.rssi);
  111. canvas_draw_str_aligned(canvas, 3, 35, AlignLeft, AlignBottom, furi_string_get_cstr(string));
  112. furi_string_printf(string, "CH: %d", ap.channel);
  113. canvas_draw_str_aligned(canvas, 3, 47, AlignLeft, AlignBottom, furi_string_get_cstr(string));
  114. furi_string_printf(string, "%d", ap.packetRxCount);
  115. canvas_draw_icon(canvas, 35, 39, &I_down);
  116. canvas_draw_str_aligned(canvas, 45, 47, AlignLeft, AlignBottom, furi_string_get_cstr(string));
  117. furi_string_printf(string, "%d", ap.packetTxCount);
  118. canvas_draw_icon(canvas, 85, 38, &I_up);
  119. canvas_draw_str_aligned(canvas, 95, 47, AlignLeft, AlignBottom, furi_string_get_cstr(string));
  120. furi_string_printf(
  121. string,
  122. "Seen: %02d:%02d:%02d (%lds ago)",
  123. ap.datetime.hour,
  124. ap.datetime.minute,
  125. ap.datetime.second,
  126. furi_hal_rtc_get_timestamp() - datetime_datetime_to_timestamp(&ap.datetime));
  127. canvas_draw_str_aligned(canvas, 3, 59, AlignLeft, AlignBottom, furi_string_get_cstr(string));
  128. furi_string_free(string);
  129. }
  130. static void render_callback(Canvas* canvas, void* context) {
  131. Context* ctx = context;
  132. FuriString* string = furi_string_alloc();
  133. canvas_set_font(canvas, FontPrimary);
  134. if(ctx->access_points_count >= MAX_ACCESS_POINTS) {
  135. canvas_draw_str(canvas, 118, 10, "!");
  136. }
  137. switch(ctx->view_state) {
  138. case SHOW_NMEA:
  139. if(UART_CH_ESP == UART_CH_GPS) {
  140. canvas_draw_str(canvas, 0, 10, "GPS channel invalid!");
  141. canvas_draw_str(canvas, 0, 20, "Change UART");
  142. canvas_draw_str(canvas, 0, 30, "channel");
  143. canvas_draw_str(canvas, 0, 40, "in the Momentum");
  144. canvas_draw_str(canvas, 0, 50, "app");
  145. } else {
  146. furi_string_printf(
  147. string, "%f", isnan(ctx->gps_data.latitude) ? 0 : (double)ctx->gps_data.latitude);
  148. canvas_draw_str(canvas, 0, 10, furi_string_get_cstr(string));
  149. furi_string_printf(
  150. string,
  151. "%f",
  152. isnan(ctx->gps_data.longitude) ? 0 : (double)ctx->gps_data.longitude);
  153. canvas_draw_str(canvas, 0, 20, furi_string_get_cstr(string));
  154. furi_string_printf(string, "%d sats", ctx->gps_data.satelites);
  155. canvas_draw_str(canvas, 0, 30, furi_string_get_cstr(string));
  156. furi_string_printf(
  157. string,
  158. "%02d:%02d:%02dZ",
  159. ctx->gps_data.hour,
  160. ctx->gps_data.minute,
  161. ctx->gps_data.second);
  162. canvas_draw_str(canvas, 0, 40, furi_string_get_cstr(string));
  163. canvas_draw_str(canvas, 70, 10, "GPS DATA");
  164. }
  165. elements_button_left(canvas, "Back");
  166. canvas_draw_icon(canvas, 82, 20, &I_WarningDolphinFlip_45x42);
  167. break;
  168. case NO_APS:
  169. canvas_draw_str(canvas, 80, 30, "No AP's");
  170. canvas_draw_str(canvas, 80, 40, "Found!");
  171. canvas_draw_icon(canvas, 1, 9, &I_DolphinWait_59x54);
  172. break;
  173. case NORMAL:
  174. default:
  175. canvas_draw_frame(canvas, 0, 0, 128, 64);
  176. furi_string_printf(
  177. string, "%d/%d", ctx->access_points_index + 1, ctx->access_points_count);
  178. canvas_draw_str(canvas, 3, 12, furi_string_get_cstr(string));
  179. draw_access_point(canvas, ctx);
  180. break;
  181. }
  182. furi_mutex_release(ctx->mutex);
  183. furi_string_free(string);
  184. }
  185. int32_t wardriver_app() {
  186. // Disable expansion protocol to avoid interference with UART Handle
  187. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  188. expansion_disable(expansion);
  189. // turn off 5v, so it gets reset on startup
  190. if(furi_hal_power_is_otg_enabled()) {
  191. furi_hal_power_disable_otg();
  192. }
  193. // Enable 5v on startup
  194. uint8_t attempts = 0;
  195. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  196. furi_hal_power_enable_otg();
  197. furi_delay_ms(10);
  198. }
  199. furi_delay_ms(200);
  200. Context* ctx = malloc(sizeof(Context));
  201. ctx->queue = furi_message_queue_alloc(8, sizeof(Event));
  202. ctx->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  203. ctx->access_points_count = 0;
  204. ctx->access_points_index = 0;
  205. ctx->pressedButton = false;
  206. ctx->view_state = NO_APS;
  207. wardriver_uart_init(ctx);
  208. ViewPort* view_port = view_port_alloc();
  209. view_port_draw_callback_set(view_port, render_callback, ctx);
  210. view_port_input_callback_set(view_port, input_callback, ctx->queue);
  211. Gui* gui = furi_record_open(RECORD_GUI);
  212. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  213. FuriTimer* timer = furi_timer_alloc(tick_callback, FuriTimerTypePeriodic, ctx->queue);
  214. furi_timer_start(timer, 100);
  215. // application loop
  216. Event event;
  217. bool processing = true;
  218. do {
  219. if(furi_message_queue_get(ctx->queue, &event, FuriWaitForever) == FuriStatusOk) {
  220. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  221. switch(event.type) {
  222. case EventTypeKey:
  223. if(event.input.type == InputTypeShort && event.input.key == InputKeyBack) {
  224. if(ctx->view_state == SHOW_NMEA) {
  225. ctx->view_state = NORMAL;
  226. } else {
  227. processing = false;
  228. }
  229. } else if(event.input.type == InputTypeLong && event.input.key == InputKeyBack) {
  230. if(ctx->view_state == SHOW_NMEA) {
  231. ctx->view_state = NORMAL;
  232. } else {
  233. processing = false;
  234. }
  235. } else if(event.input.type == InputTypeLong && event.input.key == InputKeyOk) {
  236. } else if(event.input.type == InputTypePress && event.input.key == InputKeyDown) {
  237. ctx->access_points_index--;
  238. if(ctx->access_points_index < 0) {
  239. ctx->access_points_index = ctx->access_points_count - 1;
  240. }
  241. ctx->active_access_point = ctx->access_points[ctx->access_points_index];
  242. ctx->pressedButton = true;
  243. } else if(event.input.type == InputTypePress && event.input.key == InputKeyUp) {
  244. ctx->access_points_index++;
  245. if(ctx->access_points_index >= ctx->access_points_count) {
  246. ctx->access_points_index = 0;
  247. }
  248. ctx->active_access_point = ctx->access_points[ctx->access_points_index];
  249. ctx->pressedButton = true;
  250. } else if(event.input.type == InputTypePress && event.input.key == InputKeyLeft) {
  251. if(ctx->view_state == NORMAL) {
  252. ctx->view_state = SHOW_NMEA;
  253. } else if(ctx->view_state == SHOW_NMEA) {
  254. ctx->view_state = NORMAL;
  255. }
  256. } else if(event.input.type == InputTypePress && event.input.key == InputKeyRight) {
  257. }
  258. break;
  259. case EventTypeTick:
  260. // fix for the empty active access point when there was no interaction
  261. if(!ctx->pressedButton) {
  262. ctx->access_points_index = 0;
  263. ctx->active_access_point = ctx->access_points[ctx->access_points_index];
  264. }
  265. break;
  266. default:
  267. break;
  268. }
  269. view_port_update(view_port);
  270. } else {
  271. processing = false;
  272. }
  273. } while(processing);
  274. furi_timer_free(timer);
  275. view_port_enabled_set(view_port, false);
  276. gui_remove_view_port(gui, view_port);
  277. view_port_free(view_port);
  278. furi_record_close(RECORD_GUI);
  279. furi_message_queue_free(ctx->queue);
  280. furi_mutex_free(ctx->mutex);
  281. wardriver_uart_deinit(ctx);
  282. save_file(ctx);
  283. free(ctx);
  284. furi_hal_light_set(LightBlue, 0);
  285. furi_hal_light_set(LightGreen, 0);
  286. if(furi_hal_power_is_otg_enabled()) {
  287. furi_hal_power_disable_otg();
  288. }
  289. // Return previous state of expansion
  290. expansion_enable(expansion);
  291. furi_record_close(RECORD_EXPANSION);
  292. return 0;
  293. }