Yurik72 / ESPHap

ESP32/ESP8266 Arduino library for native Apple Homekit Accessory Protocol (HAP)
MIT License
264 stars 60 forks source link

Pair Setup Step 3/3 : InstructionFetchError OR LoadProhibited when having >24 characteristics loaded #140

Closed simkard69 closed 9 months ago

simkard69 commented 10 months ago

I'm trying to create a module to drive Somfy RTS window shades.

Initializing 6 of them seems to work fine, keeping in mind that each window covering (understandging "shade") has 4 charecteristics (name, position_state, current_position and target_position). So that makes 24 characteristics in total. There are no change if I initialize them as accessory or services.

My initialization part (for debugging purposes of course) looks like this :

Serial.println("[HAP]\tAdditional accessories");
  // Define additional WINDOW COVERING accessories (Controls : CURRENT_POSITION, TARGET_POSITION, POSITION_STATE) that can be located in different rooms
  Serial.print("[HAP]\tFree heap 2 : "); Serial.println(esp_get_free_heap_size());  // Print current size of free heap memory
  if (SomfyRTSData.RTS2_Enabled == 1){        HomeKit_Svc_SomfyRTS2   = hap_add_windowcovering_service("SomfyRTS2",   HAP_SomfyRTS2_update, 0);       }
  Serial.print("[HAP]\tFree heap 3 : "); Serial.println(esp_get_free_heap_size());  // Print current size of free heap memory
  if (SomfyRTSData.RTS3_Enabled == 1){        HomeKit_Svc_SomfyRTS3   = hap_add_windowcovering_service("SomfyRTS3",   HAP_SomfyRTS3_update, 0);       }
  Serial.print("[HAP]\tFree heap 4 : "); Serial.println(esp_get_free_heap_size());  // Print current size of free heap memory
  if (SomfyRTSData.RTS4_Enabled == 1){        HomeKit_Svc_SomfyRTS4   = hap_add_windowcovering_service("SomfyRTS4",   HAP_SomfyRTS4_update, 0);       }
  Serial.print("[HAP]\tFree heap 5 : "); Serial.println(esp_get_free_heap_size());  // Print current size of free heap memory
  if (SomfyRTSData.RTS5_Enabled == 1){        HomeKit_Svc_SomfyRTS5   = hap_add_windowcovering_service("SomfyRTS5",   HAP_SomfyRTS5_update, 0);       }
  Serial.print("[HAP]\tFree heap 6 : "); Serial.println(esp_get_free_heap_size());  // Print current size of free heap memory
  if (SomfyRTSData.RTS6_Enabled == 1){        HomeKit_Svc_SomfyRTS6   = hap_add_windowcovering_service("SomfyRTS6",   HAP_SomfyRTS6_update, 0);       }
  Serial.print("[HAP]\tFree heap 7 : "); Serial.println(esp_get_free_heap_size());  // Print current size of free heap memory
  if (SomfyRTSData.RTS7_Enabled == 1){        HomeKit_Svc_SomfyRTS7   = hap_add_windowcovering_service("SomfyRTS7",   HAP_SomfyRTS7_update, 0);       }
  Serial.print("[HAP]\tFree heap 8 : "); Serial.println(esp_get_free_heap_size());  // Print current size of free heap memory
  if (SomfyRTSData.RTS8_Enabled == 1){        HomeKit_Svc_SomfyRTS8   = hap_add_windowcovering_service("SomfyRTS8",   HAP_SomfyRTS8_update, 0);       }
  Serial.print("[HAP]\tFree heap 9 : "); Serial.println(esp_get_free_heap_size());  // Print current size of free heap memory
  if (SomfyRTSData.RTS9_Enabled == 1){        HomeKit_Svc_SomfyRTS9   = hap_add_windowcovering_service("SomfyRTS9",   HAP_SomfyRTS9_update, 0);       }
  Serial.print("[HAP]\tFree heap 10: "); Serial.println(esp_get_free_heap_size());  // Print current size of free heap memory
  if (SomfyRTSData.RTS10_Enabled == 1){       HomeKit_Svc_SomfyRTS10  = hap_add_windowcovering_service("SomfyRTS10",  HAP_SomfyRTS10_update, 0);      }

And it outputs something like that :

