Changes to enabled on-line compiler

Committer:
JMF
Date:
Wed May 30 20:59:51 2018 +0000
Revision:
0:082731ede69f
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:082731ede69f 1 /**
JMF 0:082731ede69f 2 * copyright (c) 2018, James Flynn
JMF 0:082731ede69f 3 * SPDX-License-Identifier: Apache-2.0
JMF 0:082731ede69f 4 */
JMF 0:082731ede69f 5
JMF 0:082731ede69f 6 /*
JMF 0:082731ede69f 7 * Licensed under the Apache License, Version 2.0 (the "License");
JMF 0:082731ede69f 8 * you may not use this file except in compliance with the License.
JMF 0:082731ede69f 9 * You may obtain a copy of the License at
JMF 0:082731ede69f 10 *
JMF 0:082731ede69f 11 * http://www.apache.org/licenses/LICENSE-2.0
JMF 0:082731ede69f 12 *
JMF 0:082731ede69f 13 * Unless required by applicable law or agreed to in writing, software
JMF 0:082731ede69f 14 * distributed under the License is distributed on an "AS IS" BASIS,
JMF 0:082731ede69f 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JMF 0:082731ede69f 16 *
JMF 0:082731ede69f 17 * See the License for the specific language governing permissions and
JMF 0:082731ede69f 18 * limitations under the License.
JMF 0:082731ede69f 19 *
JMF 0:082731ede69f 20 */
JMF 0:082731ede69f 21
JMF 0:082731ede69f 22 /**
JMF 0:082731ede69f 23 * @file BG96Interface.h
JMF 0:082731ede69f 24 * @brief Implements NetworkInterface class for use with the Quectel BG96
JMF 0:082731ede69f 25 * data module running MBed OS v5.x
JMF 0:082731ede69f 26 *
JMF 0:082731ede69f 27 * @author James Flynn
JMF 0:082731ede69f 28 *
JMF 0:082731ede69f 29 * @date 1-April-2018
JMF 0:082731ede69f 30 */
JMF 0:082731ede69f 31
JMF 0:082731ede69f 32 #ifndef __BG96Interface_H__
JMF 0:082731ede69f 33 #define __BG96Interface_H__
JMF 0:082731ede69f 34
JMF 0:082731ede69f 35 #include <stdint.h>
JMF 0:082731ede69f 36
JMF 0:082731ede69f 37 #include "mbed.h"
JMF 0:082731ede69f 38 #include "Callback.h"
JMF 0:082731ede69f 39
JMF 0:082731ede69f 40 #include "BG96.h"
JMF 0:082731ede69f 41
JMF 0:082731ede69f 42 #define APN_DEFAULT "m2m.com.attz"
JMF 0:082731ede69f 43 #define BG96_MISC_TIMEOUT 15000
JMF 0:082731ede69f 44 #define BG96_SOCKET_COUNT 5
JMF 0:082731ede69f 45
JMF 0:082731ede69f 46 #define DBGMSG_DRV 0x04
JMF 0:082731ede69f 47 #define DBGMSG_EQ 0x08
JMF 0:082731ede69f 48 #define DBGMSG_ARRY 0x20
JMF 0:082731ede69f 49
JMF 0:082731ede69f 50 #define FIRMWARE_REV(x) (((BG96Interface*)x)->getRevision())
JMF 0:082731ede69f 51
JMF 0:082731ede69f 52 typedef struct rx_event_t {
JMF 0:082731ede69f 53 int m_rx_state; //state of the socket receive
JMF 0:082731ede69f 54 int m_rx_socketID; //which socket is being rcvd on
JMF 0:082731ede69f 55 uint8_t *m_rx_dptr; //pointer to the users data buffer
JMF 0:082731ede69f 56 uint32_t m_rx_req_size; //Requested number of bytes to receive
JMF 0:082731ede69f 57 uint32_t m_rx_asked_size;
JMF 0:082731ede69f 58 uint32_t m_rx_total_cnt; //Total number of bytes received
JMF 0:082731ede69f 59 int m_rx_timer; //Rx Timeout Timer
JMF 0:082731ede69f 60 int m_rx_disTO; //Flag to disable Timeout Timer
JMF 0:082731ede69f 61 void (*m_rx_callback)(void*);//callback used with attach
JMF 0:082731ede69f 62 void *m_rx_cb_data; //callback data to be returned
JMF 0:082731ede69f 63 uint32_t m_rx_return_cnt; //number of bytes the Event Queue is returning
JMF 0:082731ede69f 64 } RXEVENT;
JMF 0:082731ede69f 65
JMF 0:082731ede69f 66 typedef struct tx_event_t {
JMF 0:082731ede69f 67 int m_tx_state;
JMF 0:082731ede69f 68 int m_tx_socketID;
JMF 0:082731ede69f 69 uint8_t *m_tx_dptr;
JMF 0:082731ede69f 70 unsigned m_tx_orig_size;
JMF 0:082731ede69f 71 uint32_t m_tx_req_size;
JMF 0:082731ede69f 72 uint32_t m_tx_total_sent;
JMF 0:082731ede69f 73 void (*m_tx_callback)(void*);
JMF 0:082731ede69f 74 void *m_tx_cb_data;
JMF 0:082731ede69f 75 } TXEVENT;
JMF 0:082731ede69f 76
JMF 0:082731ede69f 77
JMF 0:082731ede69f 78 /** BG96_socket class
JMF 0:082731ede69f 79 * Implementation of BG96 socket structure
JMF 0:082731ede69f 80 */
JMF 0:082731ede69f 81 typedef struct _socket_t {
JMF 0:082731ede69f 82 int id; //nbr given by BG96 driver or -1 if not used
JMF 0:082731ede69f 83 SocketAddress addr; //address this socket is attached to
JMF 0:082731ede69f 84 bool disTO; //true of socket is listening for incomming data
JMF 0:082731ede69f 85 nsapi_protocol_t proto; //TCP or UDP
JMF 0:082731ede69f 86 bool connected; //true if socket is connected
JMF 0:082731ede69f 87 void (*_callback)(void*); //callback used with attach
JMF 0:082731ede69f 88 void *_data; //callback data to be returned
JMF 0:082731ede69f 89 void *dptr_last; //pointer to the last data buffer used
JMF 0:082731ede69f 90 unsigned dptr_size; //the size of the last user data buffer
JMF 0:082731ede69f 91 } BG96SOCKET;
JMF 0:082731ede69f 92
JMF 0:082731ede69f 93
JMF 0:082731ede69f 94 class BG96Interface : public NetworkStack, public NetworkInterface
JMF 0:082731ede69f 95 {
JMF 0:082731ede69f 96 public:
JMF 0:082731ede69f 97 BG96Interface();
JMF 0:082731ede69f 98 virtual ~BG96Interface();
JMF 0:082731ede69f 99
JMF 0:082731ede69f 100 /** Connect to the network (no parameters)
JMF 0:082731ede69f 101 *
JMF 0:082731ede69f 102 * @return nsapi_error_t
JMF 0:082731ede69f 103 */
JMF 0:082731ede69f 104 virtual nsapi_error_t connect(void);
JMF 0:082731ede69f 105
JMF 0:082731ede69f 106 /** Connect to the network
JMF 0:082731ede69f 107 *
JMF 0:082731ede69f 108 * @param apn Optional, APN of network
JMF 0:082731ede69f 109 * @param user Optional, username --not used--
JMF 0:082731ede69f 110 * @param pass Optional, password --not used--
JMF 0:082731ede69f 111 * @return nsapi_error_t
JMF 0:082731ede69f 112 */
JMF 0:082731ede69f 113 virtual nsapi_error_t connect(const char *apn, const char *username = 0, const char *password = 0);
JMF 0:082731ede69f 114
JMF 0:082731ede69f 115 /** Set the cellular network credentials
JMF 0:082731ede69f 116 *
JMF 0:082731ede69f 117 * @param apn Optional, APN of network
JMF 0:082731ede69f 118 * @param user Optional, username --not used--
JMF 0:082731ede69f 119 * @param pass Optional, password --not used--
JMF 0:082731ede69f 120 * @return nsapi_error_t
JMF 0:082731ede69f 121 */
JMF 0:082731ede69f 122 virtual nsapi_error_t set_credentials(const char *apn = 0,
JMF 0:082731ede69f 123 const char *username = 0, const char *password = 0);
JMF 0:082731ede69f 124
JMF 0:082731ede69f 125 /** disconnect from the network
JMF 0:082731ede69f 126 *
JMF 0:082731ede69f 127 * @return nsapi_error_t
JMF 0:082731ede69f 128 */
JMF 0:082731ede69f 129 virtual nsapi_error_t disconnect();
JMF 0:082731ede69f 130
JMF 0:082731ede69f 131 /** Get the IP address of WNC device. From NetworkStack Class
JMF 0:082731ede69f 132 *
JMF 0:082731ede69f 133 * @return IP address string or null
JMF 0:082731ede69f 134 */
JMF 0:082731ede69f 135 virtual const char *get_ip_address();
JMF 0:082731ede69f 136
JMF 0:082731ede69f 137 /** Get the MAC address of the WNC device.
JMF 0:082731ede69f 138 *
JMF 0:082731ede69f 139 * @return MAC address of the interface
JMF 0:082731ede69f 140 */
JMF 0:082731ede69f 141 virtual const char *get_mac_address();
JMF 0:082731ede69f 142
JMF 0:082731ede69f 143 /** Query Module SW revision
JMF 0:082731ede69f 144 *
JMF 0:082731ede69f 145 * @return SW Revision string
JMF 0:082731ede69f 146 */
JMF 0:082731ede69f 147 const char* getRevision(void);
JMF 0:082731ede69f 148
JMF 0:082731ede69f 149 /** Query registered state
JMF 0:082731ede69f 150 *
JMF 0:082731ede69f 151 * @return true if registerd, false if not
JMF 0:082731ede69f 152 */
JMF 0:082731ede69f 153 bool registered();
JMF 0:082731ede69f 154
JMF 0:082731ede69f 155 /** Set the level of Debug output
JMF 0:082731ede69f 156 *
JMF 0:082731ede69f 157 * @param bit field
JMF 0:082731ede69f 158 * mbed driver info = 0x04
JMF 0:082731ede69f 159 * dump buffers = 0x20
JMF 0:082731ede69f 160 * AT command tracing = 0x80
JMF 0:082731ede69f 161 */
JMF 0:082731ede69f 162 void doDebug( int v );
JMF 0:082731ede69f 163
JMF 0:082731ede69f 164
JMF 0:082731ede69f 165 protected:
JMF 0:082731ede69f 166
JMF 0:082731ede69f 167 /** Get Host IP by name.
JMF 0:082731ede69f 168 *
JMF 0:082731ede69f 169 * @return nsapi_error_t
JMF 0:082731ede69f 170 */
JMF 0:082731ede69f 171 virtual nsapi_error_t gethostbyname(const char* name, SocketAddress *address, nsapi_version_t version);
JMF 0:082731ede69f 172
JMF 0:082731ede69f 173
JMF 0:082731ede69f 174 /** return a pointer to the NetworkStack object
JMF 0:082731ede69f 175 *
JMF 0:082731ede69f 176 * @return The underlying NetworkStack object
JMF 0:082731ede69f 177 */
JMF 0:082731ede69f 178 virtual NetworkStack *get_stack(void);
JMF 0:082731ede69f 179
JMF 0:082731ede69f 180 /** Open a socket.
JMF 0:082731ede69f 181 *
JMF 0:082731ede69f 182 * @param handle Handle in which to store new socket
JMF 0:082731ede69f 183 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
JMF 0:082731ede69f 184 * @return nsapi_error_t
JMF 0:082731ede69f 185 */
JMF 0:082731ede69f 186 virtual int socket_open(void **handle, nsapi_protocol_t proto);
JMF 0:082731ede69f 187
JMF 0:082731ede69f 188
JMF 0:082731ede69f 189 /* setsockopt allows applications to pass stack-specific hints
JMF 0:082731ede69f 190 * to the underlying stack. For unsupported options,
JMF 0:082731ede69f 191 * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
JMF 0:082731ede69f 192 *
JMF 0:082731ede69f 193 * @param handle Socket handle
JMF 0:082731ede69f 194 * @param level Stack-specific protocol level
JMF 0:082731ede69f 195 * @param optname Stack-specific option identifier
JMF 0:082731ede69f 196 * @param optval Option value
JMF 0:082731ede69f 197 * @param optlen Length of the option value
JMF 0:082731ede69f 198 * @return nsapi_error_t
JMF 0:082731ede69f 199 */
JMF 0:082731ede69f 200 virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level, int optname, const void *optval, unsigned optlen);
JMF 0:082731ede69f 201
JMF 0:082731ede69f 202
JMF 0:082731ede69f 203 /* getsockopt retrieves stack-specific options.
JMF 0:082731ede69f 204 *
JMF 0:082731ede69f 205 * unsupported options return NSAPI_ERROR_UNSUPPORTED
JMF 0:082731ede69f 206 *
JMF 0:082731ede69f 207 * @param level Stack-specific protocol level or nsapi_socket_level_t
JMF 0:082731ede69f 208 * @param optname Level-specific option name
JMF 0:082731ede69f 209 * @param optval Destination for option value
JMF 0:082731ede69f 210 * @param optlen Length of the option value
JMF 0:082731ede69f 211 * @return nsapi_error_t
JMF 0:082731ede69f 212 */
JMF 0:082731ede69f 213 virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level, int optname, void *optval, unsigned *optlen);
JMF 0:082731ede69f 214
JMF 0:082731ede69f 215
JMF 0:082731ede69f 216 /** Close the socket.
JMF 0:082731ede69f 217 *
JMF 0:082731ede69f 218 * @param handle Socket handle
JMF 0:082731ede69f 219 * @return 0 on success, negative on failure
JMF 0:082731ede69f 220 */
JMF 0:082731ede69f 221 virtual int socket_close(void *handle);
JMF 0:082731ede69f 222
JMF 0:082731ede69f 223 /** Bind a server socket to a specific port.
JMF 0:082731ede69f 224 *
JMF 0:082731ede69f 225 * @brief Bind the socket to a specific port
JMF 0:082731ede69f 226 * @param handle Socket handle
JMF 0:082731ede69f 227 * @param address address to listen for
JMF 0:082731ede69f 228 * @return 0;
JMF 0:082731ede69f 229 */
JMF 0:082731ede69f 230 virtual int socket_bind(void *handle, const SocketAddress &address);
JMF 0:082731ede69f 231
JMF 0:082731ede69f 232 /** Start listening for incoming connections.
JMF 0:082731ede69f 233 *
JMF 0:082731ede69f 234 * @brief NOT SUPPORTED
JMF 0:082731ede69f 235 * @param handle Socket handle
JMF 0:082731ede69f 236 * @param backlog Number of pending connections that can be queued up at any
JMF 0:082731ede69f 237 * one time [Default: 1]
JMF 0:082731ede69f 238 * @return nsapi_error_t
JMF 0:082731ede69f 239 */
JMF 0:082731ede69f 240 virtual nsapi_error_t socket_listen(void *handle, int backlog);
JMF 0:082731ede69f 241
JMF 0:082731ede69f 242 /** Accept a new connection.
JMF 0:082731ede69f 243 *
JMF 0:082731ede69f 244 * @brief NOT SUPPORTED
JMF 0:082731ede69f 245 * @return NSAPI_ERROR_UNSUPPORTED;
JMF 0:082731ede69f 246 */
JMF 0:082731ede69f 247 virtual int socket_accept(nsapi_socket_t server,
JMF 0:082731ede69f 248 nsapi_socket_t *handle, SocketAddress *address=0);
JMF 0:082731ede69f 249
JMF 0:082731ede69f 250 /** Connects this socket to the server.
JMF 0:082731ede69f 251 *
JMF 0:082731ede69f 252 * @param handle Socket handle
JMF 0:082731ede69f 253 * @param address SocketAddress
JMF 0:082731ede69f 254 * @return nsapi_error_t
JMF 0:082731ede69f 255 */
JMF 0:082731ede69f 256 virtual int socket_connect(void *handle, const SocketAddress &address);
JMF 0:082731ede69f 257
JMF 0:082731ede69f 258 /** Send data to the remote host.
JMF 0:082731ede69f 259 *
JMF 0:082731ede69f 260 * @param handle Socket handle
JMF 0:082731ede69f 261 * @param data buffer to send
JMF 0:082731ede69f 262 * @param size length of buffer
JMF 0:082731ede69f 263 * @return Number of bytes written or negative on failure
JMF 0:082731ede69f 264 *
JMF 0:082731ede69f 265 * @note This call is non-blocking.
JMF 0:082731ede69f 266 */
JMF 0:082731ede69f 267 virtual int socket_send(void *handle, const void *data, unsigned size);
JMF 0:082731ede69f 268
JMF 0:082731ede69f 269 /** Receive data from the remote host.
JMF 0:082731ede69f 270 *
JMF 0:082731ede69f 271 * @param handle Socket handle
JMF 0:082731ede69f 272 * @param data buffer to store the recived data
JMF 0:082731ede69f 273 * @param size bytes to receive
JMF 0:082731ede69f 274 * @return received bytes received, negative on failure
JMF 0:082731ede69f 275 *
JMF 0:082731ede69f 276 * @note This call is non-blocking.
JMF 0:082731ede69f 277 */
JMF 0:082731ede69f 278 virtual int socket_recv(void *handle, void *data, unsigned size);
JMF 0:082731ede69f 279
JMF 0:082731ede69f 280 /** Send a packet to a remote endpoint.
JMF 0:082731ede69f 281 *
JMF 0:082731ede69f 282 * @param handle Socket handle
JMF 0:082731ede69f 283 * @param address SocketAddress
JMF 0:082731ede69f 284 * @param data data to send
JMF 0:082731ede69f 285 * @param size number of bytes to send
JMF 0:082731ede69f 286 * @return the number of bytes sent or negative on failure
JMF 0:082731ede69f 287 *
JMF 0:082731ede69f 288 * @note This call is non-blocking
JMF 0:082731ede69f 289 */
JMF 0:082731ede69f 290 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
JMF 0:082731ede69f 291
JMF 0:082731ede69f 292 /** Receive packet remote endpoint
JMF 0:082731ede69f 293 *
JMF 0:082731ede69f 294 * @param handle Socket handle
JMF 0:082731ede69f 295 * @param address SocketAddress
JMF 0:082731ede69f 296 * @param buffer buffer to store data to
JMF 0:082731ede69f 297 * @param size number of bytes to receive
JMF 0:082731ede69f 298 * @return the number bytes received or negative on failure
JMF 0:082731ede69f 299 *
JMF 0:082731ede69f 300 * @note This call is non-blocking
JMF 0:082731ede69f 301 */
JMF 0:082731ede69f 302 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
JMF 0:082731ede69f 303
JMF 0:082731ede69f 304 /** Register a callback on state change of the socket
JMF 0:082731ede69f 305 *
JMF 0:082731ede69f 306 * @param handle Socket handle
JMF 0:082731ede69f 307 * @param callback Function to call on state change
JMF 0:082731ede69f 308 * @param data Argument to pass to callback
JMF 0:082731ede69f 309 *
JMF 0:082731ede69f 310 * @note Callback may be called in an interrupt context.
JMF 0:082731ede69f 311 */
JMF 0:082731ede69f 312 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
JMF 0:082731ede69f 313
JMF 0:082731ede69f 314 private:
JMF 0:082731ede69f 315
JMF 0:082731ede69f 316 int tx_event(TXEVENT *ptr); //called to TX data
JMF 0:082731ede69f 317 int rx_event(RXEVENT *ptr); //called to RX data
JMF 0:082731ede69f 318 void g_eq_event(void); //event queue to tx/rx
JMF 0:082731ede69f 319 void _eq_schedule(void);
JMF 0:082731ede69f 320
JMF 0:082731ede69f 321 nsapi_error_t g_isInitialized; //TRUE if the BG96Interface is connected to the network
JMF 0:082731ede69f 322 int g_bg96_queue_id; //the ID of the EventQueue used by the driver
JMF 0:082731ede69f 323 uint32_t scheduled_events;
JMF 0:082731ede69f 324
JMF 0:082731ede69f 325 BG96SOCKET g_sock[BG96_SOCKET_COUNT]; //
JMF 0:082731ede69f 326 TXEVENT g_socTx[BG96_SOCKET_COUNT]; //
JMF 0:082731ede69f 327 RXEVENT g_socRx[BG96_SOCKET_COUNT]; //
JMF 0:082731ede69f 328
JMF 0:082731ede69f 329 Thread _bg96_monitor; //event queue thread
JMF 0:082731ede69f 330 EventQueue _bg96_queue;
JMF 0:082731ede69f 331
JMF 0:082731ede69f 332 Mutex gvupdate_mutex; //protect global variable updates
JMF 0:082731ede69f 333 Mutex txrx_mutex; //protect RX/TX event queue activities
JMF 0:082731ede69f 334 BG96 _BG96; //create the BG96 HW interface object
JMF 0:082731ede69f 335
JMF 0:082731ede69f 336 #if MBED_CONF_APP_BG96_DEBUG == true
JMF 0:082731ede69f 337 Mutex dbgout_mutex;
JMF 0:082731ede69f 338 int g_debug; //flag for debug settings
JMF 0:082731ede69f 339 void _dbDump_arry( const uint8_t* data, unsigned int size );
JMF 0:082731ede69f 340 void _dbOut(int, const char *format, ...);
JMF 0:082731ede69f 341 #endif
JMF 0:082731ede69f 342
JMF 0:082731ede69f 343 };
JMF 0:082731ede69f 344
JMF 0:082731ede69f 345 #endif /* __BG96Interface_H__ */
JMF 0:082731ede69f 346