2 Copyright (C) 2012 Intel Corporation
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "exampleplugin.h"
20 #include "timestamp.h"
21 #include "listplusplus.h"
24 #include <boost/assert.hpp>
25 #include <boost/lexical_cast.hpp>
32 uint16_t accelerationX = 0;
33 Transmission::TransmissionPositions transmissionShiftPostion = Transmission::Neutral;
34 uint16_t steeringWheelAngle=0;
35 uint16_t throttlePos = 0;
36 uint16_t engineCoolant = 40;
37 bool machineGun = false;
39 static gboolean timeoutCallback(gpointer data)
41 ExampleSourcePlugin* src = (ExampleSourcePlugin*)data;
43 src->randomizeProperties();
48 ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re, map<string, string> config)
49 :AbstractSource(re, config), velocity(0), engineSpeed(0)
51 debugOut("setting timeout");
55 if(config.find("delay") != config.end())
57 delay = boost::lexical_cast<int>(config["delay"]);
60 g_timeout_add(delay, timeoutCallback, this );
62 addPropertySupport(VehicleProperty::EngineSpeed, Zone::None);
63 addPropertySupport(VehicleProperty::VehicleSpeed, Zone::None);
64 addPropertySupport(VehicleProperty::AccelerationX, Zone::None);
65 addPropertySupport(VehicleProperty::TransmissionShiftPosition, Zone::None);
66 addPropertySupport(VehicleProperty::TransmissionGearPosition, Zone::None);
67 addPropertySupport(VehicleProperty::SteeringWheelAngle, Zone::None);
68 addPropertySupport(VehicleProperty::ThrottlePosition, Zone::None);
69 addPropertySupport(VehicleProperty::EngineCoolantTemperature, Zone::None);
70 addPropertySupport(VehicleProperty::VIN, Zone::None);
71 addPropertySupport(VehicleProperty::WMI, Zone::None);
72 addPropertySupport(VehicleProperty::BatteryVoltage, Zone::None);
73 addPropertySupport(VehicleProperty::MachineGunTurretStatus, Zone::None);
74 addPropertySupport(VehicleProperty::ExteriorBrightness, Zone::None);
75 addPropertySupport(VehicleProperty::DoorsPerRow, Zone::None);
76 addPropertySupport(VehicleProperty::AirbagStatus, Zone::None);
78 Zone::ZoneList airbagZones;
79 airbagZones.push_back(Zone::FrontLeft | Zone::FrontSide);
80 airbagZones.push_back(Zone::FrontRight | Zone::FrontSide);
81 airbagZones.push_back(Zone::RearLeft | Zone::LeftSide);
82 airbagZones.push_back(Zone::RearRight | Zone::RightSide);
84 airbagStatus[Zone::FrontLeft | Zone::FrontSide] = Airbag::Active;
85 airbagStatus[Zone::FrontRight | Zone::FrontSide] = Airbag::Inactive;
86 airbagStatus[Zone::RearLeft | Zone::LeftSide] = Airbag::Deployed;
87 airbagStatus[Zone::RearRight | Zone::RightSide] = Airbag::Deployed;
89 PropertyInfo airbagInfo(0,airbagZones);
91 propertyInfoMap[VehicleProperty::AirbagStatus] = airbagInfo;
93 addPropertySupport(VehicleProperty::AirConditioning, Zone::None);
95 Zone::ZoneList acZones;
96 acZones.push_back(Zone::FrontLeft);
97 acZones.push_back(Zone::Front | Zone::Right);
99 acStatus[Zone::Front | Zone::Left] = true;
100 acStatus[Zone::Front | Zone::Right] = false;
102 PropertyInfo acInfo(0,acZones);
103 propertyInfoMap[VehicleProperty::AirConditioning] = acInfo;
105 re->setSupported(supported(), this);
107 PropertyList testSupported = re->supported();
109 g_assert(contains(testSupported,VehicleProperty::MachineGunTurretStatus));
114 extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
116 return new ExampleSourcePlugin(routingengine, config);
120 const string ExampleSourcePlugin::uuid()
122 return "6dd4268a-c605-4a06-9034-59c1e8344c8e";
126 void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
128 DebugOut()<<"ExampleSource: getPropertyAsync called for property: "<<reply->property<<endl;
132 if(reply->property == VehicleProperty::VehicleSpeed)
134 VehicleProperty::VehicleSpeedType temp(velocity);
135 reply->value = &temp;
136 reply->success = true;
137 reply->completed(reply);
139 else if(reply->property == VehicleProperty::EngineSpeed)
141 VehicleProperty::EngineSpeedType temp(engineSpeed);
142 reply->value = &temp;
143 reply->success = true;
144 reply->completed(reply);
146 else if(reply->property == VehicleProperty::AccelerationX)
148 VehicleProperty::AccelerationXType temp(accelerationX);
149 reply->value = &temp;
150 reply->success = true;
151 reply->completed(reply);
153 else if(reply->property == VehicleProperty::TransmissionShiftPosition)
155 VehicleProperty::TransmissionShiftPositionType temp(transmissionShiftPostion);
156 reply->value = &temp;
157 reply->success = true;
158 reply->completed(reply);
160 else if(reply->property == VehicleProperty::SteeringWheelAngle)
162 VehicleProperty::SteeringWheelAngleType temp(steeringWheelAngle);
163 reply->value = &temp;
164 reply->success = true;
165 reply->completed(reply);
167 else if(reply->property == VehicleProperty::VIN)
169 VehicleProperty::VINType temp("ABC00000000000000");
170 reply->value = &temp;
171 reply->success = true;
172 reply->completed(reply);
174 else if(reply->property == VehicleProperty::WMI)
176 VehicleProperty::WMIType temp("abc");
177 reply->value = &temp;
178 reply->success = true;
179 reply->completed(reply);
181 else if(reply->property == VehicleProperty::BatteryVoltage)
183 VehicleProperty::BatteryVoltageType temp(12.6);
184 reply->value = &temp;
185 reply->success = true;
186 reply->completed(reply);
188 else if(reply->property == VehicleProperty::ExteriorBrightness)
190 VehicleProperty::ExteriorBrightnessType temp(1000);
191 reply->value = &temp;
192 reply->success = true;
193 reply->completed(reply);
195 else if(reply->property == VehicleProperty::DoorsPerRow)
197 VehicleProperty::DoorsPerRowType temp;
199 BasicPropertyType<uint16_t> row1(2);
200 BasicPropertyType<uint16_t> row2(2);
201 BasicPropertyType<uint16_t> row3(1);
207 reply->value = &temp;
208 reply->success = true;
209 reply->completed(reply);
211 else if(reply->property == VehicleProperty::AirbagStatus)
213 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
215 reply->success = false;
216 reply->error = AsyncPropertyReply::ZoneNotSupported;
217 reply->completed(reply);
223 VehicleProperty::AirbagStatusType temp;
224 temp.setValue(airbagStatus[reply->zoneFilter]);
225 temp.zone = reply->zoneFilter;
227 reply->value = &temp;
228 reply->success = true;
229 reply->completed(reply);
232 else if(reply->property == VehicleProperty::MachineGunTurretStatus)
234 VehicleProperty::MachineGunTurretStatusType temp(true);
235 reply->value = &temp;
236 reply->success = true;
237 reply->completed(reply);
239 else if(reply->property == VehicleProperty::ThrottlePosition)
241 VehicleProperty::ThrottlePositionType temp(throttlePos);
242 reply->value = &temp;
243 reply->success = true;
244 reply->completed(reply);
246 else if(reply->property == VehicleProperty::EngineCoolantTemperature)
248 VehicleProperty::EngineCoolantTemperatureType temp(engineCoolant);
249 reply->value = &temp;
250 reply->success = true;
251 reply->completed(reply);
253 else if(reply->property == VehicleProperty::AirbagStatus)
255 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
257 reply->success = false;
258 reply->error = AsyncPropertyReply::ZoneNotSupported;
259 reply->completed(reply);
263 VehicleProperty::AirbagStatusType temp(airbagStatus[reply->zoneFilter]);
264 reply->success = true;
265 reply->value = &temp;
266 reply->completed(reply);
269 else if(reply->property == VehicleProperty::AirConditioning)
271 if(acStatus.find(reply->zoneFilter) == acStatus.end())
273 reply->success = false;
274 reply->error = AsyncPropertyReply::ZoneNotSupported;
275 reply->completed(reply);
279 VehicleProperty::AirConditioningType temp(acStatus[reply->zoneFilter]);
280 reply->success = true;
281 reply->value = &temp;
282 reply->completed(reply);
288 reply->success=false;
289 reply->error = AsyncPropertyReply::InvalidOperation;
290 reply->completed(reply);
294 void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
299 AsyncPropertyReply *ExampleSourcePlugin::setProperty(AsyncSetPropertyRequest request )
301 AsyncPropertyReply *reply = new AsyncPropertyReply(request);
302 reply->success = false;
304 if(reply->property == VehicleProperty::AirConditioning)
306 if(acStatus.find(reply->zoneFilter) == acStatus.end())
308 reply->success = false;
309 reply->error = AsyncPropertyReply::ZoneNotSupported;
310 reply->completed(reply);
314 acStatus[reply->zoneFilter] = reply->value->value<bool>();
316 ///we need to update subscribers of this change:
317 routingEngine->updateProperty(reply->value,uuid());
319 ///Now reply to the set request:
320 reply->success = true;
321 reply->completed(reply);
328 reply->error = AsyncPropertyReply::InvalidOperation;
329 reply->completed(reply);
333 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
335 mRequests.push_back(property);
338 PropertyList ExampleSourcePlugin::supported()
343 int ExampleSourcePlugin::supportedOperations()
345 return Get | Set | GetRanged;
348 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
350 if(contains(mRequests,property))
351 mRequests.remove(property);
354 void ExampleSourcePlugin::randomizeProperties()
356 velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
357 engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
358 accelerationX = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
359 transmissionShiftPostion = Transmission::TransmissionPositions(1 + (6.00 * (rand() / (RAND_MAX + 1.0))));
360 steeringWheelAngle = 1 + (359.00 * (rand() / (RAND_MAX + 1.0)));
361 throttlePos = 1 + (100.00 * (rand() / (RAND_MAX + 1.0)));
362 engineCoolant = 1 + (40.00 * (rand() / (RAND_MAX + 140.0)));
364 DebugOut()<<"setting velocity to: "<<velocity<<endl;
365 DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
367 VehicleProperty::VehicleSpeedType vel(velocity);
368 VehicleProperty::EngineSpeedType es(engineSpeed);
369 VehicleProperty::AccelerationXType ac(accelerationX);
370 VehicleProperty::SteeringWheelAngleType swa(steeringWheelAngle);
371 VehicleProperty::TransmissionShiftPositionType tsp(transmissionShiftPostion);
372 VehicleProperty::TransmissionGearPositionType tgp(transmissionShiftPostion);
373 VehicleProperty::ThrottlePositionType tp(throttlePos);
374 VehicleProperty::EngineCoolantTemperatureType ec(engineCoolant);
375 VehicleProperty::MachineGunTurretStatusType mgt(machineGun);
377 machineGun = !machineGun;
379 routingEngine->updateProperty(&vel, uuid());
380 routingEngine->updateProperty(&es, uuid());
381 routingEngine->updateProperty(&ac, uuid());
382 routingEngine->updateProperty(&swa, uuid());
383 routingEngine->updateProperty(&tsp, uuid());
384 routingEngine->updateProperty(&tp, uuid());
385 routingEngine->updateProperty(&ec, uuid());
386 routingEngine->updateProperty(&tgp, uuid());
390 void ExampleSourcePlugin::addPropertySupport(VehicleProperty::Property property, Zone::Type zone)
392 mSupported.push_back(property);
394 std::list<Zone::Type> zones;
396 zones.push_back(zone);
398 PropertyInfo info(0, zones);
400 propertyInfoMap[property] = info;