Page 1 of 1

Passing in additional parameters to Event Handler functions

Posted: Sun Jun 19, 2022 12:57 am
by Chucky101
I am trying to get access to a class member variable to expose whether the class is connected to Wifi or not.

I am currently registering the handler as shown below with the assumption that "this" will get passed into the handler in place of "event_data".
  1.  
  2. esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, WifiEventHandler, this);
  3.  
When when this code runs it crashed and causes a LoadProhibited error.
  1.  
  2. void WifiModule::WifiEventHandler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
  3. {
  4.     auto ThisWifiModuleReference = (WifiModule*)(event_data);
  5.  
  6.     std::cout << ThisWifiModuleReference->m_sSSID << std::endl;
  7.  
  8.     switch (event_id)
  9.     {
  10.     case WIFI_EVENT_STA_START:
  11.         printf("WiFi connecting ... \n");
  12.         //ThisWifiModuleReference->m_bWifiConnected = false;
  13.         break;
  14.     case WIFI_EVENT_STA_CONNECTED:
  15.         printf("WiFi connected ... \n");
  16.         //ThisWifiModuleReference->m_bWifiConnected = false;
  17.         break;
  18.     case WIFI_EVENT_STA_DISCONNECTED:
  19.         printf("WiFi lost connection ... \n");
  20.         //ThisWifiModuleReference->m_bWifiConnected = false;
  21.         break;
  22.     case IP_EVENT_STA_GOT_IP:
  23.         printf("WiFi got IP ... \n\n");
  24.         //ThisWifiModuleReference->m_bWifiConnected = true;
  25.         break;
  26.     default:
  27.         break;
  28.     }
  29. }
  30.  
Still quite new to embedded programming so I would really appreciate an explanation as to why this would happen/how to fix it.

Re: Passing in additional parameters to Even Handler functions

Posted: Tue Jun 21, 2022 4:30 pm
by boarchuz
Close, but 'event_handler_arg' is your "this" pointer in this case, not 'event_data'.