@@ -65,13 +65,11 @@ static void trace_release()
65
65
trace_mutex.unlock ();
66
66
}
67
67
68
-
69
- static uint32_t cellular_starttime = us_ticker_read() / 1000L ;
70
68
static char time_st[50 ];
71
69
72
70
static char * trace_time (size_t ss)
73
71
{
74
- snprintf (time_st, 49 , " [%08lums ]" , (( us_ticker_read ()-cellular_starttime) / 1000L ));
72
+ snprintf (time_st, 49 , " [%08llums ]" , Kernel::get_ms_count ( ));
75
73
return time_st;
76
74
}
77
75
@@ -92,22 +90,25 @@ static void trace_close()
92
90
93
91
Thread dot_thread (osPriorityNormal, 512 );
94
92
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, ...)
98
94
{
99
95
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 );
102
100
trace_mutex.unlock ();
103
101
}
104
102
105
103
void dot_event ()
106
104
{
107
105
while (true ) {
108
- wait (4 );
106
+ Thread:: wait (4000 );
109
107
if (!iface.is_connected ()) {
110
- print_function (" ." );
108
+ trace_mutex.lock ();
109
+ printf (" ." );
110
+ fflush (stdout);
111
+ trace_mutex.unlock ();
111
112
} else {
112
113
break ;
113
114
}
@@ -129,18 +130,14 @@ nsapi_error_t do_connect()
129
130
} else if (retcode == NSAPI_ERROR_OK) {
130
131
print_function (" \n\n Connection Established.\n " );
131
132
} else if (retry_counter > RETRY_COUNT) {
132
- snprintf (print_text, PRINT_TEXT_LENGTH, " \n\n Fatal connection failure: %d\n " , retcode);
133
- print_function (print_text);
133
+ print_function (" \n\n Fatal connection failure: %d\n " , retcode);
134
134
} else {
135
- snprintf (print_text, PRINT_TEXT_LENGTH, " \n\n Couldn't connect: %d, will retry\n " , retcode);
136
- print_function (print_text);
135
+ print_function (" \n\n Couldn't connect: %d, will retry\n " , retcode);
137
136
retry_counter++;
138
137
continue ;
139
138
}
140
139
break ;
141
140
}
142
-
143
- dot_thread.terminate ();
144
141
return retcode;
145
142
}
146
143
@@ -159,17 +156,14 @@ nsapi_error_t test_send_recv()
159
156
160
157
retcode = sock.open (&iface);
161
158
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);
164
160
return -1 ;
165
161
}
166
162
167
163
SocketAddress sock_addr;
168
164
retcode = iface.gethostbyname (host_name, &sock_addr);
169
165
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);
173
167
return -1 ;
174
168
}
175
169
@@ -182,34 +176,28 @@ nsapi_error_t test_send_recv()
182
176
#if MBED_CONF_APP_SOCK_TYPE == TCP
183
177
retcode = sock.connect (sock_addr);
184
178
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);
187
180
return -1 ;
188
181
} 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);
191
183
}
192
184
retcode = sock.send ((void *) echo_string, sizeof (echo_string));
193
185
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);
196
187
return -1 ;
197
188
} 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);
200
190
}
201
191
202
192
n = sock.recv ((void *) recv_buf, sizeof (recv_buf));
203
193
#else
204
194
205
195
retcode = sock.sendto (sock_addr, (void *) echo_string, sizeof (echo_string));
206
196
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);
209
198
return -1 ;
210
199
} 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);
213
201
}
214
202
215
203
n = sock.recvfrom (&sock_addr, (void *) recv_buf, sizeof (recv_buf));
@@ -218,8 +206,7 @@ nsapi_error_t test_send_recv()
218
206
sock.close ();
219
207
220
208
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);
223
210
return 0 ;
224
211
}
225
212
@@ -229,9 +216,11 @@ nsapi_error_t test_send_recv()
229
216
int main ()
230
217
{
231
218
print_function (" \n\n mbed-os-example-cellular\n " );
232
-
219
+ print_function ( " Establishing connection \n " );
233
220
#if MBED_CONF_MBED_TRACE_ENABLE
234
221
trace_open ();
222
+ #else
223
+ dot_thread.start (dot_event);
235
224
#endif // #if MBED_CONF_MBED_TRACE_ENABLE
236
225
/* Set Pin code for SIM card */
237
226
iface.set_sim_pin (MBED_CONF_APP_SIM_PIN_CODE);
@@ -242,9 +231,6 @@ int main()
242
231
/* Set the modem debug on/off */
243
232
iface.modem_debug_on (MBED_CONF_APP_MODEM_TRACE);
244
233
245
- print_function (" Establishing connection " );
246
- dot_thread.start (dot_event);
247
-
248
234
nsapi_error_t retcode = NSAPI_ERROR_NO_CONNECTION;
249
235
250
236
/* Attempt to connect to a cellular network */
0 commit comments