[websocket] fixed getRanged requests
[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                 BasicPropertyType<uint16_t> row1(2);
200                 BasicPropertyType<uint16_t> row2(2);
201                 BasicPropertyType<uint16_t> row3(1);
202
203                 temp.append(row1);
204                 temp.append(row2);
205                 temp.append(row3);
206
207                 reply->value = &temp;
208                 reply->success = true;
209                 reply->completed(reply);
210         }
211         else if(reply->property == VehicleProperty::AirbagStatus)
212         {
213                 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
214                 {
215                         reply->success = false;
216                         reply->error = AsyncPropertyReply::ZoneNotSupported;
217                         reply->completed(reply);
218                 }
219
220                 else
221                 {
222
223                         VehicleProperty::AirbagStatusType temp;
224                         temp.setValue(airbagStatus[reply->zoneFilter]);
225                         temp.zone = reply->zoneFilter;
226
227                         reply->value = &temp;
228                         reply->success = true;
229                         reply->completed(reply);
230                 }
231         }
232         else if(reply->property == VehicleProperty::MachineGunTurretStatus)
233         {
234                 VehicleProperty::MachineGunTurretStatusType temp(true);
235                 reply->value = &temp;
236                 reply->success = true;
237                 reply->completed(reply);
238         }
239         else if(reply->property == VehicleProperty::ThrottlePosition)
240         {
241                 VehicleProperty::ThrottlePositionType temp(throttlePos);
242                 reply->value = &temp;
243                 reply->success = true;
244                 reply->completed(reply);
245         }
246         else if(reply->property == VehicleProperty::EngineCoolantTemperature)
247         {
248                 VehicleProperty::EngineCoolantTemperatureType temp(engineCoolant);
249                 reply->value = &temp;
250                 reply->success = true;
251                 reply->completed(reply);
252         }
253         else if(reply->property == VehicleProperty::AirbagStatus)
254         {
255                 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
256                 {
257                         reply->success = false;
258                         reply->error = AsyncPropertyReply::ZoneNotSupported;
259                         reply->completed(reply);
260                 }
261                 else
262                 {
263                         VehicleProperty::AirbagStatusType temp(airbagStatus[reply->zoneFilter]);
264                         reply->success = true;
265                         reply->value = &temp;
266                         reply->completed(reply);
267                 }
268         }
269         else if(reply->property == VehicleProperty::AirConditioning)
270         {
271                 if(acStatus.find(reply->zoneFilter) == acStatus.end())
272                 {
273                         reply->success = false;
274                         reply->error = AsyncPropertyReply::ZoneNotSupported;
275                         reply->completed(reply);
276                 }
277                 else
278                 {
279                         VehicleProperty::AirConditioningType temp(acStatus[reply->zoneFilter]);
280                         reply->success = true;
281                         reply->value = &temp;
282                         reply->completed(reply);
283                 }
284         }
285
286         else
287         {
288                 reply->success=false;
289                 reply->error = AsyncPropertyReply::InvalidOperation;
290                 reply->completed(reply);
291         }
292 }
293
294 void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
295 {
296
297 }
298
299 AsyncPropertyReply *ExampleSourcePlugin::setProperty(AsyncSetPropertyRequest request )
300 {
301         AsyncPropertyReply *reply = new AsyncPropertyReply(request);
302         reply->success = false;
303
304         if(reply->property == VehicleProperty::AirConditioning)
305         {
306                 if(acStatus.find(reply->zoneFilter) == acStatus.end())
307                 {
308                         reply->success = false;
309                         reply->error = AsyncPropertyReply::ZoneNotSupported;
310                         reply->completed(reply);
311                 }
312                 else
313                 {
314                         acStatus[reply->zoneFilter] = reply->value->value<bool>();
315
316                         ///we need to update subscribers of this change:
317                         routingEngine->updateProperty(reply->value,uuid());
318
319                         ///Now reply to the set request:
320                         reply->success = true;
321                         reply->completed(reply);
322
323                 }
324
325                 return reply;
326         }
327
328         reply->error = AsyncPropertyReply::InvalidOperation;
329         reply->completed(reply);
330         return reply;
331 }
332
333 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
334 {
335         mRequests.push_back(property);
336 }
337
338 PropertyList ExampleSourcePlugin::supported()
339 {
340         return mSupported;
341 }
342
343 int ExampleSourcePlugin::supportedOperations()
344 {
345         return Get | Set | GetRanged;
346 }
347
348 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
349 {
350         if(contains(mRequests,property))
351                 removeOne(&mRequests, property);
352 }
353
354 void ExampleSourcePlugin::randomizeProperties()
355 {
356         velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
357         engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
358         accelerationX = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
359         transmissionShiftPostion = Transmission::TransmissionPositions(1 + (6.00 * (rand() / (RAND_MAX + 1.0))));
360         steeringWheelAngle = 1 + (359.00 * (rand() / (RAND_MAX + 1.0)));
361         throttlePos = 1 + (100.00 * (rand() / (RAND_MAX + 1.0)));
362         engineCoolant = 1 + (40.00 * (rand() / (RAND_MAX + 140.0)));
363
364         DebugOut()<<"setting velocity to: "<<velocity<<endl;
365         DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
366
367         vel.setValue(velocity);
368         vel.sequence++;
369         vel.priority = AbstractPropertyType::High;
370         es.setValue(engineSpeed);
371         es.sequence++;
372         es.priority = AbstractPropertyType::Low;
373         ac.setValue(accelerationX);
374         swa.setValue(steeringWheelAngle);
375         tsp.setValue(transmissionShiftPostion);
376         tgp.setValue(transmissionShiftPostion);
377         tsp.priority = AbstractPropertyType::Low;
378         tp.setValue(throttlePos);
379         ec.setValue(engineCoolant);
380         mgt.setValue(machineGun);
381
382         machineGun = !machineGun;
383
384         routingEngine->updateProperty(&vel, uuid());
385         routingEngine->updateProperty(&es, uuid());
386         routingEngine->updateProperty(&ac, uuid());
387         routingEngine->updateProperty(&swa, uuid());
388         routingEngine->updateProperty(&tsp, uuid());
389         routingEngine->updateProperty(&tp, uuid());
390         routingEngine->updateProperty(&ec, uuid());
391         routingEngine->updateProperty(&tgp, uuid());
392
393 }
394
395 void ExampleSourcePlugin::addPropertySupport(VehicleProperty::Property property, Zone::Type zone)
396 {
397         mSupported.push_back(property);
398
399         Zone::ZoneList zones;
400
401         zones.push_back(zone);
402
403         PropertyInfo info(0, zones);
404
405         propertyInfoMap[property] = info;
406 }
407
408 int main(int argc, char** argv)
409 {
410         ExampleSourcePlugin plugin;
411
412         AsyncPropertyReply request;
413
414         request.property = VehicleProperty::VehicleSpeed;
415         request.completed = [](AsyncPropertyReply* reply)
416         {
417                 g_assert(reply->success);
418         };
419
420         plugin.getPropertyAsync(&request);
421
422         GMainLoop* mainLoop = g_main_loop_new(NULL, false);
423
424         g_main_loop_run(mainLoop);
425         return 1;
426 }