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

Hi there! I recently started using BLE112 modules with the mbed LPC1768 MCU, and I realized there was no implementation of BlueGiga's BGAPI available for mbed. This library implements only a few commands, but if you're looking to get started, this is a good place to look.

This was developed against BGAPI v1.3.2. I make no guarantees as to how well it will work with newer revisions of the software.

Committer:
dishbreak
Date:
Tue May 19 05:49:00 2015 +0000
Revision:
6:23d9a99dcde0
Parent:
5:7c7220a316ed
Child:
7:63daf39f20e1
Fixed up documentation. Added GAP Discover command.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dishbreak 1:3336b2391c80 1 #include "mbed.h"
dishbreak 2:3ce9a31a6a7e 2 #include "callbacks.h"
dishbreak 1:3336b2391c80 3
dishbreak 2:3ce9a31a6a7e 4 /** 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.
dishbreak 2:3ce9a31a6a7e 5 *
dishbreak 2:3ce9a31a6a7e 6 * Example:
dishbreak 2:3ce9a31a6a7e 7 * @code
dishbreak 2:3ce9a31a6a7e 8 * #include "mbed.h"
dishbreak 2:3ce9a31a6a7e 9 * #include "BGLib.h"
dishbreak 2:3ce9a31a6a7e 10 *
dishbreak 2:3ce9a31a6a7e 11 * BGLib ble112(p9, p10, p7, p8);
dishbreak 2:3ce9a31a6a7e 12 *
dishbreak 2:3ce9a31a6a7e 13 * void helloCallback() {
dishbreak 6:23d9a99dcde0 14 * printf("BLE112 said hello!\r\n");
dishbreak 2:3ce9a31a6a7e 15 * }
dishbreak 2:3ce9a31a6a7e 16 *
dishbreak 2:3ce9a31a6a7e 17 * int main() {
dishbreak 2:3ce9a31a6a7e 18 * ble112.set_ble_rsp_system_hello(&helloCallback);
dishbreak 2:3ce9a31a6a7e 19 * ble112.ble_cmd_system_hello();
dishbreak 2:3ce9a31a6a7e 20 * }
dishbreak 5:7c7220a316ed 21 * @endcode
dishbreak 2:3ce9a31a6a7e 22 */
dishbreak 1:3336b2391c80 23
dishbreak 5:7c7220a316ed 24 class BGLib {
dishbreak 5:7c7220a316ed 25
dishbreak 5:7c7220a316ed 26
dishbreak 1:3336b2391c80 27 public:
dishbreak 2:3ce9a31a6a7e 28 /** Create a BGLib instance
dishbreak 2:3ce9a31a6a7e 29 * @param tx Pin to use for UART transmission
dishbreak 2:3ce9a31a6a7e 30 * @param rx Pin to use for UART reception
dishbreak 2:3ce9a31a6a7e 31 * @param rts Flow control pin used for RTS.
dishbreak 2:3ce9a31a6a7e 32 * @param cts Flow control pin used for CTS.
dishbreak 2:3ce9a31a6a7e 33 */
dishbreak 1:3336b2391c80 34 BGLib(PinName tx, PinName rx, PinName rts, PinName cts);
dishbreak 1:3336b2391c80 35
dishbreak 6:23d9a99dcde0 36 /** GAP Discoverable modes */
dishbreak 6:23d9a99dcde0 37 enum gap_discover_mode {
dishbreak 6:23d9a99dcde0 38 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.
dishbreak 6:23d9a99dcde0 39 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.
dishbreak 6:23d9a99dcde0 40 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.
dishbreak 6:23d9a99dcde0 41 };
dishbreak 6:23d9a99dcde0 42
dishbreak 2:3ce9a31a6a7e 43 /** Send a Hello command to the device. */
dishbreak 2:3ce9a31a6a7e 44 void ble_cmd_system_hello();
dishbreak 2:3ce9a31a6a7e 45
dishbreak 2:3ce9a31a6a7e 46 /** Set the callback for a Hello command. Invoked when a response arrives.
dishbreak 2:3ce9a31a6a7e 47 * @param pCallback Function pointer to the desired callback.
dishbreak 2:3ce9a31a6a7e 48 */
dishbreak 2:3ce9a31a6a7e 49 void set_ble_rsp_system_hello(hello_callback_t pCallback);
dishbreak 2:3ce9a31a6a7e 50
dishbreak 2:3ce9a31a6a7e 51 /** Send a Get Info command to the device. */
dishbreak 2:3ce9a31a6a7e 52 void ble_cmd_system_get_info();
dishbreak 2:3ce9a31a6a7e 53
dishbreak 2:3ce9a31a6a7e 54 /** Set the callback for a Get Info command. Invoked when a response arrives.
dishbreak 2:3ce9a31a6a7e 55 * @param pCallback Function pointer to the desired callback.
dishbreak 2:3ce9a31a6a7e 56 */
dishbreak 2:3ce9a31a6a7e 57 void set_ble_rsp_system_get_info(get_info_callback_t pCallback);
dishbreak 1:3336b2391c80 58
dishbreak 6:23d9a99dcde0 59 /** Send a Reset command to the device.
dishbreak 6:23d9a99dcde0 60 * @param args A struct containing the DFU flag.
dishbreak 6:23d9a99dcde0 61 */
dishbreak 3:8f43af513d87 62 void ble_cmd_system_reset(ble_msg_system_reset_t args);
dishbreak 3:8f43af513d87 63
dishbreak 3:8f43af513d87 64 /** Set the callback for a Boot event. Invoked when the BLE112 is powered on or reset.
dishbreak 3:8f43af513d87 65 * @param pCallback Function pointer to the desired callback.
dishbreak 3:8f43af513d87 66 */
dishbreak 3:8f43af513d87 67 void set_ble_evt_system_boot(boot_callback_t pCallback);
dishbreak 3:8f43af513d87 68
dishbreak 6:23d9a99dcde0 69 /** Set a "timestamping" callback. This is invoked whenever the mbed receives <em>any</em> data on the UART interface.
dishbreak 4:21eee6881dac 70 * @param pCallback Function pointer to the desired callback.
dishbreak 4:21eee6881dac 71 */
dishbreak 4:21eee6881dac 72 void set_timestamp_callback(timestamp_callback_t pCallback);
dishbreak 4:21eee6881dac 73
dishbreak 6:23d9a99dcde0 74 /** Send a Discover command to the device.
dishbreak 6:23d9a99dcde0 75 * @param mode The mode that the device should do discovery in.
dishbreak 6:23d9a99dcde0 76 */
dishbreak 6:23d9a99dcde0 77 void ble_cmd_gap_discover(gap_discover_mode mode);
dishbreak 6:23d9a99dcde0 78
dishbreak 6:23d9a99dcde0 79
dishbreak 1:3336b2391c80 80 private:
dishbreak 6:23d9a99dcde0 81 /** Function that handles parsing responses on the UART port. */
dishbreak 1:3336b2391c80 82 void parse();
dishbreak 6:23d9a99dcde0 83
dishbreak 6:23d9a99dcde0 84 /** Function that sends a specific number of bytes via the UART port.
dishbreak 6:23d9a99dcde0 85 * @attention This function doesn't do any bounds checking. Be careful with what you put in!
dishbreak 6:23d9a99dcde0 86 */
dishbreak 1:3336b2391c80 87 void send_bytes(uint8_t bytes[], int length);
dishbreak 3:8f43af513d87 88
dishbreak 6:23d9a99dcde0 89 /** Serial Port object. */
dishbreak 1:3336b2391c80 90 Serial mSerial;
dishbreak 2:3ce9a31a6a7e 91
dishbreak 6:23d9a99dcde0 92
dishbreak 6:23d9a99dcde0 93 // Callbacks
dishbreak 1:3336b2391c80 94 hello_callback_t mHelloCallback;
dishbreak 2:3ce9a31a6a7e 95 get_info_callback_t mGetInfoCallback;
dishbreak 3:8f43af513d87 96 boot_callback_t mBootCallback;
dishbreak 4:21eee6881dac 97 timestamp_callback_t mTimestampCallback;
dishbreak 1:3336b2391c80 98
dishbreak 1:3336b2391c80 99 };