b9499db5401ec6ebe0cf84f55c20063e0b408848
[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         PropertyList queuedRequests;
138         bool clientConnected;
139         PropertyList activeRequests;
140         void engineSpeed(double speed);
141         void vehicleSpeed(int speed);
142         void mafValue(double maf);
143         void engineCoolantTemp(int temp);
144         PropertyList removeRequests;
145         void setSupported(PropertyList list);
146         void propertyChanged(VehicleProperty::Property property, AbstractPropertyType* value, string uuid) {}
147         void supportedChanged(PropertyList) {}
148         GAsyncQueue* commandQueue;
149         GAsyncQueue* subscriptionAddQueue;
150         GAsyncQueue* subscriptionRemoveQueue;
151         GAsyncQueue* singleShotQueue;
152         GAsyncQueue* responseQueue;
153         std::list<std::string> m_blacklistPidList;
154         std::map<std::string,int> m_blacklistPidCountMap;
155         void setConfiguration(map<string, string> config);
156         //void randomizeProperties();
157         std::string m_port;
158         std::string m_baud;
159         bool m_isBluetooth;
160         std::string m_btDeviceAddress;
161         std::string m_btAdapterAddress;
162         map<VehicleProperty::Property,AsyncPropertyReply*> propertyReplyMap;
163         void updateProperty(VehicleProperty::Property property,AbstractPropertyType *value);
164         obdLib * obd;
165         bool m_threadLive;
166         GThread *m_gThread;
167
168 private:
169         PropertyList m_supportedProperties;
170         GMutex *threadQueueMutex;
171         VehicleProperty::Property Obd2Connect;
172         typedef BasicPropertyType<bool> Obd2ConnectType;
173
174 };
175
176 #endif // OBD2SOURCE_H