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
C027Interface.h@23:5e320842ccad, 2017-04-20 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Apr 20 09:04:44 2017 -0500
- Revision:
- 23:5e320842ccad
- Parent:
- 22:69ea34ad7fc6
Only join a running threat. Zero initialise callback array.
Who changed what in which revision?
User | Revision | Line number | New 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 | |
mbed_official | 21:0c4d90bc84af | 23 | #define NUMSOCKETS 12 |
geky | 12:181a9415736b | 24 | |
geky | 12:181a9415736b | 25 | /** C027Interface class |
geky | 12:181a9415736b | 26 | * Implementation of the NetworkInterface for C027 |
geky | 12:181a9415736b | 27 | */ |
Christopher Haster |
14:3d1845f5cd81 | 28 | class C027Interface : public NetworkStack, public CellularInterface |
geky | 12:181a9415736b | 29 | { |
geky | 12:181a9415736b | 30 | public: |
Christopher Haster |
14:3d1845f5cd81 | 31 | /** C027Interfacelifetime |
Christopher Haster |
14:3d1845f5cd81 | 32 | * @param simpin Optional PIN for the SIM |
Christopher Haster |
14:3d1845f5cd81 | 33 | * @param debug Enable debugging |
Christopher Haster |
14:3d1845f5cd81 | 34 | */ |
geky | 12:181a9415736b | 35 | C027Interface(const char *simpin=0, bool debug=false); |
mbed_official | 21:0c4d90bc84af | 36 | |
mbed_official | 21:0c4d90bc84af | 37 | ~C027Interface(); |
sarahmarshy | 17:3f1e33e638a4 | 38 | /** Set the cellular network APN and credentials |
sarahmarshy | 17:3f1e33e638a4 | 39 | * |
sarahmarshy | 17:3f1e33e638a4 | 40 | * @param apn Optional name of the network to connect to |
sarahmarshy | 17:3f1e33e638a4 | 41 | * @param user Optional username for the APN |
sarahmarshy | 17:3f1e33e638a4 | 42 | * @param pass Optional password fot the APN |
sarahmarshy | 17:3f1e33e638a4 | 43 | * @return 0 on success, negative error code on failure |
sarahmarshy | 17:3f1e33e638a4 | 44 | */ |
sarahmarshy | 17:3f1e33e638a4 | 45 | virtual nsapi_error_t set_credentials(const char *apn, const char *username = 0, const char *password = 0); |
sarahmarshy | 17:3f1e33e638a4 | 46 | |
sarahmarshy | 17:3f1e33e638a4 | 47 | /** Start the interface |
sarahmarshy | 17:3f1e33e638a4 | 48 | * |
sarahmarshy | 17:3f1e33e638a4 | 49 | * Attempts to connect to a cellular network based on supplied credentials |
sarahmarshy | 17:3f1e33e638a4 | 50 | * |
sarahmarshy | 17:3f1e33e638a4 | 51 | * @return 0 on success, negative error code on failure |
sarahmarshy | 17:3f1e33e638a4 | 52 | */ |
sarahmarshy | 17:3f1e33e638a4 | 53 | virtual nsapi_error_t connect(); |
geky | 12:181a9415736b | 54 | |
Christopher Haster |
14:3d1845f5cd81 | 55 | /** Start the interface |
Christopher Haster |
14:3d1845f5cd81 | 56 | * |
Christopher Haster |
14:3d1845f5cd81 | 57 | * @param apn Optional name of the network to connect to |
Christopher Haster |
14:3d1845f5cd81 | 58 | * @param username Optional username for your APN |
Christopher Haster |
14:3d1845f5cd81 | 59 | * @param password Optional password for your APN |
Christopher Haster |
14:3d1845f5cd81 | 60 | * @return 0 on success, negative error code on failure |
Christopher Haster |
14:3d1845f5cd81 | 61 | */ |
sarahmarshy | 17:3f1e33e638a4 | 62 | virtual nsapi_error_t connect(const char *apn, const char *username = 0, const char *password = 0); |
Christopher Haster |
14:3d1845f5cd81 | 63 | |
Christopher Haster |
14:3d1845f5cd81 | 64 | /** Stop the interface |
Christopher Haster |
14:3d1845f5cd81 | 65 | * @return 0 on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 66 | */ |
Christopher Haster |
14:3d1845f5cd81 | 67 | virtual int disconnect(); |
Christopher Haster |
14:3d1845f5cd81 | 68 | |
Christopher Haster |
14:3d1845f5cd81 | 69 | /** Get the internally stored IP address |
Christopher Haster |
14:3d1845f5cd81 | 70 | * @return IP address of the interface or null if not yet connected |
Christopher Haster |
14:3d1845f5cd81 | 71 | */ |
Christopher Haster |
14:3d1845f5cd81 | 72 | virtual const char *get_ip_address(); |
Christopher Haster |
14:3d1845f5cd81 | 73 | |
Christopher Haster |
14:3d1845f5cd81 | 74 | /** Get the internally stored MAC address |
Christopher Haster |
14:3d1845f5cd81 | 75 | * @return MAC address of the interface |
Christopher Haster |
14:3d1845f5cd81 | 76 | */ |
Christopher Haster |
14:3d1845f5cd81 | 77 | virtual const char *get_mac_address(); |
Christopher Haster |
14:3d1845f5cd81 | 78 | |
Christopher Haster |
14:3d1845f5cd81 | 79 | protected: |
Christopher Haster |
14:3d1845f5cd81 | 80 | /** Open a socket |
Christopher Haster |
14:3d1845f5cd81 | 81 | * @param handle Handle in which to store new socket |
Christopher Haster |
14:3d1845f5cd81 | 82 | * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP |
Christopher Haster |
14:3d1845f5cd81 | 83 | * @return 0 on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 84 | */ |
Christopher Haster |
14:3d1845f5cd81 | 85 | virtual int socket_open(void **handle, nsapi_protocol_t proto); |
Christopher Haster |
14:3d1845f5cd81 | 86 | |
Christopher Haster |
14:3d1845f5cd81 | 87 | /** Close the socket |
Christopher Haster |
14:3d1845f5cd81 | 88 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 89 | * @return 0 on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 90 | * @note On failure, any memory associated with the socket must still |
Christopher Haster |
14:3d1845f5cd81 | 91 | * be cleaned up |
Christopher Haster |
14:3d1845f5cd81 | 92 | */ |
Christopher Haster |
14:3d1845f5cd81 | 93 | virtual int socket_close(void *handle); |
Christopher Haster |
14:3d1845f5cd81 | 94 | |
Christopher Haster |
14:3d1845f5cd81 | 95 | /** Bind a server socket to a specific port |
Christopher Haster |
14:3d1845f5cd81 | 96 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 97 | * @param address Local address to listen for incoming connections on |
Christopher Haster |
14:3d1845f5cd81 | 98 | * @return 0 on success, negative on failure. |
Christopher Haster |
14:3d1845f5cd81 | 99 | */ |
Christopher Haster |
14:3d1845f5cd81 | 100 | virtual int socket_bind(void *handle, const SocketAddress &address); |
Christopher Haster |
14:3d1845f5cd81 | 101 | |
Christopher Haster |
14:3d1845f5cd81 | 102 | /** Start listening for incoming connections |
Christopher Haster |
14:3d1845f5cd81 | 103 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 104 | * @param backlog Number of pending connections that can be queued up at any |
Christopher Haster |
14:3d1845f5cd81 | 105 | * one time [Default: 1] |
Christopher Haster |
14:3d1845f5cd81 | 106 | * @return 0 on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 107 | */ |
Christopher Haster |
14:3d1845f5cd81 | 108 | virtual int socket_listen(void *handle, int backlog); |
geky | 12:181a9415736b | 109 | |
Christopher Haster |
14:3d1845f5cd81 | 110 | /** Connects this TCP socket to the server |
Christopher Haster |
14:3d1845f5cd81 | 111 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 112 | * @param address SocketAddress to connect to |
Christopher Haster |
14:3d1845f5cd81 | 113 | * @return 0 on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 114 | */ |
Christopher Haster |
14:3d1845f5cd81 | 115 | virtual int socket_connect(void *handle, const SocketAddress &address); |
Christopher Haster |
14:3d1845f5cd81 | 116 | |
Christopher Haster |
14:3d1845f5cd81 | 117 | /** Accept a new connection. |
Christopher Haster |
14:3d1845f5cd81 | 118 | * @param handle Handle in which to store new socket |
Christopher Haster |
14:3d1845f5cd81 | 119 | * @param server Socket handle to server to accept from |
Christopher Haster |
14:3d1845f5cd81 | 120 | * @return 0 on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 121 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
14:3d1845f5cd81 | 122 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
14:3d1845f5cd81 | 123 | */ |
mbed_official | 21:0c4d90bc84af | 124 | virtual nsapi_error_t socket_accept(nsapi_socket_t server, |
mbed_official | 21:0c4d90bc84af | 125 | nsapi_socket_t *handle, SocketAddress *address=0); |
Christopher Haster |
14:3d1845f5cd81 | 126 | /** Send data to the remote host |
Christopher Haster |
14:3d1845f5cd81 | 127 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 128 | * @param data The buffer to send to the host |
Christopher Haster |
14:3d1845f5cd81 | 129 | * @param size The length of the buffer to send |
Christopher Haster |
14:3d1845f5cd81 | 130 | * @return Number of written bytes on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 131 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
14:3d1845f5cd81 | 132 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
14:3d1845f5cd81 | 133 | */ |
Christopher Haster |
14:3d1845f5cd81 | 134 | virtual int socket_send(void *handle, const void *data, unsigned size); |
geky | 12:181a9415736b | 135 | |
Christopher Haster |
14:3d1845f5cd81 | 136 | /** Receive data from the remote host |
Christopher Haster |
14:3d1845f5cd81 | 137 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 138 | * @param data The buffer in which to store the data received from the host |
Christopher Haster |
14:3d1845f5cd81 | 139 | * @param size The maximum length of the buffer |
Christopher Haster |
14:3d1845f5cd81 | 140 | * @return Number of received bytes on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 141 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
14:3d1845f5cd81 | 142 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
14:3d1845f5cd81 | 143 | */ |
Christopher Haster |
14:3d1845f5cd81 | 144 | virtual int socket_recv(void *handle, void *data, unsigned size); |
Christopher Haster |
14:3d1845f5cd81 | 145 | |
Christopher Haster |
14:3d1845f5cd81 | 146 | /** Send a packet to a remote endpoint |
Christopher Haster |
14:3d1845f5cd81 | 147 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 148 | * @param address The remote SocketAddress |
Christopher Haster |
14:3d1845f5cd81 | 149 | * @param data The packet to be sent |
Christopher Haster |
14:3d1845f5cd81 | 150 | * @param size The length of the packet to be sent |
Christopher Haster |
14:3d1845f5cd81 | 151 | * @return the number of written bytes on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 152 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
14:3d1845f5cd81 | 153 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
14:3d1845f5cd81 | 154 | */ |
Christopher Haster |
14:3d1845f5cd81 | 155 | virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); |
Christopher Haster |
14:3d1845f5cd81 | 156 | |
Christopher Haster |
14:3d1845f5cd81 | 157 | /** Receive a packet from a remote endpoint |
Christopher Haster |
14:3d1845f5cd81 | 158 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 159 | * @param address Destination for the remote SocketAddress or null |
Christopher Haster |
14:3d1845f5cd81 | 160 | * @param buffer The buffer for storing the incoming packet data |
Christopher Haster |
14:3d1845f5cd81 | 161 | * If a packet is too long to fit in the supplied buffer, |
Christopher Haster |
14:3d1845f5cd81 | 162 | * excess bytes are discarded |
Christopher Haster |
14:3d1845f5cd81 | 163 | * @param size The length of the buffer |
Christopher Haster |
14:3d1845f5cd81 | 164 | * @return the number of received bytes on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 165 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
14:3d1845f5cd81 | 166 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
14:3d1845f5cd81 | 167 | */ |
Christopher Haster |
14:3d1845f5cd81 | 168 | virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); |
Christopher Haster |
14:3d1845f5cd81 | 169 | |
Christopher Haster |
14:3d1845f5cd81 | 170 | /** Register a callback on state change of the socket |
Christopher Haster |
14:3d1845f5cd81 | 171 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 172 | * @param callback Function to call on state change |
Christopher Haster |
14:3d1845f5cd81 | 173 | * @param data Argument to pass to callback |
Christopher Haster |
14:3d1845f5cd81 | 174 | * @note Callback may be called in an interrupt context. |
Christopher Haster |
14:3d1845f5cd81 | 175 | */ |
Christopher Haster |
14:3d1845f5cd81 | 176 | virtual void socket_attach(void *handle, void (*callback)(void *), void *data); |
sarahmarshy | 16:eaf31bd133f0 | 177 | |
sarahmarshy | 16:eaf31bd133f0 | 178 | /** Provide access to the NetworkStack object |
sarahmarshy | 16:eaf31bd133f0 | 179 | * |
sarahmarshy | 16:eaf31bd133f0 | 180 | * @return The underlying NetworkStack object |
sarahmarshy | 16:eaf31bd133f0 | 181 | */ |
sarahmarshy | 16:eaf31bd133f0 | 182 | virtual NetworkStack *get_stack() |
sarahmarshy | 16:eaf31bd133f0 | 183 | { |
sarahmarshy | 16:eaf31bd133f0 | 184 | return this; |
sarahmarshy | 16:eaf31bd133f0 | 185 | } |
geky | 12:181a9415736b | 186 | |
geky | 12:181a9415736b | 187 | private: |
geky | 12:181a9415736b | 188 | // Modem object |
geky | 12:181a9415736b | 189 | bool _debug; |
geky | 12:181a9415736b | 190 | MDMSerial *_mdm; |
sarahmarshy | 20:be7a08046b6d | 191 | bool running; |
geky | 15:21d4f56eb172 | 192 | SocketAddress _ip_address; |
Christopher Haster |
14:3d1845f5cd81 | 193 | char _mac_address[NSAPI_MAC_SIZE]; |
geky | 12:181a9415736b | 194 | char _pin[sizeof "1234"]; |
sarahmarshy | 17:3f1e33e638a4 | 195 | char _apn[50]; |
sarahmarshy | 17:3f1e33e638a4 | 196 | char _username[25]; |
sarahmarshy | 17:3f1e33e638a4 | 197 | char _password[25]; |
mbed_official | 22:69ea34ad7fc6 | 198 | Mutex m; |
sarahmarshy | 16:eaf31bd133f0 | 199 | Thread _thread; |
mbed_official | 21:0c4d90bc84af | 200 | struct { |
mbed_official | 21:0c4d90bc84af | 201 | void (*callback)(void *); |
mbed_official | 21:0c4d90bc84af | 202 | void *data; |
mbed_official | 21:0c4d90bc84af | 203 | } _cbs[NUMSOCKETS]; // Callbacks for socket_attach |
sarahmarshy | 16:eaf31bd133f0 | 204 | void (*_callback)(void *); void *_data; |
sarahmarshy | 20:be7a08046b6d | 205 | void *_handle; |
sarahmarshy | 20:be7a08046b6d | 206 | void socket_check(); |
sarahmarshy | 20:be7a08046b6d | 207 | void event(); |
geky | 12:181a9415736b | 208 | }; |
geky | 12:181a9415736b | 209 | |
geky | 12:181a9415736b | 210 | #endif |