Skip to content

Commit 53d9df9

Browse files
author
Teemu Kultala
committed
TCP socket, and status info
1 parent 98f1401 commit 53d9df9

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-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: 59 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,33 @@ 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+
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+
5281
/**
5382
* Connects to the Cellular Network
5483
*/
@@ -61,21 +90,23 @@ nsapi_error_t do_connect()
6190

6291
retcode = iface.connect();
6392
if (retcode == NSAPI_ERROR_AUTH_FAILURE) {
64-
printf("\n\nAuthentication Failure. Exiting application\n");
93+
print_function("\n\nAuthentication Failure. Exiting application\n");
6594
return retcode;
6695
} else if (retcode != NSAPI_ERROR_OK) {
67-
printf("\n\nCouldn't connect: %d, will retry\n", retcode);
96+
snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nCouldn't connect: %d, will retry\n", retcode);
97+
print_function(print_text);
6898
retry_counter++;
6999
continue;
70100
} else if (retcode != NSAPI_ERROR_OK && retry_counter > RETRY_COUNT) {
71-
printf("\n\nFatal connection failure: %d\n", retcode);
101+
snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nFatal connection failure: %d\n", retcode);
102+
print_function(print_text);
72103
return retcode;
73104
}
74105

75106
break;
76107
}
77108

78-
printf("\n\nConnection Established.\n");
109+
print_function("\n\nConnection Established.\n");
79110

80111
return NSAPI_ERROR_OK;
81112
}
@@ -95,49 +126,57 @@ nsapi_error_t test_send_recv()
95126

96127
retcode = sock.open(&iface);
97128
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);
99131
return -1;
100132
}
101133

102134
SocketAddress sock_addr;
103135
retcode = iface.gethostbyname(host_name, &sock_addr);
104136
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,
106138
retcode);
139+
print_function(print_text);
107140
return -1;
108141
}
109142

110143
sock_addr.set_port(port);
111144

112145
sock.set_timeout(15000);
113146
int n = 0;
114-
char *echo_string = "TEST";
147+
const char *echo_string = "TEST";
115148
char recv_buf[4];
116149
#if MBED_CONF_APP_SOCK_TYPE == TCP
117150
retcode = sock.connect(sock_addr);
118151
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);
120154
return -1;
121155
} 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);
123158
}
124159
retcode = sock.send((void*) echo_string, sizeof(echo_string));
125160
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);
127163
return -1;
128164
} 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);
130167
}
131168

132169
n = sock.recv((void*) recv_buf, sizeof(recv_buf));
133170
#else
134171

135172
retcode = sock.sendto(sock_addr, (void*) echo_string, sizeof(echo_string));
136173
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);
138176
return -1;
139177
} 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);
141180
}
142181

143182
n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf));
@@ -146,36 +185,37 @@ nsapi_error_t test_send_recv()
146185
sock.close();
147186

148187
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);
150190
return 0;
151191
}
152192

153193
return -1;
154-
155-
return retcode;
156194
}
157195

158196
int main()
159197
{
198+
160199
iface.modem_debug_on(MBED_CONF_APP_MODEM_TRACE);
161200
/* Set Pin code for SIM card */
162201
iface.set_sim_pin(MBED_CONF_APP_SIM_PIN_CODE);
163202

164203
/* Set network credentials here, e.g., APN*/
165204
iface.set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD);
166205

167-
printf("\n\nmbed-os-example-cellular, Connecting...\n");
206+
print_function("\n\nmbed-os-example-cellular, Connecting...\n");
207+
dot_thread.start(dot_event);
168208

169209
/* Attempt to connect to a cellular network */
170210
if (do_connect() == NSAPI_ERROR_OK) {
171211
nsapi_error_t retcode = test_send_recv();
172212
if (retcode == NSAPI_ERROR_OK) {
173-
printf("\n\nSuccess. Exiting \n\n");
213+
print_function("\n\nSuccess. Exiting \n\n");
174214
return 0;
175215
}
176216
}
177217

178-
printf("\n\nFailure. Exiting \n\n");
218+
print_function("\n\nFailure. Exiting \n\n");
179219
return -1;
180220
}
181221
// 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)