Jim Flynn / Mbed OS aws-iot-device-sdk-mbed-c
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BG96.h Source File

BG96.h

Go to the documentation of this file.
00001 /**
00002 * copyright (c) 2018, James Flynn
00003 * SPDX-License-Identifier: Apache-2.0
00004 */
00005 
00006 /* 
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *
00017  * See the License for the specific language governing permissions and
00018  * limitations under the License.
00019  *
00020  */
00021  
00022 /**
00023 *   @file   BG96.h
00024 *   @brief  Implements NetworkInterface class for use with the Quectel BG96
00025 *           data module running MBed OS v5.x
00026 *
00027 *   @author James Flynn
00028 *
00029 *   @date   1-April-2018
00030 */
00031 
00032 #ifndef __BG96_H__
00033 #define __BG96_H__
00034 
00035 #include "mbed.h"
00036  
00037 // If target board does not support Arduino pins, define pins as Not Connected
00038 #if defined(TARGET_FF_ARDUINO)
00039 #if !defined(MBED_CONF_BG96_LIBRARY_BG96_TX)
00040 #define MBED_CONF_BG96_LIBRARY_BG96_TX               D8
00041 #endif
00042 #if !defined(MBED_CONF_BG96_LIBRARY_BG96_RX)
00043 #define MBED_CONF_BG96_LIBRARY_BG96_RX               D2
00044 #endif
00045 #if !defined(MBED_CONF_BG96_LIBRARY_BG96_RESET)
00046 #define MBED_CONF_BG96_LIBRARY_BG96_RESET            D7
00047 #endif
00048 #if !defined(MBED_CONF_BG96_LIBRARY_BG96_WAKE)
00049 #define MBED_CONF_BG96_LIBRARY_BG96_WAKE             D11
00050 #endif
00051 #if !defined(MBED_CONF_BG96_LIBRARY_BG96_PWRKEY)
00052 #define MBED_CONF_BG96_LIBRARY_BG96_PWRKEY           D10
00053 #endif
00054 #else // !defined(TARGET_FF_ARDUINO)
00055 #define MBED_CONF_BG96_LIBRARY_BG96_TX                NC
00056 #define MBED_CONF_BG96_LIBRARY_BG96_RX                NC
00057 #define MBED_CONF_BG96_LIBRARY_BG96_RESET             NC
00058 #define MBED_CONF_BG96_LIBRARY_BG96_WAKE              NC
00059 #define MBED_CONF_BG96_LIBRARY_BG96_PWRKEY            NC
00060 #endif // !defined(TARGET_FF_ARDUINO)
00061 
00062 /** BG96Interface class.
00063     Interface to a BG96 module.
00064  */
00065 
00066 class BG96
00067 {
00068 public:
00069     static const unsigned BG96_BUFF_SIZE = 1500;  
00070     
00071     BG96(bool debug=false);
00072     ~BG96();
00073 
00074     /**
00075     * Init the BG96
00076     *
00077     * @param mode mode in which to startup
00078     * @return true only if BG96 has started up correctly
00079     */
00080     bool startup(void);
00081  
00082     /**
00083     * Wait for 'RDY' signal or timeout waiting...
00084     *
00085     * @return none.
00086     */
00087     void waitBG96Ready(void);
00088 
00089     /**
00090     * Reset BG96
00091     *
00092     * @return true if BG96 resets successfully
00093     */
00094     void reset(void);
00095     
00096     /**
00097     * Connect BG96 to APN
00098     *
00099     * @param apn the name of the APN
00100     * @param username (not used)
00101     * @param password (not used)
00102     * @return nsapi_error_t
00103     */
00104     nsapi_error_t connect(const char *apn, const char *username, const char *password);
00105  
00106     /**
00107     * Disconnect BG96 from AP
00108     *
00109     * @return true if BG96 is disconnected successfully
00110     */
00111     bool disconnect(void);
00112  
00113     /**
00114     * Get the IP address of BG96
00115     *
00116     * @return null-teriminated IP address or null if no IP address is assigned
00117     */
00118     const char *getIPAddress(char*);
00119  
00120     /**
00121     * Get the MAC address of BG96
00122     *
00123     * @return null-terminated MAC address or null if no MAC address is assigned
00124     */
00125     const char *getMACAddress(char*);
00126  
00127     /**
00128     * Check if BG96 is conenected
00129     *
00130     * @return true only if the chip has an IP address
00131     */
00132     bool isConnected(void);
00133  
00134     /**
00135     * Open a socketed connection
00136     *
00137     * @param type the type of socket to open "u" (UDP) or "t" (TCP)
00138     * @param id for saving socket number to (returned by BG96)
00139     * @param port port to open connection with
00140     * @param addr the IP address of the destination
00141     * @return true only if socket opened successfully
00142     */
00143     bool open(const char type, int id, const char* addr, int port);
00144  
00145     /**
00146     * Sends data to an open socket
00147     *
00148     * @param id of socket to send to
00149     * @param data to be sent
00150     * @param amount of data to be sent 
00151     * @return true only if data sent successfully
00152     */
00153     bool send(int id, const void *data, uint32_t amount);
00154  
00155     /**
00156     * Receives data from an open socket
00157     *
00158     * @param id to receive from
00159     * @param pointer to data for returned information
00160     * @param amount number of bytes to be received
00161     * @return the number of bytes received
00162     */
00163     int32_t recv(int, void *, uint32_t);
00164  
00165     /**
00166     * Closes a socket
00167     *
00168     * @param id id of socket to close, valid only 0-4
00169     * @return true only if socket is closed successfully
00170     */
00171     bool close(int id);
00172  
00173     /**
00174     * Checks if data is available
00175     */
00176     bool readable();
00177  
00178     /**
00179     * Checks if data can be written
00180     */
00181     bool writeable();
00182     
00183     /**
00184     * Resolves a URL name to IP address
00185     */
00186     bool resolveUrl(const char *name, char* str);
00187  
00188     /*
00189     * Obtain or set the current BG96 active context
00190     */
00191     int setContext( int i );
00192     
00193     /*
00194     * enable/disable AT command tracing
00195     */
00196     void doDebug(int f);
00197     
00198     /** Return the BG96 revision info
00199      *
00200      *  @param          none.
00201      */
00202     const char* getRev(char*);
00203 
00204     /** Return the last error to occur
00205      *
00206      *  @param          char* [at least 40 long]
00207      */
00208     bool getError(char *);
00209 
00210     /** Return the amount a data available
00211      *
00212      *  @param          char* [at least 40 long]
00213      */
00214     int rxAvail(int);
00215 
00216     /** Return true/false if rx data is available 
00217      *
00218      *  @param          socket to check
00219      */
00220     bool        chkRxAvail(int id);
00221 
00222 private:
00223     bool        tx2bg96(char* cmd);
00224     bool        BG96Ready(void);
00225     bool        hw_reset(void);
00226 
00227     int         _contextID;
00228     Mutex       _bg96_mutex;
00229 
00230     UARTSerial  _serial;
00231     ATCmdParser _parser;
00232 
00233     DigitalOut _bg96_reset;
00234     DigitalOut _vbat_3v8_en;
00235     DigitalOut _bg96_pwrkey;
00236     
00237 };
00238  
00239 #endif  //__BG96_H__
00240