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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers C027Interface.h Source File

C027Interface.h

00001 /* C027 implementation of NetworkInterfaceAPI
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef C027_INTERFACE_H
00018 #define C027_INTERFACE_H
00019 
00020 #include "CellularInterface.h"
00021 #include "MDM.h"
00022 
00023 #define NUMSOCKETS 12
00024 
00025 /** C027Interface class
00026  *  Implementation of the NetworkInterface for C027
00027  */
00028 class C027Interface : public NetworkStack, public CellularInterface
00029 {
00030 public:
00031     /** C027Interfacelifetime
00032      * @param simpin    Optional PIN for the SIM
00033      * @param debug     Enable debugging
00034      */
00035     C027Interface(const char *simpin=0, bool debug=false);
00036    
00037     ~C027Interface(); 
00038     /** Set the cellular network APN and credentials
00039      *
00040      *  @param apn      Optional name of the network to connect to
00041      *  @param user     Optional username for the APN
00042      *  @param pass     Optional password fot the APN
00043      *  @return         0 on success, negative error code on failure
00044      */
00045     virtual nsapi_error_t set_credentials(const char *apn, const char *username = 0, const char *password = 0);
00046     
00047     /** Start the interface
00048      *
00049      *  Attempts to connect to a cellular network based on supplied credentials
00050      *
00051      *  @return         0 on success, negative error code on failure
00052      */
00053     virtual nsapi_error_t connect();
00054 
00055     /** Start the interface
00056      *
00057      *  @param apn      Optional name of the network to connect to
00058      *  @param username Optional username for your APN
00059      *  @param password Optional password for your APN 
00060      *  @return         0 on success, negative error code on failure
00061      */
00062     virtual nsapi_error_t connect(const char *apn, const char *username = 0, const char *password = 0);
00063 
00064     /** Stop the interface
00065      *  @return             0 on success, negative on failure
00066      */
00067     virtual int disconnect();
00068 
00069     /** Get the internally stored IP address
00070      *  @return             IP address of the interface or null if not yet connected
00071      */
00072     virtual const char *get_ip_address();
00073 
00074     /** Get the internally stored MAC address
00075      *  @return             MAC address of the interface
00076      */
00077     virtual const char *get_mac_address();
00078 
00079 protected:
00080     /** Open a socket
00081      *  @param handle       Handle in which to store new socket
00082      *  @param proto        Type of socket to open, NSAPI_TCP or NSAPI_UDP
00083      *  @return             0 on success, negative on failure
00084      */
00085     virtual int socket_open(void **handle, nsapi_protocol_t proto);
00086 
00087     /** Close the socket
00088      *  @param handle       Socket handle
00089      *  @return             0 on success, negative on failure
00090      *  @note On failure, any memory associated with the socket must still 
00091      *        be cleaned up
00092      */
00093     virtual int socket_close(void *handle);
00094 
00095     /** Bind a server socket to a specific port
00096      *  @param handle       Socket handle
00097      *  @param address      Local address to listen for incoming connections on 
00098      *  @return             0 on success, negative on failure.
00099      */
00100     virtual int socket_bind(void *handle, const SocketAddress &address);
00101 
00102     /** Start listening for incoming connections
00103      *  @param handle       Socket handle
00104      *  @param backlog      Number of pending connections that can be queued up at any
00105      *                      one time [Default: 1]
00106      *  @return             0 on success, negative on failure
00107      */
00108     virtual int socket_listen(void *handle, int backlog);
00109 
00110     /** Connects this TCP socket to the server
00111      *  @param handle       Socket handle
00112      *  @param address      SocketAddress to connect to
00113      *  @return             0 on success, negative on failure
00114      */
00115     virtual int socket_connect(void *handle, const SocketAddress &address);
00116 
00117     /** Accept a new connection.
00118      *  @param handle       Handle in which to store new socket
00119      *  @param server       Socket handle to server to accept from
00120      *  @return             0 on success, negative on failure
00121      *  @note This call is not-blocking, if this call would block, must
00122      *        immediately return NSAPI_ERROR_WOULD_WAIT
00123      */
00124     virtual nsapi_error_t socket_accept(nsapi_socket_t server,
00125             nsapi_socket_t *handle, SocketAddress *address=0);
00126     /** Send data to the remote host
00127      *  @param handle       Socket handle
00128      *  @param data         The buffer to send to the host
00129      *  @param size         The length of the buffer to send
00130      *  @return             Number of written bytes on success, negative on failure
00131      *  @note This call is not-blocking, if this call would block, must
00132      *        immediately return NSAPI_ERROR_WOULD_WAIT
00133      */
00134     virtual int socket_send(void *handle, const void *data, unsigned size);
00135 
00136     /** Receive data from the remote host
00137      *  @param handle       Socket handle
00138      *  @param data         The buffer in which to store the data received from the host
00139      *  @param size         The maximum length of the buffer
00140      *  @return             Number of received bytes on success, negative on failure
00141      *  @note This call is not-blocking, if this call would block, must
00142      *        immediately return NSAPI_ERROR_WOULD_WAIT
00143      */
00144     virtual int socket_recv(void *handle, void *data, unsigned size);
00145 
00146     /** Send a packet to a remote endpoint
00147      *  @param handle       Socket handle
00148      *  @param address      The remote SocketAddress
00149      *  @param data         The packet to be sent
00150      *  @param size         The length of the packet to be sent
00151      *  @return the         number of written bytes on success, negative on failure
00152      *  @note This call is not-blocking, if this call would block, must
00153      *        immediately return NSAPI_ERROR_WOULD_WAIT
00154      */
00155     virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
00156 
00157     /** Receive a packet from a remote endpoint
00158      *  @param handle       Socket handle
00159      *  @param address      Destination for the remote SocketAddress or null
00160      *  @param buffer       The buffer for storing the incoming packet data
00161      *                      If a packet is too long to fit in the supplied buffer,
00162      *                      excess bytes are discarded
00163      *  @param size         The length of the buffer
00164      *  @return the         number of received bytes on success, negative on failure
00165      *  @note This call is not-blocking, if this call would block, must
00166      *        immediately return NSAPI_ERROR_WOULD_WAIT
00167      */
00168     virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
00169 
00170     /** Register a callback on state change of the socket
00171      *  @param handle       Socket handle
00172      *  @param callback     Function to call on state change
00173      *  @param data         Argument to pass to callback
00174      *  @note Callback may be called in an interrupt context.
00175      */
00176     virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
00177     
00178      /** Provide access to the NetworkStack object
00179      *
00180      *  @return The underlying NetworkStack object
00181      */
00182      virtual NetworkStack *get_stack()
00183      {
00184          return this;
00185      }
00186 
00187 private:
00188     // Modem object
00189     bool _debug;
00190     MDMSerial *_mdm;
00191     bool running;
00192     SocketAddress _ip_address;
00193     char _mac_address[NSAPI_MAC_SIZE];
00194     char _pin[sizeof "1234"];
00195     char _apn[50];
00196     char _username[25];
00197     char _password[25];
00198         Mutex m; 
00199     Thread _thread;
00200     struct {
00201         void (*callback)(void *);
00202         void *data;
00203     } _cbs[NUMSOCKETS]; // Callbacks for socket_attach 
00204     void (*_callback)(void *); void *_data;
00205     void *_handle;
00206     void socket_check();
00207     void event();
00208 };
00209 
00210 #endif