Skip to content
72 changes: 72 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 @@ -310,6 +311,13 @@ ctrlm_obj_network_ble_t::ctrlm_obj_network_ble_t(ctrlm_network_type_t type, ctrl
if(rfc) {
rfc->add_changed_listener(ctrlm_rfc_t::attrs::BLE, std::bind(&ctrlm_obj_network_ble_t::rfc_retrieved_handler, this, std::placeholders::_1));
}

#ifdef TELEMETRY_SUPPORT
ctrlm_telemetry_t *telemetry = ctrlm_get_telemetry_obj();
if(telemetry) {
telemetry->add_listener(ctrlm_telemetry_report_t::RCU, std::bind(&ctrlm_obj_network_ble_t::telemetry_report_handler, this));
}
#endif
}

ctrlm_obj_network_ble_t::ctrlm_obj_network_ble_t() {
Expand Down Expand Up @@ -370,6 +378,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 @@ -1657,6 +1668,61 @@ 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;
}
Comment thread
klu339 marked this conversation as resolved.

#ifdef TELEMETRY_SUPPORT
// Serialize to array for ctrlm.rcu.pairing.attempt T2 marker
std::stringstream ss;
ss << "[";
Comment thread
klu339 marked this conversation as resolved.
ss << MARKER_RCU_PAIRING_ATTEMPT_VERSION << ",";
ss << "\"" << name_get() << "\",";
ss << outcome->method << ",";
ss << outcome->result << ",";
ss << outcome->discovered << ",";
ss << "\"" << outcome->name << "\",";
ss << outcome->bluezRetries << ",";

for (uint8_t i = 0; i < outcome->maxBluezRetries; i++) {
if (i >= outcome->bluezRetries || i >= outcome->bluezError.size()) {
ss << "\"null\"";
Comment thread
klu339 marked this conversation as resolved.
} else {
std::string msg = outcome->bluezError.at(i);
auto pos = msg.rfind(": ");
msg = (pos != std::string::npos) ? msg.substr(pos + 2) : msg;
ss << "\"" << msg << "\"";
}
if (i != (outcome->maxBluezRetries-1)) {
ss << ",";
}
}
ss << "]";

ctrlm_telemetry_event_t<std::string> ev(MARKER_RCU_PAIRING_ATTEMPT, ss.str());
ctrlm_telemetry_t::get_instance()->event(ctrlm_telemetry_report_t::RCU, ev);
#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 Expand Up @@ -2662,3 +2728,9 @@ void ctrlm_obj_network_ble_t::start_controller_audio_streaming(ctrlm_voice_start
bool ctrlm_obj_network_ble_t::is_managed_by_network(ctrlm_controller_id_t id) {
return (id >= BLE_RCU_ID_RANGE_MIN && id < BLE_RCU_ID_RANGE_MAX);
}

void ctrlm_obj_network_ble_t::telemetry_report_handler() {
#ifndef TELEMETRY_SUPPORT
XLOGD_WARN("telemetry is not enabled");
#endif
}
3 changes: 3 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 Expand Up @@ -195,6 +197,7 @@ class ctrlm_obj_network_ble_t : public ctrlm_obj_network_t {
std::shared_ptr<ConfigSettings> getConfigSettings();

virtual void start_controller_audio_streaming(ctrlm_voice_start_audio_params_t *params);
void telemetry_report_handler();

protected:
virtual bool is_managed_by_network(ctrlm_controller_id_t id);
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 @@ -145,12 +145,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
6 changes: 3 additions & 3 deletions src/ble/hal/blercu/blercuadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class BleRcuAdapter
virtual bool isDevicePaired(const BleAddress &address) const = 0;
virtual bool isDeviceConnected(const BleAddress &address) const = 0;

virtual bool addDevice(const BleAddress &address) = 0;
virtual bool addDevice(const BleAddress &address, int retries) = 0;
virtual bool removeDevice(const BleAddress &address) = 0;

virtual bool setConnectionParams(BleAddress address, double minInterval, double maxInterval,
Expand Down 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, bool> &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, bool> m_devicePairingErrorSlots;
Slots<const BleAddress&, bool> m_devicePairingChangedSlots;
Slots<const BleAddress&, bool> m_deviceReadyChangedSlots;

Expand Down
22 changes: 21 additions & 1 deletion src/ble/hal/blercu/blercucontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

using namespace std;


class BleRcuController_userdata {
public:
BleRcuController_userdata(shared_ptr<bool> isAlive_, BleRcuControllerImpl* controller_)
Expand Down Expand Up @@ -607,6 +606,16 @@ void BleRcuControllerImpl::onFinishedPairing()

m_state = Complete;
m_stateChangedSlots.invoke(m_state);

// emit pairing outcome telemetry
BleRcuPairingOutcome outcome;
outcome.method = m_pairingStateMachine.pairingMethod();
outcome.result = BleRcuPairingStateMachine::SUCCESS;
outcome.bluezRetries = m_pairingStateMachine.bluezRetries();
outcome.maxBluezRetries = MAX_PAIRING_RETRIES;
Comment thread
klu339 marked this conversation as resolved.
outcome.name = m_pairingStateMachine.pairedName();
outcome.discovered = m_pairingStateMachine.discoveredDevices();
m_pairingOutcomeSlots.invoke(outcome);
}

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -646,6 +655,17 @@ void BleRcuControllerImpl::onFailedPairing()

m_state = Failed;
m_stateChangedSlots.invoke(m_state);

// emit pairing outcome telemetry
BleRcuPairingOutcome outcome;
outcome.method = m_pairingStateMachine.pairingMethod();
outcome.result = m_pairingStateMachine.failureReason();
outcome.bluezRetries = m_pairingStateMachine.bluezRetries();
outcome.maxBluezRetries = MAX_PAIRING_RETRIES;
outcome.bluezError = m_pairingStateMachine.bluezError();
outcome.discovered = m_pairingStateMachine.discoveredDevices();
outcome.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
{
int method; // numeric enum value for PairingMethod
int result; // numeric enum value for FailureReason
int discovered; // number of discovered devices
int bluezRetries; // number of bluez retries attempted
int maxBluezRetries; // max number of retries
std::string name; // empty on failure
std::vector<std::string> bluezError; // bluez error messages
};

class BleRcuController
{
Expand Down Expand Up @@ -101,12 +114,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, bool> &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, bool> m_pairingErrorSlots;
};


Expand Down
Loading
Loading