Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (ble): Add setFileExchangeState characteristic #1130

Merged
merged 1 commit into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions libs/BLEKit/include/BLEServiceFileExchange.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ class BLEServiceFileExchange : public interface::BLEService
public:
BLEServiceFileExchange() : interface::BLEService(service::file_exchange::uuid, _characteristic_table) {};

void setFileExchangeState(bool value)
{
set_file_exchange_state = static_cast<uint8_t>(value);

auto data = std::make_tuple(set_file_exchange_state_characteristic.getValueHandle(),
std::span(&set_file_exchange_state, 1));
sendData(data);
}

auto getFileExchangeState() const -> bool { return set_file_exchange_state != 0x00; }

void setFileSHA256(std::array<uint8_t, 32> sha256) const
{
auto data = std::make_tuple(file_sha256_characteristic.getValueHandle(), sha256);
Expand All @@ -28,6 +39,12 @@ class BLEServiceFileExchange : public interface::BLEService

void onDataReceived(const data_received_handle_t &params) final
{
if (params.handle == set_file_exchange_state_characteristic.getValueHandle()) {
set_file_exchange_state = params.data[0];
if (_on_set_file_exchange_state_change) {
_on_set_file_exchange_state_change(static_cast<bool>(set_file_exchange_state));
}
}
if (params.handle == file_path_characteristic.getValueHandle()) {
if (params.offset == 0) {
file_path.fill('\0');
Expand All @@ -50,6 +67,11 @@ class BLEServiceFileExchange : public interface::BLEService
}
};

void onSetFileExchangeState(const std::function<void(bool)> &callback)
{
_on_set_file_exchange_state_change = callback;
}

void onFilePathReceived(const std::function<void(std::span<const char>)> &callback)
{
_on_file_path_callback = callback;
Expand All @@ -73,6 +95,11 @@ class BLEServiceFileExchange : public interface::BLEService
}

private:
uint8_t set_file_exchange_state {0x00};
ReadWriteGattCharacteristic<uint8_t> set_file_exchange_state_characteristic {
service::file_exchange::characteristic::set_state, &set_file_exchange_state};
std::function<void(bool)> _on_set_file_exchange_state_change {};

std::array<char, 256> file_path {};
WriteOnlyArrayGattCharacteristic<char, 256> file_path_characteristic {
service::file_exchange::characteristic::file_path, file_path.begin()};
Expand All @@ -89,8 +116,9 @@ class BLEServiceFileExchange : public interface::BLEService
std::function<void(std::span<const char>)> _on_file_path_callback {};
std::function<void(std::span<const char>)> _on_file_sha256_callback {};

std::array<GattCharacteristic *, 3> _characteristic_table {
&file_path_characteristic, &file_reception_buffer_characteristic, &file_sha256_characteristic};
std::array<GattCharacteristic *, 4> _characteristic_table {
&set_file_exchange_state_characteristic, &file_path_characteristic, &file_reception_buffer_characteristic,
&file_sha256_characteristic};
};

} // namespace leka
1 change: 1 addition & 0 deletions libs/BLEKit/include/internal/ServicesCharacteristics.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace file_exchange {
inline constexpr uint16_t uuid = 0x8270;

namespace characteristic {
inline constexpr uint16_t set_state = 0x8383;
inline constexpr uint16_t file_path = 0x7080;
inline constexpr uint16_t file_reception_buffer = 0x8283;
inline constexpr uint16_t file_sha256 = 0x7083;
Expand Down
72 changes: 72 additions & 0 deletions libs/BLEKit/tests/BLEServiceFileExchange_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,78 @@ TEST_F(BLEServiceFileExchangeTest, initialisation)
EXPECT_NE(&service_file_exchange, nullptr);
}

TEST_F(BLEServiceFileExchangeTest, setFileExchangeState)
{
uint8_t actual_charging_status {};

auto spy_callback = [&actual_charging_status](const BLEServiceFileExchange::data_to_send_handle_t &handle) {
actual_charging_status = std::get<1>(handle)[0];
};

service_file_exchange.onDataReadyToSend(spy_callback);

service_file_exchange.setFileExchangeState(true);
EXPECT_TRUE(actual_charging_status);

service_file_exchange.setFileExchangeState(false);
EXPECT_FALSE(actual_charging_status);
}

TEST_F(BLEServiceFileExchangeTest, getFileExchangeStateDefault)
{
auto expected_is_file_exchange_state = false;

auto actual_is_file_exchange_state = service_file_exchange.getFileExchangeState();

EXPECT_EQ(actual_is_file_exchange_state, expected_is_file_exchange_state);
}

TEST_F(BLEServiceFileExchangeTest, getFileExchangeStateTrue)
{
auto expected_is_file_exchange_state = true;

std::array<uint8_t, 1> expected_is_file_exchange_state_data = {
static_cast<uint8_t>(expected_is_file_exchange_state)};

onDataReceivedProcess(expected_is_file_exchange_state_data);

auto actual_is_file_exchange_state = service_file_exchange.getFileExchangeState();

EXPECT_EQ(actual_is_file_exchange_state, expected_is_file_exchange_state);
}

TEST_F(BLEServiceFileExchangeTest, onSetFileExchangeStateCallback)
{
bool sent_value = true;
auto sent_value_data = static_cast<uint8_t>(sent_value);

data_received_handle.data = &sent_value_data;

testing::MockFunction<void(bool)> mock_callback {};
service_file_exchange.onSetFileExchangeState(mock_callback.AsStdFunction());

EXPECT_CALL(mock_callback, Call(sent_value));

service_file_exchange.onDataReceived(data_received_handle);
}

TEST_F(BLEServiceFileExchangeTest, onSetFileExchangeStateCallbackNotSameHandle)
{
bool sent_value = true;
auto sent_value_data = static_cast<uint8_t>(sent_value);

data_received_handle.data = &sent_value_data;

testing::MockFunction<void(bool)> mock_callback {};
service_file_exchange.onSetFileExchangeState(mock_callback.AsStdFunction());

data_received_handle.handle = 0xFFFF;

EXPECT_CALL(mock_callback, Call).Times(0);

service_file_exchange.onDataReceived(data_received_handle);
}

TEST_F(BLEServiceFileExchangeTest, getFilePathAny)
{
auto expected_file_path = std::to_array("/fs/some_file.txt");
Expand Down
3 changes: 3 additions & 0 deletions spikes/lk_ble/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ auto main() -> int

initializeSD();

service_file_exchange.onSetFileExchangeState(
[](bool new_state) { log_info("New FileExchange state is: %s", (new_state ? "on" : "off")); });

service_file_exchange.onFilePathReceived(
[](std::span<const char> path) { file_reception_handler.setFilePath(path.data()); });

Expand Down