Replace json_object_from_file to inputJsonFilie
[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   parking_brake_status_(TRUE),
33   headlamp_status_(FALSE),
34   prv_parking_brake_state_("parking_brake_on"),
35   crr_parking_brake_state_("parking_brake_on"),
36   prv_car_state_("car_stop"),
37   crr_car_state_("car_stop"),
38   prv_lamp_state_("lamp_off"),
39   crr_lamp_state_("lamp_off"),
40   is_changed_parking_brake_state_(false),
41   is_changed_car_state_(false),
42   is_changed_lamp_state_(false)
43 {
44     HMI_DEBUG("wm:lcc", "Call");
45 }
46
47 void LowCanClient::initialize() {
48     HMI_DEBUG("wm:lcc", "Call");
49
50     int ret;
51
52     // Require API "low-can"
53     ret = afb_daemon_require_api_v2("low-can", 1);
54     if (0 > ret) {
55         HMI_INFO("wm:lcc", "Requirement API \"low-can\" failed");
56         return;
57     }
58
59     // Subscribe low-level-can
60     // low-can subscribe { "event": "vehicle.speed" }
61     // low-can subscribe { "event": "transmission_gear_position" }
62     // low-can subscribe { "event": "headlamp_status" }
63     // low-can subscribe { "event": "parking_brake_status" }
64     for (int i=0; i<this->kNumEvent_; i++) {
65         json_object *json_obj = json_object_new_object();
66         json_object_object_add(json_obj, "event", json_object_new_string(this->kEventName_[i]));
67         HMI_DEBUG("wm:lcc", "subscribe message:%s", json_object_get_string(json_obj));
68
69         json_object *json_result = json_object_new_object();
70         ret = afb_service_call_sync("low-can", "subscribe", json_obj, &json_result);
71         if (0 > ret) {
72             HMI_INFO("wm:lcc", "Could not subscribe to \"low-can\" :%d", ret);
73             return;
74         }
75         HMI_DEBUG("wm:lcc", "subscribe result:%s", json_object_get_string(json_result));
76     }
77
78     return;
79 }
80
81 void LowCanClient::analyzeCanSignal(struct json_object *object) {
82     HMI_DEBUG("wm:lcc", "object:%s", json_object_get_string(object));
83
84     const char* name = jh::getStringFromJson(object, "name");
85     HMI_DEBUG("wm:lcc", "CAN signal name:%s", name);
86
87     if (strstr(name, "vehicle.speed")) {
88         HMI_DEBUG("wm:lcc", "Receive vehicle speed");
89         // Update vehicle speed
90         int speed = jh::getIntFromJson(object, "value");
91         if (this->vehicle_speed_ != speed) {
92             this->vehicle_speed_ = speed;
93             HMI_DEBUG("wm:lcc", "Update vehicle speed:%d", this->vehicle_speed_);
94         }
95     }
96     else if (strstr(name, "transmission_gear_position")) {
97         HMI_DEBUG("wm:lcc", "Receive transmission gear position");
98         // Update transmission gear position
99         int gear_pos = jh::getIntFromJson(object, "value");
100         if (this->trans_gear_pos_ != gear_pos) {
101             this->trans_gear_pos_ = gear_pos;
102             HMI_DEBUG("wm:lcc", "Update transmission gear position:%d", this->trans_gear_pos_);
103         }
104     }
105     else if (strstr(name, "parking_brake_status")) {
106         HMI_DEBUG("wm:lcc", "Receive parking brake status");
107         // Update parking gear status
108         json_bool parking_brake = jh::getBoolFromJson(object, "value");
109         if (this->parking_brake_status_ != parking_brake) {
110             this->parking_brake_status_ = parking_brake;
111             HMI_DEBUG("wm:lcc", "Update parking brake status:%d", this->parking_brake_status_);
112         }
113     }
114     else if (strstr(name, "headlamp_status")) {
115         HMI_DEBUG("wm:lcc", "Receive headlamp status");
116         // Update headlamp status
117         json_bool headlamp = jh::getBoolFromJson(object, "value");
118         if (this->headlamp_status_ != headlamp) {
119             this->headlamp_status_ = headlamp;
120             HMI_DEBUG("wm:lcc", "Update headlamp status:%d", this->headlamp_status_);
121         }
122     }
123
124     // Update parking brake state
125     if (this->parking_brake_status_) {
126         this->crr_parking_brake_state_ = "parking_brake_on";
127     }
128     else {
129         this->crr_parking_brake_state_ = "parking_brake_off";
130     }
131     HMI_DEBUG("wm:lcc", "Current parking brake state:%s", this->crr_parking_brake_state_.c_str());
132
133     // Update car state
134     if ((0 == this->vehicle_speed_) || (true == this->parking_brake_status_)) {
135         this->crr_car_state_ = "car_stop";
136     }
137     else {
138         this->crr_car_state_ = "car_run";
139     }
140     HMI_DEBUG("wm:lcc", "Current car state:%s", this->crr_car_state_.c_str());
141
142     // Update lamp state
143     if (true == this->headlamp_status_) {
144         this->crr_lamp_state_ = "lamp_on";
145     }
146     else {
147         this->crr_lamp_state_ = "lamp_off";
148     }
149     HMI_DEBUG("wm:lcc", "Current lamp state:%s", this->crr_lamp_state_.c_str());
150
151     // If parking brake state is changed,
152     // backup current state for previous state and set flag
153     if (this->prv_parking_brake_state_ != this->crr_parking_brake_state_) {
154         HMI_DEBUG("wm:lcc", "Parking Brake state is changed: %s -> %s",
155                   this->prv_parking_brake_state_.c_str(), this->crr_parking_brake_state_.c_str());
156         this->prv_parking_brake_state_ = this->crr_parking_brake_state_;
157         this->is_changed_parking_brake_state_ = true;
158     }
159
160     // If car state is changed,
161     // backup current state for previous state and set flag
162     if (this->prv_car_state_ != this->crr_car_state_) {
163         HMI_DEBUG("wm:lcc", "Car state is changed: %s -> %s",
164                   this->prv_car_state_.c_str(), this->crr_car_state_.c_str());
165         this->prv_car_state_ = this->crr_car_state_;
166         this->is_changed_car_state_ = true;
167     }
168
169     // If lamp state is changed,
170     // backup current state for previous state and set flag
171     if (this->prv_lamp_state_ != this->crr_lamp_state_) {
172         HMI_DEBUG("wm:lcc", "Lamp state is changed: %s -> %s",
173                   this->prv_lamp_state_.c_str(), this->crr_lamp_state_.c_str());
174         this->prv_lamp_state_ = this->crr_lamp_state_;
175         this->is_changed_lamp_state_ = true;
176     }
177 }
178
179 bool LowCanClient::isChangedParkingBrakeState() {
180     HMI_DEBUG("wm:lcc", "Call");
181
182     // Return changed flag
183     return this->is_changed_parking_brake_state_;
184 }
185
186 bool LowCanClient::isChangedCarState() {
187     HMI_DEBUG("wm:lcc", "Call");
188
189     // Return changed flag
190     return this->is_changed_car_state_;
191 }
192
193 bool LowCanClient::isChangedLampState() {
194     HMI_DEBUG("wm:lcc", "Call");
195
196     // Return changed flag
197     return this->is_changed_lamp_state_;
198 }
199
200 const char* LowCanClient::getCurrentParkingBrakeState() {
201     HMI_DEBUG("wm:lcc", "Call");
202
203     // Clear changed flag
204     this->is_changed_parking_brake_state_ = false;
205
206     // Return current parking brake state
207     return this->crr_parking_brake_state_.c_str();
208 }
209
210 const char* LowCanClient::getCurrentCarState() {
211     HMI_DEBUG("wm:lcc", "Call");
212
213     // Clear changed flag
214     this->is_changed_car_state_ = false;
215
216     // Return current car state
217     return this->crr_car_state_.c_str();
218 }
219
220 const char* LowCanClient::getCurrentLampState() {
221     HMI_DEBUG("wm:lcc", "Call");
222
223     // Clear changed flag
224     this->is_changed_lamp_state_ = false;
225
226     // Return current lamp state
227     return this->crr_lamp_state_.c_str();
228 }
229
230
231 } // namespace wm