database plugin works
[profile/ivi/automotive-message-broker.git] / plugins / obd2plugin / obd2source.h
1
2 /*
3 Copyright (C) 2012 Intel Corporation
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20
21 #ifndef OBD2SOURCE_H
22 #define OBD2SOURCE_H
23
24
25
26 #include <abstractsource.h>
27 #include <string>
28 #include <sstream>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <termios.h>
33 #include "obdlib.h"
34 #include <glib.h>
35
36 #include "obdpid.h"
37
38 class ObdRequest
39 {
40 public:
41   VehicleProperty::Property property;
42   std::string req;
43   std::string arg;
44 };
45
46
47 class CommandRequest
48 {
49 public:
50   std::string req;
51   std::vector<std::string> arglist;
52 };
53
54 class ObdReply
55 {
56 public:
57   VehicleProperty::Property property;
58   std::string req;
59   std::string reply;
60 };
61
62
63
64 class Obd2Amb
65 {
66 public:
67
68         typedef function<std::string (std::string)> ConversionFunction;
69
70         typedef std::vector<unsigned char> ByteArray;
71         Obd2Amb()
72         {
73                 supportedPidsList.push_back(new VehicleSpeedPid());
74                 supportedPidsList.push_back(new EngineSpeedPid());
75                 supportedPidsList.push_back(new MassAirFlowPid());
76                 supportedPidsList.push_back(new VinPid());
77                 supportedPidsList.push_back(new WmiPid());
78                 supportedPidsList.push_back(new FuelConsumptionPid());
79                 supportedPidsList.push_back(new EngineCoolantPid());
80                 supportedPidsList.push_back(new AirIntakeTemperaturePid());
81         }
82
83         ~Obd2Amb()
84         {
85                 for(auto itr = supportedPidsList.begin(); itr != supportedPidsList.end(); itr++)
86                 {
87                         delete *itr;
88                 }
89         }
90
91         ObdPid* createPidFromReply(ByteArray replyVector)
92         {
93                 for(auto itr = supportedPidsList.begin(); itr != supportedPidsList.end(); itr++)
94                 {
95                         if (!(*itr)->tryParse(replyVector))
96                         {
97                                 continue;
98                         }
99                         
100                         ObdPid* pid = (*itr)->create();
101                         pid->tryParse(replyVector);
102                         return pid;
103                 }
104                 return 0;
105         }
106         ObdPid* createPidforProperty(VehicleProperty::Property property)
107         {
108                 for(auto itr = supportedPidsList.begin(); itr != supportedPidsList.end(); itr++)
109                 {
110                         VehicleProperty::Property p = (*itr)->property;
111                         if(p == property)
112                         {
113                                 ObdPid* obj = *itr;
114                                 return obj->create();
115                         }
116                 }
117                 return NULL;
118         }
119
120         std::list<ObdPid*> supportedPidsList;
121 };
122
123 class OBD2Source : public AbstractSource
124 {
125
126 public:
127         OBD2Source(AbstractRoutingEngine* re, map<string, string> config);
128         ~OBD2Source();
129         string uuid();
130         int portHandle;
131         void getPropertyAsync(AsyncPropertyReply *reply);
132         void getRangePropertyAsync(AsyncRangePropertyReply *reply){}
133         AsyncPropertyReply * setProperty(AsyncSetPropertyRequest request);
134         void subscribeToPropertyChanges(VehicleProperty::Property property);
135         void unsubscribeToPropertyChanges(VehicleProperty::Property property);
136         PropertyList supported();
137
138         int supportedOperations();
139
140         PropertyList queuedRequests;
141         bool clientConnected;
142         PropertyList activeRequests;
143         void engineSpeed(double speed);
144         void vehicleSpeed(int speed);
145         void mafValue(double maf);
146         void engineCoolantTemp(int temp);
147         PropertyList removeRequests;
148         void setSupported(PropertyList list);
149         void propertyChanged(VehicleProperty::Property property, AbstractPropertyType* value, string uuid) {}
150         void supportedChanged(PropertyList) {}
151         GAsyncQueue* commandQueue;
152         GAsyncQueue* subscriptionAddQueue;
153         GAsyncQueue* subscriptionRemoveQueue;
154         GAsyncQueue* singleShotQueue;
155         GAsyncQueue* responseQueue;
156         std::list<std::string> m_blacklistPidList;
157         std::map<std::string,int> m_blacklistPidCountMap;
158         void setConfiguration(map<string, string> config);
159         //void randomizeProperties();
160         std::string m_port;
161         std::string m_baud;
162         bool m_isBluetooth;
163         std::string m_btDeviceAddress;
164         std::string m_btAdapterAddress;
165         map<VehicleProperty::Property,AsyncPropertyReply*> propertyReplyMap;
166         void updateProperty(VehicleProperty::Property property,AbstractPropertyType *value);
167         obdLib * obd;
168         bool m_threadLive;
169         GThread *m_gThread;
170
171 private:
172         PropertyList m_supportedProperties;
173         GMutex *threadQueueMutex;
174         VehicleProperty::Property Obd2Connect;
175         typedef BasicPropertyType<bool> Obd2ConnectType;
176
177 };
178
179 #endif // OBD2SOURCE_H