fixed incorrect reporting of zone in dbus signal
[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" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
111 {
112         return new ExampleSourcePlugin(routingengine, config);
113
114 }
115
116 const string ExampleSourcePlugin::uuid()
117 {
118         return id;
119 }
120
121
122 void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
123 {
124         DebugOut()<<"ExampleSource: getPropertyAsync called for property: "<<reply->property<<endl;
125
126         if(reply->property == VehicleProperty::VehicleSpeed)
127         {
128                 VehicleProperty::VehicleSpeedType temp(velocity);
129                 reply->value = &temp;
130                 reply->success = true;
131                 reply->completed(reply);
132         }
133         else if(reply->property == VehicleProperty::EngineSpeed)
134         {
135                 VehicleProperty::EngineSpeedType temp(engineSpeed);
136                 reply->value = &temp;
137                 reply->success = true;
138                 reply->completed(reply);
139         }
140         else if(reply->property == VehicleProperty::AccelerationX)
141         {
142                 VehicleProperty::AccelerationXType temp(accelerationX);
143                 reply->value = &temp;
144                 reply->success = true;
145                 reply->completed(reply);
146         }
147         else if(reply->property == VehicleProperty::TransmissionShiftPosition)
148         {
149                 VehicleProperty::TransmissionShiftPositionType temp(transmissionShiftPostion);
150                 reply->value = &temp;
151                 reply->success = true;
152                 reply->completed(reply);
153         }
154         else if(reply->property == VehicleProperty::TransmissionGearPosition)
155         {
156                 VehicleProperty::TransmissionGearPositionType temp(transmissionShiftPostion);
157                 reply->value = &temp;
158                 reply->success = true;
159                 reply->completed(reply);
160         }
161         else if(reply->property == VehicleProperty::SteeringWheelAngle)
162         {
163                 VehicleProperty::SteeringWheelAngleType temp(steeringWheelAngle);
164                 reply->value = &temp;
165                 reply->success = true;
166                 reply->completed(reply);
167         }
168         else if(reply->property == VehicleProperty::VIN)
169         {
170                 VehicleProperty::VINType temp("ABC00000000000000");
171                 reply->value = &temp;
172                 reply->success = true;
173                 reply->completed(reply);
174         }
175         else if(reply->property == VehicleProperty::WMI)
176         {
177                 VehicleProperty::WMIType temp("abc");
178                 reply->value = &temp;
179                 reply->success = true;
180                 reply->completed(reply);
181         }
182         else if(reply->property == VehicleProperty::BatteryVoltage)
183         {
184                 VehicleProperty::BatteryVoltageType temp(12.6);
185                 reply->value = &temp;
186                 reply->success = true;
187                 reply->completed(reply);
188         }
189         else if(reply->property == VehicleProperty::ExteriorBrightness)
190         {
191                 VehicleProperty::ExteriorBrightnessType temp(1000);
192                 reply->value = &temp;
193                 reply->success = true;
194                 reply->completed(reply);
195         }
196         else if(reply->property == VehicleProperty::DoorsPerRow)
197         {
198                 VehicleProperty::DoorsPerRowType temp;
199
200                 BasicPropertyType<uint16_t> row1(2);
201                 BasicPropertyType<uint16_t> row2(2);
202                 BasicPropertyType<uint16_t> row3(1);
203
204                 temp.append(row1);
205                 temp.append(row2);
206                 temp.append(row3);
207
208                 reply->value = &temp;
209                 reply->success = true;
210                 reply->completed(reply);
211         }
212         else if(reply->property == VehicleProperty::AirbagStatus)
213         {
214                 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
215                 {
216                         reply->success = false;
217                         reply->error = AsyncPropertyReply::ZoneNotSupported;
218                         reply->completed(reply);
219                 }
220
221                 else
222                 {
223
224                         VehicleProperty::AirbagStatusType temp;
225                         temp.setValue(airbagStatus[reply->zoneFilter]);
226                         temp.zone = reply->zoneFilter;
227
228                         reply->value = &temp;
229                         reply->success = true;
230                         reply->completed(reply);
231                 }
232         }
233         else if(reply->property == VehicleProperty::MachineGunTurretStatus)
234         {
235                 VehicleProperty::MachineGunTurretStatusType temp(true);
236                 reply->value = &temp;
237                 reply->success = true;
238                 reply->completed(reply);
239         }
240         else if(reply->property == VehicleProperty::ThrottlePosition)
241         {
242                 VehicleProperty::ThrottlePositionType temp(throttlePos);
243                 reply->value = &temp;
244                 reply->success = true;
245                 reply->completed(reply);
246         }
247         else if(reply->property == VehicleProperty::EngineCoolantTemperature)
248         {
249                 VehicleProperty::EngineCoolantTemperatureType temp(engineCoolant);
250                 reply->value = &temp;
251                 reply->success = true;
252                 reply->completed(reply);
253         }
254         else if(reply->property == VehicleProperty::AirbagStatus)
255         {
256                 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
257                 {
258                         reply->success = false;
259                         reply->error = AsyncPropertyReply::ZoneNotSupported;
260                         reply->completed(reply);
261                 }
262                 else
263                 {
264                         VehicleProperty::AirbagStatusType temp(airbagStatus[reply->zoneFilter]);
265                         reply->success = true;
266                         reply->value = &temp;
267                         reply->completed(reply);
268                 }
269         }
270         else if(reply->property == VehicleProperty::AirConditioning)
271         {
272                 if(acStatus.find(reply->zoneFilter) == acStatus.end())
273                 {
274                         reply->success = false;
275                         reply->error = AsyncPropertyReply::ZoneNotSupported;
276                         reply->completed(reply);
277                 }
278                 else
279                 {
280                         VehicleProperty::AirConditioningType temp(acStatus[reply->zoneFilter]);
281                         reply->success = true;
282                         reply->value = &temp;
283                         reply->completed(reply);
284                 }
285         }
286
287         else
288         {
289                 reply->success=false;
290                 reply->error = AsyncPropertyReply::InvalidOperation;
291                 reply->completed(reply);
292         }
293 }
294
295 void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
296 {
297
298 }
299
300 AsyncPropertyReply *ExampleSourcePlugin::setProperty(AsyncSetPropertyRequest request )
301 {
302         AsyncPropertyReply *reply = new AsyncPropertyReply(request);
303         reply->success = false;
304
305         if(reply->property == VehicleProperty::AirConditioning)
306         {
307                 if(acStatus.find(reply->zoneFilter) == acStatus.end())
308                 {
309                         reply->success = false;
310                         reply->error = AsyncPropertyReply::ZoneNotSupported;
311                         reply->completed(reply);
312                 }
313                 else
314                 {
315                         acStatus[reply->zoneFilter] = reply->value->value<bool>();
316
317                         ///we need to update subscribers of this change:
318                         routingEngine->updateProperty(reply->value,uuid());
319
320                         ///Now reply to the set request:
321                         reply->success = true;
322                         reply->completed(reply);
323
324                 }
325
326                 return reply;
327         }
328
329         reply->error = AsyncPropertyReply::InvalidOperation;
330         reply->completed(reply);
331         return reply;
332 }
333
334 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
335 {
336         mRequests.push_back(property);
337 }
338
339 PropertyList ExampleSourcePlugin::supported()
340 {
341         return mSupported;
342 }
343
344 int ExampleSourcePlugin::supportedOperations()
345 {
346         return Get | Set | GetRanged;
347 }
348
349 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
350 {
351         if(contains(mRequests,property))
352                 removeOne(&mRequests, property);
353 }
354
355 void ExampleSourcePlugin::randomizeProperties()
356 {
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)));
364
365         DebugOut()<<"setting velocity to: "<<velocity<<endl;
366         DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
367
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);
380
381         machineGun = !machineGun;
382
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());
391
392 }
393
394 void ExampleSourcePlugin::addPropertySupport(VehicleProperty::Property property, Zone::Type zone)
395 {
396         mSupported.push_back(property);
397
398         std::list<Zone::Type> zones;
399
400         zones.push_back(zone);
401
402         PropertyInfo info(0, zones);
403
404         propertyInfoMap[property] = info;
405 }
406
407 int main(int argc, char** argv)
408 {
409         ExampleSourcePlugin plugin;
410
411         AsyncPropertyReply request;
412
413         request.property = VehicleProperty::VehicleSpeed;
414         request.completed = [](AsyncPropertyReply* reply)
415         {
416                 g_assert(reply->success);
417         };
418
419         plugin.getPropertyAsync(&request);
420
421         GMainLoop* mainLoop = g_main_loop_new(NULL, false);
422
423         g_main_loop_run(mainLoop);
424         return 1;
425 }