This program collects raw time series data from the ADC using the NXP board that will later be post processed by PFP Cyber-security cloud base machine learning engine to determine the state of the device.

Dependencies:   FXAS21002 FXOS8700Q

Committer:
vithyat
Date:
Fri Mar 20 20:15:18 2020 +0000
Revision:
2:990c985a69ae
Parent:
0:977e87915078
Update to work with P2Scan runtime

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vithyat 0:977e87915078 1 /*
vithyat 0:977e87915078 2 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
vithyat 0:977e87915078 3 * SPDX-License-Identifier: Apache-2.0
vithyat 0:977e87915078 4 * Licensed under the Apache License, Version 2.0 (the License); you may
vithyat 0:977e87915078 5 * not use this file except in compliance with the License.
vithyat 0:977e87915078 6 * You may obtain a copy of the License at
vithyat 0:977e87915078 7 *
vithyat 0:977e87915078 8 * http://www.apache.org/licenses/LICENSE-2.0
vithyat 0:977e87915078 9 *
vithyat 0:977e87915078 10 * Unless required by applicable law or agreed to in writing, software
vithyat 0:977e87915078 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
vithyat 0:977e87915078 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vithyat 0:977e87915078 13 * See the License for the specific language governing permissions and
vithyat 0:977e87915078 14 * limitations under the License.
vithyat 0:977e87915078 15 */
vithyat 0:977e87915078 16 #include <stdio.h>
vithyat 0:977e87915078 17 #include <string.h>
vithyat 0:977e87915078 18 #include <stdarg.h>
vithyat 0:977e87915078 19
vithyat 0:977e87915078 20 #ifdef MBED_CONF_MBED_TRACE_ENABLE
vithyat 0:977e87915078 21 #undef MBED_CONF_MBED_TRACE_ENABLE
vithyat 0:977e87915078 22 #endif
vithyat 0:977e87915078 23 #define MBED_CONF_MBED_TRACE_ENABLE 1
vithyat 0:977e87915078 24 #ifndef MBED_CONF_MBED_TRACE_FEA_IPV6
vithyat 0:977e87915078 25 #define MBED_CONF_MBED_TRACE_FEA_IPV6 1
vithyat 0:977e87915078 26 #endif
vithyat 0:977e87915078 27
vithyat 0:977e87915078 28 #include "mbed-trace/mbed_trace.h"
vithyat 0:977e87915078 29 #if MBED_CONF_MBED_TRACE_FEA_IPV6 == 1
vithyat 0:977e87915078 30 #include "mbed-client-libservice/ip6string.h"
vithyat 0:977e87915078 31 #include "mbed-client-libservice/common_functions.h"
vithyat 0:977e87915078 32 #endif
vithyat 0:977e87915078 33
vithyat 0:977e87915078 34 #if defined(YOTTA_CFG_MBED_TRACE_MEM)
vithyat 0:977e87915078 35 #define MBED_TRACE_MEM_INCLUDE YOTTA_CFG_MBED_TRACE_MEM_INCLUDE
vithyat 0:977e87915078 36 #define MBED_TRACE_MEM_ALLOC YOTTA_CFG_MBED_TRACE_MEM_ALLOC
vithyat 0:977e87915078 37 #define MBED_TRACE_MEM_FREE YOTTA_CFG_MBED_TRACE_MEM_FREE
vithyat 0:977e87915078 38 #else /* YOTTA_CFG_MEMLIB */
vithyat 0:977e87915078 39 // Default options
vithyat 0:977e87915078 40 #ifndef MBED_TRACE_MEM_INCLUDE
vithyat 0:977e87915078 41 #define MBED_TRACE_MEM_INCLUDE <stdlib.h>
vithyat 0:977e87915078 42 #endif
vithyat 0:977e87915078 43 #include MBED_TRACE_MEM_INCLUDE
vithyat 0:977e87915078 44 #ifndef MBED_TRACE_MEM_ALLOC
vithyat 0:977e87915078 45 #define MBED_TRACE_MEM_ALLOC malloc
vithyat 0:977e87915078 46 #endif
vithyat 0:977e87915078 47 #ifndef MBED_TRACE_MEM_FREE
vithyat 0:977e87915078 48 #define MBED_TRACE_MEM_FREE free
vithyat 0:977e87915078 49 #endif
vithyat 0:977e87915078 50 #endif /* YOTTA_CFG_MEMLIB */
vithyat 0:977e87915078 51
vithyat 0:977e87915078 52 #define VT100_COLOR_ERROR "\x1b[31m"
vithyat 0:977e87915078 53 #define VT100_COLOR_WARN "\x1b[33m"
vithyat 0:977e87915078 54 #define VT100_COLOR_INFO "\x1b[39m"
vithyat 0:977e87915078 55 #define VT100_COLOR_DEBUG "\x1b[90m"
vithyat 0:977e87915078 56
vithyat 0:977e87915078 57 /** default max trace line size in bytes */
vithyat 0:977e87915078 58 #ifdef MBED_TRACE_LINE_LENGTH
vithyat 0:977e87915078 59 #define DEFAULT_TRACE_LINE_LENGTH MBED_TRACE_LINE_LENGTH
vithyat 0:977e87915078 60 #elif defined YOTTA_CFG_MBED_TRACE_LINE_LENGTH
vithyat 0:977e87915078 61 #warning YOTTA_CFG_MBED_TRACE_LINE_LENGTH is deprecated and will be removed in the future! Use MBED_TRACE_LINE_LENGTH instead.
vithyat 0:977e87915078 62 #define DEFAULT_TRACE_LINE_LENGTH YOTTA_CFG_MBED_TRACE_LINE_LENGTH
vithyat 0:977e87915078 63 #else
vithyat 0:977e87915078 64 #define DEFAULT_TRACE_LINE_LENGTH 1024
vithyat 0:977e87915078 65 #endif
vithyat 0:977e87915078 66
vithyat 0:977e87915078 67 /** default max temporary buffer size in bytes, used in
vithyat 0:977e87915078 68 trace_ipv6, trace_ipv6_prefix and trace_array */
vithyat 0:977e87915078 69 #ifdef MBED_TRACE_TMP_LINE_LENGTH
vithyat 0:977e87915078 70 #define DEFAULT_TRACE_TMP_LINE_LEN MBED_TRACE_TMP_LINE_LENGTH
vithyat 0:977e87915078 71 #elif defined YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN
vithyat 0:977e87915078 72 #warning The YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN flag is deprecated and will be removed in the future! Use MBED_TRACE_TMP_LINE_LENGTH instead.
vithyat 0:977e87915078 73 #define DEFAULT_TRACE_TMP_LINE_LEN YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN
vithyat 0:977e87915078 74 #elif defined YOTTA_CFG_MTRACE_TMP_LINE_LEN
vithyat 0:977e87915078 75 #warning The YOTTA_CFG_MTRACE_TMP_LINE_LEN flag is deprecated and will be removed in the future! Use MBED_TRACE_TMP_LINE_LENGTH instead.
vithyat 0:977e87915078 76 #define DEFAULT_TRACE_TMP_LINE_LEN YOTTA_CFG_MTRACE_TMP_LINE_LEN
vithyat 0:977e87915078 77 #else
vithyat 0:977e87915078 78 #define DEFAULT_TRACE_TMP_LINE_LEN 128
vithyat 0:977e87915078 79 #endif
vithyat 0:977e87915078 80
vithyat 0:977e87915078 81 /** default max filters (include/exclude) length in bytes */
vithyat 0:977e87915078 82 #ifdef MBED_TRACE_FILTER_LENGTH
vithyat 0:977e87915078 83 #define DEFAULT_TRACE_FILTER_LENGTH MBED_TRACE_FILTER_LENGTH
vithyat 0:977e87915078 84 #else
vithyat 0:977e87915078 85 #define DEFAULT_TRACE_FILTER_LENGTH 24
vithyat 0:977e87915078 86 #endif
vithyat 0:977e87915078 87
vithyat 0:977e87915078 88 /** default trace configuration bitmask */
vithyat 0:977e87915078 89 #ifdef MBED_TRACE_CONFIG
vithyat 0:977e87915078 90 #define DEFAULT_TRACE_CONFIG MBED_TRACE_CONFIG
vithyat 0:977e87915078 91 #else
vithyat 0:977e87915078 92 #define DEFAULT_TRACE_CONFIG TRACE_MODE_COLOR | TRACE_ACTIVE_LEVEL_ALL | TRACE_CARRIAGE_RETURN
vithyat 0:977e87915078 93 #endif
vithyat 0:977e87915078 94
vithyat 0:977e87915078 95 /** default print function, just redirect str to printf */
vithyat 0:977e87915078 96 static void mbed_trace_realloc( char **buffer, int *length_ptr, int new_length);
vithyat 0:977e87915078 97 static void mbed_trace_default_print(const char *str);
vithyat 0:977e87915078 98 static void mbed_trace_reset_tmp(void);
vithyat 0:977e87915078 99
vithyat 0:977e87915078 100 typedef struct trace_s {
vithyat 0:977e87915078 101 /** trace configuration bits */
vithyat 0:977e87915078 102 uint8_t trace_config;
vithyat 0:977e87915078 103 /** exclude filters list, related group name */
vithyat 0:977e87915078 104 char *filters_exclude;
vithyat 0:977e87915078 105 /** include filters list, related group name */
vithyat 0:977e87915078 106 char *filters_include;
vithyat 0:977e87915078 107 /** Filters length */
vithyat 0:977e87915078 108 int filters_length;
vithyat 0:977e87915078 109 /** trace line */
vithyat 0:977e87915078 110 char *line;
vithyat 0:977e87915078 111 /** trace line length */
vithyat 0:977e87915078 112 int line_length;
vithyat 0:977e87915078 113 /** temporary data */
vithyat 0:977e87915078 114 char *tmp_data;
vithyat 0:977e87915078 115 /** temporary data array length */
vithyat 0:977e87915078 116 int tmp_data_length;
vithyat 0:977e87915078 117 /** temporary data pointer */
vithyat 0:977e87915078 118 char *tmp_data_ptr;
vithyat 0:977e87915078 119
vithyat 0:977e87915078 120 /** prefix function, which can be used to put time to the trace line */
vithyat 0:977e87915078 121 char *(*prefix_f)(size_t);
vithyat 0:977e87915078 122 /** suffix function, which can be used to some string to the end of trace line */
vithyat 0:977e87915078 123 char *(*suffix_f)(void);
vithyat 0:977e87915078 124 /** print out function. Can be redirect to flash for example. */
vithyat 0:977e87915078 125 void (*printf)(const char *);
vithyat 0:977e87915078 126 /** print out function for TRACE_LEVEL_CMD */
vithyat 0:977e87915078 127 void (*cmd_printf)(const char *);
vithyat 0:977e87915078 128 /** mutex wait function which can be called to lock against a mutex. */
vithyat 0:977e87915078 129 void (*mutex_wait_f)(void);
vithyat 0:977e87915078 130 /** mutex release function which must be used to release the mutex locked by mutex_wait_f. */
vithyat 0:977e87915078 131 void (*mutex_release_f)(void);
vithyat 0:977e87915078 132 /** number of times the mutex has been locked */
vithyat 0:977e87915078 133 int mutex_lock_count;
vithyat 0:977e87915078 134 } trace_t;
vithyat 0:977e87915078 135
vithyat 0:977e87915078 136 static trace_t m_trace = {
vithyat 0:977e87915078 137 .trace_config = DEFAULT_TRACE_CONFIG,
vithyat 0:977e87915078 138 .filters_exclude = 0,
vithyat 0:977e87915078 139 .filters_include = 0,
vithyat 0:977e87915078 140 .filters_length = DEFAULT_TRACE_FILTER_LENGTH,
vithyat 0:977e87915078 141 .line = 0,
vithyat 0:977e87915078 142 .line_length = DEFAULT_TRACE_LINE_LENGTH,
vithyat 0:977e87915078 143 .tmp_data = 0,
vithyat 0:977e87915078 144 .tmp_data_length = DEFAULT_TRACE_TMP_LINE_LEN,
vithyat 0:977e87915078 145 .prefix_f = 0,
vithyat 0:977e87915078 146 .suffix_f = 0,
vithyat 0:977e87915078 147 .printf = mbed_trace_default_print,
vithyat 0:977e87915078 148 .cmd_printf = 0,
vithyat 0:977e87915078 149 .mutex_wait_f = 0,
vithyat 0:977e87915078 150 .mutex_release_f = 0,
vithyat 0:977e87915078 151 .mutex_lock_count = 0
vithyat 0:977e87915078 152 };
vithyat 0:977e87915078 153
vithyat 0:977e87915078 154 int mbed_trace_init(void)
vithyat 0:977e87915078 155 {
vithyat 0:977e87915078 156 if (m_trace.line == NULL) {
vithyat 0:977e87915078 157 m_trace.line = MBED_TRACE_MEM_ALLOC(m_trace.line_length);
vithyat 0:977e87915078 158 }
vithyat 0:977e87915078 159
vithyat 0:977e87915078 160 if (m_trace.tmp_data == NULL) {
vithyat 0:977e87915078 161 m_trace.tmp_data = MBED_TRACE_MEM_ALLOC(m_trace.tmp_data_length);
vithyat 0:977e87915078 162 }
vithyat 0:977e87915078 163 m_trace.tmp_data_ptr = m_trace.tmp_data;
vithyat 0:977e87915078 164
vithyat 0:977e87915078 165 if (m_trace.filters_exclude == NULL) {
vithyat 0:977e87915078 166 m_trace.filters_exclude = MBED_TRACE_MEM_ALLOC(m_trace.filters_length);
vithyat 0:977e87915078 167 }
vithyat 0:977e87915078 168 if (m_trace.filters_include == NULL) {
vithyat 0:977e87915078 169 m_trace.filters_include = MBED_TRACE_MEM_ALLOC(m_trace.filters_length);
vithyat 0:977e87915078 170 }
vithyat 0:977e87915078 171
vithyat 0:977e87915078 172 if (m_trace.line == NULL ||
vithyat 0:977e87915078 173 m_trace.tmp_data == NULL ||
vithyat 0:977e87915078 174 m_trace.filters_exclude == NULL ||
vithyat 0:977e87915078 175 m_trace.filters_include == NULL) {
vithyat 0:977e87915078 176 //memory allocation fail
vithyat 0:977e87915078 177 mbed_trace_free();
vithyat 0:977e87915078 178 return -1;
vithyat 0:977e87915078 179 }
vithyat 0:977e87915078 180 memset(m_trace.tmp_data, 0, m_trace.tmp_data_length);
vithyat 0:977e87915078 181 memset(m_trace.filters_exclude, 0, m_trace.filters_length);
vithyat 0:977e87915078 182 memset(m_trace.filters_include, 0, m_trace.filters_length);
vithyat 0:977e87915078 183 memset(m_trace.line, 0, m_trace.line_length);
vithyat 0:977e87915078 184
vithyat 0:977e87915078 185 return 0;
vithyat 0:977e87915078 186 }
vithyat 0:977e87915078 187 void mbed_trace_free(void)
vithyat 0:977e87915078 188 {
vithyat 0:977e87915078 189 // release memory
vithyat 0:977e87915078 190 MBED_TRACE_MEM_FREE(m_trace.line);
vithyat 0:977e87915078 191 MBED_TRACE_MEM_FREE(m_trace.tmp_data);
vithyat 0:977e87915078 192 MBED_TRACE_MEM_FREE(m_trace.filters_exclude);
vithyat 0:977e87915078 193 MBED_TRACE_MEM_FREE(m_trace.filters_include);
vithyat 0:977e87915078 194
vithyat 0:977e87915078 195 // reset to default values
vithyat 0:977e87915078 196 m_trace.trace_config = DEFAULT_TRACE_CONFIG;
vithyat 0:977e87915078 197 m_trace.filters_exclude = 0;
vithyat 0:977e87915078 198 m_trace.filters_include = 0;
vithyat 0:977e87915078 199 m_trace.filters_length = DEFAULT_TRACE_FILTER_LENGTH;
vithyat 0:977e87915078 200 m_trace.line = 0;
vithyat 0:977e87915078 201 m_trace.line_length = DEFAULT_TRACE_LINE_LENGTH;
vithyat 0:977e87915078 202 m_trace.tmp_data = 0;
vithyat 0:977e87915078 203 m_trace.tmp_data_length = DEFAULT_TRACE_TMP_LINE_LEN;
vithyat 0:977e87915078 204 m_trace.prefix_f = 0;
vithyat 0:977e87915078 205 m_trace.suffix_f = 0;
vithyat 0:977e87915078 206 m_trace.printf = mbed_trace_default_print;
vithyat 0:977e87915078 207 m_trace.cmd_printf = 0;
vithyat 0:977e87915078 208 m_trace.mutex_wait_f = 0;
vithyat 0:977e87915078 209 m_trace.mutex_release_f = 0;
vithyat 0:977e87915078 210 m_trace.mutex_lock_count = 0;
vithyat 0:977e87915078 211 }
vithyat 0:977e87915078 212 static void mbed_trace_realloc( char **buffer, int *length_ptr, int new_length)
vithyat 0:977e87915078 213 {
vithyat 0:977e87915078 214 MBED_TRACE_MEM_FREE(*buffer);
vithyat 0:977e87915078 215 *buffer = MBED_TRACE_MEM_ALLOC(new_length);
vithyat 0:977e87915078 216 *length_ptr = new_length;
vithyat 0:977e87915078 217 }
vithyat 0:977e87915078 218 void mbed_trace_buffer_sizes(int lineLength, int tmpLength)
vithyat 0:977e87915078 219 {
vithyat 0:977e87915078 220 if( lineLength > 0 ) {
vithyat 0:977e87915078 221 mbed_trace_realloc( &(m_trace.line), &m_trace.line_length, lineLength );
vithyat 0:977e87915078 222 }
vithyat 0:977e87915078 223 if( tmpLength > 0 ) {
vithyat 0:977e87915078 224 mbed_trace_realloc( &(m_trace.tmp_data), &m_trace.tmp_data_length, tmpLength);
vithyat 0:977e87915078 225 mbed_trace_reset_tmp();
vithyat 0:977e87915078 226 }
vithyat 0:977e87915078 227 }
vithyat 0:977e87915078 228 void mbed_trace_config_set(uint8_t config)
vithyat 0:977e87915078 229 {
vithyat 0:977e87915078 230 m_trace.trace_config = config;
vithyat 0:977e87915078 231 }
vithyat 0:977e87915078 232 uint8_t mbed_trace_config_get(void)
vithyat 0:977e87915078 233 {
vithyat 0:977e87915078 234 return m_trace.trace_config;
vithyat 0:977e87915078 235 }
vithyat 0:977e87915078 236 void mbed_trace_prefix_function_set(char *(*pref_f)(size_t))
vithyat 0:977e87915078 237 {
vithyat 0:977e87915078 238 m_trace.prefix_f = pref_f;
vithyat 0:977e87915078 239 }
vithyat 0:977e87915078 240 void mbed_trace_suffix_function_set(char *(*suffix_f)(void))
vithyat 0:977e87915078 241 {
vithyat 0:977e87915078 242 m_trace.suffix_f = suffix_f;
vithyat 0:977e87915078 243 }
vithyat 0:977e87915078 244 void mbed_trace_print_function_set(void (*printf)(const char *))
vithyat 0:977e87915078 245 {
vithyat 0:977e87915078 246 m_trace.printf = printf;
vithyat 0:977e87915078 247 }
vithyat 0:977e87915078 248 void mbed_trace_cmdprint_function_set(void (*printf)(const char *))
vithyat 0:977e87915078 249 {
vithyat 0:977e87915078 250 m_trace.cmd_printf = printf;
vithyat 0:977e87915078 251 }
vithyat 0:977e87915078 252 void mbed_trace_mutex_wait_function_set(void (*mutex_wait_f)(void))
vithyat 0:977e87915078 253 {
vithyat 0:977e87915078 254 m_trace.mutex_wait_f = mutex_wait_f;
vithyat 0:977e87915078 255 }
vithyat 0:977e87915078 256 void mbed_trace_mutex_release_function_set(void (*mutex_release_f)(void))
vithyat 0:977e87915078 257 {
vithyat 0:977e87915078 258 m_trace.mutex_release_f = mutex_release_f;
vithyat 0:977e87915078 259 }
vithyat 0:977e87915078 260 void mbed_trace_exclude_filters_set(char *filters)
vithyat 0:977e87915078 261 {
vithyat 0:977e87915078 262 if (filters) {
vithyat 0:977e87915078 263 (void)strncpy(m_trace.filters_exclude, filters, m_trace.filters_length);
vithyat 0:977e87915078 264 } else {
vithyat 0:977e87915078 265 m_trace.filters_exclude[0] = 0;
vithyat 0:977e87915078 266 }
vithyat 0:977e87915078 267 }
vithyat 0:977e87915078 268 const char *mbed_trace_exclude_filters_get(void)
vithyat 0:977e87915078 269 {
vithyat 0:977e87915078 270 return m_trace.filters_exclude;
vithyat 0:977e87915078 271 }
vithyat 0:977e87915078 272 const char *mbed_trace_include_filters_get(void)
vithyat 0:977e87915078 273 {
vithyat 0:977e87915078 274 return m_trace.filters_include;
vithyat 0:977e87915078 275 }
vithyat 0:977e87915078 276 void mbed_trace_include_filters_set(char *filters)
vithyat 0:977e87915078 277 {
vithyat 0:977e87915078 278 if (filters) {
vithyat 0:977e87915078 279 (void)strncpy(m_trace.filters_include, filters, m_trace.filters_length);
vithyat 0:977e87915078 280 } else {
vithyat 0:977e87915078 281 m_trace.filters_include[0] = 0;
vithyat 0:977e87915078 282 }
vithyat 0:977e87915078 283 }
vithyat 0:977e87915078 284 static int8_t mbed_trace_skip(int8_t dlevel, const char *grp)
vithyat 0:977e87915078 285 {
vithyat 0:977e87915078 286 if (dlevel >= 0 && grp != 0) {
vithyat 0:977e87915078 287 // filter debug prints only when dlevel is >0 and grp is given
vithyat 0:977e87915078 288
vithyat 0:977e87915078 289 /// @TODO this could be much better..
vithyat 0:977e87915078 290 if (m_trace.filters_exclude[0] != '\0' &&
vithyat 0:977e87915078 291 strstr(m_trace.filters_exclude, grp) != 0) {
vithyat 0:977e87915078 292 //grp was in exclude list
vithyat 0:977e87915078 293 return 1;
vithyat 0:977e87915078 294 }
vithyat 0:977e87915078 295 if (m_trace.filters_include[0] != '\0' &&
vithyat 0:977e87915078 296 strstr(m_trace.filters_include, grp) == 0) {
vithyat 0:977e87915078 297 //grp was in include list
vithyat 0:977e87915078 298 return 1;
vithyat 0:977e87915078 299 }
vithyat 0:977e87915078 300 }
vithyat 0:977e87915078 301 return 0;
vithyat 0:977e87915078 302 }
vithyat 0:977e87915078 303 static void mbed_trace_default_print(const char *str)
vithyat 0:977e87915078 304 {
vithyat 0:977e87915078 305 puts(str);
vithyat 0:977e87915078 306 }
vithyat 0:977e87915078 307 void mbed_tracef(uint8_t dlevel, const char *grp, const char *fmt, ...)
vithyat 0:977e87915078 308 {
vithyat 0:977e87915078 309 va_list ap;
vithyat 0:977e87915078 310 va_start(ap, fmt);
vithyat 0:977e87915078 311 mbed_vtracef(dlevel, grp, fmt, ap);
vithyat 0:977e87915078 312 va_end(ap);
vithyat 0:977e87915078 313 }
vithyat 0:977e87915078 314 void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap)
vithyat 0:977e87915078 315 {
vithyat 0:977e87915078 316 if ( m_trace.mutex_wait_f ) {
vithyat 0:977e87915078 317 m_trace.mutex_wait_f();
vithyat 0:977e87915078 318 m_trace.mutex_lock_count++;
vithyat 0:977e87915078 319 }
vithyat 0:977e87915078 320
vithyat 0:977e87915078 321 if (NULL == m_trace.line) {
vithyat 0:977e87915078 322 goto end;
vithyat 0:977e87915078 323 }
vithyat 0:977e87915078 324
vithyat 0:977e87915078 325 m_trace.line[0] = 0; //by default trace is empty
vithyat 0:977e87915078 326
vithyat 0:977e87915078 327 if (mbed_trace_skip(dlevel, grp) || fmt == 0 || grp == 0 || !m_trace.printf) {
vithyat 0:977e87915078 328 //return tmp data pointer back to the beginning
vithyat 0:977e87915078 329 mbed_trace_reset_tmp();
vithyat 0:977e87915078 330 goto end;
vithyat 0:977e87915078 331 }
vithyat 0:977e87915078 332 if ((m_trace.trace_config & TRACE_MASK_LEVEL) & dlevel) {
vithyat 0:977e87915078 333 bool color = (m_trace.trace_config & TRACE_MODE_COLOR) != 0;
vithyat 0:977e87915078 334 bool plain = (m_trace.trace_config & TRACE_MODE_PLAIN) != 0;
vithyat 0:977e87915078 335 bool cr = (m_trace.trace_config & TRACE_CARRIAGE_RETURN) != 0;
vithyat 0:977e87915078 336
vithyat 0:977e87915078 337 int retval = 0, bLeft = m_trace.line_length;
vithyat 0:977e87915078 338 char *ptr = m_trace.line;
vithyat 0:977e87915078 339 if (plain == true || dlevel == TRACE_LEVEL_CMD) {
vithyat 0:977e87915078 340 //add trace data
vithyat 0:977e87915078 341 retval = vsnprintf(ptr, bLeft, fmt, ap);
vithyat 0:977e87915078 342 if (dlevel == TRACE_LEVEL_CMD && m_trace.cmd_printf) {
vithyat 0:977e87915078 343 m_trace.cmd_printf(m_trace.line);
vithyat 0:977e87915078 344 m_trace.cmd_printf("\n");
vithyat 0:977e87915078 345 } else {
vithyat 0:977e87915078 346 //print out whole data
vithyat 0:977e87915078 347 m_trace.printf(m_trace.line);
vithyat 0:977e87915078 348 }
vithyat 0:977e87915078 349 } else {
vithyat 0:977e87915078 350 if (color) {
vithyat 0:977e87915078 351 if (cr) {
vithyat 0:977e87915078 352 retval = snprintf(ptr, bLeft, "\r\x1b[2K");
vithyat 0:977e87915078 353 if (retval >= bLeft) {
vithyat 0:977e87915078 354 retval = 0;
vithyat 0:977e87915078 355 }
vithyat 0:977e87915078 356 if (retval > 0) {
vithyat 0:977e87915078 357 ptr += retval;
vithyat 0:977e87915078 358 bLeft -= retval;
vithyat 0:977e87915078 359 }
vithyat 0:977e87915078 360 }
vithyat 0:977e87915078 361 if (bLeft > 0) {
vithyat 0:977e87915078 362 //include color in ANSI/VT100 escape code
vithyat 0:977e87915078 363 switch (dlevel) {
vithyat 0:977e87915078 364 case (TRACE_LEVEL_ERROR):
vithyat 0:977e87915078 365 retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_ERROR);
vithyat 0:977e87915078 366 break;
vithyat 0:977e87915078 367 case (TRACE_LEVEL_WARN):
vithyat 0:977e87915078 368 retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_WARN);
vithyat 0:977e87915078 369 break;
vithyat 0:977e87915078 370 case (TRACE_LEVEL_INFO):
vithyat 0:977e87915078 371 retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_INFO);
vithyat 0:977e87915078 372 break;
vithyat 0:977e87915078 373 case (TRACE_LEVEL_DEBUG):
vithyat 0:977e87915078 374 retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_DEBUG);
vithyat 0:977e87915078 375 break;
vithyat 0:977e87915078 376 default:
vithyat 0:977e87915078 377 color = 0; //avoid unneeded color-terminate code
vithyat 0:977e87915078 378 retval = 0;
vithyat 0:977e87915078 379 break;
vithyat 0:977e87915078 380 }
vithyat 0:977e87915078 381 if (retval >= bLeft) {
vithyat 0:977e87915078 382 retval = 0;
vithyat 0:977e87915078 383 }
vithyat 0:977e87915078 384 if (retval > 0 && color) {
vithyat 0:977e87915078 385 ptr += retval;
vithyat 0:977e87915078 386 bLeft -= retval;
vithyat 0:977e87915078 387 }
vithyat 0:977e87915078 388 }
vithyat 0:977e87915078 389
vithyat 0:977e87915078 390 }
vithyat 0:977e87915078 391 if (bLeft > 0 && m_trace.prefix_f) {
vithyat 0:977e87915078 392 //find out length of body
vithyat 0:977e87915078 393 size_t sz = 0;
vithyat 0:977e87915078 394 va_list ap2;
vithyat 0:977e87915078 395 va_copy(ap2, ap);
vithyat 0:977e87915078 396 sz = vsnprintf(NULL, 0, fmt, ap2) + retval + (retval ? 4 : 0);
vithyat 0:977e87915078 397 va_end(ap2);
vithyat 0:977e87915078 398 //add prefix string
vithyat 0:977e87915078 399 retval = snprintf(ptr, bLeft, "%s", m_trace.prefix_f(sz));
vithyat 0:977e87915078 400 if (retval >= bLeft) {
vithyat 0:977e87915078 401 retval = 0;
vithyat 0:977e87915078 402 }
vithyat 0:977e87915078 403 if (retval > 0) {
vithyat 0:977e87915078 404 ptr += retval;
vithyat 0:977e87915078 405 bLeft -= retval;
vithyat 0:977e87915078 406 }
vithyat 0:977e87915078 407 }
vithyat 0:977e87915078 408 if (bLeft > 0) {
vithyat 0:977e87915078 409 //add group tag
vithyat 0:977e87915078 410 switch (dlevel) {
vithyat 0:977e87915078 411 case (TRACE_LEVEL_ERROR):
vithyat 0:977e87915078 412 retval = snprintf(ptr, bLeft, "[ERR ][%-4s]: ", grp);
vithyat 0:977e87915078 413 break;
vithyat 0:977e87915078 414 case (TRACE_LEVEL_WARN):
vithyat 0:977e87915078 415 retval = snprintf(ptr, bLeft, "[WARN][%-4s]: ", grp);
vithyat 0:977e87915078 416 break;
vithyat 0:977e87915078 417 case (TRACE_LEVEL_INFO):
vithyat 0:977e87915078 418 retval = snprintf(ptr, bLeft, "[INFO][%-4s]: ", grp);
vithyat 0:977e87915078 419 break;
vithyat 0:977e87915078 420 case (TRACE_LEVEL_DEBUG):
vithyat 0:977e87915078 421 retval = snprintf(ptr, bLeft, "[DBG ][%-4s]: ", grp);
vithyat 0:977e87915078 422 break;
vithyat 0:977e87915078 423 default:
vithyat 0:977e87915078 424 retval = snprintf(ptr, bLeft, " ");
vithyat 0:977e87915078 425 break;
vithyat 0:977e87915078 426 }
vithyat 0:977e87915078 427 if (retval >= bLeft) {
vithyat 0:977e87915078 428 retval = 0;
vithyat 0:977e87915078 429 }
vithyat 0:977e87915078 430 if (retval > 0) {
vithyat 0:977e87915078 431 ptr += retval;
vithyat 0:977e87915078 432 bLeft -= retval;
vithyat 0:977e87915078 433 }
vithyat 0:977e87915078 434 }
vithyat 0:977e87915078 435 if (retval > 0 && bLeft > 0) {
vithyat 0:977e87915078 436 //add trace text
vithyat 0:977e87915078 437 retval = vsnprintf(ptr, bLeft, fmt, ap);
vithyat 0:977e87915078 438 if (retval >= bLeft) {
vithyat 0:977e87915078 439 retval = 0;
vithyat 0:977e87915078 440 }
vithyat 0:977e87915078 441 if (retval > 0) {
vithyat 0:977e87915078 442 ptr += retval;
vithyat 0:977e87915078 443 bLeft -= retval;
vithyat 0:977e87915078 444 }
vithyat 0:977e87915078 445 }
vithyat 0:977e87915078 446
vithyat 0:977e87915078 447 if (retval > 0 && bLeft > 0 && m_trace.suffix_f) {
vithyat 0:977e87915078 448 //add suffix string
vithyat 0:977e87915078 449 retval = snprintf(ptr, bLeft, "%s", m_trace.suffix_f());
vithyat 0:977e87915078 450 if (retval >= bLeft) {
vithyat 0:977e87915078 451 retval = 0;
vithyat 0:977e87915078 452 }
vithyat 0:977e87915078 453 if (retval > 0) {
vithyat 0:977e87915078 454 ptr += retval;
vithyat 0:977e87915078 455 bLeft -= retval;
vithyat 0:977e87915078 456 }
vithyat 0:977e87915078 457 }
vithyat 0:977e87915078 458
vithyat 0:977e87915078 459 if (retval > 0 && bLeft > 0 && color) {
vithyat 0:977e87915078 460 //add zero color VT100 when color mode
vithyat 0:977e87915078 461 retval = snprintf(ptr, bLeft, "\x1b[0m");
vithyat 0:977e87915078 462 if (retval >= bLeft) {
vithyat 0:977e87915078 463 retval = 0;
vithyat 0:977e87915078 464 }
vithyat 0:977e87915078 465 if (retval > 0) {
vithyat 0:977e87915078 466 // not used anymore
vithyat 0:977e87915078 467 //ptr += retval;
vithyat 0:977e87915078 468 //bLeft -= retval;
vithyat 0:977e87915078 469 }
vithyat 0:977e87915078 470 }
vithyat 0:977e87915078 471 //print out whole data
vithyat 0:977e87915078 472 m_trace.printf(m_trace.line);
vithyat 0:977e87915078 473 }
vithyat 0:977e87915078 474 //return tmp data pointer back to the beginning
vithyat 0:977e87915078 475 mbed_trace_reset_tmp();
vithyat 0:977e87915078 476 }
vithyat 0:977e87915078 477
vithyat 0:977e87915078 478 end:
vithyat 0:977e87915078 479 if ( m_trace.mutex_release_f ) {
vithyat 0:977e87915078 480 // Store the mutex lock count to temp variable so that it won't get
vithyat 0:977e87915078 481 // clobbered during last loop iteration when mutex gets released
vithyat 0:977e87915078 482 int count = m_trace.mutex_lock_count;
vithyat 0:977e87915078 483 m_trace.mutex_lock_count = 0;
vithyat 0:977e87915078 484 // Since the helper functions (eg. mbed_trace_array) are used like this:
vithyat 0:977e87915078 485 // mbed_tracef(TRACE_LEVEL_INFO, "grp", "%s", mbed_trace_array(some_array))
vithyat 0:977e87915078 486 // The helper function MUST acquire the mutex if it modifies any buffers. However
vithyat 0:977e87915078 487 // it CANNOT unlock the mutex because that would allow another thread to acquire
vithyat 0:977e87915078 488 // the mutex after helper function unlocks it and before mbed_tracef acquires it
vithyat 0:977e87915078 489 // for itself. This means that here we have to unlock the mutex as many times
vithyat 0:977e87915078 490 // as it was acquired by trace function and any possible helper functions.
vithyat 0:977e87915078 491 do {
vithyat 0:977e87915078 492 m_trace.mutex_release_f();
vithyat 0:977e87915078 493 } while (--count > 0);
vithyat 0:977e87915078 494 }
vithyat 0:977e87915078 495 }
vithyat 0:977e87915078 496 static void mbed_trace_reset_tmp(void)
vithyat 0:977e87915078 497 {
vithyat 0:977e87915078 498 m_trace.tmp_data_ptr = m_trace.tmp_data;
vithyat 0:977e87915078 499 }
vithyat 0:977e87915078 500 const char *mbed_trace_last(void)
vithyat 0:977e87915078 501 {
vithyat 0:977e87915078 502 return m_trace.line;
vithyat 0:977e87915078 503 }
vithyat 0:977e87915078 504 /* Helping functions */
vithyat 0:977e87915078 505 #define tmp_data_left() m_trace.tmp_data_length-(m_trace.tmp_data_ptr-m_trace.tmp_data)
vithyat 0:977e87915078 506 #if MBED_CONF_MBED_TRACE_FEA_IPV6 == 1
vithyat 0:977e87915078 507 char *mbed_trace_ipv6(const void *addr_ptr)
vithyat 0:977e87915078 508 {
vithyat 0:977e87915078 509 /** Acquire mutex. It is released before returning from mbed_vtracef. */
vithyat 0:977e87915078 510 if ( m_trace.mutex_wait_f ) {
vithyat 0:977e87915078 511 m_trace.mutex_wait_f();
vithyat 0:977e87915078 512 m_trace.mutex_lock_count++;
vithyat 0:977e87915078 513 }
vithyat 0:977e87915078 514 char *str = m_trace.tmp_data_ptr;
vithyat 0:977e87915078 515 if (str == NULL) {
vithyat 0:977e87915078 516 return "";
vithyat 0:977e87915078 517 }
vithyat 0:977e87915078 518 if (tmp_data_left() < 41) {
vithyat 0:977e87915078 519 return "";
vithyat 0:977e87915078 520 }
vithyat 0:977e87915078 521 if (addr_ptr == NULL) {
vithyat 0:977e87915078 522 return "<null>";
vithyat 0:977e87915078 523 }
vithyat 0:977e87915078 524 str[0] = 0;
vithyat 0:977e87915078 525 m_trace.tmp_data_ptr += ip6tos(addr_ptr, str) + 1;
vithyat 0:977e87915078 526 return str;
vithyat 0:977e87915078 527 }
vithyat 0:977e87915078 528 char *mbed_trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len)
vithyat 0:977e87915078 529 {
vithyat 0:977e87915078 530 /** Acquire mutex. It is released before returning from mbed_vtracef. */
vithyat 0:977e87915078 531 if ( m_trace.mutex_wait_f ) {
vithyat 0:977e87915078 532 m_trace.mutex_wait_f();
vithyat 0:977e87915078 533 m_trace.mutex_lock_count++;
vithyat 0:977e87915078 534 }
vithyat 0:977e87915078 535 char *str = m_trace.tmp_data_ptr;
vithyat 0:977e87915078 536 if (str == NULL) {
vithyat 0:977e87915078 537 return "";
vithyat 0:977e87915078 538 }
vithyat 0:977e87915078 539 if (tmp_data_left() < 45) {
vithyat 0:977e87915078 540 return "";
vithyat 0:977e87915078 541 }
vithyat 0:977e87915078 542
vithyat 0:977e87915078 543 if ((prefix_len != 0 && prefix == NULL) || prefix_len > 128) {
vithyat 0:977e87915078 544 return "<err>";
vithyat 0:977e87915078 545 }
vithyat 0:977e87915078 546
vithyat 0:977e87915078 547 m_trace.tmp_data_ptr += ip6_prefix_tos(prefix, prefix_len, str) + 1;
vithyat 0:977e87915078 548 return str;
vithyat 0:977e87915078 549 }
vithyat 0:977e87915078 550 #endif //MBED_CONF_MBED_TRACE_FEA_IPV6
vithyat 0:977e87915078 551 char *mbed_trace_array(const uint8_t *buf, uint16_t len)
vithyat 0:977e87915078 552 {
vithyat 0:977e87915078 553 /** Acquire mutex. It is released before returning from mbed_vtracef. */
vithyat 0:977e87915078 554 if ( m_trace.mutex_wait_f ) {
vithyat 0:977e87915078 555 m_trace.mutex_wait_f();
vithyat 0:977e87915078 556 m_trace.mutex_lock_count++;
vithyat 0:977e87915078 557 }
vithyat 0:977e87915078 558 int i, bLeft = tmp_data_left();
vithyat 0:977e87915078 559 char *str, *wptr;
vithyat 0:977e87915078 560 str = m_trace.tmp_data_ptr;
vithyat 0:977e87915078 561 if (len == 0 || str == NULL || bLeft == 0) {
vithyat 0:977e87915078 562 return "";
vithyat 0:977e87915078 563 }
vithyat 0:977e87915078 564 if (buf == NULL) {
vithyat 0:977e87915078 565 return "<null>";
vithyat 0:977e87915078 566 }
vithyat 0:977e87915078 567 wptr = str;
vithyat 0:977e87915078 568 wptr[0] = 0;
vithyat 0:977e87915078 569 const uint8_t *ptr = buf;
vithyat 0:977e87915078 570 char overflow = 0;
vithyat 0:977e87915078 571 for (i = 0; i < len; i++) {
vithyat 0:977e87915078 572 if (bLeft <= 3) {
vithyat 0:977e87915078 573 overflow = 1;
vithyat 0:977e87915078 574 break;
vithyat 0:977e87915078 575 }
vithyat 0:977e87915078 576 int retval = snprintf(wptr, bLeft, "%02x:", *ptr++);
vithyat 0:977e87915078 577 if (retval <= 0 || retval > bLeft) {
vithyat 0:977e87915078 578 break;
vithyat 0:977e87915078 579 }
vithyat 0:977e87915078 580 bLeft -= retval;
vithyat 0:977e87915078 581 wptr += retval;
vithyat 0:977e87915078 582 }
vithyat 0:977e87915078 583 if (wptr > str) {
vithyat 0:977e87915078 584 if( overflow ) {
vithyat 0:977e87915078 585 // replace last character as 'star',
vithyat 0:977e87915078 586 // which indicate buffer len is not enough
vithyat 0:977e87915078 587 *(wptr - 1) = '*';
vithyat 0:977e87915078 588 } else {
vithyat 0:977e87915078 589 //null to replace last ':' character
vithyat 0:977e87915078 590 *(wptr - 1) = 0;
vithyat 0:977e87915078 591 }
vithyat 0:977e87915078 592 }
vithyat 0:977e87915078 593 m_trace.tmp_data_ptr = wptr;
vithyat 0:977e87915078 594 return str;
vithyat 0:977e87915078 595 }