Skip to content

Commit b1ab3e6

Browse files
author
TeemuKultala
authored
Merge pull request #45 from ARMmbed/dot_print_change
TCP socket, and status info
2 parents 98f1401 + 0240489 commit b1ab3e6

File tree

3 files changed

+64
-22
lines changed

3 files changed

+64
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ See the file `mbed_app.json` in the root directory of your application. This fil
4949

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

52-
You can choose which socket type the application should use. For example:
52+
You can choose which socket type the application should use, however please note that TCP is more reliable tranmission protocol. For example:
5353

5454
```json
5555

main.cpp

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
// Number of retries /
4141
#define RETRY_COUNT 3
4242

43+
44+
4345
// CellularInterface object
4446
OnboardCellularInterface iface;
4547

@@ -49,6 +51,34 @@ const char *host_name = "echo.u-blox.com";
4951
// Echo server port (same for TCP and UDP)
5052
const int port = 7;
5153

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+
5282
/**
5383
* Connects to the Cellular Network
5484
*/
@@ -61,21 +91,23 @@ nsapi_error_t do_connect()
6191

6292
retcode = iface.connect();
6393
if (retcode == NSAPI_ERROR_AUTH_FAILURE) {
64-
printf("\n\nAuthentication Failure. Exiting application\n");
94+
print_function("\n\nAuthentication Failure. Exiting application\n");
6595
return retcode;
6696
} else if (retcode != NSAPI_ERROR_OK) {
67-
printf("\n\nCouldn't connect: %d, will retry\n", retcode);
97+
snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nCouldn't connect: %d, will retry\n", retcode);
98+
print_function(print_text);
6899
retry_counter++;
69100
continue;
70101
} else if (retcode != NSAPI_ERROR_OK && retry_counter > RETRY_COUNT) {
71-
printf("\n\nFatal connection failure: %d\n", retcode);
102+
snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nFatal connection failure: %d\n", retcode);
103+
print_function(print_text);
72104
return retcode;
73105
}
74106

75107
break;
76108
}
77109

78-
printf("\n\nConnection Established.\n");
110+
print_function("\n\nConnection Established.\n");
79111

80112
return NSAPI_ERROR_OK;
81113
}
@@ -95,49 +127,57 @@ nsapi_error_t test_send_recv()
95127

96128
retcode = sock.open(&iface);
97129
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);
99132
return -1;
100133
}
101134

102135
SocketAddress sock_addr;
103136
retcode = iface.gethostbyname(host_name, &sock_addr);
104137
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,
106139
retcode);
140+
print_function(print_text);
107141
return -1;
108142
}
109143

110144
sock_addr.set_port(port);
111145

112146
sock.set_timeout(15000);
113147
int n = 0;
114-
char *echo_string = "TEST";
148+
const char *echo_string = "TEST";
115149
char recv_buf[4];
116150
#if MBED_CONF_APP_SOCK_TYPE == TCP
117151
retcode = sock.connect(sock_addr);
118152
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);
120155
return -1;
121156
} 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);
123159
}
124160
retcode = sock.send((void*) echo_string, sizeof(echo_string));
125161
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);
127164
return -1;
128165
} 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);
130168
}
131169

132170
n = sock.recv((void*) recv_buf, sizeof(recv_buf));
133171
#else
134172

135173
retcode = sock.sendto(sock_addr, (void*) echo_string, sizeof(echo_string));
136174
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);
138177
return -1;
139178
} 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);
141181
}
142182

143183
n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf));
@@ -146,36 +186,38 @@ nsapi_error_t test_send_recv()
146186
sock.close();
147187

148188
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);
150191
return 0;
151192
}
152193

153194
return -1;
154-
155-
return retcode;
156195
}
157196

158197
int main()
159198
{
199+
160200
iface.modem_debug_on(MBED_CONF_APP_MODEM_TRACE);
161201
/* Set Pin code for SIM card */
162202
iface.set_sim_pin(MBED_CONF_APP_SIM_PIN_CODE);
163203

164204
/* Set network credentials here, e.g., APN*/
165205
iface.set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD);
166206

167-
printf("\n\nmbed-os-example-cellular, Connecting...\n");
207+
print_function("\n\nmbed-os-example-cellular\n");
208+
print_function("Establishing connection ");
209+
dot_thread.start(dot_event);
168210

169211
/* Attempt to connect to a cellular network */
170212
if (do_connect() == NSAPI_ERROR_OK) {
171213
nsapi_error_t retcode = test_send_recv();
172214
if (retcode == NSAPI_ERROR_OK) {
173-
printf("\n\nSuccess. Exiting \n\n");
215+
print_function("\n\nSuccess. Exiting \n\n");
174216
return 0;
175217
}
176218
}
177219

178-
printf("\n\nFailure. Exiting \n\n");
220+
print_function("\n\nFailure. Exiting \n\n");
179221
return -1;
180222
}
181223
// EOF

mbed_app.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"config": {
3-
"sock-type": "UDP",
3+
"sock-type": "TCP",
44
"modem_trace": {
55
"help": "Turns AT command trace on/off from the cellular modem, defaults to off",
66
"value": false
@@ -34,4 +34,4 @@
3434
"platform.default-serial-baud-rate": 115200
3535
}
3636
}
37-
}
37+
}

0 commit comments

Comments
 (0)