Add API for reading downlink message

There is API such as api.lorawan.cfs.get() which can tell the result of the last SEND command, but seems like there is no command that can notify the application that there is a downlink message and also no way to extract the message. The existing, and seemingly suggested method is to process the downlink data in the call back function.

New suggestion is to add new APIs, such as api.lorawan.dnlk.get() == TRUE to determine if a downlink exists and api.lorawan.read(uin8_t[]) to extract from internal buffer. Which allow more flexible programming.

Welcome to the forum @chansheunglong

You can register callbacks for packet received, transmit finished and join finished
It is in the RUI3 API manual ==> registerRecvCallback

Example for the callbacks:

/**
 * @brief Callback after packet was received
 *
 * @param data Structure with the received data
 */
void receiveCallback(SERVICE_LORA_RECEIVE_T *data)
{
	MYLOG("RX-CB", "RX, fP %d, DR %d, RSSI %d, SNR %d", data->Port, data->RxDatarate, data->Rssi, data->Snr);
#if MY_DEBUG > 0
	for (int i = 0; i < data->BufferSize; i++)
	{
		Serial.printf("%02X", data->Buffer[i]);
	}
	Serial.print("\r\n");
#endif
}

/**
 * @brief Callback after TX is finished
 *
 * @param status TX status
 */
void sendCallback(int32_t status)
{
	MYLOG("TX-CB", "TX %d", status);
	digitalWrite(LED_BLUE, LOW);
}

/**
 * @brief Callback after join request cycle
 *
 * @param status Join result
 */
void joinCallback(int32_t status)
{
	if (status != 0)
	{
		if (!(ret = api.lorawan.join()))
		{
			MYLOG("J-CB", "Fail! \r\n");
		}
	}
	else
	{
		// MYLOG("J-CB", "Joined\r\n");
	}
}

Registration of the callbacks:

void setup()
{
	// Setup the callbacks for joined and send finished
	api.lorawan.registerRecvCallback(receiveCallback);
	api.lorawan.registerSendCallback(sendCallback);
	api.lorawan.registerJoinCallback(joinCallback);
...
}

Example code where I use these callbacks in my RUI3-RAK12047-Air-Quality

1 Like