Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
ws_pae_nvm_store.c
00001 /* 00002 * Copyright (c) 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 00018 #include "nsconfig.h" 00019 #include <string.h> 00020 #include <stdio.h> 00021 #include "ns_types.h" 00022 #include "ns_list.h" 00023 #include "ns_trace.h" 00024 #include "nsdynmemLIB.h" 00025 #include "common_functions.h" 00026 #include "6LoWPAN/ws/ws_config.h" 00027 #include "ns_file_system.h" 00028 #include "Security/protocols/sec_prot_certs.h" 00029 #include "Security/protocols/sec_prot_keys.h" 00030 #include "6LoWPAN/ws/ws_pae_nvm_store.h" 00031 00032 #ifdef HAVE_WS 00033 00034 #define TRACE_GROUP "wsnv" 00035 00036 #define MAX_ROOT_PATH_LEN 200 00037 00038 static uint16_t ws_pae_nvm_store_path_len_get(const char *file_name); 00039 static const char *ws_pae_nvm_store_get_root_path(void); 00040 static int8_t ws_pae_nvm_store_root_path_valid(void); 00041 static int8_t ws_pae_nvm_store_create_path(char *fast_data_path, const char *file_name); 00042 static int8_t ws_pae_nvm_store_write(const char *file_name, nvm_tlv_list_t *tlv_list); 00043 static int8_t ws_pae_nvm_store_read(const char *file_name, nvm_tlv_list_t *tlv_list); 00044 00045 int8_t ws_pae_nvm_store_tlv_file_write(const char *file, nvm_tlv_list_t *tlv_list) 00046 { 00047 if (!ws_pae_nvm_store_root_path_valid()) { 00048 return PAE_NVM_FILE_ROOT_PATH_INVALID; 00049 } 00050 00051 uint16_t path_len = ws_pae_nvm_store_path_len_get(file); 00052 00053 char nw_info_path[path_len]; 00054 00055 ws_pae_nvm_store_create_path(nw_info_path, file); 00056 00057 return ws_pae_nvm_store_write(nw_info_path, tlv_list); 00058 } 00059 00060 int8_t ws_pae_nvm_store_tlv_file_read(const char *file, nvm_tlv_list_t *tlv_list) 00061 { 00062 if (!ws_pae_nvm_store_root_path_valid()) { 00063 return PAE_NVM_FILE_ROOT_PATH_INVALID; 00064 } 00065 00066 uint16_t path_len = ws_pae_nvm_store_path_len_get(file); 00067 00068 char nw_info_path[path_len]; 00069 00070 ws_pae_nvm_store_create_path(nw_info_path, file); 00071 00072 return ws_pae_nvm_store_read(nw_info_path, tlv_list); 00073 } 00074 00075 static const char *ws_pae_nvm_store_get_root_path(void) 00076 { 00077 char *path = ns_file_system_get_root_path(); 00078 00079 if (NULL == path) { 00080 return ""; 00081 } 00082 return path; 00083 } 00084 00085 static int8_t ws_pae_nvm_store_root_path_valid(void) 00086 { 00087 if (NULL == ns_file_system_get_root_path()) { 00088 return 0; 00089 } 00090 int path_len = strlen(ws_pae_nvm_store_get_root_path()); 00091 if (path_len == 0 || path_len > MAX_ROOT_PATH_LEN) { 00092 return 0; 00093 } 00094 return 1; 00095 } 00096 00097 static uint16_t ws_pae_nvm_store_path_len_get(const char *file_name) 00098 { 00099 return strlen(file_name) + strlen(ws_pae_nvm_store_get_root_path()) + 1; 00100 } 00101 00102 static int8_t ws_pae_nvm_store_create_path(char *data_path, const char *file_name) 00103 { 00104 strcpy(data_path, ws_pae_nvm_store_get_root_path()); 00105 strcat(data_path, file_name); 00106 return 0; 00107 } 00108 00109 static int8_t ws_pae_nvm_store_write(const char *file_name, nvm_tlv_list_t *tlv_list) 00110 { 00111 FILE *fp = fopen(file_name, "w"); 00112 if (fp == NULL) { 00113 tr_error("NVM open error: %s", file_name); 00114 return PAE_NVM_FILE_CANNOT_OPEN; 00115 } 00116 00117 uint16_t list_count = ns_list_count(tlv_list); 00118 size_t n_bytes = fwrite(&list_count, 1, sizeof(uint16_t), fp); 00119 if (n_bytes != sizeof(uint16_t)) { 00120 tr_warning("NVM TLV list count write error"); 00121 fclose(fp); 00122 return PAE_NVM_FILE_WRITE_ERROR; 00123 } 00124 00125 bool failure = false; 00126 00127 ns_list_foreach(nvm_tlv_entry_t, entry, tlv_list) { 00128 n_bytes = fwrite(&entry->tag, 1, entry->len + NVM_TLV_FIXED_LEN, fp); 00129 if (n_bytes != (size_t) entry->len + NVM_TLV_FIXED_LEN) { 00130 failure = true; 00131 break; 00132 } 00133 } 00134 00135 fclose(fp); 00136 if (failure) { 00137 tr_error("NVM write error %s", file_name); 00138 return PAE_NVM_FILE_WRITE_ERROR; 00139 } else { 00140 return PAE_NVM_FILE_SUCCESS; 00141 } 00142 } 00143 00144 static int8_t ws_pae_nvm_store_read(const char *file_name, nvm_tlv_list_t *tlv_list) 00145 { 00146 FILE *fp = fopen(file_name, "r"); 00147 if (fp == NULL) { 00148 tr_warning("File not found: %s", file_name); 00149 return PAE_NVM_FILE_CANNOT_OPEN; 00150 } 00151 00152 uint16_t list_count; 00153 size_t n_bytes = fread(&list_count, 1, sizeof(uint16_t), fp); 00154 if (n_bytes != sizeof(uint16_t)) { 00155 tr_warning("NVM TLV list count read error %s", file_name); 00156 fclose(fp); 00157 return PAE_NVM_FILE_READ_ERROR; 00158 } 00159 00160 bool failure = false; 00161 00162 while (list_count-- > 0) { 00163 nvm_tlv_entry_t entry_header; 00164 memset(&entry_header, 0, sizeof(nvm_tlv_entry_t)); 00165 n_bytes = fread(&entry_header.tag, 1, NVM_TLV_FIXED_LEN, fp); 00166 if (n_bytes != NVM_TLV_FIXED_LEN) { 00167 failure = true; 00168 break; 00169 } 00170 uint16_t len = entry_header.len; 00171 00172 nvm_tlv_entry_t *entry = ns_dyn_mem_temporary_alloc(sizeof(nvm_tlv_entry_t) + len); 00173 if (!entry) { 00174 failure = true; 00175 break; 00176 } 00177 00178 memcpy(&entry->tag, &entry_header.tag, NVM_TLV_FIXED_LEN); 00179 00180 if (len > 0) { 00181 uint8_t *data_ptr = ((uint8_t *)&entry->tag) + NVM_TLV_FIXED_LEN; 00182 n_bytes = fread(data_ptr, 1, len, fp); 00183 if (n_bytes != len) { 00184 ns_dyn_mem_free(entry); 00185 failure = true; 00186 break; 00187 } 00188 } 00189 00190 ns_list_add_to_end(tlv_list, entry); 00191 } 00192 00193 fclose(fp); 00194 00195 if (failure) { 00196 ns_list_foreach_safe(nvm_tlv_entry_t, entry, tlv_list) { 00197 ns_list_remove(tlv_list, entry); 00198 ns_dyn_mem_free(entry); 00199 } 00200 tr_error("NVM read error %s", file_name); 00201 return PAE_NVM_FILE_READ_ERROR; 00202 } else { 00203 return PAE_NVM_FILE_SUCCESS; // return how many bytes was written. 00204 } 00205 } 00206 00207 00208 #endif /* HAVE_WS */ 00209
Generated on Tue Jul 12 2022 13:55:04 by
