From 06587023febc2736af16cb1a3bd65cd49109edd3 Mon Sep 17 00:00:00 2001 From: Kevron Rees Date: Fri, 8 Aug 2014 16:34:22 -0700 Subject: [PATCH] Some fixes and performance tweaks. - refactor dbus signaler to use objPath map (performance) - refactored murphy registration (fix) - cleaned up style in murphy - refactored TransmissionMode a bit and added w3c support --- lib/superptr.hpp | 46 ++++---- lib/vehicleproperty.cpp | 47 ++++---- lib/vehicleproperty.h | 61 +++++++--- plugins/dbus/dbussignaller.h | 94 +++++++-------- plugins/dbus/runningstatus.h | 3 + plugins/murphyplugin/murphysource.cpp | 208 ++++++++++++++++++---------------- 6 files changed, 259 insertions(+), 200 deletions(-) diff --git a/lib/superptr.hpp b/lib/superptr.hpp index 6987124..53de9ba 100644 --- a/lib/superptr.hpp +++ b/lib/superptr.hpp @@ -13,59 +13,63 @@ template struct traits; template<> struct traits { struct delete_functor { - void operator()(GVariant * p) const { - if (p != nullptr) - g_variant_unref(p); - } + void operator()(GVariant * p) const { + if (p != nullptr) + g_variant_unref(p); + } }; }; template<> struct traits { struct delete_functor { - void operator()(GError * p) const { - if (p != nullptr) - g_error_free(p); - } + void operator()(GError * p) const { + if (p != nullptr) + g_error_free(p); + } }; }; template<> struct traits { struct delete_functor { - void operator()(GDBusProxy * p) const { - if (p != nullptr) - g_object_unref(p); - } + void operator()(GDBusProxy * p) const { + if (p != nullptr) + g_object_unref(p); + } }; }; template<> struct traits { struct delete_functor { - void operator()(GVariantIter * p) const { - if (p != nullptr) - g_variant_iter_free(p); - } + void operator()(GVariantIter * p) const { + if (p != nullptr) + g_variant_iter_free(p); + } }; }; template<> struct traits { struct delete_functor { - void operator()(gchar * p) const { - if (p != nullptr) - g_free(p); - } + void operator()(gchar * p) const { + if (p != nullptr) + g_free(p); + } }; }; template using super_ptr = - ::std::unique_ptr::delete_functor>; + ::std::unique_ptr::delete_functor>; template super_ptr make_super(T* t) { return super_ptr(t); } +template unique_ptr make_unique(T* t) { + return unique_ptr(t); +} + } #endif diff --git a/lib/vehicleproperty.cpp b/lib/vehicleproperty.cpp index 46ab576..ad7feec 100644 --- a/lib/vehicleproperty.cpp +++ b/lib/vehicleproperty.cpp @@ -1,19 +1,19 @@ /* - Copyright (C) 2012 Intel Corporation + Copyright (C) 2012 Intel Corporation - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -36,12 +36,20 @@ std::map, Transmission::TransmissionPositions) //typedef BasicPropertyType TransmissionGearPositionType; + /// TODO: deprecate in 0.13. Replaced by Drive::Mode static const Property TransmissionMode; PROPERTYTYPE(TransmissionMode, TransmissionModeType, BasicPropertyType, Transmission::Mode) //typedef BasicPropertyType TransmissionModeType; + /**< TransmissionModeW3C + * see Transmission::W3C::Park, Transmission::W3C::Drive, etc + */ + static const Property TransmissionModeW3C; + PROPERTYTYPE(TransmissionModeW3C, TransmissionModeW3CType, StringPropertyType, std::string) + /**< Throttle position 0-100% */ static const Property ThrottlePosition; PROPERTYTYPE(ThrottlePosition, ThrottlePositionType, BasicPropertyType , uint16_t) @@ -472,7 +501,7 @@ public: /**< Tire temperature in degrees C */ static const Property TireTemperature; PROPERTYTYPEBASIC(TireTemperature, double) - + /**< Vehicle Power Mode. *@see Power::PowerModes */ @@ -556,7 +585,7 @@ public: static const Property ExteriorBrightness; PROPERTYTYPE(ExteriorBrightness, ExteriorBrightnessType, BasicPropertyType, uint16_t) //typedef BasicPropertyType ExteriorBrightnessType; - + static const Property Latitude; PROPERTYTYPE(Latitude, LatitudeType, BasicPropertyType, double) //typedef BasicPropertyType LatitudeType; diff --git a/plugins/dbus/dbussignaller.h b/plugins/dbus/dbussignaller.h index 3321e39..a4d5da5 100644 --- a/plugins/dbus/dbussignaller.h +++ b/plugins/dbus/dbussignaller.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "debugout.h" #include "abstractproperty.h" @@ -54,18 +55,25 @@ public: { DBusSignal * signal = new DBusSignal(conn, objPath, iface, sigName, prop); - bool isFound = false; - for(auto i : queue) + if(queue.find(objPath) != queue.end()) { - if(*i == *signal) + bool isFound = false; + for(auto i : queue[objPath]) { - isFound = true; - break; + if(*i == *signal) + { + isFound = true; + break; + } } - } - if(!isFound) - queue.push_back(signal); + if(!isFound) + queue[objPath].push_back(signal); + } + else + { + queue[objPath].push_back(signal); + } } private: @@ -76,59 +84,55 @@ private: { g_timeout_add(timeout,[](gpointer userData) { - std::vector *q = static_cast*>(userData); - std::vector queue = *q; + std::unordered_map> *q = static_cast>*>(userData); + std::unordered_map> queue = *q; - for(auto s : queue) + for(auto itr : queue) { - std::unique_ptr signal(s); + std::string objectPath; + GDBusConnection* connection; + std::string interfaceName; + std::string signalName; - /// Combine this with any other signals to be fired from the same object: - std::vector dupprops; - std::vector toRemoveQueue; - for(auto s2 : queue) - { - if(s != s2 && s2->objectPath == s2->objectPath) - { - std::unique_ptr signal2(s2); - dupprops.push_back(signal2->property); - toRemoveQueue.push_back(signal2.get()); - } - } + std::unordered_map variantMap; - for(auto toRemove : toRemoveQueue) + for(auto s : itr.second) { - removeOne(&queue, toRemove); - } - - GError* error = nullptr; - - AbstractProperty* property = signal->property; + std::unique_ptr signal(s); + objectPath = signal->objectPath; + connection = signal->connection; + interfaceName = signal->interface; + signalName = signal->signalName; + AbstractProperty* property = signal->property; - auto val = amb::make_super(property->toGVariant()); + auto val = g_variant_ref(property->toGVariant()); - /// Send PropertiesChanged signal + variantMap[property->name().c_str()] = val; + variantMap[std::string(property->name() + "Sequence").c_str()] = g_variant_new("i", property->sequence()); + variantMap["Time"] = g_variant_new("d", property->timestamp()); + variantMap["Zone"] = g_variant_new("i", property->value()->zone); + } GVariantBuilder builder; g_variant_builder_init(&builder, G_VARIANT_TYPE_DICTIONARY); - g_variant_builder_add(&builder, "{sv}", property->name().c_str(), val.get()); - - for(auto v : dupprops) + for(auto sv : variantMap) { - auto var = amb::make_super(v->toGVariant()); - g_variant_builder_add(&builder, "{sv}", v->name().c_str(), var.get()); + /// Send PropertiesChanged signal + //auto key = amb::make_unique(sv.first); + auto key = sv.first; + auto value = amb::make_super(g_variant_ref(sv.second)); + + g_variant_builder_add(&builder, "{sv}", key, value.get()); } - g_variant_builder_add(&builder, "{sv}", std::string(property->name() + "Sequence").c_str(), g_variant_new("i", property->sequence())); - g_variant_builder_add(&builder, "{sv}", "Time", g_variant_new("d", property->timestamp()) ); - g_variant_builder_add(&builder, "{sv}", "Zone", g_variant_new("i", property->value()->zone) ); + GError* error = nullptr; - g_dbus_connection_emit_signal(signal->connection, NULL, signal->objectPath.c_str(), + g_dbus_connection_emit_signal(connection, NULL, objectPath.c_str(), "org.freedesktop.DBus.Properties", - signal->signalName.c_str(), - g_variant_new("(sa{sv}as)", signal->interface.c_str(), &builder, NULL), + signalName.c_str(), + g_variant_new("(sa{sv}as)", interfaceName.c_str(), &builder, NULL), &error); auto errorPtr = amb::make_super(error); @@ -146,7 +150,7 @@ private: },&queue); } - std::vector queue; + std::unordered_map> queue; static DBusSignaller * singleton; }; diff --git a/plugins/dbus/runningstatus.h b/plugins/dbus/runningstatus.h index a485233..2ad2bd2 100644 --- a/plugins/dbus/runningstatus.h +++ b/plugins/dbus/runningstatus.h @@ -154,6 +154,9 @@ public: wantPropertyVariant(VehicleProperty::TransmissionGearPosition, "GearPosition", "y", AbstractProperty::Read); + wantPropertyVariant(VehicleProperty::TransmissionGearPosition, + "Gear", "y", AbstractProperty::Read); + wantPropertyVariant(VehicleProperty::TransmissionMode, "Mode", "y", AbstractProperty::Read); diff --git a/plugins/murphyplugin/murphysource.cpp b/plugins/murphyplugin/murphysource.cpp index bc9903d..3476e85 100644 --- a/plugins/murphyplugin/murphysource.cpp +++ b/plugins/murphyplugin/murphysource.cpp @@ -43,11 +43,13 @@ void MurphySource::processValue(string propertyName, AbstractPropertyType *prop) { - if (murphyProperties.find(propertyName) != murphyProperties.end()) { + if (murphyProperties.find(propertyName) != murphyProperties.end()) + { delete murphyProperties[propertyName]; murphyProperties[propertyName] = prop; } - else { + else + { murphyProperties[propertyName] = prop; m_re->updateSupported(supported(), PropertyList(), this); } @@ -94,116 +96,113 @@ static void recvfrom_msg(mrp_transport_t *transp, mrp_msg_t *msg, DebugOut() << "Property '" << property_name << "' with value: " <hasProperty(dstr)) { - VehicleProperty::registerProperty(dstr, - [dstr](){return new StringPropertyType(dstr, "");}); - } + stringstream val; - DebugOut() << "string:" << value.str << std::endl; - s->processValue(property_name, prop); - break; - } - case MRP_MSG_FIELD_DOUBLE: + switch (type) + { + case MRP_MSG_FIELD_STRING: + { + val << value.str; + if (!hasProp) { - BasicPropertyType *prop = - new BasicPropertyType(dstr, value.dbl); + VehicleProperty::registerProperty(dstr, + [dstr](){return new StringPropertyType(dstr, "");}); + } - if (!s->hasProperty(dstr)) { - VehicleProperty::registerProperty(dstr, - [dstr](){return new BasicPropertyType(dstr, 0);}); - } + DebugOut() << "string:" << value.str << std::endl; + break; + } + case MRP_MSG_FIELD_DOUBLE: + { + val << value.dbl; - DebugOut() << "double:" << value.dbl << std::endl; - s->processValue(property_name, prop); - break; - } - case MRP_MSG_FIELD_BOOL: + if (!hasProp) { - BasicPropertyType *prop = - new BasicPropertyType(dstr, value.bln); + VehicleProperty::registerProperty(dstr, + [dstr](){return new BasicPropertyType(dstr, 0);}); + } - if (!s->hasProperty(dstr)) { - VehicleProperty::registerProperty(dstr, - [dstr](){return new BasicPropertyType(dstr, FALSE);}); - } + DebugOut() << "double:" << value.dbl << std::endl; + break; + } + case MRP_MSG_FIELD_BOOL: + { + val << value.bln; - DebugOut() << "boolean:" << value.bln << std::endl; - s->processValue(property_name, prop); - break; - } - case MRP_MSG_FIELD_UINT32: + if (!hasProp) { - BasicPropertyType *prop = - new BasicPropertyType(dstr, value.u32); + VehicleProperty::registerProperty(dstr, + [dstr](){return new BasicPropertyType(dstr, FALSE);}); + } - if (!s->hasProperty(dstr)) { - VehicleProperty::registerProperty(dstr, - [dstr](){return new BasicPropertyType(dstr, 0);}); - } + DebugOut() << "boolean:" << value.bln << std::endl; + break; + } + case MRP_MSG_FIELD_UINT32: + { + val << value.u32; - DebugOut() << "uint32:" << value.u32 << std::endl; - s->processValue(property_name, prop); - break; - } - case MRP_MSG_FIELD_UINT16: + if (!hasProp) { - BasicPropertyType *prop = - new BasicPropertyType(dstr, value.u16); + VehicleProperty::registerProperty(dstr, + [dstr](){return new BasicPropertyType(dstr, 0);}); + } - if (!s->hasProperty(dstr)) { - VehicleProperty::registerProperty(dstr, - [dstr](){return new BasicPropertyType(dstr, 0);}); - } + DebugOut() << "uint32:" << value.u32 << std::endl; + break; + } + case MRP_MSG_FIELD_UINT16: + { + val << value.u16; - DebugOut() << "uint16:" << value.u16 << std::endl; - s->processValue(property_name, prop); - break; - } - case MRP_MSG_FIELD_INT32: + if (!hasProp) { - BasicPropertyType *prop = - new BasicPropertyType(dstr, value.s32); + VehicleProperty::registerProperty(dstr, + [dstr](){return new BasicPropertyType(dstr, 0);}); + } - if (!s->hasProperty(dstr)) { - VehicleProperty::registerProperty(dstr, - [dstr](){return new BasicPropertyType(dstr, 0);}); - } + DebugOut() << "uint16:" << value.u16 << std::endl; - DebugOut() << "int32:" << value.s32 << std::endl; - s->processValue(property_name, prop); - break; - } - case MRP_MSG_FIELD_INT16: + break; + } + case MRP_MSG_FIELD_INT32: + { + val << value.s32; + + if (!hasProp) { - BasicPropertyType *prop = - new BasicPropertyType(dstr, value.s16); + VehicleProperty::registerProperty(dstr, + [dstr](){return new BasicPropertyType(dstr, 0);}); + } - if (!s->hasProperty(dstr)) { - VehicleProperty::registerProperty(dstr, - [dstr](){return new BasicPropertyType(dstr, 0);}); - } + DebugOut() << "int32:" << value.s32 << std::endl; + s->processValue(property_name, prop); + break; + } + case MRP_MSG_FIELD_INT16: + { + val << value.s16; - DebugOut() << "int16:" << value.s16 << std::endl; - s->processValue(property_name, prop); - break; + if (!hasProp) + { + VehicleProperty::registerProperty(dstr, + [dstr](){return new BasicPropertyType(dstr, 0);}); } - default: - DebugOut()<<"Unknown type"<processValue(property_name, prop); - } + + prop->fromString(val.str()); + s->processValue(property_name, prop); + } static void recv_msg(mrp_transport_t *transp, mrp_msg_t *msg, void *user_data) @@ -248,19 +247,22 @@ int MurphySource::connectToMurphy() alen = mrp_transport_resolve(NULL, m_address.c_str(), &addr, sizeof(addr), &atype); - if (alen <= 0) { + if (alen <= 0) + { debugOut("Failed to resolve address"); return -1; } m_tport = mrp_transport_create(m_ml, atype, &evt, this, flags); - if (!m_tport) { + if (!m_tport) + { debugOut("Can't create a Murphy transport"); return -1; } - if (mrp_transport_connect(m_tport, &addr, alen) == 0) { + if (mrp_transport_connect(m_tport, &addr, alen) == 0) + { mrp_transport_destroy(m_tport); m_tport = NULL; debugOut("Failed to connect to Murphy"); @@ -301,7 +303,8 @@ MurphySource::~MurphySource() map::iterator i; - for (i = murphyProperties.begin(); i != murphyProperties.end(); i++) { + for (i = murphyProperties.begin(); i != murphyProperties.end(); i++) + { // TODO: unregister VehicleProperty (*i).first delete (*i).second; @@ -331,11 +334,13 @@ static void murphy_watch(const char *id, mrp_process_state_t state, void *user_d state == MRP_PROCESS_STATE_READY ? "ready" : "not ready"); if (state == MRP_PROCESS_STATE_NOT_READY && - s->getState() == MRP_PROCESS_STATE_READY) { + s->getState() == MRP_PROCESS_STATE_READY) + { DebugOut()<<"lost connection to murphyd"<connectToMurphy(); } @@ -347,7 +352,8 @@ void MurphySource::readyToConnect(mrp_mainloop_t *ml) { /* set a watch to follow Murphy status */ - if (mrp_process_set_watch("murphy-amb", ml, murphy_watch, this) < 0) { + if (mrp_process_set_watch("murphy-amb", ml, murphy_watch, this) < 0) + { DebugOut()<<"failed to set a murphy process watch"< config) { string address; - for (map::iterator i=configuration.begin();i!=configuration.end();i++) { - if ((*i).first == "address") { + for (map::iterator i=configuration.begin();i!=configuration.end();i++) + { + if ((*i).first == "address") + { address = (*i).second; // cout << "address: " << address << endl; @@ -392,7 +400,8 @@ PropertyList MurphySource::supported() PropertyList properties; map::iterator i; - for (i = murphyProperties.begin(); i != murphyProperties.end(); i++) { + for (i = murphyProperties.begin(); i != murphyProperties.end(); i++) + { properties.push_back((*i).first); } @@ -424,7 +433,8 @@ void MurphySource::getPropertyAsync(AsyncPropertyReply *reply) { // debugOut("> getPropertyAsync"); - if (murphyProperties.find(reply->property) != murphyProperties.end()) { + if (murphyProperties.find(reply->property) != murphyProperties.end()) + { AbstractPropertyType *prop = murphyProperties[reply->property]; reply->value = prop; reply->success = true; -- 2.7.4