Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LoRaWAN] Accept const uint8_t* on public API #1302

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/protocols/LoRaWAN/LoRaWAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LoRaWANNode::LoRaWANNode(PhysicalLayer* phy, const LoRaWANBand_t* band, uint8_t
}

#if defined(RADIOLIB_BUILD_ARDUINO)
int16_t LoRaWANNode::sendReceive(String& strUp, uint8_t fPort, String& strDown, bool isConfirmed, LoRaWANEvent_t* eventUp, LoRaWANEvent_t* eventDown) {
int16_t LoRaWANNode::sendReceive(const String& strUp, uint8_t fPort, String& strDown, bool isConfirmed, LoRaWANEvent_t* eventUp, LoRaWANEvent_t* eventDown) {
int16_t state = RADIOLIB_ERR_UNKNOWN;

const char* dataUp = strUp.c_str();
Expand All @@ -28,7 +28,7 @@ int16_t LoRaWANNode::sendReceive(String& strUp, uint8_t fPort, String& strDown,
size_t lenDown = 0;
uint8_t dataDown[251];

state = this->sendReceive((uint8_t*)dataUp, strlen(dataUp), fPort, dataDown, &lenDown, isConfirmed, eventUp, eventDown);
state = this->sendReceive((const uint8_t*)dataUp, strlen(dataUp), fPort, dataDown, &lenDown, isConfirmed, eventUp, eventDown);

if(state == RADIOLIB_ERR_NONE) {
// add null terminator
Expand All @@ -55,7 +55,7 @@ int16_t LoRaWANNode::sendReceive(const char* strUp, uint8_t fPort, uint8_t* data
return(this->sendReceive((uint8_t*)strUp, strlen(strUp), fPort, dataDown, lenDown, isConfirmed, eventUp, eventDown));
}

int16_t LoRaWANNode::sendReceive(uint8_t* dataUp, size_t lenUp, uint8_t fPort, bool isConfirmed, LoRaWANEvent_t* eventUp, LoRaWANEvent_t* eventDown) {
int16_t LoRaWANNode::sendReceive(const uint8_t* dataUp, size_t lenUp, uint8_t fPort, bool isConfirmed, LoRaWANEvent_t* eventUp, LoRaWANEvent_t* eventDown) {
// build a temporary buffer
// LoRaWAN downlinks can have 250 bytes at most with 1 extra byte for NULL
size_t lenDown = 0;
Expand All @@ -64,7 +64,7 @@ int16_t LoRaWANNode::sendReceive(uint8_t* dataUp, size_t lenUp, uint8_t fPort, b
return(this->sendReceive(dataUp, lenUp, fPort, dataDown, &lenDown, isConfirmed, eventUp, eventDown));
}

int16_t LoRaWANNode::sendReceive(uint8_t* dataUp, size_t lenUp, uint8_t fPort, uint8_t* dataDown, size_t* lenDown, bool isConfirmed, LoRaWANEvent_t* eventUp, LoRaWANEvent_t* eventDown) {
int16_t LoRaWANNode::sendReceive(const uint8_t* dataUp, size_t lenUp, uint8_t fPort, uint8_t* dataDown, size_t* lenDown, bool isConfirmed, LoRaWANEvent_t* eventUp, LoRaWANEvent_t* eventDown) {
if(!dataUp || !dataDown || !lenDown) {
return(RADIOLIB_ERR_NULL_POINTER);
}
Expand Down Expand Up @@ -241,7 +241,7 @@ uint8_t* LoRaWANNode::getBufferNonces() {
return(this->bufferNonces);
}

int16_t LoRaWANNode::setBufferNonces(uint8_t* persistentBuffer) {
int16_t LoRaWANNode::setBufferNonces(const uint8_t* persistentBuffer) {
if(this->isActivated()) {
RADIOLIB_DEBUG_PROTOCOL_PRINTLN("Did not update buffer: session already active");
return(RADIOLIB_ERR_NONE);
Expand Down Expand Up @@ -464,7 +464,7 @@ uint8_t* LoRaWANNode::getBufferSession() {
return(this->bufferSession);
}

int16_t LoRaWANNode::setBufferSession(uint8_t* persistentBuffer) {
int16_t LoRaWANNode::setBufferSession(const uint8_t* persistentBuffer) {
if(this->isActivated()) {
RADIOLIB_DEBUG_PROTOCOL_PRINTLN("Did not update buffer: session already active");
return(RADIOLIB_ERR_NONE);
Expand Down Expand Up @@ -588,7 +588,7 @@ int16_t LoRaWANNode::setBufferSession(uint8_t* persistentBuffer) {
return(state);
}

int16_t LoRaWANNode::beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t* nwkKey, uint8_t* appKey) {
int16_t LoRaWANNode::beginOTAA(uint64_t joinEUI, uint64_t devEUI, const uint8_t* nwkKey, const uint8_t* appKey) {
if(!appKey) {
return(RADIOLIB_ERR_NULL_POINTER);
}
Expand Down Expand Up @@ -617,7 +617,7 @@ int16_t LoRaWANNode::beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t* nwkKe
return(RADIOLIB_ERR_NONE);
}

int16_t LoRaWANNode::beginABP(uint32_t addr, uint8_t* fNwkSIntKey, uint8_t* sNwkSIntKey, uint8_t* nwkSEncKey, uint8_t* appSKey) {
int16_t LoRaWANNode::beginABP(uint32_t addr, const uint8_t* fNwkSIntKey, const uint8_t* sNwkSIntKey, const uint8_t* nwkSEncKey, const uint8_t* appSKey) {
if(!nwkSEncKey || !appSKey) {
return(RADIOLIB_ERR_NULL_POINTER);
}
Expand Down Expand Up @@ -1159,7 +1159,7 @@ void LoRaWANNode::adrBackoff() {
return;
}

void LoRaWANNode::composeUplink(uint8_t* in, uint8_t lenIn, uint8_t* out, uint8_t fPort, bool isConfirmed) {
void LoRaWANNode::composeUplink(const uint8_t* in, uint8_t lenIn, uint8_t* out, uint8_t fPort, bool isConfirmed) {
// set the packet fields
if(isConfirmed) {
out[RADIOLIB_LORAWAN_FHDR_LEN_START_OFFS] = RADIOLIB_LORAWAN_MHDR_MTYPE_CONF_DATA_UP;
Expand Down Expand Up @@ -3416,7 +3416,7 @@ void LoRaWANNode::processAES(const uint8_t* in, size_t len, uint8_t* key, uint8_
}
}

int16_t LoRaWANNode::checkBufferCommon(uint8_t *buffer, uint16_t size) {
int16_t LoRaWANNode::checkBufferCommon(const uint8_t *buffer, uint16_t size) {
// check if there are actually values in the buffer
size_t i = 0;
for(; i < size; i++) {
Expand Down
24 changes: 12 additions & 12 deletions src/protocols/LoRaWAN/LoRaWAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ class LoRaWANNode {
\param persistentBuffer Buffer that should match the internal format (previously extracted using getBufferNonces)
\returns \ref status_codes
*/
int16_t setBufferNonces(uint8_t* persistentBuffer);
int16_t setBufferNonces(const uint8_t* persistentBuffer);

/*!
\brief Clear an active session, so that the device will have to rejoin the network.
Expand All @@ -566,7 +566,7 @@ class LoRaWANNode {
\param persistentBuffer Buffer that should match the internal format (previously extracted using getBufferSession)
\returns \ref status_codes
*/
int16_t setBufferSession(uint8_t* persistentBuffer);
int16_t setBufferSession(const uint8_t* persistentBuffer);

/*!
\brief Set the device credentials and activation configuration
Expand All @@ -576,7 +576,7 @@ class LoRaWANNode {
\param appKey Pointer to the application AES-128 key.
\returns \ref status_codes
*/
int16_t beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t* nwkKey, uint8_t* appKey);
int16_t beginOTAA(uint64_t joinEUI, uint64_t devEUI, const uint8_t* nwkKey, const uint8_t* appKey);

/*!
\brief Set the device credentials and activation configuration
Expand All @@ -588,7 +588,7 @@ class LoRaWANNode {
\param appSKey Pointer to the application session AES-128 key.
\returns \ref status_codes
*/
int16_t beginABP(uint32_t addr, uint8_t* fNwkSIntKey, uint8_t* sNwkSIntKey, uint8_t* nwkSEncKey, uint8_t* appSKey);
int16_t beginABP(uint32_t addr, const uint8_t* fNwkSIntKey, const uint8_t* sNwkSIntKey, const uint8_t* nwkSEncKey, const uint8_t* appSKey);

/*!
\brief Join network by restoring OTAA session or performing over-the-air activation. By this procedure,
Expand Down Expand Up @@ -622,7 +622,7 @@ class LoRaWANNode {
(fPort, frame counter, etc.). If set to NULL, no extra information will be passed to the user.
\returns Window number > 0 if downlink was received, 0 is no downlink was received, otherwise \ref status_codes
*/
virtual int16_t sendReceive(String& strUp, uint8_t fPort, String& strDown, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
virtual int16_t sendReceive(const String& strUp, uint8_t fPort, String& strDown, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
#endif

/*!
Expand Down Expand Up @@ -665,7 +665,7 @@ class LoRaWANNode {
(fPort, frame counter, etc.). If set to NULL, no extra information will be passed to the user.
\returns Window number > 0 if downlink was received, 0 is no downlink was received, otherwise \ref status_codes
*/
virtual int16_t sendReceive(uint8_t* dataUp, size_t lenUp, uint8_t fPort = 1, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
virtual int16_t sendReceive(const uint8_t* dataUp, size_t lenUp, uint8_t fPort = 1, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);

/*!
\brief Send a message to the server and wait for a downlink during Rx1 and/or Rx2 window.
Expand All @@ -681,7 +681,7 @@ class LoRaWANNode {
(fPort, frame counter, etc.). If set to NULL, no extra information will be passed to the user.
\returns Window number > 0 if downlink was received, 0 is no downlink was received, otherwise \ref status_codes
*/
virtual int16_t sendReceive(uint8_t* dataUp, size_t lenUp, uint8_t fPort, uint8_t* dataDown, size_t* lenDown, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);
virtual int16_t sendReceive(const uint8_t* dataUp, size_t lenUp, uint8_t fPort, uint8_t* dataDown, size_t* lenDown, bool isConfirmed = false, LoRaWANEvent_t* eventUp = NULL, LoRaWANEvent_t* eventDown = NULL);

/*!
\brief Add a MAC command to the uplink queue.
Expand Down Expand Up @@ -1002,7 +1002,7 @@ class LoRaWANNode {
void adrBackoff();

// create an encrypted uplink buffer, composing metadata, user data and MAC data
void composeUplink(uint8_t* in, uint8_t lenIn, uint8_t* out, uint8_t fPort, bool isConfirmed);
void composeUplink(const uint8_t* in, uint8_t lenIn, uint8_t* out, uint8_t fPort, bool isConfirmed);

// generate and set the MIC of an uplink buffer (depends on selected channels)
void micUplink(uint8_t* inOut, uint8_t lenInOut);
Expand Down Expand Up @@ -1112,20 +1112,20 @@ class LoRaWANNode {
static uint16_t checkSum16(const uint8_t *key, uint16_t keyLen);

// check the integrity of a buffer using a 16-bit checksum located in the last two bytes of the buffer
static int16_t checkBufferCommon(uint8_t *buffer, uint16_t size);
static int16_t checkBufferCommon(const uint8_t *buffer, uint16_t size);

// network-to-host conversion method - takes data from network packet and converts it to the host endians
template<typename T>
static T ntoh(uint8_t* buff, size_t size = 0);
static T ntoh(const uint8_t* buff, size_t size = 0);

// host-to-network conversion method - takes data from host variable and and converts it to network packet endians
template<typename T>
static void hton(uint8_t* buff, T val, size_t size = 0);
};

template<typename T>
T LoRaWANNode::ntoh(uint8_t* buff, size_t size) {
uint8_t* buffPtr = buff;
T LoRaWANNode::ntoh(const uint8_t* buff, size_t size) {
const uint8_t* buffPtr = buff;
size_t targetSize = sizeof(T);
if(size != 0) {
targetSize = size;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ uint32_t rlb_reflect(uint32_t in, uint8_t bits) {
return(res);
}

void rlb_hexdump(const char* level, uint8_t* data, size_t len, uint32_t offset, uint8_t width, bool be) {
void rlb_hexdump(const char* level, const uint8_t* data, size_t len, uint32_t offset, uint8_t width, bool be) {
#if RADIOLIB_DEBUG
size_t rem_len = len;
for(size_t i = 0; i < len; i+=16) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ uint32_t rlb_reflect(uint32_t in, uint8_t bits);
\param width Word width (1 for uint8_t, 2 for uint16_t, 4 for uint32_t).
\param be Print multi-byte data as big endian. Defaults to false.
*/
void rlb_hexdump(const char* level, uint8_t* data, size_t len, uint32_t offset = 0, uint8_t width = 1, bool be = false);
void rlb_hexdump(const char* level, const uint8_t* data, size_t len, uint32_t offset = 0, uint8_t width = 1, bool be = false);

#if RADIOLIB_DEBUG && defined(RADIOLIB_BUILD_ARDUINO)
size_t rlb_printf(const char* format, ...);
Expand Down
Loading