implemented MAV_CMD_REQUEST_MESSAGE for STORAGE_INFORMATION

This commit is contained in:
David Jablonski
2020-06-03 17:57:12 +02:00
committed by Julian Oes
parent 25bfcf0497
commit fd09198c7e
4 changed files with 137 additions and 61 deletions
+111 -1
View File
@@ -5172,6 +5172,103 @@ protected:
}
};
#ifdef __PX4_DARWIN
#include <sys/param.h>
#include <sys/mount.h>
#else
#include <sys/statfs.h>
#endif
class MavlinkStreamStorageInformation : public MavlinkStream
{
public:
const char *get_name() const override
{
return MavlinkStreamStorageInformation::get_name_static();
}
static constexpr const char *get_name_static()
{
return "STORAGE_INFORMATION";
}
static constexpr uint16_t get_id_static()
{
return MAVLINK_MSG_ID_STORAGE_INFORMATION;
}
uint16_t get_id() override
{
return get_id_static();
}
static MavlinkStream *new_instance(Mavlink *mavlink)
{
return new MavlinkStreamStorageInformation(mavlink);
}
unsigned get_size() override
{
return MAVLINK_MSG_ID_STORAGE_INFORMATION_LEN + MAVLINK_NUM_NON_PAYLOAD_BYTES;
}
virtual bool request_message(float param1 = 0.0, float param2 = 0.0, float param3 = 0.0, float param4 = 0.0,
float param5 = 0.0, float param6 = 0.0, float param7 = 0.0)
{
storage_id = (int)roundf(param2);
return send(hrt_absolute_time());
}
private:
int storage_id = 0;
/* do not allow top copying this class */
MavlinkStreamStorageInformation(MavlinkStreamStorageInformation &) = delete;
MavlinkStreamStorageInformation &operator = (const MavlinkStreamStorageInformation &) = delete;
protected:
explicit MavlinkStreamStorageInformation(Mavlink *mavlink) : MavlinkStream(mavlink)
{}
bool send(const hrt_abstime t) override
{
mavlink_storage_information_t storage_info{};
const char *microsd_dir = PX4_STORAGEDIR;
if (storage_id == 0 || storage_id == 1) { // request is for all or the first storage
storage_info.storage_id = storage_id; // replace by 1
struct statfs statfs_buf;
uint64_t total_bytes = 0;
uint64_t avail_bytes = 0;
if (statfs(microsd_dir, &statfs_buf) == 0) {
total_bytes = (uint64_t)statfs_buf.f_blocks * statfs_buf.f_bsize;
avail_bytes = (uint64_t)statfs_buf.f_bavail * statfs_buf.f_bsize;
}
if (total_bytes == 0) { // on NuttX we get 0 total bytes if no SD card is inserted
storage_info.storage_count = 0;
storage_info.status = 0; // not available
} else {
storage_info.storage_count = 1;
storage_info.status = 2; // available & formatted
storage_info.total_capacity = total_bytes / 1024. / 1024.;
storage_info.available_capacity = avail_bytes / 1024. / 1024.;
storage_info.used_capacity = (total_bytes - avail_bytes) / 1024. / 1024.;
}
} else {
return false; // results in MAV_RESULT_DENIED
}
storage_info.time_boot_ms = t / 1000;
mavlink_msg_storage_information_send_struct(_mavlink->get_channel(), &storage_info);
return true;
}
};
static const StreamListItem streams_list[] = {
create_stream_list_item<MavlinkStreamHeartbeat>(),
create_stream_list_item<MavlinkStreamStatustext>(),
@@ -5231,7 +5328,8 @@ static const StreamListItem streams_list[] = {
create_stream_list_item<MavlinkStreamGroundTruth>(),
create_stream_list_item<MavlinkStreamPing>(),
create_stream_list_item<MavlinkStreamOrbitStatus>(),
create_stream_list_item<MavlinkStreamObstacleDistance>()
create_stream_list_item<MavlinkStreamObstacleDistance>(),
create_stream_list_item<MavlinkStreamStorageInformation>()
};
const char *get_stream_name(const uint16_t msg_id)
@@ -5259,3 +5357,15 @@ MavlinkStream *create_mavlink_stream(const char *stream_name, Mavlink *mavlink)
return nullptr;
}
MavlinkStream *create_mavlink_stream(const uint16_t msg_id, Mavlink *mavlink)
{
// search for stream with specified name in supported streams list
for (const auto &stream : streams_list) {
if (msg_id == stream.get_id()) {
return stream.new_instance(mavlink);
}
}
return nullptr;
}