Merge pull request #69 from fredcadete/master
[profile/ivi/automotive-message-broker.git] / plugins / exampleplugin.cpp
index 26bc53a..6ea63cd 100644 (file)
@@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 #include <iostream>
 #include <boost/assert.hpp>
+#include <boost/lexical_cast.hpp>
 #include <glib.h>
 
 using namespace std;
@@ -32,15 +33,17 @@ uint16_t accelerationX = 0;
 Transmission::TransmissionPositions transmissionShiftPostion = Transmission::Neutral;
 uint16_t steeringWheelAngle=0;
 uint16_t throttlePos = 0;
-uint16_t engineCoolant = 40;
+int engineCoolant = 40;
 bool machineGun = false;
 
+const char* id = "6dd4268a-c605-4a06-9034-59c1e8344c8e";
+
 static gboolean timeoutCallback(gpointer data)
 {
        ExampleSourcePlugin* src = (ExampleSourcePlugin*)data;
-       
+
        src->randomizeProperties();
-       
+
        return true;
 }
 
@@ -48,12 +51,21 @@ ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re, map<string,
 :AbstractSource(re, config), velocity(0), engineSpeed(0)
 {
        debugOut("setting timeout");
-       g_timeout_add(1000, timeoutCallback, this );
+
+       int delay = 1000;
+
+       if(config.find("delay") != config.end())
+       {
+               delay = boost::lexical_cast<int>(config["delay"]);
+       }
+
+       g_timeout_add(delay, timeoutCallback, this );
 
        addPropertySupport(VehicleProperty::EngineSpeed, Zone::None);
        addPropertySupport(VehicleProperty::VehicleSpeed, Zone::None);
        addPropertySupport(VehicleProperty::AccelerationX, Zone::None);
        addPropertySupport(VehicleProperty::TransmissionShiftPosition, Zone::None);
+       addPropertySupport(VehicleProperty::TransmissionGearPosition, Zone::None);
        addPropertySupport(VehicleProperty::SteeringWheelAngle, Zone::None);
        addPropertySupport(VehicleProperty::ThrottlePosition, Zone::None);
        addPropertySupport(VehicleProperty::EngineCoolantTemperature, Zone::None);
@@ -63,7 +75,7 @@ ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re, map<string,
        addPropertySupport(VehicleProperty::MachineGunTurretStatus, Zone::None);
        addPropertySupport(VehicleProperty::ExteriorBrightness, Zone::None);
        addPropertySupport(VehicleProperty::DoorsPerRow, Zone::None);
-       addPropertySupport(VehicleProperty::AirbagStatus, Zone::FrontLeft);
+       addPropertySupport(VehicleProperty::AirbagStatus, Zone::None);
 
        Zone::ZoneList airbagZones;
        airbagZones.push_back(Zone::FrontLeft | Zone::FrontSide);
@@ -80,20 +92,29 @@ ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re, map<string,
 
        propertyInfoMap[VehicleProperty::AirbagStatus] = airbagInfo;
 
-       re->setSupported(supported(), this);
+       addPropertySupport(VehicleProperty::AirConditioning, Zone::None);
+
+       Zone::ZoneList acZones;
+       acZones.push_back(Zone::FrontLeft);
+       acZones.push_back(Zone::Front | Zone::Right);
+
+       acStatus[Zone::Front | Zone::Left] = true;
+       acStatus[Zone::Front | Zone::Right] = false;
+
+       PropertyInfo acInfo(0,acZones);
+       propertyInfoMap[VehicleProperty::AirConditioning] = acInfo;
 }
 
 
 
-extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
+extern "C" void create(AbstractRoutingEngine* routingengine, map<string, string> config)
 {
-       return new ExampleSourcePlugin(routingengine, config);
-       
+       new ExampleSourcePlugin(routingengine, config);
 }
 
 const string ExampleSourcePlugin::uuid()
 {
-       return "6dd4268a-c605-4a06-9034-59c1e8344c8e";
+       return id;
 }
 
 
