automotive-message-broker  0.14.803
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
obd2source.h
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 
20 #ifndef OBD2SOURCE_H
21 #define OBD2SOURCE_H
22 
23 
24 
25 #include <abstractsource.h>
26 #include <string>
27 #include <sstream>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <termios.h>
32 #include "obdlib.h"
33 #include <glib.h>
34 
35 #include "obdpid.h"
36 
38 {
39 public:
40  VehicleProperty::Property property;
41  std::string req;
42  std::string arg;
43 };
44 
45 
47 {
48 public:
49  std::string req;
50  std::vector<std::string> arglist;
51 };
53 {
54 public:
55  std::string statusStr;
56  VehicleProperty::Property property;
57 };
58 class ObdReply
59 {
60 public:
61  VehicleProperty::Property property;
62  std::string req;
63  std::string reply;
64 };
65 
66 
67 
68 class Obd2Amb
69 {
70 public:
71 
72  typedef function<std::string (std::string)> ConversionFunction;
73 
74  typedef std::vector<unsigned char> ByteArray;
75  Obd2Amb()
76  {
77  supportedPidsList.push_back(new VehicleSpeedPid());
78  supportedPidsList.push_back(new EngineSpeedPid());
79  supportedPidsList.push_back(new MassAirFlowPid());
80  supportedPidsList.push_back(new VinPid());
81  supportedPidsList.push_back(new WmiPid());
82  supportedPidsList.push_back(new FuelConsumptionPid());
83  supportedPidsList.push_back(new EngineCoolantPid());
84  supportedPidsList.push_back(new AirIntakeTemperaturePid());
85  supportedPidsList.push_back(new EngineLoadPid());
86  supportedPidsList.push_back(new ThrottlePositionPid());
87  supportedPidsList.push_back(new BatteryVoltagePid());
88  }
89 
90  ~Obd2Amb()
91  {
92  for(auto itr = supportedPidsList.begin(); itr != supportedPidsList.end(); itr++)
93  {
94  delete *itr;
95  }
96  }
97 
98  ObdPid* createPidFromReply(ByteArray replyVector)
99  {
100  ByteArray replyVectorClean = ObdPid::cleanup(replyVector);
101  ByteArray replyVectorCompressed = ObdPid::compress(replyVectorClean);
102 
103  for(auto itr = supportedPidsList.begin(); itr != supportedPidsList.end(); itr++)
104  {
105  ObdPid* p = *itr;
106 
107  if(p->needsCompress())
108  {
109  replyVector = replyVectorCompressed;
110  }
111  else replyVector = replyVectorClean;
112 
113  if (!p->tryParse(replyVector))
114  {
115  continue;
116  }
117 
118  ObdPid* pid = (*itr)->create();
119  return pid;
120  }
121  return 0;
122  }
123  ObdPid* createPidforProperty(VehicleProperty::Property property)
124  {
125  for(auto itr = supportedPidsList.begin(); itr != supportedPidsList.end(); itr++)
126  {
127  VehicleProperty::Property p = (*itr)->property;
128  if(p == property)
129  {
130  ObdPid* obj = *itr;
131  return obj->create();
132  }
133  }
134  return NULL;
135  }
136 
137  std::list<ObdPid*> supportedPidsList;
138 };
139 
141 {
142 
143 public:
144  OBD2Source(AbstractRoutingEngine* re, map<string, string> config);
145  ~OBD2Source();
146  const string uuid();
147  int portHandle;
151  void subscribeToPropertyChanges(VehicleProperty::Property property);
152  void unsubscribeToPropertyChanges(VehicleProperty::Property property);
153  PropertyList supported();
154 
155  PropertyInfo getPropertyInfo(const VehicleProperty::Property &property);
156 
157  int supportedOperations();
158 
159  PropertyList queuedRequests;
160  bool clientConnected;
161  PropertyList activeRequests;
162  void engineSpeed(double speed);
163  void vehicleSpeed(int speed);
164  void mafValue(double maf);
165  void engineCoolantTemp(int temp);
166  PropertyList removeRequests;
167  void setSupported(PropertyList list);
168 
169  void supportedChanged(const PropertyList &) {}
170  GAsyncQueue* commandQueue;
171  GAsyncQueue* statusQueue;
172  GAsyncQueue* subscriptionAddQueue;
173  GAsyncQueue* subscriptionRemoveQueue;
174  GAsyncQueue* singleShotQueue;
175  GAsyncQueue* responseQueue;
176  std::list<std::string> m_blacklistPidList;
177  std::map<std::string,int> m_blacklistPidCountMap;
178  void setConfiguration(map<string, string> config);
179  //void randomizeProperties();
180  std::string m_port;
181  std::string m_baud;
182  bool m_isBluetooth;
183  std::string m_btDeviceAddress;
184  std::string m_btAdapterAddress;
185  std::vector<AsyncPropertyReply*> propertyReplyList;
186  void updateProperty(AbstractPropertyType *value);
187  obdLib * obd;
188  bool m_threadLive;
189  GThread *m_gThread;
190 
191  typedef BasicPropertyType<bool> Obd2ConnectType;
192  Obd2ConnectType obd2Connected;
193 
194 private:
195  PropertyList m_supportedProperties;
196  std::map<VehicleProperty::Property, AbstractPropertyType*> oldValueMap;
197  GMutex *threadQueueMutex;
198 
199 
200 };
201 
202 #endif // OBD2SOURCE_H