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.
Diff: BGLib.cpp
- Revision:
- 7:63daf39f20e1
- Parent:
- 6:23d9a99dcde0
--- 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); + } } }