Partial implementation of BlueGiga's BGAPI for use with the BLE112/3 modules over UART.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BGLib.h Source File

BGLib.h

00001 #include "mbed.h"
00002 #include "callbacks.h"
00003 
00004 /** A class which can communicate with a BlueGiga BLE112(3) over a UART Connection. Note that all communication is asynchronous. Your class will have to supply callbacks for each command it intends to use.
00005 * 
00006 * Example:
00007 * @code
00008 * #include "mbed.h"
00009 * #include "BGLib.h"
00010 * 
00011 * BGLib ble112(p9, p10, p7, p8);
00012 * 
00013 * void helloCallback() {
00014 *    printf("BLE112 said hello!\r\n");
00015 * }
00016 * 
00017 * int main() {
00018 *     ble112.set_ble_rsp_system_hello(&helloCallback);
00019 *     ble112.ble_cmd_system_hello();
00020 * }
00021 * @endcode
00022 */
00023 
00024 class BGLib {
00025 
00026 
00027 public:
00028     /** Create a BGLib instance
00029     * @param tx Pin to use for UART transmission
00030     * @param rx Pin to use for UART reception
00031     * @param rts Flow control pin used for RTS.
00032     * @param cts Flow control pin used for CTS.
00033     */
00034     BGLib(PinName tx, PinName rx, PinName rts, PinName cts);
00035     
00036     /** GAP Discoverable modes */
00037     enum gap_discover_mode {
00038         gap_discover_limited = 0, ///< Discover only limited discoverable devices, that is, Slaves which have the LE Limited Discoverable Mode bit set in the Flags AD type of their advertisement packets.
00039         gap_discover_generic = 1, ///<  Discover limited and generic discoverable devices, that is, Slaves which have the LE Limited Discoverable Mode or the LE General Discoverable Mode bit set in the Flags AD type of their advertisement packets.
00040         gap_discover_observation = 2 ///< Discover all devices regardless of the Flags AD type, so also devices in non-discoverable mode will be reported to host.
00041     };
00042     
00043     /** Send a Hello command to the device. */
00044     void ble_cmd_system_hello();
00045     
00046     /** Set the callback for a Hello command. Invoked when a response arrives.
00047     * @param pCallback Function pointer to the desired callback.
00048     */
00049     void set_ble_rsp_system_hello(hello_callback_t pCallback); 
00050     
00051     /** Send a Get Info command to the device. */
00052     void ble_cmd_system_get_info();
00053     
00054     /** Set the callback for a Get Info command. Invoked when a response arrives.
00055     * @param pCallback Function pointer to the desired callback.
00056     */
00057     void set_ble_rsp_system_get_info(get_info_callback_t pCallback);
00058     
00059     /** Send a Reset command to the device.
00060     * @param args A struct containing the DFU flag.  
00061     */
00062     void ble_cmd_system_reset(ble_msg_system_reset_t args);
00063     
00064     /** Set the callback for a Boot event. Invoked when the BLE112 is powered on or reset. 
00065     * @param pCallback Function pointer to the desired callback.
00066     */
00067     void set_ble_evt_system_boot(boot_callback_t pCallback);
00068     
00069     /** Set a "timestamping" callback. This is invoked whenever the mbed receives <em>any</em> data on the UART interface.
00070     * @param pCallback Function pointer to the desired callback.
00071     */
00072     void set_timestamp_callback(timestamp_callback_t pCallback);
00073     
00074     /** Send a Discover command to the device. 
00075     * @param mode The mode that the device should do discovery in.
00076     */
00077     void ble_cmd_gap_discover(gap_discover_mode mode);
00078     
00079     /** Set the callback for a Discover command.
00080     * @param pCallback Function pointer to the desired callback.
00081     */
00082     void set_ble_rsp_gap_discover(discover_callback_t pCallback);
00083     
00084     /** Set the callback for Scan Result events.
00085     * @param pCallback Function pointer to the desired callback.
00086     */
00087     void set_ble_evt_gap_scan_result(scan_result_callback_t pCallback);
00088         
00089     
00090 private:
00091     /** Function that handles parsing responses on the UART port. */
00092     void parse();
00093     
00094     /** Function that sends a specific number of bytes via the UART port.
00095     * @attention This function doesn't do any bounds checking. Be careful with what you put in!
00096     */
00097     void send_bytes(uint8_t bytes[], int length);
00098     
00099     /** Serial Port object. */
00100     Serial mSerial;
00101     
00102     
00103     // Callbacks
00104     hello_callback_t mHelloCallback;
00105     get_info_callback_t mGetInfoCallback;
00106     boot_callback_t mBootCallback;
00107     timestamp_callback_t mTimestampCallback;
00108     discover_callback_t mDiscoverCallback;
00109     scan_result_callback_t mScanResultCallback;
00110     
00111 };