23a39e2f12757cdd7578a31aaed7ee5123accb21
[apps/agl-service-windowmanager.git] / src / low_can_client.cpp
1 /*
2  * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include "json_helper.hpp"
19 #include "low_can_client.hpp"
20 #include "hmi-debug.h"
21
22 extern "C" {
23 #include <afb/afb-binding.h>
24 }
25
26
27 namespace wm {
28
29 LowCanClient::LowCanClient() :
30   vehicle_speed_(0),
31   trans_gear_pos_(0),
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)
40 {
41     HMI_DEBUG("wm:lcc", "Call");
42 }
43
44 void LowCanClient::initialize() {
45     HMI_DEBUG("wm:lcc", "Call");
46
47     int ret;
48
49     // Require API "low-can"
50     ret = afb_daemon_require_api_v2("low-can", 1);
51     if (0 > ret) {
52         HMI_INFO("wm:lcc", "Requirement API \"low-can\" failed");
53         return;
54     }
55
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));
65
66         json_object *json_result = json_object_new_object();
67         ret = afb_service_call_sync("low-can", "subscribe", json_obj, &json_result);
68         if (0 > ret) {
69             HMI_INFO("wm:lcc", "Could not subscribe to \"low-can\" :%d", ret);
70             return;
71         }
72         HMI_DEBUG("wm:lcc", "subscribe result:%s", json_object_get_string(json_result));
73     }
74
75     return;
76 }
77
78 void LowCanClient::analyzeCanSignal(struct json_object *object) {
79     HMI_DEBUG("wm:lcc", "object:%s", json_object_get_string(object));
80
81     const char* name = jh::getStringFromJson(object, "name");
82     HMI_DEBUG("wm:lcc", "CAN signal name:%s", name);
83
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_);
91         }
92     }
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_);
100         }
101     }
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_);
109         }
110     }
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_);
118         }
119     }
120
121     // Update car state
122     if ((0 == this->vehicle_speed_) || (true == this->park_brake_status_)) {
123         this->crr_car_state_ = "car_stop";
124     }
125     else {
126         this->crr_car_state_ = "car_run";
127     }
128     HMI_DEBUG("wm:lcc", "Current car state:%s", this->crr_car_state_.c_str());
129
130     // Update lamp state
131     if (true == this->headlamp_status_) {
132         this->crr_lamp_state_ = "lamp_on";
133     }
134     else {
135         this->crr_lamp_state_ = "lamp_off";
136     }
137     HMI_DEBUG("wm:lcc", "Current lamp state:%s", this->crr_lamp_state_.c_str());
138
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;
146     }
147
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;
155     }
156 }
157
158 bool LowCanClient::isChangedCarState() {
159     HMI_DEBUG("wm:lcc", "Call");
160
161     // Return changed flag
162     return this->is_changed_car_state_;
163 }
164
165 bool LowCanClient::isChangedLampState() {
166     HMI_DEBUG("wm:lcc", "Call");
167
168     // Return changed flag
169     return this->is_changed_lamp_state_;
170 }
171
172 const char* LowCanClient::getCurrentCarState() {
173     HMI_DEBUG("wm:lcc", "Call");
174
175     // Clear changed flag
176     this->is_changed_car_state_ = false;
177
178     // Return current car state
179     return this->crr_car_state_.c_str();
180 }
181
182 const char* LowCanClient::getCurrentLampState() {
183     HMI_DEBUG("wm:lcc", "Call");
184
185     // Clear changed flag
186     this->is_changed_lamp_state_ = false;
187
188     // Return current lamp state
189     return this->crr_lamp_state_.c_str();
190 }
191
192
193 } // namespace wm