[RN2384 lib] Only use const String& in function parameters

Making these functions a lot more memory efficient
This commit is contained in:
Gijs Noorlander
2019-08-08 23:14:34 +02:00
parent e0689f621b
commit 5e9be55656
2 changed files with 47 additions and 56 deletions
+37 -46
View File
@@ -107,7 +107,7 @@ bool rn2xx3::init()
}
bool rn2xx3::initOTAA(String AppEUI, String AppKey, String DevEUI)
bool rn2xx3::initOTAA(const String& AppEUI, const String& AppKey, const String& DevEUI)
{
_otaa = true;
_nwkskey = "0";
@@ -256,7 +256,7 @@ bool rn2xx3::initOTAA(uint8_t * AppEUI, uint8_t * AppKey, uint8_t * DevEUI)
return initOTAA(app_eui, app_key, dev_eui);
}
bool rn2xx3::initABP(String devAddr, String AppSKey, String NwkSKey)
bool rn2xx3::initABP(const String& devAddr, const String& AppSKey, const String& NwkSKey)
{
_otaa = false;
_devAddr = devAddr;
@@ -324,7 +324,7 @@ bool rn2xx3::initABP(String devAddr, String AppSKey, String NwkSKey)
}
}
TX_RETURN_TYPE rn2xx3::tx(String data)
TX_RETURN_TYPE rn2xx3::tx(const String& data)
{
return txUncnf(data); //we are unsure which mode we're in. Better not to wait for acks.
}
@@ -343,17 +343,17 @@ TX_RETURN_TYPE rn2xx3::txBytes(const byte* data, uint8_t size)
return txCommand("mac tx uncnf 1 ", dataToTx, false);
}
TX_RETURN_TYPE rn2xx3::txCnf(String data)
TX_RETURN_TYPE rn2xx3::txCnf(const String& data)
{
return txCommand("mac tx cnf 1 ", data, true);
}
TX_RETURN_TYPE rn2xx3::txUncnf(String data)
TX_RETURN_TYPE rn2xx3::txUncnf(const String& data)
{
return txCommand("mac tx uncnf 1 ", data, true);
}
TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode)
TX_RETURN_TYPE rn2xx3::txCommand(const String& command, const String& data, bool shouldEncode)
{
bool send_success = false;
uint8_t busy_count = 0;
@@ -386,7 +386,7 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode)
String receivedData = _serial.readStringUntil('\n');
//TODO: Debug print on receivedData
switch (decodeReceived(receivedData))
switch (determineReceivedDataType(receivedData))
{
case rn2xx3::ok:
{
@@ -396,7 +396,7 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode)
//TODO: Debug print on receivedData
switch (decodeReceived(receivedData))
switch (determineReceivedDataType(receivedData))
{
case rn2xx3::mac_tx_ok:
{
@@ -526,40 +526,34 @@ TX_RETURN_TYPE rn2xx3::txCommand(String command, String data, bool shouldEncode)
return TX_FAIL; //should never reach this
}
void rn2xx3::sendEncoded(String input)
void rn2xx3::sendEncoded(const String& input)
{
char working;
char buffer[3];
for (unsigned i=0; i<input.length(); i++)
{
working = input.charAt(i);
sprintf(buffer, "%02x", int(working));
sprintf(buffer, "%02x", static_cast<int>(input.charAt(i)));
_serial.print(buffer);
}
}
String rn2xx3::base16encode(String input)
String rn2xx3::base16encode(const String& input_c)
{
char charsOut[input.length()*2+1];
char charsIn[input.length()+1];
String input(input_c); // Make a deep copy to be able to do trim()
input.trim();
input.toCharArray(charsIn, input.length()+1);
unsigned i = 0;
for(i = 0; i<input.length()+1; i++)
const size_t inputLength = input.length();
String output;
output.reserve(inputLength * 2);
for(size_t i = 0; i < inputLength; ++i)
{
if(charsIn[i] == '\0') break;
int value = int(charsIn[i]);
if(input[i] == '\0') break;
char buffer[3];
sprintf(buffer, "%02x", value);
charsOut[2*i] = buffer[0];
charsOut[2*i+1] = buffer[1];
sprintf(buffer, "%02x", static_cast<int>(input[i]));
output += buffer[0];
output += buffer[1];
}
charsOut[2*i] = '\0';
String toReturn = String(charsOut);
return toReturn;
return output;
}
String rn2xx3::getRx() {
@@ -576,31 +570,28 @@ int rn2xx3::getVbat()
return readIntValue(F("sys get vdd"));
}
String rn2xx3::base16decode(String input)
String rn2xx3::base16decode(const String& input_c)
{
char charsIn[input.length()+1];
char charsOut[input.length()/2+1];
String input(input_c); // Make a deep copy to be able to do trim()
input.trim();
input.toCharArray(charsIn, input.length()+1);
const size_t inputLength = input.length();
const size_t outputLength = inputLength / 2;
String output;
output.reserve(outputLength);
unsigned i = 0;
for(i = 0; i<input.length()/2+1; i++)
for(size_t i = 0; i < outputLength; ++i)
{
if(charsIn[i*2] == '\0') break;
if(charsIn[i*2+1] == '\0') break;
char toDo[2];
toDo[0] = charsIn[i*2];
toDo[1] = charsIn[i*2+1];
char toDo[3];
toDo[0] = input[i*2];
toDo[1] = input[i*2+1];
toDo[2] = '\0';
int out = strtoul(toDo, 0, 16);
if(out<128)
if((out & 0xFF) == 0)
{
charsOut[i] = char(out);
output += char(out);
}
}
charsOut[i] = '\0';
return charsOut;
return output;
}
void rn2xx3::setDR(int dr)
@@ -778,7 +769,7 @@ bool rn2xx3::setFrequencyPlan(FREQ_PLAN fp)
}
rn2xx3::received_t rn2xx3::decodeReceived(const String& receivedData) {
rn2xx3::received_t rn2xx3::determineReceivedDataType(const String& receivedData) {
if (receivedData.length() != 0) {
#define MATCH_STRING(S) \
if (receivedData.startsWith(F(#S))) return (rn2xx3::S);
+10 -10
View File
@@ -105,7 +105,7 @@ class rn2xx3
* NwkSKey: Network Session Key as a HEX string.
* Example "AE17E567AECC8787F749A62F5541D522"
*/
bool initABP(String addr, String AppSKey, String NwkSKey);
bool initABP(const String& addr, const String& AppSKey, const String& NwkSKey);
//TODO: initABP(uint8_t * addr, uint8_t * AppSKey, uint8_t * NwkSKey)
@@ -124,7 +124,7 @@ class rn2xx3
* they will be used. Otherwise the join will fail and this function
* will return false.
*/
bool initOTAA(String AppEUI="", String AppKey="", String DevEUI="");
bool initOTAA(const String& AppEUI="", const String& AppKey="", const String& DevEUI="");
/*
* Initialise the RN2xx3 and join a network using over the air activation,
@@ -144,7 +144,7 @@ class rn2xx3
*
* Parameter is an ascii text string.
*/
TX_RETURN_TYPE tx(String);
TX_RETURN_TYPE tx(const String&);
/*
* Transmit raw byte encoded data via LoRa WAN.
@@ -158,14 +158,14 @@ class rn2xx3
*
* Parameter is an ascii text string.
*/
TX_RETURN_TYPE txCnf(String);
TX_RETURN_TYPE txCnf(const String&);
/*
* Do an unconfirmed transmission via LoRa WAN.
*
* Parameter is an ascii text string.
*/
TX_RETURN_TYPE txUncnf(String);
TX_RETURN_TYPE txUncnf(const String&);
/*
* Transmit the provided data using the provided command.
@@ -175,7 +175,7 @@ class rn2xx3
* String - an ascii text string if bool is true. A HEX string if bool is false.
* bool - should the data string be hex encoded or not
*/
TX_RETURN_TYPE txCommand(String, String, bool);
TX_RETURN_TYPE txCommand(const String&, const String&, bool);
/*
* Change the datarate at which the RN2xx3 transmits.
@@ -232,13 +232,13 @@ class rn2xx3
* Encode an ASCII string to a HEX string as needed when passed
* to the RN2xx3 module.
*/
String base16encode(String);
String base16encode(const String&);
/*
* Decode a HEX string to an ASCII string. Useful to decode a
* string received from the RN2xx3.
*/
String base16decode(String);
String base16decode(const String&);
/*
* Almost all commands can return "invalid_param"
@@ -282,7 +282,7 @@ class rn2xx3
*/
RN2xx3_t configureModuleType();
void sendEncoded(String);
void sendEncoded(const String&);
enum received_t {
busy,
@@ -302,7 +302,7 @@ class rn2xx3
UNKNOWN
};
static received_t decodeReceived(const String& receivedData);
static received_t determineReceivedDataType(const String& receivedData);
int readIntValue(const String& command);