aboutsummaryrefslogtreecommitdiffstats
path: root/src/low_can_client.cpp
blob: e3e472fa90abaec5364afa56a921407a2f16224f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "low_can_client.hpp"
#include "json_helper.hpp"
#include "hmi-debug.h"

extern "C" {
#include <afb/afb-binding.h>
}


namespace wm {

LowCanClient::LowCanClient() :
  vehicle_speed_(0),
  trans_gear_pos_(0),
  headlamp_status_(FALSE),
  parking_brake_status_(TRUE),
  accel_pedal_pos_(0),
  prv_lamp_state_("lamp_off"),
  crr_lamp_state_("lamp_off"),
  prv_parking_brake_state_("parking_brake_on"),
  crr_parking_brake_state_("parking_brake_on"),
  prv_accel_pedal_state_("accel_pedal_off"),
  crr_accel_pedal_state_("accel_pedal_off"),
  is_changed_lamp_state_(false),
  is_changed_parking_brake_state_(false),
  is_changed_accel_pedal_state_(false)
{
    HMI_DEBUG("wm:lcc", "Call");
}

void LowCanClient::initialize() {
    HMI_DEBUG("wm:lcc", "Call");

    int ret;

    // Require API "low-can"
    ret = afb_daemon_require_api_v2("low-can", 1);
    if (0 > ret) {
        HMI_INFO("wm:lcc", "Requirement API \"low-can\" failed");
        return;
    }

    // Subscribe low-level-can
    // low-can subscribe { "event": "vehicle.speed" }
    // low-can subscribe { "event": "transmission_gear_position" }
    // low-can subscribe { "event": "headlamp_status" }
    // low-can subscribe { "event": "parking_brake_status" }
    // low-can subscribe { "event": "accelerator.pedal.position" }
    for (int i=SignalNoMin; i<=SignalNoMax; i++) {
        // Set Event
        json_object *json_obj = json_object_new_object();
        json_object_object_add(json_obj, "event",
                               json_object_new_string(this->kEventName_[i]));

        // Set filter
        if (0 != strcmp("", this->kFilterValue_[i])) {
            json_object_object_add(json_obj, "filter",
                                   json_tokener_parse(this->kFilterValue_[i]));
        }
        HMI_DEBUG("wm:lcc", "subscribe message:%s", json_object_get_string(json_obj));

        // Subscribe
        json_object *json_result = json_object_new_object();
        ret = afb_service_call_sync("low-can", "subscribe", json_obj, &json_result);
        if (0 > ret) {
            HMI_INFO("wm:lcc", "Could not subscribe to \"low-can\" :%d", ret);
        }
        HMI_DEBUG("wm:lcc", "subscribe result:%s", json_object_get_string(json_result));
    }

    return;
}

void LowCanClient::analyzeCanSignal(struct json_object *object) {
    HMI_DEBUG("wm:lcc", "object:%s", json_object_get_string(object));

    const char* name = jh::getStringFromJson(object, "name");
    HMI_DEBUG("wm:lcc", "CAN signal name:%s", name);

    if (strstr(name, this->kEventName_[0])) {
        HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[0]);
        // Update vehicle speed
        int speed = jh::getIntFromJson(object, "value");
        if (this->vehicle_speed_ != speed) {
            this->vehicle_speed_ = speed;
            HMI_DEBUG("wm:lcc", "Update vehicle speed:%d", this->vehicle_speed_);
        }
    }
    else if (strstr(name, this->kEventName_[1])) {
        HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[1]);
        // Update transmission gear position
        int gear_pos = jh::getIntFromJson(object, "value");
        if (this->trans_gear_pos_ != gear_pos) {
            this->trans_gear_pos_ = gear_pos;
            HMI_DEBUG("wm:lcc", "Update transmission gear position:%d", this->trans_gear_pos_);
        }
    }
    else if (strstr(name, this->kEventName_[2])) {
        HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[2]);
        // Update headlamp status
        json_bool headlamp = jh::getBoolFromJson(object, "value");
        if (this->headlamp_status_ != headlamp) {
            this->headlamp_status_ = headlamp;
            HMI_DEBUG("wm:lcc", "Update headlamp status:%d", this->headlamp_status_);
        }
    }
    else if (strstr(name, this->kEventName_[3])) {
        HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[3]);
        // Update parking gear status
        json_bool parking_brake = jh::getBoolFromJson(object, "value");
        if (this->parking_brake_status_ != parking_brake) {
            this->parking_brake_status_ = parking_brake;
            HMI_DEBUG("wm:lcc", "Update parking brake status:%d", this->parking_brake_status_);
        }
    }
    else if (strstr(name, this->kEventName_[4])) {
        HMI_DEBUG("wm:lcc", "Receive %s", this->kEventName_[4]);
        // Update accelerator pedal status
        double accel_pedal_pos = jh::getDoubleFromJson(object, "value");
        if (this->accel_pedal_pos_ != accel_pedal_pos) {
            this->accel_pedal_pos_ = accel_pedal_pos;
            HMI_DEBUG("wm:lcc", "Update accelerator pedal status:%lf", this->accel_pedal_pos_);
        }
    }

    // Update transmission gear position state
    if (TransGearPosValN == this->trans_gear_pos_) {
        this->crr_accel_pedal_state_ = "trans_gear_neutral";
    }
    else {
        this->crr_accel_pedal_state_ = "trans_gear_not_neutral";
    }
    HMI_DEBUG("wm:lcc", "Current transmission gear state:%s", this->crr_trans_gear_state_.c_str());

    // Update lamp state
    if (true == this->headlamp_status_) {
        this->crr_lamp_state_ = "lamp_on";
    }
    else {
        this->crr_lamp_state_ = "lamp_off";
    }
    HMI_DEBUG("wm:lcc", "Current lamp state:%s", this->crr_lamp_state_.c_str());

    // Update parking brake state
    if (this->parking_brake_status_) {
        this->crr_parking_brake_state_ = "parking_brake_on";
    }
    else {
        this->crr_parking_brake_state_ = "parking_brake_off";
    }
    HMI_DEBUG("wm:lcc", "Current parking brake state:%s", this->crr_parking_brake_state_.c_str());

    // Update accelerator pedal state
    if (0 == this->accel_pedal_pos_) {
        this->crr_accel_pedal_state_ = "accel_pedal_off";
    }
    else {
        this->crr_accel_pedal_state_ = "accel_pedal_on";
    }
    HMI_DEBUG("wm:lcc", "Current accelerator pedal state:%s", this->crr_accel_pedal_state_.c_str());

    // If transmission gear state is changed,
    // backup current state for previous state and set flag
    if (this->prv_trans_gear_state_ != this->crr_trans_gear_state_) {
        HMI_DEBUG("wm:lcc", "Transmission Gear is changed: %s -> %s",
                  this->prv_trans_gear_state_.c_str(), this->crr_trans_gear_state_.c_str());
        this->prv_trans_gear_state_ = this->crr_trans_gear_state_;
        this->is_changed_trans_gear_state_ = true;
    }

    // If lamp state is changed,
    // backup current state for previous state and set flag
    if (this->prv_lamp_state_ != this->crr_lamp_state_) {
        HMI_DEBUG("wm:lcc", "Lamp state is changed: %s -> %s",
                  this->prv_lamp_state_.c_str(), this->crr_lamp_state_.c_str());
        this->prv_lamp_state_ = this->crr_lamp_state_;
        this->is_changed_lamp_state_ = true;
    }

    // If parking brake state is changed,
    // backup current state for previous state and set flag
    if (this->prv_parking_brake_state_ != this->crr_parking_brake_state_) {
        HMI_DEBUG("wm:lcc", "Parking Brake state is changed: %s -> %s",
                  this->prv_parking_brake_state_.c_str(), this->crr_parking_brake_state_.c_str());
        this->prv_parking_brake_state_ = this->crr_parking_brake_state_;
        this->is_changed_parking_brake_state_ = true;
    }

    // If accelerator pedal state is changed,
    // backup current state for previous state and set flag
    if (this->prv_accel_pedal_state_ != this->crr_accel_pedal_state_) {
        HMI_DEBUG("wm:lcc", "Accelerator Pedal is changed: %s -> %s",
                  this->prv_accel_pedal_state_.c_str(), this->crr_accel_pedal_state_.c_str());
        this->prv_accel_pedal_state_ = this->crr_accel_pedal_state_;
        this->is_changed_accel_pedal_state_ = true;
    }
}

