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";
79 AMBIF::AMBIF(AbstractRoutingEngine *engine,
80 std::map<std::string, std::string> config) :
81 AbstractSource(engine, config)
88 vehicleinfoArray.clear();
89 pthread_mutex_destroy(&mutex);
93 AMBIF::getPropertyAsync(AsyncPropertyReply *reply)
95 reply->success = false;
96 DebugOut(10) << "AMBIF " << "Get Request property : " << reply->property
99 AMBVehicleInfo *vehicleinfo = find(reply->property);
100 DebugOut(50) << "AMBIF " << "Find Data : " << reply->property << std::endl;
101 if (vehicleinfo != NULL) {
102 reply->value = vehicleinfo->value;
103 reply->success = true;
104 reply->completed(reply);
110 AMBIF::setProperty(AsyncSetPropertyRequest request)
112 DebugOut(10) << "AMBIF" << "Set Request propety : " << request.property
115 AMBVehicleInfo *vehicleinfo = find(request.property);
116 if (vehicleinfo == NULL) {
120 AsyncPropertyReply *reply = new AsyncPropertyReply(request);
121 reply->success = true;
122 DebugOut(50) << "AMBIF" << "Update Value!" << std::endl;
123 delete vehicleinfo->value;
124 vehicleinfo->value = request.value->copy();
125 reply->value = vehicleinfo->value;
126 communicator->setMWVehicleInfo(vehicleinfo);
127 DebugOut(50) << "AMBIF setProperty " << "Set Value(" << request.property
128 << "," << reply->value->toString() << ")" << std::endl;
129 reply->completed(reply);
131 routingEngine->updateProperty(vehicleinfo->value, uuid());
133 routingEngine->updateProperty(vehicleinfo->name, vehicleinfo->value, uuid());
146 AMBIF::supportedOperations()
158 return "f68f8b9a-fafb-4284-8ced-b45b5d720185";
162 AMBIF::propertyChanged(VehicleProperty::Property property,
163 AbstractPropertyType *value, std::string uuid)
165 DebugOut(1) << "INFO CHG_VIC_INF Receive notification from Core. Property is " << property << ".\n";
166 AMBVehicleInfo *vehicleinfo = find(property);
168 if (vehicleinfo != NULL) {
169 delete vehicleinfo->value;
170 vehicleinfo->value = value->copy();
176 AMBIF::setConfiguratin(std::map<std::string, std::string> config)
181 AMBIF::initialize(VICCommunicator *comm, AMBConfig *conf)
183 DebugOut(50) << "AMBIF Initialize\n";
185 mutex = PTHREAD_MUTEX_INITIALIZER;
187 vector<VehicleInfoDefine> table;
188 table = conf->getVehicleInfoConfig();
189 for (auto itr = table.begin(); itr != table.end(); itr++) {
190 for (auto itr2 = (*itr).status.begin(); itr2 != (*itr).status.end();
194 vi.name = (*itr2).ambPropertyName;
195 vi.value = VehicleProperty::getPropertyTypeForPropertyNameValue(
196 vi.name, (*itr2).defaultvalue);
197 if (vi.value == nullptr) {
198 if (!registVehicleInfo(vi.name, (*itr2).type,
199 (*itr2).defaultvalue)) {
200 DebugOut(50) << "AMBIF Initialize Couldn't regist property["
204 vi.value = VehicleProperty::getPropertyTypeForPropertyNameValue(
205 vi.name, (*itr2).defaultvalue);
208 vi.zone = (*itr2).zone;
209 vehicleinfoArray.push_back(vi);
210 propertylist.push_back(vi.name);
211 DebugOut(50) << "AMBIF Initialize regist propertyname = " << vi.name
215 routingEngine->updateSupported(supported(), PropertyList(), this);
216 DebugOut(1) << "INFO CHG_VIC_INF The number of AMB vehicle info is " << vehicleinfoArray.size() << ".\n";
221 AMBIF::getPropertyRequest(std::string propertyname)
223 DebugOut(50) << "AMBIF getPropertyRequest(" << propertyname << ")\n";
224 AsyncPropertyRequest request;
225 request.property = propertyname;
226 request.completed = [](AsyncPropertyReply *reply) {
227 if (reply->success) {
228 DebugOut(50) << "AMBIF getPropertyRequest completed success!!.\n";
231 DebugOut(50) << "AMBIF getPropertyRequest completed false!!.\n";
235 AsyncPropertyReply *reply = routingEngine->getPropertyAsync(request);
237 AMBVehicleInfo *vehicleinfo = find(propertyname);
238 if (vehicleinfo == NULL) {
243 if (vehicleinfo->value != reply->value && reply->success) {
244 delete vehicleinfo->value;
245 vehicleinfo->value = reply->value->copy();
249 DebugOut(50) << "AMBIF getPropertyRequest after call "
250 << vehicleinfo->value->toString() << std::endl;
255 AMBIF::setPropertyRequest(AMBVehicleInfo *vehicleinfo)
257 AsyncSetPropertyRequest request;
258 request.property = vehicleinfo->name;
259 request.value = vehicleinfo->value;
261 [](AsyncPropertyReply *reply) {
262 if (reply->success) {
263 DebugOut(50)<<"AMBIF" << reply->property << ":" << reply->value->toString() << std::endl;
266 DebugOut(50)<<"AMBIF" << reply->property << " isn't registered." << std::endl;
269 AsyncPropertyReply *reply = routingEngine->setProperty(request);
276 AMBIF::updateProperty(AMBVehicleInfo *vehicleinfo)
278 if (vehicleinfo->name == VehicleProperty::VehicleSpeed) {
279 static uint16_t prevspd = -1;
280 static const uint16_t unusablespd = -1;
281 uint16_t spd = vehicleinfo->value->value<uint16_t>();
282 if ((prevspd == unusablespd && spd > 0) || (prevspd == 0 && spd > 0)) {
283 DebugOut(3) << "PERF CHG_VIC_INF VIC-Plugin notify Code of update "
285 << ". VehicleSpeed is 1km/h or more.\n";
287 else if ((prevspd == unusablespd && spd == 0) ||
288 (prevspd > 0 && spd == 0)) {
289 DebugOut(3) << "PERF CHG_VIC_INF VIC-Plugin notify Code of update "
290 << vehicleinfo->name << ". VehicleSpeed is 0km/h.\n";
295 routingEngine->updateProperty(vehicleinfo->value, uuid());
297 routingEngine->updateProperty(vehicleinfo->name, vehicleinfo->value, uuid());
299 AMBVehicleInfo *ambvehicleinfo = find(vehicleinfo->name);
301 if (ambvehicleinfo != NULL) {
302 delete ambvehicleinfo->value;
303 ambvehicleinfo->value = vehicleinfo->value->copy();
311 pthread_mutex_lock(&mutex);
317 pthread_mutex_unlock(&mutex);
321 AMBIF::requestUpdate(AMBVehicleInfo *vehicleinfo)
323 DebugOut(50) << "AMBIF requestUpdate request property name is "
324 << vehicleinfo->name << "\n";
325 if (find(vehicleinfo->name) != NULL) {
326 vehicleinfo->value->zone = vehicleinfo->zone;
327 vehicleinfo->value->priority = AbstractPropertyType::Instant;
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(const VehicleProperty::Property & property) {
429 if (propertyInfoMap.find(property) != propertyInfoMap.end()) {
430 return propertyInfoMap[property];
432 Zone::ZoneList 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];