|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2020 Arm Limited and affiliates. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +#include "mbed.h" |
| 6 | +#include "CellularNonIPSocket.h" |
| 7 | +#include "CellularDevice.h" |
| 8 | + |
| 9 | +// Network interface |
| 10 | +NetworkInterface *iface; |
| 11 | + |
| 12 | +int main() |
| 13 | +{ |
| 14 | + // Bring up the cellular interface |
| 15 | + iface = CellularContext::get_default_nonip_instance(); |
| 16 | + MBED_ASSERT(iface); |
| 17 | + |
| 18 | + // sim pin, apn, credentials and possible plmn are taken automatically from json when using NetworkInterface::set_default_parameters() |
| 19 | + iface->set_default_parameters(); |
| 20 | + |
| 21 | + printf("Cellular Non-IP Socket example\n"); |
| 22 | + if (NSAPI_ERROR_OK != iface->connect() || NSAPI_STATUS_GLOBAL_UP != iface->get_connection_status()) { |
| 23 | + printf("Error connecting\n"); |
| 24 | + return -1; |
| 25 | + } |
| 26 | + |
| 27 | + CellularNonIPSocket sock; |
| 28 | + |
| 29 | + nsapi_error_t retcode = sock.open((CellularContext *)iface); |
| 30 | + |
| 31 | + if (retcode != NSAPI_ERROR_OK) { |
| 32 | + printf("CellularNonIPSocket.open() fails, code: %d\n", retcode); |
| 33 | + return -1; |
| 34 | + } |
| 35 | + |
| 36 | + const char *send_string = "TEST"; |
| 37 | + |
| 38 | + if (0 > sock.send((void *) send_string, sizeof(send_string))) { |
| 39 | + printf("Error sending data\n"); |
| 40 | + return -1; |
| 41 | + } |
| 42 | + |
| 43 | + printf("Success sending data\n"); |
| 44 | + |
| 45 | + char recv_buf[4]; |
| 46 | + if (0 > sock.recv((void *)recv_buf, sizeof(recv_buf))) { |
| 47 | + printf("Error receiving data\n"); |
| 48 | + return -1; |
| 49 | + } |
| 50 | + |
| 51 | + printf("Success receiving data\n"); |
| 52 | + |
| 53 | + // Close the socket and bring down the network interface |
| 54 | + sock.close(); |
| 55 | + iface->disconnect(); |
| 56 | + return 0; |
| 57 | +} |
0 commit comments