Skip to content

Commit

Permalink
done?!
Browse files Browse the repository at this point in the history
Signed-off-by: Brenton Poke <[email protected]>
  • Loading branch information
BrentonPoke committed Oct 22, 2024
1 parent 6c27d1a commit f3bc8ba
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 95 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
https://github.com/OperatorFoundation/Crypto.git

build_flags =
-std=c++20
-std=c++2a
-Wno-inconsistent-missing-override
-Wno-missing-field-initializers
-Wno-format
Expand Down
42 changes: 17 additions & 25 deletions src/CdpPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,23 @@ class CdpPacket {
reset();
}
CdpPacket(const std::vector<byte> & buffer) {
int buffer_length = buffer.size();
// sduid
for(int i = 0; i < DUID_LENGTH; i++) {
sduid[i] = buffer[SDUID_POS + i];
}
// dduid
for(int i = 0; i < DUID_LENGTH; i++) {
dduid[i] = buffer[DDUID_POS + i];
}
// muid
for(int i = 0; i < MUID_LENGTH; i++) {
muid[i] = buffer[MUID_POS + i];
}
// topic
topic = buffer[TOPIC_POS];
// duckType
duckType = buffer[DUCK_TYPE_POS];
// hop count
hopCount = buffer[HOP_COUNT_POS];
// data crc
dcrc = duckutils::toUint32(&buffer[DATA_CRC_POS]);
// data section
for(int i = 0; i < buffer_length; i++) {
data[i] = buffer[DATA_POS + i];
}
int buffer_length = buffer.size();
// sduid
std::copy(&buffer[SDUID_POS], &buffer[DDUID_POS], sduid.begin());
// dduid
std::copy(&buffer[DDUID_POS], &buffer[MUID_POS], dduid.begin());
// muid
std::copy(&buffer[MUID_POS], &buffer[TOPIC_POS], muid.begin());
// topic
topic = buffer[TOPIC_POS];
// duckType
duckType = buffer[DUCK_TYPE_POS];
// hop count
hopCount = buffer[HOP_COUNT_POS];
// data crc
dcrc = duckutils::toUint32(&buffer[DATA_CRC_POS]);
// data section
data.assign(&buffer[DATA_POS], &buffer[buffer_length]);

}

Expand Down
8 changes: 4 additions & 4 deletions src/DuckNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ DNSServer DuckNet::dnsServer;
const char* DuckNet::DNS = "duck";
const byte DuckNet::DNS_PORT = 53;

