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.
Revision 7:63daf39f20e1, committed 2015-05-19
- Comitter:
- dishbreak
- Date:
- Tue May 19 07:30:40 2015 +0000
- Parent:
- 6:23d9a99dcde0
- Commit message:
- Adding Scan Result callback support.
Changed in this revision
diff -r 23d9a99dcde0 -r 63daf39f20e1 BGLib.cpp --- a/BGLib.cpp Tue May 19 05:49:00 2015 +0000 +++ b/BGLib.cpp Tue May 19 07:30:40 2015 +0000 @@ -29,6 +29,10 @@ mBootCallback = pCallback; } +void BGLib::set_ble_rsp_gap_discover(discover_callback_t pCallback) { + mDiscoverCallback = pCallback; +} + void BGLib::ble_cmd_system_reset(ble_msg_system_reset_t args) { uint8_t bytes[] = {0x00, 0x01, 0x00, 0x00, 0x00}; bytes[4] = args.boot_in_dfu; @@ -39,6 +43,10 @@ mTimestampCallback = pCallback; } +void BGLib::set_ble_evt_gap_scan_result(scan_result_callback_t pCallback) { + mScanResultCallback = pCallback; +} + void BGLib::ble_cmd_gap_discover(gap_discover_mode mode) { uint8_t bytes[] = {0x00, 0x01, 0x06, 0x02, 0x00}; bytes[4] = (uint8_t) mode; @@ -81,7 +89,13 @@ result.hw = mSerial.getc(); mGetInfoCallback(result); } - } + } else if (msg_class == 0x06) { // gap_class + if (msg_id == 0x02) { // gap_discover + ble_msg_gap_discover_rsp_t result; + result.result = mSerial.getc() | (mSerial.getc() << 8); + mDiscoverCallback(result); + } + } } else if (hilen == 0x80) { // event if (msg_class == 0x00) { //system_class if (msg_id == 0x00) { //system_boot @@ -99,6 +113,34 @@ result.hw = mSerial.getc(); mBootCallback(result); } + } else if (msg_class == 0x06) { // gap_class + if (msg_id == 0x00) { // gap_scan_result + #ifdef DEBUG + printf("Scan Result Event\r\n"); + #endif + + ble_msg_gap_scan_result_evt_t result; + + result.rssi = mSerial.getc(); + + result.packet_type = mSerial.getc(); + + for (int i = 5; i >= 0; i--) { + result.hw_addr[i] = mSerial.getc(); + } + + result.address_type = mSerial.getc(); + + result.bond = mSerial.getc(); + + result.data_len = mSerial.getc(); + + result.data = new uint8_t[result.data_len]; + for (int i = result.data_len - 1; i >= 0; i--) { + result.data[i] = mSerial.getc(); + } + mScanResultCallback(result); + } } }
diff -r 23d9a99dcde0 -r 63daf39f20e1 BGLib.h --- a/BGLib.h Tue May 19 05:49:00 2015 +0000 +++ b/BGLib.h Tue May 19 07:30:40 2015 +0000 @@ -75,6 +75,16 @@ * @param mode The mode that the device should do discovery in. */ void ble_cmd_gap_discover(gap_discover_mode mode); + + /** Set the callback for a Discover command. + * @param pCallback Function pointer to the desired callback. + */ + void set_ble_rsp_gap_discover(discover_callback_t pCallback); + + /** Set the callback for Scan Result events. + * @param pCallback Function pointer to the desired callback. + */ + void set_ble_evt_gap_scan_result(scan_result_callback_t pCallback); private: @@ -95,5 +105,7 @@ get_info_callback_t mGetInfoCallback; boot_callback_t mBootCallback; timestamp_callback_t mTimestampCallback; + discover_callback_t mDiscoverCallback; + scan_result_callback_t mScanResultCallback; }; \ No newline at end of file
diff -r 23d9a99dcde0 -r 63daf39f20e1 api_datatypes.h --- a/api_datatypes.h Tue May 19 05:49:00 2015 +0000 +++ b/api_datatypes.h Tue May 19 07:30:40 2015 +0000 @@ -36,4 +36,17 @@ uint16_t result; ///< Result. Non-zero value indicates an error. } ble_msg_gap_discover_rsp_t; + + +/** Data structure representing a scan result */ +typedef struct { + int8_t rssi; ///< RSSI value (dBm). + uint8_t packet_type; ///< Scan response header. + uint8_t hw_addr[6]; ///< BLE Hardware Address. + uint8_t address_type; ///< Address type. 1 for random, 0 for public. + uint8_t bond; ///< Bond handle if known bond for device, 0xff otherwise. + uint8_t *data; ///< Service data. + uint8_t data_len; ///< Service data length (in bytes). +} ble_msg_gap_scan_result_evt_t; + #endif \ No newline at end of file
diff -r 23d9a99dcde0 -r 63daf39f20e1 callbacks.h --- a/callbacks.h Tue May 19 05:49:00 2015 +0000 +++ b/callbacks.h Tue May 19 07:30:40 2015 +0000 @@ -31,4 +31,9 @@ */ typedef void (*discover_callback_t)(ble_msg_gap_discover_rsp_t); +/** Function pointer for Scan Result event handler. +* @param ble_msg_gap_scan_result_evt_t A data structure containing the data from a Scan Result event. +*/ +typedef void (*scan_result_callback_t)(ble_msg_gap_scan_result_evt_t); + #endif \ No newline at end of file