Socket interface for C027Interface. Implements the NetworkSocketAPI

Dependencies:   C027_Support

Dependents:   HelloC027Interface U_Blox_DeviceConnector U_Blox_DeviceConnector U-Blox_Client

Fork of LWIPInterface by NetworkSocketAPI

Committer:
sarahmarshy
Date:
Thu Feb 02 21:16:38 2017 +0000
Revision:
17:3f1e33e638a4
Parent:
16:eaf31bd133f0
Child:
20:be7a08046b6d
Update to be compatible CellularInterface changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
geky 12:181a9415736b 1 /* C027 implementation of NetworkInterfaceAPI
geky 12:181a9415736b 2 * Copyright (c) 2015 ARM Limited
geky 12:181a9415736b 3 *
geky 12:181a9415736b 4 * Licensed under the Apache License, Version 2.0 (the "License");
geky 12:181a9415736b 5 * you may not use this file except in compliance with the License.
geky 12:181a9415736b 6 * You may obtain a copy of the License at
geky 12:181a9415736b 7 *
geky 12:181a9415736b 8 * http://www.apache.org/licenses/LICENSE-2.0
geky 12:181a9415736b 9 *
geky 12:181a9415736b 10 * Unless required by applicable law or agreed to in writing, software
geky 12:181a9415736b 11 * distributed under the License is distributed on an "AS IS" BASIS,
geky 12:181a9415736b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
geky 12:181a9415736b 13 * See the License for the specific language governing permissions and
geky 12:181a9415736b 14 * limitations under the License.
geky 12:181a9415736b 15 */
geky 12:181a9415736b 16
geky 12:181a9415736b 17 #ifndef C027_INTERFACE_H
geky 12:181a9415736b 18 #define C027_INTERFACE_H
geky 12:181a9415736b 19
geky 12:181a9415736b 20 #include "CellularInterface.h"
geky 12:181a9415736b 21 #include "MDM.h"
geky 12:181a9415736b 22
geky 12:181a9415736b 23
geky 12:181a9415736b 24 /** C027Interface class
geky 12:181a9415736b 25 * Implementation of the NetworkInterface for C027
geky 12:181a9415736b 26 */
Christopher Haster 14:3d1845f5cd81 27 class C027Interface : public NetworkStack, public CellularInterface
geky 12:181a9415736b 28 {
geky 12:181a9415736b 29 public:
Christopher Haster 14:3d1845f5cd81 30 /** C027Interfacelifetime
Christopher Haster 14:3d1845f5cd81 31 * @param simpin Optional PIN for the SIM
Christopher Haster 14:3d1845f5cd81 32 * @param debug Enable debugging
Christopher Haster 14:3d1845f5cd81 33 */
geky 12:181a9415736b 34 C027Interface(const char *simpin=0, bool debug=false);
sarahmarshy 17:3f1e33e638a4 35
sarahmarshy 17:3f1e33e638a4 36 /** Set the cellular network APN and credentials
sarahmarshy 17:3f1e33e638a4 37 *
sarahmarshy 17:3f1e33e638a4 38 * @param apn Optional name of the network to connect to
sarahmarshy 17:3f1e33e638a4 39 * @param user Optional username for the APN
sarahmarshy 17:3f1e33e638a4 40 * @param pass Optional password fot the APN
sarahmarshy 17:3f1e33e638a4 41 * @return 0 on success, negative error code on failure
sarahmarshy 17:3f1e33e638a4 42 */
sarahmarshy 17:3f1e33e638a4 43 virtual nsapi_error_t set_credentials(const char *apn, const char *username = 0, const char *password = 0);
sarahmarshy 17:3f1e33e638a4 44
sarahmarshy 17:3f1e33e638a4 45 /** Start the interface
sarahmarshy 17:3f1e33e638a4 46 *
sarahmarshy 17:3f1e33e638a4 47 * Attempts to connect to a cellular network based on supplied credentials
sarahmarshy 17:3f1e33e638a4 48 *
sarahmarshy 17:3f1e33e638a4 49 * @return 0 on success, negative error code on failure
sarahmarshy 17:3f1e33e638a4 50 */
sarahmarshy 17:3f1e33e638a4 51 virtual nsapi_error_t connect();
geky 12:181a9415736b 52
Christopher Haster 14:3d1845f5cd81 53 /** Start the interface
Christopher Haster 14:3d1845f5cd81 54 *
Christopher Haster 14:3d1845f5cd81 55 * @param apn Optional name of the network to connect to
Christopher Haster 14:3d1845f5cd81 56 * @param username Optional username for your APN
Christopher Haster 14:3d1845f5cd81 57 * @param password Optional password for your APN
Christopher Haster 14:3d1845f5cd81 58 * @return 0 on success, negative error code on failure
Christopher Haster 14:3d1845f5cd81 59 */
sarahmarshy 17:3f1e33e638a4 60 virtual nsapi_error_t connect(const char *apn, const char *username = 0, const char *password = 0);
Christopher Haster 14:3d1845f5cd81 61
Christopher Haster 14:3d1845f5cd81 62 /** Stop the interface
Christopher Haster 14:3d1845f5cd81 63 * @return 0 on success, negative on failure
Christopher Haster 14:3d1845f5cd81 64 */
Christopher Haster 14:3d1845f5cd81 65 virtual int disconnect();
Christopher Haster 14:3d1845f5cd81 66
Christopher Haster 14:3d1845f5cd81 67 /** Get the internally stored IP address
Christopher Haster 14:3d1845f5cd81 68 * @return IP address of the interface or null if not yet connected
Christopher Haster 14:3d1845f5cd81 69 */
Christopher Haster 14:3d1845f5cd81 70 virtual const char *get_ip_address();
Christopher Haster 14:3d1845f5cd81 71
Christopher Haster 14:3d1845f5cd81 72 /** Get the internally stored MAC address
Christopher Haster 14:3d1845f5cd81 73 * @return MAC address of the interface
Christopher Haster 14:3d1845f5cd81 74 */
Christopher Haster 14:3d1845f5cd81 75 virtual const char *get_mac_address();
Christopher Haster 14:3d1845f5cd81 76
Christopher Haster 14:3d1845f5cd81 77 protected:
Christopher Haster 14:3d1845f5cd81 78 /** Open a socket
Christopher Haster 14:3d1845f5cd81 79 * @param handle Handle in which to store new socket
Christopher Haster 14:3d1845f5cd81 80 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
Christopher Haster 14:3d1845f5cd81 81 * @return 0 on success, negative on failure
Christopher Haster 14:3d1845f5cd81 82 */
Christopher Haster 14:3d1845f5cd81 83 virtual int socket_open(void **handle, nsapi_protocol_t proto);
Christopher Haster 14:3d1845f5cd81 84
Christopher Haster 14:3d1845f5cd81 85 /** Close the socket
Christopher Haster 14:3d1845f5cd81 86 * @param handle Socket handle
Christopher Haster 14:3d1845f5cd81 87 * @return 0 on success, negative on failure
Christopher Haster 14:3d1845f5cd81 88 * @note On failure, any memory associated with the socket must still
Christopher Haster 14:3d1845f5cd81 89 * be cleaned up
Christopher Haster 14:3d1845f5cd81 90 */
Christopher Haster 14:3d1845f5cd81 91 virtual int socket_close(void *handle);
Christopher Haster 14:3d1845f5cd81 92
Christopher Haster 14:3d1845f5cd81 93 /** Bind a server socket to a specific port
Christopher Haster 14:3d1845f5cd81 94 * @param handle Socket handle
Christopher Haster 14:3d1845f5cd81 95 * @param address Local address to listen for incoming connections on
Christopher Haster 14:3d1845f5cd81 96 * @return 0 on success, negative on failure.
Christopher Haster 14:3d1845f5cd81 97 */
Christopher Haster 14:3d1845f5cd81 98 virtual int socket_bind(void *handle, const SocketAddress &address);
Christopher Haster 14:3d1845f5cd81 99
Christopher Haster 14:3d1845f5cd81 100 /** Start listening for incoming connections
Christopher Haster 14:3d1845f5cd81 101 * @param handle Socket handle
Christopher Haster 14:3d1845f5cd81 102 * @param backlog Number of pending connections that can be queued up at any
Christopher Haster 14:3d1845f5cd81 103 * one time [Default: 1]
Christopher Haster 14:3d1845f5cd81 104 * @return 0 on success, negative on failure
Christopher Haster 14:3d1845f5cd81 105 */
Christopher Haster 14:3d1845f5cd81 106 virtual int socket_listen(void *handle, int backlog);
geky 12:181a9415736b 107
Christopher Haster 14:3d1845f5cd81 108 /** Connects this TCP socket to the server
Christopher Haster 14:3d1845f5cd81 109 * @param handle Socket handle
Christopher Haster 14:3d1845f5cd81 110 * @param address SocketAddress to connect to
Christopher Haster 14:3d1845f5cd81 111 * @return 0 on success, negative on failure
Christopher Haster 14:3d1845f5cd81 112 */
Christopher Haster 14:3d1845f5cd81 113 virtual int socket_connect(void *handle, const SocketAddress &address);
Christopher Haster 14:3d1845f5cd81 114
Christopher Haster 14:3d1845f5cd81 115 /** Accept a new connection.
Christopher Haster 14:3d1845f5cd81 116 * @param handle Handle in which to store new socket
Christopher Haster 14:3d1845f5cd81 117 * @param server Socket handle to server to accept from
Christopher Haster 14:3d1845f5cd81 118 * @return 0 on success, negative on failure
Christopher Haster 14:3d1845f5cd81 119 * @note This call is not-blocking, if this call would block, must
Christopher Haster 14:3d1845f5cd81 120 * immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 14:3d1845f5cd81 121 */
sarahmarshy 17:3f1e33e638a4 122 virtual nsapi_error_t socket_accept(nsapi_socket_t server,
sarahmarshy 17:3f1e33e638a4 123 nsapi_socket_t *handle, SocketAddress *address=0);
Christopher Haster 14:3d1845f5cd81 124 /** Send data to the remote host
Christopher Haster 14:3d1845f5cd81 125 * @param handle Socket handle
Christopher Haster 14:3d1845f5cd81 126 * @param data The buffer to send to the host
Christopher Haster 14:3d1845f5cd81 127 * @param size The length of the buffer to send
Christopher Haster 14:3d1845f5cd81 128 * @return Number of written bytes on success, negative on failure
Christopher Haster 14:3d1845f5cd81 129 * @note This call is not-blocking, if this call would block, must
Christopher Haster 14:3d1845f5cd81 130 * immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 14:3d1845f5cd81 131 */
Christopher Haster 14:3d1845f5cd81 132 virtual int socket_send(void *handle, const void *data, unsigned size);
geky 12:181a9415736b 133
Christopher Haster 14:3d1845f5cd81 134 /** Receive data from the remote host
Christopher Haster 14:3d1845f5cd81 135 * @param handle Socket handle
Christopher Haster 14:3d1845f5cd81 136 * @param data The buffer in which to store the data received from the host
Christopher Haster 14:3d1845f5cd81 137 * @param size The maximum length of the buffer
Christopher Haster 14:3d1845f5cd81 138 * @return Number of received bytes on success, negative on failure
Christopher Haster 14:3d1845f5cd81 139 * @note This call is not-blocking, if this call would block, must
Christopher Haster 14:3d1845f5cd81 140 * immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 14:3d1845f5cd81 141 */
Christopher Haster 14:3d1845f5cd81 142 virtual int socket_recv(void *handle, void *data, unsigned size);
Christopher Haster 14:3d1845f5cd81 143
Christopher Haster 14:3d1845f5cd81 144 /** Send a packet to a remote endpoint
Christopher Haster 14:3d1845f5cd81 145 * @param handle Socket handle
Christopher Haster 14:3d1845f5cd81 146 * @param address The remote SocketAddress
Christopher Haster 14:3d1845f5cd81 147 * @param data The packet to be sent
Christopher Haster 14:3d1845f5cd81 148 * @param size The length of the packet to be sent
Christopher Haster 14:3d1845f5cd81 149 * @return the number of written bytes on success, negative on failure
Christopher Haster 14:3d1845f5cd81 150 * @note This call is not-blocking, if this call would block, must
Christopher Haster 14:3d1845f5cd81 151 * immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 14:3d1845f5cd81 152 */
Christopher Haster 14:3d1845f5cd81 153 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
Christopher Haster 14:3d1845f5cd81 154
Christopher Haster 14:3d1845f5cd81 155 /** Receive a packet from a remote endpoint
Christopher Haster 14:3d1845f5cd81 156 * @param handle Socket handle
Christopher Haster 14:3d1845f5cd81 157 * @param address Destination for the remote SocketAddress or null
Christopher Haster 14:3d1845f5cd81 158 * @param buffer The buffer for storing the incoming packet data
Christopher Haster 14:3d1845f5cd81 159 * If a packet is too long to fit in the supplied buffer,
Christopher Haster 14:3d1845f5cd81 160 * excess bytes are discarded
Christopher Haster 14:3d1845f5cd81 161 * @param size The length of the buffer
Christopher Haster 14:3d1845f5cd81 162 * @return the number of received bytes on success, negative on failure
Christopher Haster 14:3d1845f5cd81 163 * @note This call is not-blocking, if this call would block, must
Christopher Haster 14:3d1845f5cd81 164 * immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 14:3d1845f5cd81 165 */
Christopher Haster 14:3d1845f5cd81 166 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
Christopher Haster 14:3d1845f5cd81 167
Christopher Haster 14:3d1845f5cd81 168 /** Register a callback on state change of the socket
Christopher Haster 14:3d1845f5cd81 169 * @param handle Socket handle
Christopher Haster 14:3d1845f5cd81 170 * @param callback Function to call on state change
Christopher Haster 14:3d1845f5cd81 171 * @param data Argument to pass to callback
Christopher Haster 14:3d1845f5cd81 172 * @note Callback may be called in an interrupt context.
Christopher Haster 14:3d1845f5cd81 173 */
Christopher Haster 14:3d1845f5cd81 174 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
sarahmarshy 16:eaf31bd133f0 175
sarahmarshy 16:eaf31bd133f0 176 /** Provide access to the NetworkStack object
sarahmarshy 16:eaf31bd133f0 177 *
sarahmarshy 16:eaf31bd133f0 178 * @return The underlying NetworkStack object
sarahmarshy 16:eaf31bd133f0 179 */
sarahmarshy 16:eaf31bd133f0 180 virtual NetworkStack *get_stack()
sarahmarshy 16:eaf31bd133f0 181 {
sarahmarshy 16:eaf31bd133f0 182 return this;
sarahmarshy 16:eaf31bd133f0 183 }
geky 12:181a9415736b 184
geky 12:181a9415736b 185 private:
geky 12:181a9415736b 186 // Modem object
geky 12:181a9415736b 187 bool _debug;
geky 12:181a9415736b 188 MDMSerial *_mdm;
geky 15:21d4f56eb172 189 SocketAddress _ip_address;
Christopher Haster 14:3d1845f5cd81 190 char _mac_address[NSAPI_MAC_SIZE];
geky 12:181a9415736b 191 char _pin[sizeof "1234"];
sarahmarshy 17:3f1e33e638a4 192 char _apn[50];
sarahmarshy 17:3f1e33e638a4 193 char _username[25];
sarahmarshy 17:3f1e33e638a4 194 char _password[25];
sarahmarshy 16:eaf31bd133f0 195 Thread _thread;
sarahmarshy 16:eaf31bd133f0 196 void (*_callback)(void *); void *_data;
sarahmarshy 16:eaf31bd133f0 197 void _run();
geky 12:181a9415736b 198 };
geky 12:181a9415736b 199
geky 12:181a9415736b 200 #endif