83 ulog_trace(
"lorawan_join");
92 ulog_info(
"Using DevEUI from #define.");
95 static const uint64_t devEUI = bytes_to_u64_be(devEUI_bytes);
97 ulog_info(
"Using DevEUI from flash.");
100 if (!libtock_eui64_exists()) {
101 ulog_error(
"EUI64 driver does not exist!");
106 libtock_eui64_get(&devEUI);
108 uint8_t devEUI_bytes[8] = {};
109 u64_to_bytes_be(devEUI, devEUI_bytes);
112 ulog_info(
"DevEUI: %02x %02x %02x %02x %02x %02x %02x %02x", devEUI_bytes[0],
113 devEUI_bytes[1], devEUI_bytes[2], devEUI_bytes[3], devEUI_bytes[4],
114 devEUI_bytes[5], devEUI_bytes[6], devEUI_bytes[7]);
117 static const uint8_t joinEUI_bytes[8] =
119 static const uint64_t joinEUI = bytes_to_u64_be(joinEUI_bytes);
123 node->scanGuard = 100;
128 state = node->beginOTAA(joinEUI, devEUI, nwkKey, appKey);
130 if (
state != RADIOLIB_ERR_NONE) {
131 ulog_fatal(
"LoRaWAN OTAA setup failed, code %d",
state);
134 ulog_debug(
"LoRaWAN OTAA setup success!");
137 uint8_t persistant_nonces[RADIOLIB_LORAWAN_NONCES_BUF_SIZE];
142 ulog_error(
"Could not read nonces data from persistant storage.");
144 if (persistant_nonces[0] == 0x00) {
145 ulog_info(
"No nonces set");
147 state = node->setBufferNonces(persistant_nonces);
149 if (
state == RADIOLIB_ERR_NONCES_DISCARDED) {
150 ulog_error(
"LoRaWAN nonces is invalid. Resetting to 0.");
151 memset(persistant_nonces, 0,
sizeof(persistant_nonces));
153 ulog_error(
"Could not set LoRaWAN nonces buffer. RadioLib state = %d",
163 state = node->activateOTAA();
164 while (
state != RADIOLIB_ERR_NONE &&
state != RADIOLIB_LORAWAN_NEW_SESSION) {
166 "LoRaWAN OTAA activation failed, code %d. Delaying %d seconds, then "
168 state, retry_ms / 1000);
169 hal->delay(retry_ms);
172 ulog_debug(
"LoRaWAN OTAA activation success!");
174 uint8_t* current_nonces = node->getBufferNonces();
176 RADIOLIB_LORAWAN_NONCES_BUF_SIZE);
178 ulog_error(
"Could not write nonces data from persistant storage.");
185 node->setDatarate(2);
190 node->setDwellTime(
true, 400);
227 ulog_trace(
"lorawan_upload_fport");
235 ulog_debug(
"Sending uplink of %lu bytes.", length,
buffer);
238 uint8_t recv_data[16] = {};
239 size_t len_recv_data = 0;
242 LoRaWANEvent_t up_event = {};
243 LoRaWANEvent_t down_event = {};
246 state = node->sendReceive(
buffer, (
size_t)length, (uint8_t)fport, recv_data,
247 &len_recv_data,
false, &up_event, &down_event);
248 ulog_debug(
"LoRaWAN send/receive code %d.",
state);
250 ulog_error(
"Upload failed. LoRaWAN state %d",
state);
251 }
else if (
state > 0) {
252 int down_fport = down_event.fPort;
253 ulog_debug(
"Received downlink (window number = %d, bytes = %d, fport = %d)",
254 state, len_recv_data, down_fport);
255 ulog_info(
"recv_data: %u", recv_data[0]);
258 switch (down_fport) {
260 ulog_info(
"Restarting");
262 ulog_info(
"Failed to restart.");
265 ulog_warn(
"Downlink fport = %d not implemented", down_fport);
269 ulog_debug(
"No downlink received");
fram_status fram_write(fram_addr addr, const uint8_t *data, size_t len)
Write bytes at the given address.
fram_status fram_read(fram_addr addr, size_t len, uint8_t *data)
Read bytes at the given address.