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" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
112 return new ExampleSourcePlugin(routingengine, config);
116 const string ExampleSourcePlugin::uuid()
122 void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
124 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::TransmissionGearPosition)
156 VehicleProperty::TransmissionGearPositionType temp(transmissionShiftPostion);
157 reply->value = &temp;
158 reply->success = true;
159 reply->completed(reply);
161 else if(reply->property == VehicleProperty::SteeringWheelAngle)
163 VehicleProperty::SteeringWheelAngleType temp(steeringWheelAngle);
164 reply->value = &temp;
165 reply->success = true;
166 reply->completed(reply);
168 else if(reply->property == VehicleProperty::VIN)
170 VehicleProperty::VINType temp("ABC00000000000000");
171 reply->value = &temp;
172 reply->success = true;
173 reply->completed(reply);
175 else if(reply->property == VehicleProperty::WMI)
177 VehicleProperty::WMIType temp("abc");
178 reply->value = &temp;
179 reply->success = true;
180 reply->completed(reply);
182 else if(reply->property == VehicleProperty::BatteryVoltage)
184 VehicleProperty::BatteryVoltageType temp(12.6);
185 reply->value = &temp;
186 reply->success = true;
187 reply->completed(reply);
189 else if(reply->property == VehicleProperty::ExteriorBrightness)
191 VehicleProperty::ExteriorBrightnessType temp(1000);
192 reply->value = &temp;
193 reply->success = true;
194 reply->completed(reply);
196 else if(reply->property == VehicleProperty::DoorsPerRow)
198 VehicleProperty::DoorsPerRowType temp;
200 BasicPropertyType<uint16_t> row1(2);
201 BasicPropertyType<uint16_t> row2(2);
202 BasicPropertyType<uint16_t> row3(1);
208 reply->value = &temp;
209 reply->success = true;
210 reply->completed(reply);
212 else if(reply->property == VehicleProperty::AirbagStatus)
214 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
216 reply->success = false;
217 reply->error = AsyncPropertyReply::ZoneNotSupported;
218 reply->completed(reply);
224 VehicleProperty::AirbagStatusType temp;
225 temp.setValue(airbagStatus[reply->zoneFilter]);
226 temp.zone = reply->zoneFilter;
228 reply->value = &temp;
229 reply->success = true;
230 reply->completed(reply);
233 else if(reply->property == VehicleProperty::MachineGunTurretStatus)
235 VehicleProperty::MachineGunTurretStatusType temp(true);
236 reply->value = &temp;
237 reply->success = true;
238 reply->completed(reply);
240 else if(reply->property == VehicleProperty::ThrottlePosition)
242 VehicleProperty::ThrottlePositionType temp(throttlePos);
243 reply->value = &temp;
244 reply->success = true;
245 reply->completed(reply);
247 else if(reply->property == VehicleProperty::EngineCoolantTemperature)
249 VehicleProperty::EngineCoolantTemperatureType temp(engineCoolant);
250 reply->value = &temp;
251 reply->success = true;
252 reply->completed(reply);
254 else if(reply->property == VehicleProperty::AirbagStatus)
256 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
258 reply->success = false;
259 reply->error = AsyncPropertyReply::ZoneNotSupported;
260 reply->completed(reply);
264 VehicleProperty::AirbagStatusType temp(airbagStatus[reply->zoneFilter]);
265 reply->success = true;
266 reply->value = &temp;
267 reply->completed(reply);
270 else if(reply->property == VehicleProperty::AirConditioning)
272 if(acStatus.find(reply->zoneFilter) == acStatus.end())
274 reply->success = false;
275 reply->error = AsyncPropertyReply::ZoneNotSupported;
276 reply->completed(reply);
280 VehicleProperty::AirConditioningType temp(acStatus[reply->zoneFilter]);
281 reply->success = true;
282 reply->value = &temp;
283 reply->completed(reply);
289 reply->success=false;
290 reply->error = AsyncPropertyReply::InvalidOperation;
291 reply->completed(reply);
295 void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
300 AsyncPropertyReply *ExampleSourcePlugin::setProperty(AsyncSetPropertyRequest request )
302 AsyncPropertyReply *reply = new AsyncPropertyReply(request);
303 reply->success = false;
305 if(reply->property == VehicleProperty::AirConditioning)
307 if(acStatus.find(reply->zoneFilter) == acStatus.end())
309 reply->success = false;
310 reply->error = AsyncPropertyReply::ZoneNotSupported;
311 reply->completed(reply);
315 acStatus[reply->zoneFilter] = reply->value->value<bool>();
317 ///we need to update subscribers of this change:
318 routingEngine->updateProperty(reply->value,uuid());
320 ///Now reply to the set request:
321 reply->success = true;
322 reply->completed(reply);
329 reply->error = AsyncPropertyReply::InvalidOperation;
330 reply->completed(reply);
334 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
336 mRequests.push_back(property);
339 PropertyList ExampleSourcePlugin::supported()
344 int ExampleSourcePlugin::supportedOperations()
346 return Get | Set | GetRanged;
349 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
351 if(contains(mRequests,property))
352 removeOne(&mRequests, property);
355 void ExampleSourcePlugin::randomizeProperties()
357 velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
358 engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
359 accelerationX = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
360 transmissionShiftPostion = Transmission::TransmissionPositions(1 + (6.00 * (rand() / (RAND_MAX + 1.0))));
361 steeringWheelAngle = 1 + (359.00 * (rand() / (RAND_MAX + 1.0)));
362 throttlePos = 1 + (100.00 * (rand() / (RAND_MAX + 1.0)));
363 engineCoolant = 1 + (40.00 * (rand() / (RAND_MAX + 140.0)));
365 DebugOut()<<"setting velocity to: "<<velocity<<endl;
366 DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
368 vel.setValue(velocity);
369 vel.priority = AbstractPropertyType::High;
370 es.setValue(engineSpeed);
371 es.priority = AbstractPropertyType::Low;
372 ac.setValue(accelerationX);
373 swa.setValue(steeringWheelAngle);
374 tsp.setValue(transmissionShiftPostion);
375 tgp.setValue(transmissionShiftPostion);
376 tsp.priority = AbstractPropertyType::Low;
377 tp.setValue(throttlePos);
378 ec.setValue(engineCoolant);
379 mgt.setValue(machineGun);
381 machineGun = !machineGun;
383 routingEngine->updateProperty(&vel, uuid());
384 routingEngine->updateProperty(&es, uuid());
385 routingEngine->updateProperty(&ac, uuid());
386 routingEngine->updateProperty(&swa, uuid());
387 routingEngine->updateProperty(&tsp, uuid());
388 routingEngine->updateProperty(&tp, uuid());
389 routingEngine->updateProperty(&ec, uuid());
390 routingEngine->updateProperty(&tgp, uuid());
394 void ExampleSourcePlugin::addPropertySupport(VehicleProperty::Property property, Zone::Type zone)
396 mSupported.push_back(property);
398 Zone::ZoneList zones;
400 zones.push_back(zone);
402 PropertyInfo info(0, zones);
404 propertyInfoMap[property] = info;
407 int main(int argc, char** argv)
409 ExampleSourcePlugin plugin;
411 AsyncPropertyReply request;
413 request.property = VehicleProperty::VehicleSpeed;
414 request.completed = [](AsyncPropertyReply* reply)
416 g_assert(reply->success);
419 plugin.getPropertyAsync(&request);
421 GMainLoop* mainLoop = g_main_loop_new(NULL, false);
423 g_main_loop_run(mainLoop);