2 * @copyright Copyright (c) 2016-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.
17 #include "CAN_Thread.h"
21 #include <sys/timerfd.h>
23 #include <native_service/frameworkunified_types.h>
24 #include <native_service/frameworkunified_framework_if.h>
25 #include <native_service/frameworkunified_timer.h>
26 #include <peripheral_service/Canif_API.h>
27 #include <peripheral_service/communication_notifications.h>
32 #include <agl_thread.h>
34 #include "CAN_Delivery.h"
35 #include "CAN_CommWatch.h"
36 #include "CAN_TxMsg.h"
38 #include "communication_communicationlog.h"
39 #include "communication_cid.h"
40 #include "Thread_Common.h"
42 static EFrameworkunifiedStatus CANStateCallback(HANDLE h_app);
43 static EFrameworkunifiedStatus CANHalRcvProcess(HANDLE h_app);
44 static EFrameworkunifiedStatus CANHalSndStsProcess(HANDLE h_app);
46 static const FrameworkunifiedProtocolCallbackHandler kCanPcbhs[] = {
47 {CID_CANIF_DELIVERY_ENTRY, CANDeliveryEntry},
48 {CID_CANIF_DELIVERY_ERASE, CANClearEntry},
49 {CID_COMMSYS_TIMEOUT, CANCallbackForTimeOut},
50 {CID_CANIF_TX_START, CANTxMsg},
51 {CID_CANIF_TX_BIT_START, CANTxMsgBit},
52 {CID_CANIF_COMM_WATCH, CANCommWatch},
53 {CID_CANIF_CMD_CTRL, CANTxMsgCommand},
54 {CID_CANHAL_CMD_CAN_READY, CANStateCallback},
55 {CID_CANHAL_CMD_ERROR_NOTIFY, CommonCanHalErrorNotify},
56 {CID_CANHAL_CMD_CAN_RECV, CANHalRcvProcess},
57 {CID_CANHAL_CMD_CAN_SEND_STATUS, CANHalSndStsProcess},
60 static UI_32 kCanPcbhsD[] = {
61 CID_CANIF_DELIVERY_ENTRY,
62 CID_CANIF_DELIVERY_ERASE,
65 CID_CANIF_TX_BIT_START,
68 CID_CANHAL_CMD_CAN_READY,
69 CID_CANHAL_CMD_ERROR_NOTIFY,
70 CID_CANHAL_CMD_CAN_RECV,
71 CID_CANHAL_CMD_CAN_SEND_STATUS
74 static EFrameworkunifiedStatus CANStateCallback(HANDLE h_app) {
76 EFrameworkunifiedStatus err = eFrameworkunifiedStatusOK;
78 err = FrameworkunifiedGetMsgDataOfSize(h_app, &result, sizeof(result), eSMRRelease);
79 if (err != eFrameworkunifiedStatusOK) {
80 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: FrameworkunifiedGetMsgDataOfSize failed");
82 if (err == eFrameworkunifiedStatusInvldBufSize) {
83 FrameworkunifiedClearMsgData(h_app);
84 return eFrameworkunifiedStatusFail;
85 } else if (err != eFrameworkunifiedStatusOK) {
86 return eFrameworkunifiedStatusFail;
89 CommSetAvailabilityCurrent(CAN_AVAILABILITY);
90 return CommonStartNotify(h_app, NTFY_Communication_CAN_ISAVAILABLE);
93 static EFrameworkunifiedStatus CANThreadCanOpen(HANDLE h_app) {
94 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
95 const char *result = "Unknown";
96 const char *called = "Unknown";
99 e_status = ConvRet(CanOpen(h_app, CAN_HAL_TYPE_CAN));
100 result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
101 z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
103 FRAMEWORKUNIFIEDLOG(z, __func__, "%s %s", called, result);
107 EFrameworkunifiedStatus CANThreadStart(HANDLE h_app) {
108 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
110 CommClrAvailabilityCurrent(CAN_AVAILABILITY);
115 e_status = CommonThreadStart(h_app, kCanPcbhs, _countof(kCanPcbhs),
116 NTFY_Communication_CAN_ISAVAILABLE, CANThreadCanOpen);
120 static EFrameworkunifiedStatus CANThreadCanClose(HANDLE h_app) {
121 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
122 const char *result = "Unknown";
123 const char *called = "Unknown";
126 e_status = ConvRet(CanClose(h_app, CAN_HAL_TYPE_CAN));
127 result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
128 z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
130 FRAMEWORKUNIFIEDLOG(z, __func__, "%s %s", called, result);
134 EFrameworkunifiedStatus CANThreadStop(HANDLE h_app) {
135 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
137 CommClrAvailabilityCurrent(CAN_AVAILABILITY);
138 e_status = CommonThreadStop(h_app, kCanPcbhsD, _countof(kCanPcbhsD),
139 NTFY_Communication_CAN_ISAVAILABLE,
144 static EFrameworkunifiedStatus CANHalRcvProcess(HANDLE h_app) {
145 CanMessage canhal_recv_data;
146 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusFail;
149 e_status = FrameworkunifiedGetMsgDataOfSize(h_app, &canhal_recv_data,
150 sizeof(canhal_recv_data), eSMRRelease);
151 if (e_status != eFrameworkunifiedStatusOK) {
152 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error : FrameworkunifiedGetMsgDataOfSize is failed, e_status=%d", e_status);
155 if (e_status == eFrameworkunifiedStatusInvldBufSize) {
156 FrameworkunifiedClearMsgData(h_app);
158 } else if (e_status != eFrameworkunifiedStatusOK) {
162 if (canhal_recv_data.dlc > CAN_MESSAGE_LEN) {
163 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error : Invalid data length, dlc=%d", canhal_recv_data.dlc);
164 return eFrameworkunifiedStatusFail;
167 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "can_id=%x dlc=%d rid=%x",
168 canhal_recv_data.can_id, canhal_recv_data.dlc, canhal_recv_data.rid);
169 FRAMEWORKUNIFIEDLOG(ZONE_DEBUG, __func__, "msg_data=%s",
170 MessageDataOutputLog(canhal_recv_data.data, canhal_recv_data.dlc).c_str());
172 cmd = (uint8_t)(canhal_recv_data.can_id & 0xFF);
173 switch (canhal_recv_data.can_id) {
174 case CAN_CMDID_STARTUP_FIN_RESP_RX:
175 e_status = CANCommandDeliveryRcvProcess(h_app, &canhal_recv_data, cmd);
177 case CAN_CMDID_MRST_INFO_RESP_RX:
178 e_status = CANCommandDeliveryRcvProcess(h_app, &canhal_recv_data, cmd);
180 case CAN_CMDID_VERSION_RESP_RX:
181 e_status = CANCommandDeliveryRcvProcess(h_app, &canhal_recv_data, cmd);
183 case CAN_CMDID_CONNECTION_NODE_RESP_RX:
184 e_status = CANCommandDeliveryRcvProcess(h_app, &canhal_recv_data, cmd);
186 case CAN_CMDID_FUELCALC_RST_REQ_RX:
187 CANCommandSetFuelCalcRstReq();
188 e_status = CANCommandDeliveryRcvProcess(h_app, &canhal_recv_data, cmd);
191 e_status = CANDeliveryRcvProcess(h_app, &canhal_recv_data);
197 static EFrameworkunifiedStatus CANHalSndStsProcess(HANDLE h_app) {
198 CanSendResult canhal_recv_data;
199 EFrameworkunifiedStatus err = eFrameworkunifiedStatusOK;
201 PS_CommunicationProtocol cid;
203 err = FrameworkunifiedGetMsgDataOfSize(h_app, &canhal_recv_data,
204 sizeof(canhal_recv_data), eSMRRelease);
205 if (err != eFrameworkunifiedStatusOK) {
206 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error : FrameworkunifiedGetMsgDataOfSize is failed, err=%d", err);
209 if (err == eFrameworkunifiedStatusInvldBufSize) {
210 FrameworkunifiedClearMsgData(h_app);
211 return eFrameworkunifiedStatusFail;
212 } else if (err != eFrameworkunifiedStatusOK) {
213 return eFrameworkunifiedStatusFail;
216 ui_can_rid = canhal_recv_data.rid;
217 if (CAN_RID_NOTUSE_CODE == ui_can_rid) {
218 return eFrameworkunifiedStatusOK;
221 switch (canhal_recv_data.can_id) {
222 case CAN_CMDID_MRST_INFO_REQ_TX:
223 case CAN_CMDID_CONNECTION_NODE_REQ_TX:
224 case CAN_CMDID_FUELCALC_REQ_TX:
225 cid = CID_CAN_CMD_TX_RESULT;
228 cid = CID_CAN_TX_RESULT;
232 return CANSndStsProcess(h_app, &canhal_recv_data, cid);
235 EFrameworkunifiedStatus CANCallbackForTimeOut(HANDLE h_app) {
236 CANCommWatchTimeout(h_app);
237 return eFrameworkunifiedStatusOK;