Remove unnecessary header from policy_manager.hpp
[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 "low_can_client.hpp"
19 #include "json_helper.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   headlamp_status_(FALSE),
33   parking_brake_status_(TRUE),
34   accel_pedal_pos_(0),
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)
47 {
48     HMI_DEBUG("wm:lcc", "Call");
49 }
50
51 void LowCanClient::initialize() {
52     HMI_DEBUG("wm:lcc", "Call");
53
54     int ret;
55
56     // Require API "low-can"
57     ret = afb_daemon_require_api_v2("low-can", 1);
58     if (0 > ret) {
59         HMI_INFO("wm:lcc", "Requirement API \"low-can\" failed");
60         return;
61     }
62
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++) {
70         // Set Event
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]));
74
75         // Set filter
76         if (0 != strcmp("", this->kFilterValue_[i])) {
77             json_object_object_add(json_obj, "filter",
78                                    json_tokener_parse(this->kFilterValue_[i]));
79         }
80         HMI_DEBUG("wm:lcc", "subscribe message:%s", json_object_get_string(json_obj));
81
82         // Subscribe
83         json_object *json_result = json_object_new_object();
84         ret = afb_service_call_sync("low-can", "subscribe", json_obj, &json_result);
85         if (0 > ret) {
86             HMI_INFO("wm:lcc", "Could not subscribe to \"low-can\" :%d", ret);
87         }
88         HMI_DEBUG("wm:lcc", "subscribe result:%s", json_object_get_string(json_result));
89     }
90
91     return;
92 }
93
94 void LowCanClient::analyzeCanSignal(struct json_object *object) {
95     HMI_DEBUG("wm:lcc", "object:%s", json_object_get_string(object));
96
97     const char* name = jh::getStringFromJson(object, "name");
98     HMI_DEBUG("wm:lcc", "CAN signal name:%s", name);
99
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_);
107         }
108     }
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_);
116         }
117     }
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_);
125         }
126     }
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_);
134         }
135     }
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_);
143         }
144     }
145
146     // Update lamp state
147     if (true == this->headlamp_status_) {
148         this->crr_lamp_state_ = "lamp_on";
149     }
150     else {
151         this->crr_lamp_state_ = "lamp_off";
152     }
153     HMI_DEBUG("wm:lcc", "Current lamp state:%s", this->crr_lamp_state_.c_str());
154
155     // Update parking brake state
156     if (this->parking_brake_status_) {
157         this->crr_parking_brake_state_ = "parking_brake_on";
158     }
159     else {
160         this->crr_parking_brake_state_ = "parking_brake_off";
161     }
162     HMI_DEBUG("wm:lcc", "Current parking brake state:%s", this->crr_parking_brake_state_.c_str());
163
164     // Update accelerator pedal state
165     if (0 == this->accel_pedal_pos_) {
166         this->crr_accel_pedal_state_ = "accel_pedal_off";
167     }
168     else {
169         this->crr_accel_pedal_state_ = "accel_pedal_on";
170     }
171     HMI_DEBUG("wm:lcc", "Current accelerator pedal state:%s", this->crr_accel_pedal_state_.c_str());
172
173     // Update car state
174     if ((0 == this->vehicle_speed_) || (true == this->parking_brake_status_)) {
175         this->crr_car_state_ = "car_stop";
176     }
177     else {
178         this->crr_car_state_ = "car_run";
179     }
180     HMI_DEBUG("wm:lcc", "Current car state:%s", this->crr_car_state_.c_str());
181
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;
189     }
190
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;
198     }
199
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;
207     }
208
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;
216     }
217 }
218
219 bool LowCanClient::isChangedLampState() {
220     HMI_DEBUG("wm:lcc", "Call");
221
222     // Return changed flag
223     return this->is_changed_lamp_state_;
224 }
225
226 bool LowCanClient::isChangedParkingBrakeState() {
227     HMI_DEBUG("wm:lcc", "Call");
228
229     // Return changed flag
230     return this->is_changed_parking_brake_state_;
231 }
232
233 bool LowCanClient::isChangedAccelPedalState() {
234     HMI_DEBUG("wm:lcc", "Call");
235
236     // Return changed flag
237     return this->is_changed_accel_pedal_state_;
238 }
239
240 bool LowCanClient::isChangedCarState() {
241     HMI_DEBUG("wm:lcc", "Call");
242
243     // Return changed flag
244     return this->is_changed_car_state_;
245 }
246
247 const char* LowCanClient::getCurrentLampState() {
248     HMI_DEBUG("wm:lcc", "Call");
249
250     // Clear changed flag
251     this->is_changed_lamp_state_ = false;
252
253     // Return current lamp state
254     return this->crr_lamp_state_.c_str();
255 }
256
257 const char* LowCanClient::getCurrentParkingBrakeState() {
258     HMI_DEBUG("wm:lcc", "Call");
259
260     // Clear changed flag
261     this->is_changed_parking_brake_state_ = false;
262
263     // Return current parking brake state
264     return this->crr_parking_brake_state_.c_str();
265 }
266
267 const char* LowCanClient::getCurrentAccelPedalState() {
268     HMI_DEBUG("wm:lcc", "Call");
269
270     // Clear changed flag
271     this->is_changed_accel_pedal_state_ = false;
272
273     // Return current accelerator pedal state
274     return this->crr_accel_pedal_state_.c_str();
275 }
276
277 const char* LowCanClient::getCurrentCarState() {
278     HMI_DEBUG("wm:lcc", "Call");
279
280     // Clear changed flag
281     this->is_changed_car_state_ = false;
282
283     // Return current car state
284     return this->crr_car_state_.c_str();
285 }
286
287
288 } // namespace wm