2 * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "low_can_client.hpp"
19 #include "json_helper.hpp"
20 #include "hmi-debug.h"
23 #include <afb/afb-binding.h>
29 LowCanClient::LowCanClient() :
32 parking_brake_status_(TRUE),
33 headlamp_status_(FALSE),
34 prv_parking_brake_state_("parking_brake_on"),
35 crr_parking_brake_state_("parking_brake_on"),
36 prv_car_state_("car_stop"),
37 crr_car_state_("car_stop"),
38 prv_lamp_state_("lamp_off"),
39 crr_lamp_state_("lamp_off"),
40 is_changed_parking_brake_state_(false),
41 is_changed_car_state_(false),
42 is_changed_lamp_state_(false)
44 HMI_DEBUG("wm:lcc", "Call");
47 void LowCanClient::initialize() {
48 HMI_DEBUG("wm:lcc", "Call");
52 // Require API "low-can"
53 ret = afb_daemon_require_api_v2("low-can", 1);
55 HMI_INFO("wm:lcc", "Requirement API \"low-can\" failed");
59 // Subscribe low-level-can
60 // low-can subscribe { "event": "vehicle.speed" }
61 // low-can subscribe { "event": "transmission_gear_position" }
62 // low-can subscribe { "event": "headlamp_status" }
63 // low-can subscribe { "event": "parking_brake_status" }
64 for (int i=0; i<this->kNumEvent_; i++) {
65 json_object *json_obj = json_object_new_object();
66 json_object_object_add(json_obj, "event", json_object_new_string(this->kEventName_[i]));
67 HMI_DEBUG("wm:lcc", "subscribe message:%s", json_object_get_string(json_obj));
69 json_object *json_result = json_object_new_object();
70 ret = afb_service_call_sync("low-can", "subscribe", json_obj, &json_result);
72 HMI_INFO("wm:lcc", "Could not subscribe to \"low-can\" :%d", ret);
75 HMI_DEBUG("wm:lcc", "subscribe result:%s", json_object_get_string(json_result));
81 void LowCanClient::analyzeCanSignal(struct json_object *object) {
82 HMI_DEBUG("wm:lcc", "object:%s", json_object_get_string(object));
84 const char* name = jh::getStringFromJson(object, "name");
85 HMI_DEBUG("wm:lcc", "CAN signal name:%s", name);
87 if (strstr(name, "vehicle.speed")) {
88 HMI_DEBUG("wm:lcc", "Receive vehicle speed");
89 // Update vehicle speed
90 int speed = jh::getIntFromJson(object, "value");
91 if (this->vehicle_speed_ != speed) {
92 this->vehicle_speed_ = speed;
93 HMI_DEBUG("wm:lcc", "Update vehicle speed:%d", this->vehicle_speed_);
96 else if (strstr(name, "transmission_gear_position")) {
97 HMI_DEBUG("wm:lcc", "Receive transmission gear position");
98 // Update transmission gear position
99 int gear_pos = jh::getIntFromJson(object, "value");
100 if (this->trans_gear_pos_ != gear_pos) {
101 this->trans_gear_pos_ = gear_pos;
102 HMI_DEBUG("wm:lcc", "Update transmission gear position:%d", this->trans_gear_pos_);
105 else if (strstr(name, "parking_brake_status")) {
106 HMI_DEBUG("wm:lcc", "Receive parking brake status");
107 // Update parking gear status
108 json_bool parking_brake = jh::getBoolFromJson(object, "value");
109 if (this->parking_brake_status_ != parking_brake) {
110 this->parking_brake_status_ = parking_brake;
111 HMI_DEBUG("wm:lcc", "Update parking brake status:%d", this->parking_brake_status_);
114 else if (strstr(name, "headlamp_status")) {
115 HMI_DEBUG("wm:lcc", "Receive headlamp status");
116 // Update headlamp status
117 json_bool headlamp = jh::getBoolFromJson(object, "value");
118 if (this->headlamp_status_ != headlamp) {
119 this->headlamp_status_ = headlamp;
120 HMI_DEBUG("wm:lcc", "Update headlamp status:%d", this->headlamp_status_);
124 // Update parking brake state
125 if (this->parking_brake_status_) {
126 this->crr_parking_brake_state_ = "parking_brake_on";
129 this->crr_parking_brake_state_ = "parking_brake_off";
131 HMI_DEBUG("wm:lcc", "Current parking brake state:%s", this->crr_parking_brake_state_.c_str());
134 if ((0 == this->vehicle_speed_) || (true == this->parking_brake_status_)) {
135 this->crr_car_state_ = "car_stop";
138 this->crr_car_state_ = "car_run";
140 HMI_DEBUG("wm:lcc", "Current car state:%s", this->crr_car_state_.c_str());
143 if (true == this->headlamp_status_) {
144 this->crr_lamp_state_ = "lamp_on";
147 this->crr_lamp_state_ = "lamp_off";
149 HMI_DEBUG("wm:lcc", "Current lamp state:%s", this->crr_lamp_state_.c_str());
151 // If parking brake state is changed,
152 // backup current state for previous state and set flag
153 if (this->prv_parking_brake_state_ != this->crr_parking_brake_state_) {
154 HMI_DEBUG("wm:lcc", "Parking Brake state is changed: %s -> %s",
155 this->prv_parking_brake_state_.c_str(), this->crr_parking_brake_state_.c_str());
156 this->prv_parking_brake_state_ = this->crr_parking_brake_state_;
157 this->is_changed_parking_brake_state_ = true;
160 // If car state is changed,
161 // backup current state for previous state and set flag
162 if (this->prv_car_state_ != this->crr_car_state_) {
163 HMI_DEBUG("wm:lcc", "Car state is changed: %s -> %s",
164 this->prv_car_state_.c_str(), this->crr_car_state_.c_str());
165 this->prv_car_state_ = this->crr_car_state_;
166 this->is_changed_car_state_ = true;
169 // If lamp state is changed,
170 // backup current state for previous state and set flag
171 if (this->prv_lamp_state_ != this->crr_lamp_state_) {
172 HMI_DEBUG("wm:lcc", "Lamp state is changed: %s -> %s",
173 this->prv_lamp_state_.c_str(), this->crr_lamp_state_.c_str());
174 this->prv_lamp_state_ = this->crr_lamp_state_;
175 this->is_changed_lamp_state_ = true;
179 bool LowCanClient::isChangedParkingBrakeState() {
180 HMI_DEBUG("wm:lcc", "Call");
182 // Return changed flag
183 return this->is_changed_parking_brake_state_;
186 bool LowCanClient::isChangedCarState() {
187 HMI_DEBUG("wm:lcc", "Call");
189 // Return changed flag
190 return this->is_changed_car_state_;
193 bool LowCanClient::isChangedLampState() {
194 HMI_DEBUG("wm:lcc", "Call");
196 // Return changed flag
197 return this->is_changed_lamp_state_;
200 const char* LowCanClient::getCurrentParkingBrakeState() {
201 HMI_DEBUG("wm:lcc", "Call");
203 // Clear changed flag
204 this->is_changed_parking_brake_state_ = false;
206 // Return current parking brake state
207 return this->crr_parking_brake_state_.c_str();
210 const char* LowCanClient::getCurrentCarState() {
211 HMI_DEBUG("wm:lcc", "Call");
213 // Clear changed flag
214 this->is_changed_car_state_ = false;
216 // Return current car state
217 return this->crr_car_state_.c_str();
220 const char* LowCanClient::getCurrentLampState() {
221 HMI_DEBUG("wm:lcc", "Call");
223 // Clear changed flag
224 this->is_changed_lamp_state_ = false;
226 // Return current lamp state
227 return this->crr_lamp_state_.c_str();