wardriver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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.rssi,
  74. ap.channel,
  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, FuriMessageQueue* queue) {
  98. furi_assert(queue);
  99. Event event = {.type = EventTypeKey, .input = *input_event};
  100. furi_message_queue_put(queue, &event, FuriWaitForever);
  101. }
  102. static void draw_access_point(Canvas* canvas, Context* context) {
  103. Context* ctx = context;
  104. FuriString* string = furi_string_alloc();
  105. AccessPoint ap = ctx->active_access_point;
  106. canvas_draw_str_aligned(canvas, 62, 25, AlignCenter, AlignBottom, ap.ssid);
  107. canvas_set_font(canvas, FontSecondary);
  108. canvas_draw_str_aligned(canvas, 38, 12, AlignLeft, AlignBottom, ap.bssid);
  109. furi_string_printf(string, "Signal strength: %ddBm", ap.rssi);
  110. canvas_draw_str_aligned(canvas, 3, 35, AlignLeft, AlignBottom, furi_string_get_cstr(string));
  111. furi_string_printf(string, "CH: %d", ap.channel);
  112. canvas_draw_str_aligned(canvas, 3, 47, AlignLeft, AlignBottom, furi_string_get_cstr(string));
  113. furi_string_printf(string, "%d", ap.packetRxCount);
  114. canvas_draw_icon(canvas, 35, 39, &I_down);
  115. canvas_draw_str_aligned(canvas, 45, 47, AlignLeft, AlignBottom, furi_string_get_cstr(string));
  116. furi_string_printf(string, "%d", ap.packetTxCount);
  117. canvas_draw_icon(canvas, 85, 38, &I_up);
  118. canvas_draw_str_aligned(canvas, 95, 47, AlignLeft, AlignBottom, furi_string_get_cstr(string));
  119. furi_string_printf(
  120. string,
  121. "Seen: %02d:%02d:%02d (%lds ago)",
  122. ap.datetime.hour,
  123. ap.datetime.minute,
  124. ap.datetime.second,
  125. furi_hal_rtc_get_timestamp() - datetime_datetime_to_timestamp(&ap.datetime));
  126. canvas_draw_str_aligned(canvas, 3, 59, AlignLeft, AlignBottom, furi_string_get_cstr(string));
  127. furi_string_free(string);
  128. }
  129. static void render_callback(Canvas* canvas, void* context) {
  130. Context* ctx = context;
  131. FuriString* string = furi_string_alloc();
  132. canvas_set_font(canvas, FontPrimary);
  133. if(ctx->access_points_count >= MAX_ACCESS_POINTS) {
  134. canvas_draw_str(canvas, 118, 10, "!");
  135. }
  136. switch(ctx->view_state) {
  137. case SHOW_NMEA:
  138. if(UART_CH_ESP == UART_CH_GPS) {
  139. canvas_draw_str(canvas, 0, 10, "GPS channel invalid!");
  140. canvas_draw_str(canvas, 0, 20, "Change UART");
  141. canvas_draw_str(canvas, 0, 30, "channel");
  142. canvas_draw_str(canvas, 0, 40, "in the Momentum");
  143. canvas_draw_str(canvas, 0, 50, "app");
  144. } else {
  145. furi_string_printf(
  146. string, "%f", isnan(ctx->gps_data.latitude) ? 0 : (double)ctx->gps_data.latitude);
  147. canvas_draw_str(canvas, 0, 10, furi_string_get_cstr(string));
  148. furi_string_printf(
  149. string,
  150. "%f",
  151. isnan(ctx->gps_data.longitude) ? 0 : (double)ctx->gps_data.longitude);
  152. canvas_draw_str(canvas, 0, 20, furi_string_get_cstr(string));
  153. furi_string_printf(string, "%d sats", ctx->gps_data.satelites);
  154. canvas_draw_str(canvas, 0, 30, furi_string_get_cstr(string));
  155. furi_string_printf(
  156. string,
  157. "%02d:%02d:%02dZ",
  158. ctx->gps_data.hour,
  159. ctx->gps_data.minute,
  160. ctx->gps_data.second);
  161. canvas_draw_str(canvas, 0, 40, furi_string_get_cstr(string));
  162. canvas_draw_str(canvas, 70, 10, "GPS DATA");
  163. }
  164. elements_button_left(canvas, "Back");
  165. canvas_draw_icon(canvas, 82, 20, &I_WarningDolphinFlip_45x42);
  166. break;
  167. case NO_APS:
  168. canvas_draw_str(canvas, 80, 30, "No AP's");
  169. canvas_draw_str(canvas, 80, 40, "Found!");
  170. canvas_draw_icon(canvas, 1, 9, &I_DolphinWait_59x54);
  171. break;
  172. case NORMAL:
  173. default:
  174. canvas_draw_frame(canvas, 0, 0, 128, 64);
  175. furi_string_printf(
  176. string, "%d/%d", ctx->access_points_index + 1, ctx->access_points_count);
  177. canvas_draw_str(canvas, 3, 12, furi_string_get_cstr(string));
  178. draw_access_point(canvas, ctx);
  179. break;
  180. }
  181. furi_mutex_release(ctx->mutex);
  182. furi_string_free(string);
  183. }
  184. int32_t wardriver_app() {
  185. // Disable expansion protocol to avoid interference with UART Handle
  186. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  187. expansion_disable(expansion);
  188. // turn off 5v, so it gets reset on startup
  189. if(furi_hal_power_is_otg_enabled()) {
  190. furi_hal_power_disable_otg();
  191. }
  192. // Enable 5v on startup
  193. uint8_t attempts = 0;
  194. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  195. furi_hal_power_enable_otg();
  196. furi_delay_ms(10);
  197. }
  198. furi_delay_ms(200);
  199. Context* ctx = malloc(sizeof(Context));
  200. ctx->queue = furi_message_queue_alloc(8, sizeof(Event));
  201. ctx->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  202. ctx->access_points_count = 0;
  203. ctx->access_points_index = 0;
  204. ctx->pressedButton = false;
  205. ctx->view_state = NO_APS;
  206. wardriver_uart_init(ctx);
  207. ViewPort* view_port = view_port_alloc();
  208. view_port_draw_callback_set(view_port, render_callback, ctx);
  209. view_port_input_callback_set(view_port, input_callback, ctx->queue);
  210. Gui* gui = furi_record_open(RECORD_GUI);
  211. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  212. FuriTimer* timer = furi_timer_alloc(tick_callback, FuriTimerTypePeriodic, ctx->queue);
  213. furi_timer_start(timer, 100);
  214. // application loop
  215. Event event;
  216. bool processing = true;
  217. do {
  218. if(furi_message_queue_get(ctx->queue, &event, FuriWaitForever) == FuriStatusOk) {
  219. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  220. switch(event.type) {
  221. case EventTypeKey:
  222. if(event.input.type == InputTypeShort && event.input.key == InputKeyBack) {
  223. if(ctx->view_state == SHOW_NMEA) {
  224. ctx->view_state = NORMAL;
  225. } else {
  226. processing = false;
  227. }
  228. } else if(event.input.type == InputTypeLong && event.input.key == InputKeyBack) {
  229. if(ctx->view_state == SHOW_NMEA) {
  230. ctx->view_state = NORMAL;
  231. } else {
  232. processing = false;
  233. }
  234. } else if(event.input.type == InputTypeLong && event.input.key == InputKeyOk) {
  235. } else if(event.input.type == InputTypePress && event.input.key == InputKeyDown) {
  236. ctx->access_points_index--;
  237. if(ctx->access_points_index < 0) {
  238. ctx->access_points_index = ctx->access_points_count - 1;
  239. }
  240. ctx->active_access_point = ctx->access_points[ctx->access_points_index];
  241. ctx->pressedButton = true;
  242. } else if(event.input.type == InputTypePress && event.input.key == InputKeyUp) {
  243. ctx->access_points_index++;
  244. if(ctx->access_points_index >= ctx->access_points_count) {
  245. ctx->access_points_index = 0;
  246. }
  247. ctx->active_access_point = ctx->access_points[ctx->access_points_index];
  248. ctx->pressedButton = true;
  249. } else if(event.input.type == InputTypePress && event.input.key == InputKeyLeft) {
  250. if(ctx->view_state == NORMAL) {
  251. ctx->view_state = SHOW_NMEA;
  252. } else if(ctx->view_state == SHOW_NMEA) {
  253. ctx->view_state = NORMAL;
  254. }
  255. } else if(event.input.type == InputTypePress && event.input.key == InputKeyRight) {
  256. }
  257. break;
  258. case EventTypeTick:
  259. // fix for the empty active access point when there was no interaction
  260. if(!ctx->pressedButton) {
  261. ctx->access_points_index = 0;
  262. ctx->active_access_point = ctx->access_points[ctx->access_points_index];
  263. }
  264. break;
  265. default:
  266. break;
  267. }
  268. view_port_update(view_port);
  269. } else {
  270. processing = false;
  271. }
  272. } while(processing);
  273. furi_timer_free(timer);
  274. view_port_enabled_set(view_port, false);
  275. gui_remove_view_port(gui, view_port);
  276. view_port_free(view_port);
  277. furi_record_close(RECORD_GUI);
  278. furi_message_queue_free(ctx->queue);
  279. furi_mutex_free(ctx->mutex);
  280. wardriver_uart_deinit(ctx);
  281. save_file(ctx);
  282. free(ctx);
  283. furi_hal_light_set(LightBlue, 0);
  284. furi_hal_light_set(LightGreen, 0);
  285. if(furi_hal_power_is_otg_enabled()) {
  286. furi_hal_power_disable_otg();
  287. }
  288. // Return previous state of expansion
  289. expansion_enable(expansion);
  290. furi_record_close(RECORD_EXPANSION);
  291. return 0;
  292. }