2018-06-16 13:18:36 +02:00
|
|
|
|
|
|
|
#include "globals.h"
|
|
|
|
#include "payload.h"
|
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
PayloadConvert::PayloadConvert(uint8_t size) {
|
2018-06-16 13:18:36 +02:00
|
|
|
buffer = (uint8_t *)malloc(size);
|
|
|
|
cursor = 0;
|
|
|
|
}
|
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
PayloadConvert::~PayloadConvert(void) { free(buffer); }
|
2018-06-16 13:18:36 +02:00
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::reset(void) { cursor = 0; }
|
2018-06-16 13:18:36 +02:00
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
uint8_t PayloadConvert::getSize(void) { return cursor; }
|
2018-06-16 13:18:36 +02:00
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
uint8_t *PayloadConvert::getBuffer(void) { return buffer; }
|
2018-06-16 13:18:36 +02:00
|
|
|
|
2018-07-21 13:36:49 +02:00
|
|
|
/* ---------------- plain format without special encoding ---------- */
|
|
|
|
|
|
|
|
#if PAYLOAD_ENCODER == 1
|
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::addCount(uint16_t value1, uint16_t value2) {
|
2018-06-16 19:50:36 +02:00
|
|
|
buffer[cursor++] = value1 >> 8;
|
|
|
|
buffer[cursor++] = value1;
|
|
|
|
buffer[cursor++] = value2 >> 8;
|
|
|
|
buffer[cursor++] = value2;
|
2018-06-16 13:18:36 +02:00
|
|
|
}
|
|
|
|
|
2018-06-17 13:31:24 +02:00
|
|
|
#ifdef HAS_GPS
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::addGPS(gpsStatus_t value) {
|
2018-06-16 13:18:36 +02:00
|
|
|
buffer[cursor++] = value.latitude >> 24;
|
|
|
|
buffer[cursor++] = value.latitude >> 16;
|
|
|
|
buffer[cursor++] = value.latitude >> 8;
|
|
|
|
buffer[cursor++] = value.latitude;
|
|
|
|
buffer[cursor++] = value.longitude >> 24;
|
|
|
|
buffer[cursor++] = value.longitude >> 16;
|
|
|
|
buffer[cursor++] = value.longitude >> 8;
|
|
|
|
buffer[cursor++] = value.longitude;
|
|
|
|
buffer[cursor++] = value.satellites;
|
|
|
|
buffer[cursor++] = value.hdop >> 8;
|
|
|
|
buffer[cursor++] = value.hdop;
|
|
|
|
buffer[cursor++] = value.altitude >> 8;
|
|
|
|
buffer[cursor++] = value.altitude;
|
|
|
|
}
|
2018-06-17 13:31:24 +02:00
|
|
|
#endif
|
2018-06-16 13:18:36 +02:00
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::addConfig(configData_t value) {
|
2018-06-16 13:18:36 +02:00
|
|
|
buffer[cursor++] = value.lorasf;
|
2018-07-15 19:41:22 +02:00
|
|
|
buffer[cursor++] = value.txpower;
|
2018-06-16 13:18:36 +02:00
|
|
|
buffer[cursor++] = value.adrmode;
|
|
|
|
buffer[cursor++] = value.screensaver;
|
|
|
|
buffer[cursor++] = value.screenon;
|
|
|
|
buffer[cursor++] = value.countermode;
|
|
|
|
buffer[cursor++] = value.rssilimit >> 8;
|
|
|
|
buffer[cursor++] = value.rssilimit;
|
|
|
|
buffer[cursor++] = value.sendcycle;
|
|
|
|
buffer[cursor++] = value.wifichancycle;
|
|
|
|
buffer[cursor++] = value.blescantime;
|
|
|
|
buffer[cursor++] = value.blescan;
|
|
|
|
buffer[cursor++] = value.wifiant;
|
|
|
|
buffer[cursor++] = value.vendorfilter;
|
|
|
|
buffer[cursor++] = value.rgblum;
|
|
|
|
buffer[cursor++] = value.gpsmode;
|
|
|
|
memcpy(buffer + cursor, value.version, 10);
|
2018-06-16 19:50:36 +02:00
|
|
|
cursor += 10;
|
2018-06-16 13:18:36 +02:00
|
|
|
}
|
|
|
|
|
2018-07-21 17:14:27 +02:00
|
|
|
void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime,
|
|
|
|
float cputemp) {
|
2018-06-16 13:18:36 +02:00
|
|
|
buffer[cursor++] = voltage >> 8;
|
|
|
|
buffer[cursor++] = voltage;
|
|
|
|
buffer[cursor++] = uptime >> 56;
|
|
|
|
buffer[cursor++] = uptime >> 48;
|
|
|
|
buffer[cursor++] = uptime >> 40;
|
|
|
|
buffer[cursor++] = uptime >> 32;
|
|
|
|
buffer[cursor++] = uptime >> 24;
|
|
|
|
buffer[cursor++] = uptime >> 16;
|
|
|
|
buffer[cursor++] = uptime >> 8;
|
2018-06-16 19:50:36 +02:00
|
|
|
buffer[cursor++] = uptime;
|
|
|
|
buffer[cursor++] = (uint32_t)cputemp >> 24;
|
|
|
|
buffer[cursor++] = (uint32_t)cputemp >> 16;
|
|
|
|
buffer[cursor++] = (uint32_t)cputemp >> 8;
|
|
|
|
buffer[cursor++] = (uint32_t)cputemp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------- packed format with LoRa serialization Encoder ---------- */
|
2018-06-17 22:41:32 +02:00
|
|
|
// derived from
|
|
|
|
// https://github.com/thesolarnomad/lora-serialization/blob/master/src/LoraEncoder.cpp
|
2018-06-16 19:50:36 +02:00
|
|
|
|
2018-07-21 13:36:49 +02:00
|
|
|
#elif PAYLOAD_ENCODER == 2
|
2018-06-16 19:50:36 +02:00
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::addCount(uint16_t value1, uint16_t value2) {
|
2018-06-17 22:41:32 +02:00
|
|
|
writeUint16(value1);
|
|
|
|
writeUint16(value2);
|
2018-06-16 19:50:36 +02:00
|
|
|
}
|
|
|
|
|
2018-06-17 13:31:24 +02:00
|
|
|
#ifdef HAS_GPS
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::addGPS(gpsStatus_t value) {
|
2018-06-17 22:41:32 +02:00
|
|
|
writeLatLng(value.latitude, value.longitude);
|
|
|
|
writeUint8(value.satellites);
|
|
|
|
writeUint16(value.hdop);
|
|
|
|
writeUint16(value.altitude);
|
2018-06-16 19:50:36 +02:00
|
|
|
}
|
2018-06-17 13:31:24 +02:00
|
|
|
#endif
|
2018-06-16 19:50:36 +02:00
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::addConfig(configData_t value) {
|
2018-06-17 22:41:32 +02:00
|
|
|
writeUint8(value.lorasf);
|
2018-07-15 19:41:22 +02:00
|
|
|
writeUint8(value.txpower);
|
2018-06-17 22:41:32 +02:00
|
|
|
writeUint16(value.rssilimit);
|
|
|
|
writeUint8(value.sendcycle);
|
|
|
|
writeUint8(value.wifichancycle);
|
|
|
|
writeUint8(value.blescantime);
|
|
|
|
writeUint8(value.rgblum);
|
|
|
|
writeBitmap(value.adrmode ? true : false, value.screensaver ? true : false,
|
|
|
|
value.screenon ? true : false, value.countermode ? true : false,
|
|
|
|
value.blescan ? true : false, value.wifiant ? true : false,
|
|
|
|
value.vendorfilter ? true : false, value.gpsmode ? true : false);
|
|
|
|
}
|
|
|
|
|
2018-07-21 17:14:27 +02:00
|
|
|
void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime,
|
|
|
|
float cputemp) {
|
2018-06-17 22:41:32 +02:00
|
|
|
writeUint16(voltage);
|
2018-06-18 09:15:57 +02:00
|
|
|
writeUptime(uptime);
|
2018-06-17 22:41:32 +02:00
|
|
|
writeTemperature(cputemp);
|
|
|
|
}
|
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::intToBytes(uint8_t pos, int32_t i, uint8_t byteSize) {
|
2018-06-17 22:41:32 +02:00
|
|
|
for (uint8_t x = 0; x < byteSize; x++) {
|
2018-06-18 00:01:14 +02:00
|
|
|
buffer[x + pos] = (byte)(i >> (x * 8));
|
2018-06-17 22:41:32 +02:00
|
|
|
}
|
2018-06-18 00:01:14 +02:00
|
|
|
cursor += byteSize;
|
2018-06-17 22:41:32 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::writeUptime(uint64_t uptime) {
|
2018-06-18 09:15:57 +02:00
|
|
|
intToBytes(cursor, uptime, 8);
|
2018-06-17 22:41:32 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::writeLatLng(double latitude, double longitude) {
|
2018-06-18 09:15:57 +02:00
|
|
|
intToBytes(cursor, latitude, 4);
|
|
|
|
intToBytes(cursor, longitude, 4);
|
2018-06-17 22:41:32 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::writeUint16(uint16_t i) { intToBytes(cursor, i, 2); }
|
2018-06-17 22:41:32 +02:00
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::writeUint8(uint8_t i) { intToBytes(cursor, i, 1); }
|
2018-06-17 22:41:32 +02:00
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::writeHumidity(float humidity) {
|
2018-06-17 22:41:32 +02:00
|
|
|
int16_t h = (int16_t)(humidity * 100);
|
2018-06-18 09:15:57 +02:00
|
|
|
intToBytes(cursor, h, 2);
|
2018-06-17 22:41:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses a 16bit two's complement with two decimals, so the range is
|
|
|
|
* -327.68 to +327.67 degrees
|
|
|
|
*/
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::writeTemperature(float temperature) {
|
2018-06-17 22:41:32 +02:00
|
|
|
int16_t t = (int16_t)(temperature * 100);
|
|
|
|
if (temperature < 0) {
|
|
|
|
t = ~-t;
|
|
|
|
t = t + 1;
|
|
|
|
}
|
2018-06-18 00:39:46 +02:00
|
|
|
buffer[cursor++] = (byte)((t >> 8) & 0xFF);
|
|
|
|
buffer[cursor++] = (byte)t & 0xFF;
|
2018-06-17 22:41:32 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::writeBitmap(bool a, bool b, bool c, bool d, bool e, bool f,
|
2018-07-21 17:14:27 +02:00
|
|
|
bool g, bool h) {
|
2018-06-17 22:41:32 +02:00
|
|
|
uint8_t bitmap = 0;
|
|
|
|
// LSB first
|
|
|
|
bitmap |= (a & 1) << 7;
|
|
|
|
bitmap |= (b & 1) << 6;
|
|
|
|
bitmap |= (c & 1) << 5;
|
|
|
|
bitmap |= (d & 1) << 4;
|
|
|
|
bitmap |= (e & 1) << 3;
|
|
|
|
bitmap |= (f & 1) << 2;
|
|
|
|
bitmap |= (g & 1) << 1;
|
|
|
|
bitmap |= (h & 1) << 0;
|
|
|
|
writeUint8(bitmap);
|
2018-06-16 19:50:36 +02:00
|
|
|
}
|
|
|
|
|
2018-06-16 23:14:49 +02:00
|
|
|
/* ---------------- Cayenne LPP format ---------- */
|
2018-07-21 17:14:27 +02:00
|
|
|
// http://community.mydevices.com/t/cayenne-lpp-2-0/7510
|
2018-06-16 23:14:49 +02:00
|
|
|
|
2018-07-21 17:14:27 +02:00
|
|
|
#elif (PAYLOAD_ENCODER == 3 || PAYLOAD_ENCODER == 4)
|
2018-06-16 23:14:49 +02:00
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::addCount(uint16_t value1, uint16_t value2) {
|
2018-07-21 13:54:19 +02:00
|
|
|
uint16_t val1 = value1 * 100;
|
|
|
|
uint16_t val2 = value2 * 100;
|
2018-07-21 17:14:27 +02:00
|
|
|
#if (PAYLOAD_ENCODER == 3)
|
2018-06-16 23:14:49 +02:00
|
|
|
buffer[cursor++] = LPP_COUNT_WIFI_CHANNEL;
|
2018-07-21 17:14:27 +02:00
|
|
|
#endif
|
2018-06-17 13:24:20 +02:00
|
|
|
buffer[cursor++] = LPP_ANALOG_INPUT; // workaround, type meter not found?
|
2018-07-21 13:54:19 +02:00
|
|
|
buffer[cursor++] = val1 >> 8;
|
|
|
|
buffer[cursor++] = val1;
|
2018-07-21 17:14:27 +02:00
|
|
|
#if (PAYLOAD_ENCODER == 3)
|
2018-06-16 23:14:49 +02:00
|
|
|
buffer[cursor++] = LPP_COUNT_BLE_CHANNEL;
|
2018-07-21 17:14:27 +02:00
|
|
|
#endif
|
2018-06-17 13:24:20 +02:00
|
|
|
buffer[cursor++] = LPP_ANALOG_INPUT; // workaround, type meter not found?
|
2018-07-21 13:54:19 +02:00
|
|
|
buffer[cursor++] = val2 >> 8;
|
|
|
|
buffer[cursor++] = val2;
|
2018-06-16 23:14:49 +02:00
|
|
|
}
|
|
|
|
|
2018-06-17 13:31:24 +02:00
|
|
|
#ifdef HAS_GPS
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::addGPS(gpsStatus_t value) {
|
2018-06-17 11:07:29 +02:00
|
|
|
int32_t lat = value.latitude / 100;
|
|
|
|
int32_t lon = value.longitude / 100;
|
2018-07-21 13:36:49 +02:00
|
|
|
int32_t alt = value.altitude * 100;
|
2018-07-21 17:14:27 +02:00
|
|
|
#if (PAYLOAD_ENCODER == 3)
|
2018-06-16 23:14:49 +02:00
|
|
|
buffer[cursor++] = LPP_GPS_CHANNEL;
|
2018-07-21 17:14:27 +02:00
|
|
|
#endif
|
2018-06-16 23:14:49 +02:00
|
|
|
buffer[cursor++] = LPP_GPS;
|
|
|
|
buffer[cursor++] = lat >> 16;
|
|
|
|
buffer[cursor++] = lat >> 8;
|
|
|
|
buffer[cursor++] = lat;
|
|
|
|
buffer[cursor++] = lon >> 16;
|
|
|
|
buffer[cursor++] = lon >> 8;
|
|
|
|
buffer[cursor++] = lon;
|
|
|
|
buffer[cursor++] = alt >> 16;
|
|
|
|
buffer[cursor++] = alt >> 8;
|
|
|
|
buffer[cursor++] = alt;
|
|
|
|
}
|
2018-06-17 13:31:24 +02:00
|
|
|
#endif
|
2018-06-16 23:14:49 +02:00
|
|
|
|
2018-07-19 22:33:37 +02:00
|
|
|
void PayloadConvert::addConfig(configData_t value) {
|
2018-07-21 17:14:27 +02:00
|
|
|
#if (PAYLOAD_ENCODER == 3)
|
2018-06-16 23:14:49 +02:00
|
|
|
buffer[cursor++] = LPP_ADR_CHANNEL;
|
2018-07-21 17:14:27 +02:00
|
|
|
#endif
|
2018-06-16 23:14:49 +02:00
|
|
|
buffer[cursor++] = LPP_DIGITAL_INPUT;
|
|
|
|
buffer[cursor++] = value.adrmode;
|
|
|
|
}
|
|
|
|
|
2018-07-21 17:14:27 +02:00
|
|
|
void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime,
|
|
|
|
float celsius) {
|
2018-07-21 13:36:49 +02:00
|
|
|
int16_t val = celsius * 10;
|
2018-07-21 17:14:27 +02:00
|
|
|
#if (PAYLOAD_ENCODER == 3)
|
2018-06-16 23:14:49 +02:00
|
|
|
buffer[cursor++] = LPP_BATT_CHANNEL;
|
2018-07-21 17:14:27 +02:00
|
|
|
#endif
|
2018-06-16 23:14:49 +02:00
|
|
|
buffer[cursor++] = LPP_ANALOG_INPUT;
|
|
|
|
buffer[cursor++] = voltage >> 8;
|
|
|
|
buffer[cursor++] = voltage;
|
2018-07-21 17:14:27 +02:00
|
|
|
#if (PAYLOAD_ENCODER == 3)
|
2018-06-16 23:14:49 +02:00
|
|
|
buffer[cursor++] = LPP_TEMP_CHANNEL;
|
2018-07-21 17:14:27 +02:00
|
|
|
#endif
|
2018-06-16 23:14:49 +02:00
|
|
|
buffer[cursor++] = LPP_TEMPERATURE;
|
2018-07-21 13:36:49 +02:00
|
|
|
buffer[cursor++] = (uint16_t)val >> 8;
|
|
|
|
buffer[cursor++] = (uint16_t)val;
|
2018-07-19 22:33:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error "No valid payload converter defined"
|
2018-07-21 17:14:27 +02:00
|
|
|
#endif
|