mostly working zones and source dbus support
[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         re->setSupported(supported(), this);
68 }
69
70
71
72 extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
73 {
74         return new ExampleSourcePlugin(routingengine, config);
75         
76 }
77
78 string ExampleSourcePlugin::uuid()
79 {
80         return "6dd4268a-c605-4a06-9034-59c1e8344c8e";
81 }
82
83
84 void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply)
85 {
86         DebugOut()<<"ExampleSource: getPropertyAsync called for property: "<<reply->property<<endl;
87
88
89
90         if(reply->property == VehicleProperty::VehicleSpeed)
91         {
92                 VehicleProperty::VehicleSpeedType temp(velocity);
93                 reply->value = &temp;
94                 reply->success = true;
95                 reply->completed(reply);
96         }
97         else if(reply->property == VehicleProperty::EngineSpeed)
98         {
99                 VehicleProperty::EngineSpeedType temp(engineSpeed);
100                 reply->value = &temp;
101                 reply->success = true;
102                 reply->completed(reply);
103         }
104         else if(reply->property == VehicleProperty::AccelerationX)
105         {
106                 VehicleProperty::AccelerationXType temp(accelerationX);
107                 reply->value = &temp;
108                 reply->success = true;
109                 reply->completed(reply);
110         }
111         else if(reply->property == VehicleProperty::TransmissionShiftPosition)
112         {
113                 VehicleProperty::TransmissionShiftPositionType temp(transmissionShiftPostion);
114                 reply->value = &temp;
115                 reply->success = true;
116                 reply->completed(reply);
117         }
118         else if(reply->property == VehicleProperty::SteeringWheelAngle)
119         {
120                 VehicleProperty::SteeringWheelAngleType temp(steeringWheelAngle);
121                 reply->value = &temp;
122                 reply->success = true;
123                 reply->completed(reply);
124         }
125         else if(reply->property == VehicleProperty::VIN)
126         {
127                 VehicleProperty::VINType temp("ABC00000000000000");
128                 reply->value = &temp;
129                 reply->success = true;
130                 reply->completed(reply);
131         }
132         else if(reply->property == VehicleProperty::WMI)
133         {
134                 VehicleProperty::WMIType temp("abc");
135                 reply->value = &temp;
136                 reply->success = true;
137                 reply->completed(reply);
138         }
139         else if(reply->property == VehicleProperty::BatteryVoltage)
140         {
141                 VehicleProperty::BatteryVoltageType temp(12.6);
142                 reply->value = &temp;
143                 reply->success = true;
144                 reply->completed(reply);
145         }
146         else if(reply->property == VehicleProperty::ExteriorBrightness)
147         {
148                 VehicleProperty::ExteriorBrightnessType temp(1000);
149                 reply->value = &temp;
150                 reply->success = true;
151                 reply->completed(reply);
152         }
153         else if(reply->property == VehicleProperty::DoorsPerRow)
154         {
155                 VehicleProperty::DoorsPerRowType temp;
156
157                 BasicPropertyType<uint16_t> row1(2);
158                 BasicPropertyType<uint16_t> row2(2);
159                 BasicPropertyType<uint16_t> row3(1);
160
161                 temp.append(&row1);
162                 temp.append(&row2);
163                 temp.append(&row3);
164
165                 reply->value = &temp;
166                 reply->success = true;
167                 reply->completed(reply);
168         }
169         else if(reply->property == VehicleProperty::AirbagStatus)
170         {
171
172                 if(reply->zoneFilter != Zone::None && reply->zoneFilter != Zone::FrontLeft)
173                 {
174                         reply->success = false;
175                         reply->error = AsyncPropertyReply::ZoneNotSupported;
176                         reply->completed(reply);
177                 }
178
179                 VehicleProperty::AirbagStatusType temp(Airbag::Active);
180                 temp.zone = Zone::FrontLeft;
181
182                 reply->value = &temp;
183                 reply->success = true;
184                 reply->completed(reply);
185         }
186         else if(reply->property == VehicleProperty::MachineGunTurretStatus)
187         {
188                 VehicleProperty::MachineGunTurretStatusType temp(true);
189                 reply->value = &temp;
190                 reply->success = true;
191                 reply->completed(reply);
192         }
193 }
194
195 void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
196 {
197
198 }
199
200 AsyncPropertyReply *ExampleSourcePlugin::setProperty(AsyncSetPropertyRequest request )
201 {
202
203 }
204
205 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
206 {
207         mRequests.push_back(property);
208 }
209
210 PropertyList ExampleSourcePlugin::supported()
211 {
212         return mSupported;
213 }
214
215 int ExampleSourcePlugin::supportedOperations()
216 {
217         return Get | Set | GetRanged;
218 }
219
220 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
221 {
222         mRequests.remove(property);
223 }
224
225 void ExampleSourcePlugin::randomizeProperties()
226 {
227         velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
228         engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
229         accelerationX = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
230         transmissionShiftPostion = Transmission::TransmissionPositions(1 + (6.00 * (rand() / (RAND_MAX + 1.0))));
231         steeringWheelAngle = 1 + (359.00 * (rand() / (RAND_MAX + 1.0)));
232         throttlePos = 1 + (100.00 * (rand() / (RAND_MAX + 1.0)));
233         engineCoolant = 1 + (40.00 * (rand() / (RAND_MAX + 140.0)));
234         
235         DebugOut()<<"setting velocity to: "<<velocity<<endl;
236         DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
237         
238         VehicleProperty::VehicleSpeedType vel(velocity);
239         VehicleProperty::EngineSpeedType es(engineSpeed);
240         VehicleProperty::AccelerationXType ac(accelerationX);
241         VehicleProperty::SteeringWheelAngleType swa(steeringWheelAngle);
242         VehicleProperty::TransmissionShiftPositionType tsp(transmissionShiftPostion);
243         VehicleProperty::ThrottlePositionType tp(throttlePos);
244         VehicleProperty::EngineCoolantTemperatureType ec(engineCoolant);
245         VehicleProperty::MachineGunTurretStatusType mgt(machineGun);
246
247         machineGun = !machineGun;
248
249         vel.timestamp = amb::currentTime();
250         es.timestamp = amb::currentTime();
251         ac.timestamp = amb::currentTime();
252         swa.timestamp = amb::currentTime();
253         tsp.timestamp = amb::currentTime();
254         tp.timestamp = amb::currentTime();
255         ec.timestamp = amb::currentTime();
256
257         routingEngine->updateProperty(VehicleProperty::VehicleSpeed, &vel, uuid());
258         routingEngine->updateProperty(VehicleProperty::EngineSpeed, &es, uuid());
259         routingEngine->updateProperty(VehicleProperty::AccelerationX, &ac, uuid());
260         routingEngine->updateProperty(VehicleProperty::SteeringWheelAngle, &swa, uuid());
261         routingEngine->updateProperty(VehicleProperty::TransmissionShiftPosition,&tsp, uuid());
262         routingEngine->updateProperty(VehicleProperty::ThrottlePosition, &tp, uuid());
263         routingEngine->updateProperty(VehicleProperty::EngineCoolantTemperature, &ec, uuid());
264         //routingEngine->updateProperty(VehicleProperty::MachineGunTurretStatus, &mgt);
265 }
266
267 void ExampleSourcePlugin::addPropertySupport(VehicleProperty::Property property, Zone::Type zone)
268 {
269         mSupported.push_back(property);
270
271         std::list<Zone::Type> zones;
272
273         zones.push_back(zone);
274
275         PropertyInfo info(0, zones);
276
277         propertyInfoMap[property] = info;
278 }