Skip to content

Commit

Permalink
Minor improvements to the style of the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmeijers committed May 8, 2020
1 parent 5f71b57 commit 93600ef
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 53 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Johan Stokking <johan@thethingsnetwork.org>
Fokke Zandbergen <mail@fokkezb.nl>
Alessandro Blason <mrblason@gmail.com>
JP Meijers <git@jpmeijers.com>
2 changes: 1 addition & 1 deletion docs/TheThingsNetwork.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ See the [SendABP](https://github.com/TheThingsNetwork/arduino-device-lib/blob/ma
## Method: `setClass`
Change the downlink receive LoRaWAN Class. Class C is only supported in firmware version 1.0.5 and up. For other firmware versions this method will have nto affect.
Change the downlink receive LoRaWAN Class. Class C is only supported in firmware version 1.0.5 and up. For other firmware versions this method will have no effect.
```c
bool setClass(lorawan_class p_lw_class);
Expand Down
14 changes: 6 additions & 8 deletions examples/ReceiveClassC/ReceiveClassC.ino
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#include <TheThingsNetwork.h>

// Set your AppEUI and AppKey
//const char *appEui = "0004A30B001A5756";
const char *appEui = "0004A30B001EC935";
const char *appKey = "657CBC96D7E6F3D9CD9762CFF95A18E8";
const char *appEui = "0000000000000000";
const char *appKey = "00000000000000000000000000000000";


#define loraSerial Serial2
#define debugSerial SerialUSB
#define loraSerial Serial1
#define debugSerial Serial

// Replace REPLACE_ME with TTN_FP_EU868 or TTN_FP_US915
#define freqPlan TTN_FP_EU868
#define freqPlan REPLACE_ME

TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);

Expand Down Expand Up @@ -41,7 +39,7 @@ void loop()
{
debugSerial.println("-- LOOP");

// Send single byte to poll for incoming messages
// Check for received data.
ttn.poll();

// When using Class C we can poll as quickly as we can, as we only check the serial buffer.
Expand Down
2 changes: 1 addition & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TheThingsNode KEYWORD1
ttn_port_t KEYWORD1
ttn_response_t KEYWORD1
ttn_fp_t KEYWORD1
lorawan_class KEYWORD1
lorawan_class_t KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
Expand Down
97 changes: 58 additions & 39 deletions src/TheThingsNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,23 +547,34 @@ bool TheThingsNetwork::join(int8_t retries, uint32_t retryDelay)
return false;
}

bool TheThingsNetwork::setClass(lorawan_class p_lw_class)
bool TheThingsNetwork::setClass(lorawan_class_t p_lw_class)
{
if(p_lw_class == CLASS_C) {
bool result = sendMacSet(MAC_CLASS, "c");
// Only remember Class C if set was successful.
// Older firmware does not support class c, so keep on using Class A logic.
if(result) lw_class = p_lw_class;
return result;
} else if(p_lw_class == CLASS_A) {
lw_class = p_lw_class;
return sendMacSet(MAC_CLASS, "a");
} else {
switch(p_lw_class)
{

case CLASS_A:
{
lw_class = p_lw_class;
return sendMacSet(MAC_CLASS, "a");
}

// case CLASS_B: // Not yet supported. Use default case.

case CLASS_C:
{
bool result = sendMacSet(MAC_CLASS, "c");
// Older firmware does not support Class C. If setting change fails, keep on using Class A.
if(result) lw_class = p_lw_class;
return result;
}

default:
return false;

}
}

bool TheThingsNetwork::join(const char *appEui, const char *appKey, int8_t retries, uint32_t retryDelay, lorawan_class p_lw_class)
bool TheThingsNetwork::join(const char *appEui, const char *appKey, int8_t retries, uint32_t retryDelay, lorawan_class_t p_lw_class)
{
return provision(appEui, appKey) && join(retries, retryDelay) && setClass(p_lw_class);
}
Expand Down Expand Up @@ -621,42 +632,50 @@ ttn_response_t TheThingsNetwork::sendBytes(const uint8_t *payload, size_t length

ttn_response_t TheThingsNetwork::poll(port_t port, bool confirm)
{
if(lw_class == CLASS_C) {
// If class c, check rx buffer for any recevied data
switch(lw_class)
{

memset(buffer, 0, sizeof(buffer));
case CLASS_A:
{
// Class A: send uplink and wait for rx windows
uint8_t payload[] = {0x00};
return sendBytes(payload, 1, port, confirm);
}

long timeout = this->modemStream->getTimeout();
this->modemStream->setTimeout(100);
this->modemStream->readBytesUntil('\n', buffer, sizeof(buffer));
this->modemStream->setTimeout(timeout);
// case CLASS_B: // Not yet supported. Use default case.

if (pgmstrcmp(buffer, CMP_MAC_RX) == 0)
case CLASS_C:
{
port_t downlinkPort = receivedPort(buffer + 7);
char *data = buffer + 7 + digits(downlinkPort) + 1;
size_t downlinkLength = strlen(data) / 2;
if (downlinkLength > 0)
// Class C: check rx buffer for any recevied data
memset(buffer, 0, sizeof(buffer));

long timeout = this->modemStream->getTimeout();
this->modemStream->setTimeout(100);
this->modemStream->readBytesUntil('\n', buffer, sizeof(buffer));
this->modemStream->setTimeout(timeout);

if (pgmstrcmp(buffer, CMP_MAC_RX) == 0)
{
uint8_t downlink[downlinkLength];
for (size_t i = 0, d = 0; i < downlinkLength; i++, d += 2)
port_t downlinkPort = receivedPort(buffer + 7);
char *data = buffer + 7 + digits(downlinkPort) + 1;
size_t downlinkLength = strlen(data) / 2;
if (downlinkLength > 0)
{
downlink[i] = TTN_HEX_PAIR_TO_BYTE(data[d], data[d + 1]);
}
if (messageCallback)
{
messageCallback(downlink, downlinkLength, downlinkPort);
uint8_t downlink[downlinkLength];
for (size_t i = 0, d = 0; i < downlinkLength; i++, d += 2)
{
downlink[i] = TTN_HEX_PAIR_TO_BYTE(data[d], data[d + 1]);
}
if (messageCallback)
{
messageCallback(downlink, downlinkLength, downlinkPort);
}
}
return TTN_SUCCESSFUL_RECEIVE;
}
return TTN_SUCCESSFUL_RECEIVE;
}
}
else if(lw_class == CLASS_A) {
// If class a send uplink and wait for rx windows
uint8_t payload[] = {0x00};
return sendBytes(payload, 1, port, confirm);
}
else {

default:
return TTN_UNSUCESSFUL_RECEIVE;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/TheThingsNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ enum ttn_fp_t
TTN_FP_IN865_867
};

enum lorawan_class
enum lorawan_class_t
{
CLASS_A,
CLASS_B,
Expand All @@ -67,7 +67,7 @@ class TheThingsNetwork
char buffer[512];
bool baudDetermined = false;
void (*messageCallback)(const uint8_t *payload, size_t size, port_t port);
lorawan_class lw_class = CLASS_A;
lorawan_class_t lw_class = CLASS_A;

void clearReadBuffer();
size_t readLine(char *buffer, size_t size, uint8_t attempts = 3);
Expand Down Expand Up @@ -108,11 +108,11 @@ class TheThingsNetwork
uint16_t getVDD();
void onMessage(void (*cb)(const uint8_t *payload, size_t size, port_t port));
bool provision(const char *appEui, const char *appKey);
bool join(const char *appEui, const char *appKey, int8_t retries = -1, uint32_t retryDelay = 10000, lorawan_class = CLASS_A);
bool join(const char *appEui, const char *appKey, int8_t retries = -1, uint32_t retryDelay = 10000, lorawan_class_t = CLASS_A);
bool join(int8_t retries = -1, uint32_t retryDelay = 10000);
bool personalize(const char *devAddr, const char *nwkSKey, const char *appSKey);
bool personalize();
bool setClass(lorawan_class p_lw_class);
bool setClass(lorawan_class_t p_lw_class);
ttn_response_t sendBytes(const uint8_t *payload, size_t length, port_t port = 1, bool confirm = false, uint8_t sf = 0);
ttn_response_t poll(port_t port = 1, bool confirm = false);
void sleep(uint32_t mseconds);
Expand Down

0 comments on commit 93600ef

Please sign in to comment.