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>
30 #include <murphy/glib/glib-glue.h>
34 void MurphySource::processValue(string propertyName, AbstractPropertyType *prop)
36 if (murphyProperties.find(propertyName) != murphyProperties.end())
38 delete murphyProperties[propertyName];
39 murphyProperties[propertyName] = prop;
43 murphyProperties[propertyName] = prop;
44 m_re->updateSupported(supported(), PropertyList(), this);
47 m_re->updateProperty(prop, uuid());
50 bool MurphySource::hasProperty(string propertyName)
52 PropertyList props = supported();
53 return contains(props,propertyName);
56 static void recvfrom_msg(mrp_transport_t *transp, mrp_msg_t *msg,
57 mrp_sockaddr_t *addr, socklen_t addrlen,
60 MurphySource *s = (MurphySource *) user_data;
65 mrp_msg_value_t value;
68 std::string property_name;
70 DebugOut()<<"Received a message from Murphy!"<<endl;
72 if (!mrp_msg_iterate(msg, &cursor, &tag, &type, &value, &size))
75 if (tag == 1 && type == MRP_MSG_FIELD_STRING)
76 property_name = value.str;
78 if (!mrp_msg_iterate(msg, &cursor, &tag, &type, &value, &size))
84 DebugOut() << "Property '" << property_name << "' with value: " <<endl;
86 bool hasProp = contains(VehicleProperty::capabilities(), property_name);
92 case MRP_MSG_FIELD_STRING:
97 VehicleProperty::registerProperty(property_name,
98 [property_name](){return new StringPropertyType(property_name, "");});
101 DebugOut() << "string:" << value.str << std::endl;
104 case MRP_MSG_FIELD_DOUBLE:
110 VehicleProperty::registerProperty(property_name,
111 [property_name](){return new BasicPropertyType<double>(property_name, 0);});
114 DebugOut() << "double:" << value.dbl << std::endl;
117 case MRP_MSG_FIELD_BOOL:
123 VehicleProperty::registerProperty(property_name,
124 [property_name](){return new BasicPropertyType<bool>(property_name, FALSE);});
127 DebugOut() << "boolean:" << value.bln << std::endl;
130 case MRP_MSG_FIELD_UINT32:
136 VehicleProperty::registerProperty(property_name,
137 [property_name](){return new BasicPropertyType<uint32_t>(property_name, 0);});
140 DebugOut() << "uint32:" << value.u32 << std::endl;
143 case MRP_MSG_FIELD_UINT16:
149 VehicleProperty::registerProperty(property_name,
150 [property_name](){return new BasicPropertyType<uint16_t>(property_name, 0);});
153 DebugOut() << "uint16:" << value.u16 << std::endl;
157 case MRP_MSG_FIELD_INT32:
163 VehicleProperty::registerProperty(property_name,
164 [property_name](){return new BasicPropertyType<int32_t>(property_name, 0);});
167 DebugOut() << "int32:" << value.s32 << std::endl;
170 case MRP_MSG_FIELD_INT16:
176 VehicleProperty::registerProperty(property_name,
177 [property_name](){return new BasicPropertyType<int16_t>(property_name, 0);});
180 DebugOut() << "int16:" << value.s16 << std::endl;
184 DebugOut()<<"Unknown type"<<endl;
187 AbstractPropertyType* prop = VehicleProperty::getPropertyTypeForPropertyNameValue(property_name);
189 prop->fromString(val.str());
190 s->processValue(property_name, prop);
193 static void recv_msg(mrp_transport_t *transp, mrp_msg_t *msg, void *user_data)
195 return recvfrom_msg(transp, msg, NULL, 0, user_data);
198 static void closed_evt(mrp_transport_t *t, int error, void *user_data)
200 /* TODO: should process the error somehow */
202 MurphySource *s = (MurphySource *) user_data;
204 s->setState(MRP_PROCESS_STATE_NOT_READY);
205 s->setConnected(FALSE);
209 void MurphySource::setConnected(bool connected)
211 m_connected = connected;
215 int MurphySource::connectToMurphy()
219 int flags = MRP_TRANSPORT_REUSEADDR | MRP_TRANSPORT_MODE_MSG;
220 static mrp_transport_evt_t evt;
223 evt.recvmsg = recv_msg;
224 evt.recvmsgfrom = recvfrom_msg;
225 evt.closed = closed_evt;
227 if (m_ml == NULL || m_address.empty())
230 if (m_connected == TRUE)
233 alen = mrp_transport_resolve(NULL, m_address.c_str(), &addr, sizeof(addr), &atype);
237 debugOut("Failed to resolve address");
241 m_tport = mrp_transport_create(m_ml, atype, &evt, this, flags);
245 debugOut("Can't create a Murphy transport");
249 if (mrp_transport_connect(m_tport, &addr, alen) == 0)
251 mrp_transport_destroy(m_tport);
253 debugOut("Failed to connect to Murphy");
262 PropertyInfo MurphySource::getPropertyInfo(const VehicleProperty::Property &property)
264 Zone::ZoneList zones;
265 zones.push_back(murphyProperties[property]->zone);
267 return PropertyInfo(0, zones);
271 MurphySource::MurphySource(AbstractRoutingEngine *re, map<string, string> config) : AbstractSource(re, config)
277 // main loop integration
280 m_ml = mrp_mainloop_qt_get();
281 debugOut("Murphy plugin initialized using QT mainloop!");
283 GMainLoop *g_ml = g_main_loop_new(NULL, TRUE);
284 m_ml = mrp_mainloop_glib_get(g_ml);
285 debugOut("Murphy plugin initialized using glib mainloop!");
288 setConfiguration(config);
291 MurphySource::~MurphySource()
293 mrp_process_set_state("ambd", MRP_PROCESS_STATE_NOT_READY);
294 mrp_transport_destroy(m_tport);
295 mrp_mainloop_unregister(m_ml);
297 map<string, AbstractPropertyType *>::iterator i;
299 for (i = murphyProperties.begin(); i != murphyProperties.end(); i++)
301 // TODO: unregister VehicleProperty (*i).first
307 void MurphySource::setState(mrp_process_state_t state)
312 mrp_process_state_t MurphySource::getState()
317 static void murphy_watch(const char *id, mrp_process_state_t state, void *user_data)
319 MurphySource *s = (MurphySource *) user_data;
321 debugOut("murphy process watch event");
323 if (strcmp(id, "murphy-amb") != 0)
326 printf("murphyd state changed to %s\n",
327 state == MRP_PROCESS_STATE_READY ? "ready" : "not ready");
329 if (state == MRP_PROCESS_STATE_NOT_READY &&
330 s->getState() == MRP_PROCESS_STATE_READY)
332 DebugOut()<<"lost connection to murphyd"<<endl;
335 else if (state == MRP_PROCESS_STATE_READY)
337 /* start connecting if needed */
338 s->connectToMurphy();
344 void MurphySource::readyToConnect(mrp_mainloop_t *ml)
346 /* set a watch to follow Murphy status */
348 if (mrp_process_set_watch("murphy-amb", ml, murphy_watch, this) < 0)
350 DebugOut()<<"failed to set a murphy process watch"<<endl;
354 mrp_process_set_state("ambd", MRP_PROCESS_STATE_READY);
356 /* check if Murphy is running */
357 m_state = mrp_process_query_state("murphy-amb");
359 if (m_state == MRP_PROCESS_STATE_READY)
364 void MurphySource::setConfiguration(map<string, string> config)
368 for (map<string,string>::iterator i=configuration.begin();i!=configuration.end();i++)
370 if ((*i).first == "address")
372 address = (*i).second;
373 // cout << "address: " << address << endl;
375 // TODO: sanity check
380 // TODO: read supported values from configuration?
381 m_re->updateSupported(supported(), PropertyList(), this);
383 // set up the connection with Murphy
384 if (!m_address.empty())
385 readyToConnect(m_ml);
389 PropertyList MurphySource::supported()
391 // debugOut("> supported");
393 PropertyList properties;
394 map<string, AbstractPropertyType *>::iterator i;
396 for (i = murphyProperties.begin(); i != murphyProperties.end(); i++)
398 properties.push_back((*i).first);
405 int MurphySource::supportedOperations()
407 // debugOut("> supportedOperations");
408 return Get; // | Set;
409 //we really don't support Set
413 void MurphySource::subscribeToPropertyChanges(VehicleProperty::Property property)
415 // debugOut("> subscribeToPropertyChanges");
419 void MurphySource::unsubscribeToPropertyChanges(VehicleProperty::Property property)
421 // debugOut("> unsubscribeToPropertyChanges");
425 void MurphySource::getPropertyAsync(AsyncPropertyReply *reply)
427 // debugOut("> getPropertyAsync");
429 if (murphyProperties.find(reply->property) != murphyProperties.end())
431 AbstractPropertyType *prop = murphyProperties[reply->property];
433 reply->success = true;
434 reply->completed(reply);
439 void MurphySource::getRangePropertyAsync(AsyncRangePropertyReply *reply)
441 // debugOut("> getRangePropertyAsync");
445 AsyncPropertyReply *MurphySource::setProperty(AsyncSetPropertyRequest request)
447 // debugOut("> setProperty");
449 processValue(request.property, request.value);
451 AsyncPropertyReply* reply = new AsyncPropertyReply(request);
452 reply->success = true;
453 reply->completed(reply);
458 extern "C" void create(AbstractRoutingEngine* routingengine, map<string, string> config)
460 new MurphySource(routingengine, config);