modified to get more signal info

Dependencies:   WncControllerK64F

Fork of WNC14A2AInterface by Avnet

Committer:
tdMBED
Date:
Sat Nov 25 21:51:47 2017 +0000
Revision:
10:c037dc6c1c03
Parent:
6:7fd9e590c4e7
Child:
8:1c11661da488
modified to get more signal info

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 1:f925e07b044d 1 /* WNC14A2A implementation of NetworkInterfaceAPI
JMF 1:f925e07b044d 2 * Copyright (c) 2015 ARM Limited
JMF 1:f925e07b044d 3 *
JMF 1:f925e07b044d 4 * Licensed under the Apache License, Version 2.0 (the "License");
JMF 1:f925e07b044d 5 * you may not use this file except in compliance with the License.
JMF 1:f925e07b044d 6 * You may obtain a copy of the License at
JMF 1:f925e07b044d 7 *
JMF 1:f925e07b044d 8 * http://www.apache.org/licenses/LICENSE-2.0
JMF 1:f925e07b044d 9 *
JMF 1:f925e07b044d 10 * Unless required by applicable law or agreed to in writing, software
JMF 1:f925e07b044d 11 * distributed under the License is distributed on an "AS IS" BASIS,
JMF 1:f925e07b044d 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JMF 1:f925e07b044d 13 * See the License for the specific language governing permissions and
JMF 1:f925e07b044d 14 * limitations under the License.
JMF 1:f925e07b044d 15 */
JMF 1:f925e07b044d 16
JMF 1:f925e07b044d 17 #ifndef WNC14A2A_INTERFACE_H
JMF 1:f925e07b044d 18 #define WNC14A2A_INTERFACE_H
JMF 1:f925e07b044d 19
JMF 1:f925e07b044d 20 #include <stdint.h>
JMF 1:f925e07b044d 21
JMF 1:f925e07b044d 22 #include "mbed.h"
JMF 6:7fd9e590c4e7 23 #include "WNCDebug.h"
JMF 1:f925e07b044d 24 #include "WncControllerK64F/WncControllerK64F.h"
JMF 1:f925e07b044d 25
JMF 1:f925e07b044d 26 #define WNC14A2A_SOCKET_COUNT 5
JMF 1:f925e07b044d 27
JMF 1:f925e07b044d 28
JMF 1:f925e07b044d 29 typedef struct smsmsg_t {
JMF 1:f925e07b044d 30 string number;
JMF 1:f925e07b044d 31 string date;
JMF 1:f925e07b044d 32 string time;
JMF 1:f925e07b044d 33 string msg;
JMF 1:f925e07b044d 34 } IOTSMS;
JMF 1:f925e07b044d 35
JMF 1:f925e07b044d 36 typedef struct socket_t {
JMF 1:f925e07b044d 37 int socket; //index of this socket
JMF 1:f925e07b044d 38 string url;
JMF 1:f925e07b044d 39 SocketAddress addr; //hold info for this socket
JMF 1:f925e07b044d 40 bool opened; //has the socket been opened
JMF 1:f925e07b044d 41 int proto; //this is a TCP or UDP socket
JMF 1:f925e07b044d 42 } WNCSOCKET;
JMF 1:f925e07b044d 43
JMF 1:f925e07b044d 44 #define WNC_DEBUG 0 //1=enable the WNC startup debug output
JMF 1:f925e07b044d 45 //0=disable the WNC startup debug output
JMF 1:f925e07b044d 46 #define STOP_ON_FE 1 //1=hang forever if a fatal error occurs
JMF 1:f925e07b044d 47 //0=simply return failed response for all socket calls
JMF 1:f925e07b044d 48 #define DISPLAY_FE 1 //1 to display the fatal error when it occurs
JMF 1:f925e07b044d 49 //0 to NOT display the fatal error
JMF 1:f925e07b044d 50 #define RESETON_FE 0 //1 to cause the MCU to reset on fatal error
JMF 1:f925e07b044d 51 //0 to NOT reset the MCU
JMF 1:f925e07b044d 52
JMF 1:f925e07b044d 53 #define APN_DEFAULT "m2m.com.attz"
JMF 1:f925e07b044d 54
JMF 1:f925e07b044d 55 //
JMF 1:f925e07b044d 56 // WNC Error Handling macros & data
JMF 1:f925e07b044d 57 //
JMF 1:f925e07b044d 58 #define FATAL_FLAG WncController::WNC_NO_RESPONSE
JMF 1:f925e07b044d 59 #define WNC_GOOD WncController::WNC_ON
JMF 1:f925e07b044d 60
JMF 1:f925e07b044d 61 #define RETfail return -1
JMF 1:f925e07b044d 62 #define RETvoid return
JMF 1:f925e07b044d 63 #define RETnull return NULL
JMF 1:f925e07b044d 64 #define RETresume
JMF 1:f925e07b044d 65
JMF 1:f925e07b044d 66 #define DORET(x) RET##x
JMF 1:f925e07b044d 67
JMF 1:f925e07b044d 68 #define TOSTR(x) #x
JMF 1:f925e07b044d 69 #define INTSTR(x) TOSTR(x)
JMF 1:f925e07b044d 70 #define FATAL_STR (char*)(__FILE__ ":" INTSTR(__LINE__))
JMF 1:f925e07b044d 71
JMF 1:f925e07b044d 72 #if RESETON_FE == 1 //reset on fatal error
JMF 1:f925e07b044d 73 #define MCURESET ((*((volatile unsigned long *)0xE000ED0CU))=(unsigned long)((0x5fa<<16) | 0x04L))
JMF 1:f925e07b044d 74 #define RSTMSG "RESET MCU! "
JMF 1:f925e07b044d 75 #else
JMF 1:f925e07b044d 76 #define MCURESET
JMF 1:f925e07b044d 77 #define RSTMSG ""
JMF 1:f925e07b044d 78 #endif
JMF 1:f925e07b044d 79
JMF 1:f925e07b044d 80 #if DISPLAY_FE == 1 //display fatal error message
JMF 1:f925e07b044d 81 #define PFE {if(_debugUart)_debugUart->printf(RSTMSG "\r\n>>WNC FAILED @ %s\r\n", FATAL_STR);}
JMF 1:f925e07b044d 82 #else
JMF 1:f925e07b044d 83 #define PFE
JMF 1:f925e07b044d 84 #endif
JMF 1:f925e07b044d 85
JMF 1:f925e07b044d 86 #if STOP_ON_FE == 1 //halt cpu on fatal error
JMF 1:f925e07b044d 87 #define FATAL_WNC_ERROR(v) {_fatal_err_loc=FATAL_STR;PFE;MCURESET;while(1);}
JMF 1:f925e07b044d 88 #else
JMF 1:f925e07b044d 89 #define FATAL_WNC_ERROR(v) {_fatal_err_loc=FATAL_STR;PFE;DORET(v);}
JMF 1:f925e07b044d 90 #endif
JMF 1:f925e07b044d 91
JMF 1:f925e07b044d 92 #define CHK_WNCFE(x,y) if( x ){FATAL_WNC_ERROR(y);}
JMF 1:f925e07b044d 93
JMF 1:f925e07b044d 94 #define MAX_SMS_MSGS 3
JMF 1:f925e07b044d 95
JMF 1:f925e07b044d 96 using namespace WncController_fk;
JMF 1:f925e07b044d 97
JMF 1:f925e07b044d 98 /** WNC14A2AInterface class
JMF 1:f925e07b044d 99 * Implementation of the NetworkInterface for WNC14A2A
JMF 1:f925e07b044d 100 */
JMF 1:f925e07b044d 101 class WNC14A2AInterface : public NetworkStack, public CellularInterface
JMF 1:f925e07b044d 102 {
JMF 1:f925e07b044d 103 public:
JMF 1:f925e07b044d 104 /** WNC14A2AInterface Constructors...
JMF 1:f925e07b044d 105 * @param can include an APN string and/or a debug uart
JMF 1:f925e07b044d 106 */
JMF 6:7fd9e590c4e7 107 WNC14A2AInterface(WNCDebug *_dbgUart = NULL);
JMF 1:f925e07b044d 108 ~WNC14A2AInterface();
JMF 1:f925e07b044d 109
JMF 1:f925e07b044d 110 /** Set the cellular network APN and credentials
JMF 1:f925e07b044d 111 *
JMF 1:f925e07b044d 112 * @param apn Optional name of the network to connect to
JMF 1:f925e07b044d 113 * @param user Optional username for the APN
JMF 1:f925e07b044d 114 * @param pass Optional password fot the APN
JMF 1:f925e07b044d 115 * @return 0 on success, negative error code on failure
JMF 1:f925e07b044d 116 */
JMF 1:f925e07b044d 117 virtual nsapi_error_t set_credentials(const char *apn = 0,
JMF 1:f925e07b044d 118 const char *username = 0, const char *password = 0);
JMF 1:f925e07b044d 119
JMF 1:f925e07b044d 120 /** Start the interface
JMF 1:f925e07b044d 121 *
JMF 1:f925e07b044d 122 * @param apn Optional name of the network to connect to
JMF 1:f925e07b044d 123 * @param username Optional username for your APN
JMF 1:f925e07b044d 124 * @param password Optional password for your APN
JMF 1:f925e07b044d 125 * @return 0 on success, negative error code on failure
JMF 1:f925e07b044d 126 */
JMF 1:f925e07b044d 127 virtual nsapi_error_t connect(const char *apn,
JMF 1:f925e07b044d 128 const char *username = 0, const char *password = 0);
JMF 1:f925e07b044d 129
JMF 1:f925e07b044d 130 /** Start the interface
JMF 1:f925e07b044d 131 *
JMF 1:f925e07b044d 132 * Attempts to connect to a cellular network based on supplied credentials
JMF 1:f925e07b044d 133 *
JMF 1:f925e07b044d 134 * @return 0 on success, negative error code on failure
JMF 1:f925e07b044d 135 */
JMF 1:f925e07b044d 136 virtual nsapi_error_t connect();
JMF 1:f925e07b044d 137
JMF 1:f925e07b044d 138 /** Stop the interface
JMF 1:f925e07b044d 139 *
JMF 1:f925e07b044d 140 * @return 0 on success, negative error code on failure
JMF 1:f925e07b044d 141 */
JMF 1:f925e07b044d 142 virtual nsapi_error_t disconnect();
JMF 1:f925e07b044d 143
JMF 1:f925e07b044d 144 /** Get the internally stored IP address. From NetworkStack Class
JMF 1:f925e07b044d 145 * @return IP address of the interface or null if not yet connected
JMF 1:f925e07b044d 146 */
JMF 1:f925e07b044d 147 virtual const char *get_ip_address();
JMF 1:f925e07b044d 148
JMF 1:f925e07b044d 149 /** Get the network assigned IP address.
JMF 1:f925e07b044d 150 * @return IP address of the interface or null if not yet connected
JMF 1:f925e07b044d 151 */
JMF 1:f925e07b044d 152 const char *get_my_ip_address();
JMF 1:f925e07b044d 153
JMF 1:f925e07b044d 154 /** Get the internally stored MAC address. From CellularInterface Class
JMF 1:f925e07b044d 155 * @return MAC address of the interface
JMF 1:f925e07b044d 156 */
JMF 1:f925e07b044d 157 virtual const char *get_mac_address();
JMF 1:f925e07b044d 158
JMF 1:f925e07b044d 159 /** Attach a function to be called when a text is recevieds
JMF 1:f925e07b044d 160 * @param callback function pointer to a callback that will accept the message
JMF 1:f925e07b044d 161 * contents when a text is received.
JMF 1:f925e07b044d 162 */
JMF 1:f925e07b044d 163 void sms_attach(void (*callback)(IOTSMS *));
JMF 1:f925e07b044d 164
JMF 1:f925e07b044d 165 void doDebug(int v);
JMF 1:f925e07b044d 166
JMF 1:f925e07b044d 167 bool registered();
JMF 1:f925e07b044d 168
JMF 1:f925e07b044d 169 void sms_start(void);
JMF 1:f925e07b044d 170
JMF 1:f925e07b044d 171 /** start listening for incomming SMS messages
JMF 1:f925e07b044d 172 * @param time in msec to check
JMF 1:f925e07b044d 173 */
JMF 1:f925e07b044d 174 void sms_listen(uint16_t=1000); // Configure device to listen for text messages
JMF 1:f925e07b044d 175
JMF 1:f925e07b044d 176 int getSMS(IOTSMS **msg);
JMF 1:f925e07b044d 177
JMF 1:f925e07b044d 178 int sendIOTSms(const string&, const string&);
JMF 1:f925e07b044d 179
JMF 1:f925e07b044d 180 char* getSMSnbr();
JMF 1:f925e07b044d 181
JMF 1:f925e07b044d 182
JMF 1:f925e07b044d 183 protected:
JMF 1:f925e07b044d 184
JMF 1:f925e07b044d 185 /** Get Host IP by name. From NetworkStack Class
JMF 1:f925e07b044d 186 */
JMF 1:f925e07b044d 187 virtual nsapi_error_t gethostbyname(const char* name, SocketAddress *address, nsapi_version_t version);
JMF 1:f925e07b044d 188
JMF 1:f925e07b044d 189
JMF 1:f925e07b044d 190 /** Provide access to the NetworkStack object
JMF 1:f925e07b044d 191 *
JMF 1:f925e07b044d 192 * @return The underlying NetworkStack object
JMF 1:f925e07b044d 193 */
JMF 1:f925e07b044d 194 virtual NetworkStack *get_stack();
JMF 1:f925e07b044d 195
JMF 1:f925e07b044d 196 /** Open a socket. FROM NetworkStack
JMF 1:f925e07b044d 197 * @param handle Handle in which to store new socket
JMF 1:f925e07b044d 198 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
JMF 1:f925e07b044d 199 * @return 0 on success, negative on failure
JMF 1:f925e07b044d 200 */
JMF 1:f925e07b044d 201 virtual int socket_open(void **handle, nsapi_protocol_t proto);
JMF 1:f925e07b044d 202
JMF 1:f925e07b044d 203 /** Close the socket. FROM NetworkStack
JMF 1:f925e07b044d 204 * @param handle Socket handle
JMF 1:f925e07b044d 205 * @return 0 on success, negative on failure
JMF 1:f925e07b044d 206 * @note On failure, any memory associated with the socket must still
JMF 1:f925e07b044d 207 * be cleaned up
JMF 1:f925e07b044d 208 */
JMF 1:f925e07b044d 209 virtual int socket_close(void *handle);
JMF 1:f925e07b044d 210
JMF 1:f925e07b044d 211 /** Bind a server socket to a specific port.FROM NetworkStack
JMF 1:f925e07b044d 212 * @param handle Socket handle
JMF 1:f925e07b044d 213 * @param address Local address to listen for incoming connections on
JMF 1:f925e07b044d 214 * @return 0 on success, negative on failure.
JMF 1:f925e07b044d 215 */
JMF 1:f925e07b044d 216 virtual int socket_bind(void *handle, const SocketAddress &address);
JMF 1:f925e07b044d 217
JMF 1:f925e07b044d 218 /** Start listening for incoming connections.FROM NetworkStack
JMF 1:f925e07b044d 219 * @param handle Socket handle
JMF 1:f925e07b044d 220 * @param backlog Number of pending connections that can be queued up at any
JMF 1:f925e07b044d 221 * one time [Default: 1]
JMF 1:f925e07b044d 222 * @return 0 on success, negative on failure
JMF 1:f925e07b044d 223 */
JMF 1:f925e07b044d 224 virtual int socket_listen(void *handle, int backlog);
JMF 1:f925e07b044d 225
JMF 1:f925e07b044d 226 /** Connects this TCP socket to the server.FROM NetworkStack
JMF 1:f925e07b044d 227 * @param handle Socket handle
JMF 1:f925e07b044d 228 * @param address SocketAddress to connect to
JMF 1:f925e07b044d 229 * @return 0 on success, negative on failure
JMF 1:f925e07b044d 230 */
JMF 1:f925e07b044d 231 virtual int socket_connect(void *handle, const SocketAddress &address);
JMF 1:f925e07b044d 232
JMF 1:f925e07b044d 233 /** Accept a new connection.FROM NetworkStack
JMF 1:f925e07b044d 234 * @param handle Handle in which to store new socket
JMF 1:f925e07b044d 235 * @param server Socket handle to server to accept from
JMF 1:f925e07b044d 236 * @return 0 on success, negative on failure
JMF 1:f925e07b044d 237 * @note This call is not-blocking, if this call would block, must
JMF 1:f925e07b044d 238 * immediately return NSAPI_ERROR_WOULD_WAIT
JMF 1:f925e07b044d 239 */
JMF 1:f925e07b044d 240 virtual int socket_accept(nsapi_socket_t server,
JMF 1:f925e07b044d 241 nsapi_socket_t *handle, SocketAddress *address=0);
JMF 1:f925e07b044d 242
JMF 1:f925e07b044d 243 /** Send data to the remote host.FROM NetworkStack
JMF 1:f925e07b044d 244 * @param handle Socket handle
JMF 1:f925e07b044d 245 * @param data The buffer to send to the host
JMF 1:f925e07b044d 246 * @param size The length of the buffer to send
JMF 1:f925e07b044d 247 * @return Number of written bytes on success, negative on failure
JMF 1:f925e07b044d 248 * @note This call is not-blocking, if this call would block, must
JMF 1:f925e07b044d 249 * immediately return NSAPI_ERROR_WOULD_WAIT
JMF 1:f925e07b044d 250 */
JMF 1:f925e07b044d 251 virtual int socket_send(void *handle, const void *data, unsigned size);
JMF 1:f925e07b044d 252
JMF 1:f925e07b044d 253 /** Receive data from the remote host.FROM NetworkStack
JMF 1:f925e07b044d 254 * @param handle Socket handle
JMF 1:f925e07b044d 255 * @param data The buffer in which to store the data received from the host
JMF 1:f925e07b044d 256 * @param size The maximum length of the buffer
JMF 1:f925e07b044d 257 * @return Number of received bytes on success, negative on failure
JMF 1:f925e07b044d 258 * @note This call is not-blocking, if this call would block, must
JMF 1:f925e07b044d 259 * immediately return NSAPI_ERROR_WOULD_WAIT
JMF 1:f925e07b044d 260 */
JMF 1:f925e07b044d 261 virtual int socket_recv(void *handle, void *data, unsigned size);
JMF 1:f925e07b044d 262
JMF 1:f925e07b044d 263 /** Send a packet to a remote endpoint.FROM NetworkStack
JMF 1:f925e07b044d 264 * @param handle Socket handle
JMF 1:f925e07b044d 265 * @param address The remote SocketAddress
JMF 1:f925e07b044d 266 * @param data The packet to be sent
JMF 1:f925e07b044d 267 * @param size The length of the packet to be sent
JMF 1:f925e07b044d 268 * @return the number of written bytes on success, negative on failure
JMF 1:f925e07b044d 269 * @note This call is not-blocking, if this call would block, must
JMF 1:f925e07b044d 270 * immediately return NSAPI_ERROR_WOULD_WAIT
JMF 1:f925e07b044d 271 */
JMF 1:f925e07b044d 272 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
JMF 1:f925e07b044d 273
JMF 1:f925e07b044d 274 /** Receive a packet from a remote endpoint.FROM NetworkStack
JMF 1:f925e07b044d 275 * @param handle Socket handle
JMF 1:f925e07b044d 276 * @param address Destination for the remote SocketAddress or null
JMF 1:f925e07b044d 277 * @param buffer The buffer for storing the incoming packet data
JMF 1:f925e07b044d 278 * If a packet is too long to fit in the supplied buffer,
JMF 1:f925e07b044d 279 * excess bytes are discarded
JMF 1:f925e07b044d 280 * @param size The length of the buffer
JMF 1:f925e07b044d 281 * @return the number of received bytes on success, negative on failure
JMF 1:f925e07b044d 282 * @note This call is not-blocking, if this call would block, must
JMF 1:f925e07b044d 283 * immediately return NSAPI_ERROR_WOULD_WAIT
JMF 1:f925e07b044d 284 */
JMF 1:f925e07b044d 285 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
JMF 1:f925e07b044d 286
JMF 1:f925e07b044d 287 /** Register a callback on state change of the socket.FROM NetworkStack
JMF 1:f925e07b044d 288 * @param handle Socket handle
JMF 1:f925e07b044d 289 * @param callback Function to call on state change
JMF 1:f925e07b044d 290 * @param data Argument to pass to callback
JMF 1:f925e07b044d 291 * @note Callback may be called in an interrupt context.
JMF 1:f925e07b044d 292 */
JMF 1:f925e07b044d 293 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
JMF 1:f925e07b044d 294
JMF 1:f925e07b044d 295 /** check for errors that may have occured
JMF 1:f925e07b044d 296 * @param none.
JMF 1:f925e07b044d 297 * @note this function can be called after any WNC14A2A class operation
JMF 1:f925e07b044d 298 * to determine error specifics if desired.
JMF 1:f925e07b044d 299 */
JMF 1:f925e07b044d 300 uint16_t wnc14a2a_chk_error(void) { return _errors; }
JMF 1:f925e07b044d 301
JMF 1:f925e07b044d 302
JMF 1:f925e07b044d 303 private:
JMF 1:f925e07b044d 304 // WncController Class for managing the 14A2a
JMF 1:f925e07b044d 305 friend class WncControllerK64F;
JMF 1:f925e07b044d 306
JMF 1:f925e07b044d 307 bool m_wncpoweredup; //track if WNC has been power-up
JMF 1:f925e07b044d 308 bool m_debug;
JMF 1:f925e07b044d 309
JMF 1:f925e07b044d 310 WncIpStats myNetStats; //maintaint the network statistics
JMF 1:f925e07b044d 311 WncControllerK64F_fk::WncControllerK64F *_pwnc; //pointer to the WncController instance
JMF 1:f925e07b044d 312
JMF 1:f925e07b044d 313 int m_active_socket; // a 'pseudo' global to track the active socket
JMF 6:7fd9e590c4e7 314 WNCDebug *_debugUart; // Serial object for parser to communicate with radio
JMF 1:f925e07b044d 315 char *_fatal_err_loc; // holds string containing location of fatal error
JMF 1:f925e07b044d 316 nsapi_error_t _errors;
JMF 1:f925e07b044d 317
JMF 1:f925e07b044d 318 bool m_smsmoning; // Track if the SMS monitoring thread is running
JMF 1:f925e07b044d 319 EventQueue sms_queue; // Queue used to schedule for SMS checks
JMF 1:f925e07b044d 320 Semaphore sms_rx_sem; // Semaphore to signal sms_event_thread to check for incoming text
JMF 1:f925e07b044d 321 void (*_sms_cb)(IOTSMS *); // Callback when text message is received. User must define this as
JMF 1:f925e07b044d 322 // a static function because I'm not handling an object offset
JMF 1:f925e07b044d 323 IOTSMS m_MsgText, m_MsgText_array[MAX_SMS_MSGS]; // Used to pass SMS message to the user
JMF 1:f925e07b044d 324 struct WncController::WncSmsList m_smsmsgs; //use the WncSmsList structure to hold messages
JMF 1:f925e07b044d 325
JMF 1:f925e07b044d 326 void handle_sms_event(); // Handle incoming text data
JMF 1:f925e07b044d 327
JMF 1:f925e07b044d 328 char _mac_address[NSAPI_MAC_SIZE]; // local Mac
JMF 6:7fd9e590c4e7 329 void debugOutput(WNCDebug *dbgOut, char * format, ...);
JMF 1:f925e07b044d 330 };
JMF 1:f925e07b044d 331
JMF 1:f925e07b044d 332 #endif