|
| 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 Non-IP datagrams using Control Plane CIoT EPS optimisation feature of the NB-IoT networks. 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 CIoT EPS optimisation mode of operation if it is requested and cellular network supports it. |
| 8 | + |
| 9 | +Control Plane CIoT EPS 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: |
| 10 | + |
| 11 | +```json |
| 12 | +{ |
| 13 | + "name": "cellular", |
| 14 | + "config": { |
| 15 | + ... |
| 16 | + "control-plane-opt": { |
| 17 | + "help": "Enables control plane CIoT EPS optimisation", |
| 18 | + "value": true |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | +``` |
| 23 | +ControlPlane |
| 24 | +### CellularNonIPSocket class reference |
| 25 | + |
| 26 | +[](https://os.mbed.com/docs/development/mbed-os-api-doxy/classmbed_1_1_cellular_non_i_p_socket.html) |
| 27 | + |
| 28 | +The following code demonstrates how to create and use a cellular Non-IP socket: |
| 29 | +``` |
| 30 | +
|
| 31 | +#include "mbed.h" |
| 32 | +#include "CellularNonIPSocket.h" |
| 33 | +#include "CellularDevice.h" |
| 34 | +
|
| 35 | +// Network interface |
| 36 | +NetworkInterface *iface; |
| 37 | +
|
| 38 | +int main() { |
| 39 | + // Bring up the cellular interface |
| 40 | + iface = CellularContext::get_default_nonip_instance(); |
| 41 | + MBED_ASSERT(iface); |
| 42 | +
|
| 43 | + // sim pin, apn, credentials and possible plmn are taken automatically from json when using NetworkInterface::set_default_parameters() |
| 44 | + iface->set_default_parameters(); |
| 45 | +
|
| 46 | + printf("Cellular Non-IP Socket example\n"); |
| 47 | + if(NSAPI_ERROR_OK != iface->connect() || NSAPI_STATUS_GLOBAL_UP != iface->get_connection_status()) { |
| 48 | + printf("Error connecting\n"); |
| 49 | + return -1; |
| 50 | + } |
| 51 | +
|
| 52 | + CellularNonIPSocket sock; |
| 53 | +
|
| 54 | + nsapi_error_t retcode = sock.open((CellularContext*)iface); |
| 55 | +
|
| 56 | + if (retcode != NSAPI_ERROR_OK) { |
| 57 | + printf("CellularNonIPSocket.open() fails, code: %d\n", retcode); |
| 58 | + return -1; |
| 59 | + } |
| 60 | +
|
| 61 | + const char *send_string = "TEST"; |
| 62 | +
|
| 63 | + if(0 > sock.send((void*) send_string, sizeof(send_string))) { |
| 64 | + printf("Error sending data\n"); |
| 65 | + return -1; |
| 66 | + } |
| 67 | +
|
| 68 | + printf("Success sending data\n"); |
| 69 | +
|
| 70 | + char recv_buf[4]; |
| 71 | + if(0 > sock.recv((void *)recv_buf, sizeof(recv_buf))) { |
| 72 | + printf("Error receiving data\n"); |
| 73 | + return -1; |
| 74 | + } |
| 75 | +
|
| 76 | + printf("Success receiving data\n"); |
| 77 | +
|
| 78 | + // Close the socket and bring down the network interface |
| 79 | + sock.close(); |
| 80 | + iface->disconnect(); |
| 81 | + return 0; |
| 82 | +} |
| 83 | +
|
| 84 | +``` |
0 commit comments