Skip to content

Commit 720e53f

Browse files
committed
Cleaning code
1 parent ab09e24 commit 720e53f

File tree

2 files changed

+48
-52
lines changed

2 files changed

+48
-52
lines changed

src/TheThingsNetwork.cpp

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,15 @@ uint8_t digits(uint8_t port)
268268
return 1;
269269
}
270270

271-
uint8_t receivedPort(const char *s, size_t length)
271+
uint8_t receivedPort(const char *s)
272272
{
273273
uint8_t port = 0;
274-
while (s[length] != ' ' && s[length] != '\0')
274+
uint8_t i = 0;
275+
while (s[i] != ' ' && s[i] != '\0')
275276
{
276277
port *= 10;
277-
port += s[length] - 48;
278-
length++;
278+
port += s[i] - 48;
279+
i++;
279280
}
280281
return port;
281282
}
@@ -291,34 +292,31 @@ TheThingsNetwork::TheThingsNetwork(Stream &modemStream, Stream &debugStream, ttn
291292

292293
void TheThingsNetwork::debugPrintIndex(uint8_t index, const char *value)
293294
{
294-
strcpy_P(buffer, (char *)pgm_read_word(&(show_table[index])));
295-
debugPrint(buffer);
295+
char message[100];
296+
strcpy_P(message, (char *)pgm_read_word(&(show_table[index])));
297+
debugPrint(message);
296298
if (value)
297299
{
298300
debugPrintLn(value);
299301
}
300302
}
301303

302-
void TheThingsNetwork::debugPrintMessage(uint8_t type, uint8_t index, const char *output)
304+
void TheThingsNetwork::debugPrintMessage(uint8_t type, uint8_t index, const char *value)
303305
{
306+
char message[100];
304307
switch (type)
305308
{
306309
case ERR_MESSAGE:
307-
strcpy_P(buffer, (char *)pgm_read_word(&(error_msg[index])));
310+
strcpy_P(message, (char *)pgm_read_word(&(error_msg[index])));
308311
break;
309312
case SUCCESS_MESSAGE:
310-
strcpy_P(buffer, (char *)pgm_read_word(&(success_msg[index])));
313+
strcpy_P(message, (char *)pgm_read_word(&(success_msg[index])));
311314
break;
312315
}
313-
debugPrint(buffer);
314-
if (type == SUCCESS_MESSAGE && index == SCS_SUCCESSFUL_TRANSMISSION_RECEIVED)
315-
{
316-
debugPrint(output);
317-
debugPrintLn(" bytes");
318-
}
319-
else if (output)
316+
debugPrint(message);
317+
if (value)
320318
{
321-
debugPrintLn(output);
319+
debugPrintLn(value);
322320
}
323321
else
324322
{
@@ -367,8 +365,8 @@ size_t TheThingsNetwork::readResponse(uint8_t prefixTable, uint8_t indexTable, u
367365
void TheThingsNetwork::reset(bool adr)
368366
{
369367
size_t length = readResponse(SYS_TABLE, SYS_RESET, buffer, sizeof(buffer));
370-
debugPrintLn(buffer);
371368

369+
// buffer contains "RN2xx3 1.x.x ...", splitting model from version
372370
buffer[6] = '\0';
373371
debugPrintIndex(SHOW_MODEL, buffer);
374372
debugPrintIndex(SHOW_VERSION, buffer + 7);
@@ -496,19 +494,16 @@ ttn_response_t TheThingsNetwork::sendBytes(const uint8_t *payload, size_t length
496494
return TTN_ERROR_SEND_COMMAND_FAILED;
497495
}
498496

499-
char response[500];
500-
readLine(response, sizeof(response));
501-
502-
if (pgmstrcmp(response, CMP_MAC_TX_OK) == 0)
497+
readLine(buffer, sizeof(buffer));
498+
if (pgmstrcmp(buffer, CMP_MAC_TX_OK) == 0)
503499
{
504500
debugPrintMessage(SUCCESS_MESSAGE, SCS_SUCCESSFUL_TRANSMISSION);
505501
return TTN_SUCCESSFUL_TRANSMISSION;
506502
}
507-
508-
if (pgmstrcmp(response, CMP_MAC_RX) == 0)
503+
if (pgmstrcmp(buffer, CMP_MAC_RX) == 0)
509504
{
510-
port_t downlinkPort = receivedPort(response, 7);
511-
char *data = response + 7 + digits(downlinkPort) + 1;
505+
port_t downlinkPort = receivedPort(buffer + 7);
506+
char *data = buffer + 7 + digits(downlinkPort) + 1;
512507
size_t downlinkLength = strlen(data) / 2;
513508
uint8_t downlink[downlinkLength];
514509
for (size_t i = 0, d = 0; i < downlinkLength; i++, d += 2)
@@ -523,11 +518,11 @@ ttn_response_t TheThingsNetwork::sendBytes(const uint8_t *payload, size_t length
523518
return TTN_SUCCESSFUL_RECEIVE;
524519
}
525520

526-
debugPrintMessage(ERR_MESSAGE, ERR_UNEXPECTED_RESPONSE, response);
521+
debugPrintMessage(ERR_MESSAGE, ERR_UNEXPECTED_RESPONSE, buffer);
527522
return TTN_ERROR_UNEXPECTED_RESPONSE;
528523
}
529524

530-
int8_t TheThingsNetwork::poll(port_t port, bool confirm)
525+
ttn_response_t TheThingsNetwork::poll(port_t port, bool confirm)
531526
{
532527
uint8_t payload[] = {0x00};
533528
return sendBytes(payload, 1, port, confirm);
@@ -683,54 +678,55 @@ void TheThingsNetwork::configureChannels(uint8_t sf, uint8_t fsb)
683678

684679
void TheThingsNetwork::sendCommand(uint8_t table, uint8_t index, bool appendSpace, bool print)
685680
{
681+
char command[100];
686682
switch (table)
687683
{
688684
case MAC_TABLE:
689-
strcpy_P(buffer, (char *)pgm_read_word(&(mac_table[index])));
685+
strcpy_P(command, (char *)pgm_read_word(&(mac_table[index])));
690686
break;
691687
case MAC_GET_SET_TABLE:
692-
strcpy_P(buffer, (char *)pgm_read_word(&(mac_set_options[index])));
688+
strcpy_P(command, (char *)pgm_read_word(&(mac_set_options[index])));
693689
break;
694690
case MAC_JOIN_TABLE:
695-
strcpy_P(buffer, (char *)pgm_read_word(&(mac_join_mode[index])));
691+
strcpy_P(command, (char *)pgm_read_word(&(mac_join_mode[index])));
696692
break;
697693
case MAC_CH_TABLE:
698-
strcpy_P(buffer, (char *)pgm_read_word(&(mac_ch_options[index])));
694+
strcpy_P(command, (char *)pgm_read_word(&(mac_ch_options[index])));
699695
break;
700696
case MAC_TX_TABLE:
701-
strcpy_P(buffer, (char *)pgm_read_word(&(mac_tx_table[index])));
697+
strcpy_P(command, (char *)pgm_read_word(&(mac_tx_table[index])));
702698
break;
703699
case SYS_TABLE:
704-
strcpy_P(buffer, (char *)pgm_read_word(&(sys_table[index])));
700+
strcpy_P(command, (char *)pgm_read_word(&(sys_table[index])));
705701
break;
706702
case RADIO_TABLE:
707-
strcpy_P(buffer, (char *)pgm_read_word(&(radio_table[index])));
703+
strcpy_P(command, (char *)pgm_read_word(&(radio_table[index])));
708704
break;
709705
default:
710706
return;
711707
}
712-
modemStream->write(buffer);
708+
modemStream->write(command);
713709
if (appendSpace)
714710
{
715711
modemStream->write(" ");
716712
}
717713
if (print)
718714
{
719-
debugPrint(buffer);
715+
debugPrint(command);
720716
debugPrint(F(" "));
721717
}
722718
}
723719

724-
bool TheThingsNetwork::sendMacSet(uint8_t index, const char *setting)
720+
bool TheThingsNetwork::sendMacSet(uint8_t index, const char *value)
725721
{
726722
clearReadBuffer();
727723
debugPrint(SENDING);
728724
sendCommand(MAC_TABLE, MAC_PREFIX, true);
729725
sendCommand(MAC_TABLE, MAC_SET, true);
730726
sendCommand(MAC_GET_SET_TABLE, index, true);
731-
modemStream->write(setting);
727+
modemStream->write(value);
732728
modemStream->write(SEND_MSG);
733-
debugPrintLn(setting);
729+
debugPrintLn(value);
734730
return waitForOk();
735731
}
736732

@@ -745,7 +741,7 @@ bool TheThingsNetwork::waitForOk()
745741
return true;
746742
}
747743

748-
bool TheThingsNetwork::sendChSet(uint8_t index, uint8_t channel, const char *setting)
744+
bool TheThingsNetwork::sendChSet(uint8_t index, uint8_t channel, const char *value)
749745
{
750746
clearReadBuffer();
751747
char ch[5];
@@ -767,11 +763,11 @@ bool TheThingsNetwork::sendChSet(uint8_t index, uint8_t channel, const char *set
767763
sendCommand(MAC_CH_TABLE, index, true);
768764
modemStream->write(ch);
769765
modemStream->write(" ");
770-
modemStream->write(setting);
766+
modemStream->write(value);
771767
modemStream->write(SEND_MSG);
772768
debugPrint(channel);
773769
debugPrint(F(" "));
774-
debugPrintLn(setting);
770+
debugPrintLn(value);
775771
return waitForOk();
776772
}
777773

@@ -838,9 +834,9 @@ bool TheThingsNetwork::sendPayload(uint8_t mode, uint8_t port, uint8_t *payload,
838834
return waitForOk();
839835
}
840836

841-
void TheThingsNetwork::sleep(unsigned long msec)
837+
void TheThingsNetwork::sleep(uint32_t mseconds)
842838
{
843-
if (msec < 100 || msec >= 4294967296)
839+
if (mseconds < 100 || mseconds >= 4294967296)
844840
{
845841
return;
846842
}
@@ -849,7 +845,7 @@ void TheThingsNetwork::sleep(unsigned long msec)
849845
sendCommand(SYS_TABLE, SYS_PREFIX, true);
850846
sendCommand(SYS_TABLE, SYS_SLEEP, true);
851847

852-
sprintf(buffer, "%ld", msec);
848+
sprintf(buffer, "%ld", mseconds);
853849
modemStream->write(buffer);
854850
modemStream->write(SEND_MSG);
855851
debugPrintLn(buffer);

src/TheThingsNetwork.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class TheThingsNetwork
4141
ttn_fp_t fp;
4242
uint8_t sf;
4343
uint8_t fsb;
44-
char buffer[500];
44+
char buffer[512];
4545
void (*messageCallback)(const uint8_t *payload, size_t size, port_t port);
4646

4747
void clearReadBuffer();
@@ -50,7 +50,7 @@ class TheThingsNetwork
5050
size_t readResponse(uint8_t table, uint8_t index, char *buffer, size_t size);
5151

5252
void debugPrintIndex(uint8_t index, const char *value = NULL);
53-
void debugPrintMessage(uint8_t type, uint8_t index, const char *output = NULL);
53+
void debugPrintMessage(uint8_t type, uint8_t index, const char *value = NULL);
5454

5555
void reset(bool adr = true);
5656
void configureEU868(uint8_t sf);
@@ -59,8 +59,8 @@ class TheThingsNetwork
5959
bool waitForOk();
6060

6161
void sendCommand(uint8_t table, uint8_t index, bool appendSpace, bool print = true);
62-
bool sendMacSet(uint8_t index, const char *setting);
63-
bool sendChSet(uint8_t index, uint8_t channel, const char *setting);
62+
bool sendMacSet(uint8_t index, const char *value);
63+
bool sendChSet(uint8_t index, uint8_t channel, const char *value);
6464
bool sendJoinSet(uint8_t type);
6565
bool sendPayload(uint8_t mode, uint8_t port, uint8_t *payload, size_t len);
6666
void sendGetValue(uint8_t table, uint8_t prefix, uint8_t index);
@@ -75,8 +75,8 @@ class TheThingsNetwork
7575
bool personalize(const char *devAddr, const char *nwkSKey, const char *appSKey);
7676
bool personalize();
7777
ttn_response_t sendBytes(const uint8_t *payload, size_t length, port_t port = 1, bool confirm = false);
78-
void sleep(unsigned long mseconds);
79-
int8_t poll(port_t port = 1, bool confirm = false);
78+
ttn_response_t poll(port_t port = 1, bool confirm = false);
79+
void sleep(uint32_t mseconds);
8080
void saveState();
8181
};
8282

0 commit comments

Comments
 (0)