2 Copyright (C) 2012 Intel Corporation
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.
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.
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
19 #include "murphysource.h"
22 #include <boost/assert.hpp>
23 #include <boost/lexical_cast.hpp>
26 #include <listplusplus.h>
27 #include <timestamp.h>
29 #include <murphy/common.h>
32 #include <murphy/qt/qt-glue.h>
35 #include <murphy/glib/glib-glue.h>
38 // #include <vehicleproperty.h>
39 // #include <abstractpropertytype.h>
44 void MurphySource::processValue(string propertyName, AbstractPropertyType *prop)
46 if (murphyProperties.find(propertyName) != murphyProperties.end())
48 delete murphyProperties[propertyName];
49 murphyProperties[propertyName] = prop;
53 murphyProperties[propertyName] = prop;
54 m_re->updateSupported(supported(), PropertyList(), this);
57 // cout << "updating property!" << endl;
58 m_re->updateProperty(prop, uuid());
61 bool MurphySource::hasProperty(string propertyName)
63 PropertyList props = supported();
64 return contains(props,propertyName);
67 static void recvfrom_msg(mrp_transport_t *transp, mrp_msg_t *msg,
68 mrp_sockaddr_t *addr, socklen_t addrlen,
71 MurphySource *s = (MurphySource *) user_data;
76 mrp_msg_value_t value;
79 std::string property_name;
81 DebugOut()<<"Received a message from Murphy!"<<endl;
83 if (!mrp_msg_iterate(msg, &cursor, &tag, &type, &value, &size))
86 if (tag == 1 && type == MRP_MSG_FIELD_STRING)
87 property_name = value.str;
89 if (!mrp_msg_iterate(msg, &cursor, &tag, &type, &value, &size))
95 DebugOut() << "Property '" << property_name << "' with value: " <<endl;
97 bool hasProp = contains(VehicleProperty::capabilities(), property_name);
103 case MRP_MSG_FIELD_STRING:
108 VehicleProperty::registerProperty(property_name,
109 [property_name](){return new StringPropertyType(property_name, "");});
112 DebugOut() << "string:" << value.str << std::endl;
115 case MRP_MSG_FIELD_DOUBLE:
121 VehicleProperty::registerProperty(property_name,
122 [property_name](){return new BasicPropertyType<double>(property_name, 0);});
125 DebugOut() << "double:" << value.dbl << std::endl;
128 case MRP_MSG_FIELD_BOOL:
134 VehicleProperty::registerProperty(property_name,
135 [property_name](){return new BasicPropertyType<bool>(property_name, FALSE);});
138 DebugOut() << "boolean:" << value.bln << std::endl;
141 case MRP_MSG_FIELD_UINT32:
147 VehicleProperty::registerProperty(property_name,
148 [property_name](){return new BasicPropertyType<uint32_t>(property_name, 0);});
151 DebugOut() << "uint32:" << value.u32 << std::endl;
154 case MRP_MSG_FIELD_UINT16:
160 VehicleProperty::registerProperty(property_name,
161 [property_name](){return new BasicPropertyType<uint16_t>(property_name, 0);});
164 DebugOut() << "uint16:" << value.u16 << std::endl;
168 case MRP_MSG_FIELD_INT32:
174 VehicleProperty::registerProperty(property_name,
175 [property_name](){return new BasicPropertyType<int32_t>(property_name, 0);});
178 DebugOut() << "int32:" << value.s32 << std::endl;
181 case MRP_MSG_FIELD_INT16:
187 VehicleProperty::registerProperty(property_name,
188 [property_name](){return new BasicPropertyType<int16_t>(property_name, 0);});
191 DebugOut() << "int16:" << value.s16 << std::endl;
195 DebugOut()<<"Unknown type"<<endl;
198 AbstractPropertyType* prop = VehicleProperty::getPropertyTypeForPropertyNameValue(property_name);
200 prop->fromString(val.str());
201 s->processValue(property_name, prop);
204 static void recv_msg(mrp_transport_t *transp, mrp_msg_t *msg, void *user_data)
206 return recvfrom_msg(transp, msg, NULL, 0, user_data);
209 static void closed_evt(mrp_transport_t *t, int error, void *user_data)
211 /* TODO: should process the error somehow */
213 MurphySource *s = (MurphySource *) user_data;
215 s->setState(MRP_PROCESS_STATE_NOT_READY);
216 s->setConnected(FALSE);
220 void MurphySource::setConnected(bool connected)
222 m_connected = connected;
226 int MurphySource::connectToMurphy()
230 int flags = MRP_TRANSPORT_REUSEADDR | MRP_TRANSPORT_MODE_MSG;
231 static mrp_transport_evt_t evt;
234 evt.recvmsg = recv_msg;
235 evt.recvmsgfrom = recvfrom_msg;
236 evt.closed = closed_evt;
238 if (m_ml == NULL || m_address.empty())
241 if (m_connected == TRUE)
244 alen = mrp_transport_resolve(NULL, m_address.c_str(), &addr, sizeof(addr), &atype);
248 debugOut("Failed to resolve address");
252 m_tport = mrp_transport_create(m_ml, atype, &evt, this, flags);
256 debugOut("Can't create a Murphy transport");
260 if (mrp_transport_connect(m_tport, &addr, alen) == 0)
262 mrp_transport_destroy(m_tport);
264 debugOut("Failed to connect to Murphy");
273 PropertyInfo MurphySource::getPropertyInfo(const VehicleProperty::Property &property)
275 Zone::ZoneList zones;
276 zones.push_back(murphyProperties[property]->zone);
278 return PropertyInfo(0, zones);
282 MurphySource::MurphySource(AbstractRoutingEngine *re, map<string, string> config) : AbstractSource(re, config)
288 // main loop integration
291 m_ml = mrp_mainloop_qt_get();
292 debugOut("Murphy plugin initialized using QT mainloop!");
294 GMainLoop *g_ml = g_main_loop_new(NULL, TRUE);
295 m_ml = mrp_mainloop_glib_get(g_ml);
296 debugOut("Murphy plugin initialized using glib mainloop!");
299 setConfiguration(config);
302 MurphySource::~MurphySource()
304 mrp_process_set_state("ambd", MRP_PROCESS_STATE_NOT_READY);
305 mrp_transport_destroy(m_tport);
306 mrp_mainloop_unregister(m_ml);
308 map<string, AbstractPropertyType *>::iterator i;
310 for (i = murphyProperties.begin(); i != murphyProperties.end(); i++)
312 // TODO: unregister VehicleProperty (*i).first
318 void MurphySource::setState(mrp_process_state_t state)
323 mrp_process_state_t MurphySource::getState()
328 static void murphy_watch(const char *id, mrp_process_state_t state, void *user_data)
330 MurphySource *s = (MurphySource *) user_data;
332 debugOut("murphy process watch event");
334 if (strcmp(id, "murphy-amb") != 0)
337 printf("murphyd state changed to %s\n",
338 state == MRP_PROCESS_STATE_READY ? "ready" : "not ready");
340 if (state == MRP_PROCESS_STATE_NOT_READY &&
341 s->getState() == MRP_PROCESS_STATE_READY)
343 DebugOut()<<"lost connection to murphyd"<<endl;
346 else if (state == MRP_PROCESS_STATE_READY)
348 /* start connecting if needed */
349 s->connectToMurphy();
355 void MurphySource::readyToConnect(mrp_mainloop_t *ml)
357 /* set a watch to follow Murphy status */
359 if (mrp_process_set_watch("murphy-amb", ml, murphy_watch, this) < 0)
361 DebugOut()<<"failed to set a murphy process watch"<<endl;
365 mrp_process_set_state("ambd", MRP_PROCESS_STATE_READY);
367 /* check if Murphy is running */
368 m_state = mrp_process_query_state("murphy-amb");
370 if (m_state == MRP_PROCESS_STATE_READY)
375 void MurphySource::setConfiguration(map<string, string> config)
379 for (map<string,string>::iterator i=configuration.begin();i!=configuration.end();i++)
381 if ((*i).first == "address")
383 address = (*i).second;
384 // cout << "address: " << address << endl;
386 // TODO: sanity check
391 // TODO: read supported values from configuration?
392 m_re->updateSupported(supported(), PropertyList(), this);
394 // set up the connection with Murphy
395 if (!m_address.empty())
396 readyToConnect(m_ml);
400 PropertyList MurphySource::supported()
402 // debugOut("> supported");
404 PropertyList properties;
405 map<string, AbstractPropertyType *>::iterator i;
407 for (i = murphyProperties.begin(); i != murphyProperties.end(); i++)
409 properties.push_back((*i).first);
416 int MurphySource::supportedOperations()
418 // debugOut("> supportedOperations");
419 return Get; // | Set;
420 //we really don't support Set
424 void MurphySource::subscribeToPropertyChanges(VehicleProperty::Property property)
426 // debugOut("> subscribeToPropertyChanges");
430 void MurphySource::unsubscribeToPropertyChanges(VehicleProperty::Property property)
432 // debugOut("> unsubscribeToPropertyChanges");
436 void MurphySource::getPropertyAsync(AsyncPropertyReply *reply)
438 // debugOut("> getPropertyAsync");
440 if (murphyProperties.find(reply->property) != murphyProperties.end())
442 AbstractPropertyType *prop = murphyProperties[reply->property];
444 reply->success = true;
445 reply->completed(reply);
450 void MurphySource::getRangePropertyAsync(AsyncRangePropertyReply *reply)
452 // debugOut("> getRangePropertyAsync");
456 AsyncPropertyReply *MurphySource::setProperty(AsyncSetPropertyRequest request)
458 // debugOut("> setProperty");
460 processValue(request.property, request.value);
462 AsyncPropertyReply* reply = new AsyncPropertyReply(request);
463 reply->success = true;
464 reply->completed(reply);
469 extern "C" AbstractSource *create(AbstractRoutingEngine* routingengine, map<string, string> config)
471 return new MurphySource(routingengine, config);