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