Skip to content

Commit

Permalink
Fanet : factorize duplicated code...
Browse files Browse the repository at this point in the history
  • Loading branch information
brunotl committed Aug 3, 2024
1 parent b7c1e68 commit 70b39f3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 34 deletions.
24 changes: 9 additions & 15 deletions Common/Source/Devices/Fanet/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Created on: Sep 29, 2018
* Author: sid
*/
#include <cstdint>
#include <cstring>
#include <cmath>
#include <stdexcept>
Expand All @@ -25,7 +24,7 @@ uint16_t Frame::coord2payload_compressed(double deg) {
return ((dec_int & 0x7FFF) | (deg_odd << 15));
}

void Frame::coord2payload_absolut(double lat, double lon, Frame::payload_back_insert_iterator& out) {
void Frame::coord2payload_absolut(double lat, double lon, payload_t::back_insert_iterator& out) {
int32_t lat_i = std::round(lat * 93206.0);
int32_t lon_i = std::round(lon * 46603.0);

Expand All @@ -38,22 +37,21 @@ void Frame::coord2payload_absolut(double lat, double lon, Frame::payload_back_in
out = ((uint8_t*)&lon_i)[2];
}

Frame::payload_t Frame::serialize() const {
payload_t Frame::serialize() const {
if (src.id <= 0 || src.id >= 0xFFFF || src.manufacturer <= 0 || src.manufacturer >= 0xFE) {
throw std::runtime_error("invalid fanet id");
}

payload_t data;
data.reserve(MAC_FRM_MIN_HEADER_LENGTH + payload.size());
auto out_it = std::back_inserter(data);
auto out_it = data.back_inserter();

/* header */
out_it = (ack_requested || dest.id != 0 || dest.manufacturer != 0 || signature != 0)
<< MAC_FRM_HEADER_EXTHEADER_BIT |
forward << MAC_FRM_HEADER_FORWARD_BIT | (type & MAC_FRM_HEADER_TYPE_MASK);
out_it = src.manufacturer;
out_it = src.id & 0x00FF;
out_it = (src.id >> 8) & 0x00FF;

src.serialize(out_it);

/* extended header */
if (data[0] & 1 << 7)
Expand All @@ -63,9 +61,7 @@ Frame::payload_t Frame::serialize() const {

/* extheader and unicast -> add destination addr */
if ((data[0] & 1 << 7) && (data[4] & 1 << 5)) {
out_it = dest.manufacturer & 0x00FF;
out_it = dest.id & 0x00FF;
out_it = (dest.id >> 8) & 0x00FF;
dest.serialize(out_it);
}

/* extheader and signature -> add signature */
Expand All @@ -88,8 +84,8 @@ Frame::Frame(const uint8_t *data, size_t length) {
/* header */
forward = !!(data[0] & (1 << MAC_FRM_HEADER_FORWARD_BIT));
type = data[0] & MAC_FRM_HEADER_TYPE_MASK;
src.manufacturer = data[1];
src.id = data[2] | (data[3] << 8);

src = { data[1], data[3], data[2] };

/* extended header */
if (data[0] & 1 << MAC_FRM_HEADER_EXTHEADER_BIT) {
Expand All @@ -100,9 +96,7 @@ Frame::Frame(const uint8_t *data, size_t length) {

/* unicast */
if (data[4] & (1 << MAC_FRM_EXTHEADER_UNICAST_BIT)) {
dest.manufacturer = data[5];
dest.id = data[6] | (data[7] << 8);

dest = { data[5], data[7], data[6] };
payload_start += MAC_FRM_ADDR_LENGTH;
}

Expand Down
11 changes: 6 additions & 5 deletions Common/Source/Devices/Fanet/frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,13 @@
#define FRM_TYPE_THERMAL 9

#include "macaddr.h"
#include <vector>

class Frame final {
public:
using payload_t = std::vector<uint8_t>;
using payload_back_insert_iterator = std::back_insert_iterator<payload_t>;

/* general stuff */
static uint16_t coord2payload_compressed(double deg);
static void coord2payload_absolut(double lat, double lon, payload_back_insert_iterator& out);
static void coord2payload_absolut(double lat, double lon, payload_t::back_insert_iterator& out);

/* addresses */
MacAddr src;
Expand All @@ -76,9 +73,13 @@ class Frame final {
/* Received stuff */
int rssi = 0;

std::vector<uint8_t> serialize() const;
payload_t serialize() const;

explicit Frame(const MacAddr& addr) : src(addr) {}

Frame(const MacAddr& addr, int t, payload_t&& p)
: src(addr), type(t), payload(std::move(p)) {};

Frame() = delete;
Frame(const uint8_t* data, size_t length); // deserialize packet
~Frame() { }
Expand Down
23 changes: 19 additions & 4 deletions Common/Source/Devices/Fanet/macaddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifndef FANET_RADIO_MACADDR_H_
#define FANET_RADIO_MACADDR_H_

#include "payload.h"

/*
* 0, 0 == Broadcast
Expand All @@ -18,16 +19,30 @@ class MacAddr {
uint8_t manufacturer = 0U;
uint16_t id = 0U;

MacAddr(int manufacturer_addr, int id_addr) : manufacturer(manufacturer_addr), id(id_addr) {};
constexpr MacAddr(int manufacturer_addr, int id_addr)
: manufacturer(manufacturer_addr), id(id_addr) {}

constexpr MacAddr(int manufacturer_addr, int id_msb, int id_lsb)
: manufacturer(manufacturer_addr), id(id_lsb | (id_msb << 8)) {}

constexpr explicit MacAddr(uint32_t addr)
: manufacturer(addr & 0x000000FF), id((addr & 0x00FFFF00) >> 8) {}

MacAddr() = default; // broadcast address

MacAddr(const MacAddr& ma) = default;
constexpr MacAddr(const MacAddr& ma) = default;
MacAddr& operator= (const MacAddr& ma) = default;

MacAddr(MacAddr&& ma) = default;
constexpr MacAddr(MacAddr&& ma) = default;
MacAddr& operator= (MacAddr&& ma) = default;

uint32_t get() const {
void serialize(payload_t::back_insert_iterator& out) const {
out = manufacturer;
out = id & 0x00FF;
out = (id >> 8) & 0x00FF;
}

constexpr uint32_t get() const {
return (manufacturer & 0x000000FF) | ((id & 0x0000FFFF) << 8);
}

Expand Down
25 changes: 25 additions & 0 deletions Common/Source/Devices/Fanet/payload.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* LK8000 Tactical Flight Computer - WWW.LK8000.IT
* Released under GNU/GPL License v.2 or later
* See CREDITS.TXT file for authors and copyrights
*
* File: payload.h
* Author: Gerald Eichler
*/
#ifndef _devices_fanet_payload_h_
#define _devices_fanet_payload_h_
#include <cstdint>
#include <vector>

struct payload_t : public std::vector<uint8_t> {
using base_class = std::vector<uint8_t>;
using base_class::base_class;

using back_insert_iterator = std::back_insert_iterator<payload_t>;

back_insert_iterator back_inserter() {
return std::back_inserter(*this);
}
};

#endif // _devices_fanet_payload_h_
18 changes: 8 additions & 10 deletions Common/Source/Devices/devFlyBeeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ aircraft_t type() {
return aircraft_t::otherAircraft;
}

Frame::payload_t serialize_tracking(const NMEA_INFO& Basic, const DERIVED_INFO& Calculated) {
Frame::payload_t buffer;
payload_t serialize_tracking(const NMEA_INFO& Basic, const DERIVED_INFO& Calculated) {
payload_t buffer;
buffer.reserve(11);
auto out_it = std::back_inserter(buffer);

Expand All @@ -57,7 +57,8 @@ Frame::payload_t serialize_tracking(const NMEA_INFO& Basic, const DERIVED_INFO&
}
uint16_t data = alt;
/* online tracking */
data |= true << 15;
constexpr uint16_t online_tracking = 0x01;
data |= online_tracking << 15;
/* aircraft type */
data |= (type() & 0x7) << 12;

Expand All @@ -84,7 +85,7 @@ Frame::payload_t serialize_tracking(const NMEA_INFO& Basic, const DERIVED_INFO&
return buffer;
}

Frame::payload_t serialize_name() {
payload_t serialize_name() {
std::string name = to_utf8(PilotName_Config);
return { name.begin(), name.end() };
}
Expand Down Expand Up @@ -112,15 +113,12 @@ MacAddr generate_id() {
return addr;
}

BOOL SendData(DeviceDescriptor_t* d, uint8_t type, std::vector<uint8_t>&& data) {
BOOL SendData(DeviceDescriptor_t* d, uint8_t type, payload_t&& data) {

static MacAddr src_addr = generate_id();

auto frm = std::make_unique<Frame>(src_addr);
frm->type = type;
frm->payload = std::move(data);

auto buffer = frm->serialize();
Frame frm(src_addr, type, std::move(data));
auto buffer = frm.serialize();

d->Com->WriteGattCharacteristic(
"00001819-0000-1000-8000-00805F9B34FB",
Expand Down

0 comments on commit 70b39f3

Please sign in to comment.