Skip to content

Commit 42c7214

Browse files
authored
Merge pull request #63 from ARMmbed/adding_trace_support_and_bug_fixes
Added trace support.
2 parents 9fb7117 + b0a53e1 commit 42c7214

File tree

3 files changed

+120
-65
lines changed

3 files changed

+120
-65
lines changed

README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ See the file `mbed_app.json` in the root directory of your application. This fil
4545
"help": "The password string to use for this APN, set to 0 if none",
4646
"value": 0
4747
}
48-
```
48+
```
4949

5050
### Selecting socket type (TCP or UDP)
5151

5252

53-
You can choose which socket type the application should use; however, please note that TCP is a more reliable tranmission protocol. For example:
53+
You can choose which socket type the application should use; however, please note that TCP is a more reliable transmission protocol. For example:
5454

5555

5656
```json
@@ -70,6 +70,25 @@ If you like details and wish to know about all the AT interactions between the m
7070
},
7171
```
7272

73+
### Turning on the tracing and trace level
74+
75+
If you like to add more traces or follow the current ones you can turn traces on by changing `mbed-trace.enable` in mbed_app.json
76+
77+
```"target_overrides": {
78+
"*": {
79+
"target.features_add": ["LWIP", "COMMON_PAL"],
80+
"mbed-trace.enable": false,
81+
```
82+
83+
After you have defined `mbed-trace.enable: true`, you can set trace levels by changing value in `trace-level`
84+
85+
```"trace-level": {
86+
"help": "Options are TRACE_LEVEL_ERROR,TRACE_LEVEL_WARN,TRACE_LEVEL_INFO,TRACE_LEVEL_DEBUG",
87+
"macro_name": "MBED_TRACE_MAX_LEVEL",
88+
"value": "TRACE_LEVEL_INFO"
89+
}
90+
```
91+
7392
### Board support
7493

