request initial value for dbus properties
[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->completed(reply);
80         }
81         else if(reply->property == VehicleProperty::EngineSpeed)
82         {
83                 VehicleProperty::EngineSpeedType temp(engineSpeed);
84                 reply->value = &temp;
85                 reply->completed(reply);
86         }
87         else if(reply->property == VehicleProperty::AccelerationX)
88         {
89                 VehicleProperty::AccelerationType temp(accelerationX);
90                 reply->value = &temp;
91                 reply->completed(reply);
92         }
93         else if(reply->property == VehicleProperty::TransmissionShiftPosition)
94         {
95                 VehicleProperty::TransmissionShiftPositionType temp(transmissionShiftPostion);
96                 reply->value = &temp;
97                 reply->completed(reply);
98         }
99         else if(reply->property == VehicleProperty::SteeringWheelAngle)
100         {
101                 VehicleProperty::SteeringWheelAngleType temp(steeringWheelAngle);
102                 reply->value = &temp;
103                 reply->completed(reply);
104         }
105         else if(reply->property == VehicleProperty::VIN)
106         {
107                 VehicleProperty::VINType temp("ABC00000000000000");
108                 reply->value = &temp;
109                 reply->completed(reply);
110         }
111         else if(reply->property == VehicleProperty::WMI)
112         {
113                 VehicleProperty::WMIType temp("abc");
114                 reply->value = &temp;
115                 reply->completed(reply);
116         }
117         else if(reply->property == VehicleProperty::BatteryVoltage)
118         {
119                 VehicleProperty::BatteryVoltageType temp(12.6);
120                 reply->value = &temp;
121                 reply->completed(reply);
122         }
123         else if(reply->property == VehicleProperty::ExteriorBrightness)
124         {
125                 VehicleProperty::ExteriorBrightnessType temp(1000);
126                 reply->value = &temp;
127                 reply->completed(reply);
128         }
129 }
130
131 void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply)
132 {
133
134 }
135
136 AsyncPropertyReply *ExampleSourcePlugin::setProperty(AsyncSetPropertyRequest request )
137 {
138
139 }
140
141 void ExampleSourcePlugin::subscribeToPropertyChanges(VehicleProperty::Property property)
142 {
143         mRequests.push_back(property);
144 }
145
146 PropertyList ExampleSourcePlugin::supported()
147 {
148         PropertyList props;
149         props.push_back(VehicleProperty::EngineSpeed);
150         props.push_back(VehicleProperty::VehicleSpeed);
151         props.push_back(VehicleProperty::AccelerationX);
152         props.push_back(VehicleProperty::TransmissionShiftPosition);
153         props.push_back(VehicleProperty::SteeringWheelAngle);
154         props.push_back(VehicleProperty::ThrottlePosition);
155         props.push_back(VehicleProperty::EngineCoolantTemperature);
156         props.push_back(VehicleProperty::VIN);
157         props.push_back(VehicleProperty::WMI);
158         props.push_back(VehicleProperty::BatteryVoltage);
159         props.push_back(VehicleProperty::MachineGunTurretStatus);
160         props.push_back(VehicleProperty::ExteriorBrightness);
161         
162         return props;
163 }
164
165 int ExampleSourcePlugin::supportedOperations()
166 {
167         return Get | Set | GetRanged;
168 }
169
170 void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property property)
171 {
172         mRequests.remove(property);
173 }
174
175 void ExampleSourcePlugin::randomizeProperties()
176 {
177         velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
178         engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
179         accelerationX = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
180         transmissionShiftPostion = Transmission::TransmissionPositions(1 + (6.00 * (rand() / (RAND_MAX + 1.0))));
181         steeringWheelAngle = 1 + (359.00 * (rand() / (RAND_MAX + 1.0)));
182         throttlePos = 1 + (100.00 * (rand() / (RAND_MAX + 1.0)));
183         engineCoolant = 1 + (40.00 * (rand() / (RAND_MAX + 140.0)));
184         
185         DebugOut()<<"setting velocity to: "<<velocity<<endl;
186         DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
187         
188         VehicleProperty::VehicleSpeedType vel(velocity);
189         VehicleProperty::EngineSpeedType es(engineSpeed);
190         VehicleProperty::AccelerationType ac(accelerationX);
191         VehicleProperty::SteeringWheelAngleType swa(steeringWheelAngle);
192         VehicleProperty::TransmissionShiftPositionType tsp(transmissionShiftPostion);
193         VehicleProperty::ThrottlePositionType tp(throttlePos);
194         VehicleProperty::EngineCoolantTemperatureType ec(engineCoolant);
195         VehicleProperty::MachineGunTurretStatusType mgt(machineGun);
196
197         machineGun = !machineGun;
198
199         vel.timestamp = amb::currentTime();
200         es.timestamp = amb::currentTime();
201         ac.timestamp = amb::currentTime();
202         swa.timestamp = amb::currentTime();
203         tsp.timestamp = amb::currentTime();
204         tp.timestamp = amb::currentTime();
205         ec.timestamp = amb::currentTime();
206
207         routingEngine->updateProperty(VehicleProperty::VehicleSpeed, &vel, uuid());
208         routingEngine->updateProperty(VehicleProperty::EngineSpeed, &es, uuid());
209         routingEngine->updateProperty(VehicleProperty::AccelerationX, &ac, uuid());
210         routingEngine->updateProperty(VehicleProperty::SteeringWheelAngle, &swa, uuid());
211         routingEngine->updateProperty(VehicleProperty::TransmissionShiftPosition,&tsp, uuid());
212         routingEngine->updateProperty(VehicleProperty::ThrottlePosition, &tp, uuid());
213         routingEngine->updateProperty(VehicleProperty::EngineCoolantTemperature, &ec, uuid());
214         //routingEngine->updateProperty(VehicleProperty::MachineGunTurretStatus, &mgt);
215 }