296ebd55bdc90ca0ec827526d4e53782b6aa0cea
[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 class StatusMessage
54 {
55 public:
56   std::string statusStr;
57   VehicleProperty::Property property;
58 };
59 class ObdReply
60 {
61 public:
62   VehicleProperty::Property property;
63   std::string req;
64   std::string reply;
65 };
66
67
68
69 class Obd2Amb
70 {
71 public:
72
73         typedef function<std::string (std::string)> ConversionFunction;
74
75         typedef std::vector<unsigned char> ByteArray;
76         Obd2Amb()
77         {
78                 supportedPidsList.push_back(new VehicleSpeedPid());
79                 supportedPidsList.push_back(new EngineSpeedPid());
80                 supportedPidsList.push_back(new MassAirFlowPid());
81                 supportedPidsList.push_back(new VinPid());
82                 supportedPidsList.push_back(new WmiPid());
83                 supportedPidsList.push_back(new FuelConsumptionPid());
84                 supportedPidsList.push_back(new EngineCoolantPid());
85                 supportedPidsList.push_back(new AirIntakeTemperaturePid());
86                 supportedPidsList.push_back(new EngineLoadPid());
87                 supportedPidsList.push_back(new ThrottlePositionPid());
88                 supportedPidsList.push_back(new BatteryVoltagePid());
89         }
90
91         ~Obd2Amb()
92         {
93                 for(auto itr = supportedPidsList.begin(); itr != supportedPidsList.end(); itr++)
94                 {
95                         delete *itr;
96                 }
97         }
98
99         ObdPid* createPidFromReply(ByteArray replyVector)
100         {
101                 for(auto itr = supportedPidsList.begin(); itr != supportedPidsList.end(); itr++)
102                 {
103                         if (!(*itr)->tryParse(replyVector))
104                         {
105                                 continue;
106                         }
107                         
108                         ObdPid* pid = (*itr)->create();
109                         //pid->tryParse(replyVector);
110                         return pid;
111                 }
112                 return 0;
113         }
114         ObdPid* createPidforProperty(VehicleProperty::Property property)
115         {
116                 for(auto itr = supportedPidsList.begin(); itr != supportedPidsList.end(); itr++)
117                 {
118                         VehicleProperty::Property p = (*itr)->property;
119                         if(p == property)
120                         {
121                                 ObdPid* obj = *itr;
122                                 return obj->create();
123                         }
124                 }
125                 return NULL;
126         }
127
128         std::list<ObdPid*> supportedPidsList;
129 };
130
131 class OBD2Source : public AbstractSource
132 {
133
134 public:
135         OBD2Source(AbstractRoutingEngine* re, map<string, string> config);
136         ~OBD2Source();
137         string uuid();
138         int portHandle;
139         void getPropertyAsync(AsyncPropertyReply *reply);
140         void getRangePropertyAsync(AsyncRangePropertyReply *reply){}
141         AsyncPropertyReply * setProperty(AsyncSetPropertyRequest request);
142         void subscribeToPropertyChanges(VehicleProperty::Property property);
143         void unsubscribeToPropertyChanges(VehicleProperty::Property property);
144         PropertyList supported();
145
146         int supportedOperations();
147
148         PropertyList queuedRequests;
149         bool clientConnected;
150         PropertyList activeRequests;
151         void engineSpeed(double speed);
152         void vehicleSpeed(int speed);
153         void mafValue(double maf);
154         void engineCoolantTemp(int temp);
155         PropertyList removeRequests;
156         void setSupported(PropertyList list);
157         void propertyChanged(VehicleProperty::Property property, AbstractPropertyType* value, string uuid) {}
158         void supportedChanged(PropertyList) {}
159         GAsyncQueue* commandQueue;
160         GAsyncQueue* statusQueue;
161         GAsyncQueue* subscriptionAddQueue;
162         GAsyncQueue* subscriptionRemoveQueue;
163         GAsyncQueue* singleShotQueue;
164         GAsyncQueue* responseQueue;
165         std::list<std::string> m_blacklistPidList;
166         std::map<std::string,int> m_blacklistPidCountMap;
167         void setConfiguration(map<string, string> config);
168         //void randomizeProperties();
169         std::string m_port;
170         std::string m_baud;
171         bool m_isBluetooth;
172         std::string m_btDeviceAddress;
173         std::string m_btAdapterAddress;
174         map<VehicleProperty::Property,AsyncPropertyReply*> propertyReplyMap;
175         void updateProperty(VehicleProperty::Property property,AbstractPropertyType *value);
176         obdLib * obd;
177         bool m_threadLive;
178         GThread *m_gThread;
179
180 private:
181         PropertyList m_supportedProperties;
182         std::map<VehicleProperty::Property, AbstractPropertyType*> oldValueMap;
183         GMutex *threadQueueMutex;
184         typedef BasicPropertyType<bool> Obd2ConnectType;
185
186 };
187
188 #endif // OBD2SOURCE_H