mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-26 18:53:33 +08:00
dataman: remove locking mechanism
This commit is contained in:
@@ -55,10 +55,10 @@ DatamanClient::DatamanClient()
|
||||
|
||||
hrt_abstime timestamp = hrt_absolute_time();
|
||||
|
||||
dataman_request_s request = {
|
||||
.timestamp = timestamp,
|
||||
.request_type = DM_GET_ID
|
||||
};
|
||||
dataman_request_s request;
|
||||
request.timestamp = timestamp;
|
||||
request.request_type = DM_GET_ID;
|
||||
request.client_id = CLIENT_ID_NOT_SET;
|
||||
|
||||
bool success = syncHandler(request, response, timestamp, 1000_ms);
|
||||
|
||||
@@ -149,14 +149,13 @@ bool DatamanClient::readSync(dm_item_t item, uint32_t index, uint8_t *buffer, ui
|
||||
bool success = false;
|
||||
hrt_abstime timestamp = hrt_absolute_time();
|
||||
|
||||
dataman_request_s request = {
|
||||
.timestamp = timestamp,
|
||||
.index = index,
|
||||
.data_length = length,
|
||||
.client_id = _client_id,
|
||||
.request_type = DM_READ,
|
||||
.item = item
|
||||
};
|
||||
dataman_request_s request;
|
||||
request.timestamp = timestamp;
|
||||
request.index = index;
|
||||
request.data_length = length;
|
||||
request.client_id = _client_id;
|
||||
request.request_type = DM_READ;
|
||||
request.item = static_cast<uint8_t>(item);
|
||||
|
||||
dataman_response_s response{};
|
||||
success = syncHandler(request, response, timestamp, timeout);
|
||||
@@ -187,14 +186,13 @@ bool DatamanClient::writeSync(dm_item_t item, uint32_t index, uint8_t *buffer, u
|
||||
bool success = false;
|
||||
hrt_abstime timestamp = hrt_absolute_time();
|
||||
|
||||
dataman_request_s request = {
|
||||
.timestamp = timestamp,
|
||||
.index = index,
|
||||
.data_length = length,
|
||||
.client_id = _client_id,
|
||||
.request_type = DM_WRITE,
|
||||
.item = item
|
||||
};
|
||||
dataman_request_s request;
|
||||
request.timestamp = timestamp;
|
||||
request.index = index;
|
||||
request.data_length = length;
|
||||
request.client_id = _client_id;
|
||||
request.request_type = DM_WRITE;
|
||||
request.item = static_cast<uint8_t>(item);
|
||||
|
||||
memcpy(request.data, buffer, length);
|
||||
|
||||
@@ -219,12 +217,11 @@ bool DatamanClient::clearSync(dm_item_t item, hrt_abstime timeout)
|
||||
bool success = false;
|
||||
hrt_abstime timestamp = hrt_absolute_time();
|
||||
|
||||
dataman_request_s request = {
|
||||
.timestamp = timestamp,
|
||||
.client_id = _client_id,
|
||||
.request_type = DM_CLEAR,
|
||||
.item = item
|
||||
};
|
||||
dataman_request_s request;
|
||||
request.timestamp = timestamp;
|
||||
request.client_id = _client_id;
|
||||
request.request_type = DM_CLEAR;
|
||||
request.item = static_cast<uint8_t>(item);
|
||||
|
||||
dataman_response_s response{};
|
||||
success = syncHandler(request, response, timestamp, timeout);
|
||||
@@ -242,74 +239,6 @@ bool DatamanClient::clearSync(dm_item_t item, hrt_abstime timeout)
|
||||
return success;
|
||||
}
|
||||
|
||||
bool DatamanClient::lockSync(dm_item_t item, hrt_abstime timeout)
|
||||
{
|
||||
bool success = true;
|
||||
hrt_abstime timestamp = hrt_absolute_time();
|
||||
|
||||
dataman_request_s request = {
|
||||
.timestamp = timestamp,
|
||||
.client_id = _client_id,
|
||||
.request_type = DM_LOCK,
|
||||
.item = item
|
||||
};
|
||||
|
||||
dataman_response_s response{};
|
||||
response.status = dataman_response_s::STATUS_ALREADY_LOCKED;
|
||||
|
||||
while (success && (response.status == dataman_response_s::STATUS_ALREADY_LOCKED)) {
|
||||
success = syncHandler(request, response, timestamp, timeout);
|
||||
}
|
||||
|
||||
if (success) {
|
||||
|
||||
if (response.status != dataman_response_s::STATUS_SUCCESS) {
|
||||
|
||||
success = false;
|
||||
|
||||
PX4_ERR("lockSync failed! status=%" PRIu8 ", item=%" PRIu8,
|
||||
response.status, static_cast<uint8_t>(item));
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool DatamanClient::unlockSync(dm_item_t item, hrt_abstime timeout)
|
||||
{
|
||||
bool success = false;
|
||||
hrt_abstime timestamp = hrt_absolute_time();
|
||||
|
||||
dataman_request_s request = {
|
||||
.timestamp = timestamp,
|
||||
.client_id = _client_id,
|
||||
.request_type = DM_UNLOCK,
|
||||
.item = item
|
||||
};
|
||||
|
||||
dataman_response_s response{};
|
||||
success = syncHandler(request, response, timestamp, timeout);
|
||||
|
||||
if (success) {
|
||||
|
||||
if (response.status != dataman_response_s::STATUS_SUCCESS) {
|
||||
|
||||
success = false;
|
||||
|
||||
if (response.status == dataman_response_s::STATUS_ALREADY_UNLOCKED) {
|
||||
PX4_WARN("Dataman already unlocked for item=%" PRIu8,
|
||||
static_cast<uint8_t>(item));
|
||||
|
||||
} else {
|
||||
PX4_ERR("unlockSync failed! status=%" PRIu8 ", item=%" PRIu8,
|
||||
response.status, static_cast<uint8_t>(item));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool DatamanClient::readAsync(dm_item_t item, uint32_t index, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
if (length > g_per_item_size[item]) {
|
||||
@@ -323,14 +252,13 @@ bool DatamanClient::readAsync(dm_item_t item, uint32_t index, uint8_t *buffer, u
|
||||
|
||||
hrt_abstime timestamp = hrt_absolute_time();
|
||||
|
||||
dataman_request_s request = {
|
||||
.timestamp = timestamp,
|
||||
.index = index,
|
||||
.data_length = length,
|
||||
.client_id = _client_id,
|
||||
.request_type = DM_READ,
|
||||
.item = item
|
||||
};
|
||||
dataman_request_s request;
|
||||
request.timestamp = timestamp;
|
||||
request.index = index;
|
||||
request.data_length = length;
|
||||
request.client_id = _client_id;
|
||||
request.request_type = DM_READ;
|
||||
request.item = static_cast<uint8_t>(item);
|
||||
|
||||
_active_request.timestamp = timestamp;
|
||||
_active_request.request_type = DM_READ;
|
||||
@@ -362,14 +290,13 @@ bool DatamanClient::writeAsync(dm_item_t item, uint32_t index, uint8_t *buffer,
|
||||
|
||||
hrt_abstime timestamp = hrt_absolute_time();
|
||||
|
||||
dataman_request_s request = {
|
||||
.timestamp = timestamp,
|
||||
.index = index,
|
||||
.data_length = length,
|
||||
.client_id = _client_id,
|
||||
.request_type = DM_WRITE,
|
||||
.item = item
|
||||
};
|
||||
dataman_request_s request;
|
||||
request.timestamp = timestamp;
|
||||
request.index = index;
|
||||
request.data_length = length;
|
||||
request.client_id = _client_id;
|
||||
request.request_type = DM_WRITE;
|
||||
request.item = static_cast<uint8_t>(item);
|
||||
|
||||
memcpy(request.data, buffer, length);
|
||||
|
||||
@@ -398,72 +325,17 @@ bool DatamanClient::clearAsync(dm_item_t item)
|
||||
|
||||
hrt_abstime timestamp = hrt_absolute_time();
|
||||
|
||||
dataman_request_s request = {
|
||||
.timestamp = timestamp,
|
||||
.client_id = _client_id,
|
||||
.request_type = DM_CLEAR,
|
||||
.item = item
|
||||
};
|
||||
dataman_request_s request;
|
||||
request.timestamp = timestamp;
|
||||
request.client_id = _client_id;
|
||||
request.request_type = DM_CLEAR;
|
||||
request.item = static_cast<uint8_t>(item);
|
||||
request.index = 0;
|
||||
|
||||
_active_request.timestamp = timestamp;
|
||||
_active_request.request_type = DM_CLEAR;
|
||||
_active_request.item = item;
|
||||
_state = State::RequestSent;
|
||||
|
||||
_dataman_request_pub.publish(request);
|
||||
|
||||
success = true;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool DatamanClient::lockAsync(dm_item_t item)
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
if (_state == State::Idle) {
|
||||
|
||||
hrt_abstime timestamp = hrt_absolute_time();
|
||||
|
||||
dataman_request_s request = {
|
||||
.timestamp = timestamp,
|
||||
.client_id = _client_id,
|
||||
.request_type = DM_LOCK,
|
||||
.item = item
|
||||
};
|
||||
|
||||
_active_request.timestamp = timestamp;
|
||||
_active_request.request_type = DM_LOCK;
|
||||
_active_request.item = item;
|
||||
_state = State::RequestSent;
|
||||
|
||||
_dataman_request_pub.publish(request);
|
||||
|
||||
success = true;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool DatamanClient::unlockAsync(dm_item_t item)
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
if (_state == State::Idle) {
|
||||
|
||||
hrt_abstime timestamp = hrt_absolute_time();
|
||||
|
||||
dataman_request_s request = {
|
||||
.timestamp = timestamp,
|
||||
.client_id = _client_id,
|
||||
.request_type = DM_UNLOCK,
|
||||
.item = item
|
||||
};
|
||||
|
||||
_active_request.timestamp = timestamp;
|
||||
_active_request.request_type = DM_UNLOCK;
|
||||
_active_request.item = item;
|
||||
_active_request.index = request.index;
|
||||
_state = State::RequestSent;
|
||||
|
||||
_dataman_request_pub.publish(request);
|
||||
@@ -497,23 +369,19 @@ void DatamanClient::update()
|
||||
|
||||
_response_status = response.status;
|
||||
|
||||
if ((_response_status != dataman_response_s::STATUS_SUCCESS) &&
|
||||
(_response_status != dataman_response_s::STATUS_ALREADY_LOCKED) &&
|
||||
(_response_status != dataman_response_s::STATUS_ALREADY_UNLOCKED)) {
|
||||
if (_response_status != dataman_response_s::STATUS_SUCCESS) {
|
||||
|
||||
PX4_ERR("Async request type %" PRIu8 " failed! status=%" PRIu8 " item=%" PRIu8 " index=%" PRIu32,
|
||||
response.request_type, response.status, static_cast<uint8_t>(_active_request.item), _active_request.index);
|
||||
}
|
||||
|
||||
if (_response_status != dataman_response_s::STATUS_ALREADY_LOCKED) {
|
||||
_state = State::ResponseReceived;
|
||||
}
|
||||
_state = State::ResponseReceived;
|
||||
}
|
||||
}
|
||||
|
||||
if (_state == State::RequestSent) {
|
||||
|
||||
/* Retry the request if there is no answer or if already locked. */
|
||||
/* Retry the request if there is no answer */
|
||||
if (((_active_request.request_type != DM_CLEAR) && (hrt_elapsed_time(&_active_request.timestamp) > 100_ms)) ||
|
||||
(hrt_elapsed_time(&_active_request.timestamp) > 1000_ms)
|
||||
) {
|
||||
@@ -522,14 +390,13 @@ void DatamanClient::update()
|
||||
|
||||
_active_request.timestamp = timestamp;
|
||||
|
||||
dataman_request_s request = {
|
||||
.timestamp = timestamp,
|
||||
.index = _active_request.index,
|
||||
.data_length = _active_request.length,
|
||||
.client_id = _client_id,
|
||||
.request_type = _active_request.request_type,
|
||||
.item = _active_request.item
|
||||
};
|
||||
dataman_request_s request;
|
||||
request.timestamp = timestamp;
|
||||
request.index = _active_request.index;
|
||||
request.data_length = _active_request.length;
|
||||
request.client_id = _client_id;
|
||||
request.request_type = static_cast<uint8_t>(_active_request.request_type);
|
||||
request.item = static_cast<uint8_t>(_active_request.item);
|
||||
|
||||
if (_active_request.request_type == DM_WRITE) {
|
||||
memcpy(request.data, _active_request.buffer, _active_request.length);
|
||||
@@ -546,15 +413,12 @@ void DatamanClient::update()
|
||||
bool DatamanClient::lastOperationCompleted(bool &success)
|
||||
{
|
||||
bool completed = false;
|
||||
success = false;
|
||||
|
||||
if (_state == State::ResponseReceived) {
|
||||
|
||||
if ((_response_status == dataman_response_s::STATUS_SUCCESS) ||
|
||||
(_response_status == dataman_response_s::STATUS_ALREADY_UNLOCKED)) {
|
||||
if (_response_status == dataman_response_s::STATUS_SUCCESS) {
|
||||
success = true;
|
||||
|
||||
} else {
|
||||
success = false;
|
||||
}
|
||||
|
||||
_state = State::Idle;
|
||||
@@ -714,7 +578,8 @@ void DatamanCache::update()
|
||||
|
||||
success = _client.readAsync(static_cast<dm_item_t>(_items[_update_index].response.item),
|
||||
_items[_update_index].response.index,
|
||||
_items[_update_index].response.data, g_per_item_size[_items[_update_index].response.item]);
|
||||
_items[_update_index].response.data,
|
||||
g_per_item_size[_items[_update_index].response.item]);
|
||||
|
||||
if (success) {
|
||||
_items[_update_index].cache_state = State::RequestSent;
|
||||
|
||||
@@ -87,28 +87,6 @@ public:
|
||||
*/
|
||||
bool clearSync(dm_item_t item, hrt_abstime timeout = 1000_ms);
|
||||
|
||||
/**
|
||||
* @brief Locks an item in the dataman for exclusive access.
|
||||
*
|
||||
* This function sends a DM_LOCK request to the dataman to lock an item for exclusive access.
|
||||
* If the item is already locked, it will wait and retry until it can obtain the lock or the timeout
|
||||
* is reached. Once the lock is obtained, the item can be safely modified.
|
||||
*
|
||||
* @param[in] item The item to be locked.
|
||||
* @param[in] timeout The maximum time to wait for the lock in microseconds.
|
||||
* @return true if the item is locked successfully, false otherwise.
|
||||
*/
|
||||
bool lockSync(dm_item_t item, hrt_abstime timeout = 1000_ms);
|
||||
|
||||
/**
|
||||
* Unlock an item in dataman.
|
||||
*
|
||||
* @param[in] item: The item to unlock.
|
||||
* @param[in] timeout: The timeout for the operation.
|
||||
* @return True if the unlock operation was successful, false otherwise.
|
||||
*/
|
||||
bool unlockSync(dm_item_t item, hrt_abstime timeout = 1000_ms);
|
||||
|
||||
/**
|
||||
* @brief Initiates an asynchronous request to read the data from dataman for a specific item and index.
|
||||
*
|
||||
@@ -151,25 +129,6 @@ public:
|
||||
*/
|
||||
bool clearAsync(dm_item_t item);
|
||||
|
||||
/**
|
||||
* @brief Locks a dataman item asynchronously.
|
||||
*
|
||||
* This function sends a lock request to the dataman service, asking it to lock the specified item.
|
||||
* The function returns immediately, without waiting for the lock operation to complete.
|
||||
*
|
||||
* @param[in] item The dataman item to be locked.
|
||||
* @return True if the lock request was successfully sent, false otherwise.
|
||||
*/
|
||||
bool lockAsync(dm_item_t item);
|
||||
|
||||
/**
|
||||
* @brief Unlocks the specified dataman item asynchronously.
|
||||
*
|
||||
* @param[in] item The item to unlock.
|
||||
* @return true if the request was successfully queued, false otherwise.
|
||||
*/
|
||||
bool unlockAsync(dm_item_t item);
|
||||
|
||||
/**
|
||||
* @brief Updates the state of the dataman client for asynchronous functions.
|
||||
*
|
||||
@@ -177,7 +136,7 @@ public:
|
||||
* and updates the state accordingly. If there is no response for a request, it retries the
|
||||
* request after a timeout.
|
||||
*
|
||||
* @see readAsync(), writeAsync(), clearAsync(), lockAsync(), unlockAsync(), lastOperationCompleted()
|
||||
* @see readAsync(), writeAsync(), clearAsync(), lastOperationCompleted()
|
||||
*/
|
||||
void update();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user