2 * @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * This source code is a sample source code .
20 * Implementation of the function must be performed by the vendor.
23 #include "can_hal_core.h"
24 #include "can_hal_stm.h"
30 CANHAL_RET_API CanOpen(HANDLE h_app, CanHalType type) {
31 CANHAL_RET_API ret = CANHAL_RET_ERR_ERR;
33 if (!TypeIsValid(type)) {
34 return CANHAL_RET_ERR_PARAM;
38 return CANHAL_RET_ERR_PARAM;
41 if (IsCanHalOpened(type)) {
42 return CANHAL_RET_NORMAL;
45 if (IsDeviceEnabled(type)) {
46 return CANHAL_RET_NORMAL;
49 ret = CanOpenCore(type);
50 if (CANHAL_RET_NORMAL != ret) {
54 ret = CanHalCreateInternalThread(h_app, type);
55 if (CANHAL_RET_NORMAL != ret) {
60 SetDeviceStateEnable(type);
61 SetCanHalStateOpen(type);
62 InvokeStateCallback(type);
66 CANHAL_RET_API CanClose(HANDLE h_app, CanHalType type) {
67 CANHAL_RET_API ret = CANHAL_RET_ERR_ERR;
68 if (!TypeIsValid(type)) {
69 return CANHAL_RET_ERR_PARAM;
73 return CANHAL_RET_ERR_PARAM;
76 if (!IsCanHalOpened(type)) {
77 return CANHAL_RET_NORMAL;
80 if (IsDeviceEnabled(type)) {
81 ret = CanCloseCore(type);
82 if (CANHAL_RET_NORMAL != ret)
86 ret = CanHalDestroyInternalThread(h_app, type);
87 if (CANHAL_RET_NORMAL != ret)
90 SetDeviceStateDisable(type);
91 SetCanHalStateClose(type);
92 InvokeErrorCallback(h_app, type);
97 static CANHAL_RET_API CanSendCommonCheck(const void *message,
98 enum CanHalType type) {
99 if (!TypeIsValid(type)) {
100 return CANHAL_RET_ERR_PARAM;
104 return CANHAL_RET_ERR_PARAM;
107 if (!IsCanHalOpened(type)) {
108 return CANHAL_RET_ERR_STATE;
111 if (!IsDeviceEnabled(type)) {
112 return CANHAL_RET_ERR_STATE;
115 return CANHAL_RET_NORMAL;
118 CANHAL_RET_API CanSend(HANDLE h_app,
119 const CanMessage *message, enum CanHalType type) {
120 CANHAL_RET_API ret = CANHAL_RET_ERR_ERR;
121 void *_message = NULL;
124 ret = CanSendCommonCheck(message, type);
125 if (ret != CANHAL_RET_NORMAL)
129 return CANHAL_RET_ERR_PARAM;
132 if (CAN_NORMAL_MESSAGE_LEN < message->dlc) {
133 return CANHAL_RET_ERR_PARAM;
136 _message = CanHalPackMessage(h_app, (const void *)message,
137 sizeof(CanMessage), &sz);
139 return CANHAL_RET_ERR_ERR;
141 ret = CanHalInternalSend(type, _message, sz);
142 CanHalDestroyPackedMessage(_message);
146 CANHAL_RET_API CanGetVersion(HANDLE h_app, std::string *p_version) {
147 CanHalType type = CAN_HAL_TYPE_CAN;
149 return CANHAL_RET_ERR_PARAM;
152 if (!IsCanHalOpened(type)) {
153 return CANHAL_RET_ERR_STATE;
156 if (!IsDeviceEnabled(type)) {
157 return CANHAL_RET_ERR_STATE;
160 if (NULL == p_version) {
161 return CANHAL_RET_ERR_PARAM;
165 return CANHAL_RET_NORMAL;