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 headlamp_status_(FALSE),
33 parking_brake_status_(TRUE),
35 prv_lamp_state_("lamp_off"),
36 crr_lamp_state_("lamp_off"),
37 prv_parking_brake_state_("parking_brake_on"),
38 crr_parking_brake_state_("parking_brake_on"),
39 prv_accel_pedal_state_("accel_pedal_off"),
40 crr_accel_pedal_state_("accel_pedal_off"),
41 prv_car_state_("car_stop"),
42 crr_car_state_("car_stop"),
43 is_changed_lamp_state_(false),
44 is_changed_parking_brake_state_(false),
45 is_changed_accel_pedal_state_(false),
46 is_changed_car_state_(false)
48 HMI_DEBUG("wm:lcc", "Call");
51 void LowCanClient::initialize() {
52 HMI_DEBUG("wm:lcc", "Call");
56 // Require API "low-can"
57 ret = afb_daemon_require_api_v2("low-can", 1);
59 HMI_INFO("wm:lcc", "Requirement API \"low-can\" failed");
63 // Subscribe low-level-can
64 // low-can subscribe { "event": "vehicle.speed" }
65 // low-can subscribe { "event": "transmission_gear_position" }
66 // low-can subscribe { "event": "headlamp_status" }
67 // low-can subscribe { "event": "parking_brake_status" }
68 // low-can subscribe { "event": "accelerator.pedal.position" }
69 for (int i=SignalNoMin; i<=SignalNoMax; i++) {
71 json_object *json_obj = json_object_new_object();
72 json_object_object_add(json_obj, "event",
73 json_object_new_string(this->kEventName_[i]));
76 if (0 != strcmp("", this->kFilterValue_[i])) {
77 json_object_object_add(json_obj, "filter",
78 json_tokener_parse(this->kFilterValue_[i]));
80 HMI_DEBUG("wm:lcc", "subscribe message:%s", json_object_get_string(json_obj));
83 json_object *json_result = json_object_new_object();
84 ret = afb_service_call_sync("low-can", "subscribe", json_obj, &json_result);
86 HMI_INFO("wm:lcc", "Could not subscribe to \"low-can\" :%d", ret);
88 HMI_DEBUG("wm:lcc", "subscribe result:%s", json_object_get_string(json_result));
94 void LowCanClient::analyzeCanSignal(struct json_object *object) {
95 HMI_DEBUG("wm:lcc", "object:%s", json_object_get_string(object));
97 const char* name = jh::getStringFromJson(object, "name");
98 HMI_DEBUG("wm:lcc", "CAN signal name:%s", name);
100 if (strstr(name, this->kEventName_[0])) {
101 HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[0]);
102 // Update vehicle speed
103 int speed = jh::getIntFromJson(object, "value");
104 if (this->vehicle_speed_ != speed) {
105 this->vehicle_speed_ = speed;
106 HMI_DEBUG("wm:lcc", "Update vehicle speed:%d", this->vehicle_speed_);
109 else if (strstr(name, this->kEventName_[1])) {
110 HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[1]);
111 // Update transmission gear position
112 int gear_pos = jh::getIntFromJson(object, "value");
113 if (this->trans_gear_pos_ != gear_pos) {
114 this->trans_gear_pos_ = gear_pos;
115 HMI_DEBUG("wm:lcc", "Update transmission gear position:%d", this->trans_gear_pos_);
118 else if (strstr(name, this->kEventName_[2])) {
119 HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[2]);
120 // Update headlamp status
121 json_bool headlamp = jh::getBoolFromJson(object, "value");
122 if (this->headlamp_status_ != headlamp) {
123 this->headlamp_status_ = headlamp;
124 HMI_DEBUG("wm:lcc", "Update headlamp status:%d", this->headlamp_status_);
127 else if (strstr(name, this->kEventName_[3])) {
128 HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[3]);
129 // Update parking gear status
130 json_bool parking_brake = jh::getBoolFromJson(object, "value");
131 if (this->parking_brake_status_ != parking_brake) {
132 this->parking_brake_status_ = parking_brake;
133 HMI_DEBUG("wm:lcc", "Update parking brake status:%d", this->parking_brake_status_);
136 else if (strstr(name, this->kEventName_[4])) {
137 HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[4]);
138 // Update accelerator pedal status
139 double accel_pedal_pos = jh::getDoubleFromJson(object, "value");
140 if (this->accel_pedal_pos_ != accel_pedal_pos) {
141 this->accel_pedal_pos_ = accel_pedal_pos;
142 HMI_DEBUG("wm:lcc", "Update accelerator pedal status:%lf", this->accel_pedal_pos_);
147 if (true == this->headlamp_status_) {
148 this->crr_lamp_state_ = "lamp_on";
151 this->crr_lamp_state_ = "lamp_off";
153 HMI_DEBUG("wm:lcc", "Current lamp state:%s", this->crr_lamp_state_.c_str());
155 // Update parking brake state
156 if (this->parking_brake_status_) {
157 this->crr_parking_brake_state_ = "parking_brake_on";
160 this->crr_parking_brake_state_ = "parking_brake_off";
162 HMI_DEBUG("wm:lcc", "Current parking brake state:%s", this->crr_parking_brake_state_.c_str());
164 // Update accelerator pedal state
165 if (0 == this->accel_pedal_pos_) {
166 this->crr_accel_pedal_state_ = "accel_pedal_off";
169 this->crr_accel_pedal_state_ = "accel_pedal_on";
171 HMI_DEBUG("wm:lcc", "Current accelerator pedal state:%s", this->crr_accel_pedal_state_.c_str());
174 if ((0 == this->vehicle_speed_) || (true == this->parking_brake_status_)) {
175 this->crr_car_state_ = "car_stop";
178 this->crr_car_state_ = "car_run";
180 HMI_DEBUG("wm:lcc", "Current car state:%s", this->crr_car_state_.c_str());
182 // If lamp state is changed,
183 // backup current state for previous state and set flag
184 if (this->prv_lamp_state_ != this->crr_lamp_state_) {
185 HMI_DEBUG("wm:lcc", "Lamp state is changed: %s -> %s",
186 this->prv_lamp_state_.c_str(), this->crr_lamp_state_.c_str());
187 this->prv_lamp_state_ = this->crr_lamp_state_;
188 this->is_changed_lamp_state_ = true;
191 // If parking brake state is changed,
192 // backup current state for previous state and set flag
193 if (this->prv_parking_brake_state_ != this->crr_parking_brake_state_) {
194 HMI_DEBUG("wm:lcc", "Parking Brake state is changed: %s -> %s",
195 this->prv_parking_brake_state_.c_str(), this->crr_parking_brake_state_.c_str());
196 this->prv_parking_brake_state_ = this->crr_parking_brake_state_;
197 this->is_changed_parking_brake_state_ = true;
200 // If accelerator pedal state is changed,
201 // backup current state for previous state and set flag
202 if (this->prv_accel_pedal_state_ != this->crr_accel_pedal_state_) {
203 HMI_DEBUG("wm:lcc", "Accelerator Pedal is changed: %s -> %s",
204 this->prv_accel_pedal_state_.c_str(), this->crr_accel_pedal_state_.c_str());
205 this->prv_accel_pedal_state_ = this->crr_accel_pedal_state_;
206 this->is_changed_accel_pedal_state_ = true;
209 // If car state is changed,
210 // backup current state for previous state and set flag
211 if (this->prv_car_state_ != this->crr_car_state_) {
212 HMI_DEBUG("wm:lcc", "Car state is changed: %s -> %s",
213 this->prv_car_state_.c_str(), this->crr_car_state_.c_str());
214 this->prv_car_state_ = this->crr_car_state_;
215 this->is_changed_car_state_ = true;
219 bool LowCanClient::isChangedLampState() {
220 HMI_DEBUG("wm:lcc", "Call");
222 // Return changed flag
223 return this->is_changed_lamp_state_;
226 bool LowCanClient::isChangedParkingBrakeState() {
227 HMI_DEBUG("wm:lcc", "Call");
229 // Return changed flag
230 return this->is_changed_parking_brake_state_;
233 bool LowCanClient::isChangedAccelPedalState() {
234 HMI_DEBUG("wm:lcc", "Call");
236 // Return changed flag
237 return this->is_changed_accel_pedal_state_;
240 bool LowCanClient::isChangedCarState() {
241 HMI_DEBUG("wm:lcc", "Call");
243 // Return changed flag
244 return this->is_changed_car_state_;
247 const char* LowCanClient::getCurrentLampState() {
248 HMI_DEBUG("wm:lcc", "Call");
250 // Clear changed flag
251 this->is_changed_lamp_state_ = false;
253 // Return current lamp state
254 return this->crr_lamp_state_.c_str();
257 const char* LowCanClient::getCurrentParkingBrakeState() {
258 HMI_DEBUG("wm:lcc", "Call");
260 // Clear changed flag
261 this->is_changed_parking_brake_state_ = false;
263 // Return current parking brake state
264 return this->crr_parking_brake_state_.c_str();
267 const char* LowCanClient::getCurrentAccelPedalState() {
268 HMI_DEBUG("wm:lcc", "Call");
270 // Clear changed flag
271 this->is_changed_accel_pedal_state_ = false;
273 // Return current accelerator pedal state
274 return this->crr_accel_pedal_state_.c_str();
277 const char* LowCanClient::getCurrentCarState() {
278 HMI_DEBUG("wm:lcc", "Call");
280 // Clear changed flag
281 this->is_changed_car_state_ = false;
283 // Return current car state
284 return this->crr_car_state_.c_str();