@@ -101,8 +122,6 @@ void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
 {
        DebugOut()<<"ExampleSource: getPropertyAsync called for property: "<<reply->property<<endl;
 
-
-
        if(reply->property == VehicleProperty::VehicleSpeed)
        {
                VehicleProperty::VehicleSpeedType temp(velocity);
@@ -131,6 +150,13 @@ void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
                reply->success = true;
                reply->completed(reply);
        }
+       else if(reply->property == VehicleProperty::TransmissionGearPosition)
+       {
+               VehicleProperty::TransmissionGearPositionType temp(transmissionShiftPostion);
+               reply->value = &temp;
+               reply->success = true;
+               reply->completed(reply);
+       }
        else if(reply->property == VehicleProperty::SteeringWheelAngle)
        {
                VehicleProperty::SteeringWheelAngleType temp(steeringWheelAngle);
@@ -170,13 +196,9 @@ void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
        {
                VehicleProperty::DoorsPerRowType temp;
 
-               BasicPropertyType<uint16_t> row1(2);
-               BasicPropertyType<uint16_t> row2(2);
-               BasicPropertyType<uint16_t> row3(1);
-
-               temp.append(&row1);
-               temp.append(&row2);
-               temp.append(&row3);
+               temp.append(2);
+               temp.append(2);
+               temp.append(1);
 
                reply->value = &temp;
                reply->success = true;
@@ -240,6 +262,22 @@ void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
                        reply->completed(reply);
                }
        }
+       else if(reply->property == VehicleProperty::AirConditioning)
+       {
+               if(acStatus.find(reply->zoneFilter) == acStatus.end())
+               {
+                       reply->success = false;
+                       reply->error = AsyncPropertyReply::ZoneNotSupported;
+                       reply->completed(reply);
+               }
+               else
+               {
+                       VehicleProperty::AirConditioningType temp(acStatus[reply->zoneFilter]);
+                       reply->success = true;
+                       reply->value = &temp;
+                       reply->completed(reply);
+               }
+       }
 
        else
        {
@@ -256,7 +294,36 @@ void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
 
 AsyncPropertyReply *ExampleSourcePlugin::setProperty(AsyncSetPropertyRequest request )
 {
+       AsyncPropertyReply *reply = new AsyncPropertyReply(request);
+       reply->success = false;
+
+       if(reply->property == VehicleProperty::AirConditioning)
+       {
+               if(acStatus.find(reply->zoneFilter) == acStatus.end())
+               {
+                       reply->success = false;
+                       reply->error = AsyncPropertyReply::ZoneNotSupported;
+                       reply->completed(reply);
+               }
+               else
+               {
+                       acStatus[reply->zoneFilter] = reply->value->value<bool>();
+
+                       ///we need to update subscribers of this change:
+                       routingEngine->updateProperty(reply->value,uuid());
+
+                       ///Now reply to the set request:
+                       reply->success = true;
+                       reply->completed(reply);
 
+               }
+
+               return reply;
+       }
+
+       reply->error = AsyncPropertyReply::InvalidOperation;
+       reply->completed(reply);
+       return reply;
 }
 
 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
@@ -277,7 +344,7 @@ int ExampleSourcePlugin::supportedOperations()
 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
 {
        if(contains(mRequests,property))
-               mRequests.remove(property);
+               removeOne(&mRequests, property);
 }
 
 void ExampleSourcePlugin::randomizeProperties()
@@ -289,18 +356,24 @@ void ExampleSourcePlugin::randomizeProperties()
        steeringWheelAngle = 1 + (359.00 * (rand() / (RAND_MAX + 1.0)));
        throttlePos = 1 + (100.00 * (rand() / (RAND_MAX + 1.0)));
        engineCoolant = 1 + (40.00 * (rand() / (RAND_MAX + 140.0)));
-       
+
        DebugOut()<<"setting velocity to: "<<velocity<<endl;
        DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
-       
-       VehicleProperty::VehicleSpeedType vel(velocity);
-       VehicleProperty::EngineSpeedType es(engineSpeed);
-       VehicleProperty::AccelerationXType ac(accelerationX);
-       VehicleProperty::SteeringWheelAngleType swa(steeringWheelAngle);
-       VehicleProperty::TransmissionShiftPositionType tsp(transmissionShiftPostion);
-       VehicleProperty::ThrottlePositionType tp(throttlePos);
-       VehicleProperty::EngineCoolantTemperatureType ec(engineCoolant);
-       VehicleProperty::MachineGunTurretStatusType mgt(machineGun);
+
+       vel.setValue(velocity);
+       vel.sequence++;
+       vel.priority = AbstractPropertyType::High;
+       es.setValue(engineSpeed);
+       es.sequence++;
+       es.priority = AbstractPropertyType::Low;
+       ac.setValue(accelerationX);
+       swa.setValue(steeringWheelAngle);
+       tsp.setValue(transmissionShiftPostion);
+       tgp.setValue(transmissionShiftPostion);
+       tsp.priority = AbstractPropertyType::Low;
+       tp.setValue(throttlePos);
+       ec.setValue(engineCoolant);
+       mgt.setValue(machineGun);
 
        machineGun = !machineGun;
 
@@ -311,6 +384,7 @@ void ExampleSourcePlugin::randomizeProperties()
        routingEngine->updateProperty(&tsp, uuid());
        routingEngine->updateProperty(&tp, uuid());
        routingEngine->updateProperty(&ec, uuid());
+       routingEngine->updateProperty(&tgp, uuid());
 
 }
 
@@ -318,7 +392,7 @@ void ExampleSourcePlugin::addPropertySupport(VehicleProperty::Property property,
 {
        mSupported.push_back(property);
 
-       std::list<Zone::Type> zones;
+       Zone::ZoneList zones;
 
        zones.push_back(zone);
 
@@ -326,3 +400,23 @@ void ExampleSourcePlugin::addPropertySupport(VehicleProperty::Property property,
 
        propertyInfoMap[property] = info;
 }
+
+int main(int argc, char** argv)
+{
+       ExampleSourcePlugin plugin;
+
+       AsyncPropertyReply request;
+
+       request.property = VehicleProperty::VehicleSpeed;
+       request.completed = [](AsyncPropertyReply* reply)
+       {
+               g_assert(reply->success);
+       };
+
+       plugin.getPropertyAsync(&request);
+
+       GMainLoop* mainLoop = g_main_loop_new(NULL, false);
+
+       g_main_loop_run(mainLoop);
+       return 1;
+}