Merge branch 'master' of github.com:otcshare/automotive-message-broker
[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
22 #include <iostream>
23 #include <boost/assert.hpp>
24 #include <glib.h>
25
26 using namespace std;
27
28 #include "debugout.h"
29
30 uint16_t accelerationX = 0;
31 Transmission::TransmissionPositions transmissionShiftPostion = Transmission::Neutral;
32 uint16_t steeringWheelAngle=0;
33 uint16_t throttlePos = 0;
34 uint16_t engineCoolant = 40;
35 bool machineGun = false;
36
37 static gboolean timeoutCallback(gpointer data)
38 {
39         ExampleSourcePlugin* src = (ExampleSourcePlugin*)data;
40         
41         src->randomizeProperties();
42         
43         return true;
44 }
45
46 ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re, map<string, string> config)
47 :AbstractSource(re, config), velocity(0), engineSpeed(0)
48 {
49         debugOut("setting timeout");
50         g_timeout_add(1000, timeoutCallback, this );
51
52         addPropertySupport(VehicleProperty::EngineSpeed, Zone::None);
53         addPropertySupport(VehicleProperty::VehicleSpeed, Zone::None);
54         addPropertySupport(VehicleProperty::AccelerationX, Zone::None);
55         addPropertySupport(VehicleProperty::TransmissionShiftPosition, Zone::None);
56         addPropertySupport(VehicleProperty::SteeringWheelAngle, Zone::None);
57         addPropertySupport(VehicleProperty::ThrottlePosition, Zone::None);
58         addPropertySupport(VehicleProperty::EngineCoolantTemperature, Zone::None);
59         addPropertySupport(VehicleProperty::VIN, Zone::None);
60         addPropertySupport(VehicleProperty::WMI, Zone::None);
61         addPropertySupport(VehicleProperty::BatteryVoltage, Zone::None);
62         addPropertySupport(VehicleProperty::MachineGunTurretStatus, Zone::None);
63         addPropertySupport(VehicleProperty::ExteriorBrightness, Zone::None);
64         addPropertySupport(VehicleProperty::DoorsPerRow, Zone::None);
65         addPropertySupport(VehicleProperty::AirbagStatus, Zone::FrontLeft);
66
67         Zone::ZoneList airbagZones;
68         airbagZones.push_back(Zone::FrontLeft);
69         airbagZones.push_back(Zone::FrontRight);
70         airbagZones.push_back(Zone::RearLeft);
71         airbagZones.push_back(Zone::RearRight);
72
73         airbagStatus[Zone::FrontLeft] = Airbag::Active;
74         airbagStatus[Zone::FrontRight] = Airbag::Inactive;
75         airbagStatus[Zone::RearLeft] = Airbag::Deployed;
76         airbagStatus[Zone::RearRight] = Airbag::Deployed;
77
78         PropertyInfo airbagInfo(0,airbagZones);
79
80         propertyInfoMap[VehicleProperty::AirbagStatus] = airbagInfo;
81
82         re->setSupported(supported(), this);
83 }
84
85
86
87 extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
88 {
89         return new ExampleSourcePlugin(routingengine, config);
90         
91 }
92
93 string ExampleSourcePlugin::uuid()
94 {
95         return "6dd4268a-c605-4a06-9034-59c1e8344c8e";
96 }
97
98
99 void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
100 {
101         DebugOut()<<"ExampleSource: getPropertyAsync called for property: "<<reply->property<<endl;
102
103
104
105         if(reply->property == VehicleProperty::VehicleSpeed)
106         {
107                 VehicleProperty::VehicleSpeedType temp(velocity);
108                 reply->value = &temp;
109                 reply->success = true;
110                 reply->completed(reply);
111         }
112         else if(reply->property == VehicleProperty::EngineSpeed)
113         {
114                 VehicleProperty::EngineSpeedType temp(engineSpeed);
115                 reply->value = &temp;
116                 reply->success = true;
117                 reply->completed(reply);
118         }
119         else if(reply->property == VehicleProperty::AccelerationX)
120         {
121                 VehicleProperty::AccelerationXType temp(accelerationX);
122                 reply->value = &temp;
123                 reply->success = true;
124                 reply->completed(reply);
125         }
126         else if(reply->property == VehicleProperty::TransmissionShiftPosition)
127         {
128                 VehicleProperty::TransmissionShiftPositionType temp(transmissionShiftPostion);
129                 reply->value = &temp;
130                 reply->success = true;
131                 reply->completed(reply);
132         }
133         else if(reply->property == VehicleProperty::SteeringWheelAngle)
134         {
135                 VehicleProperty::SteeringWheelAngleType temp(steeringWheelAngle);
136                 reply->value = &temp;
137                 reply->success = true;
138                 reply->completed(reply);
139         }
140         else if(reply->property == VehicleProperty::VIN)
141         {
142                 VehicleProperty::VINType temp("ABC00000000000000");
143                 reply->value = &temp;
144                 reply->success = true;
145                 reply->completed(reply);
146         }
147         else if(reply->property == VehicleProperty::WMI)
148         {
149                 VehicleProperty::WMIType temp("abc");
150                 reply->value = &temp;
151                 reply->success = true;
152                 reply->completed(reply);
153         }
154         else if(reply->property == VehicleProperty::BatteryVoltage)
155         {
156                 VehicleProperty::BatteryVoltageType temp(12.6);
157                 reply->value = &temp;
158                 reply->success = true;
159                 reply->completed(reply);
160         }
161         else if(reply->property == VehicleProperty::ExteriorBrightness)
162         {
163                 VehicleProperty::ExteriorBrightnessType temp(1000);
164                 reply->value = &temp;
165                 reply->success = true;
166                 reply->completed(reply);
167         }
168         else if(reply->property == VehicleProperty::DoorsPerRow)
169         {
170                 VehicleProperty::DoorsPerRowType temp;
171
172                 BasicPropertyType<uint16_t> row1(2);
173                 BasicPropertyType<uint16_t> row2(2);
174                 BasicPropertyType<uint16_t> row3(1);
175
176                 temp.append(&row1);
177                 temp.append(&row2);
178                 temp.append(&row3);
179
180                 reply->value = &temp;
181                 reply->success = true;
182                 reply->completed(reply);
183         }
184         else if(reply->property == VehicleProperty::AirbagStatus)
185         {
186                 if(airbagStatus.find(reply->zoneFilter) == airbagStatus.end())
187                 {
188                         reply->success = false;
189                         reply->error = AsyncPropertyReply::ZoneNotSupported;
190                         reply->completed(reply);
191                 }
192
193                 else
194                 {
195
196                         VehicleProperty::AirbagStatusType temp;
197                         temp.setValue(airbagStatus[reply->zoneFilter]);
198                         temp.zone = reply->zoneFilter;
199
200                         reply->value = &temp;
201                         reply->success = true;
202                         reply->completed(reply);
203                 }
204         }
205         else if(reply->property == VehicleProperty::MachineGunTurretStatus)
206         {
207                 VehicleProperty::MachineGunTurretStatusType temp(true);
208                 reply->value = &temp;
209                 reply->success = true;
210                 reply->completed(reply);
211         }
212         else
213         {
214                 reply->success=false;
215                 reply->error = AsyncPropertyReply::InvalidOperation;
216                 reply->completed(reply);
217         }
218 }
219
220 void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
221 {
222
223 }
224
225 AsyncPropertyReply *ExampleSourcePlugin::setProperty(AsyncSetPropertyRequest request )
226 {
227
228 }
229
230 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
231 {
232         mRequests.push_back(property);
233 }
234
235 PropertyList ExampleSourcePlugin::supported()
236 {
237         return mSupported;
238 }
239
240 int ExampleSourcePlugin::supportedOperations()
241 {
242         return Get | Set | GetRanged;
243 }
244
245 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
246 {
247         mRequests.remove(property);
248 }
249
250 void ExampleSourcePlugin::randomizeProperties()
251 {
252         velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
253         engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
254         accelerationX = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
255         transmissionShiftPostion = Transmission::TransmissionPositions(1 + (6.00 * (rand() / (RAND_MAX + 1.0))));
256         steeringWheelAngle = 1 + (359.00 * (rand() / (RAND_MAX + 1.0)));
257         throttlePos = 1 + (100.00 * (rand() / (RAND_MAX + 1.0)));
258         engineCoolant = 1 + (40.00 * (rand() / (RAND_MAX + 140.0)));
259         
260         DebugOut()<<"setting velocity to: "<<velocity<<endl;
261         DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
262         
263         VehicleProperty::VehicleSpeedType vel(velocity);
264         VehicleProperty::EngineSpeedType es(engineSpeed);
265         VehicleProperty::AccelerationXType ac(accelerationX);
266         VehicleProperty::SteeringWheelAngleType swa(steeringWheelAngle);
267         VehicleProperty::TransmissionShiftPositionType tsp(transmissionShiftPostion);
268         VehicleProperty::ThrottlePositionType tp(throttlePos);
269         VehicleProperty::EngineCoolantTemperatureType ec(engineCoolant);
270         VehicleProperty::MachineGunTurretStatusType mgt(machineGun);
271
272         machineGun = !machineGun;
273
274         vel.timestamp = amb::currentTime();
275         es.timestamp = amb::currentTime();
276         ac.timestamp = amb::currentTime();
277         swa.timestamp = amb::currentTime();
278         tsp.timestamp = amb::currentTime();
279         tp.timestamp = amb::currentTime();
280         ec.timestamp = amb::currentTime();
281
282         routingEngine->updateProperty(VehicleProperty::VehicleSpeed, &vel, uuid());
283         routingEngine->updateProperty(VehicleProperty::EngineSpeed, &es, uuid());
284         routingEngine->updateProperty(VehicleProperty::AccelerationX, &ac, uuid());
285         routingEngine->updateProperty(VehicleProperty::SteeringWheelAngle, &swa, uuid());
286         routingEngine->updateProperty(VehicleProperty::TransmissionShiftPosition,&tsp, uuid());
287         routingEngine->updateProperty(VehicleProperty::ThrottlePosition, &tp, uuid());
288         routingEngine->updateProperty(VehicleProperty::EngineCoolantTemperature, &ec, uuid());
289         //routingEngine->updateProperty(VehicleProperty::MachineGunTurretStatus, &mgt);
290 }
291
292 void ExampleSourcePlugin::addPropertySupport(VehicleProperty::Property property, Zone::Type zone)
293 {
294         mSupported.push_back(property);
295
296         std::list<Zone::Type> zones;
297
298         zones.push_back(zone);
299
300         PropertyInfo info(0, zones);
301
302         propertyInfoMap[property] = info;
303 }