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 "json_helper.hpp"
19 #include "low_can_client.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_car_state_("car_stop"),
35 crr_car_state_("car_stop"),
36 prv_lamp_state_("lamp_off"),
37 crr_lamp_state_("lamp_off"),
38 is_changed_car_state_(false),
39 is_changed_lamp_state_(false)
41 HMI_DEBUG("wm:lcc", "Call");
44 void LowCanClient::initialize() {
45 HMI_DEBUG("wm:lcc", "Call");
49 // Require API "low-can"
50 ret = afb_daemon_require_api_v2("low-can", 1);
52 HMI_INFO("wm:lcc", "Requirement API \"low-can\" failed");
56 // Subscribe low-level-can
57 // low-can subscribe { "event": "vehicle.speed" }
58 // low-can subscribe { "event": "transmission_gear_position" }
59 // low-can subscribe { "event": "headlamp_status" }
60 // low-can subscribe { "event": "parking_brake_status" }
61 for (int i=0; i<this->kNumEvent_; i++) {
62 json_object *json_obj = json_object_new_object();
63 json_object_object_add(json_obj, "event", json_object_new_string(this->kEventName_[i]));
64 HMI_DEBUG("wm:lcc", "subscribe message:%s", json_object_get_string(json_obj));
66 json_object *json_result = json_object_new_object();
67 ret = afb_service_call_sync("low-can", "subscribe", json_obj, &json_result);
69 HMI_INFO("wm:lcc", "Could not subscribe to \"low-can\" :%d", ret);
72 HMI_DEBUG("wm:lcc", "subscribe result:%s", json_object_get_string(json_result));
78 void LowCanClient::analyzeCanSignal(struct json_object *object) {
79 HMI_DEBUG("wm:lcc", "object:%s", json_object_get_string(object));
81 const char* name = jh::getStringFromJson(object, "name");
82 HMI_DEBUG("wm:lcc", "CAN signal name:%s", name);
84 if (strstr(name, "vehicle.speed")) {
85 HMI_DEBUG("wm:lcc", "Receive vehicle speed");
86 // Update vehicle speed
87 int speed = jh::getIntFromJson(object, "value");
88 if (this->vehicle_speed_ != speed) {
89 this->vehicle_speed_ = speed;
90 HMI_DEBUG("wm:lcc", "Update vehicle speed:%d", this->vehicle_speed_);
93 else if (strstr(name, "transmission_gear_position")) {
94 HMI_DEBUG("wm:lcc", "Receive transmission gear position");
95 // Update transmission gear position
96 int gear_pos = jh::getIntFromJson(object, "value");
97 if (this->trans_gear_pos_ != gear_pos) {
98 this->trans_gear_pos_ = gear_pos;
99 HMI_DEBUG("wm:lcc", "Update transmission gear position:%d", this->trans_gear_pos_);
102 else if (strstr(name, "parking_brake_status")) {
103 HMI_DEBUG("wm:lcc", "Receive parking brake status");
104 // Update parking gear status
105 json_bool parking_brake = jh::getBoolFromJson(object, "value");
106 if (this->parking_brake_status_ != parking_brake) {
107 this->parking_brake_status_ = parking_brake;
108 HMI_DEBUG("wm:lcc", "Update parking brake status:%d", this->parking_brake_status_);
111 else if (strstr(name, "headlamp_status")) {
112 HMI_DEBUG("wm:lcc", "Receive headlamp status");
113 // Update headlamp status
114 json_bool headlamp = jh::getBoolFromJson(object, "value");
115 if (this->headlamp_status_ != headlamp) {
116 this->headlamp_status_ = headlamp;
117 HMI_DEBUG("wm:lcc", "Update headlamp status:%d", this->headlamp_status_);
121 // Update parking brake state
122 if (this->parking_brake_status_) {
123 this->crr_parking_brake_state_ = "parking_brake_on";
126 this->crr_parking_brake_state_ = "parking_brake_off";
128 HMI_DEBUG("wm:lcc", "Current parking brake state:%s", this->crr_parking_brake_state_.c_str());
131 if ((0 == this->vehicle_speed_) || (true == this->parking_brake_status_)) {
132 this->crr_car_state_ = "car_stop";
135 this->crr_car_state_ = "car_run";
137 HMI_DEBUG("wm:lcc", "Current car state:%s", this->crr_car_state_.c_str());
140 if (true == this->headlamp_status_) {
141 this->crr_lamp_state_ = "lamp_on";
144 this->crr_lamp_state_ = "lamp_off";
146 HMI_DEBUG("wm:lcc", "Current lamp state:%s", this->crr_lamp_state_.c_str());
148 // If parking brake state is changed,
149 // backup current state for previous state and set flag
150 if (this->prv_parking_brake_state_ != this->crr_parking_brake_state_) {
151 HMI_DEBUG("wm:lcc", "Parking Brake state is changed: %s -> %s",
152 this->prv_parking_brake_state_.c_str(), this->crr_parking_brake_state_.c_str());
153 this->prv_parking_brake_state_ = this->crr_parking_brake_state_;
154 this->is_changed_parking_brake_state_ = true;
157 // If car state is changed,
158 // backup current state for previous state and set flag
159 if (this->prv_car_state_ != this->crr_car_state_) {
160 HMI_DEBUG("wm:lcc", "Car state is changed: %s -> %s",
161 this->prv_car_state_.c_str(), this->crr_car_state_.c_str());
162 this->prv_car_state_ = this->crr_car_state_;
163 this->is_changed_car_state_ = true;
166 // If lamp state is changed,
167 // backup current state for previous state and set flag
168 if (this->prv_lamp_state_ != this->crr_lamp_state_) {
169 HMI_DEBUG("wm:lcc", "Lamp state is changed: %s -> %s",
170 this->prv_lamp_state_.c_str(), this->crr_lamp_state_.c_str());
171 this->prv_lamp_state_ = this->crr_lamp_state_;
172 this->is_changed_lamp_state_ = true;
176 bool LowCanClient::isChangedParkingBrakeState() {
177 HMI_DEBUG("wm:lcc", "Call");
179 // Return changed flag
180 return this->is_changed_parking_brake_state_;
183 bool LowCanClient::isChangedCarState() {
184 HMI_DEBUG("wm:lcc", "Call");
186 // Return changed flag
187 return this->is_changed_car_state_;
190 bool LowCanClient::isChangedLampState() {
191 HMI_DEBUG("wm:lcc", "Call");
193 // Return changed flag
194 return this->is_changed_lamp_state_;
197 const char* LowCanClient::getCurrentParkingBrakeState() {
198 HMI_DEBUG("wm:lcc", "Call");
200 // Clear changed flag
201 this->is_changed_parking_brake_state_ = false;
203 // Return current parking brake state
204 return this->crr_parking_brake_state_.c_str();
207 const char* LowCanClient::getCurrentCarState() {
208 HMI_DEBUG("wm:lcc", "Call");
210 // Clear changed flag
211 this->is_changed_car_state_ = false;
213 // Return current car state
214 return this->crr_car_state_.c_str();
217 const char* LowCanClient::getCurrentLampState() {
218 HMI_DEBUG("wm:lcc", "Call");
220 // Clear changed flag
221 this->is_changed_lamp_state_ = false;
223 // Return current lamp state
224 return this->crr_lamp_state_.c_str();