Skip to content
Open
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
69 changes: 69 additions & 0 deletions src/ble/ctrlm_ble_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <time.h>
#include <unordered_map>
#include "ctrlm_rcp_ipc_iarm_thunder.h"
#include "ctrlm_telemetry.h"

using namespace std;

Expand Down Expand Up @@ -370,6 +371,9 @@ ctrlm_hal_result_t ctrlm_obj_network_ble_t::hal_init_request(GThread *ctrlm_main
ble_rcu_interface_->addRcuKeypressHandler(Slot<ctrlm_hal_ble_IndKeypress_params_t*>(is_alive_,
std::bind(&ctrlm_obj_network_ble_t::ind_keypress, this, std::placeholders::_1)));

ble_rcu_interface_->addRcuPairingOutcomeHandler(Slot<const BleRcuPairingOutcome&>(is_alive_,
std::bind(&ctrlm_obj_network_ble_t::ind_rcu_pairing_outcome, this, std::placeholders::_1)));


ble_rcu_interface_->startKeyMonitorThread();

Expand Down Expand Up @@ -1673,6 +1677,71 @@ void ctrlm_obj_network_ble_t::req_process_unpair(void *data, int size) {
// BEGIN - Process Indications from HAL to the network in CTRLM Main thread context
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------

void ctrlm_obj_network_ble_t::ind_rcu_pairing_outcome(const BleRcuPairingOutcome &outcome) {
// Queue to the ctrlm main thread for duration computation and telemetry emission
auto copy = std::make_shared<BleRcuPairingOutcome>(outcome);
ctrlm_main_queue_handler_push_new<ctrlm_msg_handler_network_t, BleRcuPairingOutcome>(
CTRLM_HANDLER_NETWORK,
(ctrlm_msg_handler_network_t)&ctrlm_obj_network_ble_t::ind_process_rcu_pairing_outcome,
copy,
NULL,
id_);
}

void ctrlm_obj_network_ble_t::ind_process_rcu_pairing_outcome(void *data, int size) {
THREAD_ID_VALIDATE();
const BleRcuPairingOutcome *outcome = static_cast<const BleRcuPairingOutcome *>(data);
if (sizeof(BleRcuPairingOutcome) != size) {
XLOGD_ERROR("Invalid size!");
return;
}
if (!outcome) {
XLOGD_ERROR("pairing outcome data is NULL");
return;
}

#ifdef TELEMETRY_SUPPORT
// Serialize to JSON compatible with the ctrlm.rcu.pairing.attempt T2 marker
json_t *jroot = json_object();
if (jroot) {
json_object_set_new(jroot, "version", json_integer(MARKER_RCU_PAIRING_ATTEMPT_VERSION));
json_object_set_new(jroot, "method", json_string(outcome->method.c_str()));
json_object_set_new(jroot, "result", json_string(outcome->result.c_str()));
json_object_set_new(jroot, "paired_mac", json_string(outcome->pairedMac.c_str()));
json_object_set_new(jroot, "bluez_retries", json_integer(outcome->bluezRetries));

json_t *jbluez_errors = json_array();
if (jbluez_errors) {
for (const auto &msg : outcome->bluezError) {
json_array_append_new(jbluez_errors, json_string(msg.c_str()));
}
json_object_set_new(jroot, "bluez_msg", jbluez_errors);
}

json_t *jdiscovered = json_array();
if (jdiscovered) {
for (const auto &dev : outcome->discovered) {
json_t *jdev = json_object();
if (jdev) {
json_object_set_new(jdev, "mac", json_string(dev.first.c_str()));
json_object_set_new(jdev, "name", json_string(dev.second.c_str()));
json_array_append_new(jdiscovered, jdev);
}
}
json_object_set_new(jroot, "discovered", jdiscovered);
}

char *json_str = json_dumps(jroot, JSON_COMPACT);
if (json_str) {
ctrlm_telemetry_event_t<std::string> ev(MARKER_RCU_PAIRING_ATTEMPT, std::string(json_str));
ctrlm_telemetry_t::get_instance()->event(ctrlm_telemetry_report_t::RCU, ev);
free(json_str);
}
json_decref(jroot);
}
#endif // TELEMETRY_SUPPORT
}

void ctrlm_obj_network_ble_t::ind_rcu_status(ctrlm_hal_ble_RcuStatusData_t *params) {

// push to the main queue and process it synchronously there
Expand Down
2 changes: 2 additions & 0 deletions src/ble/ctrlm_ble_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ class ctrlm_obj_network_ble_t : public ctrlm_obj_network_t {
void ind_rcu_paired(ctrlm_hal_ble_IndPaired_params_t *params);
void ind_rcu_unpaired(ctrlm_hal_ble_IndUnPaired_params_t *params);
void ind_keypress(ctrlm_hal_ble_IndKeypress_params_t *params);
void ind_rcu_pairing_outcome(const BleRcuPairingOutcome &outcome);

void ind_process_rcu_status(void *data, int size);
void ind_process_paired(void *data, int size);
void ind_process_unpaired(void *data, int size);
void ind_process_keypress(void *data, int size);
void ind_process_rcu_pairing_outcome(void *data, int size);

virtual void req_process_network_status(void *data, int size);
virtual void req_process_controller_status(void *data, int size);
Expand Down
6 changes: 6 additions & 0 deletions src/ble/ctrlm_ble_rcu_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ void ctrlm_ble_rcu_interface_t::initialize()
m_rcuUnpairedSlots.invoke(&params);
};
m_controller->addManagedDeviceRemovedSlot(Slot<const BleAddress &>(m_isAlive, deviceRemovedSlot));

auto pairingOutcomeSlot = [this](const BleRcuPairingOutcome &outcome)
{
m_rcuPairingOutcomeSlots.invoke(outcome);
};
m_controller->addPairingOutcomeSlot(Slot<const BleRcuPairingOutcome&>(m_isAlive, pairingOutcomeSlot));
}

voice_params_par_t params;
Expand Down
5 changes: 5 additions & 0 deletions src/ble/ctrlm_ble_rcu_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,17 @@ class ctrlm_ble_rcu_interface_t
{
m_rcuKeypressSlots.addSlot(func);
}
inline void addRcuPairingOutcomeHandler(const Slot<const BleRcuPairingOutcome&> &func)
{
m_rcuPairingOutcomeSlots.addSlot(func);
}


Slots<ctrlm_hal_ble_RcuStatusData_t*> m_rcuStatusChangedSlots;
Slots<ctrlm_hal_ble_IndPaired_params_t*> m_rcuPairedSlots;
Slots<ctrlm_hal_ble_IndUnPaired_params_t*> m_rcuUnpairedSlots;
Slots<ctrlm_hal_ble_IndKeypress_params_t*> m_rcuKeypressSlots;
Slots<const BleRcuPairingOutcome&> m_rcuPairingOutcomeSlots;

private:
std::shared_ptr<bool> m_isAlive;
Expand Down
4 changes: 2 additions & 2 deletions src/ble/hal/blercu/blercuadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class BleRcuAdapter
{
m_deviceNameChangedSlots.addSlot(func);
}
inline void addDevicePairingErrorSlot(const Slot<const BleAddress&, const std::string&> &func)
inline void addDevicePairingErrorSlot(const Slot<const BleAddress&, const std::string&, int, int> &func)
{
m_devicePairingErrorSlots.addSlot(func);
}
Expand Down Expand Up @@ -135,7 +135,7 @@ class BleRcuAdapter
Slots<const BleAddress&, const std::string&> m_deviceFoundSlots;
Slots<const BleAddress&> m_deviceRemovedSlots;
Slots<const BleAddress&, const std::string&> m_deviceNameChangedSlots;
Slots<const BleAddress&, const std::string&> m_devicePairingErrorSlots;
Slots<const BleAddress&, const std::string&, int, int> m_devicePairingErrorSlots;
Slots<const BleAddress&, bool> m_devicePairingChangedSlots;
Slots<const BleAddress&, bool> m_deviceReadyChangedSlots;

Expand Down
51 changes: 51 additions & 0 deletions src/ble/hal/blercu/blercucontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@

using namespace std;

static const char* pairingMethodStr(BleRcuPairingStateMachine::PairingMethod m)
{
switch (m) {
case BleRcuPairingStateMachine::AUTO_TIMEOUT: return "auto_timeout";
case BleRcuPairingStateMachine::MAC_LIST: return "mac_list";
case BleRcuPairingStateMachine::IR_CODE: return "ir_code";
case BleRcuPairingStateMachine::MAC_HASH: return "mac_hash";
default: return "unknown";
}
}

static const char* failureReasonStr(BleRcuPairingStateMachine::FailureReason r)
{
switch (r) {
case BleRcuPairingStateMachine::SUCCESS: return "success";
case BleRcuPairingStateMachine::FAIL_DISCOVERY_TIMEOUT: return "discovery_timeout";
case BleRcuPairingStateMachine::FAIL_DISCOVERY_STOPPED: return "discovery_stopped";
case BleRcuPairingStateMachine::FAIL_DISCOVERY_STOP_TIMEOUT:return "discovery_stop_timeout";
case BleRcuPairingStateMachine::FAIL_PAIRING_TIMEOUT: return "pairing_timeout";
case BleRcuPairingStateMachine::FAIL_BLUEZ_ERROR: return "bluez_error";
case BleRcuPairingStateMachine::FAIL_ADAPTER_OFF: return "adapter_off";
case BleRcuPairingStateMachine::FAIL_DEVICE_UNPAIRED: return "device_unpaired";
case BleRcuPairingStateMachine::FAIL_DEVICE_REMOVED: return "device_removed";
case BleRcuPairingStateMachine::FAIL_CANCELLED: return "cancelled";
default: return "unknown";
}
}


class BleRcuController_userdata {
public:
Expand Down Expand Up @@ -662,6 +690,17 @@ void BleRcuControllerImpl::onFinishedPairing()

m_state = Complete;
m_stateChangedSlots.invoke(m_state);

// emit pairing outcome telemetry
BleRcuPairingOutcome outcome;
outcome.method = pairingMethodStr(m_pairingStateMachine.pairingMethod());
outcome.result = failureReasonStr(BleRcuPairingStateMachine::SUCCESS);
outcome.bluezRetries = m_pairingStateMachine.bluezRetries();
outcome.pairedMac = m_pairingStateMachine.pairedMac().toString();
for (const auto &dev : m_pairingStateMachine.discoveredDevices()) {
outcome.discovered.emplace_back(dev.mac.toString(), dev.name);
}
m_pairingOutcomeSlots.invoke(outcome);
}

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -701,6 +740,18 @@ void BleRcuControllerImpl::onFailedPairing()

m_state = Failed;
m_stateChangedSlots.invoke(m_state);

// emit pairing outcome telemetry
BleRcuPairingOutcome outcome;
outcome.method = pairingMethodStr(m_pairingStateMachine.pairingMethod());
outcome.result = failureReasonStr(m_pairingStateMachine.failureReason());
outcome.bluezRetries = m_pairingStateMachine.bluezRetries();
outcome.pairedMac = m_pairingStateMachine.pairedMac().toString();
outcome.bluezError = m_pairingStateMachine.bluezError();
for (const auto &dev : m_pairingStateMachine.discoveredDevices()) {
outcome.discovered.emplace_back(dev.mac.toString(), dev.name);
}
m_pairingOutcomeSlots.invoke(outcome);
}


Expand Down
18 changes: 18 additions & 0 deletions src/ble/hal/blercu/blercucontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,23 @@

#include <set>
#include <memory>
#include <string>
#include <utility>
#include <vector>


class BleRcuDevice;

struct BleRcuPairingOutcome
{
std::string method; // "auto_timeout" | "ir_code" | "mac_hash" | "mac_list"
std::string result; // "success" | failure-reason string
std::vector<std::pair<std::string, std::string>> discovered; // {mac, name}
int bluezRetries;
std::string pairedMac; // empty on failure
std::vector<std::string> bluezError;
};


class BleRcuController
{
Expand Down Expand Up @@ -102,12 +115,17 @@ class BleRcuController
{
m_stateChangedSlots.addSlot(func);
}
inline void addPairingOutcomeSlot(const Slot<const BleRcuPairingOutcome&> &func)
{
m_pairingOutcomeSlots.addSlot(func);
}

protected:
Slots<const BleAddress&> m_managedDeviceAddedSlots;
Slots<const BleAddress&> m_managedDeviceRemovedSlots;
Slots<bool> m_pairingStateChangedSlots;
Slots<State> m_stateChangedSlots;
Slots<const BleRcuPairingOutcome&> m_pairingOutcomeSlots;
};

#endif // !defined(BLERCUCONTROLLER_H)
5 changes: 3 additions & 2 deletions src/ble/hal/blercu/blercudevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,18 @@ class BleRcuDevice
{
m_readyChangedSlots.addSlot(func);
}
inline void addPairingErrorSlot(const Slot<const std::string&> &func)
inline void addPairingErrorSlot(const Slot<const std::string&, int, int> &func)
{
m_pairingErrorSlots.addSlot(func);
}


protected:
Slots<bool> m_connectedChangedSlots;
Slots<bool> m_pairedChangedSlots;
Slots<const std::string&> m_nameChangedSlots;
Slots<bool> m_readyChangedSlots;
Slots<const std::string&> m_pairingErrorSlots;
Slots<const std::string&, int, int> m_pairingErrorSlots;
};


Expand Down
Loading
Loading