Skip to content

FreeRTOS giveFromISR() when releasing within a handler #846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cpp_utils/BLEAdvertising.cpp
Original file line number Diff line number Diff line change
@@ -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: {
6 changes: 3 additions & 3 deletions cpp_utils/BLECharacteristic.cpp
Original file line number Diff line number Diff line change
@@ -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();
Copy link
Contributor

@copercini copercini Jun 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be m_semaphoreConfEvt.giveFromISR(param->conf.status); ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you check FreeRTOS.cpp:138, the .give() variant that passes a value stores the value in m_value, which is returned by the .wait() call. Nothing is currently done with this value, so passing & storing it is irrelevant.

Also, there is currently no '.giveFromISR()' variant that passes a value (see FreeRTOS.cpp:147).

break;
}

@@ -449,7 +449,7 @@ void BLECharacteristic::handleGATTServerEvent(
}

case ESP_GATTS_DISCONNECT_EVT: {
m_semaphoreConfEvt.give();
m_semaphoreConfEvt.giveFromISR();
break;
}

10 changes: 5 additions & 5 deletions cpp_utils/BLEClient.cpp
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion cpp_utils/BLEDescriptor.cpp
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions cpp_utils/BLERemoteCharacteristic.cpp
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion cpp_utils/BLEScan.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
6 changes: 3 additions & 3 deletions cpp_utils/BLEServer.cpp
Original file line number Diff line number Diff line change
@@ -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:
8 changes: 4 additions & 4 deletions cpp_utils/BLEService.cpp
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions cpp_utils/FreeRTOS.cpp
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions cpp_utils/FreeRTOS.h
Original file line number Diff line number Diff line change
@@ -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 = "<Unknown>");
bool take(uint32_t timeoutMs, std::string owner = "<Unknown>");