22#include <furi_hal.h>
33#include <gui/gui.h>
44#include <input/input.h>
5+ #include <notification/notification_messages.h>
56#include <stdlib.h>
6- #include <furi_hal_gpio.h>
7- #include <furi_hal_spi.h>
8- #include <furi_hal_interrupt.h>
9- #include <furi_hal_resources.h>
7+
108#include <nrf24.h>
119#include <toolbox/stream/file_stream.h>
1210
@@ -65,9 +63,12 @@ static int get_addr_index(uint8_t* addr, uint8_t addr_size) {
6563 return -1 ;
6664}
6765
68- static uint32_t get_addr_count (uint8_t * addr , uint8_t addr_size ) {
66+ /*
67+ static uint32_t get_addr_count(uint8_t* addr, uint8_t addr_size)
68+ {
6969 return counts[get_addr_index(addr, addr_size)];
7070}
71+ */
7172
7273static uint8_t get_lowest_idx () {
7374 uint32_t lowest = 10000 ;
@@ -153,7 +154,7 @@ static void hexlify(uint8_t* in, uint8_t size, char* out) {
153154 snprintf (out + strlen (out ), sizeof (out + strlen (out )), "%02X" , in [i ]);
154155}
155156
156- static bool save_addr_to_file (Storage * storage , uint8_t * data , uint8_t size ) {
157+ static bool save_addr_to_file (Storage * storage , uint8_t * data , uint8_t size , NotificationApp * notification ) {
157158 size_t file_size = 0 ;
158159 uint8_t linesize = 0 ;
159160 char filepath [42 ] = {0 };
@@ -174,7 +175,7 @@ static bool save_addr_to_file(Storage* storage, uint8_t* data, uint8_t size) {
174175 stream_seek (stream , 0 , StreamOffsetFromStart );
175176
176177 // check if address already exists in file
177- if (file_stream_open (stream , filepath , FSAM_READ , FSOM_OPEN_EXISTING )) {
178+ if (file_stream_open (stream , filepath , FSAM_READ_WRITE , FSOM_OPEN_APPEND )) {
178179 bool found = false;
179180 file_size = stream_size (stream );
180181 stream_seek (stream , 0 , StreamOffsetFromStart );
@@ -194,20 +195,27 @@ static bool save_addr_to_file(Storage* storage, uint8_t* data, uint8_t size) {
194195 }
195196 free (file_contents );
196197 }
197- stream_free (stream );
198- if (found ) return false;
199198
200- stream = file_stream_alloc (storage );
201- stream_seek (stream , 0 , StreamOffsetFromStart );
199+ if (found ) {
200+ FURI_LOG_I (TAG , "Address exists in file. Ending save process." );
201+ stream_free (stream );
202+ return false;
203+ } else {
204+ if (stream_write (stream , (uint8_t * )addrline , linesize ) != linesize ) {
205+ FURI_LOG_I (TAG , "Failed to write bytes to file stream." );
206+ stream_free (stream );
207+ return false;
208+ }
209+ }
210+ } else {
211+ FURI_LOG_I (TAG , "Cannot open file \"%s\"" , filepath );
212+ stream_free (stream );
213+ return false;
202214 }
203215
204- // save address to file
205- if (!file_stream_open (stream , filepath , FSAM_WRITE , FSOM_OPEN_APPEND ))
206- FURI_LOG_I (TAG , "Cannot open file \"%s\"" , filepath );
207- if (stream_write (stream , (uint8_t * )addrline , linesize ) != linesize )
208- FURI_LOG_I (TAG , "failed to write bytes to file stream" );
216+ notification_message (notification , & sequence_success );
209217
210- FURI_LOG_I (TAG , "save successful" );
218+ FURI_LOG_I (TAG , "Save successful! " );
211219 stream_free (stream );
212220 return true;
213221}
@@ -241,7 +249,7 @@ void alt_address(uint8_t* addr, uint8_t* altaddr) {
241249 for (int i = 0 ; i < 5 ; i ++ ) altaddr [i ] = tmpaddr [4 - i ];
242250}
243251
244- static void wrap_up (Storage * storage ) {
252+ static void wrap_up (Storage * storage , NotificationApp * notification ) {
245253 uint8_t ch ;
246254 uint8_t addr [5 ];
247255 uint8_t altaddr [5 ];
@@ -274,7 +282,7 @@ static void wrap_up(Storage* storage) {
274282
275283 if (ch <= LOGITECH_MAX_CHANNEL ) {
276284 hexlify (addr , 5 , top_address );
277- save_addr_to_file (storage , addr , 5 );
285+ save_addr_to_file (storage , addr , 5 , notification );
278286 break ;
279287 }
280288 }
@@ -287,6 +295,7 @@ static void start_sniffing() {
287295}
288296
289297int32_t nrfsniff_app (void * p ) {
298+ UNUSED (p );
290299 uint8_t address [5 ] = {0 };
291300 uint32_t start = 0 ;
292301 hexlify (address , 5 , top_address );
@@ -307,10 +316,13 @@ int32_t nrfsniff_app(void* p) {
307316 view_port_input_callback_set (view_port , input_callback , event_queue );
308317
309318 // Open GUI and register view_port
310- Gui * gui = furi_record_open ("gui" );
319+ Gui * gui = furi_record_open (RECORD_GUI );
311320 gui_add_view_port (gui , view_port , GuiLayerFullscreen );
312321
313- Storage * storage = furi_record_open ("storage" );
322+ NotificationApp * notification = furi_record_open (RECORD_NOTIFICATION );
323+ furi_hal_power_suppress_charge_enter ();
324+
325+ Storage * storage = furi_record_open (RECORD_STORAGE );
314326 storage_common_mkdir (storage , NRFSNIFF_APP_PATH_FOLDER );
315327
316328 PluginEvent event ;
@@ -360,7 +372,7 @@ int32_t nrfsniff_app(void* p) {
360372 start_sniffing ();
361373 start = furi_get_tick ();
362374 } else
363- wrap_up (storage );
375+ wrap_up (storage , notification );
364376 break ;
365377 case InputKeyBack :
366378 if (event .input .type == InputTypeLong ) processing = false;
@@ -388,7 +400,7 @@ int32_t nrfsniff_app(void* p) {
388400 }
389401
390402 if (furi_get_tick () - start >= SAMPLE_TIME ) {
391- wrap_up (storage );
403+ wrap_up (storage , notification );
392404 target_channel ++ ;
393405 if (target_channel > LOGITECH_MAX_CHANNEL ) target_channel = 2 ;
394406
@@ -404,8 +416,10 @@ int32_t nrfsniff_app(void* p) {
404416 furi_hal_spi_release (nrf24_HANDLE );
405417 view_port_enabled_set (view_port , false);
406418 gui_remove_view_port (gui , view_port );
407- furi_record_close ("gui" );
408- furi_record_close ("storage" );
419+ furi_hal_power_suppress_charge_exit ();
420+ furi_record_close (RECORD_GUI );
421+ furi_record_close (RECORD_NOTIFICATION );
422+ furi_record_close (RECORD_STORAGE );
409423 view_port_free (view_port );
410424 furi_message_queue_free (event_queue );
411425
0 commit comments