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;
108 extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
110 return new ExampleSourcePlugin(routingengine, config);
114 const string ExampleSourcePlugin::uuid()
116 return "6dd4268a-c605-4a06-9034-59c1e8344c8e";
120 void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
122 DebugOut()<<"ExampleSource: getPropertyAsync called for property: "<<reply->property<<endl;
126 if(reply->property == VehicleProperty::VehicleSpeed)
128 VehicleProperty::VehicleSpeedType temp(velocity);
129 reply->value = &temp;
130 reply->success = true;
131 reply->completed(reply);
133 else if(reply->property == VehicleProperty::EngineSpeed)
135 VehicleProperty::EngineSpeedType temp(engineSpeed);
136 reply->value = &temp;
137 reply->success = true;
138 reply->completed(reply);
140 else if(reply->property == VehicleProperty::AccelerationX)
142 VehicleProperty::AccelerationXType temp(accelerationX);
143 reply->value = &temp;
144 reply->success = true;
145 reply->completed(reply);
147 else if(reply->property == VehicleProperty::TransmissionShiftPosition)
149 VehicleProperty::TransmissionShiftPositionType temp(transmissionShiftPostion);
150 reply->value = &temp;
151 reply->success = true;
152 reply->completed(reply);
154 else if(reply->property == VehicleProperty::SteeringWheelAngle)
156 VehicleProperty::SteeringWheelAngleType temp(steeringWheelAngle);
157 reply->value = &temp;
158 reply->success = true;
159 reply->completed(reply);
161 else if(reply->property == VehicleProperty::VIN)
163 VehicleProperty::VINType temp("ABC00000000000000");
164 reply->value = &temp;
165 reply->success = true;
166 reply->completed(reply);
168 else if(reply->property == VehicleProperty::WMI)
170 VehicleProperty::WMIType temp("abc");
171 reply->value = &temp;
172 reply->success = true;
173 reply->completed(reply);
175 else if(reply->property == VehicleProperty::BatteryVoltage)
177 VehicleProperty::BatteryVoltageType temp(12.6);
178 reply->value = &temp;
179 reply->success = true;
180 reply->completed(reply);
182 else if(reply->property == VehicleProperty::ExteriorBrightness)
184 VehicleProperty::ExteriorBrightnessType temp(1000);
185 reply->value = &temp;
186 reply->success = true;
187 reply->completed(reply);
189 else if(reply->property == VehicleProperty::DoorsPerRow)
191 VehicleProperty::DoorsPerRowType temp;
193 BasicPropertyType<uint16_t> row1(2);
194 BasicPropertyType<uint16_t> row2(2);
195 BasicPropertyType<uint16_t> row3(1);
201 reply->value = &temp;
202 reply->success = true;
203 reply->completed(reply);
205 else if(reply->property == VehicleProperty::AirbagStatus)
207 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
209 reply->success = false;
210 reply->error = AsyncPropertyReply::ZoneNotSupported;
211 reply->completed(reply);
217 VehicleProperty::AirbagStatusType temp;
218 temp.setValue(airbagStatus[reply->zoneFilter]);
219 temp.zone = reply->zoneFilter;
221 reply->value = &temp;
222 reply->success = true;
223 reply->completed(reply);
226 else if(reply->property == VehicleProperty::MachineGunTurretStatus)
228 VehicleProperty::MachineGunTurretStatusType temp(true);
229 reply->value = &temp;
230 reply->success = true;
231 reply->completed(reply);
233 else if(reply->property == VehicleProperty::ThrottlePosition)
235 VehicleProperty::ThrottlePositionType temp(throttlePos);
236 reply->value = &temp;
237 reply->success = true;
238 reply->completed(reply);
240 else if(reply->property == VehicleProperty::EngineCoolantTemperature)
242 VehicleProperty::EngineCoolantTemperatureType temp(engineCoolant);
243 reply->value = &temp;
244 reply->success = true;
245 reply->completed(reply);
247 else if(reply->property == VehicleProperty::AirbagStatus)
249 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
251 reply->success = false;
252 reply->error = AsyncPropertyReply::ZoneNotSupported;
253 reply->completed(reply);
257 VehicleProperty::AirbagStatusType temp(airbagStatus[reply->zoneFilter]);
258 reply->success = true;
259 reply->value = &temp;
260 reply->completed(reply);
263 else if(reply->property == VehicleProperty::AirConditioning)
265 if(acStatus.find(reply->zoneFilter) == acStatus.end())
267 reply->success = false;
268 reply->error = AsyncPropertyReply::ZoneNotSupported;
269 reply->completed(reply);
273 VehicleProperty::AirConditioningType temp(acStatus[reply->zoneFilter]);
274 reply->success = true;
275 reply->value = &temp;
276 reply->completed(reply);
282 reply->success=false;
283 reply->error = AsyncPropertyReply::InvalidOperation;
284 reply->completed(reply);
288 void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
293 AsyncPropertyReply *ExampleSourcePlugin::setProperty(AsyncSetPropertyRequest request )
295 AsyncPropertyReply *reply = new AsyncPropertyReply(request);
296 reply->success = false;
298 if(reply->property == VehicleProperty::AirConditioning)
300 if(acStatus.find(reply->zoneFilter) == acStatus.end())
302 reply->success = false;
303 reply->error = AsyncPropertyReply::ZoneNotSupported;
304 reply->completed(reply);
308 acStatus[reply->zoneFilter] = reply->value->value<bool>();
310 ///we need to update subscribers of this change:
311 routingEngine->updateProperty(reply->value,uuid());
313 ///Now reply to the set request:
314 reply->success = true;
315 reply->completed(reply);
322 reply->error = AsyncPropertyReply::InvalidOperation;
323 reply->completed(reply);
327 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
329 mRequests.push_back(property);
332 PropertyList ExampleSourcePlugin::supported()
337 int ExampleSourcePlugin::supportedOperations()
339 return Get | Set | GetRanged;
342 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
344 if(contains(mRequests,property))
345 mRequests.remove(property);
348 void ExampleSourcePlugin::randomizeProperties()
350 velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
351 engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
352 accelerationX = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
353 transmissionShiftPostion = Transmission::TransmissionPositions(1 + (6.00 * (rand() / (RAND_MAX + 1.0))));
354 steeringWheelAngle = 1 + (359.00 * (rand() / (RAND_MAX + 1.0)));
355 throttlePos = 1 + (100.00 * (rand() / (RAND_MAX + 1.0)));
356 engineCoolant = 1 + (40.00 * (rand() / (RAND_MAX + 140.0)));
358 DebugOut()<<"setting velocity to: "<<velocity<<endl;
359 DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
361 VehicleProperty::VehicleSpeedType vel(velocity);
362 VehicleProperty::EngineSpeedType es(engineSpeed);
363 VehicleProperty::AccelerationXType ac(accelerationX);
364 VehicleProperty::SteeringWheelAngleType swa(steeringWheelAngle);
365 VehicleProperty::TransmissionShiftPositionType tsp(transmissionShiftPostion);
366 VehicleProperty::TransmissionGearPositionType tgp(transmissionShiftPostion);
367 VehicleProperty::ThrottlePositionType tp(throttlePos);
368 VehicleProperty::EngineCoolantTemperatureType ec(engineCoolant);
369 VehicleProperty::MachineGunTurretStatusType mgt(machineGun);
371 machineGun = !machineGun;
373 routingEngine->updateProperty(&vel, uuid());
374 routingEngine->updateProperty(&es, uuid());
375 routingEngine->updateProperty(&ac, uuid());
376 routingEngine->updateProperty(&swa, uuid());
377 routingEngine->updateProperty(&tsp, uuid());
378 routingEngine->updateProperty(&tp, uuid());
379 routingEngine->updateProperty(&ec, uuid());
380 routingEngine->updateProperty(&tgp, uuid());
384 void ExampleSourcePlugin::addPropertySupport(VehicleProperty::Property property, Zone::Type zone)
386 mSupported.push_back(property);
388 std::list<Zone::Type> zones;
390 zones.push_back(zone);
392 PropertyInfo info(0, zones);
394 propertyInfoMap[property] = info;