Skip to content

Commit b0a53e1

Browse files
author
Teppo Järvelin
committed
Review fixes.
1 parent 8ea7a42 commit b0a53e1

File tree

3 files changed

+28
-41
lines changed

3 files changed

+28
-41
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ If you like to add more traces or follow the current ones you can turn traces on
8080
"mbed-trace.enable": false,
8181
```
8282

83-
Changing the trace level is done also in mbed_add.json by changing value in `trace-level`
83+
After you have defined `mbed-trace.enable: true`, you can set trace levels by changing value in `trace-level`
8484

8585
```"trace-level": {
8686
"help": "Options are TRACE_LEVEL_ERROR,TRACE_LEVEL_WARN,TRACE_LEVEL_INFO,TRACE_LEVEL_DEBUG",

main.cpp

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,11 @@ static void trace_release()
6565
trace_mutex.unlock();
6666
}
6767

68-
69-
static uint32_t cellular_starttime = us_ticker_read() / 1000L;
7068
static char time_st[50];
7169

7270
static char* trace_time(size_t ss)
7371
{
74-
snprintf(time_st, 49, "[%08lums]", ((us_ticker_read()-cellular_starttime) / 1000L));
72+
snprintf(time_st, 49, "[%08llums]", Kernel::get_ms_count());
7573
return time_st;
7674
}
7775

@@ -92,22 +90,25 @@ static void trace_close()
9290

9391
Thread dot_thread(osPriorityNormal, 512);
9492

95-
#define PRINT_TEXT_LENGTH 128
96-
char print_text[PRINT_TEXT_LENGTH];
97-
void print_function(const char *input_string)
93+
void print_function(const char *format, ...)
9894
{
9995
trace_mutex.lock();
100-
printf("%s", input_string);
101-
fflush(NULL);
96+
va_list arglist;
97+
va_start( arglist, format );
98+
vprintf(format, arglist);
99+
va_end( arglist );
102100
trace_mutex.unlock();
103101
}
104102

105103
void dot_event()
106104
{
107105
while (true) {
108-
wait(4);
106+
Thread::wait(4000);
109107
if (!iface.is_connected()) {
110-
print_function(".");
108+
trace_mutex.lock();
109+
printf(".");
110+
fflush(stdout);
111+
trace_mutex.unlock();
111112
} else {
112113
break;
113114
}
@@ -129,18 +130,14 @@ nsapi_error_t do_connect()
129130
} else if (retcode == NSAPI_ERROR_OK) {
130131
print_function("\n\nConnection Established.\n");
131132
} else if (retry_counter > RETRY_COUNT) {
132-
snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nFatal connection failure: %d\n", retcode);
133-
print_function(print_text);
133+
print_function("\n\nFatal connection failure: %d\n", retcode);
134134
} else {
135-
snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nCouldn't connect: %d, will retry\n", retcode);
136-
print_function(print_text);
135+
print_function("\n\nCouldn't connect: %d, will retry\n", retcode);
137136
retry_counter++;
138137
continue;
139138
}
140139
break;
141140
}
142-
143-
dot_thread.terminate();
144141
return retcode;
145142
}
146143

@@ -159,17 +156,14 @@ nsapi_error_t test_send_recv()
159156

160157
retcode = sock.open(&iface);
161158
if (retcode != NSAPI_ERROR_OK) {
162-
snprintf(print_text, PRINT_TEXT_LENGTH, "UDPSocket.open() fails, code: %d\n", retcode);
163-
print_function(print_text);
159+
print_function("UDPSocket.open() fails, code: %d\n", retcode);
164160
return -1;
165161
}
166162

167163
SocketAddress sock_addr;
168164
retcode = iface.gethostbyname(host_name, &sock_addr);
169165
if (retcode != NSAPI_ERROR_OK) {
170-
snprintf(print_text, PRINT_TEXT_LENGTH, "Couldn't resolve remote host: %s, code: %d\n", host_name,
171-
retcode);
172-
print_function(print_text);
166+
print_function("Couldn't resolve remote host: %s, code: %d\n", host_name, retcode);
173167
return -1;
174168
}
175169

