|
| 1 | +## Non-IP cellular socket |
| 2 | + |
| 3 | +The CellularNonIPSocket class provides, through standard socket send and recv member functions, the ability to send and receive 3GPP Non-IP datagrams (NIDD) using Cellular IoT feature. This feature is implemented in [`ControlPlane_netif`](https://os.mbed.com/docs/development/mbed-os-api-doxy/classmbed_1_1_control_plane__netif.html) class. |
| 4 | + |
| 5 | +The constructor takes no parameters. To initialize the socket on a specified NetworkInterface, you must call `open` method, which takes a CellularContext pointer. |
| 6 | + |
| 7 | +[`CellularContext`](https://os.mbed.com/docs/development/mbed-os-api-doxy/_cellular_context_8h.html) is setting up the modem into the Control Plane optimisation mode of operation if it is requested and cellular network supports it. |
| 8 | + |
| 9 | +Control plane optimisations is a new feature for Cellular IoT (CIoT), where the device can use cellular control channels for data communications, to save power and resources as there is less radio singling needed. |
| 10 | + |
| 11 | +NOTE! Non-IP usage usually requires integration to cellular operator messaging service. The service supports a web API to send and receive to the non-IP using devices. |
| 12 | + |
| 13 | + |
| 14 | +Control Plane optimisation mode can be requested either on CellularDevice's [`create_context`](https://os.mbed.com/docs/development/mbed-os-api-doxy/classmbed_1_1_cellular_device.html#a43b9e992dff1cb5d880acec576e9d06f) or configured in cellular mbed_lib.json: |
| 15 | + |
| 16 | +```json |
| 17 | +{ |
| 18 | + "name": "cellular", |
| 19 | + "config": { |
| 20 | + "control-plane-opt": { |
| 21 | + "help": "Enables control plane CIoT EPS optimisation", |
| 22 | + "value": true |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | +``` |
| 27 | +ControlPlane |
| 28 | +### CellularNonIPSocket class reference |
| 29 | + |
| 30 | +[](https://os.mbed.com/docs/development/mbed-os-api-doxy/classmbed_1_1_cellular_non_i_p_socket.html) |
| 31 | + |
| 32 | +The following code demonstrates how to create and use a cellular Non-IP socket: |
| 33 | +``` |
| 34 | +
|
| 35 | +#include "mbed.h" |
| 36 | +#include "CellularNonIPSocket.h" |
| 37 | +#include "CellularDevice.h" |
| 38 | +
|
| 39 | +// Network interface |
| 40 | +NetworkInterface *iface; |
| 41 | +
|
| 42 | +int main() { |
| 43 | + // Bring up the cellular interface |
| 44 | + iface = CellularContext::get_default_nonip_instance(); |
| 45 | + MBED_ASSERT(iface); |
| 46 | +
|
| 47 | + // sim pin, apn, credentials and possible plmn are taken automatically from json when using NetworkInterface::set_default_parameters() |
| 48 | + iface->set_default_parameters(); |
| 49 | +
|
| 50 | + printf("Cellular Non-IP Socket example\n"); |
| 51 | + if(NSAPI_ERROR_OK != iface->connect() || NSAPI_STATUS_GLOBAL_UP != iface->get_connection_status()) { |
| 52 | + printf("Error connecting\n"); |
| 53 | + return -1; |
| 54 | + } |
| 55 | +
|
| 56 | + CellularNonIPSocket sock; |
| 57 | +
|
| 58 | + nsapi_error_t retcode = sock.open((CellularContext*)iface); |
| 59 | +
|
| 60 | + if (retcode != NSAPI_ERROR_OK) { |
| 61 | + printf("CellularNonIPSocket.open() fails, code: %d\n", retcode); |
| 62 | + return -1; |
| 63 | + } |
| 64 | +
|
| 65 | + const char *send_string = "TEST"; |
| 66 | +
|
| 67 | + if(0 > sock.send((void*) send_string, sizeof(send_string))) { |
| 68 | + printf("Error sending data\n"); |
| 69 | + return -1; |
| 70 | + } |
| 71 | +
|
| 72 | + printf("Success sending data\n"); |
| 73 | +
|
| 74 | + char recv_buf[4]; |
| 75 | + if(0 > sock.recv((void *)recv_buf, sizeof(recv_buf))) { |
| 76 | + printf("Error receiving data\n"); |
| 77 | + return -1; |
| 78 | + } |
| 79 | +
|
| 80 | + printf("Success receiving data\n"); |
| 81 | +
|
| 82 | + // Close the socket and bring down the network interface |
| 83 | + sock.close(); |
| 84 | + iface->disconnect(); |
| 85 | + return 0; |
| 86 | +} |
| 87 | +
|
| 88 | +``` |
0 commit comments