void DuckNet::setDeviceId(std::array<byte,8> deviceId) {
this->deviceId.insert(this->deviceId.end(), deviceId.begin(), deviceId.end());
void DuckNet::setDeviceId(std::array<byte,8> devId) {
std::copy(devId.begin(), devId.end(), deviceId.begin());
}

int DuckNet::setupWebServer(bool createCaptivePortal, std::string html) {
Expand Down Expand Up @@ -95,7 +95,7 @@ int DuckNet::setupWebServer(bool createCaptivePortal, std::string html) {

for (int i = 0; i < paramsNumber; i++) {
const AsyncWebParameter* p = request->getParam(i);
logdbg_ln("%s : %s", p->name(), p->value());
logdbg_ln("%s : %s", p->name().c_str(), p->value().c_str());

if (p->name() == "clientId") {
clientId = p->value().c_str();
Expand All @@ -118,7 +118,7 @@ int DuckNet::setupWebServer(bool createCaptivePortal, std::string html) {
{
std::string response = "{\"muid\":\"" + duckutils::toString(muid) + "\"}";
request->send(200, "text/html", response.c_str());
logdbg_ln("Sent 200 response: %s",response);
logdbg_ln("Sent 200 response: %s",response.c_str());
}
break;
case DUCKLORA_ERR_MSG_TOO_LARGE:
Expand Down
71 changes: 30 additions & 41 deletions src/DuckPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,57 +81,46 @@ int DuckPacket::prepareForSending(BloomFilter *filter,

// ----- insert packet header -----
// source device uid
buffer.insert(buffer.end(), duid.begin(), duid.end());
logdbg_ln("SDuid: %s",duckutils::convertToHex(duid.data(), duid.size()).c_str());

for(int i = 0; i < DUID_LENGTH; i++) {
buffer[SDUID_POS + i] = duid[i];
}
logdbg_ln("SDuid: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());
// destination device uid
for(int i = 0; i < DUID_LENGTH; i++) {
buffer[DDUID_POS + i] = targetDevice[i];
}
logdbg_ln("DDuid: %s", duckutils::convertToHex(targetDevice.data(), targetDevice.size()).c_str());
// destination device uid
buffer.insert(buffer.end(), targetDevice.begin(), targetDevice.end());
logdbg_ln("DDuid: %s", duckutils::convertToHex(targetDevice.data(), targetDevice.size()).c_str());

// message uid
for(int i = 0; i < MUID_LENGTH; i++) {
buffer[MUID_POS + i] = message_id[i];
}
logdbg_ln("Muid: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());
// message uid
buffer.insert(buffer.end(), &message_id[0], &message_id[MUID_LENGTH]);
logdbg_ln("Muid: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());

// topic
buffer[TOPIC_POS] = topic;
logdbg_ln("Topic: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());
// topic
buffer.insert(buffer.end(), topic);
logdbg_ln("Topic: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());

// duckType
buffer[DUCK_TYPE_POS] = duckType;
logdbg_ln("duck type: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());
// duckType
buffer.insert(buffer.end(), duckType);
logdbg_ln("duck type: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());

// hop count
buffer[HOP_COUNT_POS] = 0x00;
logdbg_ln("hop count: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());
// hop count
buffer.insert(buffer.end(), 0x00);
logdbg_ln("hop count: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());

// data crc
for(int i = 0; i < DATA_CRC_LENGTH; i++) {
buffer[DATA_CRC_POS + i] = crc_bytes[i];
}
logdbg_ln("Data CRC: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());
// data crc
buffer.insert(buffer.end(), &crc_bytes[0], &crc_bytes[DATA_CRC_LENGTH]);
logdbg_ln("Data CRC: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());

// ----- insert data -----
if(duckcrypto::getState()) {
// ----- insert data -----
if(duckcrypto::getState()) {

for(int i = 0; i < MAX_DATA_LENGTH; i++) {
buffer[DATA_POS + i] = encryptedData[i];
}
logdbg_ln("Encrypted Data: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());
buffer.insert(buffer.end(), encryptedData.begin(), encryptedData.end());
logdbg_ln("Encrypted Data: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());

} else {
for(int i = 0; i < app_data.size(); i++) {
buffer[DATA_POS + i] = app_data[i];
} else {
buffer.insert(buffer.end(), app_data.begin(), app_data.end());
logdbg_ln("Data: %s",duckutils::convertToHex(buffer.data(), buffer.size()).c_str());
}
logdbg_ln("Data: %s",duckutils::convertToHex(buffer.data(), buffer.size()).c_str());
}

logdbg_ln("Built packet: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());

logdbg_ln("Built packet: %s", duckutils::convertToHex(buffer.data(), buffer.size()).c_str());
getBuffer().shrink_to_fit();
return DUCK_ERR_NONE;
}

14 changes: 7 additions & 7 deletions src/Ducks/Duck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,13 @@ int Duck::sendData(byte topic, const byte* data, int length,
return err;
}

int Duck::sendData(byte topic, std::vector<byte> data,
int Duck::sendData(byte topic, std::vector<byte>& data,
const std::array<byte,8> targetDevice, std::array<byte,8> * outgoingMuid)
{
// if (topic < reservedTopic::max_reserved) {
// logerr_ln("ERROR send data failed, topic is reserved.");
// return DUCKPACKET_ERR_TOPIC_INVALID;
// }
if (topic < reservedTopic::max_reserved) {
logerr_ln("ERROR send data failed, topic is reserved.");
return DUCKPACKET_ERR_TOPIC_INVALID;
}
if (data.size() > MAX_DATA_LENGTH) {
logerr_ln("ERROR send data failed, message too large: %d bytes",data.size());
return DUCKPACKET_ERR_SIZE_INVALID;
Expand All @@ -238,7 +238,7 @@ int Duck::sendData(byte topic, std::vector<byte> data,
if (err != DUCK_ERR_NONE) {
return err;
}
std::vector<byte> vBuffer(txPacket->getBuffer().size());

err = duckRadio.sendData(txPacket->getBuffer());

CdpPacket packet = CdpPacket(txPacket->getBuffer());
Expand All @@ -264,7 +264,7 @@ int Duck::sendData(byte topic, std::vector<byte> data,
return err;
}

muidStatus Duck::getMuidStatus(const std::array<byte,8> & muid) const {
muidStatus Duck::getMuidStatus(const std::array<byte,4> & muid) const {
if (duckutils::isEqual(muid, lastMessageMuid)) {
if (lastMessageAck) {
return muidStatus::acked;
Expand Down
4 changes: 2 additions & 2 deletions src/Ducks/PapaDuck.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../PapaDuck.h"
#include <cassert>
int PapaDuck::setupWithDefaults(std::vector<byte> deviceId, std::string ssid, std::string password) {
int PapaDuck::setupWithDefaults(std::array<byte,8> deviceId, std::string ssid, std::string password) {
loginfo_ln("setupWithDefaults...");

int err = Duck::setupWithDefaults(deviceId, ssid, password);
Expand Down Expand Up @@ -222,7 +222,7 @@ void PapaDuck::sendCommand(byte cmd, std::vector<byte> value) {
}
}

void PapaDuck::sendCommand(byte cmd, std::vector<byte> value, std::vector<byte> dduid) {
void PapaDuck::sendCommand(byte cmd, std::vector<byte> value, std::array<byte,8> dduid) {
loginfo_ln("Initiate sending command");
std::vector<byte> dataPayload;
dataPayload.push_back(cmd);
Expand Down
4 changes: 2 additions & 2 deletions src/PapaDuck.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PapaDuck : public Duck {
* @param password wifi password (defaults to an empty string if not provided)
* @returns DUCK_ERR_NONE if setup is successfull, an error code otherwise.
*/
int setupWithDefaults(std::vector<byte> deviceId, std::string ssid = "",
int setupWithDefaults(std::array<byte,8> deviceId, std::string ssid = "",
std::string password = "");

/**
Expand Down Expand Up @@ -99,7 +99,7 @@ class PapaDuck : public Duck {
* @param value contextual data to be used in executed command.
* @param dduid destination duck ID for command to be executed.
*/
void sendCommand(byte cmd, std::vector<byte> value, std::vector<byte> dduid);
void sendCommand(byte cmd, std::vector<byte> value, std::array<byte,8> dduid);

private:

Expand Down
16 changes: 11 additions & 5 deletions src/include/Duck.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Duck {
/**
* @brief setup the duck unique ID
*
* @param an 8 byte unique id
* @param an 8 byte array
* @return DUCK_ERR_NONE if successful, an error code otherwise
*/
int setDeviceId(std::array<byte,8>& id);
Expand All @@ -65,7 +65,13 @@ class Duck {
*/
int setDeviceId(std::string& id);

int Duck::setDeviceId(byte* id);
/**
* @brief setup the duck unique ID
*
* @param an 8 byte unique id
* @return DUCK_ERR_NONE if successful, an error code otherwise
*/
int setDeviceId(byte* id);
/**
* @brief Setup serial connection.
*
Expand Down Expand Up @@ -166,7 +172,7 @@ class Duck {
* @return DUCK_ERR_NONE if the data was send successfully, an error code
otherwise.
*/
int sendData(byte topic, std::vector<byte> bytes,
int sendData(byte topic, std::vector<byte>& bytes,
const std::array<byte,8> targetDevice = ZERO_DUID, std::array<byte,8> * outgoingMuid = NULL);

/**
Expand Down Expand Up @@ -199,7 +205,7 @@ class Duck {
/**
* @brief Get the status of an MUID
*/
muidStatus getMuidStatus(const std::array<byte,8> & muid) const;
muidStatus getMuidStatus(const std::array<byte,4> & muid) const;

/**
* @brief Check wifi connection status
Expand Down Expand Up @@ -305,7 +311,7 @@ class Duck {

DuckPacket* txPacket = NULL;
DuckPacket* rxPacket = NULL;
std::array<byte,8> lastMessageMuid;
std::array<byte,4> lastMessageMuid;

bool lastMessageAck = true;
// Since this may be used to throttle outgoing packets, start out in a state
Expand Down
2 changes: 1 addition & 1 deletion src/include/DuckNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class DuckNet {

DuckPacket* txPacket = NULL;

std::vector<byte> deviceId;
std::array<byte,8> deviceId;

BloomFilter *bloomFilter = nullptr;

Expand Down
9 changes: 6 additions & 3 deletions src/include/DuckPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ class DuckPacket {
* @brief Construct a new Duck Packet object.
*
*/
DuckPacket() {buffer.reserve(229);}
DuckPacket() {
//buffer.reserve(229);
}

/**
* @brief Construct a new Duck Packet object.
Expand All @@ -51,7 +53,8 @@ class DuckPacket {
* @param filter a bloom filter
*/
DuckPacket(std::array<byte,8> duid)
: duid(duid) { buffer.reserve(229);}
: duid(duid) { //buffer.reserve(229);
}

~DuckPacket() {}
/**
Expand Down Expand Up @@ -100,7 +103,7 @@ class DuckPacket {
*
*/
void reset() {
std::vector<byte>().swap(buffer);
buffer.clear();
}

byte getTopic() { return buffer[TOPIC_POS]; }
Expand Down
8 changes: 4 additions & 4 deletions src/include/DuckUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ std::string toString(const std::vector<T>& vec) {
* @returns A std::string representing the byte array in ASCII.
*
*/
template<typename T>
std::string toString(const std::array<T,8>& arr) {
template<typename T,size_t S>
std::string toString(const std::array<T,S>& arr) {
std::string result;
for (const auto& element : arr) {
if (!std::isprint(element)) {
Expand All @@ -119,8 +119,8 @@ bool isEqual(const std::vector<T> & a, const std::vector<T> & b) {
return std::equal(a.begin(), a.end(), b.begin());
}

template<typename T>
bool isEqual(const std::array<T,8> & a, const std::array<T,8> & b) {
template<typename T,size_t S>
bool isEqual(const std::array<T,S> & a, const std::array<T,S> & b) {
if (a.size() != b.size()) {
return false;
}
Expand Down

0 comments on commit f3bc8ba

Please sign in to comment.