Skip to content

Commit

Permalink
Add WinRT event handlers to nocli service range start command (#312)
Browse files Browse the repository at this point in the history
* Add WinRT event handlers.
* Fix bad access of moved-from variable.
  • Loading branch information
abeltrano committed Jun 20, 2023
1 parent d20e5a9 commit 2c0506a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion windows/devices/uwb/UwbSession.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using namespace windows::devices::uwb;
using namespace ::uwb::protocol::fira;

UwbSession::UwbSession(uint32_t sessionId, std::weak_ptr<::uwb::UwbDevice> device, std::shared_ptr<IUwbSessionDdiConnector> uwbSessionConnector, std::weak_ptr<::uwb::UwbSessionEventCallbacks> callbacks, ::uwb::protocol::fira::DeviceType deviceType) :
::uwb::UwbSession(sessionId, std::move(device), std::move(callbacks), deviceType),
::uwb::UwbSession(sessionId, std::move(device), callbacks, deviceType),
m_uwbSessionConnector(std::move(uwbSessionConnector))
{
m_onSessionEndedCallback =
Expand Down
44 changes: 42 additions & 2 deletions windows/tools/nocli/NearObjectCliHandlerWindows.cxx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

#include <format>
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
#include <optional>
#include <string_view>

#include <magic_enum.hpp>
Expand All @@ -14,9 +15,9 @@
#include <windows/devices/uwb/UwbDevice.hxx>
#include <windows/devices/uwb/UwbDeviceDriver.hxx>

#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/windows.devices.nearobject.h>

#include "NearObjectCliDataWindows.hxx"
#include "NearObjectCliHandlerWindows.hxx"
Expand Down Expand Up @@ -168,6 +169,45 @@ NearObjectCliHandlerWindows::HandleStartRanging(::uwb::protocol::fira::DeviceTyp
m_sessionClient = sessionCreateResult.HostClient();
}

auto session = sessionCreateResult.Session();

// Register event handlers.
m_sessionEndedEventToken = session.SessionEnded(winrt::auto_revoke, [session](auto&& sender, auto&& sessionEndedEventArgs) {
PLOG_INFO << std::format("session {} ended, reason={}", session.Id(), magic_enum::enum_name(sessionEndedEventArgs.EndReason()));
});
m_rangingStartedEventToken = session.RangingStarted(winrt::auto_revoke, [session](auto&& sender, [[maybe_unused]] auto&& rangingStartedEventArgs) {
PLOG_INFO << std::format("sesion {} ranging started", session.Id());
});
m_rangingStoppedEventToken = session.RangingStopped(winrt::auto_revoke, [session]([[maybe_unused]] auto&& sender, [[maybe_unused]] auto&& rangingStoppedEventArgs) {
PLOG_INFO << std::format("sesion {} ranging stopped", session.Id());
});
m_peerPropertiesChangedEventToken = session.PeerPropertiesChanged(winrt::auto_revoke, [session](auto&& sender, auto&& peerPropertiesChangedEventArgs) {
PLOG_INFO << std::format("session {} peer properties changed:", session.Id());
for (auto&& nearObject : peerPropertiesChangedEventArgs.NearObjectsChanged()) {
auto spatialProperties = nearObject.SpatialProperties();
std::optional<double> distance = spatialProperties.Distance();
std::optional<double> angleAzimuth = spatialProperties.AngleAzimuth();
std::optional<double> angleElevation = spatialProperties.AngleElevation();
std::optional<double> elevation = spatialProperties.Elevation();
PLOG_INFO << std::format("Distance {} AngleAzimuth {} AngleElevation {} Elevation {}",
distance.has_value() ? std::to_string(distance.value()) : std::string("-"),
angleAzimuth.has_value() ? std::to_string(angleAzimuth.value()) : std::string("-"),
angleElevation.has_value() ? std::to_string(angleElevation.value()) : std::string("-"),
elevation.has_value() ? std::to_string(elevation.value()) : "-");
}
});
m_sessionMemembershipChangedEventToken = session.SessionMembershipChanged(winrt::auto_revoke, [session](auto&& sender, auto&& sessionMembershipChangedEventArgs) {
auto nearObjectsAdded = sessionMembershipChangedEventArgs.NearObjectsAdded();
auto nearObjectsRemoved = sessionMembershipChangedEventArgs.NearObjectsRemoved();
PLOG_INFO << std::format("session {} session membership changed: +{} -{}", session.Id(), nearObjectsAdded.Size(), nearObjectsRemoved.Size());
for (auto&& nearObjectAdded : nearObjectsAdded) {
// TODO: print out mac address when added to NearObject
}
for (auto&& nearObjectRemoved : nearObjectsRemoved) {
// TODO: print out mac address when added to NearObject
}
});

// Start the ranging session
if (m_sessionClient != nullptr) {
m_sessionClient.Start();
Expand Down
7 changes: 7 additions & 0 deletions windows/tools/nocli/NearObjectCliHandlerWindows.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ private:
private:
std::vector<std::shared_ptr<windows::devices::uwb::UwbDevice>> m_uwbDevices;
winrt::Windows::Devices::NearObject::INearObjectSessionClient m_sessionClient;

// Event handler tokens.
winrt::Windows::Devices::NearObject::INearObjectSessionEventProducer::SessionEnded_revoker m_sessionEndedEventToken;
winrt::Windows::Devices::NearObject::INearObjectSessionEventProducer::RangingStarted_revoker m_rangingStartedEventToken;
winrt::Windows::Devices::NearObject::INearObjectSessionEventProducer::RangingStopped_revoker m_rangingStoppedEventToken;
winrt::Windows::Devices::NearObject::INearObjectSessionEventProducer::PeerPropertiesChanged_revoker m_peerPropertiesChangedEventToken;
winrt::Windows::Devices::NearObject::INearObjectSessionEventProducer::SessionMembershipChanged_revoker m_sessionMemembershipChangedEventToken;
};
} // namespace nearobject::cli

Expand Down

0 comments on commit 2c0506a

Please sign in to comment.