08:39:30.998 -> [HAP]   Free heap 4 : 184596
08:39:30.998 -> >>> Home Integration: hap_services added chararacteristic  0: 00000023-0000-1000-8000-0026BB765291 
08:39:30.998 -> >>> Home Integration: hap_services added chararacteristic  1: 0000006D-0000-1000-8000-0026BB765291 
08:39:31.030 -> >>> Home Integration: hap_services added chararacteristic  2: 0000007C-0000-1000-8000-0026BB765291 
08:39:31.030 -> >>> Home Integration: hap_services added chararacteristic  3: 00000072-0000-1000-8000-0026BB765291 
08:39:31.030 -> [HAP]   Free heap 5 : 183720
08:39:31.030 -> >>> Home Integration: hap_services added chararacteristic  0: 00000023-0000-1000-8000-0026BB765291 
08:39:31.063 -> >>> Home Integration: hap_services added chararacteristic  1: 0000006D-0000-1000-8000-0026BB765291 
08:39:31.063 -> >>> Home Integration: hap_services added chararacteristic  2: 0000007C-0000-1000-8000-0026BB765291 
08:39:31.063 -> >>> Home Integration: hap_services added chararacteristic  3: 00000072-0000-1000-8000-0026BB765291 
08:39:31.063 -> [HAP]   Free heap 6 : 182844
08:39:31.063 -> >>> Home Integration: hap_services added chararacteristic  0: 00000023-0000-1000-8000-0026BB765291 
08:39:31.094 -> >>> Home Integration: hap_services added chararacteristic  1: 0000006D-0000-1000-8000-0026BB765291 
08:39:31.094 -> >>> Home Integration: hap_services added chararacteristic  2: 0000007C-0000-1000-8000-0026BB765291 
08:39:31.094 -> >>> Home Integration: hap_services added chararacteristic  3: 00000072-0000-1000-8000-0026BB765291 
08:39:31.126 -> [HAP]   Free heap 7 : 181968
08:39:31.126 -> [HAP]   Free heap 8 : 181092
08:39:31.126 -> [HAP]   Free heap 9 : 181092
08:39:31.126 -> [HAP]   Free heap 10: 181092
08:39:31.126 -> [HAP]   Additional values (MIN, MAX, etc ...)
08:39:31.126 -> [HAP]   Start-up values
08:39:31.126 -> Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
08:39:31.126 -> 
08:39:31.126 -> Core  1 register dump:
08:39:31.126 -> PC      : 0x400e1463  PS      : 0x00060d30  A0      : 0x800e2950  A1      : 0x3ffb2780  
08:39:31.158 -> A2      : 0x00000000  A3      : 0x3f400201  A4      : 0x3ffd2028  A5      : 0x3ffb2580  
08:39:31.158 -> A6      : 0x3ffb2579  A7      : 0x00000002  A8      : 0x3ffd1fb4  A9      : 0x00000000  
08:39:31.158 -> A10     : 0x00000000  A11     : 0x00000002  A12     : 0x00000015  A13     : 0x0000ff00  
08:39:31.158 -> A14     : 0x00ff0000  A15     : 0xff000000  SAR     : 0x00000009  EXCCAUSE: 0x0000001c  
08:39:31.190 -> EXCVADDR: 0x00000014  LBEG    : 0x40089ced  LEND    : 0x40089cfd  LCOUNT  : 0xffffffda  
08:39:31.190 -> 
08:39:31.190 -> 
08:39:31.190 -> Backtrace:0x400e1460:0x3ffb27800x400e294d:0x3ffb27a0 0x400d7572:0x3ffb27c0 0x400f7c22:0x3ffb2820 
08:39:31.190 -> 
08:39:31.190 -> 
08:39:31.190 -> 
08:39:31.190 -> 
08:39:31.190 -> ELF file SHA256: 0000000000000000
08:39:31.190 -> 
08:39:31.190 -> Rebooting...

It doesn't seem like it is a free heap problem, so I'm wondering what could be the culprit here ?

Any chance someone has an idea of what is going on ?

Thanks

simkard69 commented 10 months ago

After a little bit of "trial & error", I found out that when I try to initialize the 7th accessory/service it fails. Looked in homeintegration.c file and found these lines :

#define MAX_HAP_SERVICES 7
#define MAX_HAP_ACCESSORIES 7

I've just made some change, replaced these 2 lines with these ones :

#define MAX_HAP_SERVICES 15
#define MAX_HAP_ACCESSORIES 15

So far so good while initializing a 7th accessory or service. Will do more tests later today. Anyway, can that be extended without impacting any library/system stability ?

simkard69 commented 10 months ago

Ok, so basically, on my side, I've tried :

Something I noticed after some new trial & error : when initializing 7 accessories (including the main/base) works fine. If I try to initialize 8 accessories, HomeKit on iOS device immediately considers the main accessory as a Hub/HomeBridge. Is that something hardcoded in the library ? Is this a known behavior of HomeKit now ?

@Yurik72 : Can you please help me on that one (even if I know that you're not that much active on this library unfortunately). Thanks

simkard69 commented 9 months ago

Solved modifying the following lines in "homeintegration.c" file

FROM define MAX_HAP_SERVICES 7 define MAX_HAP_ACCESSORIES 7

TO define MAX_HAP_SERVICES 15 define MAX_HAP_ACCESSORIES 15

NOTE : From what I've read, ESP32 can handle a maximum of 40 accessories ... but I have some doubts. Keeping in mind that starting as 8 accessories per same HomeKit device, it is considered as a Hub/HomeBridge and the first accessory (main/base) cannot be controlled and there are some connection drops which avoids updating values at first try. >>> I'm trying to find a solution in this other ISSUE : #141