Partial implementation of BlueGiga's BGAPI for use with the BLE112/3 modules over UART.
Embed:
(wiki syntax)
Show/hide line numbers
BGLib.cpp
00001 #include "BGLib.h" 00002 00003 BGLib::BGLib(PinName tx, PinName rx, PinName rts, PinName cts) : 00004 mSerial(tx, rx) { 00005 mSerial.set_flow_control(SerialBase::RTSCTS, rts, cts); 00006 mSerial.baud(57600); 00007 mSerial.attach(this, &BGLib::parse); 00008 } 00009 00010 void BGLib::set_ble_rsp_system_hello(hello_callback_t pCallback) { 00011 mHelloCallback = pCallback; 00012 } 00013 00014 void BGLib::ble_cmd_system_hello() { 00015 uint8_t bytes[] = {0x00, 0x00, 0x00, 0x01}; 00016 send_bytes(bytes, 4); 00017 } 00018 00019 void BGLib::ble_cmd_system_get_info() { 00020 uint8_t bytes[] = {0x00, 0x00, 0x00, 0x08}; 00021 send_bytes(bytes, 4); 00022 } 00023 00024 void BGLib::set_ble_rsp_system_get_info(get_info_callback_t pCallback) { 00025 mGetInfoCallback = pCallback; 00026 } 00027 00028 void BGLib::set_ble_evt_system_boot(boot_callback_t pCallback) { 00029 mBootCallback = pCallback; 00030 } 00031 00032 void BGLib::set_ble_rsp_gap_discover(discover_callback_t pCallback) { 00033 mDiscoverCallback = pCallback; 00034 } 00035 00036 void BGLib::ble_cmd_system_reset(ble_msg_system_reset_t args) { 00037 uint8_t bytes[] = {0x00, 0x01, 0x00, 0x00, 0x00}; 00038 bytes[4] = args.boot_in_dfu; 00039 send_bytes(bytes, 5); 00040 } 00041 00042 void BGLib::set_timestamp_callback(timestamp_callback_t pCallback) { 00043 mTimestampCallback = pCallback; 00044 } 00045 00046 void BGLib::set_ble_evt_gap_scan_result(scan_result_callback_t pCallback) { 00047 mScanResultCallback = pCallback; 00048 } 00049 00050 void BGLib::ble_cmd_gap_discover(gap_discover_mode mode) { 00051 uint8_t bytes[] = {0x00, 0x01, 0x06, 0x02, 0x00}; 00052 bytes[4] = (uint8_t) mode; 00053 send_bytes(bytes, 5); 00054 } 00055 00056 void BGLib::parse() { 00057 mTimestampCallback(); 00058 00059 #ifdef DEBUG 00060 printf("Got data from port!\r\n"); 00061 #endif 00062 00063 uint8_t hilen = mSerial.getc(); 00064 uint8_t lolen = mSerial.getc(); 00065 uint8_t msg_class = mSerial.getc(); 00066 uint8_t msg_id = mSerial.getc(); 00067 00068 #ifdef DEBUG 00069 printf("Message header: %x %x %x %x\r\n", hilen, lolen, msg_class, msg_id); 00070 #endif 00071 00072 if (hilen == 0x00) { // response 00073 if (msg_class == 0x00) { // system_class 00074 if (msg_id == 0x01) { // system_hello 00075 mHelloCallback(); 00076 } else if (msg_id == 0x08) { //system_get_info 00077 00078 #ifdef DEBUG 00079 printf("Get Info Response\r\n"); 00080 #endif 00081 00082 ble_msg_system_get_info_rsp_t result; 00083 result.major = mSerial.getc() | (mSerial.getc() << 8); 00084 result.minor = mSerial.getc() | (mSerial.getc() << 8); 00085 result.patch = mSerial.getc() | (mSerial.getc() << 8); 00086 result.build = mSerial.getc() | (mSerial.getc() << 8); 00087 result.ll_version = mSerial.getc() | (mSerial.getc() << 8); 00088 result.protocol_version = mSerial.getc(); 00089 result.hw = mSerial.getc(); 00090 mGetInfoCallback(result); 00091 } 00092 } else if (msg_class == 0x06) { // gap_class 00093 if (msg_id == 0x02) { // gap_discover 00094 ble_msg_gap_discover_rsp_t result; 00095 result.result = mSerial.getc() | (mSerial.getc() << 8); 00096 mDiscoverCallback(result); 00097 } 00098 } 00099 } else if (hilen == 0x80) { // event 00100 if (msg_class == 0x00) { //system_class 00101 if (msg_id == 0x00) { //system_boot 00102 #ifdef DEBUG 00103 printf("Boot Event\r\n"); 00104 #endif 00105 00106 ble_msg_system_boot_evt_t result; 00107 result.major = mSerial.getc() | (mSerial.getc() << 8); 00108 result.minor = mSerial.getc() | (mSerial.getc() << 8); 00109 result.patch = mSerial.getc() | (mSerial.getc() << 8); 00110 result.build = mSerial.getc() | (mSerial.getc() << 8); 00111 result.ll_version = mSerial.getc() | (mSerial.getc() << 8); 00112 result.protocol_version = mSerial.getc(); 00113 result.hw = mSerial.getc(); 00114 mBootCallback(result); 00115 } 00116 } else if (msg_class == 0x06) { // gap_class 00117 if (msg_id == 0x00) { // gap_scan_result 00118 #ifdef DEBUG 00119 printf("Scan Result Event\r\n"); 00120 #endif 00121 00122 ble_msg_gap_scan_result_evt_t result; 00123 00124 result.rssi = mSerial.getc(); 00125 00126 result.packet_type = mSerial.getc(); 00127 00128 for (int i = 5; i >= 0; i--) { 00129 result.hw_addr[i] = mSerial.getc(); 00130 } 00131 00132 result.address_type = mSerial.getc(); 00133 00134 result.bond = mSerial.getc(); 00135 00136 result.data_len = mSerial.getc(); 00137 00138 result.data = new uint8_t[result.data_len]; 00139 for (int i = result.data_len - 1; i >= 0; i--) { 00140 result.data[i] = mSerial.getc(); 00141 } 00142 mScanResultCallback(result); 00143 } 00144 } 00145 } 00146 00147 00148 //safety valve: if there are bytes remaining 00149 } 00150 00151 void BGLib::send_bytes(uint8_t bytes[], int length) { 00152 for (int i = 0; i < length; i++) { 00153 mSerial.putc(bytes[i]); 00154 } 00155 }
Generated on Tue Jul 12 2022 20:21:19 by 1.7.2