Skip to content
Snippets Groups Projects
Commit 104ec18e authored by Davide Mor's avatar Davide Mor
Browse files

Run formatter

parent 1c37cb9c
No related branches found
No related tags found
No related merge requests found
...@@ -7,20 +7,23 @@ ...@@ -7,20 +7,23 @@
#include <mavlink_lib/pyxis/mavlink.h> #include <mavlink_lib/pyxis/mavlink.h>
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#include <iostream>
#include <fstream> #include <fstream>
#include <iostream>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
const mavlink_message_info_t& get_message_info_for(uint8_t msgid) { const mavlink_message_info_t &get_message_info_for(uint8_t msgid)
{
static const mavlink_message_info_t infos[256] = MAVLINK_MESSAGE_INFO; static const mavlink_message_info_t infos[256] = MAVLINK_MESSAGE_INFO;
return infos[msgid]; return infos[msgid];
} }
class CsvStream { class CsvStream
{
public: public:
CsvStream(std::string path, const mavlink_message_info_t& info) : CsvStream(std::string path, const mavlink_message_info_t &info)
os(), info(info) { : os(), info(info)
{
os.exceptions(std::ifstream::failbit); os.exceptions(std::ifstream::failbit);
os.open(path, std::ios::binary); os.open(path, std::ios::binary);
...@@ -28,51 +31,62 @@ public: ...@@ -28,51 +31,62 @@ public:
format_message_header(); format_message_header();
} }
void put(const mavlink_message_t &msg) { void put(const mavlink_message_t &msg) { format_message(msg); }
format_message(msg);
}
private: private:
void format_field(const mavlink_message_t &msg, int id) { void format_field(const mavlink_message_t &msg, int id)
{
const mavlink_field_info_t &field = info.fields[id]; const mavlink_field_info_t &field = info.fields[id];
if(field.array_length != 0) { if (field.array_length != 0)
{
os << "<array not implemented>"; os << "<array not implemented>";
} else { }
switch(field.type) { else
{
switch (field.type)
{
case MAVLINK_TYPE_CHAR: case MAVLINK_TYPE_CHAR:
os << _MAV_RETURN_char(&msg, field.wire_offset); os << _MAV_RETURN_char(&msg, field.wire_offset);
break; break;
case MAVLINK_TYPE_UINT8_T: case MAVLINK_TYPE_UINT8_T:
os << static_cast<uint64_t>(_MAV_RETURN_uint8_t(&msg, field.wire_offset)); os << static_cast<uint64_t>(
_MAV_RETURN_uint8_t(&msg, field.wire_offset));
break; break;
case MAVLINK_TYPE_UINT16_T: case MAVLINK_TYPE_UINT16_T:
os << static_cast<uint64_t>(_MAV_RETURN_uint16_t(&msg, field.wire_offset)); os << static_cast<uint64_t>(
_MAV_RETURN_uint16_t(&msg, field.wire_offset));
break; break;
case MAVLINK_TYPE_UINT32_T: case MAVLINK_TYPE_UINT32_T:
os << static_cast<uint64_t>(_MAV_RETURN_uint32_t(&msg, field.wire_offset)); os << static_cast<uint64_t>(
_MAV_RETURN_uint32_t(&msg, field.wire_offset));
break; break;
case MAVLINK_TYPE_UINT64_T: case MAVLINK_TYPE_UINT64_T:
os << static_cast<uint64_t>(_MAV_RETURN_uint64_t(&msg, field.wire_offset)); os << static_cast<uint64_t>(
_MAV_RETURN_uint64_t(&msg, field.wire_offset));
break; break;
case MAVLINK_TYPE_INT8_T: case MAVLINK_TYPE_INT8_T:
os << static_cast<int64_t>(_MAV_RETURN_uint8_t(&msg, field.wire_offset)); os << static_cast<int64_t>(
_MAV_RETURN_uint8_t(&msg, field.wire_offset));
break; break;
case MAVLINK_TYPE_INT16_T: case MAVLINK_TYPE_INT16_T:
os << static_cast<int64_t>(_MAV_RETURN_uint16_t(&msg, field.wire_offset)); os << static_cast<int64_t>(
_MAV_RETURN_uint16_t(&msg, field.wire_offset));
break; break;
case MAVLINK_TYPE_INT32_T: case MAVLINK_TYPE_INT32_T:
os << static_cast<int64_t>(_MAV_RETURN_uint32_t(&msg, field.wire_offset)); os << static_cast<int64_t>(
_MAV_RETURN_uint32_t(&msg, field.wire_offset));
break; break;
case MAVLINK_TYPE_INT64_T: case MAVLINK_TYPE_INT64_T:
os << static_cast<int64_t>(_MAV_RETURN_uint64_t(&msg, field.wire_offset)); os << static_cast<int64_t>(
_MAV_RETURN_uint64_t(&msg, field.wire_offset));
break; break;
case MAVLINK_TYPE_FLOAT: case MAVLINK_TYPE_FLOAT:
...@@ -86,10 +100,13 @@ private: ...@@ -86,10 +100,13 @@ private:
} }
} }
void format_message(const mavlink_message_t &msg) { void format_message(const mavlink_message_t &msg)
{
bool first = true; bool first = true;
for(unsigned i = 0; i < info.num_fields; i++) { for (unsigned i = 0; i < info.num_fields; i++)
if(!first) { {
if (!first)
{
os << ","; os << ",";
} }
...@@ -100,10 +117,13 @@ private: ...@@ -100,10 +117,13 @@ private:
os << std::endl; os << std::endl;
} }
void format_message_header() { void format_message_header()
{
bool first = true; bool first = true;
for(unsigned i = 0; i < info.num_fields; i++) { for (unsigned i = 0; i < info.num_fields; i++)
if(!first) { {
if (!first)
{
os << ","; os << ",";
} }
...@@ -118,12 +138,16 @@ private: ...@@ -118,12 +138,16 @@ private:
const mavlink_message_info_t &info; const mavlink_message_info_t &info;
}; };
class Decoder { class Decoder
{
public: public:
Decoder(std::string input, std::string output) : Decoder(std::string input, std::string output)
input(input), output(output) {} : input(input), output(output)
{
}
void decode() { void decode()
{
std::ifstream is; std::ifstream is;
is.exceptions(std::ifstream::failbit); is.exceptions(std::ifstream::failbit);
is.open(input, std::ios::binary); is.open(input, std::ios::binary);
...@@ -132,15 +156,20 @@ public: ...@@ -132,15 +156,20 @@ public:
mavlink_message_t msg; mavlink_message_t msg;
mavlink_status_t status; mavlink_status_t status;
while(is.peek() != EOF) { while (is.peek() != EOF)
if(mavlink_parse_char(MAVLINK_COMM_0, is.get(), &msg, &status)) { {
if (mavlink_parse_char(MAVLINK_COMM_0, is.get(), &msg, &status))
{
uint8_t msgid = msg.msgid; uint8_t msgid = msg.msgid;
const mavlink_message_info_t &info = get_message_info_for(msgid); const mavlink_message_info_t &info =
get_message_info_for(msgid);
auto it = streams.find(msgid); auto it = streams.find(msgid);
if(it == streams.end()) { if (it == streams.end())
{
std::string filename = get_filename_for(info); std::string filename = get_filename_for(info);
it = streams.insert({msgid, CsvStream(filename, info)}).first; it = streams.insert({msgid, CsvStream(filename, info)})
.first;
} }
it->second.put(msg); it->second.put(msg);
...@@ -149,7 +178,8 @@ public: ...@@ -149,7 +178,8 @@ public:
} }
private: private:
std::string get_filename_for(const mavlink_message_info_t &info) { std::string get_filename_for(const mavlink_message_info_t &info)
{
return output + "/" + info.name + ".csv"; return output + "/" + info.name + ".csv";
} }
...@@ -157,9 +187,9 @@ private: ...@@ -157,9 +187,9 @@ private:
std::string output; std::string output;
}; };
void print_usage() { void print_usage()
std::cout << {
"Skyward mavlink decoder 2.0\n" std::cout << "Skyward mavlink decoder 2.0\n"
"\n" "\n"
"USAGE:\n" "USAGE:\n"
" ./mavdecoder <FILENAME>\n" " ./mavdecoder <FILENAME>\n"
...@@ -170,16 +200,21 @@ void print_usage() { ...@@ -170,16 +200,21 @@ void print_usage() {
<< std::flush; << std::flush;
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
if(argc != 2) { {
if (argc != 2)
{
print_usage(); print_usage();
return -1; return -1;
} }
try { try
{
Decoder decoder(argv[1], "out"); Decoder decoder(argv[1], "out");
decoder.decode(); decoder.decode();
} catch(std::exception &ex) { }
catch (std::exception &ex)
{
std::cout << "An exception occurred: " << ex.what() << std::endl; std::cout << "An exception occurred: " << ex.what() << std::endl;
return -1; return -1;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment