wardriver.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #include "wardriver.h"
  2. #include "wardriver_uart.h"
  3. void save_file(Context* ctx) {
  4. Storage* storage = furi_record_open(RECORD_STORAGE);
  5. FuriHalRtcDateTime datetime;
  6. furi_hal_rtc_get_datetime(&datetime);
  7. FuriString* filename = furi_string_alloc();
  8. furi_string_printf(
  9. filename,
  10. "%s/%s_%d_%d_%d_%d_%d_%d.txt",
  11. FILE_PATH,
  12. "wigle",
  13. datetime.year,
  14. datetime.month,
  15. datetime.day,
  16. datetime.hour,
  17. datetime.minute,
  18. datetime.second);
  19. File* file = storage_file_alloc(storage);
  20. if(!storage_common_exists(storage, FILE_PATH)) {
  21. storage_common_mkdir(storage, FILE_PATH);
  22. }
  23. if(!storage_file_open(file, furi_string_get_cstr(filename), FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
  24. FURI_LOG_I(appname, "Failed to open file");
  25. storage_file_close(file);
  26. storage_file_free(file);
  27. furi_string_free(filename);
  28. furi_record_close(RECORD_STORAGE);
  29. furi_hal_light_blink_start(LightRed, 100, 100, 5000);
  30. return;
  31. }
  32. furi_string_reset(ctx->buffer);
  33. // WIGLE HEADERS DONT CHANGE THIS ITS IMPORTANT!
  34. furi_string_printf(
  35. ctx->buffer,
  36. "%s,%s,%s,%s,%s,%s,%s,%s\r\n",
  37. "WigleWifi-1.4",
  38. "appRelease=v2.0",
  39. "model=S33",
  40. "release=XtremeFW",
  41. "Flipper Zero",
  42. "",
  43. "Wardriver",
  44. "S33");
  45. furi_string_cat_printf(
  46. ctx->buffer,
  47. "%s,%s,%s,%s,%s,%s,%s\r\n",
  48. "MAC",
  49. "SSID",
  50. "FirstSeen",
  51. "Channel",
  52. "RSSI",
  53. "CurrentLatitude",
  54. "CurrentLongitude");
  55. if(!storage_file_write(
  56. file, furi_string_get_cstr(ctx->buffer), strlen(furi_string_get_cstr(ctx->buffer)))) {
  57. FURI_LOG_I(appname, "Failed to write header to file");
  58. }
  59. for(int i = 0; i < ctx->access_points_count; i++) {
  60. AccessPoint ap = ctx->access_points[i];
  61. furi_string_printf(
  62. ctx->buffer,
  63. "%s,%s,%04d-%02d-%02d %02d:%02d:%02d,%d,%d,%f,%f\r\n",
  64. ap.bssid,
  65. ap.ssid,
  66. ap.datetime.year,
  67. ap.datetime.month,
  68. ap.datetime.day,
  69. ap.datetime.hour,
  70. ap.datetime.minute,
  71. ap.datetime.second,
  72. ap.rssi,
  73. ap.channel,
  74. (double)ap.latitude,
  75. (double)ap.longitude);
  76. if(!storage_file_write(
  77. file,
  78. furi_string_get_cstr(ctx->buffer),
  79. strlen(furi_string_get_cstr(ctx->buffer)))) {
  80. FURI_LOG_I(appname, "Failed to write AP to file");
  81. }
  82. free(ap.ssid);
  83. free(ap.bssid);
  84. }
  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. AccessPoint ap = ctx->active_access_point;
  105. canvas_draw_str_aligned(canvas, 62, 25, AlignCenter, AlignBottom, ap.ssid);
  106. canvas_set_font(canvas, FontSecondary);
  107. canvas_draw_str_aligned(canvas, 38, 12, AlignLeft, AlignBottom, ap.bssid);
  108. furi_string_printf(ctx->buffer, "Signal strength: %ddBm", ap.rssi);
  109. canvas_draw_str_aligned(
  110. canvas, 3, 35, AlignLeft, AlignBottom, furi_string_get_cstr(ctx->buffer));
  111. furi_string_printf(ctx->buffer, "CH: %d", ap.channel);
  112. canvas_draw_str_aligned(
  113. canvas, 3, 47, AlignLeft, AlignBottom, furi_string_get_cstr(ctx->buffer));
  114. furi_string_printf(ctx->buffer, "%d", ap.packetRxCount);
  115. canvas_draw_icon(canvas, 35, 39, &I_down);
  116. canvas_draw_str_aligned(
  117. canvas, 45, 47, AlignLeft, AlignBottom, furi_string_get_cstr(ctx->buffer));
  118. furi_string_printf(ctx->buffer, "%d", ap.packetTxCount);
  119. canvas_draw_icon(canvas, 85, 38, &I_up);
  120. canvas_draw_str_aligned(
  121. canvas, 95, 47, AlignLeft, AlignBottom, furi_string_get_cstr(ctx->buffer));
  122. furi_string_printf(
  123. ctx->buffer,
  124. "Seen: %02d:%02d:%02d (%lds ago)",
  125. ap.datetime.hour,
  126. ap.datetime.minute,
  127. ap.datetime.second,
  128. furi_hal_rtc_get_timestamp() - furi_hal_rtc_datetime_to_timestamp(&ap.datetime));
  129. canvas_draw_str_aligned(
  130. canvas, 3, 59, AlignLeft, AlignBottom, furi_string_get_cstr(ctx->buffer));
  131. }
  132. static void render_callback(Canvas* canvas, void* context) {
  133. Context* ctx = context;
  134. canvas_set_font(canvas, FontPrimary);
  135. if(ctx->access_points_count >= MAX_ACCESS_POINTS) {
  136. canvas_draw_str(canvas, 118, 10, "!");
  137. }
  138. switch(ctx->view_state) {
  139. case SHOW_NMEA:
  140. if(UART_CH_ESP == UART_CH_GPS) {
  141. canvas_draw_str(canvas, 0, 10, "GPS channel invalid!");
  142. canvas_draw_str(canvas, 0, 20, "Change UART");
  143. canvas_draw_str(canvas, 0, 30, "channel");
  144. canvas_draw_str(canvas, 0, 40, "in the Xtreme");
  145. canvas_draw_str(canvas, 0, 50, "app");
  146. } else {
  147. furi_string_printf(
  148. ctx->buffer,
  149. "%f",
  150. isnan(ctx->gps_data.latitude) ? 0 : (double)ctx->gps_data.latitude);
  151. canvas_draw_str(canvas, 0, 10, furi_string_get_cstr(ctx->buffer));
  152. furi_string_printf(
  153. ctx->buffer,
  154. "%f",
  155. isnan(ctx->gps_data.longitude) ? 0 : (double)ctx->gps_data.longitude);
  156. canvas_draw_str(canvas, 0, 20, furi_string_get_cstr(ctx->buffer));
  157. furi_string_printf(ctx->buffer, "%d sats", ctx->gps_data.satelites);
  158. canvas_draw_str(canvas, 0, 30, furi_string_get_cstr(ctx->buffer));
  159. furi_string_printf(
  160. ctx->buffer,
  161. "%02d:%02d:%02dZ",
  162. ctx->gps_data.hour,
  163. ctx->gps_data.minute,
  164. ctx->gps_data.second);
  165. canvas_draw_str(canvas, 0, 40, furi_string_get_cstr(ctx->buffer));
  166. canvas_draw_str(canvas, 70, 10, "GPS DATA");
  167. }
  168. elements_button_left(canvas, "Back");
  169. canvas_draw_icon(canvas, 71, 15, &I_DolphinCommon_56x48);
  170. break;
  171. case NO_APS:
  172. canvas_draw_str(canvas, 80, 30, "No AP's");
  173. canvas_draw_str(canvas, 80, 40, "Found!");
  174. canvas_draw_icon(canvas, 1, 4, &I_DolphinWait_61x59);
  175. break;
  176. case NORMAL:
  177. default:
  178. canvas_draw_frame(canvas, 0, 0, 128, 64);
  179. furi_string_printf(
  180. ctx->buffer, "%d/%d", ctx->access_points_index + 1, ctx->access_points_count);
  181. canvas_draw_str(canvas, 3, 12, furi_string_get_cstr(ctx->buffer));
  182. draw_access_point(canvas, ctx);
  183. break;
  184. }
  185. furi_mutex_release(ctx->mutex);
  186. }
  187. int32_t wardriver_app() {
  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->buffer = furi_string_alloc();
  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. furi_string_free(ctx->buffer);
  282. wardriver_uart_deinit(ctx);
  283. save_file(ctx);
  284. free(ctx);
  285. furi_hal_light_set(LightBlue, 0);
  286. furi_hal_light_set(LightGreen, 0);
  287. if(furi_hal_power_is_otg_enabled()) {
  288. furi_hal_power_disable_otg();
  289. }
  290. return 0;
  291. }