C027Interface
Fork of C027Interface by
C027Interface.h@16:eaf31bd133f0, 2017-02-02 (annotated)
- Committer:
- sarahmarshy
- Date:
- Thu Feb 02 16:17:35 2017 +0000
- Revision:
- 16:eaf31bd133f0
- Parent:
- 15:21d4f56eb172
socket_attach and get_stack functions implemented
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 | |
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); |
geky | 12:181a9415736b | 35 | |
Christopher Haster |
14:3d1845f5cd81 | 36 | /** Start the interface |
Christopher Haster |
14:3d1845f5cd81 | 37 | * |
Christopher Haster |
14:3d1845f5cd81 | 38 | * @param apn Optional name of the network to connect to |
Christopher Haster |
14:3d1845f5cd81 | 39 | * @param username Optional username for your APN |
Christopher Haster |
14:3d1845f5cd81 | 40 | * @param password Optional password for your APN |
Christopher Haster |
14:3d1845f5cd81 | 41 | * @return 0 on success, negative error code on failure |
Christopher Haster |
14:3d1845f5cd81 | 42 | */ |
Christopher Haster |
14:3d1845f5cd81 | 43 | virtual int connect(const char *apn = 0, const char *username = 0, const char *password = 0); |
Christopher Haster |
14:3d1845f5cd81 | 44 | |
Christopher Haster |
14:3d1845f5cd81 | 45 | /** Stop the interface |
Christopher Haster |
14:3d1845f5cd81 | 46 | * @return 0 on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 47 | */ |
Christopher Haster |
14:3d1845f5cd81 | 48 | virtual int disconnect(); |
Christopher Haster |
14:3d1845f5cd81 | 49 | |
Christopher Haster |
14:3d1845f5cd81 | 50 | /** Get the internally stored IP address |
Christopher Haster |
14:3d1845f5cd81 | 51 | * @return IP address of the interface or null if not yet connected |
Christopher Haster |
14:3d1845f5cd81 | 52 | */ |
Christopher Haster |
14:3d1845f5cd81 | 53 | virtual const char *get_ip_address(); |
Christopher Haster |
14:3d1845f5cd81 | 54 | |
Christopher Haster |
14:3d1845f5cd81 | 55 | /** Get the internally stored MAC address |
Christopher Haster |
14:3d1845f5cd81 | 56 | * @return MAC address of the interface |
Christopher Haster |
14:3d1845f5cd81 | 57 | */ |
Christopher Haster |
14:3d1845f5cd81 | 58 | virtual const char *get_mac_address(); |
Christopher Haster |
14:3d1845f5cd81 | 59 | |
Christopher Haster |
14:3d1845f5cd81 | 60 | protected: |
Christopher Haster |
14:3d1845f5cd81 | 61 | /** Open a socket |
Christopher Haster |
14:3d1845f5cd81 | 62 | * @param handle Handle in which to store new socket |
Christopher Haster |
14:3d1845f5cd81 | 63 | * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP |
Christopher Haster |
14:3d1845f5cd81 | 64 | * @return 0 on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 65 | */ |
Christopher Haster |
14:3d1845f5cd81 | 66 | virtual int socket_open(void **handle, nsapi_protocol_t proto); |
Christopher Haster |
14:3d1845f5cd81 | 67 | |
Christopher Haster |
14:3d1845f5cd81 | 68 | /** Close the socket |
Christopher Haster |
14:3d1845f5cd81 | 69 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 70 | * @return 0 on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 71 | * @note On failure, any memory associated with the socket must still |
Christopher Haster |
14:3d1845f5cd81 | 72 | * be cleaned up |
Christopher Haster |
14:3d1845f5cd81 | 73 | */ |
Christopher Haster |
14:3d1845f5cd81 | 74 | virtual int socket_close(void *handle); |
Christopher Haster |
14:3d1845f5cd81 | 75 | |
Christopher Haster |
14:3d1845f5cd81 | 76 | /** Bind a server socket to a specific port |
Christopher Haster |
14:3d1845f5cd81 | 77 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 78 | * @param address Local address to listen for incoming connections on |
Christopher Haster |
14:3d1845f5cd81 | 79 | * @return 0 on success, negative on failure. |
Christopher Haster |
14:3d1845f5cd81 | 80 | */ |
Christopher Haster |
14:3d1845f5cd81 | 81 | virtual int socket_bind(void *handle, const SocketAddress &address); |
Christopher Haster |
14:3d1845f5cd81 | 82 | |
Christopher Haster |
14:3d1845f5cd81 | 83 | /** Start listening for incoming connections |
Christopher Haster |
14:3d1845f5cd81 | 84 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 85 | * @param backlog Number of pending connections that can be queued up at any |
Christopher Haster |
14:3d1845f5cd81 | 86 | * one time [Default: 1] |
Christopher Haster |
14:3d1845f5cd81 | 87 | * @return 0 on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 88 | */ |
Christopher Haster |
14:3d1845f5cd81 | 89 | virtual int socket_listen(void *handle, int backlog); |
geky | 12:181a9415736b | 90 | |
Christopher Haster |
14:3d1845f5cd81 | 91 | /** Connects this TCP socket to the server |
Christopher Haster |
14:3d1845f5cd81 | 92 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 93 | * @param address SocketAddress to connect to |
Christopher Haster |
14:3d1845f5cd81 | 94 | * @return 0 on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 95 | */ |
Christopher Haster |
14:3d1845f5cd81 | 96 | virtual int socket_connect(void *handle, const SocketAddress &address); |
Christopher Haster |
14:3d1845f5cd81 | 97 | |
Christopher Haster |
14:3d1845f5cd81 | 98 | /** Accept a new connection. |
Christopher Haster |
14:3d1845f5cd81 | 99 | * @param handle Handle in which to store new socket |
Christopher Haster |
14:3d1845f5cd81 | 100 | * @param server Socket handle to server to accept from |
Christopher Haster |
14:3d1845f5cd81 | 101 | * @return 0 on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 102 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
14:3d1845f5cd81 | 103 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
14:3d1845f5cd81 | 104 | */ |
Christopher Haster |
14:3d1845f5cd81 | 105 | virtual int socket_accept(void **handle, void *server); |
Christopher Haster |
14:3d1845f5cd81 | 106 | |
Christopher Haster |
14:3d1845f5cd81 | 107 | /** Send data to the remote host |
Christopher Haster |
14:3d1845f5cd81 | 108 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 109 | * @param data The buffer to send to the host |
Christopher Haster |
14:3d1845f5cd81 | 110 | * @param size The length of the buffer to send |
Christopher Haster |
14:3d1845f5cd81 | 111 | * @return Number of written bytes on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 112 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
14:3d1845f5cd81 | 113 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
14:3d1845f5cd81 | 114 | */ |
Christopher Haster |
14:3d1845f5cd81 | 115 | virtual int socket_send(void *handle, const void *data, unsigned size); |
geky | 12:181a9415736b | 116 | |
Christopher Haster |
14:3d1845f5cd81 | 117 | /** Receive data from the remote host |
Christopher Haster |
14:3d1845f5cd81 | 118 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 119 | * @param data The buffer in which to store the data received from the host |
Christopher Haster |
14:3d1845f5cd81 | 120 | * @param size The maximum length of the buffer |
Christopher Haster |
14:3d1845f5cd81 | 121 | * @return Number of received bytes on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 122 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
14:3d1845f5cd81 | 123 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
14:3d1845f5cd81 | 124 | */ |
Christopher Haster |
14:3d1845f5cd81 | 125 | virtual int socket_recv(void *handle, void *data, unsigned size); |
Christopher Haster |
14:3d1845f5cd81 | 126 | |
Christopher Haster |
14:3d1845f5cd81 | 127 | /** Send a packet to a remote endpoint |
Christopher Haster |
14:3d1845f5cd81 | 128 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 129 | * @param address The remote SocketAddress |
Christopher Haster |
14:3d1845f5cd81 | 130 | * @param data The packet to be sent |
Christopher Haster |
14:3d1845f5cd81 | 131 | * @param size The length of the packet to be sent |
Christopher Haster |
14:3d1845f5cd81 | 132 | * @return the number of written bytes on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 133 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
14:3d1845f5cd81 | 134 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
14:3d1845f5cd81 | 135 | */ |
Christopher Haster |
14:3d1845f5cd81 | 136 | virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); |
Christopher Haster |
14:3d1845f5cd81 | 137 | |
Christopher Haster |
14:3d1845f5cd81 | 138 | /** Receive a packet from a remote endpoint |
Christopher Haster |
14:3d1845f5cd81 | 139 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 140 | * @param address Destination for the remote SocketAddress or null |
Christopher Haster |
14:3d1845f5cd81 | 141 | * @param buffer The buffer for storing the incoming packet data |
Christopher Haster |
14:3d1845f5cd81 | 142 | * If a packet is too long to fit in the supplied buffer, |
Christopher Haster |
14:3d1845f5cd81 | 143 | * excess bytes are discarded |
Christopher Haster |
14:3d1845f5cd81 | 144 | * @param size The length of the buffer |
Christopher Haster |
14:3d1845f5cd81 | 145 | * @return the number of received bytes on success, negative on failure |
Christopher Haster |
14:3d1845f5cd81 | 146 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
14:3d1845f5cd81 | 147 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
14:3d1845f5cd81 | 148 | */ |
Christopher Haster |
14:3d1845f5cd81 | 149 | virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); |
Christopher Haster |
14:3d1845f5cd81 | 150 | |
Christopher Haster |
14:3d1845f5cd81 | 151 | /** Register a callback on state change of the socket |
Christopher Haster |
14:3d1845f5cd81 | 152 | * @param handle Socket handle |
Christopher Haster |
14:3d1845f5cd81 | 153 | * @param callback Function to call on state change |
Christopher Haster |
14:3d1845f5cd81 | 154 | * @param data Argument to pass to callback |
Christopher Haster |
14:3d1845f5cd81 | 155 | * @note Callback may be called in an interrupt context. |
Christopher Haster |
14:3d1845f5cd81 | 156 | */ |
Christopher Haster |
14:3d1845f5cd81 | 157 | virtual void socket_attach(void *handle, void (*callback)(void *), void *data); |
sarahmarshy | 16:eaf31bd133f0 | 158 | |
sarahmarshy | 16:eaf31bd133f0 | 159 | /** Provide access to the NetworkStack object |
sarahmarshy | 16:eaf31bd133f0 | 160 | * |
sarahmarshy | 16:eaf31bd133f0 | 161 | * @return The underlying NetworkStack object |
sarahmarshy | 16:eaf31bd133f0 | 162 | */ |
sarahmarshy | 16:eaf31bd133f0 | 163 | virtual NetworkStack *get_stack() |
sarahmarshy | 16:eaf31bd133f0 | 164 | { |
sarahmarshy | 16:eaf31bd133f0 | 165 | return this; |
sarahmarshy | 16:eaf31bd133f0 | 166 | } |
geky | 12:181a9415736b | 167 | |
geky | 12:181a9415736b | 168 | private: |
geky | 12:181a9415736b | 169 | // Modem object |
geky | 12:181a9415736b | 170 | bool _debug; |
geky | 12:181a9415736b | 171 | MDMSerial *_mdm; |
geky | 15:21d4f56eb172 | 172 | SocketAddress _ip_address; |
Christopher Haster |
14:3d1845f5cd81 | 173 | char _mac_address[NSAPI_MAC_SIZE]; |
geky | 12:181a9415736b | 174 | char _pin[sizeof "1234"]; |
sarahmarshy | 16:eaf31bd133f0 | 175 | Thread _thread; |
sarahmarshy | 16:eaf31bd133f0 | 176 | void (*_callback)(void *); void *_data; |
sarahmarshy | 16:eaf31bd133f0 | 177 | void _run(); |
geky | 12:181a9415736b | 178 | }; |
geky | 12:181a9415736b | 179 | |
geky | 12:181a9415736b | 180 | #endif |