automotive-message-broker  0.14.803
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
cangenplugin.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 #ifndef _CANGENPLUGINIMPL_H_
20 #define _CANGENPLUGINIMPL_H_
21 
22 #include <map>
23 #include <memory>
24 #include <tgmath.h>
25 #include <libwebsockets.h>
26 #include <json.h>
27 
28 #include <canbus.h>
29 #include <canobserver.h>
30 
31 #include <ambpluginimpl.h>
32 #include "websockets.h"
33 
76 static const char* CANSimPluginUUID = "3f43e231-11ec-4782-9b5a-3dbcc5221eeb";
77 
79 
80 public:
81 
87  CANGenPlugin(AbstractRoutingEngine* re, const std::map<std::string, std::string>& config, AbstractSource &parent);
88  virtual ~CANGenPlugin(); // has to be virtual because of unit tests
89 
90  // from AbstractSink
91 public:
92 
96  const std::string uuid() const { return "becbbef9-6cc8-4b9e-8cd7-2fbe37b9b52a"; }
97 
105 
107 
108  // from CANObserver
109 public:
115  virtual void errorOccured(CANObserver::CANError error);/* socket error */
121  virtual void standardFrameReceived(const can_frame& frame);/* SFF was present */
127  virtual void extendedFrameReceived(const can_frame& frame);/* EFF was present */
133  virtual void errorFrameReceived(const can_frame& frame);/* error frame */
139  virtual void remoteTransmissionRequest(const can_frame& frame);/* remote transmission request (SFF/EFF is still present)*/
145  virtual void timeoutDetected(const can_frame& frame);
146 
151  virtual void init();
152 
153  // from WebSocketsObserver
154 
163  void dataReceived(lws* socket, const char* data, size_t len);
164 
165 //
166 // Internal methods:
167 //
168 private:
169 
176  void printFrame(const can_frame& frame) const;
177 
183  void parseMappingTable(const std::string& json);
184 
194  void getValue(lws* socket, const std::string& property, int zone, const std::string& uuid);
195 
207  void setValue(lws* socket, const std::string& property, const std::string& value, int zone, const std::string& interface, const std::string& transactionId);
208 
217  bool sendValue(const std::string& interface, AbstractPropertyType* value);
218 
227  class MappingTable{
228  public:
229  MappingTable()
230  {
231  }
232 
233  MappingTable(const MappingTable& other) = delete;
234  MappingTable& operator=(const MappingTable& other) = delete;
235  MappingTable(MappingTable&& other) = default;
236  MappingTable& operator=(MappingTable&& other) = default;
237 
238  void addProperty(const std::string& source, json_object* signal)
239  {
240  json_object* canIdObj = json_object_object_get(signal, "can_id");
241  json_object* nameObj = json_object_object_get(signal, "name");
242  if(!canIdObj || !nameObj) // mandatory
243  return;
244  Zone::Type zone(Zone::None);
245  json_object* zoneObj = json_object_object_get(signal, "zone");
246  if(zoneObj)
247  zone = json_object_get_int(zoneObj);
248 
249  auto& zp = mapping[source];
250  auto& prop = zp[Zone::Type(zone)];
251  std::string name(json_object_get_string(nameObj));
252  int can_id = json_object_get_int(canIdObj);
253  prop[name] = can_id; // update an existing value
254  }
255 
256  int getCanId(const std::string& source, const Zone::Type& zone, const VehicleProperty::Property& name) const
257  {
258  //return mapping[source][zone][name]; // caution! this will insert if not found. I don't want it.
259  auto sourceIt = mapping.find(source);
260  if(sourceIt == mapping.end())
261  return 0;
262  auto zoneIt = sourceIt->second.find(zone);
263  if(zoneIt == sourceIt->second.end())
264  return 0;
265  auto propIt = zoneIt->second.find(name);
266  if(propIt == zoneIt->second.end())
267  return 0;
268  else
269  return propIt->second;
270  }
271 
272  void clear()
273  {
274  mapping.clear();
275  }
276 
277  private:
278  typedef std::map< Zone::Type, std::map<VehicleProperty::Property, canid_t> > ZonedProperty;
279  std::map<std::string, ZonedProperty> mapping;
280  };
281 
282 //
283 // data:
284 //
285 
290  MappingTable mappingTable;
291 
296  std::map<std::string, std::shared_ptr<CANBus> > interfaces;
297 
302  std::unique_ptr<WebSockets> ws;
303 
308  interprocess_recursive_mutex mutex;
309 };
310 
312 {
313 public:
314  SimCommand(): StringPropertyType("SimCommand") { }
315  SimCommand(std::string val) : StringPropertyType("SimCommand",val) { }
316 };
317 
318 #endif // _CANGENPLUGINIMPL_H_
319