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