Use this interface to connect to and interact with the WNC M14A2A LTE Cellular Data Module which is provided by Wistron NeWeb Corporation (WNC) when using ARMmbed v5. The interface provides a Networking interface that can be used with the AT&T Cellular IoT Starter Kit that is sold by Avnet (http://cloudconnectkits.org/product/att-cellular-iot-starter-kit).

Dependencies:   WncControllerK64F

Dependents:   easy-connect-wnc easy-connect easy-connect111

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WNC14A2AInterface.h Source File

WNC14A2AInterface.h

00001 /* WNC14A2A 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 WNC14A2A_INTERFACE_H
00018 #define WNC14A2A_INTERFACE_H
00019 
00020 #include <stdint.h>
00021 
00022 #include "mbed.h"
00023 #include "WNCDebug.h"
00024 #include "WncControllerK64F/WncControllerK64F.h"
00025 
00026 #define WNC14A2A_SOCKET_COUNT 5
00027 
00028 
00029 typedef struct smsmsg_t {
00030         string number;
00031         string date;
00032         string time;
00033         string msg;
00034     } IOTSMS;
00035 
00036 typedef struct socket_t {
00037         int socket;             //index of this socket
00038         string url;
00039         SocketAddress addr;     //hold info for this socket
00040         bool opened;            //has the socket been opened
00041         int proto;              //this is a TCP or UDP socket
00042     } WNCSOCKET;
00043 
00044 #define WNC_DEBUG   0           //1=enable the WNC startup debug output
00045                                 //0=disable the WNC startup debug output
00046 #define STOP_ON_FE  1           //1=hang forever if a fatal error occurs
00047                                 //0=simply return failed response for all socket calls
00048 #define DISPLAY_FE  1           //1 to display the fatal error when it occurs
00049                                 //0 to NOT display the fatal error
00050 #define RESETON_FE  0           //1 to cause the MCU to reset on fatal error
00051                                 //0 to NOT reset the MCU
00052 
00053 #define APN_DEFAULT             "m2m.com.attz"
00054                         
00055 //
00056 // WNC Error Handling macros & data
00057 //
00058 #define FATAL_FLAG  WncController::WNC_NO_RESPONSE
00059 #define WNC_GOOD    WncController::WNC_ON
00060 
00061 #define RETfail return -1
00062 #define RETvoid return
00063 #define RETnull return NULL
00064 #define RETresume   
00065 
00066 #define DORET(x) RET##x
00067 
00068 #define TOSTR(x) #x
00069 #define INTSTR(x) TOSTR(x)
00070 #define FATAL_STR (char*)(__FILE__ ":" INTSTR(__LINE__))
00071 
00072 #if RESETON_FE == 1   //reset on fatal error
00073 #define MCURESET     ((*((volatile unsigned long *)0xE000ED0CU))=(unsigned long)((0x5fa<<16) | 0x04L))
00074 #define RSTMSG       "RESET MCU! "
00075 #else
00076 #define MCURESET
00077 #define RSTMSG       ""
00078 #endif
00079 
00080 #if DISPLAY_FE == 1  //display fatal error message
00081 #define PFE     {if(_debugUart)_debugUart->printf(RSTMSG "\r\n>>WNC FAILED @ %s\r\n", FATAL_STR);}
00082 #else
00083 #define PFE
00084 #endif
00085 
00086 #if STOP_ON_FE == 1  //halt cpu on fatal error
00087 #define FATAL_WNC_ERROR(v)  {_fatal_err_loc=FATAL_STR;PFE;MCURESET;while(1);}
00088 #else
00089 #define FATAL_WNC_ERROR(v)  {_fatal_err_loc=FATAL_STR;PFE;DORET(v);}
00090 #endif
00091 
00092 #define CHK_WNCFE(x,y)    if( x ){FATAL_WNC_ERROR(y);}
00093 
00094 #define MAX_SMS_MSGS    3
00095 
00096 using namespace WncController_fk;
00097  
00098 /** WNC14A2AInterface class
00099  *  Implementation of the NetworkInterface for WNC14A2A 
00100  */
00101 class WNC14A2AInterface : public NetworkStack, public CellularInterface
00102 {
00103 public:
00104     /** WNC14A2AInterface Constructors...
00105      * @param can include an APN string and/or a debug uart
00106      */
00107     WNC14A2AInterface(WNCDebug *_dbgUart = NULL);
00108     virtual ~WNC14A2AInterface();
00109 
00110     /** Set the cellular network APN and credentials
00111      *
00112      *  @param apn      Optional name of the network to connect to
00113      *  @param user     Optional username for the APN
00114      *  @param pass     Optional password fot the APN
00115      *  @return         0 on success, negative error code on failure
00116      */
00117     virtual nsapi_error_t set_credentials(const char *apn = 0,
00118             const char *username = 0, const char *password = 0);
00119  
00120     /** Start the interface
00121      *
00122      *  @param apn      Optional name of the network to connect to
00123      *  @param username Optional username for your APN
00124      *  @param password Optional password for your APN 
00125      *  @return         0 on success, negative error code on failure
00126      */
00127     virtual nsapi_error_t connect(const char *apn,
00128             const char *username = 0, const char *password = 0);
00129  
00130     /** Start the interface
00131      *
00132      *  Attempts to connect to a cellular network based on supplied credentials
00133      *
00134      *  @return         0 on success, negative error code on failure
00135      */
00136     virtual nsapi_error_t connect();
00137 
00138     /** Stop the interface
00139      *
00140      *  @return         0 on success, negative error code on failure
00141      */
00142     virtual nsapi_error_t disconnect();
00143  
00144     /** Get the internally stored IP address. From NetworkStack Class
00145      *  @return             IP address of the interface or null if not yet connected
00146      */
00147     virtual const char *get_ip_address();
00148  
00149     /** Get the network assigned IP address.
00150      *  @return             IP address of the interface or null if not yet connected
00151      */
00152     const char *get_my_ip_address();
00153 
00154     /** Get the internally stored MAC address.  From CellularInterface Class
00155      *  @return             MAC address of the interface
00156      */
00157     virtual const char *get_mac_address();
00158  
00159     /** Attach a function to be called when a text is recevieds
00160      *  @param callback  function pointer to a callback that will accept the message 
00161      *  contents when a text is received.
00162      */
00163     void sms_attach(void (*callback)(IOTSMS *));
00164 
00165     void doDebug(int v);
00166 
00167     bool registered();
00168 
00169     void sms_start(void);
00170 
00171     /** start listening for incomming SMS messages
00172      *  @param time in msec to check
00173      */
00174     void sms_listen(uint16_t=1000);         // Configure device to listen for text messages 
00175         
00176     int getSMS(IOTSMS **msg);
00177 
00178     int sendIOTSms(const string&, const string&);
00179 
00180     char* getSMSnbr();
00181 
00182 
00183 protected:
00184 
00185     /** Get Host IP by name. From NetworkStack Class
00186      */
00187     virtual nsapi_error_t gethostbyname(const char* name, SocketAddress *address, nsapi_version_t version);
00188 
00189 
00190     /** Provide access to the NetworkStack object
00191      *
00192      *  @return The underlying NetworkStack object
00193      */
00194     virtual NetworkStack *get_stack();
00195 
00196     /** Open a socket. FROM NetworkStack
00197      *  @param handle       Handle in which to store new socket
00198      *  @param proto        Type of socket to open, NSAPI_TCP or NSAPI_UDP
00199      *  @return             0 on success, negative on failure
00200      */
00201     virtual int socket_open(void **handle, nsapi_protocol_t proto);
00202  
00203     /** Close the socket. FROM NetworkStack
00204      *  @param handle       Socket handle
00205      *  @return             0 on success, negative on failure
00206      *  @note On failure, any memory associated with the socket must still 
00207      *        be cleaned up
00208      */
00209     virtual int socket_close(void *handle);
00210  
00211     /** Bind a server socket to a specific port.FROM NetworkStack
00212      *  @param handle       Socket handle
00213      *  @param address      Local address to listen for incoming connections on 
00214      *  @return             0 on success, negative on failure.
00215      */
00216     virtual int socket_bind(void *handle, const SocketAddress &address);
00217  
00218     /** Start listening for incoming connections.FROM NetworkStack
00219      *  @param handle       Socket handle
00220      *  @param backlog      Number of pending connections that can be queued up at any
00221      *                      one time [Default: 1]
00222      *  @return             0 on success, negative on failure
00223      */
00224     virtual int socket_listen(void *handle, int backlog);
00225  
00226     /** Connects this TCP socket to the server.FROM NetworkStack
00227      *  @param handle       Socket handle
00228      *  @param address      SocketAddress to connect to
00229      *  @return             0 on success, negative on failure
00230      */
00231     virtual int socket_connect(void *handle, const SocketAddress &address);
00232  
00233     /** Accept a new connection.FROM NetworkStack
00234      *  @param handle       Handle in which to store new socket
00235      *  @param server       Socket handle to server to accept from
00236      *  @return             0 on success, negative on failure
00237      *  @note This call is not-blocking, if this call would block, must
00238      *        immediately return NSAPI_ERROR_WOULD_WAIT
00239      */
00240     virtual int socket_accept(nsapi_socket_t server,
00241             nsapi_socket_t *handle, SocketAddress *address=0);
00242  
00243     /** Send data to the remote host.FROM NetworkStack
00244      *  @param handle       Socket handle
00245      *  @param data         The buffer to send to the host
00246      *  @param size         The length of the buffer to send
00247      *  @return             Number of written bytes on success, negative on failure
00248      *  @note This call is not-blocking, if this call would block, must
00249      *        immediately return NSAPI_ERROR_WOULD_WAIT
00250      */
00251     virtual int socket_send(void *handle, const void *data, unsigned size);
00252  
00253     /** Receive data from the remote host.FROM NetworkStack
00254      *  @param handle       Socket handle
00255      *  @param data         The buffer in which to store the data received from the host
00256      *  @param size         The maximum length of the buffer
00257      *  @return             Number of received bytes on success, negative on failure
00258      *  @note This call is not-blocking, if this call would block, must
00259      *        immediately return NSAPI_ERROR_WOULD_WAIT
00260      */
00261     virtual int socket_recv(void *handle, void *data, unsigned size);
00262  
00263     /** Send a packet to a remote endpoint.FROM NetworkStack
00264      *  @param handle       Socket handle
00265      *  @param address      The remote SocketAddress
00266      *  @param data         The packet to be sent
00267      *  @param size         The length of the packet to be sent
00268      *  @return the         number of written bytes on success, negative on failure
00269      *  @note This call is not-blocking, if this call would block, must
00270      *        immediately return NSAPI_ERROR_WOULD_WAIT
00271      */
00272     virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
00273  
00274     /** Receive a packet from a remote endpoint.FROM NetworkStack
00275      *  @param handle       Socket handle
00276      *  @param address      Destination for the remote SocketAddress or null
00277      *  @param buffer       The buffer for storing the incoming packet data
00278      *                      If a packet is too long to fit in the supplied buffer,
00279      *                      excess bytes are discarded
00280      *  @param size         The length of the buffer
00281      *  @return the         number of received bytes on success, negative on failure
00282      *  @note This call is not-blocking, if this call would block, must
00283      *        immediately return NSAPI_ERROR_WOULD_WAIT
00284      */
00285     virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
00286  
00287     /** Register a callback on state change of the socket.FROM NetworkStack
00288      *  @param handle       Socket handle
00289      *  @param callback     Function to call on state change
00290      *  @param data         Argument to pass to callback
00291      *  @note Callback may be called in an interrupt context.
00292      */
00293     virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
00294     
00295     /** check for errors that may have occured
00296      *  @param none.
00297      *  @note this function can be called after any WNC14A2A class operation 
00298      *        to determine error specifics if desired.
00299      */
00300     uint16_t wnc14a2a_chk_error(void) { return _errors; }
00301 
00302 
00303 private:
00304     // WncController Class for managing the 14A2a
00305     friend class WncControllerK64F;  
00306 
00307     bool m_wncpoweredup;                    //track if WNC has been power-up
00308     bool m_debug;
00309 
00310     WncIpStats myNetStats;                  //maintaint the network statistics
00311     WncControllerK64F_fk::WncControllerK64F *_pwnc; //pointer to the WncController instance
00312 
00313     int m_active_socket;                    // a 'pseudo' global to track the active socket
00314     WNCDebug *_debugUart;                     // Serial object for parser to communicate with radio
00315     char *_fatal_err_loc;                   // holds string containing location of fatal error
00316     nsapi_error_t _errors;
00317 
00318     bool m_smsmoning;                       // Track if the SMS monitoring thread is running
00319     EventQueue sms_queue;                   // Queue used to schedule for SMS checks
00320     Semaphore sms_rx_sem;                   // Semaphore to signal sms_event_thread to check for incoming text 
00321     void (*_sms_cb)(IOTSMS *);              // Callback when text message is received. User must define this as 
00322                                             // a static function because I'm not handling an object offset
00323     IOTSMS m_MsgText, m_MsgText_array[MAX_SMS_MSGS];       // Used to pass SMS message to the user
00324     struct WncController::WncSmsList m_smsmsgs;            //use the WncSmsList structure to hold messages
00325 
00326     void handle_sms_event();                // Handle incoming text data
00327 
00328     char _mac_address[NSAPI_MAC_SIZE];      // local Mac
00329     void debugOutput(WNCDebug *dbgOut, char * format, ...);
00330 };
00331 
00332 #endif