Implementation of the CellularInterface for u-blox C030 boards with N2xx modems. Note: requires the N211 module firmware to be at least 06.57 A01.02.

Dependents:   example-ublox-cellular-interface HelloMQTT example-ublox-cellular-interface_r410M example-ublox-mbed-client ... more

Committer:
philware
Date:
Mon Jun 26 13:49:08 2017 +0100
Revision:
1:8ea78dce6b36
Child:
4:2bf3875a13f1
UDP Sockets over AT for SARA-N2xx modules

Who changed what in which revision?

UserRevisionLine numberNew contents of line
philware 1:8ea78dce6b36 1 /* Copyright (c) 2017 ARM Limited
philware 1:8ea78dce6b36 2 *
philware 1:8ea78dce6b36 3 * Licensed under the Apache License, Version 2.0 (the "License");
philware 1:8ea78dce6b36 4 * you may not use this file except in compliance with the License.
philware 1:8ea78dce6b36 5 * You may obtain a copy of the License at
philware 1:8ea78dce6b36 6 *
philware 1:8ea78dce6b36 7 * http://www.apache.org/licenses/LICENSE-2.0
philware 1:8ea78dce6b36 8 *
philware 1:8ea78dce6b36 9 * Unless required by applicable law or agreed to in writing, software
philware 1:8ea78dce6b36 10 * distributed under the License is distributed on an "AS IS" BASIS,
philware 1:8ea78dce6b36 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
philware 1:8ea78dce6b36 12 * See the License for the specific language governing permissions and
philware 1:8ea78dce6b36 13 * limitations under the License.
philware 1:8ea78dce6b36 14 */
philware 1:8ea78dce6b36 15
philware 1:8ea78dce6b36 16 #ifndef _UBLOX_AT_CELLULAR_INTERFACE_N2XX_
philware 1:8ea78dce6b36 17 #define _UBLOX_AT_CELLULAR_INTERFACE_N2XX_
philware 1:8ea78dce6b36 18
philware 1:8ea78dce6b36 19 #include "UbloxCellularBaseN2xx.h"
philware 1:8ea78dce6b36 20 #include "CellularBase.h"
philware 1:8ea78dce6b36 21 #include "NetworkStack.h"
philware 1:8ea78dce6b36 22
philware 1:8ea78dce6b36 23 /** UbloxATCellularInterface class.
philware 1:8ea78dce6b36 24 *
philware 1:8ea78dce6b36 25 * This uses the cellular-tuned IP stack that
philware 1:8ea78dce6b36 26 * is on-board the cellular modem instead of the
philware 1:8ea78dce6b36 27 * LWIP stack on the mbed MCU.
philware 1:8ea78dce6b36 28 *
philware 1:8ea78dce6b36 29 * There are three advantages to using this mechanism:
philware 1:8ea78dce6b36 30 *
philware 1:8ea78dce6b36 31 * 1. Since the modem interface remains in AT mode
philware 1:8ea78dce6b36 32 * throughout, it is possible to continue using
philware 1:8ea78dce6b36 33 * any AT commands (e.g. send SMS, use the module's
philware 1:8ea78dce6b36 34 * file system, etc.) while the connection is up.
philware 1:8ea78dce6b36 35 *
philware 1:8ea78dce6b36 36 * 2. The UbloxATCellularInterfaceExt class can
philware 1:8ea78dce6b36 37 * be used to perform very simple HTTP and FTP
philware 1:8ea78dce6b36 38 * operations using the modem's on-board clients.
philware 1:8ea78dce6b36 39 *
philware 1:8ea78dce6b36 40 * 3. LWIP is not required (and hence RAM is saved).
philware 1:8ea78dce6b36 41 *
philware 1:8ea78dce6b36 42 * The disadvantage is that some additional parsing
philware 1:8ea78dce6b36 43 * (at the AT interface) has to go on in order to exchange
philware 1:8ea78dce6b36 44 * IP packets, so this is less efficient under heavy loads.
philware 1:8ea78dce6b36 45 * Also TCP Server and getting/setting of socket options is
philware 1:8ea78dce6b36 46 * currently not supported.
philware 1:8ea78dce6b36 47 */
philware 1:8ea78dce6b36 48
philware 1:8ea78dce6b36 49 // Forward declaration
philware 1:8ea78dce6b36 50 class NetworkStack;
philware 1:8ea78dce6b36 51
philware 1:8ea78dce6b36 52 /*
philware 1:8ea78dce6b36 53 * NOTE: order is important in the inheritance below! PAL takes this class
philware 1:8ea78dce6b36 54 * and casts it to CellularInterface and so CellularInterface has to be first
philware 1:8ea78dce6b36 55 * in the last for that to work.
philware 1:8ea78dce6b36 56 */
philware 1:8ea78dce6b36 57
philware 1:8ea78dce6b36 58 /** UbloxATCellularInterface class.
philware 1:8ea78dce6b36 59 *
philware 1:8ea78dce6b36 60 * This class implements the network stack interface into the cellular
philware 1:8ea78dce6b36 61 * modems on the C030 and C027 boards for 2G/3G/4G modules using
philware 1:8ea78dce6b36 62 * the IP stack running on the cellular module.
philware 1:8ea78dce6b36 63 */
philware 1:8ea78dce6b36 64 class UbloxATCellularInterfaceN2xx : public CellularBase, public NetworkStack, virtual public UbloxCellularBaseN2xx {
philware 1:8ea78dce6b36 65
philware 1:8ea78dce6b36 66 public:
philware 1:8ea78dce6b36 67 // test command.
philware 1:8ea78dce6b36 68 bool SendAT(const char *cmd);
philware 1:8ea78dce6b36 69
philware 1:8ea78dce6b36 70 /** Constructor.
philware 1:8ea78dce6b36 71 *
philware 1:8ea78dce6b36 72 * @param tx the UART TX data pin to which the modem is attached.
philware 1:8ea78dce6b36 73 * @param rx the UART RX data pin to which the modem is attached.
philware 1:8ea78dce6b36 74 * @param baud the UART baud rate.
philware 1:8ea78dce6b36 75 * @param debug_on true to switch AT interface debug on, otherwise false.
philware 1:8ea78dce6b36 76 */
philware 1:8ea78dce6b36 77 UbloxATCellularInterfaceN2xx(PinName tx = MDMTXD,
philware 1:8ea78dce6b36 78 PinName rx = MDMRXD,
philware 1:8ea78dce6b36 79 int baud = MBED_CONF_UBLOX_CELL_BAUD_RATE,
philware 1:8ea78dce6b36 80 bool debug_on = false);
philware 1:8ea78dce6b36 81
philware 1:8ea78dce6b36 82 /* Destructor.
philware 1:8ea78dce6b36 83 */
philware 1:8ea78dce6b36 84 virtual ~UbloxATCellularInterfaceN2xx();
philware 1:8ea78dce6b36 85
philware 1:8ea78dce6b36 86 /** The amount of extra space needed in terms of AT interface
philware 1:8ea78dce6b36 87 * characters to get a chunk of user data (i.e. one UDP packet
philware 1:8ea78dce6b36 88 * or a portion of a TCP packet) across the AT interface.
philware 1:8ea78dce6b36 89 */
philware 1:8ea78dce6b36 90 #define AT_PACKET_OVERHEAD 77
philware 1:8ea78dce6b36 91
philware 1:8ea78dce6b36 92 /** The profile to use (on board the modem).
philware 1:8ea78dce6b36 93 */
philware 1:8ea78dce6b36 94 #define PROFILE "0"
philware 1:8ea78dce6b36 95
philware 1:8ea78dce6b36 96 /** Translates a host name to an IP address with specific IP version.
philware 1:8ea78dce6b36 97 *
philware 1:8ea78dce6b36 98 * The host name may be either a domain name or an IP address. If the
philware 1:8ea78dce6b36 99 * host name is an IP address, no network transactions will be performed.
philware 1:8ea78dce6b36 100 *
philware 1:8ea78dce6b36 101 * If no stack-specific DNS resolution is provided, the host name
philware 1:8ea78dce6b36 102 * will be resolved using a UDP socket on the stack.
philware 1:8ea78dce6b36 103 *
philware 1:8ea78dce6b36 104 * @param host Host name to resolve.
philware 1:8ea78dce6b36 105 * @param address Destination for the host SocketAddress.
philware 1:8ea78dce6b36 106 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
philware 1:8ea78dce6b36 107 * version is chosen by the stack (defaults to NSAPI_UNSPEC).
philware 1:8ea78dce6b36 108 * @return 0 on success, negative error code on failure.
philware 1:8ea78dce6b36 109 */
philware 1:8ea78dce6b36 110 virtual nsapi_error_t gethostbyname(const char *host,
philware 1:8ea78dce6b36 111 SocketAddress *address,
philware 1:8ea78dce6b36 112 nsapi_version_t version = NSAPI_UNSPEC);
philware 1:8ea78dce6b36 113
philware 1:8ea78dce6b36 114 /** Set the authentication scheme.
philware 1:8ea78dce6b36 115 *
philware 1:8ea78dce6b36 116 * @param auth The authentication scheme, chose from
philware 1:8ea78dce6b36 117 * NSAPI_SECURITY_NONE, NSAPI_SECURITY_PAP,
philware 1:8ea78dce6b36 118 * NSAPI_SECURITY_CHAP or NSAPI_SECURITY_UNKNOWN;
philware 1:8ea78dce6b36 119 * use NSAPI_SECURITY_UNKNOWN to try all of none,
philware 1:8ea78dce6b36 120 * PAP and CHAP.
philware 1:8ea78dce6b36 121 */
philware 1:8ea78dce6b36 122 virtual void set_authentication(nsapi_security_t auth);
philware 1:8ea78dce6b36 123
philware 1:8ea78dce6b36 124 /** Set the cellular network credentials.
philware 1:8ea78dce6b36 125 *
philware 1:8ea78dce6b36 126 * Please check documentation of connect() for default behaviour of APN settings.
philware 1:8ea78dce6b36 127 *
philware 1:8ea78dce6b36 128 * @param apn Access point name.
philware 1:8ea78dce6b36 129 * @param uname Optionally, user name.
philware 1:8ea78dce6b36 130 * @param pwd Optionally, password.
philware 1:8ea78dce6b36 131 */
philware 1:8ea78dce6b36 132 virtual void set_credentials(const char *apn, const char *uname = 0,
philware 1:8ea78dce6b36 133 const char *pwd = 0);
philware 1:8ea78dce6b36 134
philware 1:8ea78dce6b36 135 /** Set the PIN code for the SIM card.
philware 1:8ea78dce6b36 136 *
philware 1:8ea78dce6b36 137 * @param sim_pin PIN for the SIM card.
philware 1:8ea78dce6b36 138 */
philware 1:8ea78dce6b36 139 virtual void set_sim_pin(const char *sim_pin);
philware 1:8ea78dce6b36 140
philware 1:8ea78dce6b36 141 /** Set the local listen port when opening a UDP socket
philware 1:8ea78dce6b36 142 *
philware 1:8ea78dce6b36 143 */
philware 1:8ea78dce6b36 144 void set_LocalListenPort(int port);
philware 1:8ea78dce6b36 145
philware 1:8ea78dce6b36 146 /** Connect to the cellular network and start the interface.
philware 1:8ea78dce6b36 147 *
philware 1:8ea78dce6b36 148 * Attempts to connect to a cellular network. Note: if init() has
philware 1:8ea78dce6b36 149 * not been called beforehand, connect() will call it first.
philware 1:8ea78dce6b36 150 *
philware 1:8ea78dce6b36 151 * @param sim_pin PIN for the SIM card.
philware 1:8ea78dce6b36 152 * @param apn Optionally, access point name.
philware 1:8ea78dce6b36 153 * @param uname Optionally, user name.
philware 1:8ea78dce6b36 154 * @param pwd Optionally, password.
philware 1:8ea78dce6b36 155 * @return NSAPI_ERROR_OK on success, or negative error code on failure.
philware 1:8ea78dce6b36 156 */
philware 1:8ea78dce6b36 157 virtual nsapi_error_t connect(const char *sim_pin, const char *apn = 0,
philware 1:8ea78dce6b36 158 const char *uname = 0, const char *pwd = 0);
philware 1:8ea78dce6b36 159
philware 1:8ea78dce6b36 160 /** Attempt to connect to the cellular network.
philware 1:8ea78dce6b36 161 *
philware 1:8ea78dce6b36 162 * Brings up the network interface. Connects to the cellular radio
philware 1:8ea78dce6b36 163 * network and then brings up IP stack on the cellular modem to be used
philware 1:8ea78dce6b36 164 * indirectly via AT commands, rather than LWIP. Note: if init() has
philware 1:8ea78dce6b36 165 * not been called beforehand, connect() will call it first.
philware 1:8ea78dce6b36 166 * NOTE: even a failed attempt to connect will cause the modem to remain
philware 1:8ea78dce6b36 167 * powered up. To power it down, call deinit().
philware 1:8ea78dce6b36 168 *
philware 1:8ea78dce6b36 169 * For APN setup, default behaviour is to use 'internet' as APN string
philware 1:8ea78dce6b36 170 * and assuming no authentication is required, i.e., user name and password
philware 1:8ea78dce6b36 171 * are not set. Optionally, a database lookup can be requested by turning
philware 1:8ea78dce6b36 172 * on the APN database lookup feature. The APN database is by no means
philware 1:8ea78dce6b36 173 * exhaustive (feel free to submit a pull request with additional values).
philware 1:8ea78dce6b36 174 * It contains a short list of some public APNs with publicly available
philware 1:8ea78dce6b36 175 * user names and passwords (if required) in some particular countries only.
philware 1:8ea78dce6b36 176 * Lookup is done using IMSI (International mobile subscriber identifier).
philware 1:8ea78dce6b36 177 *
philware 1:8ea78dce6b36 178 * The preferred method is to setup APN using 'set_credentials()' API.
philware 1:8ea78dce6b36 179 *
philware 1:8ea78dce6b36 180 * If you find that the AT interface returns "CONNECT" but shortly afterwards
philware 1:8ea78dce6b36 181 * drops the connection then 99% of the time this will be because the APN
philware 1:8ea78dce6b36 182 * is incorrect.
philware 1:8ea78dce6b36 183 *
philware 1:8ea78dce6b36 184 * @return 0 on success, negative error code on failure.
philware 1:8ea78dce6b36 185 */
philware 1:8ea78dce6b36 186 virtual nsapi_error_t connect();
philware 1:8ea78dce6b36 187
philware 1:8ea78dce6b36 188 /** Attempt to disconnect from the network.
philware 1:8ea78dce6b36 189 *
philware 1:8ea78dce6b36 190 * Brings down the network interface.
philware 1:8ea78dce6b36 191 * Does not bring down the Radio network.
philware 1:8ea78dce6b36 192 *
philware 1:8ea78dce6b36 193 * @return 0 on success, negative error code on failure.
philware 1:8ea78dce6b36 194 */
philware 1:8ea78dce6b36 195 virtual nsapi_error_t disconnect();
philware 1:8ea78dce6b36 196
philware 1:8ea78dce6b36 197 /** Adds or removes a SIM facility lock.
philware 1:8ea78dce6b36 198 *
philware 1:8ea78dce6b36 199 * Can be used to enable or disable SIM PIN check at device startup.
philware 1:8ea78dce6b36 200 *
philware 1:8ea78dce6b36 201 * @param set Can be set to true if the SIM PIN check is supposed
philware 1:8ea78dce6b36 202 * to be enabled and vice versa.
philware 1:8ea78dce6b36 203 * @param immediate If true, change the SIM PIN now, else set a flag
philware 1:8ea78dce6b36 204 * and make the change only when connect() is called.
philware 1:8ea78dce6b36 205 * If this is true and init() has not been called previously,
philware 1:8ea78dce6b36 206 * it will be called first.
philware 1:8ea78dce6b36 207 * @param sim_pin The current SIM PIN, must be a const. If this is not
philware 1:8ea78dce6b36 208 * provided, the SIM PIN must have previously been set by a
philware 1:8ea78dce6b36 209 * call to set_sim_pin().
philware 1:8ea78dce6b36 210 * @return 0 on success, negative error code on failure.
philware 1:8ea78dce6b36 211 */
philware 1:8ea78dce6b36 212 nsapi_error_t set_sim_pin_check(bool set, bool immediate = false,
philware 1:8ea78dce6b36 213 const char *sim_pin = NULL);
philware 1:8ea78dce6b36 214
philware 1:8ea78dce6b36 215 /** Change the PIN for the SIM card.
philware 1:8ea78dce6b36 216 *
philware 1:8ea78dce6b36 217 * Provide the new PIN for your SIM card with this API. It is ONLY possible to
philware 1:8ea78dce6b36 218 * change the SIM PIN when SIM PIN checking is ENABLED.
philware 1:8ea78dce6b36 219 *
philware 1:8ea78dce6b36 220 * @param new_pin New PIN to be used in string format, must be a const.
philware 1:8ea78dce6b36 221 * @param immediate If true, change the SIM PIN now, else set a flag
philware 1:8ea78dce6b36 222 * and make the change only when connect() is called.
philware 1:8ea78dce6b36 223 * If this is true and init() has not been called previously,
philware 1:8ea78dce6b36 224 * it will be called first.
philware 1:8ea78dce6b36 225 * @param old_pin Old PIN, must be a const. If this is not provided, the SIM PIN
philware 1:8ea78dce6b36 226 * must have previously been set by a call to set_sim_pin().
philware 1:8ea78dce6b36 227 * @return 0 on success, negative error code on failure.
philware 1:8ea78dce6b36 228 */
philware 1:8ea78dce6b36 229 nsapi_error_t set_new_sim_pin(const char *new_pin, bool immediate = false,
philware 1:8ea78dce6b36 230 const char *old_pin = NULL);
philware 1:8ea78dce6b36 231
philware 1:8ea78dce6b36 232 /** Check if the connection is currently established or not.
philware 1:8ea78dce6b36 233 *
philware 1:8ea78dce6b36 234 * @return True if connected to a data network, otherwise false.
philware 1:8ea78dce6b36 235 */
philware 1:8ea78dce6b36 236 virtual bool is_connected();
philware 1:8ea78dce6b36 237
philware 1:8ea78dce6b36 238 /** Get the local IP address
philware 1:8ea78dce6b36 239 *
philware 1:8ea78dce6b36 240 * @return Null-terminated representation of the local IP address
philware 1:8ea78dce6b36 241 * or null if no IP address has been received.
philware 1:8ea78dce6b36 242 */
philware 1:8ea78dce6b36 243 virtual const char *get_ip_address();
philware 1:8ea78dce6b36 244
philware 1:8ea78dce6b36 245 /** Get the local network mask.
philware 1:8ea78dce6b36 246 *
philware 1:8ea78dce6b36 247 * @return Null-terminated representation of the local network mask
philware 1:8ea78dce6b36 248 * or null if no network mask has been received.
philware 1:8ea78dce6b36 249 */
philware 1:8ea78dce6b36 250 virtual const char *get_netmask();
philware 1:8ea78dce6b36 251
philware 1:8ea78dce6b36 252 /** Get the local gateways.
philware 1:8ea78dce6b36 253 *
philware 1:8ea78dce6b36 254 * @return Null-terminated representation of the local gateway
philware 1:8ea78dce6b36 255 * or null if no network mask has been received.
philware 1:8ea78dce6b36 256 */
philware 1:8ea78dce6b36 257 virtual const char *get_gateway();
philware 1:8ea78dce6b36 258
philware 1:8ea78dce6b36 259 /** Call back in case connection is lost.
philware 1:8ea78dce6b36 260 *
philware 1:8ea78dce6b36 261 * @param cb The function to call.
philware 1:8ea78dce6b36 262 */
philware 1:8ea78dce6b36 263 void connection_status_cb(Callback<void(nsapi_error_t)> cb);
philware 1:8ea78dce6b36 264
philware 1:8ea78dce6b36 265 protected:
philware 1:8ea78dce6b36 266
philware 1:8ea78dce6b36 267 /** Socket "unused" value.
philware 1:8ea78dce6b36 268 */
philware 1:8ea78dce6b36 269 #define SOCKET_UNUSED -1
philware 1:8ea78dce6b36 270
philware 1:8ea78dce6b36 271 /** Socket timeout value in milliseconds.
philware 1:8ea78dce6b36 272 * Note: the sockets layer above will retry the
philware 1:8ea78dce6b36 273 * call to the functions here when they return NSAPI_ERROR_WOULD_BLOCK
philware 1:8ea78dce6b36 274 * and the user has set a larger timeout or full blocking.
philware 1:8ea78dce6b36 275 */
philware 1:8ea78dce6b36 276 #define SOCKET_TIMEOUT 1000
philware 1:8ea78dce6b36 277
philware 1:8ea78dce6b36 278 /** The maximum number of bytes in a packet that can be written
philware 1:8ea78dce6b36 279 * to the AT interface in one go.
philware 1:8ea78dce6b36 280 */
philware 1:8ea78dce6b36 281 #define MAX_WRITE_SIZE 512
philware 1:8ea78dce6b36 282
philware 1:8ea78dce6b36 283 /** The maximum number of bytes in a packet that can be read from
philware 1:8ea78dce6b36 284 * from the AT interface in one go.
philware 1:8ea78dce6b36 285 */
philware 1:8ea78dce6b36 286 #define MAX_READ_SIZE 512
philware 1:8ea78dce6b36 287
philware 1:8ea78dce6b36 288 /** Management structure for sockets.
philware 1:8ea78dce6b36 289 */
philware 1:8ea78dce6b36 290 typedef struct {
philware 1:8ea78dce6b36 291 int modem_handle; //!< The modem's handle for the socket.
philware 1:8ea78dce6b36 292 volatile nsapi_size_t pending; //!< The number of received bytes pending.
philware 1:8ea78dce6b36 293 void (*callback)(void *); //!< A callback for events.
philware 1:8ea78dce6b36 294 void *data; //!< A data pointer that must be passed to the callback.
philware 1:8ea78dce6b36 295 } SockCtrl;
philware 1:8ea78dce6b36 296
philware 1:8ea78dce6b36 297 /** Sockets storage.
philware 1:8ea78dce6b36 298 */
philware 1:8ea78dce6b36 299 SockCtrl _sockets[7];
philware 1:8ea78dce6b36 300
philware 1:8ea78dce6b36 301 /** Storage for a single IP address.
philware 1:8ea78dce6b36 302 */
philware 1:8ea78dce6b36 303 char *_ip;
philware 1:8ea78dce6b36 304
philware 1:8ea78dce6b36 305 /** The APN to use.
philware 1:8ea78dce6b36 306 */
philware 1:8ea78dce6b36 307 const char *_apn;
philware 1:8ea78dce6b36 308
philware 1:8ea78dce6b36 309 /** The user name to use.
philware 1:8ea78dce6b36 310 */
philware 1:8ea78dce6b36 311 const char *_uname;
philware 1:8ea78dce6b36 312
philware 1:8ea78dce6b36 313 /** The password to use.
philware 1:8ea78dce6b36 314 */
philware 1:8ea78dce6b36 315 const char *_pwd;
philware 1:8ea78dce6b36 316
philware 1:8ea78dce6b36 317 /** The type of authentication to use.
philware 1:8ea78dce6b36 318 */
philware 1:8ea78dce6b36 319 nsapi_security_t _auth;
philware 1:8ea78dce6b36 320
philware 1:8ea78dce6b36 321 /** Get the next set of credentials from the database.
philware 1:8ea78dce6b36 322 */
philware 1:8ea78dce6b36 323 virtual void get_next_credentials(const char * config);
philware 1:8ea78dce6b36 324
philware 1:8ea78dce6b36 325 /** Provide access to the NetworkStack object
philware 1:8ea78dce6b36 326 *
philware 1:8ea78dce6b36 327 * @return The underlying NetworkStack object.
philware 1:8ea78dce6b36 328 */
philware 1:8ea78dce6b36 329 virtual NetworkStack *get_stack();
philware 1:8ea78dce6b36 330
philware 1:8ea78dce6b36 331 protected:
philware 1:8ea78dce6b36 332
philware 1:8ea78dce6b36 333 /** Open a socket.
philware 1:8ea78dce6b36 334 *
philware 1:8ea78dce6b36 335 * Creates a network socket and stores it in the specified handle.
philware 1:8ea78dce6b36 336 * The handle must be passed to following calls on the socket.
philware 1:8ea78dce6b36 337 *
philware 1:8ea78dce6b36 338 * @param handle Destination for the handle to a newly created socket.
philware 1:8ea78dce6b36 339 * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP.
philware 1:8ea78dce6b36 340 * @return 0 on success, negative error code on failure.
philware 1:8ea78dce6b36 341 */
philware 1:8ea78dce6b36 342 virtual nsapi_error_t socket_open(nsapi_socket_t *handle,
philware 1:8ea78dce6b36 343 nsapi_protocol_t proto);
philware 1:8ea78dce6b36 344
philware 1:8ea78dce6b36 345 /** Close a socket.
philware 1:8ea78dce6b36 346 *
philware 1:8ea78dce6b36 347 * Closes any open connection and deallocates any memory associated
philware 1:8ea78dce6b36 348 * with the socket.
philware 1:8ea78dce6b36 349 *
philware 1:8ea78dce6b36 350 * @param handle Socket handle.
philware 1:8ea78dce6b36 351 * @return 0 on success, negative error code on failure.
philware 1:8ea78dce6b36 352 */
philware 1:8ea78dce6b36 353 virtual nsapi_error_t socket_close(nsapi_socket_t handle);
philware 1:8ea78dce6b36 354
philware 1:8ea78dce6b36 355 /** Bind a specific port to a socket.
philware 1:8ea78dce6b36 356 *
philware 1:8ea78dce6b36 357 * Binding a socket specifies port on which to receive
philware 1:8ea78dce6b36 358 * data. The IP address is ignored. Note that binding
philware 1:8ea78dce6b36 359 * a socket involves closing it and reopening and so the
philware 1:8ea78dce6b36 360 * bind operation should be carried out before any others.
philware 1:8ea78dce6b36 361 *
philware 1:8ea78dce6b36 362 * @param handle Socket handle.
philware 1:8ea78dce6b36 363 * @param address Local address to bind (of which only the port is used).
philware 1:8ea78dce6b36 364 * @return 0 on success, negative error code on failure.
philware 1:8ea78dce6b36 365 */
philware 1:8ea78dce6b36 366 virtual nsapi_error_t socket_bind(nsapi_socket_t handle,
philware 1:8ea78dce6b36 367 const SocketAddress &address);
philware 1:8ea78dce6b36 368
philware 1:8ea78dce6b36 369 /** Connects TCP socket to a remote host.
philware 1:8ea78dce6b36 370 *
philware 1:8ea78dce6b36 371 * Initiates a connection to a remote server specified by the
philware 1:8ea78dce6b36 372 * indicated address.
philware 1:8ea78dce6b36 373 *
philware 1:8ea78dce6b36 374 * @param handle Socket handle.
philware 1:8ea78dce6b36 375 * @param address The SocketAddress of the remote host.
philware 1:8ea78dce6b36 376 * @return 0 on success, negative error code on failure.
philware 1:8ea78dce6b36 377 */
philware 1:8ea78dce6b36 378 virtual nsapi_error_t socket_connect(nsapi_socket_t handle,
philware 1:8ea78dce6b36 379 const SocketAddress &address);
philware 1:8ea78dce6b36 380
philware 1:8ea78dce6b36 381 /** Send data over a TCP socket.
philware 1:8ea78dce6b36 382 *
philware 1:8ea78dce6b36 383 * The socket must be connected to a remote host. Returns the number of
philware 1:8ea78dce6b36 384 * bytes sent from the buffer. This class sets no upper buffer limit on
philware 1:8ea78dce6b36 385 * buffer size and the maximum packet size is not connected with the
philware 1:8ea78dce6b36 386 * platform.buffered-serial-txbuf-size/platform.buffered-serial-rxbuf-size
philware 1:8ea78dce6b36 387 * definitions.
philware 1:8ea78dce6b36 388 *
philware 1:8ea78dce6b36 389 * @param handle Socket handle.
philware 1:8ea78dce6b36 390 * @param data Buffer of data to send to the host.
philware 1:8ea78dce6b36 391 * @param size Size of the buffer in bytes.
philware 1:8ea78dce6b36 392 * @return Number of sent bytes on success, negative error
philware 1:8ea78dce6b36 393 * code on failure.
philware 1:8ea78dce6b36 394 */
philware 1:8ea78dce6b36 395 virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle,
philware 1:8ea78dce6b36 396 const void *data, nsapi_size_t size);
philware 1:8ea78dce6b36 397
philware 1:8ea78dce6b36 398 /** Send a packet over a UDP socket.
philware 1:8ea78dce6b36 399 *
philware 1:8ea78dce6b36 400 * Sends data to the specified address. Returns the number of bytes
philware 1:8ea78dce6b36 401 * sent from the buffer.
philware 1:8ea78dce6b36 402 *
philware 1:8ea78dce6b36 403 * PACKET SIZES: the maximum packet size that can be sent in a single
philware 1:8ea78dce6b36 404 * UDP packet is limited by the configuration value
philware 1:8ea78dce6b36 405 * platform.buffered-serial-txbuf-size (defaults to 256).
philware 1:8ea78dce6b36 406 * The maximum UDP packet size is:
philware 1:8ea78dce6b36 407 *
philware 1:8ea78dce6b36 408 * platform.buffered-serial-txbuf-size - AT_PACKET_OVERHEAD
philware 1:8ea78dce6b36 409 *
philware 1:8ea78dce6b36 410 * ...with a limit of 1024 bytes (at the AT interface). So, to allow sending
philware 1:8ea78dce6b36 411 * of a 1024 byte UDP packet, edit your mbed_app.json to add a target override
philware 1:8ea78dce6b36 412 * setting platform.buffered-serial-txbuf-size to 1101. However, for
philware 1:8ea78dce6b36 413 * UDP packets, 508 bytes is considered a more realistic size, taking into
philware 1:8ea78dce6b36 414 * account fragmentation sizes over the public internet, which leads to a
philware 1:8ea78dce6b36 415 * platform.buffered-serial-txbuf-size/platform.buffered-serial-rxbuf-size
philware 1:8ea78dce6b36 416 * setting of 585.
philware 1:8ea78dce6b36 417 *
philware 1:8ea78dce6b36 418 * If size is larger than this limit, the data will be split across separate
philware 1:8ea78dce6b36 419 * UDP packets.
philware 1:8ea78dce6b36 420 *
philware 1:8ea78dce6b36 421 * @param handle Socket handle.
philware 1:8ea78dce6b36 422 * @param address The SocketAddress of the remote host.
philware 1:8ea78dce6b36 423 * @param data Buffer of data to send to the host.
philware 1:8ea78dce6b36 424 * @param size Size of the buffer in bytes.
philware 1:8ea78dce6b36 425 * @return Number of sent bytes on success, negative error
philware 1:8ea78dce6b36 426 * code on failure.
philware 1:8ea78dce6b36 427 */
philware 1:8ea78dce6b36 428 virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle,
philware 1:8ea78dce6b36 429 const SocketAddress &address,
philware 1:8ea78dce6b36 430 const void *data,
philware 1:8ea78dce6b36 431 nsapi_size_t size);
philware 1:8ea78dce6b36 432
philware 1:8ea78dce6b36 433 /** Receive data over a TCP socket.
philware 1:8ea78dce6b36 434 *
philware 1:8ea78dce6b36 435 * The socket must be connected to a remote host. Returns the number of
philware 1:8ea78dce6b36 436 * bytes received into the buffer. This class sets no upper limit on the
philware 1:8ea78dce6b36 437 * buffer size and the maximum packet size is not connected with the
philware 1:8ea78dce6b36 438 * platform.buffered-serial-txbuf-size/platform.buffered-serial-rxbuf-size
philware 1:8ea78dce6b36 439 * definitions.
philware 1:8ea78dce6b36 440 *
philware 1:8ea78dce6b36 441 * @param handle Socket handle.
philware 1:8ea78dce6b36 442 * @param data Destination buffer for data received from the host.
philware 1:8ea78dce6b36 443 * @param size Size of the buffer in bytes.
philware 1:8ea78dce6b36 444 * @return Number of received bytes on success, negative error
philware 1:8ea78dce6b36 445 * code on failure.
philware 1:8ea78dce6b36 446 */
philware 1:8ea78dce6b36 447 virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle,
philware 1:8ea78dce6b36 448 void *data, nsapi_size_t size);
philware 1:8ea78dce6b36 449
philware 1:8ea78dce6b36 450 /** Receive a packet over a UDP socket.
philware 1:8ea78dce6b36 451 *
philware 1:8ea78dce6b36 452 * Receives data and stores the source address in address if address
philware 1:8ea78dce6b36 453 * is not NULL. Returns the number of bytes received into the buffer.
philware 1:8ea78dce6b36 454 *
philware 1:8ea78dce6b36 455 * PACKET SIZES: the maximum packet size that can be retrieved in a
philware 1:8ea78dce6b36 456 * single call to this method is limited by the configuration value
philware 1:8ea78dce6b36 457 * platform.buffered-serial-rxbuf-size (default 256). The maximum
philware 1:8ea78dce6b36 458 * UDP packet size is:
philware 1:8ea78dce6b36 459 *
philware 1:8ea78dce6b36 460 * platform.buffered-serial-rxbuf-size - AT_PACKET_OVERHEAD
philware 1:8ea78dce6b36 461 *
philware 1:8ea78dce6b36 462 * ...with a limit of 1024 (at the AT interface). So to allow reception of a
philware 1:8ea78dce6b36 463 * 1024 byte UDP packet in a single call, edit your mbed_app.json to add a
philware 1:8ea78dce6b36 464 * target override setting platform.buffered-serial-rxbuf-size to 1101.
philware 1:8ea78dce6b36 465 *
philware 1:8ea78dce6b36 466 * If the received packet is larger than this limit, any remainder will
philware 1:8ea78dce6b36 467 * be returned in subsequent calls to this method. Once a single UDP
philware 1:8ea78dce6b36 468 * packet has been received, this method will return.
philware 1:8ea78dce6b36 469 *
philware 1:8ea78dce6b36 470 * @param handle Socket handle.
philware 1:8ea78dce6b36 471 * @param address Destination for the source address or NULL.
philware 1:8ea78dce6b36 472 * @param data Destination buffer for data received from the host.
philware 1:8ea78dce6b36 473 * @param size Size of the buffer in bytes.
philware 1:8ea78dce6b36 474 * @return Number of received bytes on success, negative error
philware 1:8ea78dce6b36 475 * code on failure.
philware 1:8ea78dce6b36 476 */
philware 1:8ea78dce6b36 477 virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle,
philware 1:8ea78dce6b36 478 SocketAddress *address,
philware 1:8ea78dce6b36 479 void *data, nsapi_size_t size);
philware 1:8ea78dce6b36 480
philware 1:8ea78dce6b36 481 /** Register a callback on state change of the socket.
philware 1:8ea78dce6b36 482 *
philware 1:8ea78dce6b36 483 * The specified callback will be called on state changes such as when
philware 1:8ea78dce6b36 484 * the socket can recv/send/accept successfully and on when an error
philware 1:8ea78dce6b36 485 * occurs. The callback may also be called spuriously without reason.
philware 1:8ea78dce6b36 486 *
philware 1:8ea78dce6b36 487 * The callback may be called in an interrupt context and should not
philware 1:8ea78dce6b36 488 * perform expensive operations such as recv/send calls.
philware 1:8ea78dce6b36 489 *
philware 1:8ea78dce6b36 490 * @param handle Socket handle.
philware 1:8ea78dce6b36 491 * @param callback Function to call on state change.
philware 1:8ea78dce6b36 492 * @param data Argument to pass to callback.
philware 1:8ea78dce6b36 493 */
philware 1:8ea78dce6b36 494 virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *),
philware 1:8ea78dce6b36 495 void *data);
philware 1:8ea78dce6b36 496
philware 1:8ea78dce6b36 497 /** Listen for connections on a TCP socket.
philware 1:8ea78dce6b36 498 *
philware 1:8ea78dce6b36 499 * Marks the socket as a passive socket that can be used to accept
philware 1:8ea78dce6b36 500 * incoming connections.
philware 1:8ea78dce6b36 501 *
philware 1:8ea78dce6b36 502 * @param handle Socket handle.
philware 1:8ea78dce6b36 503 * @param backlog Number of pending connections that can be queued
philware 1:8ea78dce6b36 504 * simultaneously, defaults to 1.
philware 1:8ea78dce6b36 505 * @return 0 on success, negative error code on failure.
philware 1:8ea78dce6b36 506 */
philware 1:8ea78dce6b36 507 virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog);
philware 1:8ea78dce6b36 508
philware 1:8ea78dce6b36 509 /** Accepts a connection on a TCP socket.
philware 1:8ea78dce6b36 510 *
philware 1:8ea78dce6b36 511 * The server socket must be bound and set to listen for connections.
philware 1:8ea78dce6b36 512 * On a new connection, creates a network socket and stores it in the
philware 1:8ea78dce6b36 513 * specified handle. The handle must be passed to following calls on
philware 1:8ea78dce6b36 514 * the socket.
philware 1:8ea78dce6b36 515 *
philware 1:8ea78dce6b36 516 * A stack may have a finite number of sockets, in this case
philware 1:8ea78dce6b36 517 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
philware 1:8ea78dce6b36 518 *
philware 1:8ea78dce6b36 519 * This call is non-blocking. If accept would block,
philware 1:8ea78dce6b36 520 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
philware 1:8ea78dce6b36 521 *
philware 1:8ea78dce6b36 522 * @param server Socket handle to server to accept from.
philware 1:8ea78dce6b36 523 * @param handle Destination for a handle to the newly created socket.
philware 1:8ea78dce6b36 524 * @param address Destination for the remote address or NULL.
philware 1:8ea78dce6b36 525 * @return 0 on success, negative error code on failure.
philware 1:8ea78dce6b36 526 */
philware 1:8ea78dce6b36 527 virtual nsapi_error_t socket_accept(nsapi_socket_t server,
philware 1:8ea78dce6b36 528 nsapi_socket_t *handle,
philware 1:8ea78dce6b36 529 SocketAddress *address = 0);
philware 1:8ea78dce6b36 530
philware 1:8ea78dce6b36 531 /** Set stack-specific socket options.
philware 1:8ea78dce6b36 532 *
philware 1:8ea78dce6b36 533 * The setsockopt allow an application to pass stack-specific hints
philware 1:8ea78dce6b36 534 * to the underlying stack. For unsupported options,
philware 1:8ea78dce6b36 535 * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
philware 1:8ea78dce6b36 536 *
philware 1:8ea78dce6b36 537 * @param handle Socket handle.
philware 1:8ea78dce6b36 538 * @param level Stack-specific protocol level.
philware 1:8ea78dce6b36 539 * @param optname Stack-specific option identifier.
philware 1:8ea78dce6b36 540 * @param optval Option value.
philware 1:8ea78dce6b36 541 * @param optlen Length of the option value.
philware 1:8ea78dce6b36 542 * @return 0 on success, negative error code on failure.
philware 1:8ea78dce6b36 543 */
philware 1:8ea78dce6b36 544 virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level,
philware 1:8ea78dce6b36 545 int optname, const void *optval,
philware 1:8ea78dce6b36 546 unsigned optlen);
philware 1:8ea78dce6b36 547
philware 1:8ea78dce6b36 548 /** Get stack-specific socket options.
philware 1:8ea78dce6b36 549 *
philware 1:8ea78dce6b36 550 * The getstackopt allow an application to retrieve stack-specific hints
philware 1:8ea78dce6b36 551 * from the underlying stack. For unsupported options,
philware 1:8ea78dce6b36 552 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
philware 1:8ea78dce6b36 553 *
philware 1:8ea78dce6b36 554 * @param handle Socket handle.
philware 1:8ea78dce6b36 555 * @param level Stack-specific protocol level.
philware 1:8ea78dce6b36 556 * @param optname Stack-specific option identifier.
philware 1:8ea78dce6b36 557 * @param optval Destination for option value.
philware 1:8ea78dce6b36 558 * @param optlen Length of the option value.
philware 1:8ea78dce6b36 559 * @return 0 on success, negative error code on failure.
philware 1:8ea78dce6b36 560 */
philware 1:8ea78dce6b36 561 virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level,
philware 1:8ea78dce6b36 562 int optname, void *optval,
philware 1:8ea78dce6b36 563 unsigned *optlen);
philware 1:8ea78dce6b36 564
philware 1:8ea78dce6b36 565 private:
philware 1:8ea78dce6b36 566
philware 1:8ea78dce6b36 567 // u_ added to namespace us somewhat as this darned macro
philware 1:8ea78dce6b36 568 // is defined by everyone and their dog
philware 1:8ea78dce6b36 569 #define u_stringify(a) str(a)
philware 1:8ea78dce6b36 570 #define str(a) #a
philware 1:8ea78dce6b36 571
philware 1:8ea78dce6b36 572 int _localListenPort;
philware 1:8ea78dce6b36 573
philware 1:8ea78dce6b36 574 bool _sim_pin_check_change_pending;
philware 1:8ea78dce6b36 575 bool _sim_pin_check_change_pending_enabled_value;
philware 1:8ea78dce6b36 576 bool _sim_pin_change_pending;
philware 1:8ea78dce6b36 577 const char *_sim_pin_change_pending_new_pin_value;
philware 1:8ea78dce6b36 578 Thread event_thread;
philware 1:8ea78dce6b36 579 void handle_event();
philware 1:8ea78dce6b36 580 SockCtrl * find_socket(int modem_handle = SOCKET_UNUSED);
philware 1:8ea78dce6b36 581 void clear_socket(SockCtrl * socket);
philware 1:8ea78dce6b36 582 bool check_socket(SockCtrl * socket);
philware 1:8ea78dce6b36 583
philware 1:8ea78dce6b36 584 nsapi_size_or_error_t receivefrom(int socketId, SocketAddress *address, int length, char *buf);
philware 1:8ea78dce6b36 585 nsapi_size_or_error_t sendto(SockCtrl *socket, const SocketAddress &address, const char *buf, int size);
philware 1:8ea78dce6b36 586
philware 1:8ea78dce6b36 587 char hex_char(char c);
philware 1:8ea78dce6b36 588 int hex_to_bin(const char* s, char * buff, int length);
philware 1:8ea78dce6b36 589 void bin_to_hex(const char *buff, unsigned int length, char *output);
philware 1:8ea78dce6b36 590
philware 1:8ea78dce6b36 591 Callback<void(nsapi_error_t)> _connection_status_cb;
philware 1:8ea78dce6b36 592 void NSONMI_URC();
philware 1:8ea78dce6b36 593 };
philware 1:8ea78dce6b36 594
philware 1:8ea78dce6b36 595 #endif // _UBLOX_AT_CELLULAR_INTERFACE_
philware 1:8ea78dce6b36 596