C027Interface

Dependencies:   C027_Support

Fork of C027Interface 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 
00024 /** C027Interface class
00025  *  Implementation of the NetworkInterface for C027
00026  */
00027 class C027Interface : public NetworkStack, public CellularInterface
00028 {
00029 public:
00030     /** C027Interfacelifetime
00031      * @param simpin    Optional PIN for the SIM
00032      * @param debug     Enable debugging
00033      */
00034     C027Interface(const char *simpin=0, bool debug=false);
00035 
00036     /** Start the interface
00037      *
00038      *  @param apn      Optional name of the network to connect to
00039      *  @param username Optional username for your APN
00040      *  @param password Optional password for your APN 
00041      *  @return         0 on success, negative error code on failure
00042      */
00043     virtual int connect(const char *apn = 0, const char *username = 0, const char *password = 0);
00044 
00045     /** Stop the interface
00046      *  @return             0 on success, negative on failure
00047      */
00048     virtual int disconnect();
00049 
00050     /** Get the internally stored IP address
00051      *  @return             IP address of the interface or null if not yet connected
00052      */
00053     virtual const char *get_ip_address();
00054 
00055     /** Get the internally stored MAC address
00056      *  @return             MAC address of the interface
00057      */
00058     virtual const char *get_mac_address();
00059 
00060 protected:
00061     /** Open a socket
00062      *  @param handle       Handle in which to store new socket
00063      *  @param proto        Type of socket to open, NSAPI_TCP or NSAPI_UDP
00064      *  @return             0 on success, negative on failure
00065      */
00066     virtual int socket_open(void **handle, nsapi_protocol_t proto);
00067 
00068     /** Close the socket
00069      *  @param handle       Socket handle
00070      *  @return             0 on success, negative on failure
00071      *  @note On failure, any memory associated with the socket must still 
00072      *        be cleaned up
00073      */
00074     virtual int socket_close(void *handle);
00075 
00076     /** Bind a server socket to a specific port
00077      *  @param handle       Socket handle
00078      *  @param address      Local address to listen for incoming connections on 
00079      *  @return             0 on success, negative on failure.
00080      */
00081     virtual int socket_bind(void *handle, const SocketAddress &address);
00082 
00083     /** Start listening for incoming connections
00084      *  @param handle       Socket handle
00085      *  @param backlog      Number of pending connections that can be queued up at any
00086      *                      one time [Default: 1]
00087      *  @return             0 on success, negative on failure
00088      */
00089     virtual int socket_listen(void *handle, int backlog);
00090 
00091     /** Connects this TCP socket to the server
00092      *  @param handle       Socket handle
00093      *  @param address      SocketAddress to connect to
00094      *  @return             0 on success, negative on failure
00095      */
00096     virtual int socket_connect(void *handle, const SocketAddress &address);
00097 
00098     /** Accept a new connection.
00099      *  @param handle       Handle in which to store new socket
00100      *  @param server       Socket handle to server to accept from
00101      *  @return             0 on success, negative on failure
00102      *  @note This call is not-blocking, if this call would block, must
00103      *        immediately return NSAPI_ERROR_WOULD_WAIT
00104      */
00105     virtual int socket_accept(void **handle, void *server);
00106 
00107     /** Send data to the remote host
00108      *  @param handle       Socket handle
00109      *  @param data         The buffer to send to the host
00110      *  @param size         The length of the buffer to send
00111      *  @return             Number of written bytes on success, negative on failure
00112      *  @note This call is not-blocking, if this call would block, must
00113      *        immediately return NSAPI_ERROR_WOULD_WAIT
00114      */
00115     virtual int socket_send(void *handle, const void *data, unsigned size);
00116 
00117     /** Receive data from the remote host
00118      *  @param handle       Socket handle
00119      *  @param data         The buffer in which to store the data received from the host
00120      *  @param size         The maximum length of the buffer
00121      *  @return             Number of received bytes on success, negative on failure
00122      *  @note This call is not-blocking, if this call would block, must
00123      *        immediately return NSAPI_ERROR_WOULD_WAIT
00124      */
00125     virtual int socket_recv(void *handle, void *data, unsigned size);
00126 
00127     /** Send a packet to a remote endpoint
00128      *  @param handle       Socket handle
00129      *  @param address      The remote SocketAddress
00130      *  @param data         The packet to be sent
00131      *  @param size         The length of the packet to be sent
00132      *  @return the         number of written bytes on success, negative on failure
00133      *  @note This call is not-blocking, if this call would block, must
00134      *        immediately return NSAPI_ERROR_WOULD_WAIT
00135      */
00136     virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
00137 
00138     /** Receive a packet from a remote endpoint
00139      *  @param handle       Socket handle
00140      *  @param address      Destination for the remote SocketAddress or null
00141      *  @param buffer       The buffer for storing the incoming packet data
00142      *                      If a packet is too long to fit in the supplied buffer,
00143      *                      excess bytes are discarded
00144      *  @param size         The length of the buffer
00145      *  @return the         number of received bytes on success, negative on failure
00146      *  @note This call is not-blocking, if this call would block, must
00147      *        immediately return NSAPI_ERROR_WOULD_WAIT
00148      */
00149     virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
00150 
00151     /** Register a callback on state change of the socket
00152      *  @param handle       Socket handle
00153      *  @param callback     Function to call on state change
00154      *  @param data         Argument to pass to callback
00155      *  @note Callback may be called in an interrupt context.
00156      */
00157     virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
00158     
00159      /** Provide access to the NetworkStack object
00160      *
00161      *  @return The underlying NetworkStack object
00162      */
00163      virtual NetworkStack *get_stack()
00164      {
00165          return this;
00166      }
00167 
00168 private:
00169     // Modem object
00170     bool _debug;
00171     MDMSerial *_mdm;
00172     SocketAddress _ip_address;
00173     char _mac_address[NSAPI_MAC_SIZE];
00174     char _pin[sizeof "1234"];
00175     Thread _thread;
00176     void (*_callback)(void *); void *_data;
00177     void _run();
00178 };
00179 
00180 #endif