2 * Copyright (C) 2012 TOYOTA MOTOR 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
25 #include "ambinterface.h"
26 #include "ambconfig.h"
28 #include "mwinterface.h"
29 #include "viccommunicator.h"
31 extern "C" AbstractSource *
32 create(AbstractRoutingEngine* routingengine, map<string, string> config)
34 AMBIF *ambif = new AMBIF(routingengine, config);
35 AMBConfig *conf = new AMBConfig();
36 if (!conf->readConfig(config["configfile"])) {
37 DebugOut(DebugOut::Error) << "Failed to Load Configfile for VIC-Plugin.\n";
42 VICCommunicator *communicator = new VICCommunicator();
43 MWIF *mwif = new MWIF();
44 Converter *converter = new Converter();
45 if (!ambif->initialize(communicator, conf)) {
46 DebugOut(DebugOut::Error) << "Failed to initialize AMBIF\n";
53 if (!communicator->initialize(ambif, mwif, converter)) {
54 DebugOut(DebugOut::Error) << "Failed to initialize VICCommunicator\n";
61 if (!mwif->initialize(communicator, conf)) {
62 DebugOut(DebugOut::Error) << "Failed to initialize MWIF\n";
69 if (!converter->initialize(conf)) {
70 DebugOut(DebugOut::Error) << "Failed to initialize Converter\n";
80 AMBIF::AMBIF(AbstractRoutingEngine *engine,
81 std::map<std::string, std::string> config) :
82 AbstractSource(engine, config)
89 vehicleinfoArray.clear();
90 pthread_mutex_destroy(&mutex);
94 AMBIF::getPropertyAsync(AsyncPropertyReply *reply)
96 reply->success = false;
97 DebugOut(10) << "AMBIF " << "Get Request property : " << reply->property
100 AMBVehicleInfo *vehicleinfo = find(reply->property);
101 DebugOut(50) << "AMBIF " << "Find Data : " << reply->property << std::endl;
102 if (vehicleinfo != NULL) {
103 reply->value = vehicleinfo->value;
104 reply->success = true;
105 reply->completed(reply);
111 AMBIF::setProperty(AsyncSetPropertyRequest request)
113 DebugOut(10) << "AMBIF" << "Set Request propety : " << request.property
116 AMBVehicleInfo *vehicleinfo = find(request.property);
117 if (vehicleinfo == NULL) {
121 AsyncPropertyReply *reply = new AsyncPropertyReply(request);
122 reply->success = true;
123 DebugOut(50) << "AMBIF" << "Update Value!" << std::endl;
124 delete vehicleinfo->value;
125 vehicleinfo->value = request.value->copy();
126 reply->value = vehicleinfo->value;
127 communicator->setMWVehicleInfo(vehicleinfo);
128 DebugOut(50) << "AMBIF setProperty " << "Set Value(" << request.property
129 << "," << reply->value->toString() << ")" << std::endl;
130 reply->completed(reply);
132 routingEngine->updateProperty(vehicleinfo->value, uuid());
134 routingEngine->updateProperty(vehicleinfo->name, vehicleinfo->value, uuid());
147 AMBIF::supportedOperations()
159 return "f68f8b9a-fafb-4284-8ced-b45b5d720185";
163 AMBIF::propertyChanged(VehicleProperty::Property property,
164 AbstractPropertyType *value, std::string uuid)
166 DebugOut(1) << "INFO CHG_VIC_INF Receive notification from Core. Property is " << property << ".\n";
167 AMBVehicleInfo *vehicleinfo = find(property);
169 if (vehicleinfo != NULL) {
170 delete vehicleinfo->value;
171 vehicleinfo->value = value->copy();
177 AMBIF::setConfiguratin(std::map<std::string, std::string> config)
182 AMBIF::initialize(VICCommunicator *comm, AMBConfig *conf)
184 DebugOut(50) << "AMBIF Initialize\n";
186 mutex = PTHREAD_MUTEX_INITIALIZER;
188 vector<VehicleInfoDefine> table;
189 table = conf->getVehicleInfoConfig();
190 for (auto itr = table.begin(); itr != table.end(); itr++) {
191 for (auto itr2 = (*itr).status.begin(); itr2 != (*itr).status.end();
195 vi.name = (*itr2).ambPropertyName;
196 vi.value = VehicleProperty::getPropertyTypeForPropertyNameValue(
197 vi.name, (*itr2).defaultvalue);
198 if (vi.value == nullptr) {
199 if (!registVehicleInfo(vi.name, (*itr2).type,
200 (*itr2).defaultvalue)) {
201 DebugOut(50) << "AMBIF Initialize Couldn't regist property["
205 vi.value = VehicleProperty::getPropertyTypeForPropertyNameValue(
206 vi.name, (*itr2).defaultvalue);
209 vi.zone = (*itr2).zone;
210 vehicleinfoArray.push_back(vi);
211 propertylist.push_back(vi.name);
212 DebugOut(50) << "AMBIF Initialize regist propertyname = " << vi.name
216 routingEngine->updateSupported(supported(), PropertyList(), this);
217 DebugOut(1) << "INFO CHG_VIC_INF The number of AMB vehicle info is " << vehicleinfoArray.size() << ".\n";
222 AMBIF::getPropertyRequest(std::string propertyname)
224 DebugOut(50) << "AMBIF getPropertyRequest(" << propertyname << ")\n";
225 AsyncPropertyRequest request;
226 request.property = propertyname;
227 request.completed = [](AsyncPropertyReply *reply) {
228 if (reply->success) {
229 DebugOut(50) << "AMBIF getPropertyRequest completed success!!.\n";
232 DebugOut(50) << "AMBIF getPropertyRequest completed false!!.\n";
236 AsyncPropertyReply *reply = routingEngine->getPropertyAsync(request);
238 AMBVehicleInfo *vehicleinfo = find(propertyname);
239 if (vehicleinfo == NULL) {
244 if (vehicleinfo->value != reply->value && reply->success) {
245 delete vehicleinfo->value;
246 vehicleinfo->value = reply->value->copy();
250 DebugOut(50) << "AMBIF getPropertyRequest after call "
251 << vehicleinfo->value->toString() << std::endl;
256 AMBIF::setPropertyRequest(AMBVehicleInfo *vehicleinfo)
258 AsyncSetPropertyRequest request;
259 request.property = vehicleinfo->name;
260 request.value = vehicleinfo->value;
262 [](AsyncPropertyReply *reply) {
263 if (reply->success) {
264 DebugOut(50)<<"AMBIF" << reply->property << ":" << reply->value->toString() << std::endl;
267 DebugOut(50)<<"AMBIF" << reply->property << " isn't registered." << std::endl;
270 AsyncPropertyReply *reply = routingEngine->setProperty(request);
277 AMBIF::updateProperty(AMBVehicleInfo *vehicleinfo)
279 if (vehicleinfo->name == VehicleProperty::VehicleSpeed) {
280 static uint16_t prevspd = -1;
281 static const uint16_t unusablespd = -1;
282 uint16_t spd = vehicleinfo->value->value<uint16_t>();
283 if ((prevspd == unusablespd && spd > 0) || (prevspd == 0 && spd > 0)) {
284 DebugOut(3) << "PERF CHG_VIC_INF VIC-Plugin notify Code of update "
286 << ". VehicleSpeed is 1km/h or more.\n";
288 else if ((prevspd == unusablespd && spd == 0) ||
289 (prevspd > 0 && spd == 0)) {
290 DebugOut(3) << "PERF CHG_VIC_INF VIC-Plugin notify Code of update "
291 << vehicleinfo->name << ". VehicleSpeed is 0km/h.\n";
296 routingEngine->updateProperty(vehicleinfo->value, uuid());
298 routingEngine->updateProperty(vehicleinfo->name, vehicleinfo->value, uuid());
300 AMBVehicleInfo *ambvehicleinfo = find(vehicleinfo->name);
302 if (ambvehicleinfo != NULL) {
303 delete ambvehicleinfo->value;
304 ambvehicleinfo->value = vehicleinfo->value->copy();
312 pthread_mutex_lock(&mutex);
318 pthread_mutex_unlock(&mutex);
322 AMBIF::requestUpdate(AMBVehicleInfo *vehicleinfo)
324 DebugOut(50) << "AMBIF requestUpdate request property name is "
325 << vehicleinfo->name << "\n";
326 if (find(vehicleinfo->name) != NULL) {
327 vehicleinfo->value->zone = vehicleinfo->zone;
328 DebugOut(50) << "AMBIF requestUpdate request property name is "
329 << vehicleinfo->name << ", zone is " << vehicleinfo->zone << "\n";
330 updateProperty(vehicleinfo);
333 setPropertyRequest(vehicleinfo);
338 AMBIF::registVehicleInfo(std::string propertyName, DataType type, string value)
340 DebugOut(50) << "AMBIF registVehicleInfo(" << propertyName << ")\n";
341 VehicleProperty::PropertyTypeFactoryCallback factory;
345 factory = [value, propertyName]() {
346 return new BasicPropertyType<int>(propertyName, value);
352 factory = [value, propertyName]() {
353 return new BasicPropertyType<double>(propertyName, value);
359 factory = [value, propertyName]() {
360 return new BasicPropertyType<char>(propertyName, value);
366 factory = [value, propertyName]() {
367 return new BasicPropertyType<int16_t>(propertyName, value);
373 factory = [value, propertyName]() {
374 return new BasicPropertyType<uint16_t>(propertyName, value);
380 factory = [value, propertyName]() {
381 return new BasicPropertyType<uint32_t>(propertyName, value);
387 factory = [value, propertyName]() {
388 return new BasicPropertyType<int64_t>(propertyName, value);
394 factory = [value, propertyName]() {
395 return new BasicPropertyType<uint64_t>(propertyName, value);
401 factory = [value, propertyName]() {
402 return new BasicPropertyType<bool>(propertyName, value);
411 return VehicleProperty::registerProperty(propertyName, factory);
415 AMBIF::find(std::string propertyName)
418 vi.name = propertyName;
419 std::vector<AMBVehicleInfo>::iterator itr;
420 if ((itr = std::find(vehicleinfoArray.begin(), vehicleinfoArray.end(), vi))
421 != vehicleinfoArray.end()) {
428 PropertyInfo AMBIF::getPropertyInfo(VehicleProperty::Property property) {
429 if (propertyInfoMap.find(property) != propertyInfoMap.end()) {
430 return propertyInfoMap[property];
432 std::list<Zone::Type> zones;
435 std::vector<AMBVehicleInfo>::iterator itr, itr_idx, itr_end;
436 itr_idx = vehicleinfoArray.begin();
437 itr_end = vehicleinfoArray.end();
438 while ((itr = std::find(itr_idx, itr_end, vi)) != itr_end) {
439 zones.push_back((*itr).zone);
443 return PropertyInfo::invalid();
446 PropertyInfo info(0, zones);
447 propertyInfoMap[vi.name] = info;
448 return propertyInfoMap[vi.name];