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

Commit ed52d30

Browse files
committed
Add isValid
Add isValid function to checks if the buffer is a CayenneLPP valid payload.
1 parent 4ee7439 commit ed52d30

File tree

7 files changed

+83
-12
lines changed

7 files changed

+83
-12
lines changed

docs/CayenneLPPDecode.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,11 @@ Decode buffer to the JsonObject root, check [ArduinoJson](https://github.com/bbl
4040
void decode(JsonObject &root);
4141
```
4242

43+
## Method: `isValid`
4344

45+
This function checks if the buffer is a CayenneLPP valid payload.
46+
47+
```c
48+
bool isValid();
49+
```
4450

examples/Decode/Decode.ino

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
#include <CayenneLPPDecode.h>
1414

15-
void setup() {
15+
void setup()
16+
{
1617
DynamicJsonDocument jsonBuffer(512);
1718
CayenneLPP lpp(64);
1819
CayenneLPPDecode lppd;
@@ -37,12 +38,14 @@ void setup() {
3738
lpp.addGPS(1, -12.34f, 45.56f, 9.01f);
3839

3940
lppd.write(lpp.getBuffer(), lpp.getSize());
40-
41-
lppd.decode(root);
42-
serializeJsonPretty(root ,Serial);
43-
Serial.println();
41+
if (lppd.isValid())
42+
{
43+
lppd.decode(root);
44+
serializeJsonPretty(root, Serial);
45+
Serial.println();
46+
}
4447
}
4548

46-
void loop() {
47-
49+
void loop()
50+
{
4851
}

examples/DecodeLoRaDuplex/DecodeLoRaDuplex.ino

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,16 @@ void onReceive(int packetSize) {
8888
lppd.write(LoRa.read());
8989
}
9090

91-
lppd.decode(root);
92-
9391
Serial.print("Receive: ");
9492
Serial.println();
9593

96-
serializeJsonPretty(root ,Serial);
97-
Serial.println();
94+
if (lppd.isValid())
95+
{
96+
lppd.decode(root);
97+
98+
serializeJsonPretty(root, Serial);
99+
Serial.println();
100+
}
98101
}
99102

100103
boolean runEvery(unsigned long interval)

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ addBarometricPressure KEYWORD2
3030
addGyrometer KEYWORD2
3131
addGPS KEYWORD2
3232

33+
isValid KEYWORD2
3334
decode KEYWORD2
3435
dumpBuffer KEYWORD2
3536

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=CayenneLPPDecode
2-
version=1.0.2
2+
version=1.0.3
33
author=Luiz Henrique Cassettari & The Things Network
44
maintainer=Luiz Henrique Cassettari <ricaun@gmail.com>
55
sentence=Decode CayenneLPP to Json format as TTN.

src/CayenneLPPDecode.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,63 @@ void CayenneLPPDecode::reset(void)
9999
payload_position = 0;
100100
}
101101

102+
bool CayenneLPPDecode::isValid()
103+
{
104+
int i = 0;
105+
int len = payload_length;
106+
if (len < 2)
107+
return false;
108+
109+
while (i < len)
110+
{
111+
int channel = payload_buffer[i];
112+
int type = payload_buffer[i + 1];
113+
switch (type)
114+
{
115+
case LPP_DIGITAL_INPUT:
116+
case LPP_DIGITAL_OUTPUT:
117+
i += LPP_DIGITAL_OUTPUT_SIZE;
118+
break;
119+
case LPP_ANALOG_INPUT:
120+
case LPP_ANALOG_OUTPUT:
121+
i += LPP_ANALOG_OUTPUT_SIZE;
122+
break;
123+
case LPP_LUMINOSITY:
124+
i += LPP_LUMINOSITY_SIZE;
125+
break;
126+
case LPP_PRESENCE:
127+
i += LPP_PRESENCE_SIZE;
128+
break;
129+
case LPP_TEMPERATURE:
130+
i += LPP_TEMPERATURE_SIZE;
131+
break;
132+
case LPP_RELATIVE_HUMIDITY:
133+
i += LPP_RELATIVE_HUMIDITY_SIZE;
134+
break;
135+
case LPP_ACCELEROMETER:
136+
i += LPP_ACCELEROMETER_SIZE;
137+
break;
138+
case LPP_BAROMETRIC_PRESSURE:
139+
i += LPP_BAROMETRIC_PRESSURE_SIZE;
140+
break;
141+
case LPP_GYROMETER:
142+
i += LPP_GYROMETER_SIZE;
143+
break;
144+
case LPP_GPS:
145+
i += LPP_GPS_SIZE;
146+
break;
147+
default:
148+
return false;
149+
break;
150+
}
151+
}
152+
153+
if (i > len)
154+
return false;
155+
156+
return true;
157+
}
158+
102159
void CayenneLPPDecode::decode(JsonObject &_root)
103160
{
104161
JsonObject &root = (JsonObject &)_root;

src/CayenneLPPDecode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CayenneLPPDecode : public Stream
4141
void decode(JsonObject &_root);
4242

4343
void reset();
44+
bool isValid();
4445

4546
private:
4647
virtual int available();

0 commit comments

Comments
 (0)