/* * Copyright (c) 2018 TOYOTA MOTOR CORPORATION * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "low_can_client.hpp" #include "json_helper.hpp" #include "hmi-debug.h" extern "C" { #include } namespace wm { LowCanClient::LowCanClient() : vehicle_speed_(0), trans_gear_pos_(0), headlamp_status_(FALSE), parking_brake_status_(TRUE), accel_pedal_pos_(0), accel_pedal_stt_(FALSE), lightstatus_brake_status_(TRUE), is_changed_accel_pedal_stt_(false) { HMI_DEBUG("wm:lcc", "Call"); } void LowCanClient::initialize() { HMI_DEBUG("wm:lcc", "Call"); int ret; // Require API "low-can" ret = afb_daemon_require_api_v2("low-can", 1); if (0 > ret) { HMI_INFO("wm:lcc", "Requirement API \"low-can\" failed"); return; } // Subscribe low-level-can // low-can subscribe { "event": "vehicle.speed" } // low-can subscribe { "event": "transmission_gear_position" } // low-can subscribe { "event": "headlamp_status" } // low-can subscribe { "event": "parking_brake_status" } // low-can subscribe { "event": "accelerator.pedal.position" } // low-can subscribe { "event": "lightstatus.brake" } for (int i=SignalNoMin; i<=SignalNoMax; i++) { // Set Event json_object *json_obj = json_object_new_object(); json_object_object_add(json_obj, "event", json_object_new_string(this->kSignalName_[i])); // Set filter if (0 != strcmp("", this->kFilterValue_[i])) { json_object_object_add(json_obj, "filter", json_tokener_parse(this->kFilterValue_[i])); } HMI_DEBUG("wm:lcc", "subscribe message:%s", json_object_get_string(json_obj)); // Subscribe json_object *json_result = json_object_new_object(); ret = afb_service_call_sync("low-can", "subscribe", json_obj, &json_result); if (0 > ret) { HMI_INFO("wm:lcc", "Could not subscribe to \"low-can\" :%d", ret); } HMI_DEBUG("wm:lcc", "subscribe result:%s", json_object_get_string(json_result)); } return; } const char* LowCanClient::analyzeCanSignal(struct json_object *object) { HMI_DEBUG("wm:lcc", "object:%s", json_object_get_string(object)); const char* name = jh::getStringFromJson(object, "name"); HMI_DEBUG("wm:lcc", "CAN signal name:%s", name); if (strstr(name, this->kSignalName_[SignalNoVehicliSpeed])) { // Update vehicle speed this->vehicle_speed_ = jh::getIntFromJson(object, "value"); HMI_DEBUG("wm:lcc", "Update vehicle speed:%d", this->vehicle_speed_); } else if (strstr(name, this->kSignalName_[SignalNoTransGearPos])) { // Update transmission gear position this->trans_gear_pos_ = jh::getIntFromJson(object, "value"); HMI_DEBUG("wm:lcc", "Update transmission gear position:%d", this->trans_gear_pos_); } else if (strstr(name, this->kSignalName_[SignalNoHeadlame])) { // Update headlamp status this->headlamp_status_ = jh::getBoolFromJson(object, "value"); HMI_DEBUG("wm:lcc", "Update headlamp status:%d", this->headlamp_status_); } else if (strstr(name, this->kSignalName_[SignalNoParkingBrake])) { // Update parking gear status this->parking_brake_status_ = jh::getBoolFromJson(object, "value"); HMI_DEBUG("wm:lcc", "Update parking brake status:%d", this->parking_brake_status_); } else if (strstr(name, this->kSignalName_[SignalNoAccelPedalPos])) { // Clear flag for whether accel pedal state is changed this->is_changed_accel_pedal_stt_ = false; // Update accelerator pedal status this->accel_pedal_pos_ = jh::getDoubleFromJson(object, "value"); HMI_DEBUG("wm:lcc", "Update accelerator pedal position:%lf", this->accel_pedal_pos_); bool accel_pedal_stt; if (0 != this->accel_pedal_pos_) { accel_pedal_stt = true; } else { accel_pedal_stt = false; } if (accel_pedal_stt != this->accel_pedal_stt_) { this->is_changed_accel_pedal_stt_ = true; this->accel_pedal_stt_ = accel_pedal_stt; } } else if (strstr(name, this->kSignalName_[SignalNoLightstatusBrake])) { // Update lightstatus brake status this->lightstatus_brake_status_ = jh::getBoolFromJson(object, "value"); HMI_DEBUG("wm:lcc", "Update lightstatus brake status:%d", this->lightstatus_brake_status_); } return name; } bool LowCanClient::isChangedAccelPedalState() { HMI_DEBUG("wm:lcc", "Call"); return this->is_changed_accel_pedal_stt_; } int LowCanClient::getCurrentTransGearState() { HMI_DEBUG("wm:lcc", "Call"); return this->trans_gear_pos_; } bool LowCanClient::getCurrentHeadlampState() { HMI_DEBUG("wm:lcc", "Call"); return (bool)this->headlamp_status_; } bool LowCanClient::getCurrentParkingBrakeState() { HMI_DEBUG("wm:lcc", "Call"); return (bool)this->parking_brake_status_; } double LowCanClient::getCurrentAccelPedalPosition() { HMI_DEBUG("wm:lcc", "Call"); return this->accel_pedal_pos_; } bool LowCanClient::getCurrentAccelPedalState() { HMI_DEBUG("wm:lcc", "Call"); return this->accel_pedal_stt_; } bool LowCanClient::getCurrentLightstatusBrakeState() { HMI_DEBUG("wm:lcc", "Call"); return (bool)this->lightstatus_brake_status_; } } // namespace wm