Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nwk_nvm.c Source File

nwk_nvm.c

00001 /*
00002  * Copyright (c) 2017-2019, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "nsconfig.h"
00018 
00019 #include "ns_types.h"
00020 #include "eventOS_event.h"
00021 #include "ns_trace.h"
00022 #include "string.h"
00023 #include "randLIB.h"
00024 #include "nsdynmemLIB.h"
00025 #include "Core/include/ns_socket.h"
00026 #include "NWK_INTERFACE/Include/protocol.h"
00027 #include "6LoWPAN/MAC/mac_helper.h"
00028 #include "6LoWPAN/NVM/nwk_nvm.h"
00029 #include "Service_Libs/mle_service/mle_service_api.h"
00030 
00031 #define TRACE_GROUP "nNVM"
00032 
00033 #define COUNTER_NVM_UPDATE_INCREMENT 1000
00034 
00035 static wpan_nvm_params_t  *nwk_nvm_params_get_cb(nwk_wpan_nvm_api_t *api, uint16_t pan_id);
00036 
00037 static void nwk_nvm_params_update_cb(nwk_wpan_nvm_api_t *api, bool if_down_call);
00038 
00039 int8_t net_nvm_wpan_params_storage_enable(int8_t interface_id, wpan_params_updated *nvm_update_cb, wpan_params_get *nvm_get_cb)
00040 {
00041 
00042     protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(interface_id);
00043     if (!cur || !nvm_update_cb || !nvm_get_cb) {
00044         return -1;
00045     }
00046 
00047     if (cur->nwk_wpan_nvm_api) {
00048         return 0;
00049     }
00050     cur->nwk_wpan_nvm_api = ns_dyn_mem_alloc(sizeof(nwk_wpan_nvm_api_t));
00051     if (!cur->nwk_wpan_nvm_api) {
00052         return -2;
00053     }
00054 
00055     cur->nwk_wpan_nvm_api->interface = cur;
00056     cur->nwk_wpan_nvm_api->params.pan_id = 0xffff;
00057     cur->nwk_wpan_nvm_api->NVM_GET = nvm_get_cb;
00058     cur->nwk_wpan_nvm_api->NVM_PUSH = nvm_update_cb;
00059     cur->nwk_wpan_nvm_api->nvm_params_get_cb = nwk_nvm_params_get_cb;
00060     cur->nwk_wpan_nvm_api->nvm_params_update_cb = nwk_nvm_params_update_cb;
00061 
00062     return 0;
00063 }
00064 
00065 
00066 int8_t net_nvm_wpan_params_storage_disable(int8_t interface_id)
00067 {
00068     protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(interface_id);
00069     if (!cur || !cur->nwk_wpan_nvm_api) {
00070         return -1;
00071     }
00072     ns_dyn_mem_free(cur->nwk_wpan_nvm_api);
00073     cur->nwk_wpan_nvm_api = NULL;
00074     return 0;
00075 
00076 }
00077 
00078 int8_t net_nvm_wpan_params_storage_reset(int8_t interface_id)
00079 {
00080     protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(interface_id);
00081     if (!cur || !cur->nwk_wpan_nvm_api) {
00082         return -1;
00083     }
00084     cur->nwk_wpan_nvm_api->params.pan_id = 0xffff;
00085     return 0;
00086 }
00087 
00088 
00089 static wpan_nvm_params_t  *nwk_nvm_params_get_cb(nwk_wpan_nvm_api_t *api, uint16_t pan_id)
00090 {
00091     wpan_nvm_params_t temp_params;
00092     if (api->params.pan_id == 0xffff || api->params.pan_id != pan_id) {
00093         temp_params.pan_id = pan_id;
00094         if (api->NVM_GET(&temp_params)) {
00095             temp_params.mac_security_frame_counter += COUNTER_NVM_UPDATE_INCREMENT;
00096             temp_params.mle_securit_counter += COUNTER_NVM_UPDATE_INCREMENT;
00097 
00098         } else {
00099             temp_params.mac_security_frame_counter = 0;
00100             temp_params.mle_securit_counter = 0;
00101 
00102         }
00103         //SET Init values
00104         api->NVM_PUSH(&temp_params);
00105         api->params = temp_params;
00106     }
00107     return &api->params;
00108 }
00109 
00110 static void nwk_nvm_params_update_cb(nwk_wpan_nvm_api_t *api, bool if_down_call)
00111 {
00112     uint32_t mac_counter;
00113     uint32_t mlme_counter;
00114     if (api->params.pan_id == 0xffff) {
00115         return;
00116     }
00117 
00118     //Read first current values and compare different to last read opration
00119     if (mac_helper_link_frame_counter_read(api->interface->id, &mac_counter) != 0) {
00120         return;
00121     }
00122     mlme_counter = mle_service_security_get_frame_counter(api->interface->id);
00123 
00124     if (if_down_call) {
00125         api->params.mac_security_frame_counter = mac_counter;
00126         api->params.mle_securit_counter = mlme_counter;
00127         return;
00128     }
00129 
00130     bool push_new_data = false;
00131     if (api->params.mac_security_frame_counter < mac_counter) {
00132         if (mac_counter - api->params.mac_security_frame_counter > COUNTER_NVM_UPDATE_INCREMENT - 50) {
00133             push_new_data = true;
00134             api->params.mac_security_frame_counter = mac_counter;
00135         }
00136     }
00137 
00138     if (api->params.mle_securit_counter < mlme_counter) {
00139         if (mlme_counter - api->params.mle_securit_counter > COUNTER_NVM_UPDATE_INCREMENT - 50) {
00140             push_new_data = true;
00141             api->params.mle_securit_counter = mlme_counter;
00142         }
00143     }
00144 
00145     if (push_new_data) {
00146         api->NVM_PUSH(&api->params);
00147     }
00148 }