Merge pull request #69 from fredcadete/master
[profile/ivi/automotive-message-broker.git] / plugins / exampleplugin.cpp
index dcccec0..6ea63cd 100644 (file)
@@ -17,150 +17,406 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
 #include "exampleplugin.h"
+#include "timestamp.h"
+#include "listplusplus.h"
 
 #include <iostream>
 #include <boost/assert.hpp>
+#include <boost/lexical_cast.hpp>
+#include <glib.h>
 
 using namespace std;
 
-#include "enginespeedproperty.h"
-#include "vehiclepowermodeproperty.h"
-#include "fueleconomyproperty.h"
-#include "tripmeterproperty.h"
-#include "transmissiongearstatus.h"
-#include "cruisecontrolstatus.h"
-#include "wheelbrakeproperty.h"
-#include "lightstatusproperty.h"
-#include "interiorlightstatusproperty.h"
-#include "hornproperty.h"
-#include "chime.h"
-#include "fuellevelproperty.h"
-#include "fuelrangeproperty.h"
-#include "engineoilproperty.h"
-#include "enginecoolantproperty.h"
-#include "accelerationproperty.h"
-#include "steeringwheelangleproperty.h"
-
 #include "debugout.h"
 
-ExamplePlugin::ExamplePlugin()
+uint16_t accelerationX = 0;
+Transmission::TransmissionPositions transmissionShiftPostion = Transmission::Neutral;
+uint16_t steeringWheelAngle=0;
+uint16_t throttlePos = 0;
+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;
+}
+
+ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re, map<string, string> config)
+:AbstractSource(re, config), velocity(0), engineSpeed(0)
+{
+       debugOut("setting timeout");
+
+       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);
+       addPropertySupport(VehicleProperty::VIN, Zone::None);
+       addPropertySupport(VehicleProperty::WMI, Zone::None);
+       addPropertySupport(VehicleProperty::BatteryVoltage, Zone::None);
+       addPropertySupport(VehicleProperty::MachineGunTurretStatus, Zone::None);
+       addPropertySupport(VehicleProperty::ExteriorBrightness, Zone::None);
+       addPropertySupport(VehicleProperty::DoorsPerRow, Zone::None);
+       addPropertySupport(VehicleProperty::AirbagStatus, Zone::None);
+
+       Zone::ZoneList airbagZones;
+       airbagZones.push_back(Zone::FrontLeft | Zone::FrontSide);
+       airbagZones.push_back(Zone::FrontRight | Zone::FrontSide);
+       airbagZones.push_back(Zone::RearLeft | Zone::LeftSide);
+       airbagZones.push_back(Zone::RearRight | Zone::RightSide);
+
+       airbagStatus[Zone::FrontLeft | Zone::FrontSide] = Airbag::Active;
+       airbagStatus[Zone::FrontRight | Zone::FrontSide] = Airbag::Inactive;
+       airbagStatus[Zone::RearLeft | Zone::LeftSide] = Airbag::Deployed;
+       airbagStatus[Zone::RearRight | Zone::RightSide] = Airbag::Deployed;
+
+       PropertyInfo airbagInfo(0,airbagZones);
+
+       propertyInfoMap[VehicleProperty::AirbagStatus] = airbagInfo;
+
+       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" void create(AbstractRoutingEngine* routingengine, map<string, string> config)
+{
+       new ExampleSourcePlugin(routingengine, config);
+}
+
+const string ExampleSourcePlugin::uuid()
+{
+       return id;
+}
+
+
+void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
+{
+       DebugOut()<<"ExampleSource: getPropertyAsync called for property: "<<reply->property<<endl;
+
+       if(reply->property == VehicleProperty::VehicleSpeed)
+       {
+               VehicleProperty::VehicleSpeedType temp(velocity);
+               reply->value = &temp;
+               reply->success = true;
+               reply->completed(reply);
+       }
+       else if(reply->property == VehicleProperty::EngineSpeed)
+       {
+               VehicleProperty::EngineSpeedType temp(engineSpeed);
+               reply->value = &temp;
+               reply->success = true;
+               reply->completed(reply);
+       }
+       else if(reply->property == VehicleProperty::AccelerationX)
+       {
+               VehicleProperty::AccelerationXType temp(accelerationX);
+               reply->value = &temp;
+               reply->success = true;
+               reply->completed(reply);
+       }
+       else if(reply->property == VehicleProperty::TransmissionShiftPosition)
+       {
+               VehicleProperty::TransmissionShiftPositionType temp(transmissionShiftPostion);
+               reply->value = &temp;
+               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);
+               reply->value = &temp;
+               reply->success = true;
+               reply->completed(reply);
+       }
+       else if(reply->property == VehicleProperty::VIN)
+       {
+               VehicleProperty::VINType temp("ABC00000000000000");
+               reply->value = &temp;
+               reply->success = true;
+               reply->completed(reply);
+       }
+       else if(reply->property == VehicleProperty::WMI)
+       {
+               VehicleProperty::WMIType temp("abc");
+               reply->value = &temp;
+               reply->success = true;
+               reply->completed(reply);
+       }
+       else if(reply->property == VehicleProperty::BatteryVoltage)
+       {
+               VehicleProperty::BatteryVoltageType temp(12.6);
+               reply->value = &temp;
+               reply->success = true;
+               reply->completed(reply);
+       }
+       else if(reply->property == VehicleProperty::ExteriorBrightness)
+       {
+               VehicleProperty::ExteriorBrightnessType temp(1000);
+               reply->value = &temp;
+               reply->success = true;
+               reply->completed(reply);
+       }
+       else if(reply->property == VehicleProperty::DoorsPerRow)
+       {
+               VehicleProperty::DoorsPerRowType temp;
+
+               temp.append(2);
+               temp.append(2);
+               temp.append(1);
+
+               reply->value = &temp;
+               reply->success = true;
+               reply->completed(reply);
+       }
+       else if(reply->property == VehicleProperty::AirbagStatus)
+       {
+               if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
+               {
+                       reply->success = false;
+                       reply->error = AsyncPropertyReply::ZoneNotSupported;
+                       reply->completed(reply);
+               }
+
+               else
+               {
+
+                       VehicleProperty::AirbagStatusType temp;
+                       temp.setValue(airbagStatus[reply->zoneFilter]);
+                       temp.zone = reply->zoneFilter;
+
+                       reply->value = &temp;
+                       reply->success = true;
+                       reply->completed(reply);
+               }
+       }
+       else if(reply->property == VehicleProperty::MachineGunTurretStatus)
+       {
+               VehicleProperty::MachineGunTurretStatusType temp(true);
+               reply->value = &temp;
+               reply->success = true;
+               reply->completed(reply);
+       }
+       else if(reply->property == VehicleProperty::ThrottlePosition)
+       {
+               VehicleProperty::ThrottlePositionType temp(throttlePos);
+               reply->value = &temp;
+               reply->success = true;
+               reply->completed(reply);
+       }
+       else if(reply->property == VehicleProperty::EngineCoolantTemperature)
+       {
+               VehicleProperty::EngineCoolantTemperatureType temp(engineCoolant);
+               reply->value = &temp;
+               reply->success = true;
+               reply->completed(reply);
+       }
+       else if(reply->property == VehicleProperty::AirbagStatus)
+       {
+               if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
+               {
+                       reply->success = false;
+                       reply->error = AsyncPropertyReply::ZoneNotSupported;
+                       reply->completed(reply);
+               }
+               else
+               {
+                       VehicleProperty::AirbagStatusType temp(airbagStatus[reply->zoneFilter]);
+                       reply->success = true;
+                       reply->value = &temp;
+                       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
+       {
+               reply->success=false;
+               reply->error = AsyncPropertyReply::InvalidOperation;
+               reply->completed(reply);
+       }
+}
+
+void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
+{
+
+}
+
+AsyncPropertyReply *ExampleSourcePlugin::setProperty(AsyncSetPropertyRequest request )
 {
-       int velocity = 5;
-       
-       VehicleSpeedProperty* speed = new VehicleSpeedProperty();
-       *speed = velocity;
-       
-       EngineSpeedProperty* engineSpeed = new EngineSpeedProperty();
-       engineSpeed->setValue(2000);
-       
-       VehiclePowerModeProperty* vehiclePowerMode = new VehiclePowerModeProperty();
-       *vehiclePowerMode = VehiclePowerModeProperty::Run;
-       
-       FuelEconomyProperty* fueleconomyproperty = new FuelEconomyProperty();
-       *fueleconomyproperty = 42;
-       
-       TripMeters trips;
-       trips.push_back(1500);
-       trips.push_back(1000);
-       
-       TripMeterProperty* tripmeterproperty = new TripMeterProperty();
-       *tripmeterproperty = trips;
-       
-       TransmissionGearStatusProperty* transmissiongearstatus = new TransmissionGearStatusProperty();
-       transmissiongearstatus->setValue(TransmissionGearStatusProperty::First);
-       
-       CruiseControlStatus cruisecontrolstatus = CruiseControlStatus(true, velocity);
-
-       CruiseControlStatusProperty * cruisecontrolstatusproperty = new CruiseControlStatusProperty();
-       *cruisecontrolstatusproperty = cruisecontrolstatus;
-       
-       WheelBrakeProperty *wheelbrakeproperty = new WheelBrakeProperty();
-       *wheelbrakeproperty = false;
-       
-       LightStatusProperty::LightStatus lights;
-       lights[LightStatusProperty::Brake] = wheelbrakeproperty->value();
-       lights[LightStatusProperty::Fog] = true;
-       lights[LightStatusProperty::Head] = true;
-       lights[LightStatusProperty::HighBeam] = false;
-       lights[LightStatusProperty::Hazard] = false;
-       lights[LightStatusProperty::LeftTurn] = false;
-       lights[LightStatusProperty::RightTurn] = false;
-       lights[LightStatusProperty::Parking] = false;
-       
-       LightStatusProperty *lightstatusproperty = new LightStatusProperty();
-       *lightstatusproperty = lights;
-       
-       InteriorLightStatusProperty::InteriorLightStatus interiorLights;
-       interiorLights[InteriorLightStatusProperty::Driver] = false;
-       interiorLights[InteriorLightStatusProperty::Passenger] = true;
-       interiorLights[InteriorLightStatusProperty::Center] = false;
-       
-       InteriorLightStatusProperty* interiorlightstatusproperty = new InteriorLightStatusProperty();
-       *interiorlightstatusproperty = interiorLights;
-
-       HornProperty *hornProperty = new HornProperty();
-       *hornProperty = false;
-
-       Chime * chime = new Chime();
-       *chime = false;
-
-       bool val = *chime;
-
-       BOOST_ASSERT(val == false);
-
-       FuelLevelProperty *fuelLevel = new FuelLevelProperty();
-       *fuelLevel = (uint8_t)95;
-
-       uint8_t fl = *fuelLevel;
-
-       BOOST_ASSERT(fl == 95);
-
-       FuelRangeProperty* fuelRange = new FuelRangeProperty();
-
-       *fuelRange = 321;
-
-       uint16_t fr = *fuelRange;
-
-       BOOST_ASSERT(fr == 321);
-
-       EngineOilProperty *oilproperty = new EngineOilProperty();
-
-       EngineOil oil;
-       oil.temperature = 32;
-       oil.pressure = 400;
-       oil.remaining = 88;
-
-       *oilproperty = oil;
-
-       EngineOil ol = *oilproperty;
-
-       BOOST_ASSERT(ol == oil);
-
-       EngineCoolant coolant;
-       coolant.level= 99;
-       coolant.temperature = 44;
-
-       EngineCoolantProperty *coolantProp = new EngineCoolantProperty();
-       *coolantProp = coolant;
-
-       EngineCoolant ec = *coolantProp;
-
-       BOOST_ASSERT(ec == coolant);
-
-       AccelerationProperty* accelerationProp = new AccelerationProperty();
-       Acceleration accel;
-       accel.x = 1;
-       accel.y = 0.25;
-       accel.z = 0;
-
-       *accelerationProp = accel;
-
-       SteeringWheelAngleProperty * steeringWheelAngle = new SteeringWheelAngleProperty();
+       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)
+{
+       mRequests.push_back(property);
+}
+
+PropertyList ExampleSourcePlugin::supported()
+{
+       return mSupported;
+}
 
-       *steeringWheelAngle = 100;
+int ExampleSourcePlugin::supportedOperations()
+{
+       return Get | Set | GetRanged;
+}
+
+void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
+{
+       if(contains(mRequests,property))
+               removeOne(&mRequests, property);
 }
 
-extern "C" void create()
+void ExampleSourcePlugin::randomizeProperties()
 {
-       new ExamplePlugin;
+       velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
+       engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
+       accelerationX = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
+       transmissionShiftPostion = Transmission::TransmissionPositions(1 + (6.00 * (rand() / (RAND_MAX + 1.0))));
+       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;
+
+       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;
+
+       routingEngine->updateProperty(&vel, uuid());
+       routingEngine->updateProperty(&es, uuid());
+       routingEngine->updateProperty(&ac, uuid());
+       routingEngine->updateProperty(&swa, uuid());
+       routingEngine->updateProperty(&tsp, uuid());
+       routingEngine->updateProperty(&tp, uuid());
+       routingEngine->updateProperty(&ec, uuid());
+       routingEngine->updateProperty(&tgp, uuid());
+
+}
+
+void ExampleSourcePlugin::addPropertySupport(VehicleProperty::Property property, Zone::Type zone)
+{
+       mSupported.push_back(property);
+
+       Zone::ZoneList zones;
+
+       zones.push_back(zone);
+
+       PropertyInfo info(0, zones);
+
+       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;
 }