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.
BGLib.cpp
- Committer:
- dishbreak
- Date:
- 2015-05-19
- Revision:
- 6:23d9a99dcde0
- Parent:
- 4:21eee6881dac
- Child:
- 7:63daf39f20e1
File content as of revision 6:23d9a99dcde0:
#include "BGLib.h" BGLib::BGLib(PinName tx, PinName rx, PinName rts, PinName cts) : mSerial(tx, rx) { mSerial.set_flow_control(SerialBase::RTSCTS, rts, cts); mSerial.baud(57600); mSerial.attach(this, &BGLib::parse); } void BGLib::set_ble_rsp_system_hello(hello_callback_t pCallback) { mHelloCallback = pCallback; } void BGLib::ble_cmd_system_hello() { uint8_t bytes[] = {0x00, 0x00, 0x00, 0x01}; send_bytes(bytes, 4); } void BGLib::ble_cmd_system_get_info() { uint8_t bytes[] = {0x00, 0x00, 0x00, 0x08}; send_bytes(bytes, 4); } void BGLib::set_ble_rsp_system_get_info(get_info_callback_t pCallback) { mGetInfoCallback = pCallback; } void BGLib::set_ble_evt_system_boot(boot_callback_t pCallback) { mBootCallback = 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; send_bytes(bytes, 5); } void BGLib::set_timestamp_callback(timestamp_callback_t pCallback) { mTimestampCallback = pCallback; } void BGLib::ble_cmd_gap_discover(gap_discover_mode mode) { uint8_t bytes[] = {0x00, 0x01, 0x06, 0x02, 0x00}; bytes[4] = (uint8_t) mode; send_bytes(bytes, 5); } void BGLib::parse() { mTimestampCallback(); #ifdef DEBUG printf("Got data from port!\r\n"); #endif uint8_t hilen = mSerial.getc(); uint8_t lolen = mSerial.getc(); uint8_t msg_class = mSerial.getc(); uint8_t msg_id = mSerial.getc(); #ifdef DEBUG printf("Message header: %x %x %x %x\r\n", hilen, lolen, msg_class, msg_id); #endif if (hilen == 0x00) { // response if (msg_class == 0x00) { // system_class if (msg_id == 0x01) { // system_hello mHelloCallback(); } else if (msg_id == 0x08) { //system_get_info #ifdef DEBUG printf("Get Info Response\r\n"); #endif ble_msg_system_get_info_rsp_t result; result.major = mSerial.getc() | (mSerial.getc() << 8); result.minor = mSerial.getc() | (mSerial.getc() << 8); result.patch = mSerial.getc() | (mSerial.getc() << 8); result.build = mSerial.getc() | (mSerial.getc() << 8); result.ll_version = mSerial.getc() | (mSerial.getc() << 8); result.protocol_version = mSerial.getc(); result.hw = mSerial.getc(); mGetInfoCallback(result); } } } else if (hilen == 0x80) { // event if (msg_class == 0x00) { //system_class if (msg_id == 0x00) { //system_boot #ifdef DEBUG printf("Boot Event\r\n"); #endif ble_msg_system_boot_evt_t result; result.major = mSerial.getc() | (mSerial.getc() << 8); result.minor = mSerial.getc() | (mSerial.getc() << 8); result.patch = mSerial.getc() | (mSerial.getc() << 8); result.build = mSerial.getc() | (mSerial.getc() << 8); result.ll_version = mSerial.getc() | (mSerial.getc() << 8); result.protocol_version = mSerial.getc(); result.hw = mSerial.getc(); mBootCallback(result); } } } //safety valve: if there are bytes remaining } void BGLib::send_bytes(uint8_t bytes[], int length) { for (int i = 0; i < length; i++) { mSerial.putc(bytes[i]); } }