| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- #include "wardriver.h"
- #include "wardriver_uart.h"
- #include <expansion/expansion.h>
- void save_file(Context* ctx) {
- Storage* storage = furi_record_open(RECORD_STORAGE);
- DateTime datetime;
- furi_hal_rtc_get_datetime(&datetime);
- FuriString* filename = furi_string_alloc();
- furi_string_printf(
- filename,
- "%s/%s_%d_%d_%d_%d_%d_%d.txt",
- FILE_PATH,
- "wigle",
- datetime.year,
- datetime.month,
- datetime.day,
- datetime.hour,
- datetime.minute,
- datetime.second);
- File* file = storage_file_alloc(storage);
- if(!storage_common_exists(storage, FILE_PATH)) {
- storage_common_mkdir(storage, FILE_PATH);
- }
- if(!storage_file_open(file, furi_string_get_cstr(filename), FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
- FURI_LOG_I(appname, "Failed to open file");
- storage_file_close(file);
- storage_file_free(file);
- furi_string_free(filename);
- furi_record_close(RECORD_STORAGE);
- furi_hal_light_blink_start(LightRed, 100, 100, 5000);
- return;
- }
- FuriString* string = furi_string_alloc();
- // WIGLE HEADERS DONT CHANGE THIS ITS IMPORTANT!
- furi_string_printf(
- string,
- "%s,%s,%s,%s,%s,%s,%s,%s\r\n",
- "WigleWifi-1.4",
- "appRelease=v2.0",
- "model=S33",
- "release=MomentumFW",
- "Flipper Zero",
- "",
- "Wardriver",
- "S33");
- furi_string_cat_printf(
- string,
- "%s,%s,%s,%s,%s,%s,%s\r\n",
- "MAC",
- "SSID",
- "FirstSeen",
- "Channel",
- "RSSI",
- "CurrentLatitude",
- "CurrentLongitude");
- if(!storage_file_write(
- file, furi_string_get_cstr(string), strlen(furi_string_get_cstr(string)))) {
- FURI_LOG_I(appname, "Failed to write header to file");
- }
- for(int i = 0; i < ctx->access_points_count; i++) {
- AccessPoint ap = ctx->access_points[i];
- furi_string_printf(
- string,
- "%s,%s,%04d-%02d-%02d %02d:%02d:%02d,%d,%d,%f,%f\r\n",
- ap.bssid,
- ap.ssid,
- ap.datetime.year,
- ap.datetime.month,
- ap.datetime.day,
- ap.datetime.hour,
- ap.datetime.minute,
- ap.datetime.second,
- ap.rssi,
- ap.channel,
- (double)ap.latitude,
- (double)ap.longitude);
- if(!storage_file_write(
- file, furi_string_get_cstr(string), strlen(furi_string_get_cstr(string)))) {
- FURI_LOG_I(appname, "Failed to write AP to file");
- }
- free(ap.ssid);
- free(ap.bssid);
- }
- furi_string_free(string);
- storage_file_close(file);
- storage_file_free(file);
- furi_string_free(filename);
- furi_record_close(RECORD_STORAGE);
- return;
- }
- static void tick_callback(void* ctx_q) {
- furi_assert(ctx_q);
- FuriMessageQueue* queue = ctx_q;
- Event event = {.type = EventTypeTick};
- furi_message_queue_put(queue, &event, 0);
- }
- static void input_callback(InputEvent* input_event, FuriMessageQueue* queue) {
- furi_assert(queue);
- Event event = {.type = EventTypeKey, .input = *input_event};
- furi_message_queue_put(queue, &event, FuriWaitForever);
- }
- static void draw_access_point(Canvas* canvas, Context* context) {
- Context* ctx = context;
- FuriString* string = furi_string_alloc();
- AccessPoint ap = ctx->active_access_point;
- canvas_draw_str_aligned(canvas, 62, 25, AlignCenter, AlignBottom, ap.ssid);
- canvas_set_font(canvas, FontSecondary);
- canvas_draw_str_aligned(canvas, 38, 12, AlignLeft, AlignBottom, ap.bssid);
- furi_string_printf(string, "Signal strength: %ddBm", ap.rssi);
- canvas_draw_str_aligned(canvas, 3, 35, AlignLeft, AlignBottom, furi_string_get_cstr(string));
- furi_string_printf(string, "CH: %d", ap.channel);
- canvas_draw_str_aligned(canvas, 3, 47, AlignLeft, AlignBottom, furi_string_get_cstr(string));
- furi_string_printf(string, "%d", ap.packetRxCount);
- canvas_draw_icon(canvas, 35, 39, &I_down);
- canvas_draw_str_aligned(canvas, 45, 47, AlignLeft, AlignBottom, furi_string_get_cstr(string));
- furi_string_printf(string, "%d", ap.packetTxCount);
- canvas_draw_icon(canvas, 85, 38, &I_up);
- canvas_draw_str_aligned(canvas, 95, 47, AlignLeft, AlignBottom, furi_string_get_cstr(string));
- furi_string_printf(
- string,
- "Seen: %02d:%02d:%02d (%lds ago)",
- ap.datetime.hour,
- ap.datetime.minute,
- ap.datetime.second,
- furi_hal_rtc_get_timestamp() - datetime_datetime_to_timestamp(&ap.datetime));
- canvas_draw_str_aligned(canvas, 3, 59, AlignLeft, AlignBottom, furi_string_get_cstr(string));
- furi_string_free(string);
- }
- static void render_callback(Canvas* canvas, void* context) {
- Context* ctx = context;
- FuriString* string = furi_string_alloc();
- canvas_set_font(canvas, FontPrimary);
- if(ctx->access_points_count >= MAX_ACCESS_POINTS) {
- canvas_draw_str(canvas, 118, 10, "!");
- }
- switch(ctx->view_state) {
- case SHOW_NMEA:
- if(UART_CH_ESP == UART_CH_GPS) {
- canvas_draw_str(canvas, 0, 10, "GPS channel invalid!");
- canvas_draw_str(canvas, 0, 20, "Change UART");
- canvas_draw_str(canvas, 0, 30, "channel");
- canvas_draw_str(canvas, 0, 40, "in the Momentum");
- canvas_draw_str(canvas, 0, 50, "app");
- } else {
- furi_string_printf(
- string, "%f", isnan(ctx->gps_data.latitude) ? 0 : (double)ctx->gps_data.latitude);
- canvas_draw_str(canvas, 0, 10, furi_string_get_cstr(string));
- furi_string_printf(
- string,
- "%f",
- isnan(ctx->gps_data.longitude) ? 0 : (double)ctx->gps_data.longitude);
- canvas_draw_str(canvas, 0, 20, furi_string_get_cstr(string));
- furi_string_printf(string, "%d sats", ctx->gps_data.satelites);
- canvas_draw_str(canvas, 0, 30, furi_string_get_cstr(string));
- furi_string_printf(
- string,
- "%02d:%02d:%02dZ",
- ctx->gps_data.hour,
- ctx->gps_data.minute,
- ctx->gps_data.second);
- canvas_draw_str(canvas, 0, 40, furi_string_get_cstr(string));
- canvas_draw_str(canvas, 70, 10, "GPS DATA");
- }
- elements_button_left(canvas, "Back");
- canvas_draw_icon(canvas, 82, 20, &I_WarningDolphinFlip_45x42);
- break;
- case NO_APS:
- canvas_draw_str(canvas, 80, 30, "No AP's");
- canvas_draw_str(canvas, 80, 40, "Found!");
- canvas_draw_icon(canvas, 1, 9, &I_DolphinWait_59x54);
- break;
- case NORMAL:
- default:
- canvas_draw_frame(canvas, 0, 0, 128, 64);
- furi_string_printf(
- string, "%d/%d", ctx->access_points_index + 1, ctx->access_points_count);
- canvas_draw_str(canvas, 3, 12, furi_string_get_cstr(string));
- draw_access_point(canvas, ctx);
- break;
- }
- furi_mutex_release(ctx->mutex);
- furi_string_free(string);
- }
- int32_t wardriver_app() {
- // Disable expansion protocol to avoid interference with UART Handle
- Expansion* expansion = furi_record_open(RECORD_EXPANSION);
- expansion_disable(expansion);
- // turn off 5v, so it gets reset on startup
- if(furi_hal_power_is_otg_enabled()) {
- furi_hal_power_disable_otg();
- }
- // Enable 5v on startup
- uint8_t attempts = 0;
- while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
- furi_hal_power_enable_otg();
- furi_delay_ms(10);
- }
- furi_delay_ms(200);
- Context* ctx = malloc(sizeof(Context));
- ctx->queue = furi_message_queue_alloc(8, sizeof(Event));
- ctx->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
- ctx->access_points_count = 0;
- ctx->access_points_index = 0;
- ctx->pressedButton = false;
- ctx->view_state = NO_APS;
- wardriver_uart_init(ctx);
- ViewPort* view_port = view_port_alloc();
- view_port_draw_callback_set(view_port, render_callback, ctx);
- view_port_input_callback_set(view_port, input_callback, ctx->queue);
- Gui* gui = furi_record_open(RECORD_GUI);
- gui_add_view_port(gui, view_port, GuiLayerFullscreen);
- FuriTimer* timer = furi_timer_alloc(tick_callback, FuriTimerTypePeriodic, ctx->queue);
- furi_timer_start(timer, 100);
- // application loop
- Event event;
- bool processing = true;
- do {
- if(furi_message_queue_get(ctx->queue, &event, FuriWaitForever) == FuriStatusOk) {
- furi_mutex_acquire(ctx->mutex, FuriWaitForever);
- switch(event.type) {
- case EventTypeKey:
- if(event.input.type == InputTypeShort && event.input.key == InputKeyBack) {
- if(ctx->view_state == SHOW_NMEA) {
- ctx->view_state = NORMAL;
- } else {
- processing = false;
- }
- } else if(event.input.type == InputTypeLong && event.input.key == InputKeyBack) {
- if(ctx->view_state == SHOW_NMEA) {
- ctx->view_state = NORMAL;
- } else {
- processing = false;
- }
- } else if(event.input.type == InputTypeLong && event.input.key == InputKeyOk) {
- } else if(event.input.type == InputTypePress && event.input.key == InputKeyDown) {
- ctx->access_points_index--;
- if(ctx->access_points_index < 0) {
- ctx->access_points_index = ctx->access_points_count - 1;
- }
- ctx->active_access_point = ctx->access_points[ctx->access_points_index];
- ctx->pressedButton = true;
- } else if(event.input.type == InputTypePress && event.input.key == InputKeyUp) {
- ctx->access_points_index++;
- if(ctx->access_points_index >= ctx->access_points_count) {
- ctx->access_points_index = 0;
- }
- ctx->active_access_point = ctx->access_points[ctx->access_points_index];
- ctx->pressedButton = true;
- } else if(event.input.type == InputTypePress && event.input.key == InputKeyLeft) {
- if(ctx->view_state == NORMAL) {
- ctx->view_state = SHOW_NMEA;
- } else if(ctx->view_state == SHOW_NMEA) {
- ctx->view_state = NORMAL;
- }
- } else if(event.input.type == InputTypePress && event.input.key == InputKeyRight) {
- }
- break;
- case EventTypeTick:
- // fix for the empty active access point when there was no interaction
- if(!ctx->pressedButton) {
- ctx->access_points_index = 0;
- ctx->active_access_point = ctx->access_points[ctx->access_points_index];
- }
- break;
- default:
- break;
- }
- view_port_update(view_port);
- } else {
- processing = false;
- }
- } while(processing);
- furi_timer_free(timer);
- view_port_enabled_set(view_port, false);
- gui_remove_view_port(gui, view_port);
- view_port_free(view_port);
- furi_record_close(RECORD_GUI);
- furi_message_queue_free(ctx->queue);
- furi_mutex_free(ctx->mutex);
- wardriver_uart_deinit(ctx);
- save_file(ctx);
- free(ctx);
- furi_hal_light_set(LightBlue, 0);
- furi_hal_light_set(LightGreen, 0);
- if(furi_hal_power_is_otg_enabled()) {
- furi_hal_power_disable_otg();
- }
- // Return previous state of expansion
- expansion_enable(expansion);
- furi_record_close(RECORD_EXPANSION);
- return 0;
- }
|