aboutsummaryrefslogtreecommitdiffstats
path: root/src/low_can_client.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/low_can_client.cpp')
-rw-r--r--src/low_can_client.cpp193
1 files changed, 193 insertions, 0 deletions
diff --git a/src/low_can_client.cpp b/src/low_can_client.cpp
new file mode 100644
index 0000000..23a39e2
--- /dev/null
+++ b/src/low_can_client.cpp
@@ -0,0 +1,193 @@
+/*
+ * 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 "json_helper.hpp"
+#include "low_can_client.hpp"
+#include "hmi-debug.h"
+
+extern "C" {
+#include <afb/afb-binding.h>
+}
+
+
+namespace wm {
+
+LowCanClient::LowCanClient() :
+ vehicle_speed_(0),
+ trans_gear_pos_(0),
+ park_brake_status_(TRUE),
+ headlamp_status_(FALSE),
+ prv_car_state_("car_stop"),
+ crr_car_state_("car_stop"),
+ prv_lamp_state_("lamp_off"),
+ crr_lamp_state_("lamp_off"),
+ is_changed_car_state_(false),
+ is_changed_lamp_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" }
+ for (int i=0; i<this->kNumEvent_; i++) {
+ json_object *json_obj = json_object_new_object();
+ json_object_object_add(json_obj, "event", json_object_new_string(this->kEventName_[i]));
+ HMI_DEBUG("wm:lcc", "subscribe message:%s", json_object_get_string(json_obj));
+
+ 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);
+ return;
+ }
+ 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, "vehicle.speed")) {
+ HMI_DEBUG("wm:lcc", "Receive vehicle speed");
+ // 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, "transmission_gear_position")) {
+ HMI_DEBUG("wm:lcc", "Receive transmission gear position");
+ // 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, "parking_brake_status")) {
+ HMI_DEBUG("wm:lcc", "Receive parking brake status");
+ // Update parking gear status
+ json_bool park_brake = jh::getBoolFromJson(object, "value");
+ if (this->park_brake_status_ != park_brake) {
+ this->park_brake_status_ = park_brake;
+ HMI_DEBUG("wm:lcc", "Update parking brake status:%d", this->park_brake_status_);
+ }
+ }
+ else if (strstr(name, "headlamp_status")) {
+ HMI_DEBUG("wm:lcc", "Receive headlamp status");
+ // 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_);
+ }
+ }
+
+ // Update car state
+ if ((0 == this->vehicle_speed_) || (true == this->park_brake_status_)) {
+ this->crr_car_state_ = "car_stop";
+ }
+ else {
+ this->crr_car_state_ = "car_run";
+ }
+ HMI_DEBUG("wm:lcc", "Current car state:%s", this->crr_car_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());
+
+ // If car state is changed,
+ // backup current state for previous state and set flag
+ if (this->prv_car_state_ != this->crr_car_state_) {
+ HMI_DEBUG("wm:lcc", "Car state is changed: %s -> %s",
+ this->prv_car_state_.c_str(), this->crr_car_state_.c_str());
+ this->prv_car_state_ = this->crr_car_state_;
+ this->is_changed_car_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;
+ }
+}
+
+bool LowCanClient::isChangedCarState() {
+ HMI_DEBUG("wm:lcc", "Call");
+
+ // Return changed flag
+ return this->is_changed_car_state_;
+}
+
+bool LowCanClient::isChangedLampState() {
+ HMI_DEBUG("wm:lcc", "Call");
+
+ // Return changed flag
+ return this->is_changed_lamp_state_;
+}
+
+const char* LowCanClient::getCurrentCarState() {
+ HMI_DEBUG("wm:lcc", "Call");
+
+ // Clear changed flag
+ this->is_changed_car_state_ = false;
+
+ // Return current car state
+ return this->crr_car_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();
+}
+
+
+} // namespace wm