summaryrefslogtreecommitdiffstats
path: root/demo3/common/agl-service-windowmanager/src/low_can_client.cpp
blob: 090aa14938bb5448b7a65bfd1649b7d7dc85b33c (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
/*
 * 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 "util.hpp"

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),
      lightstatus_brake_status(TRUE),
      is_changed_accel_pedal_stt(false)
{
}

void LowCanClient::initialize()
{
    int ret;

    // Require API "low-can"
    ret = afb_daemon_require_api_v2("low-can", 1);
    if (0 > ret)
    {
        HMI_INFO("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" }
    // low-can subscribe { "event": "lightstatus.brake" }
    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("subscribe message:%s", json_object_get_string(json_obj));

        // Subscribe
        afb_service_call("low-can", "subscribe", json_obj,
                         [](void *closure, int status, json_object *result) {
                             HMI_DEBUG("subscribe result:%s", json_object_get_string(result));
                         },
                         nullptr);
    }

    return;
}

const char *LowCanClient::analyzeCanSignal(struct json_object *object)
{
    HMI_DEBUG("object:%s", json_object_get_string(object));

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

    if (strstr(name, this->kSignalName[SignalNoVehicliSpeed]))
    {
        // Update vehicle speed
        this->vehicle_speed = jh::getIntFromJson(object, "value");
        HMI_DEBUG("Update vehicle speed:%d", this->vehicle_speed);
    }
    else if (strstr(name, this->kSignalName[SignalNoTransGearPos]))
    {
        // Update transmission gear position
        this->trans_gear_pos = jh::getIntFromJson(object, "value");
        HMI_DEBUG("Update transmission gear position:%d", this->trans_gear_pos);
    }
    else if (strstr(name, this->kSignalName[SignalNoHeadlame]))
    {
        // Update headlamp status
        this->headlamp_status = jh::getBoolFromJson(object, "value");
        HMI_DEBUG("Update headlamp status:%d", this->headlamp_status);
    }
    else if (strstr(name, this->kSignalName[SignalNoParkingBrake]))
    {
        // Update parking gear status
        this->parking_brake_status = jh::getBoolFromJson(object, "value");
        HMI_DEBUG("Update parking brake status:%d", this->parking_brake_status);
    }
    else if (strstr(name, this->kSignalName[SignalNoAccelPedalPos]))
    {
        // 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("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;
        }
    }
    else if (strstr(name, this->kSignalName[SignalNoLightstatusBrake]))
    {
        // Update lightstatus brake status
        this->lightstatus_brake_status = jh::getBoolFromJson(object, "value");
        HMI_DEBUG("Update lightstatus brake status:%d", this->lightstatus_brake_status);
    }

    return name;
}

bool LowCanClient::isChangedAccelPedalState()
{
    return this->is_changed_accel_pedal_stt;
}

int LowCanClient::getCurrentTransGearState()
{
    return this->trans_gear_pos;
}

bool LowCanClient::getCurrentHeadlampState()
{
    return (bool)this->headlamp_status;
}

bool LowCanClient::getCurrentParkingBrakeState()
{
    return (bool)this->parking_brake_status;
}

double LowCanClient::getCurrentAccelPedalPosition()
{
    return this->accel_pedal_pos;
}

bool LowCanClient::getCurrentAccelPedalState()
{
    return this->accel_pedal_stt;
}

bool LowCanClient::getCurrentLightstatusBrakeState()
{
    return (bool)this->lightstatus_brake_status;
}

} // namespace wm