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
1 change: 1 addition & 0 deletions base/cvd/cuttlefish/common/libs/sensors/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ inline constexpr char OUTER_DELIM = ' ';
inline constexpr int kUpdateRotationVec = 0;
inline constexpr int kGetSensorsData = 1;
inline constexpr int kUpdateHal = 2;
inline constexpr int kUpdateLowLatencyOffBodyDetect = 3;

using SensorsCmd = int;

Expand Down
6 changes: 6 additions & 0 deletions base/cvd/cuttlefish/host/commands/sensors_simulator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ Result<void> ProcessWebrtcRequest(transport::SharedFdChannel& channel,
"Can't send request for cmd: " << cmd);
break;
}
case kUpdateLowLatencyOffBodyDetect: {
double value;
CF_EXPECT(static_cast<bool>(ss >> value), kReqMisFormatted);
sensors_simulator.UpdateLowLatencyOffBodyDetect(value);
break;
}
default: {
return CF_ERR("Unsupported cmd: " << cmd);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ static constexpr uint32_t kIntervalMs = 1000;
static constexpr SensorsMask kContinuousModeSensors =
(1 << kAccelerationId) | (1 << kGyroscopeId) | (1 << kMagneticId) |
(1 << kPressureId) | (1 << kUncalibGyroscopeId) |
(1 << kUncalibAccelerationId) | (1 << kLightId);
(1 << kUncalibAccelerationId) | (1 << kLightId) |
(1 << kSensorHandleLowLatencyOffBodyDetect);

Result<std::string> SensorIdToName(int id) {
switch (id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ inline double ToRadians(double x) { return x * M_PI / 180; }
// Check if a given sensor id provides scalar data
static bool IsScalarSensor(int id) {
return (id == kTemperatureId) || (id == kProximityId) || (id == kLightId) ||
(id == kPressureId) || (id == kHumidityId) || (id == kHingeAngle0Id);
(id == kPressureId) || (id == kHumidityId) || (id == kHingeAngle0Id) ||
(id == kSensorHandleLowLatencyOffBodyDetect);
}

// Calculate the rotation matrix of the pitch, roll, and yaw angles.
Expand Down Expand Up @@ -134,6 +135,10 @@ void SensorsSimulator::RefreshSensors(double x, double y, double z) {
sensors_data_[kUncalibMagneticId].v = mgn_update;
}

void SensorsSimulator::UpdateLowLatencyOffBodyDetect(double value) {
sensors_data_[kSensorHandleLowLatencyOffBodyDetect].f = value;
}

std::string SensorsSimulator::GetSensorsData(const SensorsMask mask) {
std::stringstream sensors_msg;
std::lock_guard<std::mutex> lock(sensors_data_mtx_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class SensorsSimulator {
SensorsSimulator(bool is_auto);
// Update sensor values based on new rotation status.
void RefreshSensors(double x, double y, double z);
void UpdateLowLatencyOffBodyDetect(double value);

// Return a string with serialized sensors data in ascending order of
// sensor id. A bitmask is used to specify which sensors to include.
Expand Down
55 changes: 41 additions & 14 deletions base/cvd/cuttlefish/host/frontend/webrtc/connection_observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,22 +246,13 @@ class ConnectionObserverImpl : public webrtc_streaming::ConnectionObserver {

void OnSensorsMessage(const uint8_t *msg, size_t size) override {
std::string msgstr(msg, msg + size);
std::vector<std::string> xyz = android::base::Split(msgstr, " ");

if (xyz.size() != 3) {
LOG(WARNING) << "Invalid rotation angles: Expected 3, received "
<< xyz.size();
return;
if (msgstr.rfind(kOnSensorLowLatencyOffBodyDetectCmd, 0) == 0) {
auto payload = msgstr.substr(kOnSensorLowLatencyOffBodyDetectCmd.size());
HandleLowLatencyOffBodySensorMessage(payload);
} else {
HandleRotationSensorMessage(msgstr);
}

double x, y, z;
CHECK(android::base::ParseDouble(xyz.at(0), &x))
<< "X rotation value must be a double";
CHECK(android::base::ParseDouble(xyz.at(1), &y))
<< "Y rotation value must be a double";
CHECK(android::base::ParseDouble(xyz.at(2), &z))
<< "Z rotation value must be a double";
sensors_handler_.HandleMessage(x, y, z);
}

void OnLightsChannelOpen(
Expand Down Expand Up @@ -431,6 +422,40 @@ class ConnectionObserverImpl : public webrtc_streaming::ConnectionObserver {
}
}

void HandleRotationSensorMessage(const std::string &msg) {
std::vector<std::string> xyz = android::base::Split(msg, " ");

if (xyz.size() != 3) {
LOG(WARNING) << "Invalid rotation angles: Expected 3, received "
<< xyz.size();
return;
}

double x, y, z;
if (!android::base::ParseDouble(xyz.at(0), &x)) {
LOG(ERROR) << "X rotation value must be a double";
return;
}
if (!android::base::ParseDouble(xyz.at(1), &y)) {
LOG(ERROR) << "Y rotation value must be a double";
return;
}
if (!android::base::ParseDouble(xyz.at(2), &z)) {
LOG(ERROR) << "Z rotation value must be a double";
return;
}
sensors_handler_.HandleMessage(x, y, z);
}

void HandleLowLatencyOffBodySensorMessage(const std::string &msg) {
double value;
if (!android::base::ParseDouble(msg, &value)) {
LOG(ERROR) << "Low latency off body value must be double";
return;
}
sensors_handler_.HandleLowLatencyOffBodyDetectMessage(value);
}

std::unique_ptr<InputConnector::EventSink> input_events_sink_;
KernelLogEventsHandler &kernel_log_events_handler_;
int kernel_log_subscription_id_ = -1;
Expand All @@ -446,6 +471,8 @@ class ConnectionObserverImpl : public webrtc_streaming::ConnectionObserver {
std::shared_ptr<webrtc_streaming::LightsObserver> lights_observer_;
int sensors_subscription_id = -1;
int lights_subscription_id_ = -1;
static constexpr std::string_view kOnSensorLowLatencyOffBodyDetectCmd =
"low-latency-off-body-detect:";
};

CfConnectionObserverFactory::CfConnectionObserverFactory(
Expand Down
24 changes: 24 additions & 0 deletions base/cvd/cuttlefish/host/frontend/webrtc/sensors_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ Result<void> SensorsHandler::RefreshSensors(const double x, const double y,
return {};
}

Result<void> SensorsHandler::RefreshLowLatencyOffBodyDetect(
const double value) {
auto msg = std::to_string(value);
auto size = msg.size();
auto cmd = sensors::kUpdateLowLatencyOffBodyDetect;
auto request = CF_EXPECT(transport::CreateMessage(cmd, size),
"Failed to allocate message for cmd: "
<< cmd << " with size: " << size << " bytes. ");
std::memcpy(request->payload, msg.data(), size);
CF_EXPECT(channel_.SendRequest(*request),
"Can't send request for cmd: " << cmd);
return {};
}

Result<std::string> SensorsHandler::GetSensorsData() {
auto msg = std::to_string(kUiSupportedSensors);
auto size = msg.size();
Expand Down Expand Up @@ -88,6 +102,16 @@ void SensorsHandler::HandleMessage(const double x, const double y, const double
UpdateSensorsUi();
}

void SensorsHandler::HandleLowLatencyOffBodyDetectMessage(double value) {
auto refresh_result = RefreshLowLatencyOffBodyDetect(value);
if (!refresh_result.ok()) {
LOG(ERROR) << "Failed to refresh low latency off body detect: "
<< refresh_result.error().FormatForEnv();
return;
}
UpdateSensorsUi();
}

int SensorsHandler::Subscribe(std::function<void(const uint8_t*, size_t)> send_to_client) {
int subscriber_id = ++last_client_channel_id_;
{
Expand Down
2 changes: 2 additions & 0 deletions base/cvd/cuttlefish/host/frontend/webrtc/sensors_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ struct SensorsHandler {
SensorsHandler(SharedFD sensors_fd);
~SensorsHandler();
void HandleMessage(const double x, const double y, const double z);
void HandleLowLatencyOffBodyDetectMessage(double value);
int Subscribe(std::function<void(const uint8_t*, size_t)> send_to_client);
void UnSubscribe(int subscriber_id);

private:
Result<void> RefreshSensors(const double x, const double y, const double z);
Result<void> RefreshLowLatencyOffBodyDetect(const double value);
Result<std::string> GetSensorsData();
void UpdateSensorsUi();
std::unordered_map<int, std::function<void(const uint8_t*, size_t)>> client_channels_;
Expand Down