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 conf->readConfig(config["configfile"]);
37 VICCommunicator *communicator = new VICCommunicator();
38 MWIF *mwif = new MWIF();
39 Converter *converter = new Converter();
40 if (!ambif->initialize(communicator, conf)) {
41 DebugOut() << "Failed to initialize AMBIF\n";
48 if (!communicator->initialize(ambif, mwif, converter)) {
49 DebugOut() << "Failed to initialize VICCommunicator\n";
56 if (!mwif->initialize(communicator, conf)) {
57 DebugOut() << "Failed to initialize MWIF\n";
64 if (!converter->initialize(conf)) {
65 DebugOut() << "Failed to initialize Converter\n";
75 AMBIF::AMBIF(AbstractRoutingEngine *engine,
76 std::map<std::string, std::string> config) :
77 AbstractSource(engine, config)
84 vehicleinfoArray.clear();
85 pthread_mutex_destroy(&mutex);
89 AMBIF::getPropertyAsync(AsyncPropertyReply *reply)
91 reply->success = false;
92 DebugOut() << "AMBIF " << "Get Request property : " << reply->property
95 AMBVehicleInfo *vehicleinfo = find(reply->property);
96 DebugOut() << "AMBIF " << "Find Data : " << reply->property << std::endl;
97 if (vehicleinfo != NULL) {
98 reply->value = vehicleinfo->value;
99 reply->success = true;
100 reply->completed(reply);
106 AMBIF::setProperty(AsyncSetPropertyRequest request)
108 DebugOut() << "AMBIF" << "Set Request propety : " << request.property
111 AMBVehicleInfo *vehicleinfo = find(request.property);
112 if (vehicleinfo == NULL) {
116 AsyncPropertyReply *reply = new AsyncPropertyReply(request);
117 reply->success = true;
118 DebugOut(10) << "AMBIF" << "Update Value!" << std::endl;
119 delete vehicleinfo->value;
120 vehicleinfo->value = request.value->copy();
121 reply->value = vehicleinfo->value;
122 communicator->setMWVehicleInfo(vehicleinfo);
123 DebugOut() << "AMBIF setProperty " << "Set Value(" << request.property
124 << "," << reply->value->toString() << ")" << std::endl;
125 reply->completed(reply);
126 routingEngine->updateProperty(vehicleinfo->name, vehicleinfo->value,
139 AMBIF::supportedOperations()
147 return "f68f8b9a-fafb-4284-8ced-b45b5d720185";
151 AMBIF::propertyChanged(VehicleProperty::Property property,
152 AbstractPropertyType *value, std::string uuid)
154 AMBVehicleInfo *vehicleinfo = find(property);
156 if (vehicleinfo != NULL) {
157 delete vehicleinfo->value;
158 vehicleinfo->value = value->copy();
164 AMBIF::setConfiguratin(std::map<std::string, std::string> config)
169 AMBIF::initialize(VICCommunicator *comm, AMBConfig *conf)
171 DebugOut() << "AMBIF Initialize\n";
173 mutex = PTHREAD_MUTEX_INITIALIZER;
175 vector<VehicleInfoDefine> table;
176 table = conf->getVehicleInfoConfig();
177 for (auto itr = table.begin(); itr != table.end(); itr++) {
178 for (auto itr2 = (*itr).status.begin(); itr2 != (*itr).status.end();
182 vi.name = (*itr2).ambPropertyName;
183 vi.value = VehicleProperty::getPropertyTypeForPropertyNameValue(
184 vi.name, (*itr2).defaultvalue);
185 if (vi.value == nullptr) {
186 if (!registVehicleInfo(vi.name, (*itr2).type,
187 (*itr2).defaultvalue)) {
188 DebugOut() << "AMBIF Initialize Couldn't regist property["
192 vi.value = VehicleProperty::getPropertyTypeForPropertyNameValue(
193 vi.name, (*itr2).defaultvalue);
196 vehicleinfoArray.push_back(vi);
197 propertylist.push_back(vi.name);
198 DebugOut() << "AMBIF Initialize regist propertyname = " << vi.name
202 routingEngine->setSupported(supported(), this);
207 AMBIF::getPropertyRequest(std::string propertyname)
209 DebugOut() << "AMBIF getPropertyRequest(" << propertyname << ")\n";
210 AsyncPropertyRequest request;
211 request.property = propertyname;
212 request.completed = [](AsyncPropertyReply *reply) {
213 if (reply->success) {
214 DebugOut() << "AMBIF getPropertyRequest completed success!!.\n";
217 DebugOut() << "AMBIF getPropertyRequest completed false!!.\n";
221 AsyncPropertyReply *reply = routingEngine->getPropertyAsync(request);
223 AMBVehicleInfo *vehicleinfo = find(propertyname);
224 if (vehicleinfo == NULL) {
229 if (vehicleinfo->value != reply->value && reply->success) {
230 delete vehicleinfo->value;
231 vehicleinfo->value = reply->value->copy();
235 DebugOut() << "AMBIF getPropertyRequest after call "
236 << vehicleinfo->value->toString() << std::endl;
241 AMBIF::setPropertyRequest(AMBVehicleInfo *vehicleinfo)
243 AsyncSetPropertyRequest request;
244 request.property = vehicleinfo->name;
245 request.value = vehicleinfo->value;
247 [](AsyncPropertyReply *reply) {
248 if (reply->success) {
249 DebugOut()<<"AMBIF" << reply->property << ":" << reply->value->toString() << std::endl;
252 DebugOut()<<"AMBIF" << reply->property << " isn't registered." << std::endl;
255 AsyncPropertyReply *reply = routingEngine->setProperty(request);
262 AMBIF::updateProperty(AMBVehicleInfo *vehicleinfo)
264 routingEngine->updateProperty(vehicleinfo->name, vehicleinfo->value,
266 AMBVehicleInfo *ambvehicleinfo = find(vehicleinfo->name);
268 if (ambvehicleinfo != NULL) {
269 delete ambvehicleinfo->value;
270 ambvehicleinfo->value = vehicleinfo->value->copy();
278 pthread_mutex_lock(&mutex);
284 pthread_mutex_unlock(&mutex);
288 AMBIF::requestUpdate(AMBVehicleInfo *vehicleinfo)
290 DebugOut() << "AMBIF requestUpdate request property name is "
291 << vehicleinfo->name << "\n";
292 if (find(vehicleinfo->name) != NULL) {
293 updateProperty(vehicleinfo);
296 setPropertyRequest(vehicleinfo);
301 AMBIF::registVehicleInfo(std::string propertyName, DataType type, string value)
303 DebugOut() << "AMBIF registVehicleInfo(" << propertyName << ")\n";
304 VehicleProperty::PropertyTypeFactoryCallback factory;
308 factory = [value, propertyName]() {
309 return new BasicPropertyType<int>(propertyName, value);
315 factory = [value, propertyName]() {
316 return new BasicPropertyType<double>(propertyName, value);
322 factory = [value, propertyName]() {
323 return new BasicPropertyType<char>(propertyName, value);
329 factory = [value, propertyName]() {
330 return new BasicPropertyType<int16_t>(propertyName, value);
336 factory = [value, propertyName]() {
337 return new BasicPropertyType<uint16_t>(propertyName, value);
343 factory = [value, propertyName]() {
344 return new BasicPropertyType<uint32_t>(propertyName, value);
350 factory = [value, propertyName]() {
351 return new BasicPropertyType<int64_t>(propertyName, value);
357 factory = [value, propertyName]() {
358 return new BasicPropertyType<uint64_t>(propertyName, value);
364 factory = [value, propertyName]() {
365 return new BasicPropertyType<bool>(propertyName, value);
374 return VehicleProperty::registerProperty(propertyName, factory);
378 AMBIF::find(std::string propertyName)
380 for (auto itr = vehicleinfoArray.begin(); itr != vehicleinfoArray.end();
382 if ((*itr).name == propertyName) {