Use $ORIGIN for rpath
[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         json_object *json_result = json_object_new_object();
76         ret = afb_service_call_sync("low-can", "subscribe", json_obj, &json_result);
77         if (0 > ret) {
78             HMI_INFO("wm:lcc", "Could not subscribe to \"low-can\" :%d", ret);
79         }
80         HMI_DEBUG("wm:lcc", "subscribe result:%s", json_object_get_string(json_result));
81     }
82
83     return;
84 }
85
86 const char* LowCanClient::analyzeCanSignal(struct json_object *object) {
87     HMI_DEBUG("wm:lcc", "object:%s", json_object_get_string(object));
88
89     const char* name = jh::getStringFromJson(object, "name");
90     HMI_DEBUG("wm:lcc", "CAN signal name:%s", name);
91
92     if (strstr(name, this->kSignalName_[SignalNoVehicliSpeed])) {
93         // Update vehicle speed
94         this->vehicle_speed_ = jh::getIntFromJson(object, "value");
95         HMI_DEBUG("wm:lcc", "Update vehicle speed:%d", this->vehicle_speed_);
96     }
97     else if (strstr(name, this->kSignalName_[SignalNoTransGearPos])) {
98         // Update transmission gear position
99         this->trans_gear_pos_ = jh::getIntFromJson(object, "value");
100         HMI_DEBUG("wm:lcc", "Update transmission gear position:%d", this->trans_gear_pos_);
101     }
102     else if (strstr(name, this->kSignalName_[SignalNoHeadlame])) {
103         // Update headlamp status
104         this->headlamp_status_ = jh::getBoolFromJson(object, "value");
105         HMI_DEBUG("wm:lcc", "Update headlamp status:%d", this->headlamp_status_);
106     }
107     else if (strstr(name, this->kSignalName_[SignalNoParkingBrake])) {
108         // Update parking gear status
109         this->parking_brake_status_ = jh::getBoolFromJson(object, "value");
110         HMI_DEBUG("wm:lcc", "Update parking brake status:%d", this->parking_brake_status_);
111     }
112     else if (strstr(name, this->kSignalName_[SignalNoAccelPedalPos])) {
113         // Clear flag for whether accel pedal state is changed
114         this->is_changed_accel_pedal_stt_ = false;
115
116         // Update accelerator pedal status
117         this->accel_pedal_pos_ = jh::getDoubleFromJson(object, "value");
118         HMI_DEBUG("wm:lcc", "Update accelerator pedal position:%lf", this->accel_pedal_pos_);
119
120         bool accel_pedal_stt;
121         if (0 != this->accel_pedal_pos_) {
122             accel_pedal_stt = true;
123         }
124         else {
125             accel_pedal_stt = false;
126         }
127
128         if (accel_pedal_stt != this->accel_pedal_stt_) {
129             this->is_changed_accel_pedal_stt_ = true;
130             this->accel_pedal_stt_ = accel_pedal_stt;
131         }
132     }
133     else if (strstr(name, this->kSignalName_[SignalNoLightstatusBrake])) {
134         // Update lightstatus brake status
135         this->lightstatus_brake_status_ = jh::getBoolFromJson(object, "value");
136         HMI_DEBUG("wm:lcc", "Update lightstatus brake status:%d", this->lightstatus_brake_status_);
137     }
138
139     return name;
140 }
141
142 bool LowCanClient::isChangedAccelPedalState() {
143     HMI_DEBUG("wm:lcc", "Call");
144
145     return this->is_changed_accel_pedal_stt_;
146 }
147
148 int LowCanClient::getCurrentTransGearState() {
149     HMI_DEBUG("wm:lcc", "Call");
150
151     return this->trans_gear_pos_;
152 }
153
154 bool LowCanClient::getCurrentHeadlampState() {
155     HMI_DEBUG("wm:lcc", "Call");
156
157     return (bool)this->headlamp_status_;
158 }
159
160 bool LowCanClient::getCurrentParkingBrakeState() {
161     HMI_DEBUG("wm:lcc", "Call");
162
163     return (bool)this->parking_brake_status_;
164 }
165
166 double LowCanClient::getCurrentAccelPedalPosition() {
167     HMI_DEBUG("wm:lcc", "Call");
168
169     return this->accel_pedal_pos_;
170 }
171
172 bool LowCanClient::getCurrentAccelPedalState() {
173     HMI_DEBUG("wm:lcc", "Call");
174
175     return this->accel_pedal_stt_;
176 }
177
178 bool LowCanClient::getCurrentLightstatusBrakeState() {
179     HMI_DEBUG("wm:lcc", "Call");
180
181     return (bool)this->lightstatus_brake_status_;
182 }
183
184
185 } // namespace wm