Skip to content

Commit 93600ef

Browse files
committed
Minor improvements to the style of the code.
1 parent 5f71b57 commit 93600ef

File tree

6 files changed

+71
-53
lines changed

6 files changed

+71
-53
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Johan Stokking <johan@thethingsnetwork.org>
22
Fokke Zandbergen <mail@fokkezb.nl>
33
Alessandro Blason <mrblason@gmail.com>
4+
JP Meijers <git@jpmeijers.com>

docs/TheThingsNetwork.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ See the [SendABP](https://github.com/TheThingsNetwork/arduino-device-lib/blob/ma
130130
131131
## Method: `setClass`
132132
133-
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.
133+
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.
134134
135135
```c
136136
bool setClass(lorawan_class p_lw_class);

examples/ReceiveClassC/ReceiveClassC.ino

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
#include <TheThingsNetwork.h>
22

33
// Set your AppEUI and AppKey
4-
//const char *appEui = "0004A30B001A5756";
5-
const char *appEui = "0004A30B001EC935";
6-
const char *appKey = "657CBC96D7E6F3D9CD9762CFF95A18E8";
4+
const char *appEui = "0000000000000000";
5+
const char *appKey = "00000000000000000000000000000000";
76

8-
9-
#define loraSerial Serial2
10-
#define debugSerial SerialUSB
7+
#define loraSerial Serial1
8+
#define debugSerial Serial
119

1210
// Replace REPLACE_ME with TTN_FP_EU868 or TTN_FP_US915
13-
#define freqPlan TTN_FP_EU868
11+
#define freqPlan REPLACE_ME
1412

1513
TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
1614

@@ -41,7 +39,7 @@ void loop()
4139
{
4240
debugSerial.println("-- LOOP");
4341

44-
// Send single byte to poll for incoming messages
42+
// Check for received data.
4543
ttn.poll();
4644

4745
// When using Class C we can poll as quickly as we can, as we only check the serial buffer.

keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ TheThingsNode KEYWORD1
1313
ttn_port_t KEYWORD1
1414
ttn_response_t KEYWORD1
1515
ttn_fp_t KEYWORD1
16-
lorawan_class KEYWORD1
16+
lorawan_class_t KEYWORD1
1717

1818
#######################################
1919
# Methods and Functions (KEYWORD2)

src/TheThingsNetwork.cpp

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -547,23 +547,34 @@ bool TheThingsNetwork::join(int8_t retries, uint32_t retryDelay)
547547
return false;
548548
}
549549

550-
bool TheThingsNetwork::setClass(lorawan_class p_lw_class)
550+
bool TheThingsNetwork::setClass(lorawan_class_t p_lw_class)
551551
{
552-
if(p_lw_class == CLASS_C) {
553-
bool result = sendMacSet(MAC_CLASS, "c");
554-
// Only remember Class C if set was successful.
555-
// Older firmware does not support class c, so keep on using Class A logic.
556-
if(result) lw_class = p_lw_class;
557-
return result;
558-
} else if(p_lw_class == CLASS_A) {
559-
lw_class = p_lw_class;
560-
return sendMacSet(MAC_CLASS, "a");
561-
} else {
552+
switch(p_lw_class)
553+
{
554+
555+
case CLASS_A:
556+
{
557+
lw_class = p_lw_class;
558+
return sendMacSet(MAC_CLASS, "a");
559+
}
560+
561+
// case CLASS_B: // Not yet supported. Use default case.
562+
563+
case CLASS_C:
564+
{
565+
bool result = sendMacSet(MAC_CLASS, "c");
566+
// Older firmware does not support Class C. If setting change fails, keep on using Class A.
567+
if(result) lw_class = p_lw_class;
568+
return result;
569+
}
570+
571+
default:
562572
return false;
573+
563574
}
564575
}
565576

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

622633
ttn_response_t TheThingsNetwork::poll(port_t port, bool confirm)
623634
{
624-
if(lw_class == CLASS_C) {
625-
// If class c, check rx buffer for any recevied data
635+
switch(lw_class)
636+
{
626637

627-
memset(buffer, 0, sizeof(buffer));
638+
case CLASS_A:
639+
{
640+
// Class A: send uplink and wait for rx windows
641+
uint8_t payload[] = {0x00};
642+
return sendBytes(payload, 1, port, confirm);
643+
}
628644

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

634-
if (pgmstrcmp(buffer, CMP_MAC_RX) == 0)
647+
case CLASS_C:
635648
{
636-
port_t downlinkPort = receivedPort(buffer + 7);
637-
char *data = buffer + 7 + digits(downlinkPort) + 1;
638-
size_t downlinkLength = strlen(data) / 2;
639-
if (downlinkLength > 0)
649+
// Class C: check rx buffer for any recevied data
650+
memset(buffer, 0, sizeof(buffer));
651+
652+
long timeout = this->modemStream->getTimeout();
653+
this->modemStream->setTimeout(100);
654+
this->modemStream->readBytesUntil('\n', buffer, sizeof(buffer));
655+
this->modemStream->setTimeout(timeout);
656+
657+
if (pgmstrcmp(buffer, CMP_MAC_RX) == 0)
640658
{
641-
uint8_t downlink[downlinkLength];
642-
for (size_t i = 0, d = 0; i < downlinkLength; i++, d += 2)
659+
port_t downlinkPort = receivedPort(buffer + 7);
660+
char *data = buffer + 7 + digits(downlinkPort) + 1;
661+
size_t downlinkLength = strlen(data) / 2;
662+
if (downlinkLength > 0)
643663
{
644-
downlink[i] = TTN_HEX_PAIR_TO_BYTE(data[d], data[d + 1]);
645-
}
646-
if (messageCallback)
647-
{
648-
messageCallback(downlink, downlinkLength, downlinkPort);
664+
uint8_t downlink[downlinkLength];
665+
for (size_t i = 0, d = 0; i < downlinkLength; i++, d += 2)
666+
{
667+
downlink[i] = TTN_HEX_PAIR_TO_BYTE(data[d], data[d + 1]);
668+
}
669+
if (messageCallback)
670+
{
671+
messageCallback(downlink, downlinkLength, downlinkPort);
672+
}
649673
}
674+
return TTN_SUCCESSFUL_RECEIVE;
650675
}
651-
return TTN_SUCCESSFUL_RECEIVE;
652676
}
653-
}
654-
else if(lw_class == CLASS_A) {
655-
// If class a send uplink and wait for rx windows
656-
uint8_t payload[] = {0x00};
657-
return sendBytes(payload, 1, port, confirm);
658-
}
659-
else {
677+
678+
default:
660679
return TTN_UNSUCESSFUL_RECEIVE;
661680
}
662681
}

src/TheThingsNetwork.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ enum ttn_fp_t
4848
TTN_FP_IN865_867
4949
};
5050

51-
enum lorawan_class
51+
enum lorawan_class_t
5252
{
5353
CLASS_A,
5454
CLASS_B,
@@ -67,7 +67,7 @@ class TheThingsNetwork
6767
char buffer[512];
6868
bool baudDetermined = false;
6969
void (*messageCallback)(const uint8_t *payload, size_t size, port_t port);
70-
lorawan_class lw_class = CLASS_A;
70+
lorawan_class_t lw_class = CLASS_A;
7171

7272
void clearReadBuffer();
7373
size_t readLine(char *buffer, size_t size, uint8_t attempts = 3);
@@ -108,11 +108,11 @@ class TheThingsNetwork
108108
uint16_t getVDD();
109109
void onMessage(void (*cb)(const uint8_t *payload, size_t size, port_t port));
110110
bool provision(const char *appEui, const char *appKey);
111-
bool join(const char *appEui, const char *appKey, int8_t retries = -1, uint32_t retryDelay = 10000, lorawan_class = CLASS_A);
111+
bool join(const char *appEui, const char *appKey, int8_t retries = -1, uint32_t retryDelay = 10000, lorawan_class_t = CLASS_A);
112112
bool join(int8_t retries = -1, uint32_t retryDelay = 10000);
113113
bool personalize(const char *devAddr, const char *nwkSKey, const char *appSKey);
114114
bool personalize();
115-
bool setClass(lorawan_class p_lw_class);
115+
bool setClass(lorawan_class_t p_lw_class);
116116
ttn_response_t sendBytes(const uint8_t *payload, size_t length, port_t port = 1, bool confirm = false, uint8_t sf = 0);
117117
ttn_response_t poll(port_t port = 1, bool confirm = false);
118118
void sleep(uint32_t mseconds);

0 commit comments

Comments
 (0)