aboutsummaryrefslogtreecommitdiffstats
path: root/src/low_can_client.cpp
blob: 192e124347199a4b56bb813951dd6fc63456859c (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
/*
 * 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),
  accel_pedal_stt_(FALSE),
  is_changed_accel_pedal_stt_(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->kSignalName_[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;
}

const char* 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->kSignalName_[0])) {
        // Update vehicle speed
        this->vehicle_speed_ = jh::getIntFromJson(object, "value");
        HMI_DEBUG("wm:lcc", "Update vehicle speed:%d", this->vehicle_speed_);
    }
    else if (strstr(name, this->kSignalName_[1])) {
        // Update transmission gear position
        this->trans_gear_pos_ = jh::getIntFromJson(object, "value");
        HMI_DEBUG("wm:lcc", "Update transmission gear position:%d", this->trans_gear_pos_);
    }
    else if (strstr(name, this->kSignalName_[2])) {
        // Update headlamp status
        this->headlamp_status_ = jh::getBoolFromJson(object, "value");
        HMI_DEBUG("wm:lcc", "Update headlamp status:%d", this->headlamp_status_);
    }
    else if (strstr(name, this->kSignalName_[3])) {
        // Update parking gear status
        this->parking_brake_status_ = jh::getBoolFromJson(object, "value");
        HMI_DEBUG("wm:lcc", "Update parking brake status:%d", this->parking_brake_status_);
    }
    else if (strstr(name, this->kSignalName_[4])) {
        // Clear flag for whether accel pedal state is changed
        this->is_changed_accel_pedal_stt_ = false;

        // Update accelerator pedal status
        this->accel_pedal_pos_ = jh::getDoubleFromJson(object, "value");
        HMI_DEBUG("wm:lcc", "Update accelerator pedal position:%lf", this->accel_pedal_pos_);

        bool accel_pedal_stt;
        if (0 != this->accel_pedal_pos_) {
            accel_pedal_stt = true;
        }
        else {
            accel_pedal_stt = false;
        }

        if (accel_pedal_stt != this->accel_pedal_stt_) {
            this->is_changed_accel_pedal_stt_ = true;
            this->accel_pedal_stt_ = accel_pedal_stt;
        }
    }

    return name;
}

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

    return this->is_changed_accel_pedal_stt_;
}

int LowCanClient::getCurrentTransGearState() {
    HMI_DEBUG("wm:lcc", "Call");

    return this->trans_gear_pos_;
}

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

    return (bool)this->headlamp_status_;
}

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

    return (bool)this->parking_brake_status_;
}

double LowCanClient::getCurrentAccelPedalPosition() {
    HMI_DEBUG("wm:lcc", "Call");

    return this->accel_pedal_pos_;
}

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

    return this->accel_pedal_stt_;
}


} // namespace wm