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 park_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 park_brake = jh::getBoolFromJson(object, "value");
106 if (this->park_brake_status_ != park_brake) {
107 this->park_brake_status_ = park_brake;
108 HMI_DEBUG("wm:lcc", "Update parking brake status:%d", this->park_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_);
122 if ((0 == this->vehicle_speed_) || (true == this->park_brake_status_)) {
123 this->crr_car_state_ = "car_stop";
126 this->crr_car_state_ = "car_run";
128 HMI_DEBUG("wm:lcc", "Current car state:%s", this->crr_car_state_.c_str());
131 if (true == this->headlamp_status_) {
132 this->crr_lamp_state_ = "lamp_on";
135 this->crr_lamp_state_ = "lamp_off";
137 HMI_DEBUG("wm:lcc", "Current lamp state:%s", this->crr_lamp_state_.c_str());
139 // If car state is changed,
140 // backup current state for previous state and set flag
141 if (this->prv_car_state_ != this->crr_car_state_) {
142 HMI_DEBUG("wm:lcc", "Car state is changed: %s -> %s",
143 this->prv_car_state_.c_str(), this->crr_car_state_.c_str());
144 this->prv_car_state_ = this->crr_car_state_;
145 this->is_changed_car_state_ = true;
148 // If lamp state is changed,
149 // backup current state for previous state and set flag
150 if (this->prv_lamp_state_ != this->crr_lamp_state_) {
151 HMI_DEBUG("wm:lcc", "Lamp state is changed: %s -> %s",
152 this->prv_lamp_state_.c_str(), this->crr_lamp_state_.c_str());
153 this->prv_lamp_state_ = this->crr_lamp_state_;
154 this->is_changed_lamp_state_ = true;
158 bool LowCanClient::isChangedCarState() {
159 HMI_DEBUG("wm:lcc", "Call");
161 // Return changed flag
162 return this->is_changed_car_state_;
165 bool LowCanClient::isChangedLampState() {
166 HMI_DEBUG("wm:lcc", "Call");
168 // Return changed flag
169 return this->is_changed_lamp_state_;
172 const char* LowCanClient::getCurrentCarState() {
173 HMI_DEBUG("wm:lcc", "Call");
175 // Clear changed flag
176 this->is_changed_car_state_ = false;
178 // Return current car state
179 return this->crr_car_state_.c_str();
182 const char* LowCanClient::getCurrentLampState() {
183 HMI_DEBUG("wm:lcc", "Call");
185 // Clear changed flag
186 this->is_changed_lamp_state_ = false;
188 // Return current lamp state
189 return this->crr_lamp_state_.c_str();