@@ -182,34 +176,28 @@ nsapi_error_t test_send_recv()
182176
#if MBED_CONF_APP_SOCK_TYPE == TCP
183177
retcode = sock.connect(sock_addr);
184178
if (retcode < 0) {
185-
snprintf(print_text, PRINT_TEXT_LENGTH, "TCPSocket.connect() fails, code: %d\n", retcode);
186-
print_function(print_text);
179+
print_function("TCPSocket.connect() fails, code: %d\n", retcode);
187180
return -1;
188181
} else {
189-
snprintf(print_text, PRINT_TEXT_LENGTH, "TCP: connected with %s server\n", host_name);
190-
print_function(print_text);
182+
print_function("TCP: connected with %s server\n", host_name);
191183
}
192184
retcode = sock.send((void*) echo_string, sizeof(echo_string));
193185
if (retcode < 0) {
194-
snprintf(print_text, PRINT_TEXT_LENGTH, "TCPSocket.send() fails, code: %d\n", retcode);
195-
print_function(print_text);
186+
print_function("TCPSocket.send() fails, code: %d\n", retcode);
196187
return -1;
197188
} else {
198-
snprintf(print_text, PRINT_TEXT_LENGTH, "TCP: Sent %d Bytes to %s\n", retcode, host_name);
199-
print_function(print_text);
189+
print_function("TCP: Sent %d Bytes to %s\n", retcode, host_name);
200190
}
201191

202192
n = sock.recv((void*) recv_buf, sizeof(recv_buf));
203193
#else
204194

205195
retcode = sock.sendto(sock_addr, (void*) echo_string, sizeof(echo_string));
206196
if (retcode < 0) {
207-
snprintf(print_text, PRINT_TEXT_LENGTH, "UDPSocket.sendto() fails, code: %d\n", retcode);
208-
print_function(print_text);
197+
print_function("UDPSocket.sendto() fails, code: %d\n", retcode);
209198
return -1;
210199
} else {
211-
snprintf(print_text, PRINT_TEXT_LENGTH, "UDP: Sent %d Bytes to %s\n", retcode, host_name);
212-
print_function(print_text);
200+
print_function("UDP: Sent %d Bytes to %s\n", retcode, host_name);
213201
}
214202

215203
n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf));
@@ -218,8 +206,7 @@ nsapi_error_t test_send_recv()
218206
sock.close();
219207

220208
if (n > 0) {
221-
snprintf(print_text, PRINT_TEXT_LENGTH, "Received from echo server %d Bytes\n", n);
222-
print_function(print_text);
209+
print_function("Received from echo server %d Bytes\n", n);
223210
return 0;
224211
}
225212

@@ -229,9 +216,11 @@ nsapi_error_t test_send_recv()
229216
int main()
230217
{
231218
print_function("\n\nmbed-os-example-cellular\n");
232-
219+
print_function("Establishing connection\n");
233220
#if MBED_CONF_MBED_TRACE_ENABLE
234221
trace_open();
222+
#else
223+
dot_thread.start(dot_event);
235224
#endif // #if MBED_CONF_MBED_TRACE_ENABLE
236225
/* Set Pin code for SIM card */
237226
iface.set_sim_pin(MBED_CONF_APP_SIM_PIN_CODE);
@@ -242,9 +231,6 @@ int main()
242231
/* Set the modem debug on/off */
243232
iface.modem_debug_on(MBED_CONF_APP_MODEM_TRACE);
244233

245-
print_function("Establishing connection ");
246-
dot_thread.start(dot_event);
247-
248234
nsapi_error_t retcode = NSAPI_ERROR_NO_CONNECTION;
249235

250236
/* Attempt to connect to a cellular network */

mbed_app.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"lwip.tcp-enabled": true,
3838
"platform.stdio-convert-newlines": true,
3939
"platform.stdio-baud-rate": 115200,
40-
"platform.default-serial-baud-rate": 115200
40+
"platform.default-serial-baud-rate": 115200,
41+
"platform.stdio-buffered-serial": true
4142
}
4243
}
4344
}

0 commit comments

Comments
 (0)