|
20 | 20 | #include "ns_types.h"
|
21 | 21 | #include "ns_trace.h"
|
22 | 22 | #include "nsdynmemLIB.h"
|
| 23 | +#include "randLIB.h" |
| 24 | +#include "common_functions.h" |
23 | 25 | #include "net_interface.h"
|
24 | 26 | #include "socket_api.h"
|
25 | 27 | #include "eventOS_event.h"
|
|
31 | 33 | #include "6LoWPAN/ws/ws_bootstrap.h"
|
32 | 34 | #include "6LoWPAN/ws/ws_cfg_settings.h"
|
33 | 35 | #include "6LoWPAN/ws/ws_pae_key_storage.h"
|
| 36 | +#include "6LoWPAN/ws/ws_pae_nvm_store.h" |
34 | 37 | #include "RPL/rpl_control.h"
|
35 | 38 | #include "RPL/rpl_data.h"
|
36 | 39 | #include "Common_Protocols/icmpv6.h"
|
@@ -61,6 +64,20 @@ static uint8_t current_instance_id = RPL_INSTANCE_ID;
|
61 | 64 | #define BBR_CHECK_INTERVAL 60
|
62 | 65 | #define BBR_BACKUP_ULA_DELAY 300
|
63 | 66 |
|
| 67 | +//TAG ID This must be update if NVM_BBR_INFO_LEN or data structure |
| 68 | +#define NVM_BBR_INFO_TAG 1 |
| 69 | +// BSI 2 bytes |
| 70 | +#define NVM_BBR_INFO_LEN 2 |
| 71 | + |
| 72 | +typedef struct bbr_info_nvm_tlv { |
| 73 | + uint16_t tag; /**< Unique tag */ |
| 74 | + uint16_t len; /**< Number of the bytes after the length field */ |
| 75 | + uint8_t data[NVM_BBR_INFO_LEN]; /**< Data */ |
| 76 | +} bbr_info_nvm_tlv_t; |
| 77 | + |
| 78 | +//NVM file name |
| 79 | +static const char *BBR_INFO_FILE = "pae_bbr_info"; |
| 80 | + |
64 | 81 | /* when creating BBR make ULA dodag ID always and when network becomes available add prefix to DHCP
|
65 | 82 | *
|
66 | 83 | *
|
@@ -105,6 +122,52 @@ typedef struct dns_resolution {
|
105 | 122 | #define MAX_DNS_RESOLUTIONS 4
|
106 | 123 |
|
107 | 124 | static dns_resolution_t pre_resolved_dns_queries[MAX_DNS_RESOLUTIONS] = {0};
|
| 125 | +//BBR NVM info buffer |
| 126 | + |
| 127 | +#define BBR_NVM_BSI_OFFSET 0 |
| 128 | +static bbr_info_nvm_tlv_t bbr_info_nvm_tlv = { |
| 129 | + .tag = NVM_BBR_INFO_TAG, |
| 130 | + .len = 0, |
| 131 | + .data = {0} |
| 132 | +}; |
| 133 | + |
| 134 | +static uint16_t ws_bbr_fhss_bsi = 0; |
| 135 | + |
| 136 | +static int8_t ws_bbr_nvm_info_read(bbr_info_nvm_tlv_t *tlv_entry) |
| 137 | +{ |
| 138 | + tlv_entry->tag = NVM_BBR_INFO_TAG; |
| 139 | + tlv_entry->len = NVM_BBR_INFO_LEN; |
| 140 | + |
| 141 | + int8_t ret_val = ws_pae_nvm_store_tlv_file_read(BBR_INFO_FILE, (nvm_tlv_t *) &bbr_info_nvm_tlv); |
| 142 | + |
| 143 | + if (ret_val < 0 || tlv_entry->tag != NVM_BBR_INFO_TAG || tlv_entry->len != NVM_BBR_INFO_LEN) { |
| 144 | + ws_pae_nvm_store_tlv_file_remove(BBR_INFO_FILE); |
| 145 | + tlv_entry->len = 0; |
| 146 | + return -1; |
| 147 | + } |
| 148 | + return 0; |
| 149 | +} |
| 150 | + |
| 151 | +static void ws_bbr_nvm_info_write(bbr_info_nvm_tlv_t *tlv_entry) |
| 152 | +{ |
| 153 | + tlv_entry->tag = NVM_BBR_INFO_TAG; |
| 154 | + tlv_entry->len = NVM_BBR_INFO_LEN; |
| 155 | + ws_pae_nvm_store_tlv_file_write(BBR_INFO_FILE, (nvm_tlv_t *) tlv_entry); |
| 156 | + tr_debug("BBR info NVM update"); |
| 157 | +} |
| 158 | + |
| 159 | +static uint16_t ws_bbr_bsi_read(bbr_info_nvm_tlv_t *tlv_entry) |
| 160 | +{ |
| 161 | + if (tlv_entry->tag != NVM_BBR_INFO_TAG || tlv_entry->len != NVM_BBR_INFO_LEN) { |
| 162 | + return 0; |
| 163 | + } |
| 164 | + return common_read_16_bit(tlv_entry->data + BBR_NVM_BSI_OFFSET); |
| 165 | +} |
| 166 | + |
| 167 | +static void ws_bbr_bsi_write(bbr_info_nvm_tlv_t *tlv_entry, uint16_t bsi) |
| 168 | +{ |
| 169 | + common_write_16_bit(bsi, tlv_entry->data + BBR_NVM_BSI_OFFSET); |
| 170 | +} |
108 | 171 |
|
109 | 172 | static void ws_bbr_rpl_version_timer_start(protocol_interface_info_entry_t *cur, uint8_t version)
|
110 | 173 | {
|
@@ -134,7 +197,6 @@ static void ws_bbr_rpl_version_increase(protocol_interface_info_entry_t *cur)
|
134 | 197 | ws_bbr_rpl_version_timer_start(cur, rpl_control_increment_dodag_version(protocol_6lowpan_rpl_root_dodag));
|
135 | 198 | }
|
136 | 199 |
|
137 |
| - |
138 | 200 | void ws_bbr_rpl_config(protocol_interface_info_entry_t *cur, uint8_t imin, uint8_t doubling, uint8_t redundancy, uint16_t dag_max_rank_increase, uint16_t min_hop_rank_increase)
|
139 | 201 | {
|
140 | 202 | if (imin == 0 || doubling == 0) {
|
@@ -785,6 +847,35 @@ bool ws_bbr_ready_to_start(protocol_interface_info_entry_t *cur)
|
785 | 847 |
|
786 | 848 | return true;
|
787 | 849 | }
|
| 850 | + |
| 851 | +void ws_bbr_init(protocol_interface_info_entry_t *interface) |
| 852 | +{ |
| 853 | + (void) interface; |
| 854 | + //Read From NVM |
| 855 | + if (ws_bbr_nvm_info_read(&bbr_info_nvm_tlv) < 0) { |
| 856 | + //NVM value not available Randomize Value Here by first time |
| 857 | + ws_bbr_fhss_bsi = randLIB_get_16bit(); |
| 858 | + tr_debug("Randomized init value BSI %u", ws_bbr_fhss_bsi); |
| 859 | + } else { |
| 860 | + ws_bbr_fhss_bsi = ws_bbr_bsi_read(&bbr_info_nvm_tlv); |
| 861 | + tr_debug("Read BSI %u from NVM", ws_bbr_fhss_bsi); |
| 862 | + } |
| 863 | +} |
| 864 | + |
| 865 | + |
| 866 | +uint16_t ws_bbr_bsi_generate(protocol_interface_info_entry_t *interface) |
| 867 | +{ |
| 868 | + (void) interface; |
| 869 | + //Give current one |
| 870 | + uint16_t bsi = ws_bbr_fhss_bsi; |
| 871 | + //Update value for next round |
| 872 | + ws_bbr_fhss_bsi++; |
| 873 | + //Store To NVN |
| 874 | + ws_bbr_bsi_write(&bbr_info_nvm_tlv, ws_bbr_fhss_bsi); |
| 875 | + ws_bbr_nvm_info_write(&bbr_info_nvm_tlv); |
| 876 | + return bsi; |
| 877 | +} |
| 878 | + |
788 | 879 | #endif //HAVE_WS_BORDER_ROUTER
|
789 | 880 |
|
790 | 881 | /* Public APIs
|
@@ -1070,6 +1161,33 @@ int ws_bbr_rpl_parameters_validate(int8_t interface_id, uint8_t dio_interval_min
|
1070 | 1161 | #endif
|
1071 | 1162 | }
|
1072 | 1163 |
|
| 1164 | +int ws_bbr_bsi_set(int8_t interface_id, uint16_t new_bsi) |
| 1165 | +{ |
| 1166 | + (void) interface_id; |
| 1167 | +#ifdef HAVE_WS_BORDER_ROUTER |
| 1168 | + |
| 1169 | + protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(interface_id); |
| 1170 | + |
| 1171 | + //Check if new value is different than current active |
| 1172 | + if (cur && cur->ws_info && cur->lowpan_info & INTERFACE_NWK_ACTIVE) { |
| 1173 | + if (cur->ws_info->hopping_schdule.fhss_bsi == new_bsi) { |
| 1174 | + return 0; |
| 1175 | + } |
| 1176 | + tr_debug("New BSI %u to delayed activate", new_bsi); |
| 1177 | + ws_bootstrap_restart_delayed(cur->id); |
| 1178 | + } |
| 1179 | + |
| 1180 | + ws_bbr_bsi_write(&bbr_info_nvm_tlv, new_bsi); |
| 1181 | + ws_bbr_nvm_info_write(&bbr_info_nvm_tlv); |
| 1182 | + ws_bbr_fhss_bsi = new_bsi; |
| 1183 | + return 0; |
| 1184 | +#else |
| 1185 | + (void) new_bsi; |
| 1186 | + return -1; |
| 1187 | +#endif |
| 1188 | +} |
| 1189 | + |
| 1190 | + |
1073 | 1191 | int ws_bbr_pan_configuration_set(int8_t interface_id, uint16_t pan_id)
|
1074 | 1192 | {
|
1075 | 1193 | (void) interface_id;
|
|
0 commit comments