bool LowCanClient::isChangedTransGearState() {
    HMI_DEBUG("wm:lcc", "Call");

    // Return changed flag
    return this->is_changed_trans_gear_state_;
}

bool LowCanClient::isChangedLampState() {
    HMI_DEBUG("wm:lcc", "Call");

    // Return changed flag
    return this->is_changed_lamp_state_;
}

bool LowCanClient::isChangedParkingBrakeState() {
    HMI_DEBUG("wm:lcc", "Call");

    // Return changed flag
    return this->is_changed_parking_brake_state_;
}

bool LowCanClient::isChangedAccelPedalState() {
    HMI_DEBUG("wm:lcc", "Call");

    // Return changed flag
    return this->is_changed_accel_pedal_state_;
}

const char* LowCanClient::getCurrentTransGearState() {
    HMI_DEBUG("wm:lcc", "Call");

    // Clear changed flag
    this->is_changed_trans_gear_state_ = false;

    // Return current transmission gear state
    return this->crr_trans_gear_state_.c_str();
}

const char* LowCanClient::getCurrentLampState() {
    HMI_DEBUG("wm:lcc", "Call");

    // Clear changed flag
    this->is_changed_lamp_state_ = false;

    // Return current lamp state
    return this->crr_lamp_state_.c_str();
}

const char* LowCanClient::getCurrentParkingBrakeState() {
    HMI_DEBUG("wm:lcc", "Call");

    // Clear changed flag
    this->is_changed_parking_brake_state_ = false;

    // Return current parking brake state
    return this->crr_parking_brake_state_.c_str();
}

const char* LowCanClient::getCurrentAccelPedalState() {
    HMI_DEBUG("wm:lcc", "Call");

    // Clear changed flag
    this->is_changed_accel_pedal_state_ = false;

    // Return current accelerator pedal state
    return this->crr_accel_pedal_state_.c_str();
}


} // namespace wm