@@ -98,26 +98,19 @@ bool homekit_storage_set_magic() {
98
98
99
99
#ifdef HOMEKIT_DEBUG
100
100
static 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 ) {
102
102
if (memcmp (data -> magic , hap_magic , sizeof (hap_magic )) || !data -> active ) return ;
103
103
104
104
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) " : "" );
106
106
for (int i = 0 ; i < sizeof (data -> device_id ); i ++ ) {
107
107
t += sprintf (t , "%c" , data -> device_id [i ] > 31 && data -> device_id [i ] < 128 ? data -> device_id [i ] : '.' );
108
108
}
109
109
110
110
INFO ("%s" , buf );
111
111
}
112
-
113
- void dump_storage ()
112
+ static void _dump_storage (byte * __buf )
114
113
{
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
- }
121
114
char * t = buf ;
122
115
t += sprintf (t , "dump:" );
123
116
for (int i = 0 ; i < (PAIRINGS_OFFSET + (MAX_PAIRINGS * sizeof (pairing_data_t ))); i ++ ) {
@@ -130,6 +123,18 @@ void dump_storage()
130
123
t += sprintf (t , "%02x " , __buf [i ]);
131
124
}
132
125
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 );
133
138
134
139
free (__buf );
135
140
}
@@ -157,7 +162,7 @@ int homekit_storage_init(int purge) {
157
162
int paired = 0 ;
158
163
for (int i = 0 ; i < MAX_PAIRINGS ; i ++ ) {
159
164
spiflash_read (PAIRINGS_ADDR + (sizeof (data ) * i ), (uint32_t * ) & data , sizeof (data ));
160
- dump_pairing_data (& data );
165
+ dump_pairing_data (i , & data );
161
166
if (!memcmp (data .magic , hap_magic , sizeof (hap_magic )) && data .active ) {
162
167
paired ++ ;
163
168
}
@@ -295,11 +300,11 @@ static int compact_data() {
295
300
296
301
int next_pairing_idx = 0 ;
297
302
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 ) {
300
305
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 ));
303
308
}
304
309
next_pairing_idx ++ ;
305
310
}
@@ -316,12 +321,22 @@ static int compact_data() {
316
321
free (storage );
317
322
return -2 ;
318
323
}
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" );
322
326
free (storage );
323
327
return -1 ;
324
328
}
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
+ }
325
340
326
341
free (storage );
327
342
0 commit comments