#include "Protocol.h"
#include <cstring>
#include "protocol/simpleble.pb.h"
using namespace SimpleBLE::Dongl::Serial;
Protocol::Protocol(const std::string& device_path) : ProtocolBase(device_path) {}
Protocol::~Protocol() {}
basic_WhoamiRsp Protocol::basic_whoami() {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_basic_tag;
command.cmd.basic.which_cmd = basic_Command_whoami_tag;
basic_WhoamiCmd whoami_cmd = basic_WhoamiCmd_init_default;
command.cmd.basic.cmd.whoami = whoami_cmd;
dongl_Response response = exchange(command);
return response.rsp.basic.rsp.whoami;
}
basic_ResetRsp Protocol::basic_reset() {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_basic_tag;
command.cmd.basic.which_cmd = basic_Command_reset_tag;
basic_ResetCmd reset_cmd = basic_ResetCmd_init_default;
command.cmd.basic.cmd.reset = reset_cmd;
dongl_Response response = exchange(command);
return response.rsp.basic.rsp.reset;
}
basic_PowerOnRsp Protocol::basic_power_on() {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_basic_tag;
command.cmd.basic.which_cmd = basic_Command_power_on_tag;
basic_PowerOnCmd power_on_cmd = basic_PowerOnCmd_init_default;
command.cmd.basic.cmd.power_on = power_on_cmd;
dongl_Response response = exchange(command);
return response.rsp.basic.rsp.power_on;
}
basic_PowerOffRsp Protocol::basic_power_off() {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_basic_tag;
command.cmd.basic.which_cmd = basic_Command_power_off_tag;
basic_PowerOffCmd power_off_cmd = basic_PowerOffCmd_init_default;
command.cmd.basic.cmd.power_off = power_off_cmd;
dongl_Response response = exchange(command);
return response.rsp.basic.rsp.power_off;
}
basic_IsPoweredRsp Protocol::basic_is_powered() {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_basic_tag;
command.cmd.basic.which_cmd = basic_Command_is_powered_tag;
basic_IsPoweredCmd is_powered_cmd = basic_IsPoweredCmd_init_default;
command.cmd.basic.cmd.is_powered = is_powered_cmd;
dongl_Response response = exchange(command);
return response.rsp.basic.rsp.is_powered;
}
simpleble_InitRsp Protocol::simpleble_init() {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_simpleble_tag;
command.cmd.simpleble.which_cmd = simpleble_Command_init_tag;
simpleble_InitCmd init_cmd = simpleble_InitCmd_init_default;
command.cmd.simpleble.cmd.init = init_cmd;
dongl_Response response = exchange(command);
return response.rsp.simpleble.rsp.init;
}
simpleble_ScanStartRsp Protocol::simpleble_scan_start() {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_simpleble_tag;
command.cmd.simpleble.which_cmd = simpleble_Command_scan_start_tag;
simpleble_ScanStartCmd scan_start_cmd = simpleble_ScanStartCmd_init_default;
command.cmd.simpleble.cmd.scan_start = scan_start_cmd;
dongl_Response response = exchange(command);
return response.rsp.simpleble.rsp.scan_start;
}
simpleble_ScanStopRsp Protocol::simpleble_scan_stop() {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_simpleble_tag;
command.cmd.simpleble.which_cmd = simpleble_Command_scan_stop_tag;
simpleble_ScanStopCmd scan_stop_cmd = simpleble_ScanStopCmd_init_default;
command.cmd.simpleble.cmd.scan_stop = scan_stop_cmd;
dongl_Response response = exchange(command);
return response.rsp.simpleble.rsp.scan_stop;
}
simpleble_ScanIsActiveRsp Protocol::simpleble_scan_is_active() {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_simpleble_tag;
command.cmd.simpleble.which_cmd = simpleble_Command_scan_is_active_tag;
simpleble_ScanIsActiveCmd scan_is_active_cmd = simpleble_ScanIsActiveCmd_init_default;
command.cmd.simpleble.cmd.scan_is_active = scan_is_active_cmd;
dongl_Response response = exchange(command);
return response.rsp.simpleble.rsp.scan_is_active;
}
simpleble_ConnectRsp Protocol::simpleble_connect(simpleble_BluetoothAddressType address_type,
const std::string& address) {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_simpleble_tag;
command.cmd.simpleble.which_cmd = simpleble_Command_connect_tag;
simpleble_ConnectCmd connect_cmd = simpleble_ConnectCmd_init_default;
command.cmd.simpleble.cmd.connect = connect_cmd;
command.cmd.simpleble.cmd.connect.address_type = address_type;
strncpy(command.cmd.simpleble.cmd.connect.address, address.c_str(),
sizeof(command.cmd.simpleble.cmd.connect.address));
dongl_Response response = exchange(command);
return response.rsp.simpleble.rsp.connect;
}
simpleble_DisconnectRsp Protocol::simpleble_disconnect(uint16_t conn_handle) {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_simpleble_tag;
command.cmd.simpleble.which_cmd = simpleble_Command_disconnect_tag;
simpleble_DisconnectCmd disconnect_cmd = simpleble_DisconnectCmd_init_default;
command.cmd.simpleble.cmd.disconnect = disconnect_cmd;
command.cmd.simpleble.cmd.disconnect.conn_handle = conn_handle;
dongl_Response response = exchange(command);
return response.rsp.simpleble.rsp.disconnect;
}
simpleble_ReadRsp Protocol::simpleble_read(uint16_t conn_handle, uint16_t handle) {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_simpleble_tag;
command.cmd.simpleble.which_cmd = simpleble_Command_read_tag;
simpleble_ReadCmd read_cmd = simpleble_ReadCmd_init_default;
command.cmd.simpleble.cmd.read = read_cmd;
command.cmd.simpleble.cmd.read.conn_handle = conn_handle;
command.cmd.simpleble.cmd.read.handle = handle;
dongl_Response response = exchange(command);
return response.rsp.simpleble.rsp.read;
}
simpleble_WriteRsp Protocol::simpleble_write(uint16_t conn_handle, uint16_t handle, simpleble_WriteOperation operation,
const std::vector<uint8_t>& data) {
if (data.size() > sizeof(simpleble_WriteCmd_data_t::bytes)) {
throw std::length_error("Payload exceeds maximum size of 512 bytes");
}
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_simpleble_tag;
command.cmd.simpleble.which_cmd = simpleble_Command_write_tag;
simpleble_WriteCmd write_cmd = simpleble_WriteCmd_init_default;
command.cmd.simpleble.cmd.write = write_cmd;
command.cmd.simpleble.cmd.write.conn_handle = conn_handle;
command.cmd.simpleble.cmd.write.handle = handle;
command.cmd.simpleble.cmd.write.op = operation;
command.cmd.simpleble.cmd.write.data.size = data.size();
memcpy(command.cmd.simpleble.cmd.write.data.bytes, data.data(), data.size());
dongl_Response response = exchange(command);
return response.rsp.simpleble.rsp.write;
}
simpleble_IsPairedRsp Protocol::simpleble_is_paired(simpleble_BluetoothAddressType address_type,
const std::string& address) {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_simpleble_tag;
command.cmd.simpleble.which_cmd = simpleble_Command_is_paired_tag;
command.cmd.simpleble.cmd.is_paired.address_type = address_type;
strncpy(command.cmd.simpleble.cmd.is_paired.address, address.c_str(),
sizeof(command.cmd.simpleble.cmd.is_paired.address) - 1);
dongl_Response response = exchange(command);
return response.rsp.simpleble.rsp.is_paired;
}
simpleble_UnpairRsp Protocol::simpleble_unpair(simpleble_BluetoothAddressType address_type,
const std::string& address) {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_simpleble_tag;
command.cmd.simpleble.which_cmd = simpleble_Command_unpair_tag;
command.cmd.simpleble.cmd.unpair.address_type = address_type;
strncpy(command.cmd.simpleble.cmd.unpair.address, address.c_str(),
sizeof(command.cmd.simpleble.cmd.unpair.address) - 1);
dongl_Response response = exchange(command);
return response.rsp.simpleble.rsp.unpair;
}
simpleble_AuthKeyReplyRsp Protocol::simpleble_auth_key_reply(uint16_t conn_handle, uint32_t request_id,
const std::vector<uint8_t>& key, bool accept) {
if (key.size() > sizeof(simpleble_AuthKeyReplyCmd_key_t::bytes)) {
throw std::length_error("Pairing key exceeds maximum size of 16 bytes");
}
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_simpleble_tag;
command.cmd.simpleble.which_cmd = simpleble_Command_auth_key_reply_tag;
command.cmd.simpleble.cmd.auth_key_reply.conn_handle = conn_handle;
command.cmd.simpleble.cmd.auth_key_reply.request_id = request_id;
command.cmd.simpleble.cmd.auth_key_reply.key.size = key.size();
memcpy(command.cmd.simpleble.cmd.auth_key_reply.key.bytes, key.data(), key.size());
command.cmd.simpleble.cmd.auth_key_reply.accept = accept;
dongl_Response response = exchange(command);
return response.rsp.simpleble.rsp.auth_key_reply;
}
simpleble_GetPairedPeripheralRsp Protocol::simpleble_get_paired_peripheral(uint16_t index) {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_simpleble_tag;
command.cmd.simpleble.which_cmd = simpleble_Command_get_paired_peripheral_tag;
command.cmd.simpleble.cmd.get_paired_peripheral.index = index;
dongl_Response response = exchange(command);
return response.rsp.simpleble.rsp.get_paired_peripheral;
}
simpleble_GetPairedPeripheralCountRsp Protocol::simpleble_get_paired_peripheral_count() {
dongl_Command command = dongl_Command_init_zero;
command.which_cmd = dongl_Command_simpleble_tag;
command.cmd.simpleble.which_cmd = simpleble_Command_get_paired_peripheral_count_tag;
dongl_Response response = exchange(command);
return response.rsp.simpleble.rsp.get_paired_peripheral_count;
}