Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Add isValid
Browse files Browse the repository at this point in the history
Add isValid function to checks if the buffer is a CayenneLPP valid payload.
  • Loading branch information
ricaun committed May 10, 2019
1 parent 4ee7439 commit ed52d30
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 12 deletions.
6 changes: 6 additions & 0 deletions docs/CayenneLPPDecode.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,11 @@ Decode buffer to the JsonObject root, check [ArduinoJson](https://github.com/bbl
void decode(JsonObject &root);
```

## Method: `isValid`

This function checks if the buffer is a CayenneLPP valid payload.

```c
bool isValid();
```

17 changes: 10 additions & 7 deletions examples/Decode/Decode.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

#include <CayenneLPPDecode.h>

void setup() {
void setup()
{
DynamicJsonDocument jsonBuffer(512);
CayenneLPP lpp(64);
CayenneLPPDecode lppd;
Expand All @@ -37,12 +38,14 @@ void setup() {
lpp.addGPS(1, -12.34f, 45.56f, 9.01f);

lppd.write(lpp.getBuffer(), lpp.getSize());

lppd.decode(root);
serializeJsonPretty(root ,Serial);
Serial.println();
if (lppd.isValid())
{
lppd.decode(root);
serializeJsonPretty(root, Serial);
Serial.println();
}
}

void loop() {

void loop()
{
}
11 changes: 7 additions & 4 deletions examples/DecodeLoRaDuplex/DecodeLoRaDuplex.ino
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,16 @@ void onReceive(int packetSize) {
lppd.write(LoRa.read());
}

lppd.decode(root);

Serial.print("Receive: ");
Serial.println();

serializeJsonPretty(root ,Serial);
Serial.println();
if (lppd.isValid())
{
lppd.decode(root);

serializeJsonPretty(root, Serial);
Serial.println();
}
}

boolean runEvery(unsigned long interval)
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ addBarometricPressure KEYWORD2
addGyrometer KEYWORD2
addGPS KEYWORD2

isValid KEYWORD2
decode KEYWORD2
dumpBuffer KEYWORD2

Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=CayenneLPPDecode
version=1.0.2
version=1.0.3
author=Luiz Henrique Cassettari & The Things Network
maintainer=Luiz Henrique Cassettari <ricaun@gmail.com>
sentence=Decode CayenneLPP to Json format as TTN.
Expand Down
57 changes: 57 additions & 0 deletions src/CayenneLPPDecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,63 @@ void CayenneLPPDecode::reset(void)
payload_position = 0;
}

bool CayenneLPPDecode::isValid()
{
int i = 0;
int len = payload_length;
if (len < 2)
return false;

while (i < len)
{
int channel = payload_buffer[i];
int type = payload_buffer[i + 1];
switch (type)
{
case LPP_DIGITAL_INPUT:
case LPP_DIGITAL_OUTPUT:
i += LPP_DIGITAL_OUTPUT_SIZE;
break;
case LPP_ANALOG_INPUT:
case LPP_ANALOG_OUTPUT:
i += LPP_ANALOG_OUTPUT_SIZE;
break;
case LPP_LUMINOSITY:
i += LPP_LUMINOSITY_SIZE;
break;
case LPP_PRESENCE:
i += LPP_PRESENCE_SIZE;
break;
case LPP_TEMPERATURE:
i += LPP_TEMPERATURE_SIZE;
break;
case LPP_RELATIVE_HUMIDITY:
i += LPP_RELATIVE_HUMIDITY_SIZE;
break;
case LPP_ACCELEROMETER:
i += LPP_ACCELEROMETER_SIZE;
break;
case LPP_BAROMETRIC_PRESSURE:
i += LPP_BAROMETRIC_PRESSURE_SIZE;
break;
case LPP_GYROMETER:
i += LPP_GYROMETER_SIZE;
break;
case LPP_GPS:
i += LPP_GPS_SIZE;
break;
default:
return false;
break;
}
}

if (i > len)
return false;

return true;
}

void CayenneLPPDecode::decode(JsonObject &_root)
{
JsonObject &root = (JsonObject &)_root;
Expand Down
1 change: 1 addition & 0 deletions src/CayenneLPPDecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class CayenneLPPDecode : public Stream
void decode(JsonObject &_root);

void reset();
bool isValid();

private:
virtual int available();
Expand Down

0 comments on commit ed52d30

Please sign in to comment.