Skip to content

Commit

Permalink
Profile and adjust initial flatbuffer message sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Jan 29, 2025
1 parent d6a1e5a commit 2281204
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
4 changes: 3 additions & 1 deletion include/serialization/WSLocal.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

#include <esp_wifi_types.h>

#include <string_view>

namespace OpenShock {
class WiFiNetwork;
}

namespace OpenShock::Serialization::Local {
bool SerializeErrorMessage(const char* message, Common::SerializationCallbackFn callback);
bool SerializeErrorMessage(std::string_view message, Common::SerializationCallbackFn callback);
bool SerializeReadyMessage(const WiFiNetwork* connectedNetwork, bool accountLinked, Common::SerializationCallbackFn callback);
bool SerializeWiFiScanStatusChangedEvent(OpenShock::WiFiScanStatus status, Common::SerializationCallbackFn callback);
bool SerializeWiFiNetworkEvent(Types::WifiNetworkEventType eventType, const WiFiNetwork& network, Common::SerializationCallbackFn callback);
Expand Down
10 changes: 5 additions & 5 deletions src/serialization/WSGateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bool Gateway::SerializePongMessage(Common::SerializationCallbackFn callback)
return false;
}

flatbuffers::FlatBufferBuilder builder(256); // TODO: Profile this and adjust the size accordingly
flatbuffers::FlatBufferBuilder builder(64);

auto pong = Gateway::CreatePong(builder, static_cast<uint64_t>(uptime), static_cast<int32_t>(rssi));

Expand All @@ -40,7 +40,7 @@ bool Gateway::SerializePongMessage(Common::SerializationCallbackFn callback)

