40
40
// Number of retries /
41
41
#define RETRY_COUNT 3
42
42
43
+
44
+
43
45
// CellularInterface object
44
46
OnboardCellularInterface iface;
45
47
@@ -49,6 +51,34 @@ const char *host_name = "echo.u-blox.com";
49
51
// Echo server port (same for TCP and UDP)
50
52
const int port = 7 ;
51
53
54
+ Mutex PrintMutex;
55
+ Thread dot_thread;
56
+
57
+ #define PRINT_TEXT_LENGTH 128
58
+ char print_text[PRINT_TEXT_LENGTH];
59
+ void print_function (const char *input_string)
60
+ {
61
+ PrintMutex.lock ();
62
+ printf (" %s" , input_string);
63
+ fflush (NULL );
64
+ PrintMutex.unlock ();
65
+ }
66
+
67
+ void dot_event ()
68
+ {
69
+
70
+ while (true ) {
71
+ wait (4 );
72
+ if (!iface.is_connected ()) {
73
+ print_function (" ." );
74
+ } else {
75
+ break ;
76
+ }
77
+ }
78
+
79
+ }
80
+
81
+
52
82
/* *
53
83
* Connects to the Cellular Network
54
84
*/
@@ -61,21 +91,23 @@ nsapi_error_t do_connect()
61
91
62
92
retcode = iface.connect ();
63
93
if (retcode == NSAPI_ERROR_AUTH_FAILURE) {
64
- printf (" \n\n Authentication Failure. Exiting application\n " );
94
+ print_function (" \n\n Authentication Failure. Exiting application\n " );
65
95
return retcode;
66
96
} else if (retcode != NSAPI_ERROR_OK) {
67
- printf (" \n\n Couldn't connect: %d, will retry\n " , retcode);
97
+ snprintf (print_text, PRINT_TEXT_LENGTH, " \n\n Couldn't connect: %d, will retry\n " , retcode);
98
+ print_function (print_text);
68
99
retry_counter++;
69
100
continue ;
70
101
} else if (retcode != NSAPI_ERROR_OK && retry_counter > RETRY_COUNT) {
71
- printf (" \n\n Fatal connection failure: %d\n " , retcode);
102
+ snprintf (print_text, PRINT_TEXT_LENGTH, " \n\n Fatal connection failure: %d\n " , retcode);
103
+ print_function (print_text);
72
104
return retcode;
73
105
}
74
106
75
107
break ;
76
108
}
77
109
78
- printf (" \n\n Connection Established.\n " );
110
+ print_function (" \n\n Connection Established.\n " );
79
111
80
112
return NSAPI_ERROR_OK;
81
113
}
@@ -95,49 +127,57 @@ nsapi_error_t test_send_recv()
95
127
96
128
retcode = sock.open (&iface);
97
129
if (retcode != NSAPI_ERROR_OK) {
98
- printf (" UDPSocket.open() fails, code: %d\n " , retcode);
130
+ snprintf (print_text, PRINT_TEXT_LENGTH, " UDPSocket.open() fails, code: %d\n " , retcode);
131
+ print_function (print_text);
99
132
return -1 ;
100
133
}
101
134
102
135
SocketAddress sock_addr;
103
136
retcode = iface.gethostbyname (host_name, &sock_addr);
104
137
if (retcode != NSAPI_ERROR_OK) {
105
- printf ( " Couldn't resolve remote host: %s, code: %d\n " , host_name,
138
+ snprintf (print_text, PRINT_TEXT_LENGTH, " Couldn't resolve remote host: %s, code: %d\n " , host_name,
106
139
retcode);
140
+ print_function (print_text);
107
141
return -1 ;
108
142
}
109
143
110
144
sock_addr.set_port (port);
111
145
112
146
sock.set_timeout (15000 );
113
147
int n = 0 ;
114
- char *echo_string = " TEST" ;
148
+ const char *echo_string = " TEST" ;
115
149
char recv_buf[4 ];
116
150
#if MBED_CONF_APP_SOCK_TYPE == TCP
117
151
retcode = sock.connect (sock_addr);
118
152
if (retcode < 0 ) {
119
- printf (" TCPSocket.connect() fails, code: %d\n " , retcode);
153
+ snprintf (print_text, PRINT_TEXT_LENGTH, " TCPSocket.connect() fails, code: %d\n " , retcode);
154
+ print_function (print_text);
120
155
return -1 ;
121
156
} else {
122
- printf (" TCP: connected with %s server\n " , host_name);
157
+ snprintf (print_text, PRINT_TEXT_LENGTH, " TCP: connected with %s server\n " , host_name);
158
+ print_function (print_text);
123
159
}
124
160
retcode = sock.send ((void *) echo_string, sizeof (echo_string));
125
161
if (retcode < 0 ) {
126
- printf (" TCPSocket.send() fails, code: %d\n " , retcode);
162
+ snprintf (print_text, PRINT_TEXT_LENGTH, " TCPSocket.send() fails, code: %d\n " , retcode);
163
+ print_function (print_text);
127
164
return -1 ;
128
165
} else {
129
- printf (" TCP: Sent %d Bytes to %s\n " , retcode, host_name);
166
+ snprintf (print_text, PRINT_TEXT_LENGTH, " TCP: Sent %d Bytes to %s\n " , retcode, host_name);
167
+ print_function (print_text);
130
168
}
131
169
132
170
n = sock.recv ((void *) recv_buf, sizeof (recv_buf));
133
171
#else
134
172
135
173
retcode = sock.sendto (sock_addr, (void *) echo_string, sizeof (echo_string));
136
174
if (retcode < 0 ) {
137
- printf (" UDPSocket.sendto() fails, code: %d\n " , retcode);
175
+ snprintf (print_text, PRINT_TEXT_LENGTH, " UDPSocket.sendto() fails, code: %d\n " , retcode);
176
+ print_function (print_text);
138
177
return -1 ;
139
178
} else {
140
- printf (" UDP: Sent %d Bytes to %s\n " , retcode, host_name);
179
+ snprintf (print_text, PRINT_TEXT_LENGTH, " UDP: Sent %d Bytes to %s\n " , retcode, host_name);
180
+ print_function (print_text);
141
181
}
142
182
143
183
n = sock.recvfrom (&sock_addr, (void *) recv_buf, sizeof (recv_buf));
@@ -146,36 +186,38 @@ nsapi_error_t test_send_recv()
146
186
sock.close ();
147
187
148
188
if (n > 0 ) {
149
- printf (" Received from echo server %d Bytes\n " , n);
189
+ snprintf (print_text, PRINT_TEXT_LENGTH, " Received from echo server %d Bytes\n " , n);
190
+ print_function (print_text);
150
191
return 0 ;
151
192
}
152
193
153
194
return -1 ;
154
-
155
- return retcode;
156
195
}
157
196
158
197
int main ()
159
198
{
199
+
160
200
iface.modem_debug_on (MBED_CONF_APP_MODEM_TRACE);
161
201
/* Set Pin code for SIM card */
162
202
iface.set_sim_pin (MBED_CONF_APP_SIM_PIN_CODE);
163
203
164
204
/* Set network credentials here, e.g., APN*/
165
205
iface.set_credentials (MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD);
166
206
167
- printf (" \n\n mbed-os-example-cellular, Connecting...\n " );
207
+ print_function (" \n\n mbed-os-example-cellular\n " );
208
+ print_function (" Establishing connection " );
209
+ dot_thread.start (dot_event);
168
210
169
211
/* Attempt to connect to a cellular network */
170
212
if (do_connect () == NSAPI_ERROR_OK) {
171
213
nsapi_error_t retcode = test_send_recv ();
172
214
if (retcode == NSAPI_ERROR_OK) {
173
- printf (" \n\n Success. Exiting \n\n " );
215
+ print_function (" \n\n Success. Exiting \n\n " );
174
216
return 0 ;
175
217
}
176
218
}
177
219
178
- printf (" \n\n Failure. Exiting \n\n " );
220
+ print_function (" \n\n Failure. Exiting \n\n " );
179
221
return -1 ;
180
222
}
181
223
// EOF
0 commit comments