3b7fa8a47270fd8869bf67703b51ab5ff2abfbd1
[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 uint16_t engineCoolant = 40;
37 bool machineGun = false;
38
39 static gboolean timeoutCallback(gpointer data)
40 {
41         ExampleSourcePlugin* src = (ExampleSourcePlugin*)data;
42         
43         src->randomizeProperties();
44         
45         return true;
46 }
47
48 ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re, map<string, string> config)
49 :AbstractSource(re, config), velocity(0), engineSpeed(0)
50 {
51         debugOut("setting timeout");
52
53         int delay = 1000;
54
55         if(config.find("delay") != config.end())
56         {
57                 delay = boost::lexical_cast<int>(config["delay"]);
58         }
59
60         g_timeout_add(delay, timeoutCallback, this );
61
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);
77
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);
83
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;
88
89         PropertyInfo airbagInfo(0,airbagZones);
90
91         propertyInfoMap[VehicleProperty::AirbagStatus] = airbagInfo;
92
93         addPropertySupport(VehicleProperty::AirConditioning, Zone::None);
94
95         Zone::ZoneList acZones;
96         acZones.push_back(Zone::FrontLeft);
97         acZones.push_back(Zone::Front | Zone::Right);
98
99         acStatus[Zone::Front | Zone::Left] = true;
100         acStatus[Zone::Front | Zone::Right] = false;
101
102         PropertyInfo acInfo(0,acZones);
103         propertyInfoMap[VehicleProperty::AirConditioning] = acInfo;
104 }
105
106
107
108 extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
109 {
110         return new ExampleSourcePlugin(routingengine, config);
111         
112 }
113
114 const string ExampleSourcePlugin::uuid()
115 {
116         return "6dd4268a-c605-4a06-9034-59c1e8344c8e";
117 }
118
119
120 void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
121 {
122         DebugOut()<<"ExampleSource: getPropertyAsync called for property: "<<reply->property<<endl;
123
124
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::SteeringWheelAngle)
155         {
156                 VehicleProperty::SteeringWheelAngleType temp(steeringWheelAngle);
157                 reply->value = &temp;
158                 reply->success = true;
159                 reply->completed(reply);
160         }
161         else if(reply->property == VehicleProperty::VIN)
162         {
163                 VehicleProperty::VINType temp("ABC00000000000000");
164                 reply->value = &temp;
165                 reply->success = true;
166                 reply->completed(reply);
167         }
168         else if(reply->property == VehicleProperty::WMI)
169         {
170                 VehicleProperty::WMIType temp("abc");
171                 reply->value = &temp;
172                 reply->success = true;
173                 reply->completed(reply);
174         }
175         else if(reply->property == VehicleProperty::BatteryVoltage)
176         {
177                 VehicleProperty::BatteryVoltageType temp(12.6);
178                 reply->value = &temp;
179                 reply->success = true;
180                 reply->completed(reply);
181         }
182         else if(reply->property == VehicleProperty::ExteriorBrightness)
183         {
184                 VehicleProperty::ExteriorBrightnessType temp(1000);
185                 reply->value = &temp;
186                 reply->success = true;
187                 reply->completed(reply);
188         }
189         else if(reply->property == VehicleProperty::DoorsPerRow)
190         {
191                 VehicleProperty::DoorsPerRowType temp;
192
193                 BasicPropertyType<uint16_t> row1(2);
194                 BasicPropertyType<uint16_t> row2(2);
195                 BasicPropertyType<uint16_t> row3(1);
196
197                 temp.append(&row1);
198                 temp.append(&row2);
199                 temp.append(&row3);
200
201                 reply->value = &temp;
202                 reply->success = true;
203                 reply->completed(reply);
204         }
205         else if(reply->property == VehicleProperty::AirbagStatus)
206         {
207                 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
208                 {
209                         reply->success = false;
210                         reply->error = AsyncPropertyReply::ZoneNotSupported;
211                         reply->completed(reply);
212                 }
213
214                 else
215                 {
216
217                         VehicleProperty::AirbagStatusType temp;
218                         temp.setValue(airbagStatus[reply->zoneFilter]);
219                         temp.zone = reply->zoneFilter;
220
221                         reply->value = &temp;
222                         reply->success = true;
223                         reply->completed(reply);
224                 }
225         }
226         else if(reply->property == VehicleProperty::MachineGunTurretStatus)
227         {
228                 VehicleProperty::MachineGunTurretStatusType temp(true);
229                 reply->value = &temp;
230                 reply->success = true;
231                 reply->completed(reply);
232         }
233         else if(reply->property == VehicleProperty::ThrottlePosition)
234         {
235                 VehicleProperty::ThrottlePositionType temp(throttlePos);
236                 reply->value = &temp;
237                 reply->success = true;
238                 reply->completed(reply);
239         }
240         else if(reply->property == VehicleProperty::EngineCoolantTemperature)
241         {
242                 VehicleProperty::EngineCoolantTemperatureType temp(engineCoolant);
243                 reply->value = &temp;
244                 reply->success = true;
245                 reply->completed(reply);
246         }
247         else if(reply->property == VehicleProperty::AirbagStatus)
248         {
249                 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
250                 {
251                         reply->success = false;
252                         reply->error = AsyncPropertyReply::ZoneNotSupported;
253                         reply->completed(reply);
254                 }
255                 else
256                 {
257                         VehicleProperty::AirbagStatusType temp(airbagStatus[reply->zoneFilter]);
258                         reply->success = true;
259                         reply->value = &temp;
260                         reply->completed(reply);
261                 }
262         }
263         else if(reply->property == VehicleProperty::AirConditioning)
264         {
265                 if(acStatus.find(reply->zoneFilter) == acStatus.end())
266                 {
267                         reply->success = false;
268                         reply->error = AsyncPropertyReply::ZoneNotSupported;
269                         reply->completed(reply);
270                 }
271                 else
272                 {
273                         VehicleProperty::AirConditioningType temp(acStatus[reply->zoneFilter]);
274                         reply->success = true;
275                         reply->value = &temp;
276                         reply->completed(reply);
277                 }
278         }
279
280         else
281         {
282                 reply->success=false;
283                 reply->error = AsyncPropertyReply::InvalidOperation;
284                 reply->completed(reply);
285         }
286 }
287
288 void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
289 {
290
291 }
292
293 AsyncPropertyReply *ExampleSourcePlugin::setProperty(AsyncSetPropertyRequest request )
294 {
295         AsyncPropertyReply *reply = new AsyncPropertyReply(request);
296         reply->success = false;
297
298         if(reply->property == VehicleProperty::AirConditioning)
299         {
300                 if(acStatus.find(reply->zoneFilter) == acStatus.end())
301                 {
302                         reply->success = false;
303                         reply->error = AsyncPropertyReply::ZoneNotSupported;
304                         reply->completed(reply);
305                 }
306                 else
307                 {
308                         acStatus[reply->zoneFilter] = reply->value->value<bool>();
309
310                         ///we need to update subscribers of this change:
311                         routingEngine->updateProperty(reply->value,uuid());
312
313                         ///Now reply to the set request:
314                         reply->success = true;
315                         reply->completed(reply);
316
317                 }
318
319                 return reply;
320         }
321
322         reply->error = AsyncPropertyReply::InvalidOperation;
323         reply->completed(reply);
324         return reply;
325 }
326
327 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
328 {
329         mRequests.push_back(property);
330 }
331
332 PropertyList ExampleSourcePlugin::supported()
333 {
334         return mSupported;
335 }
336
337 int ExampleSourcePlugin::supportedOperations()
338 {
339         return Get | Set | GetRanged;
340 }
341
342 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
343 {
344         if(contains(mRequests,property))
345                 mRequests.remove(property);
346 }
347
348 void ExampleSourcePlugin::randomizeProperties()
349 {
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)));
357         
358         DebugOut()<<"setting velocity to: "<<velocity<<endl;
359         DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
360         
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);
370
371         machineGun = !machineGun;
372
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());
381
382 }
383
384 void ExampleSourcePlugin::addPropertySupport(VehicleProperty::Property property, Zone::Type zone)
385 {
386         mSupported.push_back(property);
387
388         std::list<Zone::Type> zones;
389
390         zones.push_back(zone);
391
392         PropertyInfo info(0, zones);
393
394         propertyInfoMap[property] = info;
395 }