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 BG96.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 __BG96_H__
JMF 0:082731ede69f 33 #define __BG96_H__
JMF 0:082731ede69f 34
JMF 0:082731ede69f 35 #include "mbed.h"
JMF 0:082731ede69f 36
JMF 0:082731ede69f 37 // If target board does not support Arduino pins, define pins as Not Connected
JMF 0:082731ede69f 38 #if defined(TARGET_FF_ARDUINO)
JMF 0:082731ede69f 39 #if !defined(MBED_CONF_BG96_LIBRARY_BG96_TX)
JMF 0:082731ede69f 40 #define MBED_CONF_BG96_LIBRARY_BG96_TX D8
JMF 0:082731ede69f 41 #endif
JMF 0:082731ede69f 42 #if !defined(MBED_CONF_BG96_LIBRARY_BG96_RX)
JMF 0:082731ede69f 43 #define MBED_CONF_BG96_LIBRARY_BG96_RX D2
JMF 0:082731ede69f 44 #endif
JMF 0:082731ede69f 45 #if !defined(MBED_CONF_BG96_LIBRARY_BG96_RESET)
JMF 0:082731ede69f 46 #define MBED_CONF_BG96_LIBRARY_BG96_RESET D7
JMF 0:082731ede69f 47 #endif
JMF 0:082731ede69f 48 #if !defined(MBED_CONF_BG96_LIBRARY_BG96_WAKE)
JMF 0:082731ede69f 49 #define MBED_CONF_BG96_LIBRARY_BG96_WAKE D11
JMF 0:082731ede69f 50 #endif
JMF 0:082731ede69f 51 #if !defined(MBED_CONF_BG96_LIBRARY_BG96_PWRKEY)
JMF 0:082731ede69f 52 #define MBED_CONF_BG96_LIBRARY_BG96_PWRKEY D10
JMF 0:082731ede69f 53 #endif
JMF 0:082731ede69f 54 #else // !defined(TARGET_FF_ARDUINO)
JMF 0:082731ede69f 55 #define MBED_CONF_BG96_LIBRARY_BG96_TX NC
JMF 0:082731ede69f 56 #define MBED_CONF_BG96_LIBRARY_BG96_RX NC
JMF 0:082731ede69f 57 #define MBED_CONF_BG96_LIBRARY_BG96_RESET NC
JMF 0:082731ede69f 58 #define MBED_CONF_BG96_LIBRARY_BG96_WAKE NC
JMF 0:082731ede69f 59 #define MBED_CONF_BG96_LIBRARY_BG96_PWRKEY NC
JMF 0:082731ede69f 60 #endif // !defined(TARGET_FF_ARDUINO)
JMF 0:082731ede69f 61
JMF 0:082731ede69f 62 /** BG96Interface class.
JMF 0:082731ede69f 63 Interface to a BG96 module.
JMF 0:082731ede69f 64 */
JMF 0:082731ede69f 65
JMF 0:082731ede69f 66 class BG96
JMF 0:082731ede69f 67 {
JMF 0:082731ede69f 68 public:
JMF 0:082731ede69f 69 static const unsigned BG96_BUFF_SIZE = 1500;
JMF 0:082731ede69f 70
JMF 0:082731ede69f 71 BG96(bool debug=false);
JMF 0:082731ede69f 72 ~BG96();
JMF 0:082731ede69f 73
JMF 0:082731ede69f 74 /**
JMF 0:082731ede69f 75 * Init the BG96
JMF 0:082731ede69f 76 *
JMF 0:082731ede69f 77 * @param mode mode in which to startup
JMF 0:082731ede69f 78 * @return true only if BG96 has started up correctly
JMF 0:082731ede69f 79 */
JMF 0:082731ede69f 80 bool startup(void);
JMF 0:082731ede69f 81
JMF 0:082731ede69f 82 /**
JMF 0:082731ede69f 83 * Wait for 'RDY' signal or timeout waiting...
JMF 0:082731ede69f 84 *
JMF 0:082731ede69f 85 * @return none.
JMF 0:082731ede69f 86 */
JMF 0:082731ede69f 87 void waitBG96Ready(void);
JMF 0:082731ede69f 88
JMF 0:082731ede69f 89 /**
JMF 0:082731ede69f 90 * Reset BG96
JMF 0:082731ede69f 91 *
JMF 0:082731ede69f 92 * @return true if BG96 resets successfully
JMF 0:082731ede69f 93 */
JMF 0:082731ede69f 94 void reset(void);
JMF 0:082731ede69f 95
JMF 0:082731ede69f 96 /**
JMF 0:082731ede69f 97 * Connect BG96 to APN
JMF 0:082731ede69f 98 *
JMF 0:082731ede69f 99 * @param apn the name of the APN
JMF 0:082731ede69f 100 * @param username (not used)
JMF 0:082731ede69f 101 * @param password (not used)
JMF 0:082731ede69f 102 * @return nsapi_error_t
JMF 0:082731ede69f 103 */
JMF 0:082731ede69f 104 nsapi_error_t connect(const char *apn, const char *username, const char *password);
JMF 0:082731ede69f 105
JMF 0:082731ede69f 106 /**
JMF 0:082731ede69f 107 * Disconnect BG96 from AP
JMF 0:082731ede69f 108 *
JMF 0:082731ede69f 109 * @return true if BG96 is disconnected successfully
JMF 0:082731ede69f 110 */
JMF 0:082731ede69f 111 bool disconnect(void);
JMF 0:082731ede69f 112
JMF 0:082731ede69f 113 /**
JMF 0:082731ede69f 114 * Get the IP address of BG96
JMF 0:082731ede69f 115 *
JMF 0:082731ede69f 116 * @return null-teriminated IP address or null if no IP address is assigned
JMF 0:082731ede69f 117 */
JMF 0:082731ede69f 118 const char *getIPAddress(char*);
JMF 0:082731ede69f 119
JMF 0:082731ede69f 120 /**
JMF 0:082731ede69f 121 * Get the MAC address of BG96
JMF 0:082731ede69f 122 *
JMF 0:082731ede69f 123 * @return null-terminated MAC address or null if no MAC address is assigned
JMF 0:082731ede69f 124 */
JMF 0:082731ede69f 125 const char *getMACAddress(char*);
JMF 0:082731ede69f 126
JMF 0:082731ede69f 127 /**
JMF 0:082731ede69f 128 * Check if BG96 is conenected
JMF 0:082731ede69f 129 *
JMF 0:082731ede69f 130 * @return true only if the chip has an IP address
JMF 0:082731ede69f 131 */
JMF 0:082731ede69f 132 bool isConnected(void);
JMF 0:082731ede69f 133
JMF 0:082731ede69f 134 /**
JMF 0:082731ede69f 135 * Open a socketed connection
JMF 0:082731ede69f 136 *
JMF 0:082731ede69f 137 * @param type the type of socket to open "u" (UDP) or "t" (TCP)
JMF 0:082731ede69f 138 * @param id for saving socket number to (returned by BG96)
JMF 0:082731ede69f 139 * @param port port to open connection with
JMF 0:082731ede69f 140 * @param addr the IP address of the destination
JMF 0:082731ede69f 141 * @return true only if socket opened successfully
JMF 0:082731ede69f 142 */
JMF 0:082731ede69f 143 bool open(const char type, int id, const char* addr, int port);
JMF 0:082731ede69f 144
JMF 0:082731ede69f 145 /**
JMF 0:082731ede69f 146 * Sends data to an open socket
JMF 0:082731ede69f 147 *
JMF 0:082731ede69f 148 * @param id of socket to send to
JMF 0:082731ede69f 149 * @param data to be sent
JMF 0:082731ede69f 150 * @param amount of data to be sent
JMF 0:082731ede69f 151 * @return true only if data sent successfully
JMF 0:082731ede69f 152 */
JMF 0:082731ede69f 153 bool send(int id, const void *data, uint32_t amount);
JMF 0:082731ede69f 154
JMF 0:082731ede69f 155 /**
JMF 0:082731ede69f 156 * Receives data from an open socket
JMF 0:082731ede69f 157 *
JMF 0:082731ede69f 158 * @param id to receive from
JMF 0:082731ede69f 159 * @param pointer to data for returned information
JMF 0:082731ede69f 160 * @param amount number of bytes to be received
JMF 0:082731ede69f 161 * @return the number of bytes received
JMF 0:082731ede69f 162 */
JMF 0:082731ede69f 163 int32_t recv(int, void *, uint32_t);
JMF 0:082731ede69f 164
JMF 0:082731ede69f 165 /**
JMF 0:082731ede69f 166 * Closes a socket
JMF 0:082731ede69f 167 *
JMF 0:082731ede69f 168 * @param id id of socket to close, valid only 0-4
JMF 0:082731ede69f 169 * @return true only if socket is closed successfully
JMF 0:082731ede69f 170 */
JMF 0:082731ede69f 171 bool close(int id);
JMF 0:082731ede69f 172
JMF 0:082731ede69f 173 /**
JMF 0:082731ede69f 174 * Checks if data is available
JMF 0:082731ede69f 175 */
JMF 0:082731ede69f 176 bool readable();
JMF 0:082731ede69f 177
JMF 0:082731ede69f 178 /**
JMF 0:082731ede69f 179 * Checks if data can be written
JMF 0:082731ede69f 180 */
JMF 0:082731ede69f 181 bool writeable();
JMF 0:082731ede69f 182
JMF 0:082731ede69f 183 /**
JMF 0:082731ede69f 184 * Resolves a URL name to IP address
JMF 0:082731ede69f 185 */
JMF 0:082731ede69f 186 bool resolveUrl(const char *name, char* str);
JMF 0:082731ede69f 187
JMF 0:082731ede69f 188 /*
JMF 0:082731ede69f 189 * Obtain or set the current BG96 active context
JMF 0:082731ede69f 190 */
JMF 0:082731ede69f 191 int setContext( int i );
JMF 0:082731ede69f 192
JMF 0:082731ede69f 193 /*
JMF 0:082731ede69f 194 * enable/disable AT command tracing
JMF 0:082731ede69f 195 */
JMF 0:082731ede69f 196 void doDebug(int f);
JMF 0:082731ede69f 197
JMF 0:082731ede69f 198 /** Return the BG96 revision info
JMF 0:082731ede69f 199 *
JMF 0:082731ede69f 200 * @param none.
JMF 0:082731ede69f 201 */
JMF 0:082731ede69f 202 const char* getRev(char*);
JMF 0:082731ede69f 203
JMF 0:082731ede69f 204 /** Return the last error to occur
JMF 0:082731ede69f 205 *
JMF 0:082731ede69f 206 * @param char* [at least 40 long]
JMF 0:082731ede69f 207 */
JMF 0:082731ede69f 208 bool getError(char *);
JMF 0:082731ede69f 209
JMF 0:082731ede69f 210 /** Return the amount a data available
JMF 0:082731ede69f 211 *
JMF 0:082731ede69f 212 * @param char* [at least 40 long]
JMF 0:082731ede69f 213 */
JMF 0:082731ede69f 214 int rxAvail(int);
JMF 0:082731ede69f 215
JMF 0:082731ede69f 216 /** Return true/false if rx data is available
JMF 0:082731ede69f 217 *
JMF 0:082731ede69f 218 * @param socket to check
JMF 0:082731ede69f 219 */
JMF 0:082731ede69f 220 bool chkRxAvail(int id);
JMF 0:082731ede69f 221
JMF 0:082731ede69f 222 private:
JMF 0:082731ede69f 223 bool tx2bg96(char* cmd);
JMF 0:082731ede69f 224 bool BG96Ready(void);
JMF 0:082731ede69f 225 bool hw_reset(void);
JMF 0:082731ede69f 226
JMF 0:082731ede69f 227 int _contextID;
JMF 0:082731ede69f 228 Mutex _bg96_mutex;
JMF 0:082731ede69f 229
JMF 0:082731ede69f 230 UARTSerial _serial;
JMF 0:082731ede69f 231 ATCmdParser _parser;
JMF 0:082731ede69f 232
JMF 0:082731ede69f 233 DigitalOut _bg96_reset;
JMF 0:082731ede69f 234 DigitalOut _vbat_3v8_en;
JMF 0:082731ede69f 235 DigitalOut _bg96_pwrkey;
JMF 0:082731ede69f 236
JMF 0:082731ede69f 237 };
JMF 0:082731ede69f 238
JMF 0:082731ede69f 239 #endif //__BG96_H__
JMF 0:082731ede69f 240