sniffer.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <notification/notification_messages.h>
  5. #include <assets_icons.h>
  6. #include <toolbox/stream/file_stream.h>
  7. #include <xtreme.h>
  8. #include "helpers/minmea.h"
  9. #include "wifisniffer_icons.h"
  10. #define appname "ll-wifisniffer"
  11. #define RX_BUF_SIZE 2048
  12. #define MAX_ACCESS_POINTS 2048 // imagine getting this many access points
  13. #define MAX_SSID_LENGTH 32
  14. #define MAX_BSSID_LENGTH 18
  15. #define UART_CH_ESP \
  16. (XTREME_SETTINGS()->uart_esp_channel == UARTDefault ? FuriHalUartIdUSART1 : \
  17. FuriHalUartIdLPUART1)
  18. #define UART_CH_GPS \
  19. (XTREME_SETTINGS()->uart_nmea_channel == UARTDefault ? FuriHalUartIdUSART1 : \
  20. FuriHalUartIdLPUART1)
  21. #define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
  22. typedef enum {
  23. WorkerEvtStop = (1 << 0),
  24. WorkerEvtRxDone = (1 << 1),
  25. } WorkerEvtFlags;
  26. typedef enum {
  27. EventTypeKey,
  28. EventTypeTick,
  29. } EventType;
  30. typedef struct {
  31. EventType type;
  32. InputEvent input;
  33. } Event;
  34. typedef struct {
  35. char* recievedMac;
  36. char* sentMac;
  37. } Packet;
  38. typedef struct {
  39. char* ssid;
  40. char* bssid;
  41. int8_t rssi;
  42. uint8_t channel;
  43. FuriHalRtcDateTime datetime;
  44. uint16_t packetRxCount;
  45. uint16_t packetTxCount;
  46. float latitude;
  47. float longitude;
  48. } AccessPoint;
  49. typedef struct {
  50. FuriMessageQueue* queue;
  51. FuriMutex* mutex;
  52. FuriString* buffer;
  53. FuriString* buffer2;
  54. NotificationApp* notifications;
  55. FuriThread* thread_esp;
  56. FuriStreamBuffer* rx_stream_esp;
  57. uint8_t rx_buf_esp[2048];
  58. FuriThread* thread_gps;
  59. FuriStreamBuffer* rx_stream_gps;
  60. uint8_t rx_buf_gps[2048];
  61. File* file;
  62. char* dataString;
  63. uint16_t access_points_count;
  64. AccessPoint access_points[MAX_ACCESS_POINTS];
  65. int16_t access_points_index;
  66. AccessPoint active_access_point;
  67. bool extra_info;
  68. bool pressedButton;
  69. float last_latitude;
  70. float last_longitude;
  71. } Context;
  72. static void tick_callback(void* ctx_q) {
  73. furi_assert(ctx_q);
  74. FuriMessageQueue* queue = ctx_q;
  75. Event event = {.type = EventTypeTick};
  76. furi_message_queue_put(queue, &event, 0);
  77. }
  78. static void input_callback(InputEvent* input_event, FuriMessageQueue* queue) {
  79. furi_assert(queue);
  80. Event event = {.type = EventTypeKey, .input = *input_event};
  81. furi_message_queue_put(queue, &event, FuriWaitForever);
  82. }
  83. static void show_access_point(Canvas* canvas, Context* context) {
  84. Context* ctx = context;
  85. AccessPoint ap = ctx->active_access_point;
  86. canvas_draw_str_aligned(canvas, 62, 25, AlignCenter, AlignBottom, ap.ssid);
  87. canvas_set_font(canvas, FontSecondary);
  88. canvas_draw_str_aligned(
  89. canvas, 38 + (ctx->access_points_count > 99 ? 5 : 0), 12, AlignLeft, AlignBottom, ap.bssid);
  90. furi_string_printf(ctx->buffer, "Signal strength: %ddBm", ap.rssi);
  91. canvas_draw_str_aligned(
  92. canvas, 3, 35, AlignLeft, AlignBottom, furi_string_get_cstr(ctx->buffer));
  93. furi_string_printf(ctx->buffer, "CH: %d", ap.channel);
  94. canvas_draw_str_aligned(
  95. canvas, 3, 47, AlignLeft, AlignBottom, furi_string_get_cstr(ctx->buffer));
  96. if(ap.latitude == 0 && ap.longitude == 0) {
  97. canvas_draw_str_aligned(canvas, 29, 47, AlignLeft, AlignBottom, "X");
  98. } else {
  99. canvas_draw_str_aligned(canvas, 29, 47, AlignLeft, AlignBottom, "O");
  100. }
  101. furi_string_printf(ctx->buffer, "%d", ap.packetRxCount);
  102. canvas_draw_icon(canvas, 35, 39, &I_down);
  103. canvas_draw_str_aligned(
  104. canvas, 45, 47, AlignLeft, AlignBottom, furi_string_get_cstr(ctx->buffer));
  105. furi_string_printf(ctx->buffer, "%d", ap.packetTxCount);
  106. canvas_draw_icon(canvas, 85, 38, &I_up);
  107. canvas_draw_str_aligned(
  108. canvas, 95, 47, AlignLeft, AlignBottom, furi_string_get_cstr(ctx->buffer));
  109. furi_string_printf(
  110. ctx->buffer,
  111. "Seen: %d:%d:%d (%lds ago)",
  112. ap.datetime.hour,
  113. ap.datetime.minute,
  114. ap.datetime.second,
  115. furi_hal_rtc_get_timestamp() - furi_hal_rtc_datetime_to_timestamp(&ap.datetime));
  116. canvas_draw_str_aligned(
  117. canvas, 3, 59, AlignLeft, AlignBottom, furi_string_get_cstr(ctx->buffer));
  118. }
  119. static void render_callback(Canvas* canvas, void* context) {
  120. Context* ctx = context;
  121. canvas_draw_frame(canvas, 0, 0, 128, 64);
  122. canvas_set_font(canvas, FontPrimary);
  123. if(ctx->access_points_count >= MAX_ACCESS_POINTS) {
  124. canvas_draw_str(canvas, 118, 10, "!");
  125. }
  126. if(ctx->access_points_count == 0) {
  127. canvas_draw_str(canvas, 80, 30, "No AP's");
  128. canvas_draw_str(canvas, 80, 40, "Found!");
  129. canvas_draw_icon(canvas, 1, 4, &I_DolphinWait_61x59);
  130. } else {
  131. canvas_draw_frame(canvas, 0, 0, 35 + (ctx->access_points_count > 99 ? 5 : 0), 15);
  132. furi_string_printf(
  133. ctx->buffer, "%d/%d", ctx->access_points_index + 1, ctx->access_points_count);
  134. canvas_draw_str(canvas, 3, 12, furi_string_get_cstr(ctx->buffer));
  135. show_access_point(canvas, ctx);
  136. }
  137. // canvas_clear(canvas);
  138. furi_mutex_release(ctx->mutex);
  139. }
  140. // order ctx->access_points by ssid alphabetically
  141. static void sort_access_points(Context* ctx) {
  142. for(int i = 0; i < ctx->access_points_count; i++) {
  143. for(int j = i + 1; j < ctx->access_points_count; j++) {
  144. if(strcmp(ctx->access_points[i].ssid, ctx->access_points[j].ssid) > 0) {
  145. AccessPoint temp = ctx->access_points[i];
  146. ctx->access_points[i] = ctx->access_points[j];
  147. ctx->access_points[j] = temp;
  148. }
  149. }
  150. }
  151. }
  152. // set the index from the active access point
  153. static void set_index_from_access_points(Context* ctx) {
  154. for(int i = 0; i < ctx->access_points_count; i++) {
  155. if(ctx->access_points[i].bssid == ctx->active_access_point.bssid) {
  156. ctx->access_points_index = i;
  157. break;
  158. }
  159. }
  160. }
  161. static void removeSpaces(char* str) {
  162. // Remove spaces from the beginning of the string
  163. int i = 0;
  164. while(isspace((unsigned char)str[i])) {
  165. i++;
  166. }
  167. // Move the remaining characters to the beginning of the string
  168. int j = 0;
  169. while(str[i] != '\0') {
  170. str[j++] = str[i++];
  171. }
  172. str[j] = '\0';
  173. // Remove spaces from the end of the string
  174. int len = strlen(str);
  175. while(len > 0 && isspace((unsigned char)str[len - 1])) {
  176. str[--len] = '\0';
  177. }
  178. }
  179. static void parseLine(void* context, char* line) {
  180. Context* ctx = context;
  181. AccessPoint ap = {.ssid = malloc(MAX_SSID_LENGTH + 1), .bssid = malloc(MAX_BSSID_LENGTH + 1)};
  182. Packet pkt = {.recievedMac = malloc(18 + 1), .sentMac = malloc(18 + 1)};
  183. char* token = strtok(line, ",");
  184. int i = 0;
  185. bool isAp = false;
  186. bool isValid = true;
  187. UNUSED(isValid);
  188. while(token != NULL) {
  189. switch(i) {
  190. case 0:
  191. if(strcmp(token, "AR") == 0) {
  192. isAp = true;
  193. isValid = true;
  194. } else if(strcmp(token, "PK") == 0) {
  195. isAp = false;
  196. isValid = true;
  197. }
  198. break;
  199. case 1:
  200. if(isAp && isValid) {
  201. removeSpaces(token);
  202. strcpy(ap.ssid, token);
  203. } else if(!isAp && isValid) {
  204. strncpy(pkt.recievedMac, token, 18);
  205. pkt.recievedMac[18] = '\0';
  206. }
  207. break;
  208. case 2:
  209. if(isAp && isValid) {
  210. strcpy(ap.bssid, token);
  211. } else if(!isAp && isValid) {
  212. strncpy(pkt.sentMac, token, 18);
  213. pkt.sentMac[18] = '\0';
  214. }
  215. break;
  216. case 3:
  217. if(isAp && isValid) {
  218. ap.rssi = atoi(token);
  219. }
  220. break;
  221. case 4:
  222. if(isAp && isValid) {
  223. ap.channel = atoi(token);
  224. }
  225. break;
  226. }
  227. token = strtok(NULL, ",");
  228. i++;
  229. }
  230. if(isAp && isValid) {
  231. // free the packet
  232. free(pkt.recievedMac);
  233. free(pkt.sentMac);
  234. // check if values are valid
  235. // bssid needs an ":"
  236. // rssi needs to be negative
  237. // channel needs to be between 1 and 14
  238. // ssid needs to be at least 1 character long
  239. if(ap.bssid[2] != ':' || ap.bssid[5] != ':' || ap.bssid[8] != ':' || ap.bssid[11] != ':' ||
  240. ap.bssid[14] != ':' || ap.rssi > 0 || ap.channel < 1 || ap.channel > 14 ||
  241. strlen(ap.ssid) < 1) {
  242. free(ap.ssid);
  243. free(ap.bssid);
  244. return;
  245. }
  246. furi_hal_light_set(LightBlue, 0);
  247. furi_hal_light_set(LightGreen, 255);
  248. furi_hal_rtc_get_datetime(&ap.datetime);
  249. if(isnan(ctx->last_latitude) || isnan(ctx->last_longitude)) {
  250. ctx->last_latitude = 0;
  251. ctx->last_longitude = 0;
  252. } else {
  253. ap.latitude = ctx->last_latitude;
  254. ap.longitude = ctx->last_longitude;
  255. }
  256. // check if ap is already in the list otherwise add it but update the rssi
  257. bool found = false;
  258. for(size_t i = 0; i < ctx->access_points_count; i++) {
  259. if(strcmp(ctx->access_points[i].bssid, ap.bssid) == 0) {
  260. found = true;
  261. //update rssi channel datetime
  262. ctx->access_points[i].rssi = ap.rssi;
  263. ctx->access_points[i].channel = ap.channel;
  264. ctx->access_points[i].datetime = ap.datetime;
  265. ctx->access_points[i].latitude = ap.latitude;
  266. ctx->access_points[i].longitude = ap.longitude;
  267. if(strcmp(ctx->active_access_point.bssid, ap.bssid) == 0) {
  268. ctx->active_access_point.rssi = ap.rssi;
  269. ctx->active_access_point.channel = ap.channel;
  270. ctx->active_access_point.datetime = ap.datetime;
  271. ctx->active_access_point.latitude = ap.latitude;
  272. ctx->active_access_point.longitude = ap.longitude;
  273. }
  274. free(ap.ssid);
  275. free(ap.bssid);
  276. break;
  277. }
  278. }
  279. if(!found) {
  280. memcpy(&ctx->access_points[ctx->access_points_count], &ap, sizeof(AccessPoint));
  281. ctx->access_points_count++;
  282. }
  283. sort_access_points(ctx);
  284. set_index_from_access_points(ctx);
  285. } else {
  286. // it is a packet so screw the ap
  287. free(ap.ssid);
  288. free(ap.bssid);
  289. // check if values are valid
  290. // mac needs to be 6 characters long
  291. if(strlen(pkt.recievedMac) != 17 || strlen(pkt.sentMac) != 17 ||
  292. ctx->access_points_count == 0) {
  293. free(pkt.recievedMac);
  294. free(pkt.sentMac);
  295. return;
  296. }
  297. furi_hal_light_set(LightGreen, 0);
  298. furi_hal_light_set(LightBlue, 255);
  299. for(size_t i = 0; i < ctx->access_points_count; i++) {
  300. if(strcmp(ctx->access_points[i].bssid, pkt.recievedMac) == 0) {
  301. ctx->access_points[i].packetRxCount++;
  302. break;
  303. }
  304. }
  305. for(size_t i = 0; i < ctx->access_points_count; i++) {
  306. if(strcmp(ctx->access_points[i].bssid, pkt.sentMac) == 0) {
  307. ctx->access_points[i].packetTxCount++;
  308. break;
  309. }
  310. }
  311. free(pkt.recievedMac);
  312. free(pkt.sentMac);
  313. }
  314. }
  315. static void uart_cb_esp(UartIrqEvent ev, uint8_t data, void* context) {
  316. Context* ctx = (Context*)context;
  317. if(ev == UartIrqEventRXNE) {
  318. furi_stream_buffer_send(ctx->rx_stream_esp, &data, 1, 0);
  319. furi_thread_flags_set(furi_thread_get_id(ctx->thread_esp), WorkerEvtRxDone);
  320. }
  321. }
  322. static int32_t uart_worker_esp(void* context) {
  323. Context* ctx = (Context*)context;
  324. size_t rx_offset = 0;
  325. while(1) {
  326. uint32_t events =
  327. furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
  328. furi_check((events & FuriFlagError) == 0);
  329. if(events & WorkerEvtStop) {
  330. break;
  331. }
  332. if(events & WorkerEvtRxDone) {
  333. size_t len = 0;
  334. do {
  335. // receive serial bytes into rx_buf, starting at rx_offset from the start of the buffer
  336. // the maximum we can receive is RX_BUF_SIZE - 1 - rx_offset
  337. len = furi_stream_buffer_receive(
  338. ctx->rx_stream_esp,
  339. ctx->rx_buf_esp + rx_offset,
  340. RX_BUF_SIZE - 1 - rx_offset,
  341. 0);
  342. if(len > 0) {
  343. // increase rx_offset by the number of bytes received, and null-terminate rx_buf
  344. rx_offset += len;
  345. ctx->rx_buf_esp[rx_offset] = '\0';
  346. // look for strings ending in newlines, starting at the start of rx_buf
  347. char* line_current = (char*)ctx->rx_buf_esp;
  348. while(1) {
  349. // skip null characters
  350. while(*line_current == '\0' &&
  351. line_current < (char*)ctx->rx_buf_esp + rx_offset - 1) {
  352. line_current++;
  353. }
  354. // find the next newline
  355. char* newline = strchr(line_current, '\n');
  356. if(newline) // newline found
  357. {
  358. // put a null terminator in place of the newline, to delimit the line string
  359. *newline = '\0';
  360. parseLine(ctx, line_current);
  361. // move the cursor to the character after the newline
  362. line_current = newline + 1;
  363. } else // no more newlines found
  364. {
  365. if(line_current >
  366. (char*)ctx->rx_buf_esp) // at least one line was found
  367. {
  368. // clear parsed lines, and move any leftover bytes to the start of rx_buf
  369. rx_offset = 0;
  370. while(
  371. *line_current) // stop when the original rx_offset terminator is reached
  372. {
  373. ctx->rx_buf_esp[rx_offset++] = *(line_current++);
  374. }
  375. }
  376. break; // go back to receiving bytes from the serial stream
  377. }
  378. }
  379. }
  380. } while(len > 0);
  381. }
  382. }
  383. furi_hal_uart_set_irq_cb(UART_CH_ESP, NULL, NULL);
  384. furi_stream_buffer_free(ctx->rx_stream_esp);
  385. return 0;
  386. }
  387. static void gps_uart_parse_nmea(Context* ctx, char* line) {
  388. switch(minmea_sentence_id(line, false)) {
  389. case MINMEA_SENTENCE_RMC: {
  390. struct minmea_sentence_rmc frame;
  391. if(minmea_parse_rmc(&frame, line)) {
  392. ctx->last_latitude = minmea_tocoord(&frame.latitude);
  393. ctx->last_longitude = minmea_tocoord(&frame.longitude);
  394. }
  395. } break;
  396. case MINMEA_SENTENCE_GGA: {
  397. struct minmea_sentence_gga frame;
  398. if(minmea_parse_gga(&frame, line)) {
  399. ctx->last_latitude = minmea_tocoord(&frame.latitude);
  400. ctx->last_longitude = minmea_tocoord(&frame.longitude);
  401. }
  402. } break;
  403. case MINMEA_SENTENCE_GLL: {
  404. struct minmea_sentence_gll frame;
  405. if(minmea_parse_gll(&frame, line)) {
  406. ctx->last_latitude = minmea_tocoord(&frame.latitude);
  407. ctx->last_longitude = minmea_tocoord(&frame.longitude);
  408. }
  409. } break;
  410. default:
  411. break;
  412. }
  413. }
  414. static void uart_cb_gps(UartIrqEvent ev, uint8_t data, void* context) {
  415. Context* ctx = (Context*)context;
  416. if(ev == UartIrqEventRXNE) {
  417. furi_stream_buffer_send(ctx->rx_stream_gps, &data, 1, 0);
  418. furi_thread_flags_set(furi_thread_get_id(ctx->thread_gps), WorkerEvtRxDone);
  419. }
  420. }
  421. static int32_t uart_worker_gps(void* context) {
  422. Context* ctx = (Context*)context;
  423. size_t rx_offset = 0;
  424. while(1) {
  425. uint32_t events =
  426. furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
  427. furi_check((events & FuriFlagError) == 0);
  428. if(events & WorkerEvtStop) {
  429. break;
  430. }
  431. if(events & WorkerEvtRxDone) {
  432. size_t len = 0;
  433. do {
  434. // receive serial bytes into rx_buf, starting at rx_offset from the start of the buffer
  435. // the maximum we can receive is RX_BUF_SIZE - 1 - rx_offset
  436. len = furi_stream_buffer_receive(
  437. ctx->rx_stream_gps,
  438. ctx->rx_buf_gps + rx_offset,
  439. RX_BUF_SIZE - 1 - rx_offset,
  440. 0);
  441. if(len > 0) {
  442. // increase rx_offset by the number of bytes received, and null-terminate rx_buf
  443. rx_offset += len;
  444. ctx->rx_buf_gps[rx_offset] = '\0';
  445. // look for strings ending in newlines, starting at the start of rx_buf
  446. char* line_current = (char*)ctx->rx_buf_gps;
  447. while(1) {
  448. // skip null characters
  449. while(*line_current == '\0' &&
  450. line_current < (char*)ctx->rx_buf_gps + rx_offset - 1) {
  451. line_current++;
  452. }
  453. // find the next newline
  454. char* newline = strchr(line_current, '\n');
  455. if(newline) // newline found
  456. {
  457. // put a null terminator in place of the newline, to delimit the line string
  458. *newline = '\0';
  459. // FURI_LOG_I(appname, "Received line: %s", line_current);
  460. gps_uart_parse_nmea(ctx, line_current);
  461. // move the cursor to the character after the newline
  462. line_current = newline + 1;
  463. } else // no more newlines found
  464. {
  465. if(line_current >
  466. (char*)ctx->rx_buf_gps) // at least one line was found
  467. {
  468. // clear parsed lines, and move any leftover bytes to the start of rx_buf
  469. rx_offset = 0;
  470. while(
  471. *line_current) // stop when the original rx_offset terminator is reached
  472. {
  473. ctx->rx_buf_gps[rx_offset++] = *(line_current++);
  474. }
  475. }
  476. break; // go back to receiving bytes from the serial stream
  477. }
  478. }
  479. }
  480. } while(len > 0);
  481. }
  482. }
  483. furi_hal_uart_set_irq_cb(UART_CH_GPS, NULL, NULL);
  484. furi_stream_buffer_free(ctx->rx_stream_gps);
  485. return 0;
  486. }
  487. int32_t wifisniffer_app(void* p) {
  488. UNUSED(p);
  489. // if(UART_CH_ESP == UART_CH_GPS) {
  490. // FURI_LOG_I(appname, "ESP and GPS uart can't be the same");
  491. // return -1;
  492. // }
  493. // turn off 5v, so it gets reset on startup
  494. if(furi_hal_power_is_otg_enabled()) {
  495. furi_hal_power_disable_otg();
  496. }
  497. // Enable 5v on startup
  498. uint8_t attempts = 0;
  499. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  500. furi_hal_power_enable_otg();
  501. furi_delay_ms(10);
  502. }
  503. furi_delay_ms(200);
  504. // alloc everything
  505. Context* ctx = malloc(sizeof(Context));
  506. ctx->queue = furi_message_queue_alloc(8, sizeof(Event));
  507. ctx->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  508. ctx->buffer = furi_string_alloc();
  509. ctx->buffer2 = furi_string_alloc();
  510. ctx->notifications = furi_record_open(RECORD_NOTIFICATION);
  511. ctx->access_points_count = 0;
  512. ctx->access_points_index = 0;
  513. ctx->pressedButton = false;
  514. //esp uart
  515. ctx->rx_stream_esp = furi_stream_buffer_alloc(RX_BUF_SIZE * 5, 1);
  516. ctx->thread_esp = furi_thread_alloc();
  517. furi_thread_set_name(ctx->thread_esp, "LLwifiSnifferUartWorkerESP");
  518. furi_thread_set_stack_size(ctx->thread_esp, 2048);
  519. furi_thread_set_context(ctx->thread_esp, ctx);
  520. furi_thread_set_callback(ctx->thread_esp, uart_worker_esp);
  521. furi_thread_start(ctx->thread_esp);
  522. if(UART_CH_ESP == FuriHalUartIdUSART1) {
  523. furi_hal_console_disable();
  524. } else if(UART_CH_ESP == FuriHalUartIdLPUART1) {
  525. furi_hal_uart_init(UART_CH_ESP, 115200);
  526. }
  527. furi_hal_uart_set_br(UART_CH_ESP, 115200);
  528. furi_hal_uart_set_irq_cb(UART_CH_ESP, uart_cb_esp, ctx);
  529. furi_hal_uart_tx(UART_CH_ESP, (uint8_t*)"XFW#WIFISNIFF=1\r\n", strlen("XFW#WIFISNIFF=1\r\n"));
  530. //end esp uart
  531. //gps uart
  532. if(UART_CH_ESP != UART_CH_GPS) {
  533. ctx->rx_stream_gps = furi_stream_buffer_alloc(RX_BUF_SIZE * 5, 1);
  534. ctx->thread_gps = furi_thread_alloc();
  535. furi_thread_set_name(ctx->thread_gps, "LLwifiSnifferUartWorkerGPS");
  536. furi_thread_set_stack_size(ctx->thread_gps, 2048);
  537. furi_thread_set_context(ctx->thread_gps, ctx);
  538. furi_thread_set_callback(ctx->thread_gps, uart_worker_gps);
  539. furi_thread_start(ctx->thread_gps);
  540. if(UART_CH_GPS == FuriHalUartIdUSART1) {
  541. furi_hal_console_disable();
  542. } else if(UART_CH_GPS == FuriHalUartIdLPUART1) {
  543. furi_hal_uart_init(UART_CH_GPS, 9600);
  544. }
  545. furi_hal_uart_set_br(UART_CH_GPS, 9600);
  546. furi_hal_uart_set_irq_cb(UART_CH_GPS, uart_cb_gps, ctx);
  547. }
  548. //end gps uart
  549. ViewPort* view_port = view_port_alloc();
  550. view_port_draw_callback_set(view_port, render_callback, ctx);
  551. view_port_input_callback_set(view_port, input_callback, ctx->queue);
  552. Gui* gui = furi_record_open(RECORD_GUI);
  553. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  554. FuriTimer* timer = furi_timer_alloc(tick_callback, FuriTimerTypePeriodic, ctx->queue);
  555. furi_timer_start(timer, 100);
  556. // application loop
  557. Event event;
  558. bool processing = true;
  559. do {
  560. if(furi_message_queue_get(ctx->queue, &event, FuriWaitForever) == FuriStatusOk) {
  561. furi_mutex_acquire(ctx->mutex, FuriWaitForever);
  562. switch(event.type) {
  563. case EventTypeKey:
  564. // applicatie verlaten
  565. if(event.input.type == InputTypeShort && event.input.key == InputKeyBack) {
  566. processing = false;
  567. } else if(event.input.type == InputTypeLong && event.input.key == InputKeyBack) {
  568. processing = false;
  569. } else if(event.input.type == InputTypeLong && event.input.key == InputKeyOk) {
  570. // remove accespoint
  571. if(ctx->access_points_count > 0) {
  572. for(int i = ctx->access_points_index; i < ctx->access_points_count - 1;
  573. i++) {
  574. ctx->access_points[i] = ctx->access_points[i + 1];
  575. }
  576. ctx->access_points_count--;
  577. if(ctx->access_points_index >= ctx->access_points_count) {
  578. ctx->access_points_index = ctx->access_points_count - 1;
  579. }
  580. }
  581. } else if(event.input.type == InputTypePress && event.input.key == InputKeyDown) {
  582. ctx->access_points_index--;
  583. if(ctx->access_points_index < 0) {
  584. ctx->access_points_index = ctx->access_points_count - 1;
  585. }
  586. ctx->active_access_point = ctx->access_points[ctx->access_points_index];
  587. } else if(event.input.type == InputTypePress && event.input.key == InputKeyUp) {
  588. ctx->access_points_index++;
  589. if(ctx->access_points_index >= ctx->access_points_count) {
  590. ctx->access_points_index = 0;
  591. }
  592. ctx->active_access_point = ctx->access_points[ctx->access_points_index];
  593. } else if(event.input.type == InputTypePress && event.input.key == InputKeyLeft) {
  594. } else if(event.input.type == InputTypePress && event.input.key == InputKeyRight) {
  595. }
  596. ctx->pressedButton = true;
  597. break;
  598. case EventTypeTick:
  599. // fix for the empty active access point when there was no interaction
  600. if(!ctx->pressedButton) {
  601. ctx->access_points_index = 0;
  602. ctx->active_access_point = ctx->access_points[ctx->access_points_index];
  603. }
  604. break;
  605. default:
  606. break;
  607. }
  608. view_port_update(view_port);
  609. } else {
  610. processing = false;
  611. }
  612. } while(processing);
  613. // save the data to the file
  614. Storage* storage = furi_record_open(RECORD_STORAGE);
  615. FuriHalRtcDateTime datetime;
  616. furi_hal_rtc_get_datetime(&datetime);
  617. FuriString* filename = furi_string_alloc();
  618. furi_string_printf(
  619. filename,
  620. "%d_%d_%d_%d_%d_%d.txt",
  621. datetime.year,
  622. datetime.month,
  623. datetime.day,
  624. datetime.hour,
  625. datetime.minute,
  626. datetime.second);
  627. FuriString* path = furi_string_alloc();
  628. furi_string_printf(path, "/ext/apps_data/llsniffer/%s", furi_string_get_cstr(filename));
  629. // open file
  630. ctx->file = storage_file_alloc(storage);
  631. if(!storage_common_exists(storage, EXT_PATH("apps_data/llsniffer"))) {
  632. storage_common_mkdir(storage, EXT_PATH("apps_data/llsniffer"));
  633. }
  634. if(!storage_file_open(ctx->file, furi_string_get_cstr(path), FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
  635. FURI_LOG_E(appname, "Failed to open file");
  636. }
  637. for(int i = 0; i < ctx->access_points_count; i++) {
  638. AccessPoint ap = ctx->access_points[i];
  639. furi_string_printf(
  640. ctx->buffer2,
  641. "%s,%s,%s,%d,%d,%d,%d,%d,%d,%d,%d,%f,%f\r\n",
  642. "Accesspoint",
  643. ap.ssid,
  644. ap.bssid,
  645. ap.rssi,
  646. ap.channel,
  647. ap.datetime.year,
  648. ap.datetime.month,
  649. ap.datetime.day,
  650. ap.datetime.hour,
  651. ap.datetime.minute,
  652. ap.datetime.second,
  653. (double)ap.latitude,
  654. (double)ap.longitude);
  655. if(!storage_file_write(
  656. ctx->file,
  657. furi_string_get_cstr(ctx->buffer2),
  658. strlen(furi_string_get_cstr(ctx->buffer2)))) {
  659. FURI_LOG_E(appname, "Failed to write AP to file");
  660. }
  661. }
  662. // free everything
  663. furi_record_close(RECORD_NOTIFICATION);
  664. furi_timer_free(timer);
  665. view_port_enabled_set(view_port, false);
  666. gui_remove_view_port(gui, view_port);
  667. view_port_free(view_port);
  668. furi_record_close(RECORD_GUI);
  669. furi_message_queue_free(ctx->queue);
  670. furi_mutex_free(ctx->mutex);
  671. furi_thread_flags_set(furi_thread_get_id(ctx->thread_esp), WorkerEvtStop);
  672. furi_thread_join(ctx->thread_esp);
  673. furi_thread_free(ctx->thread_esp);
  674. if(UART_CH_ESP != UART_CH_GPS) {
  675. furi_thread_flags_set(furi_thread_get_id(ctx->thread_gps), WorkerEvtStop);
  676. furi_thread_join(ctx->thread_gps);
  677. furi_thread_free(ctx->thread_gps);
  678. }
  679. storage_file_close(ctx->file);
  680. storage_file_free(ctx->file);
  681. furi_record_close(RECORD_STORAGE);
  682. free(ctx);
  683. furi_hal_light_set(LightBlue, 0);
  684. furi_hal_light_set(LightGreen, 0);
  685. if(UART_CH_ESP == FuriHalUartIdLPUART1) {
  686. furi_hal_uart_deinit(UART_CH_ESP);
  687. } else if(UART_CH_ESP == FuriHalUartIdUSART1) {
  688. furi_hal_console_enable();
  689. }
  690. if(UART_CH_GPS == FuriHalUartIdLPUART1) {
  691. furi_hal_uart_deinit(UART_CH_GPS);
  692. } else if(UART_CH_GPS == FuriHalUartIdUSART1) {
  693. furi_hal_console_enable();
  694. }
  695. if(furi_hal_power_is_otg_enabled()) {
  696. furi_hal_power_disable_otg();
  697. }
  698. return 0;
  699. }