Skip to content

Commit

Permalink
Optimize the network device enumeration code.
Browse files Browse the repository at this point in the history
  • Loading branch information
hzcyf committed Sep 14, 2024
1 parent 2d7014b commit 7e05e58
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 176 deletions.
2 changes: 1 addition & 1 deletion src/device/DeviceBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void DeviceBase::deactivate() {
while(!tempComponents.empty()) {
// The clear order should be reversed as the order of the components are added.
// Otherwise, the dependency between components may be broken and cause crash.
tempComponents.erase(components_.end() - 1);
tempComponents.erase(tempComponents.end() - 1);
}
sensorPortInfos_.clear();
}
Expand Down
12 changes: 8 additions & 4 deletions src/device/component/property/InternalProperty.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
namespace libobsensor {

typedef enum {
OB_PROP_FEMTO_MEGA_HARDWARE_D2C_BOOL = 13, /**< FemtoMega hardware d2c switch*/
OB_PROP_DEVICE_RESET_BOOL = 29, /**< Reset/reboot the device*/
OB_PROP_STOP_DEPTH_STREAM_BOOL = 38, /**<Disable the deep stream (MX6600 chip also acts as the right IR stream), used for devices that cannot disable the stream via the standard UVC protocol. */
OB_PROP_STOP_IR_STREAM_BOOL = 39, /**<Disable the IR stream (MX6600 chip also serves as the left IR stream) for devices that cannot disable the stream via the standard UVC protocol */
OB_PROP_FEMTO_MEGA_HARDWARE_D2C_BOOL = 13, /**< FemtoMega hardware d2c switch*/
OB_PROP_DEVICE_RESET_BOOL = 29, /**< Reset/reboot the device*/
OB_PROP_STOP_DEPTH_STREAM_BOOL = 38, /**<Disable the deep stream (MX6600 chip also acts as the right IR stream), used for devices that cannot disable the
stream via the standard UVC protocol. */
OB_PROP_STOP_IR_STREAM_BOOL = 39, /**<Disable the IR stream (MX6600 chip also serves as the left IR stream) for devices that cannot disable the stream via
the standard UVC protocol */
OB_PROP_TOF_EXPOSURE_TIME_INT = 47, /**<TOF exposure time // only sdk-firmware internal use */
OB_PROP_TOF_GAIN_INT = 48, /**<TOF gain value // only sdk-firmware internal use */
OB_PROP_REBOOT_DEVICE_BOOL = 57, /**< Reboot the device*/
OB_PROP_STOP_COLOR_STREAM_BOOL = 77, /**< Disable the Color stream for devices that cannot disable the stream via the standard UVC protocol*/
OB_PROP_DEVICE_COMMUNICATION_TYPE_INT = 97, // Device communication type, 0: USB; 1: Ethernet(RTSP)
OB_PROP_FAN_WORK_LEVEL_INT = 110, /**< Fan speed settings */
OB_PROP_DEVICE_PID_INT = 111, /**< Device product id */
OB_PROP_DEPTH_MIRROR_MODULE_STATUS_BOOL = 108, /**< Depth mirror module status*/
OB_PROP_FAN_WORK_SPEED_INT = 120, /**< Fan speed */

Expand Down
66 changes: 38 additions & 28 deletions src/device/devicemanager/NetDeviceEnumerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "femtomega/FemtoMegaDeviceInfo.hpp"
#include "ethernet/RTSPStreamPort.hpp"
#include "ethernet/NetDataStreamPort.hpp"
#include "property/VendorPropertyAccessor.hpp"
#include "property/InternalProperty.hpp"

#include "utils/Utils.hpp"

Expand All @@ -10,12 +12,6 @@

namespace libobsensor {

const std::map<uint16_t, std::string> pidToNameMap = {
{ static_cast<uint16_t>(0x0669), "Femto Mega" },
{ static_cast<uint16_t>(0x06c0), "Femto Mega i" },
{ static_cast<uint16_t>(0x0671), "Gemini2 XL" },
};

NetDeviceEnumerator::NetDeviceEnumerator(DeviceChangedCallback callback) : platform_(Platform::getInstance()), deviceChangedCallback_(callback) {
deviceInfoList_ = queryDeviceList();
if(!deviceInfoList_.empty()) {
Expand All @@ -40,13 +36,34 @@ NetDeviceEnumerator::~NetDeviceEnumerator() noexcept {

DeviceEnumInfoList NetDeviceEnumerator::queryDeviceList() {
std::unique_lock<std::recursive_mutex> lock(deviceInfoListMutex_);
sourcePortInfoList_ = platform_->queryNetSourcePort();
sourcePortInfoList_.clear();
auto portInfoList = platform_->queryNetSourcePort();

if(sourcePortInfoList_.empty()) {
if(portInfoList.empty()) {
LOG_DEBUG("No net source port found!");
return {};
}

for(const auto &portInfo: portInfoList) {
auto info = std::dynamic_pointer_cast<const NetSourcePortInfo>(portInfo);
if(info->pid != 0) {
sourcePortInfoList_.push_back(info);
continue;
}

// try fetch pid from device via vendor property
BEGIN_TRY_EXECUTE({
auto port = platform_->getSourcePort(info);
auto vendorPropAccessor = std::make_shared<VendorPropertyAccessor>(nullptr, port);
OBPropertyValue value;
value.intValue = 0;
vendorPropAccessor->getPropertyValue(OB_PROP_DEVICE_PID_INT, &value);
auto newInfo = std::make_shared<NetSourcePortInfo>(info->portType, info->address, info->port, info->mac, info->serialNumber, value.intValue);
sourcePortInfoList_.push_back(newInfo);
})
CATCH_EXCEPTION_AND_LOG(DEBUG, "Get device pid failed! address:{}, port:{}", info->address, info->port);
}

LOG_DEBUG("Current net source port list:");
for(const auto &item: sourcePortInfoList_) {
auto info = std::dynamic_pointer_cast<const NetSourcePortInfo>(item);
Expand All @@ -62,9 +79,8 @@ DeviceEnumInfoList NetDeviceEnumerator::getDeviceInfoList() {

DeviceEnumInfoList NetDeviceEnumerator::deviceInfoMatch(const SourcePortInfoList infoList) {
DeviceEnumInfoList deviceInfoList;
auto megaDevices = FemtoMegaDeviceInfo::pickDevices(infoList);
auto megaDevices = FemtoMegaDeviceInfo::pickNetDevices(infoList);
deviceInfoList.insert(deviceInfoList.end(), megaDevices.begin(), megaDevices.end());

return deviceInfoList;
}

Expand Down Expand Up @@ -129,26 +145,20 @@ void NetDeviceEnumerator::onPlatformDeviceChanged(OBDeviceChangedType changeType
}

std::shared_ptr<IDevice> NetDeviceEnumerator::createDevice(std::string address, uint16_t port) {
// todo: refactor this code, try get pid from device and create source port info at XxxDeviceInfo class
SourcePortInfoList list;
auto info =
std::make_shared<NetSourcePortInfo>(SOURCE_PORT_NET_VENDOR, address, static_cast<uint16_t>(8090), address + ":" + std::to_string(port), "Unknown",
static_cast<uint16_t>(0x0669)); // 0x0669 for mega pid
list.push_back(info);
list.emplace_back(std::make_shared<RTSPStreamPortInfo>(info->address, static_cast<uint16_t>(8888), info->port, OB_STREAM_COLOR, info->mac,
info->serialNumber, info->pid));
list.emplace_back(std::make_shared<RTSPStreamPortInfo>(info->address, static_cast<uint16_t>(8554), info->port, OB_STREAM_DEPTH, info->mac,
info->serialNumber, info->pid));
list.emplace_back(
std::make_shared<RTSPStreamPortInfo>(info->address, static_cast<uint16_t>(8554), info->port, OB_STREAM_IR, info->mac, info->serialNumber, info->pid));
list.emplace_back(
std::make_shared<NetDataStreamPortInfo>(info->address, static_cast<uint16_t>(8900), info->port, info->mac, info->serialNumber, info->pid));

auto deviceEnumInfoList = deviceInfoMatch(list);
auto info =
std::make_shared<NetSourcePortInfo>(SOURCE_PORT_NET_VENDOR, address, static_cast<uint16_t>(8090), address + ":" + std::to_string(port), "Unknown", 0);
auto sourcePort = Platform::getInstance()->getNetSourcePort(info);
auto vendorPropAccessor = std::make_shared<VendorPropertyAccessor>(nullptr, sourcePort);
OBPropertyValue value;
value.intValue = 0;
vendorPropAccessor->getPropertyValue(OB_PROP_DEVICE_PID_INT, &value);
info->pid = static_cast<uint16_t>(value.intValue);

auto deviceEnumInfoList = deviceInfoMatch({ info });
if(deviceEnumInfoList.empty()) {
throw std::runtime_error("No device found");
throw invalid_value_exception("No supported device found for address: " + address + ":" + std::to_string(port));
}

LOG_DEBUG("Create device for address: {}:{}", address, port);
return deviceEnumInfoList.front()->createDevice();
}

Expand Down
12 changes: 8 additions & 4 deletions src/device/femtomega/FemtoMegaDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ void FemtoMegaUsbDevice::initProperties() {
// propertyServer->registerProperty(OB_PROP_DEPTH_MAX_SPECKLE_SIZE_INT, "rw", "rw", vendorPropertyAccessor);
propertyServer->registerProperty(OB_PROP_EXTERNAL_SIGNAL_RESET_BOOL, "", "rw", vendorPropertyAccessor);
propertyServer->registerProperty(OB_PROP_HEARTBEAT_BOOL, "rw", "rw", vendorPropertyAccessor);
propertyServer->registerProperty(OB_PROP_DEVICE_COMMUNICATION_TYPE_INT, "rw", "rw", vendorPropertyAccessor);
propertyServer->registerProperty(OB_PROP_DEVICE_COMMUNICATION_TYPE_INT, "", "w", vendorPropertyAccessor);
propertyServer->registerProperty(OB_PROP_SWITCH_IR_MODE_INT, "rw", "rw", vendorPropertyAccessor);
propertyServer->registerProperty(OB_PROP_FAN_WORK_LEVEL_INT, "rw", "rw", vendorPropertyAccessor);
propertyServer->registerProperty(OB_PROP_FAN_WORK_SPEED_INT, "rw", "rw", vendorPropertyAccessor);
Expand Down Expand Up @@ -426,6 +426,9 @@ void FemtoMegaUsbDevice::initProperties() {
propertyServer->aliasProperty(OB_PROP_DEPTH_EXPOSURE_INT, OB_PROP_TOF_EXPOSURE_TIME_INT);

registerComponent(OB_DEV_COMPONENT_PROPERTY_SERVER, propertyServer, true);

BEGIN_TRY_EXECUTE({ propertyServer->setPropertyValueT(OB_PROP_DEVICE_COMMUNICATION_TYPE_INT, OB_COMM_USB); })
CATCH_EXCEPTION_AND_EXECUTE({ LOG_ERROR("Set device communication type to usb mode failed!"); })
}

FemtoMegaNetDevice::FemtoMegaNetDevice(const std::shared_ptr<const IDeviceEnumInfo> &info) : DeviceBase(info) {
Expand Down Expand Up @@ -754,7 +757,7 @@ void FemtoMegaNetDevice::initProperties() {
// propertyServer->registerProperty(OB_PROP_DEPTH_MAX_SPECKLE_SIZE_INT, "rw", "rw", vendorPropertyAccessor); //
propertyServer->registerProperty(OB_PROP_EXTERNAL_SIGNAL_RESET_BOOL, "", "rw", vendorPropertyAccessor);
propertyServer->registerProperty(OB_PROP_HEARTBEAT_BOOL, "rw", "rw", vendorPropertyAccessor);
propertyServer->registerProperty(OB_PROP_DEVICE_COMMUNICATION_TYPE_INT, "rw", "rw", vendorPropertyAccessor);
propertyServer->registerProperty(OB_PROP_DEVICE_COMMUNICATION_TYPE_INT, "", "w", vendorPropertyAccessor);
propertyServer->registerProperty(OB_PROP_SWITCH_IR_MODE_INT, "rw", "rw", vendorPropertyAccessor);
propertyServer->registerProperty(OB_PROP_FAN_WORK_LEVEL_INT, "rw", "rw", vendorPropertyAccessor);
propertyServer->registerProperty(OB_PROP_FAN_WORK_SPEED_INT, "rw", "rw", vendorPropertyAccessor);
Expand Down Expand Up @@ -847,6 +850,9 @@ void FemtoMegaNetDevice::initProperties() {
propertyServer->aliasProperty(OB_PROP_DEPTH_EXPOSURE_INT, OB_PROP_TOF_EXPOSURE_TIME_INT);

registerComponent(OB_DEV_COMPONENT_PROPERTY_SERVER, propertyServer, true);

BEGIN_TRY_EXECUTE({ propertyServer->setPropertyValueT(OB_PROP_DEVICE_COMMUNICATION_TYPE_INT, OB_COMM_NET); })
CATCH_EXCEPTION_AND_EXECUTE({ LOG_ERROR("Set device communication type to ethernet mode failed!"); })
}

void FemtoMegaNetDevice::initSensorStreamProfile(std::shared_ptr<ISensor> sensor) {
Expand Down Expand Up @@ -883,8 +889,6 @@ void FemtoMegaNetDevice::initSensorStreamProfile(std::shared_ptr<ISensor> sensor

void FemtoMegaNetDevice::fetchAllProfileList() {
auto propServer = getPropertyServer();
BEGIN_TRY_EXECUTE({ propServer->setPropertyValueT(OB_PROP_DEVICE_COMMUNICATION_TYPE_INT, OB_COMM_NET); })
CATCH_EXCEPTION_AND_EXECUTE({ LOG_ERROR("Set device ethernet mode failed!"); })

std::vector<uint8_t> data;
BEGIN_TRY_EXECUTE({
Expand Down
51 changes: 43 additions & 8 deletions src/device/femtomega/FemtoMegaDeviceInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@
#include "ethernet/NetPortGroup.hpp"
#include "utils/Utils.hpp"
#include "exception/ObException.hpp"
#include "ethernet/RTSPStreamPort.hpp"
#include "ethernet/NetDataStreamPort.hpp"
namespace libobsensor {
const std::map<int, std::string> FemtoMegaDeviceNameMap = {
{ 0x06c0, "Femto Megai" },
{ 0x0669, "Femto Mega" },
};

FemtoMegaDeviceInfo::FemtoMegaDeviceInfo(const SourcePortInfoList groupedInfoList) {
auto firstPortInfo = groupedInfoList.front();
if(IS_USB_PORT(firstPortInfo->portType)) {
auto portInfo = std::dynamic_pointer_cast<const USBSourcePortInfo>(groupedInfoList.front());

name_ = "Femto Mega";
auto iter = FemtoMegaDeviceNameMap.find(portInfo->pid);
if(iter != FemtoMegaDeviceNameMap.end()) {
name_ = iter->second;
}
else {
name_ = "Femto Mega series device";
}
fullName_ = "Orbbec " + name_;
pid_ = portInfo->pid;
vid_ = portInfo->vid;
Expand All @@ -23,7 +36,13 @@ FemtoMegaDeviceInfo::FemtoMegaDeviceInfo(const SourcePortInfoList groupedInfoLis
else if(IS_NET_PORT(firstPortInfo->portType)) {
auto portInfo = std::dynamic_pointer_cast<const NetSourcePortInfo>(groupedInfoList.front());

name_ = "Femto Mega";
auto iter = FemtoMegaDeviceNameMap.find(portInfo->pid);
if(iter != FemtoMegaDeviceNameMap.end()) {
name_ = iter->second;
}
else {
name_ = "Femto Mega series device";
}
fullName_ = "Orbbec " + name_;
pid_ = portInfo->pid;
vid_ = 0x2BC5;
Expand Down Expand Up @@ -63,18 +82,34 @@ std::vector<std::shared_ptr<IDeviceEnumInfo>> FemtoMegaDeviceInfo::pickDevices(c
iter++;
}

// pick ethernet device
remainder = FilterNetPortInfoByPid(infoList, FemtoMegaDevPids);
groups = utils::groupVector<std::shared_ptr<const SourcePortInfo>>(remainder, GroupNetSourcePortByMac);
iter = groups.begin();
return femtoMegaDeviceInfos;
}

std::vector<std::shared_ptr<IDeviceEnumInfo>> FemtoMegaDeviceInfo::pickNetDevices(const SourcePortInfoList infoList) {
std::vector<std::shared_ptr<IDeviceEnumInfo>> femtoMegaDeviceInfos;
auto remainder = FilterNetPortInfoByPid(infoList, FemtoMegaDevPids);
auto groups = utils::groupVector<std::shared_ptr<const SourcePortInfo>>(remainder, GroupNetSourcePortByMac);
auto iter = groups.begin();
while(iter != groups.end()) {
if(iter->size() >= 1) {
auto info = std::make_shared<FemtoMegaDeviceInfo>(*iter);
femtoMegaDeviceInfos.push_back(info);

auto portInfo = std::dynamic_pointer_cast<const NetSourcePortInfo>(iter->front());
iter->emplace_back(std::make_shared<RTSPStreamPortInfo>(portInfo->address, static_cast<uint16_t>(8888), portInfo->port, OB_STREAM_COLOR,
portInfo->mac, portInfo->serialNumber, portInfo->pid));
iter->emplace_back(std::make_shared<RTSPStreamPortInfo>(portInfo->address, static_cast<uint16_t>(8554), portInfo->port, OB_STREAM_DEPTH,
portInfo->mac, portInfo->serialNumber, portInfo->pid));
iter->emplace_back(std::make_shared<RTSPStreamPortInfo>(portInfo->address, static_cast<uint16_t>(8554), portInfo->port, OB_STREAM_IR, portInfo->mac,
portInfo->serialNumber, portInfo->pid));
iter->emplace_back(std::make_shared<NetDataStreamPortInfo>(portInfo->address, static_cast<uint16_t>(8900), portInfo->port, portInfo->mac,
portInfo->serialNumber, portInfo->pid)); // imu data stream

auto deviceEnumInfo = std::make_shared<FemtoMegaDeviceInfo>(*iter);
femtoMegaDeviceInfos.push_back(deviceEnumInfo);
}
iter++;
}

return femtoMegaDeviceInfos;
}

} // namespace libobsensor
1 change: 1 addition & 0 deletions src/device/femtomega/FemtoMegaDeviceInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class FemtoMegaDeviceInfo : public DeviceEnumInfoBase, public std::enable_shared

std::shared_ptr<IDevice> createDevice() const override;
static std::vector<std::shared_ptr<IDeviceEnumInfo>> pickDevices(const SourcePortInfoList infoList);
static std::vector<std::shared_ptr<IDeviceEnumInfo>> pickNetDevices(const SourcePortInfoList infoList);
};

} // namespace libobsensor
Loading

0 comments on commit 7e05e58

Please sign in to comment.