Bug fix: Always changed flag becomes true when restriction mode is changed
[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   accel_pedal_stt(FALSE),
36   lightstatus_brake_status(TRUE),
37   is_changed_accel_pedal_stt(false)
38 {
39     HMI_DEBUG("wm:lcc", "Call");
40 }
41
42 void LowCanClient::initialize() {
43     HMI_DEBUG("wm:lcc", "Call");
44
45     int ret;
46
47     // Require API "low-can"
48     ret = afb_daemon_require_api_v2("low-can", 1);
49     if (0 > ret) {
50         HMI_INFO("wm:lcc", "Requirement API \"low-can\" failed");
51         return;
52     }
53
54     // Subscribe low-level-can
55     // low-can subscribe { "event": "vehicle.speed" }
56     // low-can subscribe { "event": "transmission_gear_position" }
57     // low-can subscribe { "event": "headlamp_status" }
58     // low-can subscribe { "event": "parking_brake_status" }
59     // low-can subscribe { "event": "accelerator.pedal.position" }
60     // low-can subscribe { "event": "lightstatus.brake" }
61     for (int i=SignalNoMin; i<=SignalNoMax; i++) {
62         // Set Event
63         json_object *json_obj = json_object_new_object();
64         json_object_object_add(json_obj, "event",
65                                json_object_new_string(this->kSignalName[i]));
66
67         // Set filter
68         if (0 != strcmp("", this->kFilterValue[i])) {
69             json_object_object_add(json_obj, "filter",
70                                    json_tokener_parse(this->kFilterValue[i]));
71         }
72         HMI_DEBUG("wm:lcc", "subscribe message:%s", json_object_get_string(json_obj));
73
74         // Subscribe
75         afb_service_call("low-can", "subscribe", json_obj,
76                                [](void* closure, int status, json_object *result){
77                 HMI_DEBUG("wm:lcc", "subscribe result:%s", json_object_get_string(result));
78             },
79             nullptr);
80     }
81
82     return;
83 }
84
85 const char* LowCanClient::analyzeCanSignal(struct json_object *object) {
86     HMI_DEBUG("wm:lcc", "object:%s", json_object_get_string(object));
87
88     const char* name = jh::getStringFromJson(object, "name");
89     HMI_DEBUG("wm:lcc", "CAN signal name:%s", name);
90
91     if (strstr(name, this->kSignalName[SignalNoVehicliSpeed])) {
92         // Update vehicle speed
93         this->vehicle_speed = jh::getIntFromJson(object, "value");
94         HMI_DEBUG("wm:lcc", "Update vehicle speed:%d", this->vehicle_speed);
95     }
96     else if (strstr(name, this->kSignalName[SignalNoTransGearPos])) {
97         // Update transmission gear position
98         this->trans_gear_pos = jh::getIntFromJson(object, "value");
99         HMI_DEBUG("wm:lcc", "Update transmission gear position:%d", this->trans_gear_pos);
100     }
101     else if (strstr(name, this->kSignalName[SignalNoHeadlame])) {
102         // Update headlamp status
103         this->headlamp_status = jh::getBoolFromJson(object, "value");
104         HMI_DEBUG("wm:lcc", "Update headlamp status:%d", this->headlamp_status);
105     }
106     else if (strstr(name, this->kSignalName[SignalNoParkingBrake])) {
107         // Update parking gear status
108         this->parking_brake_status = jh::getBoolFromJson(object, "value");
109         HMI_DEBUG("wm:lcc", "Update parking brake status:%d", this->parking_brake_status);
110     }
111     else if (strstr(name, this->kSignalName[SignalNoAccelPedalPos])) {
112         // Clear flag for whether accel pedal state is changed
113         this->is_changed_accel_pedal_stt = false;
114
115         // Update accelerator pedal status
116         this->accel_pedal_pos = jh::getDoubleFromJson(object, "value");
117         HMI_DEBUG("wm:lcc", "Update accelerator pedal position:%lf", this->accel_pedal_pos);
118
119         bool accel_pedal_stt;
120         if (0 != this->accel_pedal_pos) {
121             accel_pedal_stt = true;
122         }
123         else {
124             accel_pedal_stt = false;
125         }
126
127         if (accel_pedal_stt != this->accel_pedal_stt) {
128             this->is_changed_accel_pedal_stt = true;
129             this->accel_pedal_stt = accel_pedal_stt;
130         }
131     }
132     else if (strstr(name, this->kSignalName[SignalNoLightstatusBrake])) {
133         // Update lightstatus brake status
134         this->lightstatus_brake_status = jh::getBoolFromJson(object, "value");
135         HMI_DEBUG("wm:lcc", "Update lightstatus brake status:%d", this->lightstatus_brake_status);
136     }
137
138     return name;
139 }
140
141 bool LowCanClient::isChangedAccelPedalState() {
142     HMI_DEBUG("wm:lcc", "Call");
143
144     return this->is_changed_accel_pedal_stt;
145 }
146
147 int LowCanClient::getCurrentTransGearState() {
148     HMI_DEBUG("wm:lcc", "Call");
149
150     return this->trans_gear_pos;
151 }
152
153 bool LowCanClient::getCurrentHeadlampState() {
154     HMI_DEBUG("wm:lcc", "Call");
155
156     return (bool)this->headlamp_status;
157 }
158
159 bool LowCanClient::getCurrentParkingBrakeState() {
160     HMI_DEBUG("wm:lcc", "Call");
161
162     return (bool)this->parking_brake_status;
163 }
164
165 double LowCanClient::getCurrentAccelPedalPosition() {
166     HMI_DEBUG("wm:lcc", "Call");
167
168     return this->accel_pedal_pos;
169 }
170
171 bool LowCanClient::getCurrentAccelPedalState() {
172     HMI_DEBUG("wm:lcc", "Call");
173
174     return this->accel_pedal_stt;
175 }
176
177 bool LowCanClient::getCurrentLightstatusBrakeState() {
178     HMI_DEBUG("wm:lcc", "Call");
179
180     return (bool)this->lightstatus_brake_status;
181 }
182
183
184 } // namespace wm