7594
The [generic cellular modem driver](https://github.com/ARMmbed/mbed-os/tree/master/features/netsocket/cellular/generic_modem_driver) this application uses was written using only a standard AT command set. It uses PPP with an Mbed-supported external IP stack. These abilities make the driver essentially generic, or nonvendor specific. However, this particular driver is for onboard-modem types. In other words, the modem exists on the Mbed Enabled target as opposed to plug-in modules (shields). For more details, please see our [Mbed OS cellular documentation](https://os.mbed.com/docs/latest/reference/cellular-api.html).
@@ -94,7 +113,7 @@ You should see an output similar to this:
94113

95114
```
96115
mbed-os-example-cellular
97-
Establishing connection ......
116+
Establishing connection ......
98117
99118
Connection Established.
100119
TCP: connected with echo.mbedcloudtesting.com server

main.cpp

Lines changed: 89 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "common_functions.h"
1919
#include "UDPSocket.h"
2020
#include "OnboardCellularInterface.h"
21+
#include "CellularLog.h"
2122

2223
#define UDP 0
2324
#define TCP 1
@@ -51,65 +52,93 @@ const char *host_name = "echo.mbedcloudtesting.com";
5152
// Echo server port (same for TCP and UDP)
5253
const int port = 7;
5354

54-
Mutex PrintMutex;
55+
static rtos::Mutex trace_mutex;
56+
57+
#if MBED_CONF_MBED_TRACE_ENABLE
58+
static void trace_wait()
59+
{
60+
trace_mutex.lock();
61+
}
62+
63+
static void trace_release()
64+
{
65+
trace_mutex.unlock();
66+
}
67+
68+
static char time_st[50];
69+
70+
static char* trace_time(size_t ss)
71+
{
72+
snprintf(time_st, 49, "[%08llums]", Kernel::get_ms_count());
73+
return time_st;
74+
}
75+
76+
static void trace_open()
77+
{
78+
mbed_trace_init();
79+
mbed_trace_prefix_function_set( &trace_time );
80+
81+
mbed_trace_mutex_wait_function_set(trace_wait);
82+
mbed_trace_mutex_release_function_set(trace_release);
83+
}
84+
85+
static void trace_close()
86+
{
87+
mbed_trace_free();
88+
}
89+
#endif // #if MBED_CONF_MBED_TRACE_ENABLE
90+
5591
Thread dot_thread(osPriorityNormal, 512);
5692

57-
#define PRINT_TEXT_LENGTH 128
58-
char print_text[PRINT_TEXT_LENGTH];
59-
void print_function(const char *input_string)
93+
void print_function(const char *format, ...)
6094
{
61-
PrintMutex.lock();
62-
printf("%s", input_string);
63-
fflush(NULL);
64-
PrintMutex.unlock();
95+
trace_mutex.lock();
96+
va_list arglist;
97+
va_start( arglist, format );
98+
vprintf(format, arglist);
99+
va_end( arglist );
100+
trace_mutex.unlock();
65101
}
66102

67103
void dot_event()
68104
{
69-
70105
while (true) {
71-
wait(4);
106+
Thread::wait(4000);
72107
if (!iface.is_connected()) {
73-
print_function(".");
108+
trace_mutex.lock();
109+
printf(".");
110+
fflush(stdout);
111+
trace_mutex.unlock();
74112
} else {
75113
break;
76114
}
77115
}
78-
79116
}
80117

81-
82118
/**
83119
* Connects to the Cellular Network
84120
*/
85121
nsapi_error_t do_connect()
86122
{
87-
nsapi_error_t retcode;
123+
nsapi_error_t retcode = NSAPI_ERROR_OK;
88124
uint8_t retry_counter = 0;
89125

90126
while (!iface.is_connected()) {
91-
92127
retcode = iface.connect();
93128
if (retcode == NSAPI_ERROR_AUTH_FAILURE) {
94129
print_function("\n\nAuthentication Failure. Exiting application\n");
95-
return retcode;
96-
} else if (retcode != NSAPI_ERROR_OK) {
97-
snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nCouldn't connect: %d, will retry\n", retcode);
98-
print_function(print_text);
130+
} else if (retcode == NSAPI_ERROR_OK) {
131+
print_function("\n\nConnection Established.\n");
132+
} else if (retry_counter > RETRY_COUNT) {
133+
print_function("\n\nFatal connection failure: %d\n", retcode);
134+
} else {
135+
print_function("\n\nCouldn't connect: %d, will retry\n", retcode);
99136
retry_counter++;
100137
continue;
101-
} else if (retcode != NSAPI_ERROR_OK && retry_counter > RETRY_COUNT) {
102-
snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nFatal connection failure: %d\n", retcode);
103-
print_function(print_text);
104-
return retcode;
105138
}
106-
107139
break;
108140
}
109-
110-
print_function("\n\nConnection Established.\n");
111-
112-
return NSAPI_ERROR_OK;
141+
return retcode;
113142
}
114143

115144
/**
@@ -127,17 +156,14 @@ nsapi_error_t test_send_recv()
127156

128157
retcode = sock.open(&iface);
129158
if (retcode != NSAPI_ERROR_OK) {
130-
snprintf(print_text, PRINT_TEXT_LENGTH, "UDPSocket.open() fails, code: %d\n", retcode);
131-
print_function(print_text);
159+
print_function("UDPSocket.open() fails, code: %d\n", retcode);
132160
return -1;
133161
}
134162

135163
SocketAddress sock_addr;
136164
retcode = iface.gethostbyname(host_name, &sock_addr);
137165
if (retcode != NSAPI_ERROR_OK) {
138-
snprintf(print_text, PRINT_TEXT_LENGTH, "Couldn't resolve remote host: %s, code: %d\n", host_name,
139-
retcode);
140-
print_function(print_text);
166+
print_function("Couldn't resolve remote host: %s, code: %d\n", host_name, retcode);
141167
return -1;
142168
}
143169

@@ -150,34 +176,28 @@ nsapi_error_t test_send_recv()
150176
#if MBED_CONF_APP_SOCK_TYPE == TCP
151177
retcode = sock.connect(sock_addr);
152178
if (retcode < 0) {
153-
snprintf(print_text, PRINT_TEXT_LENGTH, "TCPSocket.connect() fails, code: %d\n", retcode);
154-
print_function(print_text);
179+
print_function("TCPSocket.connect() fails, code: %d\n", retcode);
155180
return -1;
156181
} else {
157-
snprintf(print_text, PRINT_TEXT_LENGTH, "TCP: connected with %s server\n", host_name);
158-
print_function(print_text);
182+
print_function("TCP: connected with %s server\n", host_name);
159183
}
160184
retcode = sock.send((void*) echo_string, sizeof(echo_string));
161185
if (retcode < 0) {
162-
snprintf(print_text, PRINT_TEXT_LENGTH, "TCPSocket.send() fails, code: %d\n", retcode);
163-
print_function(print_text);
186+
print_function("TCPSocket.send() fails, code: %d\n", retcode);
164187
return -1;
165188
} else {
166-
snprintf(print_text, PRINT_TEXT_LENGTH, "TCP: Sent %d Bytes to %s\n", retcode, host_name);
167-
print_function(print_text);
189+
print_function("TCP: Sent %d Bytes to %s\n", retcode, host_name);
168190
}
169191

170192
n = sock.recv((void*) recv_buf, sizeof(recv_buf));
171193
#else
172194

173195
retcode = sock.sendto(sock_addr, (void*) echo_string, sizeof(echo_string));
174196
if (retcode < 0) {
175-
snprintf(print_text, PRINT_TEXT_LENGTH, "UDPSocket.sendto() fails, code: %d\n", retcode);
176-
print_function(print_text);
197+
print_function("UDPSocket.sendto() fails, code: %d\n", retcode);
177198
return -1;
178199
} else {
179-
snprintf(print_text, PRINT_TEXT_LENGTH, "UDP: Sent %d Bytes to %s\n", retcode, host_name);
180-
print_function(print_text);
200+
print_function("UDP: Sent %d Bytes to %s\n", retcode, host_name);
181201
}
182202

183203
n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf));
@@ -186,8 +206,7 @@ nsapi_error_t test_send_recv()
186206
sock.close();
187207

188208
if (n > 0) {
189-
snprintf(print_text, PRINT_TEXT_LENGTH, "Received from echo server %d Bytes\n", n);
190-
print_function(print_text);
209+
print_function("Received from echo server %d Bytes\n", n);
191210
return 0;
192211
}
193212

@@ -196,28 +215,38 @@ nsapi_error_t test_send_recv()
196215

197216
int main()
198217
{
199-
200-
iface.modem_debug_on(MBED_CONF_APP_MODEM_TRACE);
218+
print_function("\n\nmbed-os-example-cellular\n");
219+
print_function("Establishing connection\n");
220+
#if MBED_CONF_MBED_TRACE_ENABLE
221+
trace_open();
222+
#else
223+
dot_thread.start(dot_event);
224+
#endif // #if MBED_CONF_MBED_TRACE_ENABLE
201225
/* Set Pin code for SIM card */
202226
iface.set_sim_pin(MBED_CONF_APP_SIM_PIN_CODE);
203227

204-
/* Set network credentials here, e.g., APN*/
228+
/* Set network credentials here, e.g., APN */
205229
iface.set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD);
206230

207-
print_function("\n\nmbed-os-example-cellular\n");
208-
print_function("Establishing connection ");
209-
dot_thread.start(dot_event);
231+
/* Set the modem debug on/off */
232+
iface.modem_debug_on(MBED_CONF_APP_MODEM_TRACE);
233+
234+
nsapi_error_t retcode = NSAPI_ERROR_NO_CONNECTION;
210235

211236
/* Attempt to connect to a cellular network */
212237
if (do_connect() == NSAPI_ERROR_OK) {
213-
nsapi_error_t retcode = test_send_recv();
214-
if (retcode == NSAPI_ERROR_OK) {
215-
print_function("\n\nSuccess. Exiting \n\n");
216-
return 0;
217-
}
238+
retcode = test_send_recv();
218239
}
219240

220-
print_function("\n\nFailure. Exiting \n\n");
221-
return -1;
241+
if (retcode == NSAPI_ERROR_OK) {
242+
print_function("\n\nSuccess. Exiting \n\n");
243+
} else {
244+
print_function("\n\nFailure. Exiting \n\n");
245+
}
246+
#if MBED_CONF_MBED_TRACE_ENABLE
247+
trace_close();
248+
#endif // #if MBED_CONF_MBED_TRACE_ENABLE
249+
250+
return 0;
222251
}
223252
// EOF

mbed_app.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,25 @@
2020
"password": {
2121
"help": "The password string to use for this APN, set to 0 if none",
2222
"value": 0
23+
},
24+
"trace-level": {
25+
"help": "Options are TRACE_LEVEL_ERROR,TRACE_LEVEL_WARN,TRACE_LEVEL_INFO,TRACE_LEVEL_DEBUG",
26+
"macro_name": "MBED_TRACE_MAX_LEVEL",
27+
"value": "TRACE_LEVEL_INFO"
2328
}
24-
},
29+
},
2530
"target_overrides": {
2631
"*": {
2732
"target.features_add": ["LWIP", "COMMON_PAL"],
33+
"mbed-trace.enable": false,
2834
"lwip.ipv4-enabled": true,
2935
"lwip.ethernet-enabled": false,
3036
"lwip.ppp-enabled": true,
3137
"lwip.tcp-enabled": true,
3238
"platform.stdio-convert-newlines": true,
3339
"platform.stdio-baud-rate": 115200,
34-
"platform.default-serial-baud-rate": 115200
40+
"platform.default-serial-baud-rate": 115200,
41+
"platform.stdio-buffered-serial": true
3542
}
3643
}
3744
}

0 commit comments

Comments
 (0)