Jim Carver / Mbed OS mbed-cloud-workshop-connect-HTS221

Dependencies:   HTS221

Committer:
JimCarver
Date:
Fri Oct 19 02:01:38 2018 +0000
Revision:
1:521604503e81
Parent:
0:6b753f761943
bug fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JimCarver 0:6b753f761943 1 /*
JimCarver 0:6b753f761943 2 * Copyright (c) 2016 ARM Limited. All rights reserved.
JimCarver 0:6b753f761943 3 * SPDX-License-Identifier: Apache-2.0
JimCarver 0:6b753f761943 4 * Licensed under the Apache License, Version 2.0 (the License); you may
JimCarver 0:6b753f761943 5 * not use this file except in compliance with the License.
JimCarver 0:6b753f761943 6 * You may obtain a copy of the License at
JimCarver 0:6b753f761943 7 *
JimCarver 0:6b753f761943 8 * http://www.apache.org/licenses/LICENSE-2.0
JimCarver 0:6b753f761943 9 *
JimCarver 0:6b753f761943 10 * Unless required by applicable law or agreed to in writing, software
JimCarver 0:6b753f761943 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
JimCarver 0:6b753f761943 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JimCarver 0:6b753f761943 13 * See the License for the specific language governing permissions and
JimCarver 0:6b753f761943 14 * limitations under the License.
JimCarver 0:6b753f761943 15 */
JimCarver 0:6b753f761943 16
JimCarver 0:6b753f761943 17
JimCarver 0:6b753f761943 18
JimCarver 0:6b753f761943 19 // ----------------------------------------------------------- Includes -----------------------------------------------------------
JimCarver 0:6b753f761943 20
JimCarver 0:6b753f761943 21 #include "sotp.h"
JimCarver 0:6b753f761943 22
JimCarver 0:6b753f761943 23 #if (SYS_CONF_SOTP==SYS_CONF_SOTP_LIMITED)
JimCarver 0:6b753f761943 24
JimCarver 0:6b753f761943 25 #include "esfs.h"
JimCarver 0:6b753f761943 26 #include "pal.h"
JimCarver 0:6b753f761943 27 #include <string.h>
JimCarver 0:6b753f761943 28 #include <stdio.h>
JimCarver 0:6b753f761943 29 #include "sotp_int.h"
JimCarver 0:6b753f761943 30
JimCarver 0:6b753f761943 31 #define FILE_NAME_BASE "sotp_type_"
JimCarver 0:6b753f761943 32 #define FILE_NAME_SIZE (sizeof(FILE_NAME_BASE)+4)
JimCarver 0:6b753f761943 33
JimCarver 0:6b753f761943 34 STATIC bool init_done = false;
JimCarver 0:6b753f761943 35
JimCarver 0:6b753f761943 36 static const sotp_type_e otp_types[] = {SOTP_TYPE_TRUSTED_TIME_SRV_ID};
JimCarver 0:6b753f761943 37
JimCarver 0:6b753f761943 38 // --------------------------------------------------------- Definitions ----------------------------------------------------------
JimCarver 0:6b753f761943 39
JimCarver 0:6b753f761943 40
JimCarver 0:6b753f761943 41 // -------------------------------------------------- Local Functions Declaration ----------------------------------------------------
JimCarver 0:6b753f761943 42
JimCarver 0:6b753f761943 43 // -------------------------------------------------- Functions Implementation ----------------------------------------------------
JimCarver 0:6b753f761943 44
JimCarver 0:6b753f761943 45 // Start of API functions
JimCarver 0:6b753f761943 46
JimCarver 0:6b753f761943 47 bool sotp_is_otp_type(uint32_t type)
JimCarver 0:6b753f761943 48 {
JimCarver 0:6b753f761943 49 unsigned int i;
JimCarver 0:6b753f761943 50 for (i = 0; i < sizeof(otp_types) / sizeof(sotp_type_e); i++) {
JimCarver 0:6b753f761943 51 if (otp_types[i] == type) {
JimCarver 0:6b753f761943 52 return true;
JimCarver 0:6b753f761943 53 }
JimCarver 0:6b753f761943 54 }
JimCarver 0:6b753f761943 55 return false;
JimCarver 0:6b753f761943 56 }
JimCarver 0:6b753f761943 57
JimCarver 0:6b753f761943 58 sotp_result_e sotp_get(uint32_t type, uint16_t buf_len_bytes, uint32_t *buf, uint16_t *actual_len_bytes)
JimCarver 0:6b753f761943 59 {
JimCarver 0:6b753f761943 60 esfs_file_t handle;
JimCarver 0:6b753f761943 61 uint16_t mode;
JimCarver 0:6b753f761943 62 char file_name[FILE_NAME_SIZE];
JimCarver 0:6b753f761943 63 esfs_result_e esfs_ret;
JimCarver 0:6b753f761943 64 size_t act_size;
JimCarver 0:6b753f761943 65 sotp_result_e ret;
JimCarver 0:6b753f761943 66
JimCarver 0:6b753f761943 67 if (!init_done) {
JimCarver 0:6b753f761943 68 ret = sotp_init();
JimCarver 0:6b753f761943 69 if (ret != SOTP_SUCCESS)
JimCarver 0:6b753f761943 70 return ret;
JimCarver 0:6b753f761943 71 }
JimCarver 0:6b753f761943 72
JimCarver 0:6b753f761943 73 if (type > SOTP_MAX_TYPES) {
JimCarver 0:6b753f761943 74 return SOTP_BAD_VALUE;
JimCarver 0:6b753f761943 75 }
JimCarver 0:6b753f761943 76
JimCarver 0:6b753f761943 77 memset(&handle, 0, sizeof(handle));
JimCarver 0:6b753f761943 78 sprintf(file_name, "%s%ld", FILE_NAME_BASE, type);
JimCarver 0:6b753f761943 79
JimCarver 0:6b753f761943 80 esfs_ret = esfs_open((uint8_t *)file_name, strlen(file_name), &mode, &handle);
JimCarver 0:6b753f761943 81 if (esfs_ret == ESFS_NOT_EXISTS) {
JimCarver 0:6b753f761943 82 return SOTP_NOT_FOUND;
JimCarver 0:6b753f761943 83 }
JimCarver 0:6b753f761943 84 else if (esfs_ret != ESFS_SUCCESS) {
JimCarver 0:6b753f761943 85 return SOTP_OS_ERROR;
JimCarver 0:6b753f761943 86 }
JimCarver 0:6b753f761943 87
JimCarver 0:6b753f761943 88 if (!buf) {
JimCarver 0:6b753f761943 89 buf_len_bytes = 0;
JimCarver 0:6b753f761943 90 }
JimCarver 0:6b753f761943 91
JimCarver 0:6b753f761943 92 esfs_ret = esfs_file_size(&handle, &act_size);
JimCarver 0:6b753f761943 93 *actual_len_bytes = (uint16_t) act_size;
JimCarver 0:6b753f761943 94 if (esfs_ret != ESFS_SUCCESS) {
JimCarver 0:6b753f761943 95 esfs_close(&handle);
JimCarver 0:6b753f761943 96 return SOTP_READ_ERROR;
JimCarver 0:6b753f761943 97 }
JimCarver 0:6b753f761943 98
JimCarver 0:6b753f761943 99 if (*actual_len_bytes > buf_len_bytes) {
JimCarver 0:6b753f761943 100 esfs_close(&handle);
JimCarver 0:6b753f761943 101 return SOTP_BUFF_TOO_SMALL;
JimCarver 0:6b753f761943 102 }
JimCarver 0:6b753f761943 103
JimCarver 0:6b753f761943 104 if (*actual_len_bytes) {
JimCarver 0:6b753f761943 105 esfs_ret = esfs_read(&handle, buf, buf_len_bytes, &act_size);
JimCarver 0:6b753f761943 106 if (esfs_ret != ESFS_SUCCESS) {
JimCarver 0:6b753f761943 107 esfs_close(&handle);
JimCarver 0:6b753f761943 108 return SOTP_READ_ERROR;
JimCarver 0:6b753f761943 109 }
JimCarver 0:6b753f761943 110 }
JimCarver 0:6b753f761943 111
JimCarver 0:6b753f761943 112 esfs_ret = esfs_close(&handle);
JimCarver 0:6b753f761943 113 if (esfs_ret != ESFS_SUCCESS) {
JimCarver 0:6b753f761943 114 return SOTP_OS_ERROR;
JimCarver 0:6b753f761943 115 }
JimCarver 0:6b753f761943 116
JimCarver 0:6b753f761943 117 return SOTP_SUCCESS;
JimCarver 0:6b753f761943 118 }
JimCarver 0:6b753f761943 119
JimCarver 0:6b753f761943 120 sotp_result_e sotp_get_item_size(uint32_t type, uint16_t *actual_len_bytes)
JimCarver 0:6b753f761943 121 {
JimCarver 0:6b753f761943 122 esfs_file_t handle;
JimCarver 0:6b753f761943 123 uint16_t mode;
JimCarver 0:6b753f761943 124 char file_name[FILE_NAME_SIZE];
JimCarver 0:6b753f761943 125 esfs_result_e esfs_ret;
JimCarver 0:6b753f761943 126 size_t size_bytes;
JimCarver 0:6b753f761943 127 sotp_result_e ret;
JimCarver 0:6b753f761943 128
JimCarver 0:6b753f761943 129 if (!init_done) {
JimCarver 0:6b753f761943 130 ret = sotp_init();
JimCarver 0:6b753f761943 131 if (ret != SOTP_SUCCESS)
JimCarver 0:6b753f761943 132 return ret;
JimCarver 0:6b753f761943 133 }
JimCarver 0:6b753f761943 134
JimCarver 0:6b753f761943 135 if (type > SOTP_MAX_TYPES) {
JimCarver 0:6b753f761943 136 return SOTP_BAD_VALUE;
JimCarver 0:6b753f761943 137 }
JimCarver 0:6b753f761943 138
JimCarver 0:6b753f761943 139 memset(&handle, 0, sizeof(handle));
JimCarver 0:6b753f761943 140 sprintf(file_name, "%s%ld", FILE_NAME_BASE, type);
JimCarver 0:6b753f761943 141
JimCarver 0:6b753f761943 142 esfs_ret = esfs_open((uint8_t *)file_name, strlen(file_name), &mode, &handle);
JimCarver 0:6b753f761943 143 if (esfs_ret == ESFS_NOT_EXISTS) {
JimCarver 0:6b753f761943 144 return SOTP_NOT_FOUND;
JimCarver 0:6b753f761943 145 }
JimCarver 0:6b753f761943 146 if (esfs_ret != ESFS_SUCCESS) {
JimCarver 0:6b753f761943 147 return SOTP_OS_ERROR;
JimCarver 0:6b753f761943 148 }
JimCarver 0:6b753f761943 149
JimCarver 0:6b753f761943 150 esfs_ret = esfs_file_size(&handle, &size_bytes);
JimCarver 0:6b753f761943 151 if (esfs_ret != ESFS_SUCCESS) {
JimCarver 0:6b753f761943 152 esfs_close(&handle);
JimCarver 0:6b753f761943 153 return SOTP_READ_ERROR;
JimCarver 0:6b753f761943 154 }
JimCarver 0:6b753f761943 155
JimCarver 0:6b753f761943 156 esfs_ret = esfs_close(&handle);
JimCarver 0:6b753f761943 157 if (esfs_ret != ESFS_SUCCESS) {
JimCarver 0:6b753f761943 158 return SOTP_OS_ERROR;
JimCarver 0:6b753f761943 159 }
JimCarver 0:6b753f761943 160
JimCarver 0:6b753f761943 161 *actual_len_bytes = (uint16_t) size_bytes;
JimCarver 0:6b753f761943 162 return SOTP_SUCCESS;
JimCarver 0:6b753f761943 163 }
JimCarver 0:6b753f761943 164
JimCarver 0:6b753f761943 165 sotp_result_e sotp_set(uint32_t type, uint16_t buf_len_bytes, const uint32_t *buf)
JimCarver 0:6b753f761943 166 {
JimCarver 0:6b753f761943 167 esfs_file_t handle;
JimCarver 0:6b753f761943 168 uint16_t mode;
JimCarver 0:6b753f761943 169 char file_name[FILE_NAME_SIZE];
JimCarver 0:6b753f761943 170 esfs_result_e esfs_ret;
JimCarver 0:6b753f761943 171 sotp_result_e ret;
JimCarver 0:6b753f761943 172
JimCarver 0:6b753f761943 173 if (!init_done) {
JimCarver 0:6b753f761943 174 ret = sotp_init();
JimCarver 0:6b753f761943 175 if (ret != SOTP_SUCCESS)
JimCarver 0:6b753f761943 176 return ret;
JimCarver 0:6b753f761943 177 }
JimCarver 0:6b753f761943 178
JimCarver 0:6b753f761943 179 if (type > SOTP_MAX_TYPES) {
JimCarver 0:6b753f761943 180 return SOTP_BAD_VALUE;
JimCarver 0:6b753f761943 181 }
JimCarver 0:6b753f761943 182
JimCarver 0:6b753f761943 183 // Only perform actual setting of values on OTP types. Return success for the rest without
JimCarver 0:6b753f761943 184 // doing anything.
JimCarver 0:6b753f761943 185 if (!sotp_is_otp_type(type)) {
JimCarver 0:6b753f761943 186 return SOTP_SUCCESS;
JimCarver 0:6b753f761943 187 }
JimCarver 0:6b753f761943 188
JimCarver 0:6b753f761943 189 memset(&handle, 0, sizeof(handle));
JimCarver 0:6b753f761943 190 sprintf(file_name, "%s%ld", FILE_NAME_BASE, type);
JimCarver 0:6b753f761943 191
JimCarver 0:6b753f761943 192 esfs_ret = esfs_open((uint8_t *)file_name, strlen(file_name), &mode, &handle);
JimCarver 0:6b753f761943 193 if (esfs_ret == ESFS_SUCCESS) {
JimCarver 0:6b753f761943 194 esfs_close(&handle);
JimCarver 0:6b753f761943 195 return SOTP_ALREADY_EXISTS;
JimCarver 0:6b753f761943 196 }
JimCarver 0:6b753f761943 197 if (esfs_ret != ESFS_NOT_EXISTS) {
JimCarver 0:6b753f761943 198 return SOTP_OS_ERROR;
JimCarver 0:6b753f761943 199 }
JimCarver 0:6b753f761943 200
JimCarver 0:6b753f761943 201 esfs_ret = esfs_create((uint8_t *)file_name, strlen(file_name), NULL, 0, ESFS_FACTORY_VAL, &handle);
JimCarver 0:6b753f761943 202 if (esfs_ret != ESFS_SUCCESS) {
JimCarver 0:6b753f761943 203 return SOTP_OS_ERROR;
JimCarver 0:6b753f761943 204 }
JimCarver 0:6b753f761943 205
JimCarver 0:6b753f761943 206 if (buf && buf_len_bytes) {
JimCarver 0:6b753f761943 207 esfs_ret = esfs_write(&handle, buf, buf_len_bytes);
JimCarver 0:6b753f761943 208 if (esfs_ret != ESFS_SUCCESS) {
JimCarver 0:6b753f761943 209 esfs_close(&handle);
JimCarver 0:6b753f761943 210 return SOTP_WRITE_ERROR;
JimCarver 0:6b753f761943 211 }
JimCarver 0:6b753f761943 212 }
JimCarver 0:6b753f761943 213
JimCarver 0:6b753f761943 214 esfs_ret = esfs_close(&handle);
JimCarver 0:6b753f761943 215 if (esfs_ret != ESFS_SUCCESS) {
JimCarver 0:6b753f761943 216 return SOTP_OS_ERROR;
JimCarver 0:6b753f761943 217 }
JimCarver 0:6b753f761943 218
JimCarver 0:6b753f761943 219 return SOTP_SUCCESS;
JimCarver 0:6b753f761943 220 }
JimCarver 0:6b753f761943 221
JimCarver 0:6b753f761943 222 #ifdef SOTP_TESTING
JimCarver 0:6b753f761943 223
JimCarver 0:6b753f761943 224 sotp_result_e sotp_set_for_testing(uint32_t type, uint16_t buf_len_bytes, const uint32_t *buf)
JimCarver 0:6b753f761943 225 {
JimCarver 0:6b753f761943 226 char file_name[FILE_NAME_SIZE];
JimCarver 0:6b753f761943 227 esfs_result_e esfs_ret;
JimCarver 0:6b753f761943 228 sotp_result_e ret;
JimCarver 0:6b753f761943 229
JimCarver 0:6b753f761943 230 if (!init_done) {
JimCarver 0:6b753f761943 231 ret = sotp_init();
JimCarver 0:6b753f761943 232 if (ret != SOTP_SUCCESS)
JimCarver 0:6b753f761943 233 return ret;
JimCarver 0:6b753f761943 234 }
JimCarver 0:6b753f761943 235
JimCarver 0:6b753f761943 236 sprintf(file_name, "%s%ld", FILE_NAME_BASE, type);
JimCarver 0:6b753f761943 237 esfs_ret = esfs_delete((uint8_t *)file_name, strlen(file_name));
JimCarver 0:6b753f761943 238 if ((esfs_ret != ESFS_NOT_EXISTS) && (esfs_ret != ESFS_SUCCESS)) {
JimCarver 0:6b753f761943 239 return SOTP_OS_ERROR;
JimCarver 0:6b753f761943 240 }
JimCarver 0:6b753f761943 241 return sotp_set(type, buf_len_bytes, buf);
JimCarver 0:6b753f761943 242 }
JimCarver 0:6b753f761943 243
JimCarver 0:6b753f761943 244 sotp_result_e sotp_delete(uint32_t type)
JimCarver 0:6b753f761943 245 {
JimCarver 0:6b753f761943 246 char file_name[FILE_NAME_SIZE];
JimCarver 0:6b753f761943 247 esfs_result_e esfs_ret;
JimCarver 0:6b753f761943 248 sotp_result_e ret;
JimCarver 0:6b753f761943 249
JimCarver 0:6b753f761943 250 if (!init_done) {
JimCarver 0:6b753f761943 251 ret = sotp_init();
JimCarver 0:6b753f761943 252 if (ret != SOTP_SUCCESS)
JimCarver 0:6b753f761943 253 return ret;
JimCarver 0:6b753f761943 254 }
JimCarver 0:6b753f761943 255
JimCarver 0:6b753f761943 256 sprintf(file_name, "%s%ld", FILE_NAME_BASE, type);
JimCarver 0:6b753f761943 257 esfs_ret = esfs_delete((uint8_t *)file_name, strlen(file_name));
JimCarver 0:6b753f761943 258 if (esfs_ret == ESFS_SUCCESS)
JimCarver 0:6b753f761943 259 return SOTP_SUCCESS;
JimCarver 0:6b753f761943 260
JimCarver 0:6b753f761943 261 if (esfs_ret == ESFS_NOT_EXISTS)
JimCarver 0:6b753f761943 262 return SOTP_NOT_FOUND;
JimCarver 0:6b753f761943 263
JimCarver 0:6b753f761943 264 return SOTP_OS_ERROR;
JimCarver 0:6b753f761943 265 }
JimCarver 0:6b753f761943 266
JimCarver 0:6b753f761943 267 #endif
JimCarver 0:6b753f761943 268
JimCarver 0:6b753f761943 269 sotp_result_e sotp_init(void)
JimCarver 0:6b753f761943 270 {
JimCarver 0:6b753f761943 271 esfs_result_e esfs_ret;
JimCarver 0:6b753f761943 272
JimCarver 0:6b753f761943 273 if (init_done)
JimCarver 0:6b753f761943 274 return SOTP_SUCCESS;
JimCarver 0:6b753f761943 275
JimCarver 0:6b753f761943 276 esfs_ret = esfs_init();
JimCarver 0:6b753f761943 277 if (esfs_ret != ESFS_SUCCESS) {
JimCarver 0:6b753f761943 278 return SOTP_OS_ERROR;
JimCarver 0:6b753f761943 279 }
JimCarver 0:6b753f761943 280
JimCarver 0:6b753f761943 281 return SOTP_SUCCESS;
JimCarver 0:6b753f761943 282 }
JimCarver 0:6b753f761943 283
JimCarver 0:6b753f761943 284 sotp_result_e sotp_deinit(void)
JimCarver 0:6b753f761943 285 {
JimCarver 0:6b753f761943 286 return SOTP_SUCCESS;
JimCarver 0:6b753f761943 287 }
JimCarver 0:6b753f761943 288
JimCarver 0:6b753f761943 289 sotp_result_e sotp_reset(void)
JimCarver 0:6b753f761943 290 {
JimCarver 0:6b753f761943 291 char file_name[FILE_NAME_SIZE];
JimCarver 0:6b753f761943 292 esfs_result_e esfs_ret;
JimCarver 0:6b753f761943 293 uint32_t type;
JimCarver 0:6b753f761943 294 sotp_result_e ret;
JimCarver 0:6b753f761943 295
JimCarver 0:6b753f761943 296 if (!init_done) {
JimCarver 0:6b753f761943 297 ret = sotp_init();
JimCarver 0:6b753f761943 298 if (ret != SOTP_SUCCESS)
JimCarver 0:6b753f761943 299 return ret;
JimCarver 0:6b753f761943 300 }
JimCarver 0:6b753f761943 301
JimCarver 0:6b753f761943 302 for (type = 0; type < SOTP_MAX_TYPES; type++) {
JimCarver 0:6b753f761943 303 sprintf(file_name, "%s%ld", FILE_NAME_BASE, type);
JimCarver 0:6b753f761943 304
JimCarver 0:6b753f761943 305 esfs_ret = esfs_delete((uint8_t *)file_name, strlen(file_name));
JimCarver 0:6b753f761943 306 if ((esfs_ret != ESFS_NOT_EXISTS) && (esfs_ret != ESFS_SUCCESS)) {
JimCarver 0:6b753f761943 307 return SOTP_OS_ERROR;
JimCarver 0:6b753f761943 308 }
JimCarver 0:6b753f761943 309 }
JimCarver 0:6b753f761943 310
JimCarver 0:6b753f761943 311 return SOTP_SUCCESS;
JimCarver 0:6b753f761943 312 }
JimCarver 0:6b753f761943 313
JimCarver 0:6b753f761943 314 #ifdef SOTP_TESTING
JimCarver 0:6b753f761943 315
JimCarver 0:6b753f761943 316 sotp_result_e sotp_force_garbage_collection(void)
JimCarver 0:6b753f761943 317 {
JimCarver 0:6b753f761943 318 return SOTP_SUCCESS;
JimCarver 0:6b753f761943 319 }
JimCarver 0:6b753f761943 320
JimCarver 0:6b753f761943 321 #endif
JimCarver 0:6b753f761943 322
JimCarver 0:6b753f761943 323 #endif