|
| 1 | +/* |
| 2 | + RadioLib Non-Arduino Tock Library LoRaWAN test application |
| 3 | +
|
| 4 | + Licensed under the MIT or Apache License |
| 5 | +
|
| 6 | + Copyright (c) 2023 Alistair Francis <[email protected]> |
| 7 | + */ |
| 8 | + |
| 9 | +#include <cinttypes> |
| 10 | +#include <stdlib.h> |
| 11 | + |
| 12 | +// include the library |
| 13 | +#include <RadioLib.h> |
| 14 | + |
| 15 | +// Include some libtock-c helpers |
| 16 | +#include <libtock-sync/storage/kv.h> |
| 17 | + |
| 18 | +// To get this working copy radioConfig_example.h to radioConfig.h |
| 19 | +// and then modify it to match the LoRaWAN gateway settings. |
| 20 | +#ifdef RADIO_CONFIG_CI |
| 21 | +#include "radioConfig_example.h" |
| 22 | +#else |
| 23 | +#include "radioConfig.h" |
| 24 | +#endif |
| 25 | + |
| 26 | +#define JOIN_EUI_KEY_LEN 8 |
| 27 | +uint8_t join_eui_key_buf[JOIN_EUI_KEY_LEN] = "joinEUI"; |
| 28 | + |
| 29 | +#define DEV_EUI_KEY_LEN 7 |
| 30 | +uint8_t dev_eui_key_buf[DEV_EUI_KEY_LEN] = "devEUI"; |
| 31 | + |
| 32 | +#define NWK_KEY_KEY_LEN 7 |
| 33 | +uint8_t nwk_key_key_buf[NWK_KEY_KEY_LEN] = "nwkKey"; |
| 34 | + |
| 35 | +#define APP_KEY_KEY_LEN 7 |
| 36 | +uint8_t app_key_key_buf[APP_KEY_KEY_LEN] = "appKey"; |
| 37 | + |
| 38 | +#define KV_DATA_LEN 8 |
| 39 | +uint8_t kv_data_buf[KV_DATA_LEN]; |
| 40 | + |
| 41 | +// Store the joinEUI to the Tock K/V store |
| 42 | +static int set_join_eui(void) { |
| 43 | + returncode_t ret; |
| 44 | + |
| 45 | + if (!libtock_kv_exists()) { |
| 46 | + return 1; |
| 47 | + } |
| 48 | + |
| 49 | + kv_data_buf[0] = joinEUI & 0xFF; |
| 50 | + kv_data_buf[1] = (joinEUI >> 8) & 0xFF; |
| 51 | + kv_data_buf[2] = (joinEUI >> 16) & 0xFF; |
| 52 | + kv_data_buf[3] = (joinEUI >> 24) & 0xFF; |
| 53 | + kv_data_buf[4] = (joinEUI >> 32) & 0xFF; |
| 54 | + kv_data_buf[5] = (joinEUI >> 40) & 0xFF; |
| 55 | + kv_data_buf[6] = (joinEUI >> 48) & 0xFF; |
| 56 | + kv_data_buf[7] = (joinEUI >> 56) & 0xFF; |
| 57 | + |
| 58 | + ret = libtocksync_kv_set(join_eui_key_buf, JOIN_EUI_KEY_LEN, kv_data_buf, KV_DATA_LEN); |
| 59 | + |
| 60 | + if (ret == RETURNCODE_SUCCESS) { |
| 61 | + return 0; |
| 62 | + } else { |
| 63 | + return 1; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Store the devEUI to the Tock K/V store |
| 68 | +static int set_dev_eui(void) { |
| 69 | + returncode_t ret; |
| 70 | + |
| 71 | + if (!libtock_kv_exists()) { |
| 72 | + return 1; |
| 73 | + } |
| 74 | + |
| 75 | + kv_data_buf[0] = devEUI & 0xFF; |
| 76 | + kv_data_buf[1] = (devEUI >> 8) & 0xFF; |
| 77 | + kv_data_buf[2] = (devEUI >> 16) & 0xFF; |
| 78 | + kv_data_buf[3] = (devEUI >> 24) & 0xFF; |
| 79 | + kv_data_buf[4] = (devEUI >> 32) & 0xFF; |
| 80 | + kv_data_buf[5] = (devEUI >> 40) & 0xFF; |
| 81 | + kv_data_buf[6] = (devEUI >> 48) & 0xFF; |
| 82 | + kv_data_buf[7] = (devEUI >> 56) & 0xFF; |
| 83 | + |
| 84 | + ret = libtocksync_kv_set(dev_eui_key_buf, DEV_EUI_KEY_LEN, kv_data_buf, KV_DATA_LEN); |
| 85 | + |
| 86 | + if (ret == RETURNCODE_SUCCESS) { |
| 87 | + return 0; |
| 88 | + } else { |
| 89 | + return 1; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// Store the nwkKey to the Tock K/V store |
| 94 | +static int set_nwk_key(void) { |
| 95 | + returncode_t ret; |
| 96 | + |
| 97 | + if (!libtock_kv_exists()) { |
| 98 | + return 1; |
| 99 | + } |
| 100 | + |
| 101 | + ret = libtocksync_kv_set(nwk_key_key_buf, NWK_KEY_KEY_LEN, nwkKey, 16); |
| 102 | + |
| 103 | + if (ret == RETURNCODE_SUCCESS) { |
| 104 | + return 0; |
| 105 | + } else { |
| 106 | + return 1; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Store the appKey to the Tock K/V store |
| 111 | +static int set_app_key(void) { |
| 112 | + returncode_t ret; |
| 113 | + |
| 114 | + if (!libtock_kv_exists()) { |
| 115 | + return 1; |
| 116 | + } |
| 117 | + |
| 118 | + ret = libtocksync_kv_set(app_key_key_buf, APP_KEY_KEY_LEN, appKey, 16); |
| 119 | + |
| 120 | + if (ret == RETURNCODE_SUCCESS) { |
| 121 | + return 0; |
| 122 | + } else { |
| 123 | + return 1; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// the entry point for the program |
| 128 | +int main(void) { |
| 129 | + if (set_join_eui() == 0) { |
| 130 | + printf("Set joinEUI key to storage: 0x%lx%lx\r\n", |
| 131 | + (uint32_t)(joinEUI >> 32), (uint32_t)joinEUI); |
| 132 | + } else { |
| 133 | + printf("Unable to store joinEUI key to storage\r\n"); |
| 134 | + return 1; |
| 135 | + } |
| 136 | + |
| 137 | + if (set_dev_eui() == 0) { |
| 138 | + printf("Set devEUI key to storage: 0x%lx%lx\r\n", |
| 139 | + (uint32_t)(devEUI >> 32), (uint32_t)devEUI); |
| 140 | + } else { |
| 141 | + printf("Unable to store devEUI key to storage\r\n"); |
| 142 | + return 1; |
| 143 | + } |
| 144 | + |
| 145 | + if (set_nwk_key() == 0) { |
| 146 | + printf("Set nwkKey key to storage\r\n"); |
| 147 | + } else { |
| 148 | + printf("Unable to store nwkKey to storage\r\n"); |
| 149 | + return 1; |
| 150 | + } |
| 151 | + |
| 152 | + if (set_app_key() == 0) { |
| 153 | + printf("Set appKey key to storage\r\n"); |
| 154 | + } else { |
| 155 | + printf("Unable to store appKey to storage\r\n"); |
| 156 | + return 1; |
| 157 | + } |
| 158 | + |
| 159 | + return 0; |
| 160 | +} |
0 commit comments