|
| 1 | +/* |
| 2 | + GSM.h - Library for GSM on mbed platforms. |
| 3 | + Copyright (c) 2011-2023 Arduino LLC. All right reserved. |
| 4 | + |
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | + |
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | + |
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#include <mbed.h> |
| 21 | +#include <GSM.h> |
| 22 | + |
| 23 | +#if MBED_CONF_MBED_TRACE_ENABLE |
| 24 | + |
| 25 | +static Stream* trace_stream = nullptr; |
| 26 | +static PlatformMutex trace_mutex; |
| 27 | +static char trace_timestamp[8]; |
| 28 | + |
| 29 | +static void trace_wait() { |
| 30 | + trace_mutex.lock(); |
| 31 | +} |
| 32 | + |
| 33 | +static void trace_release() { |
| 34 | + trace_mutex.unlock(); |
| 35 | +} |
| 36 | + |
| 37 | +static char* trace_time(size_t ss) { |
| 38 | + auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>(rtos::Kernel::Clock::now()).time_since_epoch().count(); |
| 39 | + snprintf(trace_timestamp, 8, "[%08llu]", ms); |
| 40 | + return trace_timestamp; |
| 41 | +} |
| 42 | + |
| 43 | +static void trace_println(const char* c) { |
| 44 | + if (trace_stream) { |
| 45 | + trace_stream->println(c); |
| 46 | + } |
| 47 | +} |
| 48 | +#endif |
| 49 | + |
| 50 | +void arduino::GSMClass::setTraceLevel(int trace_level, bool timestamp) { |
| 51 | +#if MBED_CONF_MBED_TRACE_ENABLE |
| 52 | + switch(trace_level) { |
| 53 | + case 0: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_NONE); break; |
| 54 | + case 1: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_CMD); break; |
| 55 | + case 2: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ERROR); break; |
| 56 | + case 3: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_WARN); break; |
| 57 | + case 4: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_INFO); break; |
| 58 | + case 5: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_DEBUG); break; |
| 59 | + case 6: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL); break; |
| 60 | + default: mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL); break; |
| 61 | + } |
| 62 | + |
| 63 | + if (timestamp) { |
| 64 | + mbed_trace_prefix_function_set( &trace_time ); |
| 65 | + } |
| 66 | +#endif |
| 67 | +} |
| 68 | + |
| 69 | +void arduino::GSMClass::trace(Stream& stream) { |
| 70 | +#if MBED_CONF_MBED_TRACE_ENABLE |
| 71 | + trace_stream = &stream; |
| 72 | + |
| 73 | + mbed_trace_init(); |
| 74 | + mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL); |
| 75 | + mbed_trace_print_function_set(trace_println); |
| 76 | + mbed_trace_mutex_wait_function_set(trace_wait); |
| 77 | + mbed_trace_mutex_release_function_set(trace_release); |
| 78 | +#endif |
| 79 | +} |
0 commit comments