@@ -98,26 +98,19 @@ bool homekit_storage_set_magic() {
9898
9999#ifdef HOMEKIT_DEBUG
100100static char buf [128 ];
101- static void dump_pairing_data (pairing_data_t * data ) {
101+ static void dump_pairing_data (int index , pairing_data_t * data ) {
102102 if (memcmp (data -> magic , hap_magic , sizeof (hap_magic )) || !data -> active ) return ;
103103
104104 char * t = buf ;
105- t += sprintf (t , "device_id: %s" , data -> permissions & pairing_permissions_admin ? "(admin) " : "" );
105+ t += sprintf (t , "device_id (%d) : %s" , index , data -> permissions & pairing_permissions_admin ? "(admin) " : "" );
106106 for (int i = 0 ; i < sizeof (data -> device_id ); i ++ ) {
107107 t += sprintf (t , "%c" , data -> device_id [i ] > 31 && data -> device_id [i ] < 128 ? data -> device_id [i ] : '.' );
108108 }
109109
110110 INFO ("%s" , buf );
111111}
112-
113- void dump_storage ()
112+ static void _dump_storage (byte * __buf )
114113{
115- byte * __buf = malloc (PAIRINGS_OFFSET + (MAX_PAIRINGS * sizeof (pairing_data_t )));
116- if (!spiflash_read (STORAGE_BASE_ADDR , (uint32_t * ) __buf , PAIRINGS_OFFSET + (MAX_PAIRINGS * sizeof (pairing_data_t )))) {
117- ERROR ("Failed to read the storage sector" );
118- free (__buf );
119- return ;
120- }
121114 char * t = buf ;
122115 t += sprintf (t , "dump:" );
123116 for (int i = 0 ; i < (PAIRINGS_OFFSET + (MAX_PAIRINGS * sizeof (pairing_data_t ))); i ++ ) {
@@ -130,6 +123,18 @@ void dump_storage()
130123 t += sprintf (t , "%02x " , __buf [i ]);
131124 }
132125 INFO ("%s" , buf );
126+ }
127+ void dump_storage ()
128+ {
129+ byte * __buf = malloc (PAIRINGS_OFFSET + (MAX_PAIRINGS * sizeof (pairing_data_t )));
130+
131+ if (!spiflash_read (STORAGE_BASE_ADDR , (uint32_t * ) __buf , PAIRINGS_OFFSET + (MAX_PAIRINGS * sizeof (pairing_data_t )))) {
132+ ERROR ("Failed to read the storage sector" );
133+ free (__buf );
134+ return ;
135+ }
136+
137+ _dump_storage (__buf );
133138
134139 free (__buf );
135140}
@@ -157,7 +162,7 @@ int homekit_storage_init(int purge) {
157162 int paired = 0 ;
158163 for (int i = 0 ; i < MAX_PAIRINGS ; i ++ ) {
159164 spiflash_read (PAIRINGS_ADDR + (sizeof (data ) * i ), (uint32_t * ) & data , sizeof (data ));
160- dump_pairing_data (& data );
165+ dump_pairing_data (i , & data );
161166 if (!memcmp (data .magic , hap_magic , sizeof (hap_magic )) && data .active ) {
162167 paired ++ ;
163168 }
@@ -295,11 +300,11 @@ static int compact_data() {
295300
296301 int next_pairing_idx = 0 ;
297302 for (int i = 0 ; i < MAX_PAIRINGS ; i ++ ) {
298- pairing_data_t * data = (pairing_data_t * ) & storage [PAIRINGS_OFFSET + (i * sizeof (pairing_data_t ))];
299- if (!memcmp (data -> magic , hap_magic , sizeof (hap_magic )) && data -> active ) {
303+ pairing_data_t * src = (pairing_data_t * ) & storage [PAIRINGS_OFFSET + (i * sizeof (pairing_data_t ))];
304+ if (!memcmp (src -> magic , hap_magic , sizeof (hap_magic )) && src -> active ) {
300305 if (i != next_pairing_idx ) {
301- memcpy ( & storage [PAIRINGS_ADDR + (sizeof ( pairing_data_t ) * next_pairing_idx )], data , sizeof (* data )) ;
302- memset ( & storage [ PAIRINGS_ADDR + ( sizeof ( pairing_data_t ) * i )], 0 , sizeof (* data ));
306+ pairing_data_t * dest = ( pairing_data_t * ) & storage [PAIRINGS_OFFSET + (next_pairing_idx * sizeof (pairing_data_t ))] ;
307+ memcpy ( dest , src , sizeof (pairing_data_t ));
303308 }
304309 next_pairing_idx ++ ;
305310 }
@@ -316,12 +321,22 @@ static int compact_data() {
316321 free (storage );
317322 return -2 ;
318323 }
319-
320- if (!spiflash_write (STORAGE_BASE_ADDR , (uint32_t * ) storage , PAIRINGS_OFFSET + (next_pairing_idx * sizeof (pairing_data_t )))) {
321- ERROR ("Failed to compact HomeKit storage: error writing compacted storage" );
324+ if (!spiflash_write (STORAGE_BASE_ADDR , (uint32_t * ) storage , PAIRINGS_OFFSET )) {
325+ ERROR ("Failed to compact HomeKit storage: error writing compacted storage header" );
322326 free (storage );
323327 return -1 ;
324328 }
329+ for (int i = 0 ; i < next_pairing_idx ; i ++ ) {
330+ int offset = PAIRINGS_OFFSET + (i * sizeof (pairing_data_t ));
331+ pairing_data_t * data = (pairing_data_t * ) & storage [offset ];
332+ if (!memcmp (data -> magic , hap_magic , sizeof (hap_magic )) && data -> active ) {
333+ if (!spiflash_write (STORAGE_BASE_ADDR + offset , (uint32_t * ) & storage [offset ], sizeof (pairing_data_t ) - 4 )) {
334+ ERROR ("Failed to compact HomeKit storage: error writing compacted storage" );
335+ free (storage );
336+ return -1 ;
337+ }
338+ } else break ;
339+ }
325340
326341 free (storage );
327342
0 commit comments