summaryrefslogtreecommitdiffstats
path: root/can_hal/src
diff options
context:
space:
mode:
Diffstat (limited to 'can_hal/src')
-rw-r--r--can_hal/src/Makefile52
-rw-r--r--can_hal/src/can_hal_api.cpp166
-rw-r--r--can_hal/src/can_hal_core.cpp372
-rw-r--r--can_hal/src/can_hal_stm.cpp243
-rw-r--r--can_hal/src/driver_can__CWORD31_.c472
-rw-r--r--can_hal/src/inc/can_hal_core.h37
-rw-r--r--can_hal/src/inc/can_hal_frameworkunifiedlog.h76
-rw-r--r--can_hal/src/inc/can_hal_internal.h31
-rw-r--r--can_hal/src/inc/can_hal_stm.h45
-rw-r--r--can_hal/src/inc/can_mng_api.h120
10 files changed, 1614 insertions, 0 deletions
diff --git a/can_hal/src/Makefile b/can_hal/src/Makefile
new file mode 100644
index 00000000..f34eb759
--- /dev/null
+++ b/can_hal/src/Makefile
@@ -0,0 +1,52 @@
+#
+# @copyright Copyright (c) 2019-2020 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.
+#
+
+######### installed shared library(*.so) #############
+INST_SHLIBS = libcan_hal
+
+######### installed static library(*.a) #############
+
+######### install headers(*.h) #############
+INST_HEADERS = ../hal_api/can_hal.h
+
+######### compiled sources #############
+libcan_hal_SRCS = can_hal_core.cpp can_hal_stm.cpp can_hal_api.cpp
+libcan_hal_SRCS += driver_can__CWORD31_.c
+
+######### include paths/files ###################
+CPPFLAGS += -I./inc/
+CPPFLAGS += -I../hal_api/
+
+######### compile options #######################
+CPPFLAGS += -Wall -fPIC
+
+######### link options ##########################
+LDFLAGS += -shared
+LDFLAGS += -Wl,--gc-sections
+LDFLAGS += -Wl,--no-as-needed
+LDFLAGS += -Wl,--no-undefined
+LDFLAGS += -L$(SDKTARGETSYSROOT)/usr/agl/lib
+
+RPATH := /usr/lib:/usr/agl/lib
+
+######### linked library ########################
+LDLIBS += -Wl,-Bdynamic -lstdc++
+LDLIBS += -Wl,-Bdynamic -lpthread
+LDLIBS += -Wl,-Bdynamic -lNS_FrameworkUnified
+
+COMPONENT_NAME =
+AGL_TOPDIR ?= $(DESTDIR)/usr
+include ../can_hal.mk
diff --git a/can_hal/src/can_hal_api.cpp b/can_hal/src/can_hal_api.cpp
new file mode 100644
index 00000000..86a0104d
--- /dev/null
+++ b/can_hal/src/can_hal_api.cpp
@@ -0,0 +1,166 @@
+/*
+ * @copyright Copyright (c) 2017-2020 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.
+ */
+
+/*
+ * Information.
+ * This source code is a sample source code .
+ * Implementation of the function must be performed by the vendor.
+ */
+
+#include "can_hal_core.h"
+#include "can_hal_stm.h"
+#include <stdio.h>
+#include <pthread.h>
+#include <string.h>
+#include <assert.h>
+
+CANHAL_RET_API CanOpen(HANDLE h_app, CanHalType type) {
+ CANHAL_RET_API ret = CANHAL_RET_ERR_ERR;
+
+ if (!TypeIsValid(type)) {
+ return CANHAL_RET_ERR_PARAM;
+ }
+
+ if (!h_app) {
+ return CANHAL_RET_ERR_PARAM;
+ }
+
+ if (IsCanHalOpened(type)) {
+ return CANHAL_RET_NORMAL;
+ }
+
+ if (IsDeviceEnabled(type)) {
+ return CANHAL_RET_NORMAL;
+ }
+
+ ret = CanOpenCore(type);
+ if (CANHAL_RET_NORMAL != ret) {
+ return ret;
+ }
+
+ ret = CanHalCreateInternalThread(h_app, type);
+ if (CANHAL_RET_NORMAL != ret) {
+ CanCloseCore(type);
+ return ret;
+ }
+
+ SetDeviceStateEnable(type);
+ SetCanHalStateOpen(type);
+ InvokeStateCallback(type);
+ return ret;
+}
+
+CANHAL_RET_API CanClose(HANDLE h_app, CanHalType type) {
+ CANHAL_RET_API ret = CANHAL_RET_ERR_ERR;
+ if (!TypeIsValid(type)) {
+ return CANHAL_RET_ERR_PARAM;
+ }
+
+ if (!h_app) {
+ return CANHAL_RET_ERR_PARAM;
+ }
+
+ if (!IsCanHalOpened(type)) {
+ return CANHAL_RET_NORMAL;
+ }
+
+ if (IsDeviceEnabled(type)) {
+ ret = CanCloseCore(type);
+ if (CANHAL_RET_NORMAL != ret)
+ return ret;
+ }
+
+ ret = CanHalDestroyInternalThread(h_app, type);
+ if (CANHAL_RET_NORMAL != ret)
+ return ret;
+
+ SetDeviceStateDisable(type);
+ SetCanHalStateClose(type);
+ InvokeErrorCallback(h_app, type);
+
+ return ret;
+}
+
+static CANHAL_RET_API CanSendCommonCheck(const void *message,
+ enum CanHalType type) {
+ if (!TypeIsValid(type)) {
+ return CANHAL_RET_ERR_PARAM;
+ }
+
+ if (!message) {
+ return CANHAL_RET_ERR_PARAM;
+ }
+
+ if (!IsCanHalOpened(type)) {
+ return CANHAL_RET_ERR_STATE;
+ }
+
+ if (!IsDeviceEnabled(type)) {
+ return CANHAL_RET_ERR_STATE;
+ }
+
+ return CANHAL_RET_NORMAL;
+}
+
+CANHAL_RET_API CanSend(HANDLE h_app,
+ const CanMessage *message, enum CanHalType type) {
+ CANHAL_RET_API ret = CANHAL_RET_ERR_ERR;
+ void *_message = NULL;
+ ssize_t sz = 0;
+
+ ret = CanSendCommonCheck(message, type);
+ if (ret != CANHAL_RET_NORMAL)
+ return ret;
+
+ if (!h_app) {
+ return CANHAL_RET_ERR_PARAM;
+ }
+
+ if (CAN_NORMAL_MESSAGE_LEN < message->dlc) {
+ return CANHAL_RET_ERR_PARAM;
+ }
+
+ _message = CanHalPackMessage(h_app, (const void *)message,
+ sizeof(CanMessage), &sz);
+ if (!_message)
+ return CANHAL_RET_ERR_ERR;
+
+ ret = CanHalInternalSend(type, _message, sz);
+ CanHalDestroyPackedMessage(_message);
+ return ret;
+}
+
+CANHAL_RET_API CanGetVersion(HANDLE h_app, std::string *p_version) {
+ CanHalType type = CAN_HAL_TYPE_CAN;
+ if (!h_app) {
+ return CANHAL_RET_ERR_PARAM;
+ }
+
+ if (!IsCanHalOpened(type)) {
+ return CANHAL_RET_ERR_STATE;
+ }
+
+ if (!IsDeviceEnabled(type)) {
+ return CANHAL_RET_ERR_STATE;
+ }
+
+ if (NULL == p_version) {
+ return CANHAL_RET_ERR_PARAM;
+ }
+
+ *p_version = "FFFF";
+ return CANHAL_RET_NORMAL;
+}
diff --git a/can_hal/src/can_hal_core.cpp b/can_hal/src/can_hal_core.cpp
new file mode 100644
index 00000000..bdbdbbce
--- /dev/null
+++ b/can_hal/src/can_hal_core.cpp
@@ -0,0 +1,372 @@
+/*
+ * @copyright Copyright (c) 2017-2020 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 "can_hal.h"
+#include "can_hal_core.h"
+#include "can_hal_internal.h"
+#include "can_hal_frameworkunifiedlog.h"
+#include "can_hal_stm.h"
+extern "C" {
+ #include "can_mng_api.h"
+}
+#include <native_service/ns_message_center_if.h>
+#include <native_service/frameworkunified_framework_if.h>
+#include <native_service/frameworkunified_multithreading.h>
+#include <stdio.h>
+#include <pthread.h>
+#include <string>
+
+static CanCtlApiObj g_driver_obj; // can driver handler
+
+typedef struct MessageWrapper {
+ HANDLE h_app;
+ void *message;
+} MessageWrapper;
+
+// define frame
+enum CANHAL_FRAME {
+ CANHAL_FRAME_SF = 0x00,
+ CANHAL_FRAME_FF,
+ CANHAL_FRAME_CF,
+ CANHAL_FRAME_NONE = 0xFF,
+};
+
+// Declaration of the local functions
+static EFrameworkunifiedStatus CanSendData(HANDLE h_app);
+static UINT8 PackageSendData(const CanMessage *send_msg,
+ UINT8 *send_cmd);
+static UINT8 PackageRecvData(const UINT8 *recv_data, CanMessage *message);
+static uint32_t GetCanId(const UINT8 *recv_data);
+
+#define CANHAL_VALID_NUM_INDEX 0x00
+#define CANHAL_CANID_HI_INDEX 0x00
+#define CANHAL_CANID_LO_INDEX 0x01
+#define CANHAL_DLC_INDEX 0x02
+#define CANHAL_DATA_START_INDEX 0x03
+
+#define CANHAL_RECV_DATA_LENGTH_INVALID 0xFF
+
+#define CANHAL_RECV_DATABLOCK_SIZE (0x0B)
+#define CANHAL_RECV_NTA_INDEX (0x03)
+
+// Register table for framework callback
+static const FrameworkunifiedProtocolCallbackHandler kCanHalPcbhs[] = {
+ { TX_INTERNAL, CanSendData },
+};
+
+void *CanHalPackMessage(HANDLE h_app, const void *msg, ssize_t msg_sz,
+ ssize_t *packed_sz) {
+ *packed_sz = msg_sz + sizeof(ssize_t) + sizeof(HANDLE);
+ void *p = malloc(*packed_sz);
+ if (p) {
+ char *_p = (char *)p;
+ // Set HANDLE to new buffer.
+ memcpy(_p, &h_app, sizeof(HANDLE));
+ // Set message size to new buffer
+ _p += sizeof(HANDLE);
+ *(ssize_t *)_p = msg_sz;
+ // Set message body to new buffer
+ _p += (sizeof(ssize_t));
+ memcpy(_p, msg, msg_sz);
+ }
+ return p;
+}
+
+void CanHalUnPackMessage(void *packed, HANDLE *h_app, void **msg,
+ ssize_t *msg_sz) {
+ char *_p = (char *)packed;
+ *h_app = *(HANDLE *)_p;
+ _p += sizeof(HANDLE);
+ *msg_sz = *((ssize_t *)_p);
+ _p += sizeof(ssize_t);
+ *msg = _p;
+}
+
+void CanHalDestroyPackedMessage(void *packed) {
+ free(packed);
+}
+
+// Start function of the receive thread
+void *CanRecvRun(void *arg) {
+ UINT8 data_start_index = 0;
+ UINT8 loop_num = 0;
+ UINT32 ret = 0;
+ CanCtlApiCmd recv_cmd;
+ memset(&recv_cmd, 0x00, sizeof(CanCtlApiCmd));
+ CanMessage recv_data_can;
+ memset(&recv_data_can, 0x00, sizeof(recv_data_can));
+ enum CanHalType *type = (enum CanHalType *)arg;
+
+ while (1) {
+ // call driver API to receive the can data
+ ret = CanCtlApiRcvCmd(&g_driver_obj, &recv_cmd);
+ if (CAN_CTL_RET_SUCCESS != ret) {
+ continue;
+ }
+
+ /**
+ * Receive data in the following structure:
+ * -----------------
+ * BYTE 01 | num of can data |
+ * -----------------
+ * BYTE 02 | CAN ID (Hi) |
+ * -----------------
+ * BYTE 03 | CAN ID (Lo) |
+ * -----------------
+ * BYTE 04 | DLC |
+ * -----------------
+ * BYTE 05 | DATA #1 |
+ * -----------------
+ * | ... |
+ * -----------------
+ * BYTE 12 | DATA #8 |
+ * -----------------
+ * BYTE 13 | CAN ID (Hi) |
+ * -----------------
+ * BYTE 14 | CAN ID (Lo) |
+ * -----------------
+ * BYTE 15 | DLC |
+ * -----------------
+ * BYTE 16 | DATA #1 |
+ * -----------------
+ * | ... |
+ * -----------------
+ * BYTE 23 | DATA #8 |
+ * -----------------
+ * | ... |
+ * -----------------
+ * BYTE 255 | |
+ * -----------------
+ *
+ * BYTE 0 for the invalid number of the can data
+ * CAN ID (Hi) and (Lo) combine for the CAN ID
+ * CAN ID (Hi) byte's low 7 bits is the high 7 bits of the CAN ID
+ * CAN ID (Lo) byte's high 4 bits is the low 4 bits of the CAN ID
+ * DLC for the length of the following data
+ * DATA #1 ~ #8 for the receive data, its actual length changes
+ * according to the previous DLC byte
+ */
+ UINT8 total_data_num = recv_cmd.data[CANHAL_VALID_NUM_INDEX];
+ data_start_index = 1;
+
+ for (loop_num = 0; loop_num < total_data_num; loop_num++) {
+ uint32_t can_id;
+ can_id = GetCanId(&(recv_cmd.data[data_start_index]));
+
+ // normal can
+ // Return value of the PackageRecvData is the total length
+ // of one can data package, so add to the data_start_index
+ // for the next can data package.
+ ret = PackageRecvData(&(recv_cmd.data[data_start_index]),
+ &recv_data_can);
+ if (CANHAL_RECV_DATA_LENGTH_INVALID == ret) {
+ continue;
+ }
+ data_start_index += ret;
+ CanHalReceiveNotify(*type, &(recv_data_can), sizeof(recv_data_can));
+ }
+
+ // reset buffer
+ memset(&recv_cmd, 0x00, sizeof(CanCtlApiCmd));
+ usleep(1000);
+ }
+
+ return NULL;
+}
+
+// get can id from recive buffer
+static uint32_t GetCanId(const UINT8 *recv_data) {
+ uint32_t can_id;
+ can_id = (recv_data[CANHAL_CANID_HI_INDEX] & 0x7F);
+ can_id <<= 4;
+ can_id |= ((recv_data[CANHAL_CANID_LO_INDEX] & 0xF0) >> 4);
+ return can_id;
+}
+
+static CANHAL_RET_API CanOpenCoreCAN(void) {
+ CanCtlRcvId recv_can_id;
+ memset(recv_can_id.id, 0xFF, sizeof(recv_can_id.id));
+ if (CAN_CTL_RET_SUCCESS != CanCtlApiOpen(&g_driver_obj))
+ return CANHAL_RET_ERR_ERR;
+
+ if (CAN_CTL_RET_SUCCESS != CanCtlApiSetRcvId(&g_driver_obj, &recv_can_id)) {
+ return CANHAL_RET_ERR_ERR;
+ }
+
+ return CANHAL_RET_NORMAL;
+}
+
+CANHAL_RET_API CanOpenCore(CanHalType type) {
+ CANHAL_RET_API ret = CANHAL_RET_ERR_ERR;
+ switch (type) {
+ case CAN_HAL_TYPE_CAN:
+ ret = CanOpenCoreCAN();
+ break;
+ default:
+ // Do nothing
+ break;
+ }
+ return ret;
+}
+
+// Initialize the sending thread
+EFrameworkunifiedStatus CanSendThreadStart(HANDLE h_app) {
+ EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
+ intptr_t ptr;
+ e_status = FrameworkunifiedAttachCallbacksToDispatcher(h_app,
+ FRAMEWORKUNIFIED_ANY_SOURCE,
+ kCanHalPcbhs,
+ _countof(kCanHalPcbhs));
+ if (e_status != eFrameworkunifiedStatusOK)
+ goto finish;
+
+ e_status = FrameworkunifiedGetMsgDataOfSize(h_app, &ptr, sizeof(ptr));
+ if (e_status != eFrameworkunifiedStatusOK)
+ goto finish;
+
+ *((HANDLE *)ptr) = FrameworkunifiedMcOpenSender(h_app, FrameworkunifiedGetAppName(h_app));
+ if (!(*(HANDLE *)ptr))
+ e_status = eFrameworkunifiedStatusFail;
+
+finish:
+ return e_status;
+}
+
+// Clean the sending thread
+EFrameworkunifiedStatus CanSendThreadStop(HANDLE h_app) {
+ EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
+ intptr_t ptr;
+ e_status = FrameworkunifiedGetMsgDataOfSize(h_app, &ptr, sizeof(ptr));
+ if (e_status != eFrameworkunifiedStatusOK)
+ goto finish;
+
+ FrameworkunifiedMcClose(*((HANDLE*)ptr));
+finish:
+ return e_status;
+}
+
+static CANHAL_RET_API CanCloseCoreCAN(void) {
+ CanCtlApiClose(&g_driver_obj);
+ return CANHAL_RET_NORMAL;
+}
+
+// Stop the can hal
+CANHAL_RET_API CanCloseCore(CanHalType type) {
+ CANHAL_RET_API ret = CANHAL_RET_NORMAL;
+ switch (type) {
+ case CAN_HAL_TYPE_CAN:
+ ret = CanCloseCoreCAN();
+ if (ret != CANHAL_RET_NORMAL)
+ goto finish;
+ break;
+ default:
+ goto finish;
+ break;
+ }
+finish:
+ return ret;
+}
+
+// Callback for the sending thread to send the can data
+static EFrameworkunifiedStatus CanSendData(HANDLE h_app) {
+ UINT8 ret = 0;
+ EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
+ HANDLE hb = NULL;
+ CanMessage mb;
+ ssize_t sz = 0;
+ void *send_msg = NULL;
+ HANDLE unpacked_h_app;
+ CanMessage *unpacked_msg;
+ ssize_t unpacked_sz;
+
+ send_msg = CanHalPackMessage(hb, (const void *)&mb, sizeof(mb), &sz);
+ if (!send_msg)
+ return eFrameworkunifiedStatusFail;
+
+ e_status = FrameworkunifiedGetMsgDataOfSize(h_app, send_msg, sz, eSMRRelease);
+ if (eFrameworkunifiedStatusOK != e_status) {
+ if (eFrameworkunifiedStatusInvldBufSize == e_status) {
+ FrameworkunifiedClearMsgData(h_app);
+ }
+ return eFrameworkunifiedStatusFail;
+ }
+
+ CanHalUnPackMessage(send_msg, &unpacked_h_app,
+ (void **)&unpacked_msg, &unpacked_sz);
+
+ CanCtlApiCmd send_can_data;
+ memset(&send_can_data, 0, sizeof(CanCtlApiCmd));
+ send_can_data.data[CANHAL_VALID_NUM_INDEX] = 1;
+ ret = PackageSendData(unpacked_msg, &(send_can_data.data[1]));
+ send_can_data.len = ret + 1;
+ CanSendResult send_result;
+ send_result.can_id = unpacked_msg->can_id;
+ send_result.rid = unpacked_msg->rid;
+
+ if (CAN_CTL_RET_SUCCESS == CanCtlApiSndCmd(&g_driver_obj, &send_can_data)) {
+ send_result.result = CAN_SEND_RESULT_SUCCESS;
+ } else {
+ send_result.result = CAN_SEND_RESULT_FAILURE;
+ }
+ FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Send status is %d.", send_result.result);
+
+ if (CANHAL_RET_NORMAL != CanHalSendStatus(CAN_HAL_TYPE_CAN,
+ unpacked_h_app, &send_result, sizeof(send_result))) {
+ CanHalDestroyPackedMessage(send_msg);
+ return eFrameworkunifiedStatusFail;
+ }
+
+ CanHalDestroyPackedMessage(send_msg);
+ return eFrameworkunifiedStatusOK;
+}
+
+/**
+ * param [in] send_msg
+ * param [out] send_can_data
+ */
+static UINT8 PackageSendData(const CanMessage *send_msg,
+ UINT8 *send_can_data) {
+ // package according to the rule of the data structure,
+ // refer to the line 108 and 109
+ send_can_data[CANHAL_CANID_HI_INDEX] = (send_msg->can_id & 0x7F0) >> 4;
+ send_can_data[CANHAL_CANID_LO_INDEX] = (send_msg->can_id & 0x0F) << 4;
+ send_can_data[CANHAL_DLC_INDEX] = send_msg->dlc;
+ memcpy(&(send_can_data[CANHAL_DATA_START_INDEX]),
+ send_msg->data,
+ send_msg->dlc);
+ return (send_msg->dlc + CANHAL_DATA_START_INDEX);
+}
+
+/**
+ * param [in] recv_data
+ * param [out] message
+ */
+static UINT8 PackageRecvData(const UINT8 *recv_data, CanMessage *message) {
+ if (CAN_NORMAL_MESSAGE_LEN < recv_data[CANHAL_DLC_INDEX]) {
+ // receive data's length is invalid
+ return CANHAL_RECV_DATA_LENGTH_INVALID;
+ }
+ // package according to the rule of the data structure,
+ // refer to the line 108 and 109
+ message->can_id = recv_data[CANHAL_CANID_HI_INDEX] & 0x7F;
+ message->can_id <<= 4;
+ message->can_id |= (recv_data[CANHAL_CANID_LO_INDEX] & 0xF0) >> 4;
+ message->dlc = recv_data[CANHAL_DLC_INDEX];
+ memcpy(message->data, &(recv_data[CANHAL_DATA_START_INDEX]), CAN_NORMAL_MESSAGE_LEN);
+
+ return CANHAL_RECV_DATABLOCK_SIZE;
+}
+
diff --git a/can_hal/src/can_hal_stm.cpp b/can_hal/src/can_hal_stm.cpp
new file mode 100644
index 00000000..fc24964a
--- /dev/null
+++ b/can_hal/src/can_hal_stm.cpp
@@ -0,0 +1,243 @@
+/*
+ * @copyright Copyright (c) 2017-2020 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 "can_hal_stm.h"
+#include "can_hal_core.h"
+#include "can_hal_internal.h"
+#include <native_service/ns_message_center_if.h>
+#include <native_service/frameworkunified_framework_if.h>
+#include <native_service/frameworkunified_multithreading.h>
+#include <string>
+#include <stdbool.h>
+#include <stdint.h>
+#include <assert.h>
+#include <pthread.h>
+
+// BSS section would be initialized to all 0.
+struct CanHalStateMachine {
+ CanHalType type;
+ HANDLE h_app;
+ bool can_hal_is_opened;
+ bool device_is_enabled;
+ struct {
+ HANDLE tx;
+ HANDLE tx_sender;
+ uint32_t tx_cmd;
+ const char *tx_name;
+ bool rx_initialized;
+ pthread_t rx;
+ pthread_attr_t rx_attr;
+ } internal;
+} can_hal_stm[NR_CAN_HAL_TYPES];
+
+bool TypeIsValid(enum CanHalType type) {
+ if (CAN_HAL_TYPE_CAN == type)
+ return true;
+
+ return false;
+}
+
+bool IsCanHalOpened(enum CanHalType type) {
+ return can_hal_stm[type].can_hal_is_opened;
+}
+
+void SetCanHalStateOpen(enum CanHalType type) {
+ can_hal_stm[type].can_hal_is_opened = true;
+}
+
+void SetCanHalStateClose(enum CanHalType type) {
+ can_hal_stm[type].can_hal_is_opened = false;
+}
+
+bool IsDeviceEnabled(enum CanHalType type) {
+ return can_hal_stm[type].device_is_enabled;
+}
+
+void SetDeviceStateEnable(enum CanHalType type) {
+ can_hal_stm[type].device_is_enabled = true;
+}
+
+void SetDeviceStateDisable(enum CanHalType type) {
+ can_hal_stm[type].device_is_enabled = false;
+}
+
+CANHAL_RET_API CanHalDestroyInternalThread(HANDLE h_app, enum CanHalType type) {
+ intptr_t ptr = (intptr_t)&(can_hal_stm[type].internal.tx_sender);
+
+ if (can_hal_stm[type].internal.tx) {
+ FrameworkunifiedStopChildThread(h_app, can_hal_stm[type].internal.tx, sizeof(ptr), &ptr);
+ FrameworkunifiedDestroyChildThread(h_app, can_hal_stm[type].internal.tx);
+ can_hal_stm[type].internal.tx = NULL;
+ }
+
+ if (can_hal_stm[type].internal.rx_initialized) {
+ pthread_cancel(can_hal_stm[type].internal.rx);
+ pthread_join(can_hal_stm[type].internal.rx, NULL);
+ can_hal_stm[type].internal.rx_initialized = false;
+ }
+ can_hal_stm[type].h_app = NULL;
+ return CANHAL_RET_NORMAL;
+}
+
+CANHAL_RET_API CanHalCreateInternalThread(HANDLE h_app, enum CanHalType type) {
+ enum CANHAL_RET_API ret = CANHAL_RET_ERR_PARAM;
+ EFrameworkunifiedStatus err = eFrameworkunifiedStatusOK;
+ intptr_t ptr = (intptr_t)&(can_hal_stm[type].internal.tx_sender);
+
+ can_hal_stm[type].h_app = h_app;
+ can_hal_stm[type].internal.tx_name = CANHAL_CAN_SEND_THREAD;
+ can_hal_stm[type].internal.tx = FrameworkunifiedCreateChildThread(h_app,
+ can_hal_stm[type].internal.tx_name,
+ CanSendThreadStart, CanSendThreadStop);
+ if (!can_hal_stm[type].internal.tx)
+ goto cleanup;
+
+ err = FrameworkunifiedStartChildThread(h_app,
+ can_hal_stm[type].internal.tx,
+ sizeof(ptr), &ptr);
+ if (err != eFrameworkunifiedStatusOK)
+ goto cleanup;
+
+ if (0 != pthread_attr_init(&(can_hal_stm[type].internal.rx_attr)))
+ goto cleanup;
+
+ if (0 != pthread_create(&(can_hal_stm[type].internal.rx),
+ &(can_hal_stm[type].internal.rx_attr), CanRecvRun,
+ (void *)&(can_hal_stm[type].type)))
+ goto cleanup;
+
+ can_hal_stm[type].internal.rx_initialized = true;
+ ret = CANHAL_RET_NORMAL;
+ return ret;
+cleanup:
+ if (can_hal_stm[type].internal.tx) {
+ FrameworkunifiedStopChildThread(h_app, can_hal_stm[type].internal.tx, sizeof(ptr), &ptr);
+ FrameworkunifiedDestroyChildThread(h_app, can_hal_stm[type].internal.tx);
+ can_hal_stm[type].internal.tx = NULL;
+ }
+
+ return ret;
+}
+
+CANHAL_RET_API CanHalInternalSend_CWORD118_(enum CanHalType type,
+ const void *msg, ssize_t sz) {
+ EFrameworkunifiedStatus e_status = FrameworkunifiedSendMsg(can_hal_stm[type].internal.tx_sender,
+ TX_INTERNAL__CWORD118_, sz, msg);
+ if (e_status != eFrameworkunifiedStatusOK) {
+ return CANHAL_RET_ERR_ERR;
+ }
+ return CANHAL_RET_NORMAL;
+}
+
+static EFrameworkunifiedStatus FrameworkunifiedSendMsgOneshot(HANDLE h_app, UI_32 cmd,
+ UI_32 l, PCVOID d) {
+ HANDLE h_client = NULL;
+ EFrameworkunifiedStatus err = eFrameworkunifiedStatusFail;
+
+ h_client = FrameworkunifiedMcOpenSender(h_app, FrameworkunifiedGetAppName(h_app));
+ if (!h_client)
+ return err;
+
+ err = FrameworkunifiedSendMsg(h_client, cmd, l, d);
+ FrameworkunifiedMcClose(h_client);
+ return err;
+}
+
+CANHAL_RET_API CanHalInternalSend(enum CanHalType type,
+ const void *msg, ssize_t sz) {
+ EFrameworkunifiedStatus e_status = FrameworkunifiedSendMsg(can_hal_stm[type].internal.tx_sender,
+ TX_INTERNAL, sz, msg);
+ if (e_status != eFrameworkunifiedStatusOK) {
+ return CANHAL_RET_ERR_ERR;
+ }
+ return CANHAL_RET_NORMAL;
+}
+
+
+CANHAL_RET_API InvokeStateCallback(enum CanHalType type) {
+ HANDLE sender = can_hal_stm[type].h_app;
+ uint32_t cmd = 0;
+ EFrameworkunifiedStatus err = eFrameworkunifiedStatusOK;
+ bool send = true;
+
+ switch (type) {
+ case CAN_HAL_TYPE_CAN:
+ cmd = CID_CANHAL_CMD_CAN_READY;
+ break;
+ default:
+ assert(0);
+ break;
+ }
+
+ err = FrameworkunifiedSendMsgOneshot(sender, cmd, sizeof(send), &send);
+ if (err != eFrameworkunifiedStatusOK)
+ return CANHAL_RET_ERR_ERR;
+ return CANHAL_RET_NORMAL;
+}
+
+CANHAL_RET_API InvokeErrorCallback(HANDLE h_app, enum CanHalType type) {
+ HANDLE sender = h_app;
+ uint32_t cmd = CID_CANHAL_CMD_ERROR_NOTIFY;
+ char msg[CANHAL_ERROR_MESSAGE_LEN] = {0};
+ ssize_t sz = sizeof(msg);
+ EFrameworkunifiedStatus err = eFrameworkunifiedStatusOK;
+
+ switch (type) {
+ case CAN_HAL_TYPE_CAN:
+ sprintf(msg, "Global CAN Stop");
+ break;
+ default:
+ assert(0);
+ break;
+ }
+
+ err = FrameworkunifiedSendMsgOneshot(sender, cmd, sz, msg);
+ if (err != eFrameworkunifiedStatusOK)
+ return CANHAL_RET_ERR_ERR;
+ return CANHAL_RET_NORMAL;
+}
+
+CANHAL_RET_API CanHalSendStatus(enum CanHalType type, HANDLE h_app,
+ const void *msg, ssize_t sz) {
+ EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
+ e_status = FrameworkunifiedSendMsgOneshot(h_app, CID_CANHAL_CMD_CAN_SEND_STATUS, sz, msg);
+ if (e_status != eFrameworkunifiedStatusOK) {
+ return CANHAL_RET_ERR_ERR;
+ }
+ return CANHAL_RET_NORMAL;
+}
+
+CANHAL_RET_API CanHalReceiveNotify(enum CanHalType type,
+ const void *msg, ssize_t sz) {
+ EFrameworkunifiedStatus e_status;
+ uint32_t cmd = 0;
+
+ switch (type) {
+ case CAN_HAL_TYPE_CAN:
+ cmd = CID_CANHAL_CMD_CAN_RECV;
+ break;
+ default:
+ assert(0);
+ break;
+ }
+
+ e_status = FrameworkunifiedSendMsgOneshot(can_hal_stm[type].h_app, cmd, sz, msg);
+ if (e_status != eFrameworkunifiedStatusOK) {
+ return CANHAL_RET_ERR_ERR;
+ }
+
+ return CANHAL_RET_NORMAL;
+}
diff --git a/can_hal/src/driver_can__CWORD31_.c b/can_hal/src/driver_can__CWORD31_.c
new file mode 100644
index 00000000..bd9cb291
--- /dev/null
+++ b/can_hal/src/driver_can__CWORD31_.c
@@ -0,0 +1,472 @@
+/*
+ * @copyright Copyright (c) 2020 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 <stdio.h>
+#include <string.h>
+#include "can_mng_api.h"
+
+#include <poll.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <linux/can/raw.h>
+#include <net/if.h>
+
+
+#define DUMP_DIR "/nv/driver-can"
+#define DUMP_RCVID "/nv/driver-can/dump_rcvid"
+#define CAN_DATA_SIZE (sizeof(CanData))
+
+static int fd_can = -1;
+static struct sockaddr_can tx_address;
+static CanCtlRcvId RcvId;
+const char* device_name = "vcan0";
+
+static UINT32 getData(CanCtlRcvId*, CanCtlApiCmd*);
+static UINT32 getCanData(unsigned char*);
+static UINT32 copyEnableCandata(UINT16* , unsigned char*, unsigned char*);
+static UINT32 isOpendDriver(void);
+
+UINT32 CanCtlApiOpen(CanCtlApiObj* pClientObj)
+{
+ int err_no;
+ int ret;
+
+ CAN_MNG_API_LOGT("FUNC IN");
+
+ /*----------------------------------------------------------------------*/
+ /* Clear Object */
+ /*----------------------------------------------------------------------*/
+ if (pClientObj != NULL) {
+ memset(pClientObj, 0, sizeof(*pClientObj));
+ }
+
+ if (fd_can != -1) {
+ CAN_MNG_API_LOGT("Already Opened");
+ CAN_MNG_API_LOGT("FUNC OUT");
+ return CAN_CTL_RET_SUCCESS;
+ }
+
+ // open socket
+ CAN_MNG_API_LOGT("open socket start");
+ struct ifreq ifr = {0};
+ fd_can = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+ err_no = errno;
+ CAN_MNG_API_LOGT("open socket end");
+ if (fd_can == -1) {
+ CAN_MNG_API_LOGE("Socket Open Error.");
+ CAN_MNG_API_LOGE("(errno[%d]).", err_no);
+ return CAN_CTL_RET_ERR_ERR;
+ }
+
+ // ioctl(SIOCGIFINDEX)
+ strcpy(ifr.ifr_name, device_name);
+ ret = ioctl(fd_can, SIOCGIFINDEX, &ifr);
+ err_no = errno;
+ if (ret < 0) {
+ CAN_MNG_API_LOGE("ioctl(SIOCGIFINDEX) Error.");
+ CAN_MNG_API_LOGE("(errno[%d]).", err_no);
+ (void)close(fd_can);
+ fd_can = -1;
+ return CAN_CTL_RET_ERR_ERR;
+ }
+
+ // bind
+ tx_address.can_family = AF_CAN;
+ tx_address.can_ifindex = ifr.ifr_ifindex;
+
+ ret = bind(fd_can, (struct sockaddr *)&tx_address, sizeof(tx_address));
+ err_no = errno;
+ if (ret < 0) {
+ CAN_MNG_API_LOGE("Socket Bind Error.");
+ CAN_MNG_API_LOGE("(errno[%d]).", err_no);
+ (void)close(fd_can);
+ fd_can = -1;
+ return -1;
+ }
+
+ // Initialize RcvId Map
+ memset(&RcvId, 0, sizeof(RcvId));
+
+ CAN_MNG_API_LOGT("FUNC OUT");
+
+ return CAN_CTL_RET_SUCCESS;
+}
+
+UINT32 CanCtlApiClose(CanCtlApiObj* pClientObj)
+{
+ /*----------------------------------------------------------------------*/
+ /* Clear Object */
+ /*----------------------------------------------------------------------*/
+ if (pClientObj != NULL) {
+ memset(pClientObj, 0, sizeof(*pClientObj));
+ }
+
+ if (fd_can != -1) {
+ (void)close(fd_can);
+ }
+
+ // Initialize fd info
+ fd_can = -1;
+
+ CAN_MNG_API_LOGT("FUNC OUT");
+
+ return CAN_CTL_RET_SUCCESS;
+}
+
+UINT32 CanCtlApiSndCmd(CanCtlApiObj* pClientObj, CanCtlApiCmd* pSndCmd)
+{
+ int i;
+ printf("%s: call_id=0x%x len=%d\n",
+ __func__, pClientObj->call_id, pSndCmd->len);
+ for (i = 0; i < pSndCmd->len; i++) {
+ printf("[%d]0x%x ", i, pSndCmd->data[i]);
+ if ((i != 0) && ((i % 7) == 0))
+ printf("\n");
+ }
+ printf("\n");
+
+
+ int ret;
+ int err_no;
+
+ CAN_MNG_API_LOGT("FUNC IN");
+ /*----------------------------------------------------------------------*/
+ /* Check Input Value */
+ /*----------------------------------------------------------------------*/
+ if (NULL == pSndCmd)
+ {
+ CAN_MNG_API_LOGE("%s(%p) invalid parameter.", __func__, pSndCmd);
+ return CAN_CTL_RET_ERR_PARAM;
+ }
+
+ ret = isOpendDriver();
+ if (ret != CAN_CTL_RET_SUCCESS) {
+ CAN_MNG_API_LOGE("Not Open Driver.");
+ return CAN_CTL_RET_ERR_ERR;
+ }
+
+ struct can_frame frame = {0};
+
+ frame.can_id = pSndCmd->data[1] & 0x7F;
+ frame.can_id <<= 4;
+ frame.can_id |= (pSndCmd->data[2] & 0xF0) >> 4;
+ frame.can_dlc = pSndCmd->data[3];
+ frame.data[0] = pSndCmd->data[4];
+ frame.data[1] = pSndCmd->data[5];
+ frame.data[2] = pSndCmd->data[6];
+ frame.data[3] = pSndCmd->data[7];
+ frame.data[4] = pSndCmd->data[8];
+ frame.data[5] = pSndCmd->data[9];
+ frame.data[6] = pSndCmd->data[10];
+ frame.data[7] = pSndCmd->data[11];
+
+ CAN_MNG_API_LOGT("write() CanDataExtSendNotif start size = %d", (int)sizeof(frame));
+ ret = write(fd_can, &frame, sizeof(frame));
+ err_no = errno;
+ CAN_MNG_API_LOGE("write() ret = %d).", ret);
+
+ CAN_MNG_API_LOGT("write() CanDataExtSendNotif end");
+ if (ret == -1) {
+ CAN_MNG_API_LOGE("write() error(errno[%d]).", err_no);
+ return CAN_CTL_RET_ERR_ERR;
+ }
+
+ CAN_MNG_API_LOGT("FUNC OUT");
+
+ return CAN_CTL_RET_SUCCESS;
+}
+
+UINT32 CanCtlApiSetRcvId(CanCtlApiObj* pClientObj, CanCtlRcvId* pRcvId)
+{
+ FILE* fp;
+ size_t nmemb;
+
+ CAN_MNG_API_LOGT("FUNC IN");
+ /*----------------------------------------------------------------------*/
+ /* Check Input Value */
+ /*----------------------------------------------------------------------*/
+ if (NULL == pRcvId)
+ {
+ CAN_MNG_API_LOGE("%s(%p) invalid parameter.", __func__, pRcvId);
+ return CAN_CTL_RET_ERR_PARAM;
+ }
+
+ struct stat st;
+ int ret = 0;
+ if (stat(DUMP_DIR, &st) != 0) {
+ ret = mkdir(DUMP_DIR, 0755);
+ }
+ if (ret != 0) {
+ CAN_MNG_API_LOGE("Can not Created RcvId Dump dir.");
+ return CAN_CTL_RET_ERR_ERR;
+ }
+ CAN_MNG_API_LOGT("fopen(DUMP_RCVID) start");
+ fp = fopen(DUMP_RCVID, "wb");
+ CAN_MNG_API_LOGT("fopen(DUMP_RCVID) end");
+ if (fp == NULL) {
+ CAN_MNG_API_LOGE("Can not Opened RcvId Dump file.");
+ return CAN_CTL_RET_ERR_ERR;
+ }
+
+ CAN_MNG_API_LOGT("fwrite(pRcvId) start");
+ nmemb = fwrite(pRcvId, sizeof(CanCtlRcvId), 1, fp);
+ CAN_MNG_API_LOGT("fwrite(pRcvId) end");
+ if (nmemb != 1) {
+ CAN_MNG_API_LOGE("RcvId Dump file write error.");
+ (void)fclose(fp);
+ return CAN_CTL_RET_ERR_ERR;
+ }
+
+ CAN_MNG_API_LOGT("fclose() start");
+ (void)fclose(fp);
+ CAN_MNG_API_LOGT("fclose() end");
+
+ CAN_MNG_API_LOGT("FUNC OUT");
+
+ return CAN_CTL_RET_SUCCESS;
+}
+
+UINT32 CanCtlApiRcvCmd(CanCtlApiObj* pClientObj, CanCtlApiCmd* pRcvCmd)
+{
+ int ret;
+ int enable_ret;
+
+ CanCtlRcvId InitialRcvId;
+ CanCtlRcvId RcvId;
+ FILE* fp;
+ size_t nmemb;
+
+ CAN_MNG_API_LOGT("FUNC IN");
+ /*----------------------------------------------------------------------*/
+ /* Check Input Value */
+ /*----------------------------------------------------------------------*/
+ if (NULL == pRcvCmd)
+ {
+ CAN_MNG_API_LOGE("%s(%p) invalid parameter.", __func__, pRcvCmd);
+ return CAN_CTL_RET_ERR_PARAM;
+ }
+
+ ret = isOpendDriver();
+ if (ret != CAN_CTL_RET_SUCCESS) {
+ CAN_MNG_API_LOGE("Not Open Driver.");
+ return CAN_CTL_RET_ERR_ERR;
+ }
+
+ // Restore RcvId
+ CAN_MNG_API_LOGT("fopen(DUMP_RCVID) start");
+ fp = fopen(DUMP_RCVID, "rb");
+ CAN_MNG_API_LOGT("fopen(DUMP_RCVID) end");
+ if (fp == NULL) {
+ CAN_MNG_API_LOGE("Can not Opened RcvId Dump file.");
+ return CAN_CTL_RET_ERR_ERR;
+ }
+
+ CAN_MNG_API_LOGT("fread(RcvId) start");
+ nmemb = fread(&RcvId, sizeof(CanCtlRcvId), 1, fp);
+ CAN_MNG_API_LOGT("fread(RcvId) end");
+ if (nmemb != 1) {
+ CAN_MNG_API_LOGE("RcvId Dump file read error.");
+ (void)fclose(fp);
+ return CAN_CTL_RET_ERR_ERR;
+ }
+
+ CAN_MNG_API_LOGT("fclose() start");
+ (void)fclose(fp);
+ CAN_MNG_API_LOGT("fclose() end");
+
+ memset(&InitialRcvId.id, 0, sizeof(InitialRcvId.id));
+
+ if (memcmp(InitialRcvId.id, RcvId.id, sizeof(RcvId.id)) == 0) {
+ CAN_MNG_API_LOGE("No RecvId Maps.");
+ return CAN_CTL_RET_ERR_ERR;
+ }
+
+ enable_ret = getData(&RcvId, pRcvCmd);
+
+ if (enable_ret != CAN_CTL_RET_SUCCESS) {
+ CAN_MNG_API_LOGE("No data founds.");
+ return enable_ret;
+ }
+
+ CAN_MNG_API_LOGT("FUNC OUT");
+
+ return CAN_CTL_RET_SUCCESS;
+}
+
+static UINT32 getData(CanCtlRcvId* pRcvId, CanCtlApiCmd* pRcvCmd) {
+ int ret;
+ int total_cnt = 0;
+ int copy_cnt = 0;
+ int i;
+ unsigned char can_data[CAN_CTL_CMD_LEN_MAX+1];
+ unsigned char enable_can_data[CAN_CTL_CMD_LEN_MAX];
+ unsigned char store_can_data[CAN_CTL_CMD_LEN_MAX];
+ unsigned char* scd;
+
+
+ UINT16 id_map[CAN_CTL_CMD_ID_HI_NUM];
+
+ CAN_MNG_API_LOGT("FUNC IN");
+
+ memcpy(id_map, pRcvId->id, sizeof(id_map));
+
+ memset(store_can_data, 0, sizeof(store_can_data));
+ scd = store_can_data;
+
+ while(1) {
+ memset(can_data, 0, sizeof(can_data));
+ memset(enable_can_data, 0, sizeof(enable_can_data));
+ ret = getCanData(can_data);
+ if (ret != CAN_CTL_RET_SUCCESS) {
+ // In case of error, read next data
+ CAN_MNG_API_LOGE("getCanData error.");
+ continue;
+ }
+
+ // Extract data of "CAN data extended reception notification 4"
+ copy_cnt = copyEnableCandata(id_map, can_data, enable_can_data);
+ if( copy_cnt == 0 ){
+ continue;
+ }
+
+ // Store in work buffer
+ for (i = 0; i < copy_cnt; i++) {
+ if (total_cnt >= CAN_CTL_MAX_RCV_CAN_SIZE) {
+ CAN_MNG_API_LOGE("buffer over");
+ break;
+ }
+ memcpy(scd, &enable_can_data[CAN_DATA_SIZE * i], CAN_DATA_SIZE);
+ scd = scd + CAN_DATA_SIZE;
+ total_cnt++;
+ }
+
+ // Check work buffer overflow
+ if (total_cnt >= CAN_CTL_MAX_RCV_CAN_SIZE) {
+ CAN_MNG_API_LOGE("buffer over");
+ break;
+ }
+ }
+
+ // If data exists, set and return
+ if (total_cnt != 0) {
+ pRcvCmd->len = 1 + (total_cnt * CAN_DATA_SIZE);
+ pRcvCmd->data[0] = total_cnt;
+ memcpy( &(pRcvCmd->data[1]), store_can_data, (total_cnt * CAN_DATA_SIZE));
+ } else {
+ // If there is no data, return with length 0
+ pRcvCmd->len = 0;
+ memset( &(pRcvCmd->data[0]), 0, sizeof(UINT8) * CAN_DAT_LEN_MAX );
+ CAN_MNG_API_LOGE("data not found");
+ }
+
+ CAN_MNG_API_LOGT("FUNC OUT");
+
+ return CAN_CTL_RET_SUCCESS;
+}
+
+static UINT32 getCanData(unsigned char *can_data) {
+ int err_no;
+ int i;
+
+ CAN_MNG_API_LOGT("FUNC IN");
+
+ CAN_MNG_API_LOGE("recvfrom start");
+
+ int nbytes = 0;
+ struct can_frame frame = {0};
+ socklen_t addrlen = sizeof(struct sockaddr_can);
+ nbytes = recvfrom(fd_can, &frame, sizeof(frame), 0, (struct sockaddr*)&tx_address, &addrlen);
+ err_no = errno;
+ CAN_MNG_API_LOGE("recvfrom end ret = %d",nbytes);
+ if (nbytes == -1) {
+ CAN_MNG_API_LOGE("Not Read CAN Driver(errno[%d]).", err_no);
+ return CAN_CTL_RET_ERR_ERR;
+ } else if (nbytes != CAN_MTU) {
+ CAN_MNG_API_LOGE("Receive Error size: %d.", nbytes);
+ return CAN_CTL_RET_ERR_ERR;
+ }
+
+ CAN_MNG_API_LOGT("Recvfrom CAN Data(start)");
+ can_data[0] = (frame.can_id & 0x7F0) >> 4;
+ can_data[1] = (frame.can_id & 0x0F) << 4;
+ can_data[2] = frame.can_dlc;
+ for (i = 0; i < frame.can_dlc; i++) {
+ can_data[3+i] = frame.data[i];
+ CAN_MNG_API_LOGE(" 0x%02x", frame.data[i]);
+ }
+ CAN_MNG_API_LOGT("");
+ CAN_MNG_API_LOGT("Recvfrom Data(end)");
+
+ CAN_MNG_API_LOGT("FUNC OUT");
+ return CAN_CTL_RET_SUCCESS;
+}
+
+static UINT32 copyEnableCandata(UINT16* id_map, unsigned char* can_data, unsigned char* enable_can_data) {
+ int id;
+ UINT16 val;
+ unsigned char* ecd = enable_can_data;
+ UINT32 count = 0;
+
+ CAN_MNG_API_LOGT("FUNC IN");
+ // Search data of "CAN data extended reception notification 4"
+ id = can_data[0];
+ val = can_data[1] >> 4; // Shift right 4 bits and determine search position
+
+ CAN_MNG_API_LOGE("data id =%x", id);
+ CAN_MNG_API_LOGE("data val=%x", val);
+ CAN_MNG_API_LOGE("file val=%x", id_map[id]);
+
+ // If the target data is found (If the bit is on ?)
+ if ((id_map[id] & (0x01 << val)) != 0) {
+ CAN_MNG_API_LOGT("matched.");
+ CAN_MNG_API_LOGE("id_map[%u]", id_map[id]);
+
+ // Store data in work buffer
+ memcpy(ecd, &can_data[0], CAN_DATA_SIZE);
+ count++;
+ }
+
+ CAN_MNG_API_LOGT("FUNC OUT");
+ return count;
+}
+
+UINT32 CanCtlApiRcvSpd(CanCtlApiObj* pClientObj, CanCtlApiCmd* pRcvCmd)
+{
+ // Note.
+ // If vendor needs the special implementation about receiving the vehicle speed,
+ // it should be implemented by vendor.
+ return CAN_CTL_RET_SUCCESS;
+}
+
+static UINT32 isOpendDriver() {
+ CAN_MNG_API_LOGT("FUNC IN");
+
+ if (fd_can == -1) {
+ CAN_MNG_API_LOGE("Not Open CAN Driver.");
+ return CAN_CTL_RET_ERR_ERR;
+ }
+
+ CAN_MNG_API_LOGT("FUNC OUT");
+ return CAN_CTL_RET_SUCCESS;
+}
diff --git a/can_hal/src/inc/can_hal_core.h b/can_hal/src/inc/can_hal_core.h
new file mode 100644
index 00000000..57a9c9d8
--- /dev/null
+++ b/can_hal/src/inc/can_hal_core.h
@@ -0,0 +1,37 @@
+/*
+ * @copyright Copyright (c) 2020 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.
+ */
+
+#ifndef _CAN_HAL_CORE_H_
+#define _CAN_HAL_CORE_H_
+#include <stdint.h>
+#include "can_hal.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+CANHAL_RET_API CanOpenCore(CanHalType type);
+CANHAL_RET_API CanCloseCore(CanHalType type);
+EFrameworkunifiedStatus CanSendThreadStart(HANDLE h_app);
+EFrameworkunifiedStatus CanSendThreadStop(HANDLE h_app);
+void *CanRecvRun(void *arg);
+void *CanHalPackMessage(HANDLE h_app, const void *msg, ssize_t msg_sz,
+ ssize_t *packed_sz);
+void CanHalUnPackMessage(void *packed, HANDLE *h_app, void **msg,
+ ssize_t *msg_sz);
+void CanHalDestroyPackedMessage(void *packed);
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/can_hal/src/inc/can_hal_frameworkunifiedlog.h b/can_hal/src/inc/can_hal_frameworkunifiedlog.h
new file mode 100644
index 00000000..f3a5f500
--- /dev/null
+++ b/can_hal/src/inc/can_hal_frameworkunifiedlog.h
@@ -0,0 +1,76 @@
+/*
+ * @copyright Copyright (c) 2018-2020 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.
+ */
+
+#ifndef INC_CAN_HAL_FRAMEWORKUNIFIEDLOG_H_
+#define INC_CAN_HAL_FRAMEWORKUNIFIEDLOG_H_
+
+#include <native_service/ns_logger_if.h>
+
+#define ZONE_INIT ZONEMASK(10)
+#define ZONE_FUNC ZONEMASK(11)
+#define ZONE_MEM ZONEMASK(12)
+#define ZONE_13 ZONEMASK(13)
+#define ZONE_14 ZONEMASK(14)
+#define ZONE_15 ZONEMASK(15)
+#define ZONE_16 ZONEMASK(16)
+#define ZONE_17 ZONEMASK(17)
+#define ZONE_18 ZONEMASK(18)
+#define ZONE_19 ZONEMASK(19)
+#define ZONE_20 ZONEMASK(20)
+#define ZONE_21 ZONEMASK(21)
+#define ZONE_22 ZONEMASK(22)
+#define ZONE_23 ZONEMASK(23)
+#define ZONE_24 ZONEMASK(24)
+#define ZONE_25 ZONEMASK(25)
+#define ZONE_26 ZONEMASK(26)
+#define ZONE_27 ZONEMASK(27)
+#define ZONE_28 ZONEMASK(28)
+#define ZONE_INFO ZONEMASK(29)
+#define ZONE_WARN ZONEMASK(30)
+#define ZONE_ERR ZONEMASK(31)
+
+#define ZONE_TEXT_10 "Init"
+#define ZONE_TEXT_11 "Function"
+#define ZONE_TEXT_12 "Memory"
+#define ZONE_TEXT_13 ""
+#define ZONE_TEXT_14 ""
+#define ZONE_TEXT_15 ""
+#define ZONE_TEXT_16 ""
+#define ZONE_TEXT_17 ""
+#define ZONE_TEXT_18 ""
+#define ZONE_TEXT_19 ""
+#define ZONE_TEXT_20 ""
+#define ZONE_TEXT_21 ""
+#define ZONE_TEXT_22 ""
+#define ZONE_TEXT_23 ""
+#define ZONE_TEXT_24 ""
+#define ZONE_TEXT_25 ""
+#define ZONE_TEXT_26 ""
+#define ZONE_TEXT_27 ""
+#define ZONE_TEXT_28 ""
+#define ZONE_TEXT_29 "Info"
+#define ZONE_TEXT_30 "Warning"
+#define ZONE_TEXT_31 "Error"
+
+#ifndef FRAMEWORKUNIFIEDLOGOPTIONS
+#define FRAMEWORKUNIFIEDLOGOPTIONS (LSHAREDMEM) // LPRINT , LMSGQ, LSLOGGER
+#endif
+
+#ifndef FRAMEWORKUNIFIEDLOGAPPZONES
+#define FRAMEWORKUNIFIEDLOGAPPZONES ZONE_ERR, ZONE_WARN, ZONE_INFO
+#endif
+
+#endif // INC_CAN_HAL_FRAMEWORKUNIFIEDLOG_H_
diff --git a/can_hal/src/inc/can_hal_internal.h b/can_hal/src/inc/can_hal_internal.h
new file mode 100644
index 00000000..f42a73f7
--- /dev/null
+++ b/can_hal/src/inc/can_hal_internal.h
@@ -0,0 +1,31 @@
+/*
+ * @copyright Copyright (c) 2018-2020 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.
+ */
+
+#ifndef INC_CAN_HAL_INTERNAL_H_
+#define INC_CAN_HAL_INTERNAL_H_
+
+/**
+ * \~english Name of the CAN send thread
+ */
+#define CANHAL_CAN_SEND_THREAD "HalCANSend"
+
+enum CanHalInternalCmd {
+ INVALID_CMD = 0,
+ TX_INTERNAL,
+ TX_INTERNAL__CWORD118_
+};
+
+#endif // INC_CAN_HAL_INTERNAL_H_
diff --git a/can_hal/src/inc/can_hal_stm.h b/can_hal/src/inc/can_hal_stm.h
new file mode 100644
index 00000000..cd078140
--- /dev/null
+++ b/can_hal/src/inc/can_hal_stm.h
@@ -0,0 +1,45 @@
+/*
+ * @copyright Copyright (c) 2020 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.
+ */
+
+#ifndef _CAN_HAL_STM_H_
+#define _CAN_HAL_STM_H_
+#include "can_hal.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+bool TypeIsValid(enum CanHalType type);
+bool IsCanHalOpened(enum CanHalType type);
+void SetCanHalStateOpen(enum CanHalType type);
+void SetCanHalStateClose(enum CanHalType type);
+bool IsDeviceEnabled(enum CanHalType type);
+void SetDeviceStateEnable(enum CanHalType type);
+void SetDeviceStateDisable(enum CanHalType type);
+CANHAL_RET_API InvokeStateCallback(enum CanHalType type);
+CANHAL_RET_API InvokeErrorCallback(HANDLE h_app, enum CanHalType type);
+CANHAL_RET_API CanHalInternalSend(enum CanHalType type,
+ const void *msg, ssize_t sz);
+CANHAL_RET_API CanHalInternalSend_CWORD118_(enum CanHalType type,
+ const void *msg, ssize_t sz);
+CANHAL_RET_API CanHalSendStatus(enum CanHalType type, HANDLE h_app,
+ const void *msg, ssize_t sz);
+CANHAL_RET_API CanHalReceiveNotify(enum CanHalType type,
+ const void *msg, ssize_t sz);
+CANHAL_RET_API CanHalCreateInternalThread(HANDLE h_app, enum CanHalType type);
+CANHAL_RET_API CanHalDestroyInternalThread(HANDLE h_app, enum CanHalType type);
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/can_hal/src/inc/can_mng_api.h b/can_hal/src/inc/can_mng_api.h
new file mode 100644
index 00000000..ef03750f
--- /dev/null
+++ b/can_hal/src/inc/can_mng_api.h
@@ -0,0 +1,120 @@
+/*
+ * @copyright Copyright (c) 2020 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.
+ */
+
+#ifndef __CAN_MNG_API_H__
+#define __CAN_MNG_API_H__
+
+/** @includes
+ *
+ */
+
+/** @define
+ *
+ */
+#ifndef UINT8
+#define UINT8 unsigned char
+#endif
+#ifndef UINT16
+#define UINT16 unsigned short
+#endif
+#ifndef INT32
+#define INT32 int
+#endif
+#ifndef UINT32
+#define UINT32 unsigned int
+#endif
+
+/*------------------------------------------*/
+/* Log */
+/*------------------------------------------*/
+#if defined(CAN_MNG_API_FORCE_DEBUG)
+ #define CAN_MNG_API_LOGT(fmt,...) fprintf(stderr, "[T][CAN_API] %s(%s)(%d):" fmt "\n", __FILE__, __func__, __LINE__, ## __VA_ARGS__)
+ #define CAN_MNG_API_LOGD(fmt,...) fprintf(stderr, "[D][CAN_API] %s(%s)(%d):" fmt "\n", __FILE__, __func__, __LINE__, ## __VA_ARGS__)
+ #define CAN_MNG_API_LOGE(fmt,...) fprintf(stderr, "[E][CAN_API] %s(%s)(%d):" fmt "\n", __FILE__, __func__, __LINE__, ## __VA_ARGS__)
+#else
+ #define CAN_MNG_API_LOGT(fmt,...)
+ #define CAN_MNG_API_LOGD(fmt,...)
+ #define CAN_MNG_API_LOGE(fmt,...)
+#endif
+/*------------------------------------------*/
+/* API return value */
+/*------------------------------------------*/
+#define CAN_CTL_RET_SUCCESS 0 /* Normal end */
+#define CAN_CTL_RET_ERR_PARAM -1 /* Parameter error */
+#define CAN_CTL_RET_ERR_NOSVC -2 /* Service not open error */
+#define CAN_CTL_RET_ERR_ERR -3 /* Other error */
+
+/*------------------------------------------*/
+/* Received command information */
+/*------------------------------------------*/
+#define CAN_CTL_CMD_LEN_MAX 255 /* Maximum data length */
+#define CAN_DAT_LEN_MAX CAN_CTL_CMD_LEN_MAX /* Maximum data length */
+#define CAN_CTL_CMD_ID_HI_NUM 128 /* Number of higher CAN-ID(8bit) (00 to 7F) */
+
+#define CAN_CTL_MAX_RCV_CAN_SIZE 23
+
+/** @typedefs
+ *
+ */
+/* Object for CAN communication control API */
+typedef struct _CanCtlApiObj {
+ /* ID assigned to the command reception notification callback */
+ INT32 call_id;
+} CanCtlApiObj ;
+
+/* CAN command structure */
+typedef struct _CanCtlApiCmd {
+ UINT8 len; /* Data length */
+ UINT8 data[CAN_DAT_LEN_MAX]; /* Data */
+} CanCtlApiCmd;
+
+/* Received ID MAP information */
+typedef struct _CanCtlRcvId {
+ UINT16 id[CAN_CTL_CMD_ID_HI_NUM]; /* CAN ID MAP(000~7FF) */
+} CanCtlRcvId;
+
+
+/* CAN Frame structure */
+typedef union {
+ UINT8 Data[2];
+} CanId;
+
+#define LCN_SRV_MSB_FRAME_DAT_SIZE (8) /* MSB send/receive frame data size */
+
+typedef struct _CanData {
+ CanId id; /* CAN Frame structure */
+ UINT8 dlc; /* Valid bytes of DATA#1~#8. DLC is 1 to 8. */
+ UINT8 Data[LCN_SRV_MSB_FRAME_DAT_SIZE]; /* Receive data */
+ /* Data of DLC size is valid, otherwise set to 0 as invalid value. */
+} CanData;
+
+/** @forward declarations
+ *
+ */
+/* API prototype */
+#ifdef __cplusplus
+extern "C" {
+#endif
+UINT32 CanCtlApiOpen(CanCtlApiObj* pClientObj);
+UINT32 CanCtlApiClose(CanCtlApiObj* pClientObj);
+UINT32 CanCtlApiSndCmd(CanCtlApiObj* pClientObj, CanCtlApiCmd* pSndCmd);
+UINT32 CanCtlApiSetRcvId(CanCtlApiObj* pClientObj, CanCtlRcvId* pRcvId);
+UINT32 CanCtlApiRcvCmd(CanCtlApiObj* pClientObj, CanCtlApiCmd* pRcvCmd);
+UINT32 CanCtlApiRcvSpd(CanCtlApiObj* pClientObj, CanCtlApiCmd* pRcvCmd);
+#ifdef __cplusplus
+}
+#endif
+#endif /* __CAN_MNG_API_H__ */