mirror of
https://github.com/OpenBluetoothToolbox/SimpleBLE
synced 2025-10-14 02:08:22 +08:00
Added commands to Dongl code
This commit is contained in:
75
dependencies/external/kvn/kvn_bytearray.h
vendored
75
dependencies/external/kvn/kvn_bytearray.h
vendored
@@ -8,6 +8,7 @@
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
namespace kvn {
|
||||
@@ -209,8 +210,82 @@ class bytearray {
|
||||
uint8_t& operator[](size_t index) { return data_[index]; }
|
||||
const uint8_t& operator[](size_t index) const { return data_[index]; }
|
||||
void push_back(uint8_t byte) { data_.push_back(byte); }
|
||||
|
||||
/**
|
||||
* @brief Appends a uint16_t value as 2 little-endian bytes.
|
||||
* @param value The uint16_t value to append.
|
||||
*/
|
||||
void push_back(uint16_t value) {
|
||||
for (size_t i = 0; i < 2; ++i) {
|
||||
data_.push_back(static_cast<uint8_t>(value >> (i * 8)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Appends a uint32_t value as 4 little-endian bytes.
|
||||
* @param value The uint32_t value to append.
|
||||
*/
|
||||
void push_back(uint32_t value) {
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
data_.push_back(static_cast<uint8_t>(value >> (i * 8)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Appends a uint64_t value as 8 little-endian bytes.
|
||||
* @param value The uint64_t value to append.
|
||||
*/
|
||||
void push_back(uint64_t value) {
|
||||
for (size_t i = 0; i < 8; ++i) {
|
||||
data_.push_back(static_cast<uint8_t>(value >> (i * 8)));
|
||||
}
|
||||
}
|
||||
auto begin() { return data_.begin(); }
|
||||
auto begin() const { return data_.begin(); }
|
||||
auto end() { return data_.end(); }
|
||||
auto end() const { return data_.end(); }
|
||||
|
||||
/**
|
||||
* @brief Inserts a single byte at the specified position.
|
||||
* @param pos Iterator to the position where the element is inserted.
|
||||
* @param value The byte to insert.
|
||||
* @return Iterator pointing to the inserted element.
|
||||
*/
|
||||
auto insert(typename std::vector<uint8_t>::iterator pos, uint8_t value) { return data_.insert(pos, value); }
|
||||
auto insert(typename std::vector<uint8_t>::const_iterator pos, uint8_t value) { return data_.insert(pos, value); }
|
||||
|
||||
/**
|
||||
* @brief Inserts multiple copies of a byte at the specified position.
|
||||
* @param pos Iterator to the position where the elements are inserted.
|
||||
* @param count Number of copies to insert.
|
||||
* @param value The byte to insert.
|
||||
* @return Iterator pointing to the first inserted element.
|
||||
*/
|
||||
auto insert(typename std::vector<uint8_t>::iterator pos, size_t count, uint8_t value) { return data_.insert(pos, count, value); }
|
||||
auto insert(typename std::vector<uint8_t>::const_iterator pos, size_t count, uint8_t value) { return data_.insert(pos, count, value); }
|
||||
|
||||
/**
|
||||
* @brief Inserts elements from a range at the specified position.
|
||||
* @tparam InputIt Iterator type.
|
||||
* @param pos Iterator to the position where the elements are inserted.
|
||||
* @param first Iterator to the first element of the range.
|
||||
* @param last Iterator to one past the last element of the range.
|
||||
* @return Iterator pointing to the first inserted element.
|
||||
*/
|
||||
template <typename InputIt>
|
||||
auto insert(typename std::vector<uint8_t>::iterator pos, InputIt first, InputIt last) { return data_.insert(pos, first, last); }
|
||||
template <typename InputIt>
|
||||
auto insert(typename std::vector<uint8_t>::const_iterator pos, InputIt first, InputIt last) { return data_.insert(pos, first, last); }
|
||||
|
||||
/**
|
||||
* @brief Inserts another bytearray at the specified position.
|
||||
* @param pos Iterator to the position where the elements are inserted.
|
||||
* @param other The bytearray to insert.
|
||||
* @return Iterator pointing to the first inserted element.
|
||||
*/
|
||||
auto insert(typename std::vector<uint8_t>::iterator pos, const bytearray& other) { return data_.insert(pos, other.begin(), other.end()); }
|
||||
auto insert(typename std::vector<uint8_t>::const_iterator pos, const bytearray& other) { return data_.insert(pos, other.begin(), other.end()); }
|
||||
|
||||
//! @endcond
|
||||
|
||||
private:
|
||||
|
@@ -151,14 +151,37 @@ target_compile_definitions(simpleble PRIVATE
|
||||
SIMPLEBLE_BACKEND_IOS=$<BOOL:${SIMPLEBLE_BACKEND_IOS}>
|
||||
SIMPLEBLE_BACKEND_ANDROID=$<BOOL:${SIMPLEBLE_BACKEND_ANDROID}>)
|
||||
|
||||
# Dongl Source File Selection
|
||||
set(SIMPLEBLE_DONGL_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelper.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/cmd/Commands.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/cmd/Utils.cpp)
|
||||
|
||||
if(SIMPLEBLE_PLAIN)
|
||||
list(APPEND SIMPLEBLE_DONGL_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelperNull.cpp)
|
||||
elseif(SIMPLEBLE_BACKEND_LINUX)
|
||||
list(APPEND SIMPLEBLE_DONGL_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelperLinux.cpp)
|
||||
elseif(SIMPLEBLE_BACKEND_WINDOWS)
|
||||
list(APPEND SIMPLEBLE_DONGL_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelperWindows.cpp)
|
||||
elseif(SIMPLEBLE_BACKEND_MACOS)
|
||||
list(APPEND SIMPLEBLE_DONGL_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelperApple.cpp)
|
||||
elseif(SIMPLEBLE_BACKEND_IOS)
|
||||
list(APPEND SIMPLEBLE_DONGL_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelperNull.cpp)
|
||||
elseif(SIMPLEBLE_BACKEND_ANDROID)
|
||||
list(APPEND SIMPLEBLE_DONGL_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelperNull.cpp)
|
||||
endif()
|
||||
|
||||
# Detect the operating system and load the necessary dependencies
|
||||
if(SIMPLEBLE_PLAIN)
|
||||
message(STATUS "Plain Flavor Requested")
|
||||
|
||||
target_sources(simpleble PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelper.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelperNull.cpp
|
||||
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/plain/AdapterPlain.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/plain/PeripheralPlain.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/plain/BackendPlain.cpp)
|
||||
@@ -183,9 +206,6 @@ elseif(SIMPLEBLE_BACKEND_LINUX)
|
||||
endif()
|
||||
|
||||
target_sources(simpleble PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelper.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelperLinux.cpp
|
||||
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/linux/AdapterLinux.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/linux/PeripheralLinux.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/linux/BackendBluez.cpp
|
||||
@@ -294,9 +314,6 @@ elseif(SIMPLEBLE_BACKEND_WINDOWS)
|
||||
endif()
|
||||
|
||||
target_sources(simpleble PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelper.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelperWindows.cpp
|
||||
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/windows/AdapterWindows.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/windows/PeripheralWindows.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/windows/BackendWinRT.cpp
|
||||
@@ -322,16 +339,6 @@ elseif(SIMPLEBLE_BACKEND_MACOS OR SIMPLEBLE_BACKEND_IOS)
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/macos/PeripheralBaseMacOS.mm
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/macos/BackendCoreBluetooth.mm)
|
||||
|
||||
if(SIMPLEBLE_BACKEND_MACOS)
|
||||
target_sources(simpleble PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelper.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelperApple.cpp)
|
||||
elseif(SIMPLEBLE_BACKEND_IOS)
|
||||
target_sources(simpleble PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelper.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelperNull.cpp)
|
||||
endif()
|
||||
|
||||
set_property(TARGET simpleble PROPERTY INSTALL_RPATH @loader_path)
|
||||
|
||||
elseif(SIMPLEBLE_BACKEND_ANDROID)
|
||||
@@ -344,8 +351,6 @@ elseif(SIMPLEBLE_BACKEND_ANDROID)
|
||||
|
||||
target_sources(simpleble PRIVATE
|
||||
${SIMPLEJNI_SOURCES}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelper.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/usb/UsbHelperNull.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/android/AdapterAndroid.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/android/PeripheralAndroid.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/android/BackendAndroid.cpp
|
||||
@@ -369,6 +374,12 @@ elseif(SIMPLEBLE_BACKEND_ANDROID)
|
||||
target_link_libraries(simpleble PUBLIC android nativehelper log)
|
||||
endif()
|
||||
|
||||
target_sources(simpleble PRIVATE
|
||||
${SIMPLEBLE_DONGL_SOURCES})
|
||||
|
||||
target_include_directories(simpleble PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/backends/dongl/include)
|
||||
|
||||
apply_build_options(simpleble
|
||||
"${PRIVATE_COMPILE_DEFINITIONS}"
|
||||
"${PRIVATE_COMPILE_OPTIONS}"
|
||||
|
@@ -6,6 +6,7 @@
|
||||
#include "PeripheralBase.h"
|
||||
#include "PeripheralDongl.h"
|
||||
|
||||
#include "cmd/Commands.h"
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
|
||||
@@ -20,9 +21,9 @@ AdapterDongl::AdapterDongl(const std::string& device_path) : _usb_helper(std::ma
|
||||
fmt::print("Received data: {}\n", data.toHex(true));
|
||||
});
|
||||
|
||||
auto readVersionCommand = Dongl::CMD::UartReadDeviceIdCommand();
|
||||
_usb_helper->tx(readVersionCommand.to_bytes());
|
||||
|
||||
const std::vector<uint8_t> readVersionCommand = {0x05, 0x01, 0x00, 0x00, 0x51, 0x28};
|
||||
_usb_helper->tx(readVersionCommand);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
|
||||
}
|
||||
|
184
simpleble/src/backends/dongl/cmd/Commands.cpp
Normal file
184
simpleble/src/backends/dongl/cmd/Commands.cpp
Normal file
@@ -0,0 +1,184 @@
|
||||
#include "Commands.h"
|
||||
#include <fmt/format.h>
|
||||
#include <typeinfo>
|
||||
|
||||
using namespace SimpleBLE::Dongl::CMD;
|
||||
|
||||
|
||||
kvn::bytearray UartCommand::to_bytes() const {
|
||||
kvn::bytearray command;
|
||||
command.push_back(UART_START_BYTE);
|
||||
command.push_back(get_op_code());
|
||||
command.push_back(static_cast<uint16_t>(payload.size()));
|
||||
command.insert(command.end(), payload.begin(), payload.end());
|
||||
uint16_t crc = calculate_crc(command.data(), command.size());
|
||||
command.push_back(crc);
|
||||
return command;
|
||||
}
|
||||
|
||||
std::string UartCommand::to_string() const {
|
||||
return fmt::format("{} (Payload: {})",
|
||||
typeid(*this).name(),
|
||||
payload.empty() ? "None" : payload.toHex());
|
||||
}
|
||||
|
||||
UartSetUartConfigCommand::UartSetUartConfigCommand(uint32_t baudrate, bool hw_flow_ctrl)
|
||||
: baudrate(baudrate), hw_flow_ctrl(hw_flow_ctrl) {
|
||||
payload.push_back(baudrate);
|
||||
payload.push_back(static_cast<uint8_t>(hw_flow_ctrl));
|
||||
}
|
||||
|
||||
std::string UartSetUartConfigCommand::to_string() const {
|
||||
return fmt::format("UartSetUartConfigCommand(Baudrate: {}, HW Flow Control: {})",
|
||||
baudrate, hw_flow_ctrl);
|
||||
}
|
||||
|
||||
UartSetDeviceNameCommand::UartSetDeviceNameCommand(const std::string& name)
|
||||
: name(name) {
|
||||
payload.insert(payload.end(), name.begin(), name.end());
|
||||
}
|
||||
|
||||
std::string UartSetDeviceNameCommand::to_string() const {
|
||||
return fmt::format("UartSetDeviceNameCommand(Device Name: {})", name);
|
||||
}
|
||||
|
||||
UartSetDataConnectionParametersCommand::UartSetDataConnectionParametersCommand(
|
||||
uint32_t interval, std::optional<uint64_t> timeout, std::optional<uint32_t> event_length)
|
||||
: interval(interval), timeout(timeout), event_length(event_length) {
|
||||
payload.push_back(interval);
|
||||
if (timeout) payload.push_back(*timeout);
|
||||
if (event_length) payload.push_back(*event_length);
|
||||
}
|
||||
|
||||
std::string UartSetDataConnectionParametersCommand::to_string() const {
|
||||
return fmt::format("UartSetDataConnectionParametersCommand(Interval: {}, Timeout: {}, Event Length: {})",
|
||||
interval,
|
||||
timeout ? fmt::format("{}", *timeout) : "None",
|
||||
event_length ? fmt::format("{}", *event_length) : "None");
|
||||
}
|
||||
|
||||
UartSetRealTimeConnectionParametersCommand::UartSetRealTimeConnectionParametersCommand(
|
||||
uint32_t interval, uint32_t latency, uint8_t num_of_retransmission, uint16_t num_of_payload_bytes)
|
||||
: interval(interval), latency(latency), num_of_retransmission(num_of_retransmission),
|
||||
num_of_payload_bytes(num_of_payload_bytes) {
|
||||
payload.push_back(interval);
|
||||
payload.push_back(latency);
|
||||
payload.push_back(num_of_retransmission);
|
||||
payload.push_back(num_of_payload_bytes);
|
||||
}
|
||||
|
||||
std::string UartSetRealTimeConnectionParametersCommand::to_string() const {
|
||||
return fmt::format("UartSetRealTimeConnectionParametersCommand(Interval: {}, Latency: {}, Num of Retransmission: {}, Num of Payload Bytes: {})",
|
||||
interval, latency, num_of_retransmission, num_of_payload_bytes);
|
||||
}
|
||||
|
||||
UartSetSecurityModeCommand::UartSetSecurityModeCommand(SecurityMode mode, std::optional<uint32_t> passkey)
|
||||
: mode(mode), passkey(passkey) {
|
||||
payload.push_back(static_cast<uint8_t>(mode));
|
||||
if (passkey) payload.push_back(*passkey);
|
||||
}
|
||||
|
||||
std::string UartSetSecurityModeCommand::to_string() const {
|
||||
return fmt::format("UartSetSecurityModeCommand(Mode: {}, Passkey: {})",
|
||||
static_cast<int>(mode),
|
||||
passkey ? fmt::format("{}", *passkey) : "None");
|
||||
}
|
||||
|
||||
UartReadDataConnectionStatusCommand::UartReadDataConnectionStatusCommand(uint8_t peer_handle)
|
||||
: peer_handle(peer_handle) {
|
||||
payload.push_back(peer_handle);
|
||||
}
|
||||
|
||||
std::string UartReadDataConnectionStatusCommand::to_string() const {
|
||||
return fmt::format("UartReadDataConnectionStatusCommand(Peer Handle: {})", peer_handle);
|
||||
}
|
||||
|
||||
UartConnectDataConnectionCommand::UartConnectDataConnectionCommand(uint8_t peer_handle,
|
||||
const kvn::bytearray& peer_address)
|
||||
: peer_handle(peer_handle), peer_address(peer_address) {
|
||||
payload.push_back(peer_handle);
|
||||
payload.insert(payload.end(), peer_address.begin(), peer_address.end());
|
||||
}
|
||||
|
||||
std::string UartConnectDataConnectionCommand::to_string() const {
|
||||
return fmt::format("UartConnectDataConnectionCommand(Peer Handle: {}, Peer Address: {})",
|
||||
peer_handle, peer_address.toHex());
|
||||
}
|
||||
|
||||
UartDisconnectDataConnectionCommand::UartDisconnectDataConnectionCommand(uint8_t peer_handle)
|
||||
: peer_handle(peer_handle) {
|
||||
payload.push_back(peer_handle);
|
||||
}
|
||||
|
||||
std::string UartDisconnectDataConnectionCommand::to_string() const {
|
||||
return fmt::format("UartDisconnectDataConnectionCommand(Peer Handle: {})", peer_handle);
|
||||
}
|
||||
|
||||
UartReadRealTimeConnectionStatusCommand::UartReadRealTimeConnectionStatusCommand(uint8_t peer_handle)
|
||||
: peer_handle(peer_handle) {
|
||||
payload.push_back(peer_handle);
|
||||
}
|
||||
|
||||
std::string UartReadRealTimeConnectionStatusCommand::to_string() const {
|
||||
return fmt::format("UartReadRealTimeConnectionStatusCommand(Peer Handle: {})", peer_handle);
|
||||
}
|
||||
|
||||
UartConnectRealTimeConnectionCommand::UartConnectRealTimeConnectionCommand(uint8_t peer_handle)
|
||||
: peer_handle(peer_handle) {
|
||||
payload.push_back(peer_handle);
|
||||
}
|
||||
|
||||
std::string UartConnectRealTimeConnectionCommand::to_string() const {
|
||||
return fmt::format("UartConnectRealTimeConnectionCommand(Peer Handle: {})", peer_handle);
|
||||
}
|
||||
|
||||
UartDisconnectRealTimeConnectionCommand::UartDisconnectRealTimeConnectionCommand(uint8_t peer_handle)
|
||||
: peer_handle(peer_handle) {
|
||||
payload.push_back(peer_handle);
|
||||
}
|
||||
|
||||
std::string UartDisconnectRealTimeConnectionCommand::to_string() const {
|
||||
return fmt::format("UartDisconnectRealTimeConnectionCommand(Peer Handle: {})", peer_handle);
|
||||
}
|
||||
|
||||
UartTxDataCommand::UartTxDataCommand(uint8_t peer_handle, const kvn::bytearray& data)
|
||||
: peer_handle(peer_handle), data(data) {
|
||||
payload.push_back(peer_handle);
|
||||
payload.insert(payload.end(), data.begin(), data.end());
|
||||
}
|
||||
|
||||
std::string UartTxDataCommand::to_string() const {
|
||||
return fmt::format("UartTxDataCommand(Peer Handle: {}, Data: {})",
|
||||
peer_handle, data.toHex());
|
||||
}
|
||||
|
||||
UartPrepareFwUpdateCommand::UartPrepareFwUpdateCommand(const kvn::bytearray& hash, uint32_t size)
|
||||
: hash(hash), size(size) {
|
||||
payload.insert(payload.end(), hash.begin(), hash.end());
|
||||
payload.push_back(size);
|
||||
}
|
||||
|
||||
std::string UartPrepareFwUpdateCommand::to_string() const {
|
||||
return fmt::format("UartPrepareFwUpdateCommand(Hash: {}, Size: {})",
|
||||
hash.toHex(), size);
|
||||
}
|
||||
|
||||
UartWriteFwDataCommand::UartWriteFwDataCommand(uint16_t seq, const kvn::bytearray& data)
|
||||
: seq(seq), data(data) {
|
||||
payload.push_back(seq);
|
||||
payload.insert(payload.end(), data.begin(), data.end());
|
||||
}
|
||||
|
||||
std::string UartWriteFwDataCommand::to_string() const {
|
||||
return fmt::format("UartWriteFwDataCommand(Seq: {}, Data: {})",
|
||||
seq, data.toHex());
|
||||
}
|
||||
|
||||
UartEchoCommand::UartEchoCommand(const kvn::bytearray& data)
|
||||
: data(data) {
|
||||
payload = data;
|
||||
}
|
||||
|
||||
std::string UartEchoCommand::to_string() const {
|
||||
return fmt::format("UartEchoCommand(Data: {})", data.toHex());
|
||||
}
|
333
simpleble/src/backends/dongl/cmd/Commands.h
Normal file
333
simpleble/src/backends/dongl/cmd/Commands.h
Normal file
@@ -0,0 +1,333 @@
|
||||
#pragma once
|
||||
|
||||
#include <kvn/kvn_bytearray.h>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace SimpleBLE {
|
||||
namespace Dongl {
|
||||
namespace CMD {
|
||||
|
||||
enum class SecurityMode : uint8_t { NONE = 0x00, PAIRING = 0x01, PAIRING_WITH_PASSKEY = 0x02 };
|
||||
|
||||
constexpr uint8_t UART_START_BYTE = 0x05;
|
||||
constexpr size_t UART_FW_DATA_BLOCK_SIZE = 256;
|
||||
|
||||
// Forward declaration for CRC function
|
||||
uint16_t calculate_crc(const uint8_t* src, size_t len);
|
||||
|
||||
class UartCommand {
|
||||
protected:
|
||||
kvn::bytearray payload;
|
||||
|
||||
public:
|
||||
UartCommand() = default;
|
||||
virtual ~UartCommand() = default;
|
||||
|
||||
virtual uint8_t get_op_code() const = 0;
|
||||
virtual kvn::bytearray to_bytes() const;
|
||||
virtual std::string to_string() const;
|
||||
};
|
||||
|
||||
class UartRebootCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x00;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartReadVersionCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x01;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartReadTransmitBufferCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x02;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartReadDeviceIdCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x03;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartFactoryResetCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x0F;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartSetUartConfigCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x10;
|
||||
UartSetUartConfigCommand(uint32_t baudrate, bool hw_flow_ctrl);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
uint32_t baudrate;
|
||||
bool hw_flow_ctrl;
|
||||
};
|
||||
|
||||
class UartReadUartConfigCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x11;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartSetDeviceNameCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x12;
|
||||
UartSetDeviceNameCommand(const std::string& name);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
};
|
||||
|
||||
class UartReadDeviceNameCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x13;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartSetDataConnectionParametersCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x14;
|
||||
UartSetDataConnectionParametersCommand(uint32_t interval, std::optional<uint64_t> timeout = std::nullopt,
|
||||
std::optional<uint32_t> event_length = std::nullopt);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
uint32_t interval;
|
||||
std::optional<uint64_t> timeout;
|
||||
std::optional<uint32_t> event_length;
|
||||
};
|
||||
|
||||
class UartReadDataConnectionParametersCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x15;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartSetRealTimeConnectionParametersCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x16;
|
||||
UartSetRealTimeConnectionParametersCommand(uint32_t interval, uint32_t latency, uint8_t num_of_retransmission,
|
||||
uint16_t num_of_payload_bytes);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
uint32_t interval;
|
||||
uint32_t latency;
|
||||
uint8_t num_of_retransmission;
|
||||
uint16_t num_of_payload_bytes;
|
||||
};
|
||||
|
||||
class UartReadRealTimeConnectionParametersCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x17;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartSetSecurityModeCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x18;
|
||||
UartSetSecurityModeCommand(SecurityMode mode, std::optional<uint32_t> passkey = std::nullopt);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
SecurityMode mode;
|
||||
std::optional<uint32_t> passkey;
|
||||
};
|
||||
|
||||
class UartReadSecurityModeCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x19;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartStartScanningCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x20;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartStopScanningCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x21;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartStartAdvertisingCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x22;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartStopAdvertisingCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x23;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartReadDataConnectionStatusCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x30;
|
||||
UartReadDataConnectionStatusCommand(uint8_t peer_handle);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
uint8_t peer_handle;
|
||||
};
|
||||
|
||||
class UartConnectDataConnectionCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x31;
|
||||
UartConnectDataConnectionCommand(uint8_t peer_handle, const kvn::bytearray& peer_address);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
uint8_t peer_handle;
|
||||
kvn::bytearray peer_address;
|
||||
};
|
||||
|
||||
class UartDisconnectDataConnectionCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x32;
|
||||
UartDisconnectDataConnectionCommand(uint8_t peer_handle);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
uint8_t peer_handle;
|
||||
};
|
||||
|
||||
class UartReadRealTimeConnectionStatusCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x33;
|
||||
UartReadRealTimeConnectionStatusCommand(uint8_t peer_handle);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
uint8_t peer_handle;
|
||||
};
|
||||
|
||||
class UartConnectRealTimeConnectionCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x34;
|
||||
UartConnectRealTimeConnectionCommand(uint8_t peer_handle);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
uint8_t peer_handle;
|
||||
};
|
||||
|
||||
class UartDisconnectRealTimeConnectionCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x35;
|
||||
UartDisconnectRealTimeConnectionCommand(uint8_t peer_handle);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
uint8_t peer_handle;
|
||||
};
|
||||
|
||||
class UartTxDataCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x36;
|
||||
UartTxDataCommand(uint8_t peer_handle, const kvn::bytearray& data);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
uint8_t peer_handle;
|
||||
kvn::bytearray data;
|
||||
};
|
||||
|
||||
class UartReadFwInfoCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x40;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartPrepareFwUpdateCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x41;
|
||||
UartPrepareFwUpdateCommand(const kvn::bytearray& hash, uint32_t size);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
kvn::bytearray hash;
|
||||
uint32_t size;
|
||||
};
|
||||
|
||||
class UartWriteFwDataCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x42;
|
||||
UartWriteFwDataCommand(uint16_t seq, const kvn::bytearray& data);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
uint16_t seq;
|
||||
kvn::bytearray data;
|
||||
};
|
||||
|
||||
class UartVerifyFwDataCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x43;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartSwapFwCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x44;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartConfirmFwCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x45;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartEchoCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0x70;
|
||||
UartEchoCommand(const kvn::bytearray& data);
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
std::string to_string() const override;
|
||||
|
||||
private:
|
||||
kvn::bytearray data;
|
||||
};
|
||||
|
||||
class UartUnsupportedCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0xFE;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
class UartInvalidCommand : public UartCommand {
|
||||
public:
|
||||
static constexpr uint8_t op_code = 0xFF;
|
||||
uint8_t get_op_code() const override { return op_code; }
|
||||
};
|
||||
|
||||
} // namespace CMD
|
||||
} // namespace Dongl
|
||||
} // namespace SimpleBLE
|
29
simpleble/src/backends/dongl/cmd/Utils.cpp
Normal file
29
simpleble/src/backends/dongl/cmd/Utils.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "Utils.h"
|
||||
|
||||
namespace SimpleBLE {
|
||||
namespace Dongl {
|
||||
namespace CMD {
|
||||
|
||||
uint16_t calculate_crc(const uint8_t* src, size_t len) {
|
||||
const uint16_t poly = 0xA001;
|
||||
uint16_t crc = 0xFFFF;
|
||||
size_t i, j;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
crc ^= (uint16_t)src[i];
|
||||
|
||||
for (j = 0; j < 8; j++) {
|
||||
if (crc & 0x0001UL) {
|
||||
crc = (crc >> 1U) ^ poly;
|
||||
} else {
|
||||
crc = crc >> 1U;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
} // namespace CMD
|
||||
} // namespace Dongl
|
||||
} // namespace SimpleBLE
|
14
simpleble/src/backends/dongl/cmd/Utils.h
Normal file
14
simpleble/src/backends/dongl/cmd/Utils.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace SimpleBLE {
|
||||
namespace Dongl {
|
||||
namespace CMD {
|
||||
|
||||
uint16_t calculate_crc(const uint8_t* src, size_t len);
|
||||
|
||||
} // namespace CMD
|
||||
} // namespace Dongl
|
||||
} // namespace SimpleBLE
|
Reference in New Issue
Block a user