2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
#include "SinkImpl.h"
|
|
|
|
|
|
2024-02-12 22:33:03 -08:00
|
|
|
#include <wpi/SmallString.h>
|
2019-01-11 20:33:05 -08:00
|
|
|
#include <wpi/json.h>
|
|
|
|
|
|
2018-10-31 20:22:58 -07:00
|
|
|
#include "Instance.h"
|
2016-11-18 10:20:56 -08:00
|
|
|
#include "Notifier.h"
|
2016-09-05 12:00:04 -07:00
|
|
|
#include "SourceImpl.h"
|
2024-02-12 22:33:03 -08:00
|
|
|
#include "c_util.h"
|
2016-09-05 12:00:04 -07:00
|
|
|
|
|
|
|
|
using namespace cs;
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
SinkImpl::SinkImpl(std::string_view name, wpi::Logger& logger,
|
2018-10-31 20:22:58 -07:00
|
|
|
Notifier& notifier, Telemetry& telemetry)
|
|
|
|
|
: m_logger(logger),
|
|
|
|
|
m_notifier(notifier),
|
|
|
|
|
m_telemetry(telemetry),
|
2021-06-06 16:13:58 -07:00
|
|
|
m_name{name} {}
|
2016-09-08 23:52:23 -07:00
|
|
|
|
|
|
|
|
SinkImpl::~SinkImpl() {
|
|
|
|
|
if (m_source) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_enabledCount > 0) {
|
|
|
|
|
m_source->DisableSink();
|
|
|
|
|
}
|
2016-09-08 23:52:23 -07:00
|
|
|
m_source->RemoveSink();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void SinkImpl::SetDescription(std::string_view description) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2021-06-06 16:13:58 -07:00
|
|
|
m_description = description;
|
2016-10-26 23:31:48 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view SinkImpl::GetDescription(
|
|
|
|
|
wpi::SmallVectorImpl<char>& buf) const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2016-10-26 23:31:48 -07:00
|
|
|
buf.append(m_description.begin(), m_description.end());
|
2021-06-06 16:13:58 -07:00
|
|
|
return {buf.data(), buf.size()};
|
2016-10-26 23:31:48 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-18 10:20:56 -08:00
|
|
|
void SinkImpl::Enable() {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2016-11-18 10:20:56 -08:00
|
|
|
++m_enabledCount;
|
|
|
|
|
if (m_enabledCount == 1) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_source) {
|
|
|
|
|
m_source->EnableSink();
|
|
|
|
|
}
|
2018-10-31 20:22:58 -07:00
|
|
|
m_notifier.NotifySink(*this, CS_SINK_ENABLED);
|
2016-11-18 10:20:56 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SinkImpl::Disable() {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2016-11-18 10:20:56 -08:00
|
|
|
--m_enabledCount;
|
|
|
|
|
if (m_enabledCount == 0) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_source) {
|
|
|
|
|
m_source->DisableSink();
|
|
|
|
|
}
|
2018-10-31 20:22:58 -07:00
|
|
|
m_notifier.NotifySink(*this, CS_SINK_DISABLED);
|
2016-11-18 10:20:56 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SinkImpl::SetEnabled(bool enabled) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2016-11-18 10:20:56 -08:00
|
|
|
if (enabled && m_enabledCount == 0) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_source) {
|
|
|
|
|
m_source->EnableSink();
|
|
|
|
|
}
|
2016-11-18 10:20:56 -08:00
|
|
|
m_enabledCount = 1;
|
2018-10-31 20:22:58 -07:00
|
|
|
m_notifier.NotifySink(*this, CS_SINK_ENABLED);
|
2016-11-18 10:20:56 -08:00
|
|
|
} else if (!enabled && m_enabledCount > 0) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_source) {
|
|
|
|
|
m_source->DisableSink();
|
|
|
|
|
}
|
2016-11-18 10:20:56 -08:00
|
|
|
m_enabledCount = 0;
|
2018-10-31 20:22:58 -07:00
|
|
|
m_notifier.NotifySink(*this, CS_SINK_DISABLED);
|
2016-11-18 10:20:56 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-08 23:52:23 -07:00
|
|
|
void SinkImpl::SetSource(std::shared_ptr<SourceImpl> source) {
|
2016-11-11 00:01:58 -08:00
|
|
|
{
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_source == source) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-11-11 00:01:58 -08:00
|
|
|
if (m_source) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_enabledCount > 0) {
|
|
|
|
|
m_source->DisableSink();
|
|
|
|
|
}
|
2016-11-11 00:01:58 -08:00
|
|
|
m_source->RemoveSink();
|
|
|
|
|
}
|
|
|
|
|
m_source = source;
|
2016-12-28 00:47:26 -08:00
|
|
|
if (m_source) {
|
|
|
|
|
m_source->AddSink();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_enabledCount > 0) {
|
|
|
|
|
m_source->EnableSink();
|
|
|
|
|
}
|
2016-12-28 00:47:26 -08:00
|
|
|
}
|
2016-09-08 23:52:23 -07:00
|
|
|
}
|
2016-11-11 00:01:58 -08:00
|
|
|
SetSourceImpl(source);
|
2016-09-08 23:52:23 -07:00
|
|
|
}
|
2016-10-26 23:31:48 -07:00
|
|
|
|
|
|
|
|
std::string SinkImpl::GetError() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_source) {
|
|
|
|
|
return "no source connected";
|
|
|
|
|
}
|
2021-06-06 16:13:58 -07:00
|
|
|
return std::string{m_source->GetCurFrame().GetError()};
|
2016-10-26 23:31:48 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view SinkImpl::GetError(wpi::SmallVectorImpl<char>& buf) const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_source) {
|
|
|
|
|
return "no source connected";
|
|
|
|
|
}
|
2016-12-20 20:48:31 -08:00
|
|
|
// Make a copy as it's shared data
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view error = m_source->GetCurFrame().GetError();
|
2016-12-20 20:48:31 -08:00
|
|
|
buf.clear();
|
|
|
|
|
buf.append(error.data(), error.data() + error.size());
|
2021-06-06 16:13:58 -07:00
|
|
|
return {buf.data(), buf.size()};
|
2016-10-26 23:31:48 -07:00
|
|
|
}
|
2016-11-11 00:01:58 -08:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
bool SinkImpl::SetConfigJson(std::string_view config, CS_Status* status) {
|
2019-01-11 20:33:05 -08:00
|
|
|
wpi::json j;
|
|
|
|
|
try {
|
|
|
|
|
j = wpi::json::parse(config);
|
|
|
|
|
} catch (const wpi::json::parse_error& e) {
|
2021-06-06 16:13:58 -07:00
|
|
|
SWARNING("SetConfigJson: parse error at byte {}: {}", e.byte, e.what());
|
2019-01-11 20:33:05 -08:00
|
|
|
*status = CS_PROPERTY_WRITE_FAILED;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return SetConfigJson(j, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SinkImpl::SetConfigJson(const wpi::json& config, CS_Status* status) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (config.count("properties") != 0) {
|
2019-01-11 20:33:05 -08:00
|
|
|
SetPropertiesJson(config.at("properties"), m_logger, GetName(), status);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2019-01-11 20:33:05 -08:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string SinkImpl::GetConfigJson(CS_Status* status) {
|
|
|
|
|
std::string rv;
|
|
|
|
|
wpi::raw_string_ostream os(rv);
|
|
|
|
|
GetConfigJsonObject(status).dump(os, 4);
|
|
|
|
|
os.flush();
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpi::json SinkImpl::GetConfigJsonObject(CS_Status* status) {
|
|
|
|
|
wpi::json j;
|
|
|
|
|
|
|
|
|
|
wpi::json props = GetPropertiesJsonObject(status);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (props.is_array()) {
|
|
|
|
|
j.emplace("properties", props);
|
|
|
|
|
}
|
2019-01-11 20:33:05 -08:00
|
|
|
|
|
|
|
|
return j;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-27 22:12:30 -07:00
|
|
|
void SinkImpl::NotifyPropertyCreated(int propIndex, PropertyImpl& prop) {
|
2018-10-31 20:22:58 -07:00
|
|
|
m_notifier.NotifySinkProperty(*this, CS_SINK_PROPERTY_CREATED, prop.name,
|
|
|
|
|
propIndex, prop.propKind, prop.value,
|
|
|
|
|
prop.valueStr);
|
2018-07-27 22:12:30 -07:00
|
|
|
// also notify choices updated event for enum types
|
2020-12-28 12:58:06 -08:00
|
|
|
if (prop.propKind == CS_PROP_ENUM) {
|
2018-10-31 20:22:58 -07:00
|
|
|
m_notifier.NotifySinkProperty(*this, CS_SINK_PROPERTY_CHOICES_UPDATED,
|
|
|
|
|
prop.name, propIndex, prop.propKind,
|
2021-06-06 16:13:58 -07:00
|
|
|
prop.value, {});
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2018-07-27 22:12:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SinkImpl::UpdatePropertyValue(int property, bool setString, int value,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view valueStr) {
|
2018-07-27 22:12:30 -07:00
|
|
|
auto prop = GetProperty(property);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!prop) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-07-27 22:12:30 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (setString) {
|
2018-07-27 22:12:30 -07:00
|
|
|
prop->SetValue(valueStr);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2018-07-27 22:12:30 -07:00
|
|
|
prop->SetValue(value);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2018-07-27 22:12:30 -07:00
|
|
|
|
|
|
|
|
// Only notify updates after we've notified created
|
2018-10-31 20:22:58 -07:00
|
|
|
if (m_properties_cached) {
|
|
|
|
|
m_notifier.NotifySinkProperty(*this, CS_SINK_PROPERTY_VALUE_UPDATED,
|
|
|
|
|
prop->name, property, prop->propKind,
|
|
|
|
|
prop->value, prop->valueStr);
|
|
|
|
|
}
|
2018-07-27 22:12:30 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-11 00:01:58 -08:00
|
|
|
void SinkImpl::SetSourceImpl(std::shared_ptr<SourceImpl> source) {}
|
2024-02-12 22:33:03 -08:00
|
|
|
|
|
|
|
|
namespace cs {
|
|
|
|
|
static constexpr unsigned SinkMask = CS_SINK_CV | CS_SINK_RAW;
|
|
|
|
|
|
|
|
|
|
void SetSinkDescription(CS_Sink sink, std::string_view description,
|
|
|
|
|
CS_Status* status) {
|
|
|
|
|
auto data = Instance::GetInstance().GetSink(sink);
|
|
|
|
|
if (!data || (data->kind & SinkMask) == 0) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
data->sink->SetDescription(description);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string GetSinkError(CS_Sink sink, CS_Status* status) {
|
|
|
|
|
auto data = Instance::GetInstance().GetSink(sink);
|
|
|
|
|
if (!data || (data->kind & SinkMask) == 0) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
return std::string{};
|
|
|
|
|
}
|
|
|
|
|
return data->sink->GetError();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string_view GetSinkError(CS_Sink sink, wpi::SmallVectorImpl<char>& buf,
|
|
|
|
|
CS_Status* status) {
|
|
|
|
|
auto data = Instance::GetInstance().GetSink(sink);
|
|
|
|
|
if (!data || (data->kind & SinkMask) == 0) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
return data->sink->GetError(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetSinkEnabled(CS_Sink sink, bool enabled, CS_Status* status) {
|
|
|
|
|
auto data = Instance::GetInstance().GetSink(sink);
|
|
|
|
|
if (!data || (data->kind & SinkMask) == 0) {
|
|
|
|
|
*status = CS_INVALID_HANDLE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
data->sink->SetEnabled(enabled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace cs
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
void CS_SetSinkDescription(CS_Sink sink, const char* description,
|
|
|
|
|
CS_Status* status) {
|
|
|
|
|
return cs::SetSinkDescription(sink, description, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* CS_GetSinkError(CS_Sink sink, CS_Status* status) {
|
|
|
|
|
wpi::SmallString<128> buf;
|
|
|
|
|
auto str = cs::GetSinkError(sink, buf, status);
|
|
|
|
|
if (*status != 0) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return cs::ConvertToC(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CS_SetSinkEnabled(CS_Sink sink, CS_Bool enabled, CS_Status* status) {
|
|
|
|
|
return cs::SetSinkEnabled(sink, enabled, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // extern "C"
|