bool Gateway::SerializeBootStatusMessage(int32_t updateId, OpenShock::FirmwareBootType bootType, Common::SerializationCallbackFn callback)
{
flatbuffers::FlatBufferBuilder builder(256); // TODO: Profile this and adjust the size accordingly
flatbuffers::FlatBufferBuilder builder(128);

auto fbsVersion = Types::CreateSemVerDirect(builder, OPENSHOCK_FW_VERSION_MAJOR, OPENSHOCK_FW_VERSION_MINOR, OPENSHOCK_FW_VERSION_PATCH, OPENSHOCK_FW_VERSION_PRERELEASE, OPENSHOCK_FW_VERSION_BUILD);

Expand All @@ -57,7 +57,7 @@ bool Gateway::SerializeBootStatusMessage(int32_t updateId, OpenShock::FirmwareBo

bool Gateway::SerializeOtaUpdateStartedMessage(int32_t updateId, const OpenShock::SemVer& version, Common::SerializationCallbackFn callback)
{
flatbuffers::FlatBufferBuilder builder(256); // TODO: Profile this and adjust the size accordingly
flatbuffers::FlatBufferBuilder builder(128);

auto versionOffset = Types::CreateSemVerDirect(builder, version.major, version.minor, version.patch, version.prerelease.data(), version.build.data());

Expand All @@ -74,7 +74,7 @@ bool Gateway::SerializeOtaUpdateStartedMessage(int32_t updateId, const OpenShock

bool Gateway::SerializeOtaUpdateProgressMessage(int32_t updateId, Types::OtaUpdateProgressTask task, float progress, Common::SerializationCallbackFn callback)
{
flatbuffers::FlatBufferBuilder builder(64); // TODO: Profile this and adjust the size accordingly
flatbuffers::FlatBufferBuilder builder(64);

auto otaUpdateProgressOffset = Gateway::CreateOtaUpdateProgress(builder, updateId, task, progress);

Expand All @@ -89,7 +89,7 @@ bool Gateway::SerializeOtaUpdateProgressMessage(int32_t updateId, Types::OtaUpda

bool Gateway::SerializeOtaUpdateFailedMessage(int32_t updateId, std::string_view message, bool fatal, Common::SerializationCallbackFn callback)
{
flatbuffers::FlatBufferBuilder builder(256); // TODO: Profile this and adjust the size accordingly
flatbuffers::FlatBufferBuilder builder(128 + message.size());

auto messageOffset = builder.CreateString(message.data(), message.size());

Expand Down
37 changes: 21 additions & 16 deletions src/serialization/WSLocal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ using namespace OpenShock::Serialization;

typedef OpenShock::Serialization::Types::WifiAuthMode WiFiAuthMode;

constexpr WiFiAuthMode GetWiFiAuthModeEnum(wifi_auth_mode_t authMode) {
constexpr WiFiAuthMode GetWiFiAuthModeEnum(wifi_auth_mode_t authMode)
{
switch (authMode) {
case wifi_auth_mode_t::WIFI_AUTH_OPEN:
return WiFiAuthMode::Open;
Expand All @@ -39,31 +40,32 @@ constexpr WiFiAuthMode GetWiFiAuthModeEnum(wifi_auth_mode_t authMode) {
}
}

flatbuffers::Offset<OpenShock::Serialization::Types::WifiNetwork> _createWiFiNetwork(flatbuffers::FlatBufferBuilder& builder, const OpenShock::WiFiNetwork& network) {
flatbuffers::Offset<OpenShock::Serialization::Types::WifiNetwork> _createWiFiNetwork(flatbuffers::FlatBufferBuilder& builder, const OpenShock::WiFiNetwork& network)
{
auto bssid = network.GetHexBSSID();
auto authMode = GetWiFiAuthModeEnum(network.authMode);

return Types::CreateWifiNetworkDirect(builder, network.ssid, bssid.data(), network.channel, network.rssi, authMode, network.IsSaved());
}

bool Local::SerializeErrorMessage(const char* message, Common::SerializationCallbackFn callback) {
flatbuffers::FlatBufferBuilder builder(256); // TODO: Profile this and adjust the size accordingly
bool Local::SerializeErrorMessage(std::string_view message, Common::SerializationCallbackFn callback)
{
flatbuffers::FlatBufferBuilder builder(64 + message.length());

auto wrapperOffset = Local::CreateErrorMessage(builder, builder.CreateString(message));
auto wrapperOffset = Local::CreateErrorMessage(builder, builder.CreateString(message.data(), message.length()));

auto msg = Local::CreateHubToLocalMessage(builder, Local::HubToLocalMessagePayload::ErrorMessage, wrapperOffset.Union());

Serialization::Local::FinishHubToLocalMessageBuffer(builder, msg);

auto span = builder.GetBufferSpan();

callback(span.data(), span.size());

return true;
return callback(span.data(), span.size());
}

bool Local::SerializeReadyMessage(const WiFiNetwork* connectedNetwork, bool accountLinked, Common::SerializationCallbackFn callback) {
flatbuffers::FlatBufferBuilder builder(256);
bool Local::SerializeReadyMessage(const WiFiNetwork* connectedNetwork, bool accountLinked, Common::SerializationCallbackFn callback)
{
flatbuffers::FlatBufferBuilder builder(768);

flatbuffers::Offset<Serialization::Types::WifiNetwork> fbsNetwork = 0;

Expand Down Expand Up @@ -96,8 +98,9 @@ bool Local::SerializeReadyMessage(const WiFiNetwork* connectedNetwork, bool acco
return callback(span.data(), span.size());
}

bool Local::SerializeWiFiScanStatusChangedEvent(OpenShock::WiFiScanStatus status, Common::SerializationCallbackFn callback) {
flatbuffers::FlatBufferBuilder builder(32); // TODO: Profile this and adjust the size accordingly
bool Local::SerializeWiFiScanStatusChangedEvent(OpenShock::WiFiScanStatus status, Common::SerializationCallbackFn callback)
{
flatbuffers::FlatBufferBuilder builder(64);

auto scanStatusOffset = Serialization::Local::CreateWifiScanStatusMessage(builder, status);

Expand All @@ -110,8 +113,9 @@ bool Local::SerializeWiFiScanStatusChangedEvent(OpenShock::WiFiScanStatus status
return callback(span.data(), span.size());
}

bool Local::SerializeWiFiNetworkEvent(Types::WifiNetworkEventType eventType, const WiFiNetwork& network, Common::SerializationCallbackFn callback) {
flatbuffers::FlatBufferBuilder builder(256); // TODO: Profile this and adjust the size accordingly
bool Local::SerializeWiFiNetworkEvent(Types::WifiNetworkEventType eventType, const WiFiNetwork& network, Common::SerializationCallbackFn callback)
{
flatbuffers::FlatBufferBuilder builder(64);

auto networkOffset = _createWiFiNetwork(builder, network);

Expand All @@ -126,8 +130,9 @@ bool Local::SerializeWiFiNetworkEvent(Types::WifiNetworkEventType eventType, con
return callback(span.data(), span.size());
}

bool Local::SerializeWiFiNetworksEvent(Types::WifiNetworkEventType eventType, const std::vector<WiFiNetwork>& networks, Common::SerializationCallbackFn callback) {
flatbuffers::FlatBufferBuilder builder(256); // TODO: Profile this and adjust the size accordingly
bool Local::SerializeWiFiNetworksEvent(Types::WifiNetworkEventType eventType, const std::vector<WiFiNetwork>& networks, Common::SerializationCallbackFn callback)
{
flatbuffers::FlatBufferBuilder builder(256);

std::vector<flatbuffers::Offset<Serialization::Types::WifiNetwork>> fbsNetworks;
fbsNetworks.reserve(networks.size());
Expand Down

0 comments on commit 2281204

Please sign in to comment.