diff --git a/cpp_utils/BLEAdvertising.cpp b/cpp_utils/BLEAdvertising.cpp index 230d77cb..480d6d4c 100644 --- a/cpp_utils/BLEAdvertising.cpp +++ b/cpp_utils/BLEAdvertising.cpp @@ -480,15 +480,15 @@ void BLEAdvertising::handleGAPEvent( switch(event) { case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: { - // m_semaphoreSetAdv.give(); + // m_semaphoreSetAdv.giveFromISR(); break; } case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT: { - // m_semaphoreSetAdv.give(); + // m_semaphoreSetAdv.giveFromISR(); break; } case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: { - // m_semaphoreSetAdv.give(); + // m_semaphoreSetAdv.giveFromISR(); break; } case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: { diff --git a/cpp_utils/BLECharacteristic.cpp b/cpp_utils/BLECharacteristic.cpp index 2868ee26..db28985b 100644 --- a/cpp_utils/BLECharacteristic.cpp +++ b/cpp_utils/BLECharacteristic.cpp @@ -261,7 +261,7 @@ void BLECharacteristic::handleGATTServerEvent( // pDescriptor->executeCreate(this); // pDescriptor = m_descriptorMap.getNext(); // } // End while - m_semaphoreCreateEvt.give(); + m_semaphoreCreateEvt.giveFromISR(); } break; } // ESP_GATTS_ADD_CHAR_EVT @@ -440,7 +440,7 @@ void BLECharacteristic::handleGATTServerEvent( case ESP_GATTS_CONF_EVT: { // ESP_LOGD(LOG_TAG, "m_handle = %d, conf->handle = %d", m_handle, param->conf.handle); if(param->conf.conn_id == getService()->getServer()->getConnId()) // && param->conf.handle == m_handle) // bug in esp-idf and not implemented in arduino yet - m_semaphoreConfEvt.give(param->conf.status); + m_semaphoreConfEvt.giveFromISR(); break; } @@ -449,7 +449,7 @@ void BLECharacteristic::handleGATTServerEvent( } case ESP_GATTS_DISCONNECT_EVT: { - m_semaphoreConfEvt.give(); + m_semaphoreConfEvt.giveFromISR(); break; } diff --git a/cpp_utils/BLEClient.cpp b/cpp_utils/BLEClient.cpp index d6e75988..effb303f 100644 --- a/cpp_utils/BLEClient.cpp +++ b/cpp_utils/BLEClient.cpp @@ -193,7 +193,7 @@ void BLEClient::gattClientEventHandler( ESP_LOGE(__func__, "disconnect event, reason: %d, connId: %d, my connId: %d, my IF: %d, gattc_if: %d", (int)evtParam->disconnect.reason, evtParam->disconnect.conn_id, getConnId(), getGattcIf(), gattc_if); if(m_gattc_if != gattc_if) break; - m_semaphoreOpenEvt.give(evtParam->disconnect.reason); + m_semaphoreOpenEvt.giveFromISR(evtParam->disconnect.reason); if(!m_isConnected) break; // If we receive a disconnect event, set the class flag that indicates that we are @@ -225,7 +225,7 @@ void BLEClient::gattClientEventHandler( m_isConnected = true; // Flag us as connected. m_mtu = evtParam->open.mtu; } - m_semaphoreOpenEvt.give(evtParam->open.status); + m_semaphoreOpenEvt.giveFromISR(evtParam->open.status); break; } // ESP_GATTC_OPEN_EVT @@ -241,7 +241,7 @@ void BLEClient::gattClientEventHandler( if(m_appId == evtParam->reg.app_id){ ESP_LOGI(__func__, "register app id: %d, %d, gattc_if: %d", m_appId, evtParam->reg.app_id, gattc_if); m_gattc_if = gattc_if; - m_semaphoreRegEvt.give(); + m_semaphoreRegEvt.giveFromISR(); } break; } // ESP_GATTC_REG_EVT @@ -294,7 +294,7 @@ void BLEClient::gattClientEventHandler( // ESP_LOGI(LOG_TAG, "unknown service source"); // } #endif - m_semaphoreSearchCmplEvt.give(evtParam->search_cmpl.status); + m_semaphoreSearchCmplEvt.giveFromISR(evtParam->search_cmpl.status); break; } // ESP_GATTC_SEARCH_CMPL_EVT @@ -493,7 +493,7 @@ void BLEClient::handleGAPEvent( // - esp_bd_addr_t remote_addr // case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT: { - m_semaphoreRssiCmplEvt.give((uint32_t) param->read_rssi_cmpl.rssi); + m_semaphoreRssiCmplEvt.giveFromISR((uint32_t) param->read_rssi_cmpl.rssi); break; } // ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT diff --git a/cpp_utils/BLEDescriptor.cpp b/cpp_utils/BLEDescriptor.cpp index ba5753de..b13e765e 100644 --- a/cpp_utils/BLEDescriptor.cpp +++ b/cpp_utils/BLEDescriptor.cpp @@ -151,7 +151,7 @@ void BLEDescriptor::handleGATTServerEvent( m_pCharacteristic->getService()->getHandle() == param->add_char_descr.service_handle && m_pCharacteristic == m_pCharacteristic->getService()->getLastCreatedCharacteristic()) { setHandle(param->add_char_descr.attr_handle); - m_semaphoreCreateEvt.give(); + m_semaphoreCreateEvt.giveFromISR(); } break; } // ESP_GATTS_ADD_CHAR_DESCR_EVT diff --git a/cpp_utils/BLERemoteCharacteristic.cpp b/cpp_utils/BLERemoteCharacteristic.cpp index ef16da7b..28788b77 100644 --- a/cpp_utils/BLERemoteCharacteristic.cpp +++ b/cpp_utils/BLERemoteCharacteristic.cpp @@ -197,7 +197,7 @@ void BLERemoteCharacteristic::gattClientEventHandler(esp_gattc_cb_event_t event, m_value = ""; } - m_semaphoreReadCharEvt.give(); + m_semaphoreReadCharEvt.giveFromISR(); break; } // ESP_GATTC_READ_CHAR_EVT @@ -211,7 +211,7 @@ void BLERemoteCharacteristic::gattClientEventHandler(esp_gattc_cb_event_t event, if (evtParam->reg_for_notify.handle != getHandle()) break; // We have processed the notify registration and can unlock the semaphore. - m_semaphoreRegForNotifyEvt.give(); + m_semaphoreRegForNotifyEvt.giveFromISR(); break; } // ESP_GATTC_REG_FOR_NOTIFY_EVT @@ -223,7 +223,7 @@ void BLERemoteCharacteristic::gattClientEventHandler(esp_gattc_cb_event_t event, case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: { if (evtParam->unreg_for_notify.handle != getHandle()) break; // We have processed the notify un-registration and can unlock the semaphore. - m_semaphoreRegForNotifyEvt.give(); + m_semaphoreRegForNotifyEvt.giveFromISR(); break; } // ESP_GATTC_UNREG_FOR_NOTIFY_EVT: @@ -239,7 +239,7 @@ void BLERemoteCharacteristic::gattClientEventHandler(esp_gattc_cb_event_t event, // There is nothing further we need to do here. This is merely an indication // that the write has completed and we can unlock the caller. - m_semaphoreWriteCharEvt.give(); + m_semaphoreWriteCharEvt.giveFromISR(); break; } // ESP_GATTC_WRITE_CHAR_EVT diff --git a/cpp_utils/BLEScan.cpp b/cpp_utils/BLEScan.cpp index a96e6df6..173e1f99 100644 --- a/cpp_utils/BLEScan.cpp +++ b/cpp_utils/BLEScan.cpp @@ -77,7 +77,7 @@ void BLEScan::handleGAPEvent( case ESP_GAP_SEARCH_INQ_CMPL_EVT: { ESP_LOGW(LOG_TAG, "ESP_GAP_SEARCH_INQ_CMPL_EVT"); m_stopped = true; - m_semaphoreScanEnd.give(); + m_semaphoreScanEnd.giveFromISR(); if (m_scanCompleteCB != nullptr) { m_scanCompleteCB(m_scanResults); } diff --git a/cpp_utils/BLEServer.cpp b/cpp_utils/BLEServer.cpp index 83497331..00d9198d 100755 --- a/cpp_utils/BLEServer.cpp +++ b/cpp_utils/BLEServer.cpp @@ -197,7 +197,7 @@ void BLEServer::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t case ESP_GATTS_CREATE_EVT: { BLEService* pService = m_serviceMap.getByUUID(param->create.service_id.id.uuid, param->create.service_id.id.inst_id); // <--- very big bug for multi services with the same uuid m_serviceMap.setByHandle(param->create.service_handle, pService); - m_semaphoreCreateEvt.give(); + m_semaphoreCreateEvt.giveFromISR(); break; } // ESP_GATTS_CREATE_EVT @@ -245,7 +245,7 @@ void BLEServer::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t // case ESP_GATTS_REG_EVT: { m_gatts_if = gatts_if; - m_semaphoreRegisterAppEvt.give(); // Unlock the mutex waiting for the registration of the app. + m_semaphoreRegisterAppEvt.giveFromISR(); // Unlock the mutex waiting for the registration of the app. break; } // ESP_GATTS_REG_EVT @@ -268,7 +268,7 @@ void BLEServer::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t } case ESP_GATTS_OPEN_EVT: - m_semaphoreOpenEvt.give(param->open.status); + m_semaphoreOpenEvt.giveFromISR(param->open.status); break; default: diff --git a/cpp_utils/BLEService.cpp b/cpp_utils/BLEService.cpp index 3034cf18..14583da2 100644 --- a/cpp_utils/BLEService.cpp +++ b/cpp_utils/BLEService.cpp @@ -307,7 +307,7 @@ void BLEService::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t // uint16_t service_handle case ESP_GATTS_START_EVT: { if (param->start.service_handle == getHandle()) { - m_semaphoreStartEvt.give(); + m_semaphoreStartEvt.giveFromISR(); } break; } // ESP_GATTS_START_EVT @@ -320,7 +320,7 @@ void BLEService::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t // case ESP_GATTS_STOP_EVT: { if (param->stop.service_handle == getHandle()) { - m_semaphoreStopEvt.give(); + m_semaphoreStopEvt.giveFromISR(); } break; } // ESP_GATTS_STOP_EVT @@ -341,7 +341,7 @@ void BLEService::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t case ESP_GATTS_CREATE_EVT: { if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid)) && m_instId == param->create.service_id.id.inst_id) { setHandle(param->create.service_handle); - m_semaphoreCreateEvt.give(); + m_semaphoreCreateEvt.giveFromISR(); } break; } // ESP_GATTS_CREATE_EVT @@ -356,7 +356,7 @@ void BLEService::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t // case ESP_GATTS_DELETE_EVT: { if (param->del.service_handle == getHandle()) { - m_semaphoreDeleteEvt.give(); + m_semaphoreDeleteEvt.giveFromISR(); } break; } // ESP_GATTS_DELETE_EVT diff --git a/cpp_utils/FreeRTOS.cpp b/cpp_utils/FreeRTOS.cpp index 40e5bbf3..0f2d2132 100644 --- a/cpp_utils/FreeRTOS.cpp +++ b/cpp_utils/FreeRTOS.cpp @@ -154,6 +154,17 @@ void FreeRTOS::Semaphore::giveFromISR() { } // giveFromISR +/** + * @brief Give a semaphore from an ISR. + * The Semaphore is given with an associated value. + * @param [in] value The value to associate with the semaphore. + */ +void FreeRTOS::Semaphore::giveFromISR(uint32_t value) { + m_value = value; + giveFromISR(); +} // giveFromISR + + /** * @brief Take a semaphore. * Take a semaphore and wait indefinitely. diff --git a/cpp_utils/FreeRTOS.h b/cpp_utils/FreeRTOS.h index b861c875..50e5e2d4 100644 --- a/cpp_utils/FreeRTOS.h +++ b/cpp_utils/FreeRTOS.h @@ -35,6 +35,7 @@ class FreeRTOS { void give(); void give(uint32_t value); void giveFromISR(); + void giveFromISR(uint32_t value); void setName(std::string name); bool take(std::string owner = ""); bool take(uint32_t timeoutMs, std::string owner = "");