[sim] Various WebSockets fixes and enhancements (#2952)

This is a breaking change to the WebSockets layer to align it with
recent specification documentation work.

To support this, HAL SimValue changed readonly to a direction enum.
This allows specifying bidirectional in addition to input and output.

The SimValue change is specifically designed to avoid API and ABI breakage.
This is completely transparent in C++; in Java a new callback class was added,
and the old readonly functions have been marked deprecated.

A new SimValue creation function for enums allows specifying double values
for each enum value, not just strings.  This allows mapping enum values to
doubles in the WebSockets layer.

A ":" in the SimDevice name now maps it to different WebSocket types (e.g.
"Accel:Name" becomes type "Accel", device "Name").  The type is hidden
in the GUI.

Other WebSockets changes:
* Implemented match_time and game_data
* Added joystick rumble data
* Added builtin accelerometer support
* SimValue enums are mapped to string and double value on WS interface
* Added WebSockets protocol specification
* Added READMEs
This commit is contained in:
Peter Johnson
2020-12-23 15:54:11 -08:00
committed by GitHub
parent 699bbe21a4
commit 10b396b4c2
38 changed files with 1264 additions and 187 deletions

View File

@@ -122,11 +122,10 @@ void SimDeviceData::FreeDevice(HAL_SimDeviceHandle handle) {
m_deviceFreed(deviceImpl->name.c_str(), handle + 1);
}
HAL_SimValueHandle SimDeviceData::CreateValue(HAL_SimDeviceHandle device,
const char* name, bool readonly,
int32_t numOptions,
const char** options,
const HAL_Value& initialValue) {
HAL_SimValueHandle SimDeviceData::CreateValue(
HAL_SimDeviceHandle device, const char* name, int32_t direction,
int32_t numOptions, const char** options, const double* optionValues,
const HAL_Value& initialValue) {
std::scoped_lock lock(m_mutex);
// look up device
@@ -142,7 +141,7 @@ HAL_SimValueHandle SimDeviceData::CreateValue(HAL_SimDeviceHandle device,
if (deviceImpl->values.size() >= 4095) return 0;
// create and save; encode device into handle
auto valueImplPtr = std::make_unique<Value>(name, readonly, initialValue);
auto valueImplPtr = std::make_unique<Value>(name, direction, initialValue);
Value* valueImpl = valueImplPtr.get();
HAL_SimValueHandle valueHandle =
(device << 16) |
@@ -159,10 +158,14 @@ HAL_SimValueHandle SimDeviceData::CreateValue(HAL_SimDeviceHandle device,
valueImpl->enumOptions.back().c_str());
}
}
// copy option values (if any provided)
if (numOptions > 0 && optionValues) {
valueImpl->enumOptionValues.assign(optionValues, optionValues + numOptions);
}
deviceImpl->valueMap[name] = valueImpl;
// notify callbacks
deviceImpl->valueCreated(name, valueHandle, readonly, &initialValue);
deviceImpl->valueCreated(name, valueHandle, direction, &initialValue);
return valueHandle;
}
@@ -191,7 +194,7 @@ void SimDeviceData::SetValue(HAL_SimValueHandle handle,
// notify callbacks
valueImpl->changed(valueImpl->name.c_str(), valueImpl->handle,
valueImpl->readonly, &value);
valueImpl->direction, &value);
}
int32_t SimDeviceData::RegisterDeviceCreatedCallback(
@@ -273,7 +276,7 @@ int32_t SimDeviceData::RegisterValueCreatedCallback(
// initial notifications
if (initialNotify) {
for (auto&& value : deviceImpl->values)
callback(value->name.c_str(), param, value->handle, value->readonly,
callback(value->name.c_str(), param, value->handle, value->direction,
&value->value);
}
@@ -302,7 +305,7 @@ int32_t SimDeviceData::RegisterValueChangedCallback(
// initial notification
if (initialNotify)
callback(valueImpl->name.c_str(), param, valueImpl->handle,
valueImpl->readonly, &valueImpl->value);
valueImpl->direction, &valueImpl->value);
// encode device and value into uid
return (((handle >> 16) & 0xfff) << 19) | ((handle & 0xfff) << 7) |
@@ -337,7 +340,7 @@ void SimDeviceData::EnumerateValues(HAL_SimDeviceHandle device, void* param,
if (!deviceImpl) return;
for (auto&& value : deviceImpl->values)
callback(value->name.c_str(), param, value->handle, value->readonly,
callback(value->name.c_str(), param, value->handle, value->direction,
&value->value);
}
@@ -355,6 +358,20 @@ const char** SimDeviceData::GetValueEnumOptions(HAL_SimValueHandle handle,
return options.data();
}
const double* SimDeviceData::GetValueEnumDoubleValues(HAL_SimValueHandle handle,
int32_t* numOptions) {
*numOptions = 0;
std::scoped_lock lock(m_mutex);
Value* valueImpl = LookupValue(handle);
if (!valueImpl) return nullptr;
// get list of option values (safe to return as they never change)
auto& optionValues = valueImpl->enumOptionValues;
*numOptions = optionValues.size();
return optionValues.data();
}
void SimDeviceData::ResetData() {
std::scoped_lock lock(m_mutex);
m_devices.clear();
@@ -452,6 +469,11 @@ const char** HALSIM_GetSimValueEnumOptions(HAL_SimValueHandle handle,
return SimSimDeviceData->GetValueEnumOptions(handle, numOptions);
}
const double* HALSIM_GetSimValueEnumDoubleValues(HAL_SimValueHandle handle,
int32_t* numOptions) {
return SimSimDeviceData->GetValueEnumDoubleValues(handle, numOptions);
}
void HALSIM_ResetSimDeviceData(void) { SimSimDeviceData->ResetData(); }
} // extern "C"