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