spec: Add new tool package for debugging and testing
[profile/ivi/automotive-message-broker.git] / plugins / exampleplugin.cpp
1 /*
2 Copyright (C) 2012 Intel Corporation
3
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.
8
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.
13
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
17 */
18
19 #include "exampleplugin.h"
20 #include "timestamp.h"
21 #include "listplusplus.h"
22
23 #include <iostream>
24 #include <boost/assert.hpp>
25 #include <boost/lexical_cast.hpp>
26 #include <glib.h>
27
28 using namespace std;
29
30 #include "debugout.h"
31
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;
38
39 const char* id = "6dd4268a-c605-4a06-9034-59c1e8344c8e";
40
41 static gboolean timeoutCallback(gpointer data)
42 {
43         ExampleSourcePlugin* src = (ExampleSourcePlugin*)data;
44
45         src->randomizeProperties();
46
47         return true;
48 }
49
50 ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re, map<string, string> config)
51 :AbstractSource(re, config), velocity(0), engineSpeed(0)
52 {
53         debugOut("setting timeout");
54
55         int delay = 1000;
56
57         if(config.find("delay") != config.end())
58         {
59                 delay = boost::lexical_cast<int>(config["delay"]);
60         }
61
62         g_timeout_add(delay, timeoutCallback, this );
63
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);
79
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);
85
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;
90
91         PropertyInfo airbagInfo(0,airbagZones);
92
93         propertyInfoMap[VehicleProperty::AirbagStatus] = airbagInfo;
94
95         addPropertySupport(VehicleProperty::AirConditioning, Zone::None);
96
97         Zone::ZoneList acZones;
98         acZones.push_back(Zone::FrontLeft);
99         acZones.push_back(Zone::Front | Zone::Right);
100
101         acStatus[Zone::Front | Zone::Left] = true;
102         acStatus[Zone::Front | Zone::Right] = false;
103
104         PropertyInfo acInfo(0,acZones);
105         propertyInfoMap[VehicleProperty::AirConditioning] = acInfo;
106 }
107
108
109
110 extern "C" void create(AbstractRoutingEngine* routingengine, map<string, string> config)
111 {
112         new ExampleSourcePlugin(routingengine, config);
113 }
114
115 const string ExampleSourcePlugin::uuid()
116 {
117         return id;
118 }
119
120
121 void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
122 {
123         DebugOut()<<"ExampleSource: getPropertyAsync called for property: "<<reply->property<<endl;
124
125         if(reply->property == VehicleProperty::VehicleSpeed)
126         {
127                 VehicleProperty::VehicleSpeedType temp(velocity);
128                 reply->value = &temp;
129                 reply->success = true;
130                 reply->completed(reply);
131         }
132         else if(reply->property == VehicleProperty::EngineSpeed)
133         {
134                 VehicleProperty::EngineSpeedType temp(engineSpeed);
135                 reply->value = &temp;
136                 reply->success = true;
137                 reply->completed(reply);
138         }
139         else if(reply->property == VehicleProperty::AccelerationX)
140         {
141                 VehicleProperty::AccelerationXType temp(accelerationX);
142                 reply->value = &temp;
143                 reply->success = true;
144                 reply->completed(reply);
145         }
146         else if(reply->property == VehicleProperty::TransmissionShiftPosition)
147         {
148                 VehicleProperty::TransmissionShiftPositionType temp(transmissionShiftPostion);
149                 reply->value = &temp;
150                 reply->success = true;
151                 reply->completed(reply);
152         }
153         else if(reply->property == VehicleProperty::TransmissionGearPosition)
154         {
155                 VehicleProperty::TransmissionGearPositionType temp(transmissionShiftPostion);
156                 reply->value = &temp;
157                 reply->success = true;
158                 reply->completed(reply);
159         }
160         else if(reply->property == VehicleProperty::SteeringWheelAngle)
161         {
162                 VehicleProperty::SteeringWheelAngleType temp(steeringWheelAngle);
163                 reply->value = &temp;
164                 reply->success = true;
165                 reply->completed(reply);
166         }
167         else if(reply->property == VehicleProperty::VIN)
168         {
169                 VehicleProperty::VINType temp("ABC00000000000000");
170                 reply->value = &temp;
171                 reply->success = true;
172                 reply->completed(reply);
173         }
174         else if(reply->property == VehicleProperty::WMI)
175         {
176                 VehicleProperty::WMIType temp("abc");
177                 reply->value = &temp;
178                 reply->success = true;
179                 reply->completed(reply);
180         }
181         else if(reply->property == VehicleProperty::BatteryVoltage)
182         {
183                 VehicleProperty::BatteryVoltageType temp(12.6);
184                 reply->value = &temp;
185                 reply->success = true;
186                 reply->completed(reply);
187         }
188         else if(reply->property == VehicleProperty::ExteriorBrightness)
189         {
190                 VehicleProperty::ExteriorBrightnessType temp(1000);
191                 reply->value = &temp;
192                 reply->success = true;
193                 reply->completed(reply);
194         }
195         else if(reply->property == VehicleProperty::DoorsPerRow)
196         {
197                 VehicleProperty::DoorsPerRowType temp;
198
199                 temp.append(2);
200                 temp.append(2);
201                 temp.append(1);
202
203                 reply->value = &temp;
204                 reply->success = true;
205                 reply->completed(reply);
206         }
207         else if(reply->property == VehicleProperty::AirbagStatus)
208         {
209                 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
210                 {
211                         reply->success = false;
212                         reply->error = AsyncPropertyReply::ZoneNotSupported;
213                         reply->completed(reply);
214                 }
215
216                 else
217                 {
218
219                         VehicleProperty::AirbagStatusType temp;
220                         temp.setValue(airbagStatus[reply->zoneFilter]);
221                         temp.zone = reply->zoneFilter;
222
223                         reply->value = &temp;
224                         reply->success = true;
225                         reply->completed(reply);
226                 }
227         }
228         else if(reply->property == VehicleProperty::MachineGunTurretStatus)
229         {
230                 VehicleProperty::MachineGunTurretStatusType temp(true);
231                 reply->value = &temp;
232                 reply->success = true;
233                 reply->completed(reply);
234         }
235         else if(reply->property == VehicleProperty::ThrottlePosition)
236         {
237                 VehicleProperty::ThrottlePositionType temp(throttlePos);
238                 reply->value = &temp;
239                 reply->success = true;
240                 reply->completed(reply);
241         }
242         else if(reply->property == VehicleProperty::EngineCoolantTemperature)
243         {
244                 VehicleProperty::EngineCoolantTemperatureType temp(engineCoolant);
245                 reply->value = &temp;
246                 reply->success = true;
247                 reply->completed(reply);
248         }
249         else if(reply->property == VehicleProperty::AirbagStatus)
250         {
251                 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
252                 {
253                         reply->success = false;
254                         reply->error = AsyncPropertyReply::ZoneNotSupported;
255                         reply->completed(reply);
256                 }
257                 else
258                 {
259                         VehicleProperty::AirbagStatusType temp(airbagStatus[reply->zoneFilter]);
260                         reply->success = true;
261                         reply->value = &temp;
262                         reply->completed(reply);
263                 }
264         }
265         else if(reply->property == VehicleProperty::AirConditioning)
266         {
267                 if(acStatus.find(reply->zoneFilter) == acStatus.end())
268                 {
269                         reply->success = false;
270                         reply->error = AsyncPropertyReply::ZoneNotSupported;
271                         reply->completed(reply);
272                 }
273                 else
274                 {
275                         VehicleProperty::AirConditioningType temp(acStatus[reply->zoneFilter]);
276                         reply->success = true;
277                         reply->value = &temp;
278                         reply->completed(reply);
279                 }
280         }
281
282         else
283         {
284                 reply->success=false;
285                 reply->error = AsyncPropertyReply::InvalidOperation;
286                 reply->completed(reply);
287         }
288 }
289
290 void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
291 {
292
293 }
294
295 AsyncPropertyReply *ExampleSourcePlugin::setProperty(AsyncSetPropertyRequest request )
296 {
297         AsyncPropertyReply *reply = new AsyncPropertyReply(request);
298         reply->success = false;
299
300         if(reply->property == VehicleProperty::AirConditioning)
301         {
302                 if(acStatus.find(reply->zoneFilter) == acStatus.end())
303                 {
304                         reply->success = false;
305                         reply->error = AsyncPropertyReply::ZoneNotSupported;
306                         reply->completed(reply);
307                 }
308                 else
309                 {
310                         acStatus[reply->zoneFilter] = reply->value->value<bool>();
311
312                         ///we need to update subscribers of this change:
313                         routingEngine->updateProperty(reply->value,uuid());
314
315                         ///Now reply to the set request:
316                         reply->success = true;
317                         reply->completed(reply);
318
319                 }
320
321                 return reply;
322         }
323
324         reply->error = AsyncPropertyReply::InvalidOperation;
325         reply->completed(reply);
326         return reply;
327 }
328
329 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
330 {
331         mRequests.push_back(property);
332 }
333
334 PropertyList ExampleSourcePlugin::supported()
335 {
336         return mSupported;
337 }
338
339 int ExampleSourcePlugin::supportedOperations()
340 {
341         return Get | Set | GetRanged;
342 }
343
344 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
345 {
346         if(contains(mRequests,property))
347                 removeOne(&mRequests, property);
348 }
349
350 void ExampleSourcePlugin::randomizeProperties()
351 {
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)));
359
360         DebugOut()<<"setting velocity to: "<<velocity<<endl;
361         DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
362
363         vel.setValue(velocity);
364         vel.sequence++;
365         vel.priority = AbstractPropertyType::High;
366         es.setValue(engineSpeed);
367         es.sequence++;
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);
377
378         machineGun = !machineGun;
379
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());
388
389 }
390
391 void ExampleSourcePlugin::addPropertySupport(VehicleProperty::Property property, Zone::Type zone)
392 {
393         mSupported.push_back(property);
394
395         Zone::ZoneList zones;
396
397         zones.push_back(zone);
398
399         PropertyInfo info(0, zones);
400
401         propertyInfoMap[property] = info;
402 }
403
404 int main(int argc, char** argv)
405 {
406         ExampleSourcePlugin plugin;
407
408         AsyncPropertyReply request;
409
410         request.property = VehicleProperty::VehicleSpeed;
411         request.completed = [](AsyncPropertyReply* reply)
412         {
413                 g_assert(reply->success);
414         };
415
416         plugin.getPropertyAsync(&request);
417
418         GMainLoop* mainLoop = g_main_loop_new(NULL, false);
419
420         g_main_loop_run(mainLoop);
421         return 1;
422 }