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 int engineCoolant = 40;
37 bool machineGun = false;
39 const char* id = "6dd4268a-c605-4a06-9034-59c1e8344c8e";
41 static gboolean timeoutCallback(gpointer data)
43 ExampleSourcePlugin* src = (ExampleSourcePlugin*)data;
45 src->randomizeProperties();
50 ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re, map<string, string> config)
51 :AbstractSource(re, config), velocity(0), engineSpeed(0)
53 debugOut("setting timeout");
57 if(config.find("delay") != config.end())
59 delay = boost::lexical_cast<int>(config["delay"]);
62 g_timeout_add(delay, timeoutCallback, this );
64 addPropertySupport(VehicleProperty::EngineSpeed, Zone::None);
65 addPropertySupport(VehicleProperty::VehicleSpeed, Zone::None);
66 addPropertySupport(VehicleProperty::AccelerationX, Zone::None);
67 addPropertySupport(VehicleProperty::TransmissionShiftPosition, Zone::None);
68 addPropertySupport(VehicleProperty::TransmissionGearPosition, Zone::None);
69 addPropertySupport(VehicleProperty::SteeringWheelAngle, Zone::None);
70 addPropertySupport(VehicleProperty::ThrottlePosition, Zone::None);
71 addPropertySupport(VehicleProperty::EngineCoolantTemperature, Zone::None);
72 addPropertySupport(VehicleProperty::VIN, Zone::None);
73 addPropertySupport(VehicleProperty::WMI, Zone::None);
74 addPropertySupport(VehicleProperty::BatteryVoltage, Zone::None);
75 addPropertySupport(VehicleProperty::MachineGunTurretStatus, Zone::None);
76 addPropertySupport(VehicleProperty::ExteriorBrightness, Zone::None);
77 addPropertySupport(VehicleProperty::DoorsPerRow, Zone::None);
78 addPropertySupport(VehicleProperty::AirbagStatus, Zone::None);
80 Zone::ZoneList airbagZones;
81 airbagZones.push_back(Zone::FrontLeft | Zone::FrontSide);
82 airbagZones.push_back(Zone::FrontRight | Zone::FrontSide);
83 airbagZones.push_back(Zone::RearLeft | Zone::LeftSide);
84 airbagZones.push_back(Zone::RearRight | Zone::RightSide);
86 airbagStatus[Zone::FrontLeft | Zone::FrontSide] = Airbag::Active;
87 airbagStatus[Zone::FrontRight | Zone::FrontSide] = Airbag::Inactive;
88 airbagStatus[Zone::RearLeft | Zone::LeftSide] = Airbag::Deployed;
89 airbagStatus[Zone::RearRight | Zone::RightSide] = Airbag::Deployed;
91 PropertyInfo airbagInfo(0,airbagZones);
93 propertyInfoMap[VehicleProperty::AirbagStatus] = airbagInfo;
95 addPropertySupport(VehicleProperty::AirConditioning, Zone::None);
97 Zone::ZoneList acZones;
98 acZones.push_back(Zone::FrontLeft);
99 acZones.push_back(Zone::Front | Zone::Right);
101 acStatus[Zone::Front | Zone::Left] = true;
102 acStatus[Zone::Front | Zone::Right] = false;
104 PropertyInfo acInfo(0,acZones);
105 propertyInfoMap[VehicleProperty::AirConditioning] = acInfo;
110 extern "C" void create(AbstractRoutingEngine* routingengine, map<string, string> config)
112 new ExampleSourcePlugin(routingengine, config);
115 const string ExampleSourcePlugin::uuid()
121 void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
123 DebugOut()<<"ExampleSource: getPropertyAsync called for property: "<<reply->property<<endl;
125 if(reply->property == VehicleProperty::VehicleSpeed)
127 VehicleProperty::VehicleSpeedType temp(velocity);
128 reply->value = &temp;
129 reply->success = true;
130 reply->completed(reply);
132 else if(reply->property == VehicleProperty::EngineSpeed)
134 VehicleProperty::EngineSpeedType temp(engineSpeed);
135 reply->value = &temp;
136 reply->success = true;
137 reply->completed(reply);
139 else if(reply->property == VehicleProperty::AccelerationX)
141 VehicleProperty::AccelerationXType temp(accelerationX);
142 reply->value = &temp;
143 reply->success = true;
144 reply->completed(reply);
146 else if(reply->property == VehicleProperty::TransmissionShiftPosition)
148 VehicleProperty::TransmissionShiftPositionType temp(transmissionShiftPostion);
149 reply->value = &temp;
150 reply->success = true;
151 reply->completed(reply);
153 else if(reply->property == VehicleProperty::TransmissionGearPosition)
155 VehicleProperty::TransmissionGearPositionType 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;
203 reply->value = &temp;
204 reply->success = true;
205 reply->completed(reply);
207 else if(reply->property == VehicleProperty::AirbagStatus)
209 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
211 reply->success = false;
212 reply->error = AsyncPropertyReply::ZoneNotSupported;
213 reply->completed(reply);
219 VehicleProperty::AirbagStatusType temp;
220 temp.setValue(airbagStatus[reply->zoneFilter]);
221 temp.zone = reply->zoneFilter;
223 reply->value = &temp;
224 reply->success = true;
225 reply->completed(reply);
228 else if(reply->property == VehicleProperty::MachineGunTurretStatus)
230 VehicleProperty::MachineGunTurretStatusType temp(true);
231 reply->value = &temp;
232 reply->success = true;
233 reply->completed(reply);
235 else if(reply->property == VehicleProperty::ThrottlePosition)
237 VehicleProperty::ThrottlePositionType temp(throttlePos);
238 reply->value = &temp;
239 reply->success = true;
240 reply->completed(reply);
242 else if(reply->property == VehicleProperty::EngineCoolantTemperature)
244 VehicleProperty::EngineCoolantTemperatureType temp(engineCoolant);
245 reply->value = &temp;
246 reply->success = true;
247 reply->completed(reply);
249 else if(reply->property == VehicleProperty::AirbagStatus)
251 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
253 reply->success = false;
254 reply->error = AsyncPropertyReply::ZoneNotSupported;
255 reply->completed(reply);
259 VehicleProperty::AirbagStatusType temp(airbagStatus[reply->zoneFilter]);
260 reply->success = true;
261 reply->value = &temp;
262 reply->completed(reply);
265 else if(reply->property == VehicleProperty::AirConditioning)
267 if(acStatus.find(reply->zoneFilter) == acStatus.end())
269 reply->success = false;
270 reply->error = AsyncPropertyReply::ZoneNotSupported;
271 reply->completed(reply);
275 VehicleProperty::AirConditioningType temp(acStatus[reply->zoneFilter]);
276 reply->success = true;
277 reply->value = &temp;
278 reply->completed(reply);
284 reply->success=false;
285 reply->error = AsyncPropertyReply::InvalidOperation;
286 reply->completed(reply);
290 void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
295 AsyncPropertyReply *ExampleSourcePlugin::setProperty(AsyncSetPropertyRequest request )
297 AsyncPropertyReply *reply = new AsyncPropertyReply(request);
298 reply->success = false;
300 if(reply->property == VehicleProperty::AirConditioning)
302 if(acStatus.find(reply->zoneFilter) == acStatus.end())
304 reply->success = false;
305 reply->error = AsyncPropertyReply::ZoneNotSupported;
306 reply->completed(reply);
310 acStatus[reply->zoneFilter] = reply->value->value<bool>();
312 ///we need to update subscribers of this change:
313 routingEngine->updateProperty(reply->value,uuid());
315 ///Now reply to the set request:
316 reply->success = true;
317 reply->completed(reply);
324 reply->error = AsyncPropertyReply::InvalidOperation;
325 reply->completed(reply);
329 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
331 mRequests.push_back(property);
334 PropertyList ExampleSourcePlugin::supported()
339 int ExampleSourcePlugin::supportedOperations()
341 return Get | Set | GetRanged;
344 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
346 if(contains(mRequests,property))
347 removeOne(&mRequests, property);
350 void ExampleSourcePlugin::randomizeProperties()
352 velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
353 engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
354 accelerationX = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
355 transmissionShiftPostion = Transmission::TransmissionPositions(1 + (6.00 * (rand() / (RAND_MAX + 1.0))));
356 steeringWheelAngle = 1 + (359.00 * (rand() / (RAND_MAX + 1.0)));
357 throttlePos = 1 + (100.00 * (rand() / (RAND_MAX + 1.0)));
358 engineCoolant = 1 + (40.00 * (rand() / (RAND_MAX + 140.0)));
360 DebugOut()<<"setting velocity to: "<<velocity<<endl;
361 DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
363 vel.setValue(velocity);
365 vel.priority = AbstractPropertyType::High;
366 es.setValue(engineSpeed);
368 es.priority = AbstractPropertyType::Low;
369 ac.setValue(accelerationX);
370 swa.setValue(steeringWheelAngle);
371 tsp.setValue(transmissionShiftPostion);
372 tgp.setValue(transmissionShiftPostion);
373 tsp.priority = AbstractPropertyType::Low;
374 tp.setValue(throttlePos);
375 ec.setValue(engineCoolant);
376 mgt.setValue(machineGun);
378 machineGun = !machineGun;
380 routingEngine->updateProperty(&vel, uuid());
381 routingEngine->updateProperty(&es, uuid());
382 routingEngine->updateProperty(&ac, uuid());
383 routingEngine->updateProperty(&swa, uuid());
384 routingEngine->updateProperty(&tsp, uuid());
385 routingEngine->updateProperty(&tp, uuid());
386 routingEngine->updateProperty(&ec, uuid());
387 routingEngine->updateProperty(&tgp, uuid());
391 void ExampleSourcePlugin::addPropertySupport(VehicleProperty::Property property, Zone::Type zone)
393 mSupported.push_back(property);
395 Zone::ZoneList zones;
397 zones.push_back(zone);
399 PropertyInfo info(0, zones);
401 propertyInfoMap[property] = info;
404 int main(int argc, char** argv)
406 ExampleSourcePlugin plugin;
408 AsyncPropertyReply request;
410 request.property = VehicleProperty::VehicleSpeed;
411 request.completed = [](AsyncPropertyReply* reply)
413 g_assert(reply->success);
416 plugin.getPropertyAsync(&request);
418 GMainLoop* mainLoop = g_main_loop_new(NULL, false);
420 g_main_loop_run(mainLoop);