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