Skip to content

Commit ad73bc7

Browse files
Merge pull request #109 from TheThingsNetwork/optimizeTypes
Optimize types with a specific bit-size
2 parents beb1bb9 + 946b953 commit ad73bc7

File tree

6 files changed

+71
-68
lines changed

6 files changed

+71
-68
lines changed

API.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ Include and instantiate the TheThingsNetwork class. The constructor initialize t
66
```c
77
#include <TheThingsNetwork.h>
88

9-
TheThingsNetwork ttn(Stream& modemStream, Stream& debugStream, fp_ttn_t fp, int sf = 7, int fsb = 2);
9+
TheThingsNetwork ttn(Stream& modemStream, Stream& debugStream, fp_ttn_t fp, uint8_t sf = 7, uint8_t fsb = 2);
1010
```
1111
1212
- `Stream& modemStream`: Stream for the LoRa modem (for The Things Node/Uno use `Serial1` and data rate `57600`).
1313
- `Stream& debugStream`: Stream to write debug logs to (for The Things Node/Uno use `Serial` and data rate `9600`).
1414
- `fp_ttn_fp fp`: The frequency plan: `TTN_FP_EU868` or `TTN_FP_US915` depending on the region you deploy in.
15-
- `int sf = 7`: Optional custom spreading factor. Can be `7` to `12` for `TTN_FP_EU868` and `7` to `12` for `TTN_FP_US915`. Defaults to `7`.
16-
- `int fsb = 2`: Optional custom front-side bus. Can be `1` to `8`. Defaults to `2`.
15+
- `uint8_t sf = 7`: Optional custom spreading factor. Can be `7` to `12` for `TTN_FP_EU868` and `7` to `12` for `TTN_FP_US915`. Defaults to `7`.
16+
- `uint8_t fsb = 2`: Optional custom front-side bus. Can be `1` to `8`. Defaults to `2`.
1717
1818
## Method: showStatus
1919
Writes information about the device and LoRa module to `debugStream`.
@@ -42,27 +42,27 @@ See the [DeviceInfo](https://github.com/TheThingsNetwork/arduino-device-lib/blob
4242
Sets a function which will be called to process incoming messages.
4343

4444
```c
45-
void onMessage(void (*cb)(const byte* payload, int length, int port));
45+
void onMessage(void (*cb)(const byte* payload, size_t length, port_t port));
4646
```
4747
4848
- `const byte* payload`: Bytes received.
49-
- `int length`: Number of bytes.
50-
- `int port`: The port addressed.
49+
- `size_t length`: Number of bytes.
50+
- `port_t port`: The port addressed.
5151
5252
See the [Receive](https://github.com/TheThingsNetwork/arduino-device-lib/blob/master/examples/Receive/Receive.ino) example.
5353
5454
## Method: join
5555
Activate the device via OTAA (default).
5656
5757
```c
58-
bool join(const byte appEui[8], const byte appKey[16], int retries = -1, long int retryDelay = 10000);
59-
bool join(int retries = -1, long int retryDelay = 10000);
58+
bool join(const byte appEui[8], const byte appKey[16], int8_t retries = -1, uint32_t retryDelay = 10000);
59+
bool join(int8_t retries = -1, uint32_t retryDelay = 10000);
6060
```
6161

6262
- `const byte appEui[8]`: Application EUI the device is registered to.
6363
- `const byte appKey[16]`: Application Key assigned to the device.
64-
- `int retries = -1`: Number of times to retry after failed or unconfirmed join. Defaults to `-1` which means infinite.
65-
- `long int retryDelay = 10000`: Delay in ms between attempts. Defaults to 10 seconds.
64+
- `int8_t retries = -1`: Number of times to retry after failed or unconfirmed join. Defaults to `-1` which means infinite.
65+
- `uint32_t retryDelay = 10000`: Delay in ms between attempts. Defaults to 10 seconds.
6666

6767
Returns `true` or `false` depending on whether it received confirmation that the activation was successful before the maximum number of attempts.
6868

@@ -90,21 +90,21 @@ See the [ABP](https://github.com/TheThingsNetwork/arduino-device-lib/blob/master
9090
Send a message to the application using raw bytes.
9191
9292
```c
93-
int sendBytes(const byte* payload, int length, int port = 1, bool confirm = false);
93+
int sendBytes(const byte* payload, size_t length, port_t port = 1, bool confirm = false);
9494
```
9595

9696
- `const byte* payload `: Bytes to send.
97-
- `int length`: The number of bytes. Use `sizeof(payload)` to get it.
98-
- `int port = 1`: The port to address. Defaults to `1`.
97+
- `size_t length`: The number of bytes. Use `sizeof(payload)` to get it.
98+
- `port_t port = 1`: The port to address. Defaults to `1`.
9999
- `bool confirm = false`: Whether to ask for confirmation. Defaults to `false`.
100100

101-
Returns a success or error code and logs the related error message:
101+
Returns a success or error code and logs the related error message:
102102

103103
* `-1`: Send command failed.
104104
* `-2`: Time-out.
105105
* `1`: Successful transmission.
106106
* `2`: Successful transmission. Received \<N> bytes
107-
* `-10`: Unexpected response: \<Response>
107+
* `-10`: Unexpected response: \<Response>
108108

109109
See the [Send](https://github.com/TheThingsNetwork/arduino-device-lib/blob/master/examples/Send/Send.ino) example.
110110

@@ -113,7 +113,7 @@ Also in sendBytes, due to TTN's 30 second fair access policy, we update the airt
113113
## Method: poll
114114
Calls `sendBytes()` with `{ 0x00 }` as payload to poll for incoming messages.
115115

116-
- `int port = 1`: The port to address. Defaults to `1`.
116+
- `port_t port = 1`: The port to address. Defaults to `1`.
117117
- `bool confirm = false`: Whether to ask for confirmation.
118118

119119
Returns the result of `sendBytes()`.
@@ -124,7 +124,7 @@ See the [Receive](https://github.com/TheThingsNetwork/arduino-device-lib/blob/ma
124124
Sets the information needed to activate the device via OTAA, without actually activating. Call join() without the first 2 arguments to activate.
125125

126126
```c
127-
bool provision(const byte appEui[8], const appKey[16]);
127+
bool provision(const byte appEui[8], const byte appKey[16]);
128128
```
129129
130130
- `const byte appEui[8]`: Application Identifier for the device.

examples/QuickStart/QuickStart.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void loop() {
3939
delay(10000);
4040
}
4141

42-
void message(const byte* payload, int length, int port) {
42+
void message(const byte* payload, size_t length, port_t port) {
4343
debugSerial.println("-- MESSAGE");
4444

4545
// Only handle messages of a single byte
@@ -50,7 +50,7 @@ void message(const byte* payload, int length, int port) {
5050
if (payload[0] == 0) {
5151
debugSerial.println("LED: off");
5252
digitalWrite(LED_BUILTIN, LOW);
53-
53+
5454
} else if (payload[0] == 1) {
5555
debugSerial.println("LED: on");
5656
digitalWrite(LED_BUILTIN, HIGH);

examples/Receive/Receive.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void loop() {
3535
delay(10000);
3636
}
3737

38-
void message(const byte* payload, int length, int port) {
38+
void message(const byte* payload, size_t length, port_t port) {
3939
debugSerial.println("-- MESSAGE");
4040
debugSerial.print("Received " + String(length) + " bytes on port " + String(port) + ":");
4141

examples/Workshop/Workshop.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void setup() {
3838
}
3939

4040
void loop() {
41-
// Create a buffer with three bytes
41+
// Create a buffer with three bytes
4242
byte payload[3] = { 0x01, 0x02, 0x03 };
4343

4444
// Send it to the network

src/TheThingsNetwork.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ bool TheThingsNetwork::sendCommand(String cmd) {
4444
}
4545

4646
bool TheThingsNetwork::sendCommand(String cmd, String value) {
47-
int l = value.length();
47+
size_t l = value.length();
4848
byte buf[l];
4949
value.getBytes(buf, l);
5050

@@ -61,10 +61,10 @@ char btohexa_low(unsigned char b) {
6161
return (b > 0x9u) ? b + 'A' - 10 : b + '0';
6262
}
6363

64-
bool TheThingsNetwork::sendCommand(String cmd, const byte *buf, int length) {
64+
bool TheThingsNetwork::sendCommand(String cmd, const byte *buf, size_t length) {
6565
String str = cmd + " ";
6666

67-
for (int i = 0; i < length; i++) {
67+
for (size_t i = 0; i < length; i++) {
6868
str += btohexa_high(buf[i]);
6969
str += btohexa_low(buf[i]);
7070
}
@@ -96,7 +96,7 @@ void TheThingsNetwork::reset(bool adr) {
9696
sendCommand(str);
9797
}
9898

99-
void TheThingsNetwork::onMessage(void (*cb)(const byte* payload, int length, int port)) {
99+
void TheThingsNetwork::onMessage(void (*cb)(const byte* payload, size_t length, port_t port)) {
100100
this->messageCallback = cb;
101101
}
102102

@@ -130,7 +130,7 @@ bool TheThingsNetwork::provision(const byte appEui[8], const byte appKey[16]) {
130130
return sendCommand(F("mac save"));
131131
}
132132

133-
bool TheThingsNetwork::join(int retries, long int retryDelay) {
133+
bool TheThingsNetwork::join(int8_t retries, uint32_t retryDelay) {
134134
configureChannels(this->sf, this->fsb);
135135
String devEui = readValue(F("sys get hweui"));
136136
String str = "";
@@ -163,13 +163,13 @@ bool TheThingsNetwork::join(int retries, long int retryDelay) {
163163
return false;
164164
}
165165

166-
bool TheThingsNetwork::join(const byte appEui[8], const byte appKey[16], int retries, long int retryDelay) {
166+
bool TheThingsNetwork::join(const byte appEui[8], const byte appKey[16], int8_t retries, uint32_t retryDelay) {
167167
reset();
168168
provision(appEui, appKey);
169169
return join(retries, retryDelay);
170170
}
171171

172-
int TheThingsNetwork::sendBytes(const byte* payload, int length, int port, bool confirm) {
172+
int TheThingsNetwork::sendBytes(const byte* payload, size_t length, port_t port, bool confirm) {
173173
String str = "";
174174
str.concat(F("mac tx "));
175175
str.concat(confirm ? F("cnf ") : F("uncnf "));
@@ -193,12 +193,12 @@ int TheThingsNetwork::sendBytes(const byte* payload, int length, int port, bool
193193
return 1;
194194
}
195195
if (response.startsWith(F("mac_rx"))) {
196-
int portEnds = response.indexOf(" ", 7);
197-
int downlinkPort = response.substring(7, portEnds).toInt();
196+
uint8_t portEnds = response.indexOf(" ", 7);
197+
port_t downlinkPort = response.substring(7, portEnds).toInt();
198198
String data = response.substring(portEnds + 1);
199-
int downlinkLength = data.length() / 2;
199+
size_t downlinkLength = data.length() / 2;
200200
byte downlink[64];
201-
for (int i = 0, d = 0; i < downlinkLength; i++, d += 2) {
201+
for (size_t i = 0, d = 0; i < downlinkLength; i++, d += 2) {
202202
downlink[i] = TTN_HEX_PAIR_TO_BYTE(data[d], data[d+1]);
203203
}
204204
debugPrint(F("Successful transmission. Received "));
@@ -214,7 +214,7 @@ int TheThingsNetwork::sendBytes(const byte* payload, int length, int port, bool
214214
return -10;
215215
}
216216

217-
int TheThingsNetwork::poll(int port, bool confirm) {
217+
int TheThingsNetwork::poll(port_t port, bool confirm) {
218218
byte payload[] = { 0x00 };
219219
return sendBytes(payload, 1, port, confirm);
220220
}
@@ -227,7 +227,7 @@ void TheThingsNetwork::fillAirtimeInfo() {
227227
this->info.cr = 0;
228228
this->info.de = 0;
229229

230-
int i;
230+
uint8_t i;
231231
String message = readValue(F("radio get sf"));
232232
for (i = 2; message[i] && i <= 3; i++) {
233233
this->info.sf = (this->info.sf + message[i] - 48) * 10;
@@ -252,12 +252,12 @@ void TheThingsNetwork::fillAirtimeInfo() {
252252
this->info.de = this->info.sf >= 11 ? 1 : 0;
253253
}
254254

255-
void TheThingsNetwork::trackAirtime(int payloadSize) {
255+
void TheThingsNetwork::trackAirtime(size_t payloadSize) {
256256
payloadSize = 13 + payloadSize;
257257

258258
float Tsym = pow(2, this->info.sf) / this->info.band;
259259
float Tpreamble = (this->info.ps + 4.25) * Tsym;
260-
unsigned int payLoadSymbNb = 8 + (max(ceil((8 * payloadSize - 4 * this->info.sf + 28 + 16 - 20 * this->info.header) / (4 * (this->info.sf - 2 * this->info.de))) * (this->info.cr + 4), 0));
260+
uint16_t payLoadSymbNb = 8 + (max(ceil((8 * payloadSize - 4 * this->info.sf + 28 + 16 - 20 * this->info.header) / (4 * (this->info.sf - 2 * this->info.de))) * (this->info.cr + 4), 0));
261261
float Tpayload = payLoadSymbNb * Tsym;
262262
float Tpacket = Tpreamble + Tpayload;
263263
this->airtime = this->airtime + (Tpacket / 1000);
@@ -288,10 +288,10 @@ void TheThingsNetwork::showStatus() {
288288
debugPrintLn(F(" s"));
289289
}
290290

291-
void TheThingsNetwork::configureEU868(int sf) {
292-
int ch;
293-
int dr = -1;
294-
long int freq = 867100000;
291+
void TheThingsNetwork::configureEU868(uint8_t sf) {
292+
uint8_t ch;
293+
int8_t dr = -1;
294+
uint32_t freq = 867100000;
295295
String str = "";
296296

297297
str.concat(F("mac set rx2 3 869525000"));
@@ -360,13 +360,13 @@ void TheThingsNetwork::configureEU868(int sf) {
360360
}
361361
}
362362

363-
void TheThingsNetwork::configureUS915(int sf, int fsb) {
364-
int ch;
365-
int dr = -1;
363+
void TheThingsNetwork::configureUS915(uint8_t sf, uint8_t fsb) {
364+
uint8_t ch;
365+
int8_t dr = -1;
366366
String str = "";
367-
int chLow = fsb > 0 ? (fsb - 1) * 8 : 0;
368-
int chHigh = fsb > 0 ? chLow + 7 : 71;
369-
int ch500 = fsb + 63;
367+
uint8_t chLow = fsb > 0 ? (fsb - 1) * 8 : 0;
368+
uint8_t chHigh = fsb > 0 ? chLow + 7 : 71;
369+
uint8_t ch500 = fsb + 63;
370370

371371
sendCommand(F("radio set freq 904200000"));
372372
str = "";
@@ -419,7 +419,7 @@ void TheThingsNetwork::configureUS915(int sf, int fsb) {
419419
}
420420
}
421421

422-
void TheThingsNetwork::configureChannels(int sf, int fsb) {
422+
void TheThingsNetwork::configureChannels(uint8_t sf, uint8_t fsb) {
423423
switch (this->fp) {
424424
case TTN_FP_EU868:
425425
configureEU868(sf);
@@ -437,7 +437,7 @@ void TheThingsNetwork::configureChannels(int sf, int fsb) {
437437
sendCommand(retries);
438438
}
439439

440-
TheThingsNetwork::TheThingsNetwork(Stream& modemStream, Stream& debugStream, ttn_fp_t fp, int sf, int fsb) {
440+
TheThingsNetwork::TheThingsNetwork(Stream& modemStream, Stream& debugStream, ttn_fp_t fp, uint8_t sf, uint8_t fsb) {
441441
this->debugStream = &debugStream;
442442
this->modemStream = &modemStream;
443443
this->fp = fp;

src/TheThingsNetwork.h

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,60 @@
1414
#define TTN_PWRIDX_868 1
1515
#define TTN_PWRIDX_915 5
1616

17+
typedef uint8_t port_t;
18+
1719
enum ttn_fp_t {
1820
TTN_FP_EU868,
1921
TTN_FP_US915
2022
};
2123

2224
typedef struct airtime_s
2325
{
24-
int sf;
25-
int de;
26-
int ps;
27-
int band;
28-
int header;
29-
int cr;
26+
uint8_t sf;
27+
uint8_t de;
28+
uint8_t ps;
29+
uint16_t band;
30+
uint8_t header;
31+
uint8_t cr;
3032
} airtime_t;
3133

3234
class TheThingsNetwork
3335
{
3436
private:
37+
port_t port;
3538
Stream* modemStream;
3639
Stream* debugStream;
3740
String model;
3841
airtime_t info;
3942
float airtime;
4043
ttn_fp_t fp;
41-
int sf;
42-
int fsb;
43-
void (* messageCallback)(const byte* payload, int length, int port);
44+
uint8_t sf;
45+
uint8_t fsb;
46+
void (* messageCallback)(const byte* payload, size_t length, port_t port);
4447

4548
String readLine();
4649
void fillAirtimeInfo();
47-
void trackAirtime(int payloadSize);
50+
void trackAirtime(size_t payloadSize);
4851
String readValue(String key);
4952
bool sendCommand(String cmd);
5053
bool sendCommand(String cmd, String value);
51-
bool sendCommand(String cmd, const byte* buf, int length);
54+
bool sendCommand(String cmd, const byte* buf, size_t length);
5255
void reset(bool adr = true);
53-
void configureEU868(int sf);
54-
void configureUS915(int sf, int fsb);
55-
void configureChannels(int sf, int fsb);
56+
void configureEU868(uint8_t sf);
57+
void configureUS915(uint8_t sf, uint8_t fsb);
58+
void configureChannels(uint8_t sf, uint8_t fsb);
5659

5760
public:
58-
TheThingsNetwork(Stream& modemStream, Stream& debugStream, ttn_fp_t fp, int sf = TTN_DEFAULT_SF, int fsb = TTN_DEFAULT_FSB);
61+
TheThingsNetwork(Stream& modemStream, Stream& debugStream, ttn_fp_t fp, uint8_t sf = TTN_DEFAULT_SF, uint8_t fsb = TTN_DEFAULT_FSB);
5962
void showStatus();
60-
void onMessage(void (*cb)(const byte* payload, int length, int port));
63+
void onMessage(void (*cb)(const byte* payload, size_t length, port_t port));
6164
bool provision(const byte appEui[8], const byte appKey[16]);
62-
bool join(const byte appEui[8], const byte appKey[16], int retries = -1, long int retryDelay = 10000);
63-
bool join(int retries = -1, long int retryDelay = 10000);
65+
bool join(const byte appEui[8], const byte appKey[16], int8_t retries = -1, uint32_t retryDelay = 10000);
66+
bool join(int8_t retries = -1, uint32_t retryDelay = 10000);
6467
bool personalize(const byte devAddr[4], const byte nwkSKey[16], const byte appSKey[16]);
6568
bool personalize();
66-
int sendBytes(const byte* payload, int length, int port = 1, bool confirm = false);
67-
int poll(int port = 1, bool confirm = false);
69+
int sendBytes(const byte* payload, size_t length, port_t port = 1, bool confirm = false);
70+
int poll(port_t port = 1, bool confirm = false);
6871
};
6972

7073
#endif

0 commit comments

Comments
 (0)