Skip to content

Commit bed5201

Browse files
committed
GSM: rename debug() method into trace()
* Move functions in a separate GSMTrace.cpp file * Add function to set trace level and enable/disable trace timestamps * Cleanup
1 parent 66b026a commit bed5201

File tree

3 files changed

+81
-47
lines changed

3 files changed

+81
-47
lines changed

libraries/GSM/src/GSM.cpp

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -157,53 +157,7 @@ bool arduino::GSMClass::isConnected()
157157
}
158158
}
159159

160-
static PlatformMutex trace_mutex;
161160

162-
static void trace_wait()
163-
{
164-
trace_mutex.lock();
165-
}
166-
167-
static void trace_release()
168-
{
169-
trace_mutex.unlock();
170-
}
171-
172-
static char* trace_time(size_t ss)
173-
{
174-
static char time_st[50];
175-
auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>(rtos::Kernel::Clock::now()).time_since_epoch().count();
176-
//snprintf(time_st, 49, "[%08llums]", ms);
177-
snprintf(time_st, 1, "\n");
178-
return time_st;
179-
}
180-
181-
static Stream* trace_stream = nullptr;
182-
static void arduino_print(const char* c) {
183-
if (trace_stream) {
184-
trace_stream->println(c);
185-
}
186-
}
187-
188-
void arduino::GSMClass::debug(Stream& stream) {
189-
190-
#if MBED_CONF_MBED_TRACE_ENABLE
191-
192-
mbed_trace_init();
193-
194-
trace_stream = &stream;
195-
mbed_trace_print_function_set(arduino_print);
196-
mbed_trace_prefix_function_set( &trace_time );
197-
198-
mbed_trace_mutex_wait_function_set(trace_wait);
199-
mbed_trace_mutex_release_function_set(trace_release);
200-
201-
mbed_cellular_trace::mutex_wait_function_set(trace_wait);
202-
mbed_cellular_trace::mutex_release_function_set(trace_release);
203-
204-
#endif
205-
206-
}
207161

208162
NetworkInterface* arduino::GSMClass::getNetwork() {
209163
return _context;

libraries/GSM/src/GSM.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ class GSMClass : public MbedSocketClass {
9090
bool setTime(unsigned long const epoch, int const timezone = 0);
9191
void enableCmux();
9292
bool isCmuxEnable();
93-
void debug(Stream& stream);
93+
void trace(Stream& stream);
94+
void setTraceLevel(int trace_level, bool timestamp = false);
9495
int ping(const char* hostname, uint8_t ttl = 128);
9596
int ping(const String& hostname, uint8_t ttl = 128);
9697
int ping(IPAddress host, uint8_t ttl = 128);

libraries/GSM/src/GSMTrace.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)