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"
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 Config *conf = new Config();
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, Config *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 = "
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;
246 request.completed = [](AsyncPropertyReply *reply) {
247 if (reply->success) {
248 DebugOut()<<"AMBIF" << reply->property << ":" << reply->value->toString() << std::endl;
251 DebugOut()<<"AMBIF" << reply->property << " isn't registered." << std::endl;
254 AsyncPropertyReply *reply = routingEngine->setProperty(request);
261 AMBIF::updateProperty(AMBVehicleInfo *vehicleinfo)
263 routingEngine->updateProperty(vehicleinfo->name, vehicleinfo->value,
265 AMBVehicleInfo *ambvehicleinfo = find(vehicleinfo->name);
267 if (ambvehicleinfo != NULL) {
268 delete ambvehicleinfo->value;
269 ambvehicleinfo->value = vehicleinfo->value->copy();
277 pthread_mutex_lock(&mutex);
283 pthread_mutex_unlock(&mutex);
287 AMBIF::requestUpdate(AMBVehicleInfo *vehicleinfo)
289 DebugOut() << "AMBIF requestUpdate request property name is "
290 << vehicleinfo->name << "\n";
291 if (find(vehicleinfo->name) != NULL) {
292 updateProperty(vehicleinfo);
295 setPropertyRequest(vehicleinfo);
300 AMBIF::registVehicleInfo(std::string propertyName,
301 VehicleInfoDefine::Status::DataType type, string value)
303 DebugOut() << "AMBIF registVehicleInfo(" << propertyName
305 VehicleProperty::PropertyTypeFactoryCallback factory;
307 case VehicleInfoDefine::Status::INT:
309 factory = [value]() {
310 return new BasicPropertyType<int>(value);
314 case VehicleInfoDefine::Status::DOUBLE:
316 factory = [value]() {
317 return new BasicPropertyType<double>(value);
321 case VehicleInfoDefine::Status::CHAR:
323 factory = [value]() {
324 return new BasicPropertyType<char>(value);
328 case VehicleInfoDefine::Status::INT16:
330 factory = [value]() {
331 return new BasicPropertyType<int16_t>(value);
335 case VehicleInfoDefine::Status::UINT16:
337 factory = [value]() {
338 return new BasicPropertyType<uint16_t>(value);
342 case VehicleInfoDefine::Status::UINT32:
344 factory = [value]() {
345 return new BasicPropertyType<uint32_t>(value);
349 case VehicleInfoDefine::Status::INT64:
351 factory = [value]() {
352 return new BasicPropertyType<int64_t>(value);
356 case VehicleInfoDefine::Status::UINT64:
358 factory = [value]() {
359 return new BasicPropertyType<uint64_t>(value);
363 case VehicleInfoDefine::Status::BOOL:
365 factory = [value]() {
366 return new BasicPropertyType<bool>(value);
375 return VehicleProperty::registerProperty(propertyName, factory);
379 AMBIF::find(std::string propertyName)
381 for (auto itr = vehicleinfoArray.begin(); itr != vehicleinfoArray.end();
383 if ((*itr).name == propertyName) {