Skip to content

Move code example to the examples repo #804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 11 additions & 135 deletions docs/tutorials/using_apis/connectivity/cellular_tcp.md
Original file line number Diff line number Diff line change
@@ -1,144 +1,20 @@
## Cellular TCP sockets

This example opens a TCP socket with an echo server and performs a TCP transaction.
Establishing a cellular connection to the network with Mbed OS only requires the following operations:

```cpp TODO
#include "mbed.h"
#include "UDPSocket.h"
#include "OnboardCellularInterface.h"

// SIM pin code goes here
#define PIN_CODE "1234"

// Network credentials like APN go here, e.g.,
// "apn, username, password"
#define CREDENTIALS "internet"

// Number of retries /
#define RETRY_COUNT 3

// Cellular network interface object
```
OnboardCellularInterface iface;

// Echo server hostname
const char *host_name = "echo.u-blox.com";

// Echo server TCP port
const int port = 7;

/**
* Connects to the Cellular Network
*/
nsapi_error_t do_connect()
{
nsapi_error_t retcode;
uint8_t retry_counter = 0;

while (!iface.is_connected()) {

retcode = iface.connect();
if (retcode == NSAPI_ERROR_AUTH_FAILURE) {
printf("\n\nAuthentication Failure. Exiting application\n");
return retcode;
} else if (retcode != NSAPI_ERROR_OK) {
printf("\n\nCouldn't connect: %d, will retry\n", retcode);
retry_counter++;
continue;
} else if (retcode != NSAPI_ERROR_OK && retry_counter > RETRY_COUNT) {
printf("\n\nFatal connection failure: %d\n", retcode);
return retcode;
}

break;
}

printf("\n\nConnection Established.\n");

return NSAPI_ERROR_OK;
}

/**
* Opens a TCP socket with the given echo server and undegoes an echo
* transaction.
*/
nsapi_error_t test_send_recv()
{
nsapi_size_or_error_t retcode;

TCPSocket sock;
/* Set Pin code for SIM card */
iface.set_sim_pin(PIN_CODE);

retcode = sock.open(&iface);
if (retcode != NSAPI_ERROR_OK) {
printf("TCPSocket.open() fails, code: %d\n", retcode);
return -1;
}
/* Set network credentials here, for example the APN */
iface.set_credentials(CREDENTIALS);

SocketAddress sock_addr;
retcode = iface.gethostbyname(host_name, &sock_addr);
if (retcode != NSAPI_ERROR_OK) {
printf("Couldn't resolve remote host: %s, code: %d\n", host_name,
retcode);
return -1;
}

sock_addr.set_port(port);

sock.set_timeout(15000);
int n = 0;
char *echo_string = "TEST";
char recv_buf[4];

retcode = sock.connect(sock_addr);
if (retcode < 0) {
printf("TCPSocket.connect() fails, code: %d\n", retcode);
return -1;
} else {
printf("TCP: connected with %s server\n", host_name);
}
retcode = sock.send((void*) echo_string, sizeof(echo_string));
if (retcode < 0) {
printf("TCPSocket.send() fails, code: %d\n", retcode);
return -1;
} else {
printf("TCP: Sent %d Bytes to %s\n", retcode, host_name);
}

n = sock.recv((void*) recv_buf, sizeof(recv_buf));

sock.close();

if (n > 0) {
printf("Received from echo server %d Bytes\n", n);
return 0;
}

return -1;

return retcode;
}

int main()
{
iface.modem_debug_on(MBED_CONF_APP_MODEM_TRACE);
/* Set Pin code for SIM card */
iface.set_sim_pin(PIN_CODE);

/* Set network credentials here, e.g., APN*/
iface.set_credentials(CREDENTIALS);

printf("\n\nmbed-os-example-cellular, Connecting...\n");
/* Connect */
iface.connect()
```

/* Attempt to connect to a cellular network */
if (do_connect() == NSAPI_ERROR_OK) {
nsapi_error_t retcode = test_send_recv();
if (retcode == NSAPI_ERROR_OK) {
printf("\n\nSuccess. Exiting \n\n");
return 0;
}
}
This example demonstrates how to establish a connection and proceed to a simple TCP echo test:

printf("\n\nFailure. Exiting \n\n");
return -1;
}
// EOF
```
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/CellularTCP/)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/CellularTCP/main.cpp)