|
1 | 1 | ## Cellular TCP sockets
|
2 | 2 |
|
3 |
| -This example opens a TCP socket with an echo server and performs a TCP transaction. |
| 3 | +Establishing a cellular connection to the network with Mbed OS only requires the following operations: |
4 | 4 |
|
5 |
| -```cpp TODO |
6 |
| -#include "mbed.h" |
7 |
| -#include "UDPSocket.h" |
8 |
| -#include "OnboardCellularInterface.h" |
9 |
| - |
10 |
| -// SIM pin code goes here |
11 |
| -#define PIN_CODE "1234" |
12 |
| - |
13 |
| -// Network credentials like APN go here, e.g., |
14 |
| -// "apn, username, password" |
15 |
| -#define CREDENTIALS "internet" |
16 |
| - |
17 |
| -// Number of retries / |
18 |
| -#define RETRY_COUNT 3 |
19 |
| - |
20 |
| -// Cellular network interface object |
| 5 | +``` |
21 | 6 | OnboardCellularInterface iface;
|
22 | 7 |
|
23 |
| -// Echo server hostname |
24 |
| -const char *host_name = "echo.u-blox.com"; |
25 |
| - |
26 |
| -// Echo server TCP port |
27 |
| -const int port = 7; |
28 |
| - |
29 |
| -/** |
30 |
| - * Connects to the Cellular Network |
31 |
| - */ |
32 |
| -nsapi_error_t do_connect() |
33 |
| -{ |
34 |
| - nsapi_error_t retcode; |
35 |
| - uint8_t retry_counter = 0; |
36 |
| - |
37 |
| - while (!iface.is_connected()) { |
38 |
| - |
39 |
| - retcode = iface.connect(); |
40 |
| - if (retcode == NSAPI_ERROR_AUTH_FAILURE) { |
41 |
| - printf("\n\nAuthentication Failure. Exiting application\n"); |
42 |
| - return retcode; |
43 |
| - } else if (retcode != NSAPI_ERROR_OK) { |
44 |
| - printf("\n\nCouldn't connect: %d, will retry\n", retcode); |
45 |
| - retry_counter++; |
46 |
| - continue; |
47 |
| - } else if (retcode != NSAPI_ERROR_OK && retry_counter > RETRY_COUNT) { |
48 |
| - printf("\n\nFatal connection failure: %d\n", retcode); |
49 |
| - return retcode; |
50 |
| - } |
51 |
| - |
52 |
| - break; |
53 |
| - } |
54 |
| - |
55 |
| - printf("\n\nConnection Established.\n"); |
56 |
| - |
57 |
| - return NSAPI_ERROR_OK; |
58 |
| -} |
59 |
| - |
60 |
| -/** |
61 |
| - * Opens a TCP socket with the given echo server and undegoes an echo |
62 |
| - * transaction. |
63 |
| - */ |
64 |
| -nsapi_error_t test_send_recv() |
65 |
| -{ |
66 |
| - nsapi_size_or_error_t retcode; |
67 |
| - |
68 |
| - TCPSocket sock; |
| 8 | +/* Set Pin code for SIM card */ |
| 9 | +iface.set_sim_pin(PIN_CODE); |
69 | 10 |
|
70 |
| - retcode = sock.open(&iface); |
71 |
| - if (retcode != NSAPI_ERROR_OK) { |
72 |
| - printf("TCPSocket.open() fails, code: %d\n", retcode); |
73 |
| - return -1; |
74 |
| - } |
| 11 | +/* Set network credentials here, for example the APN */ |
| 12 | +iface.set_credentials(CREDENTIALS); |
75 | 13 |
|
76 |
| - SocketAddress sock_addr; |
77 |
| - retcode = iface.gethostbyname(host_name, &sock_addr); |
78 |
| - if (retcode != NSAPI_ERROR_OK) { |
79 |
| - printf("Couldn't resolve remote host: %s, code: %d\n", host_name, |
80 |
| - retcode); |
81 |
| - return -1; |
82 |
| - } |
83 |
| - |
84 |
| - sock_addr.set_port(port); |
85 |
| - |
86 |
| - sock.set_timeout(15000); |
87 |
| - int n = 0; |
88 |
| - char *echo_string = "TEST"; |
89 |
| - char recv_buf[4]; |
90 |
| - |
91 |
| - retcode = sock.connect(sock_addr); |
92 |
| - if (retcode < 0) { |
93 |
| - printf("TCPSocket.connect() fails, code: %d\n", retcode); |
94 |
| - return -1; |
95 |
| - } else { |
96 |
| - printf("TCP: connected with %s server\n", host_name); |
97 |
| - } |
98 |
| - retcode = sock.send((void*) echo_string, sizeof(echo_string)); |
99 |
| - if (retcode < 0) { |
100 |
| - printf("TCPSocket.send() fails, code: %d\n", retcode); |
101 |
| - return -1; |
102 |
| - } else { |
103 |
| - printf("TCP: Sent %d Bytes to %s\n", retcode, host_name); |
104 |
| - } |
105 |
| - |
106 |
| - n = sock.recv((void*) recv_buf, sizeof(recv_buf)); |
107 |
| - |
108 |
| - sock.close(); |
109 |
| - |
110 |
| - if (n > 0) { |
111 |
| - printf("Received from echo server %d Bytes\n", n); |
112 |
| - return 0; |
113 |
| - } |
114 |
| - |
115 |
| - return -1; |
116 |
| - |
117 |
| - return retcode; |
118 |
| -} |
119 |
| - |
120 |
| -int main() |
121 |
| -{ |
122 |
| - iface.modem_debug_on(MBED_CONF_APP_MODEM_TRACE); |
123 |
| - /* Set Pin code for SIM card */ |
124 |
| - iface.set_sim_pin(PIN_CODE); |
125 |
| - |
126 |
| - /* Set network credentials here, e.g., APN*/ |
127 |
| - iface.set_credentials(CREDENTIALS); |
128 |
| - |
129 |
| - printf("\n\nmbed-os-example-cellular, Connecting...\n"); |
| 14 | +/* Connect */ |
| 15 | +iface.connect() |
| 16 | +``` |
130 | 17 |
|
131 |
| - /* Attempt to connect to a cellular network */ |
132 |
| - if (do_connect() == NSAPI_ERROR_OK) { |
133 |
| - nsapi_error_t retcode = test_send_recv(); |
134 |
| - if (retcode == NSAPI_ERROR_OK) { |
135 |
| - printf("\n\nSuccess. Exiting \n\n"); |
136 |
| - return 0; |
137 |
| - } |
138 |
| - } |
| 18 | +This example demonstrates how to establish a connection and proceed to a simple TCP echo test: |
139 | 19 |
|
140 |
| - printf("\n\nFailure. Exiting \n\n"); |
141 |
| - return -1; |
142 |
| -} |
143 |
| -// EOF |
144 |
| -``` |
| 20 | +[](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/CellularTCP/main.cpp) |
0 commit comments