Init basesystem source codes.
[staging/basesystem.git] / peripheralservice / communication / server / src / threads / CAN_Thread.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "CAN_Thread.h"
18
19 extern "C" {
20 #include <poll.h>
21 #include <sys/timerfd.h>
22 }
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>
28 #include <can_hal.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <agl_thread.h>
33 #include <string>
34 #include "CAN_Delivery.h"
35 #include "CAN_CommWatch.h"
36 #include "CAN_TxMsg.h"
37
38 #include "communication_communicationlog.h"
39 #include "communication_cid.h"
40 #include "Thread_Common.h"
41
42 static EFrameworkunifiedStatus CANStateCallback(HANDLE h_app);
43 static EFrameworkunifiedStatus CANHalRcvProcess(HANDLE h_app);
44 static EFrameworkunifiedStatus CANHalSndStsProcess(HANDLE h_app);
45
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},
58 };
59
60 static UI_32 kCanPcbhsD[] = {
61     CID_CANIF_DELIVERY_ENTRY,
62     CID_CANIF_DELIVERY_ERASE,
63     CID_COMMSYS_TIMEOUT,
64     CID_CANIF_TX_START,
65     CID_CANIF_TX_BIT_START,
66     CID_CANIF_COMM_WATCH,
67     CID_CANIF_CMD_CTRL,
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
72 };
73
74 static EFrameworkunifiedStatus CANStateCallback(HANDLE h_app) {
75   bool result;
76   EFrameworkunifiedStatus err = eFrameworkunifiedStatusOK;
77
78   err = FrameworkunifiedGetMsgDataOfSize(h_app, &result, sizeof(result), eSMRRelease);
79   if (err != eFrameworkunifiedStatusOK) {
80     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: FrameworkunifiedGetMsgDataOfSize failed");
81   }
82   if (err == eFrameworkunifiedStatusInvldBufSize) {
83     FrameworkunifiedClearMsgData(h_app);
84     return eFrameworkunifiedStatusFail;
85   } else if (err != eFrameworkunifiedStatusOK) {
86     return eFrameworkunifiedStatusFail;
87   }
88
89   CommSetAvailabilityCurrent(CAN_AVAILABILITY);
90   return CommonStartNotify(h_app, NTFY_Communication_CAN_ISAVAILABLE);
91 }
92
93 static EFrameworkunifiedStatus CANThreadCanOpen(HANDLE h_app) {
94   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
95   const char *result = "Unknown";
96   const char *called = "Unknown";
97   UI_16 z = ZONE_ERR;
98
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;
102   called = "CanOpen";
103   FRAMEWORKUNIFIEDLOG(z, __func__, "%s %s", called, result);
104   return e_status;
105 }
106
107 EFrameworkunifiedStatus CANThreadStart(HANDLE h_app) {
108   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
109
110   CommClrAvailabilityCurrent(CAN_AVAILABILITY);
111   CANDeliveryInit();
112   CANCommWatchInit();
113   CANTxMsgInit();
114
115   e_status = CommonThreadStart(h_app, kCanPcbhs, _countof(kCanPcbhs),
116                           NTFY_Communication_CAN_ISAVAILABLE, CANThreadCanOpen);
117   return e_status;
118 }
119
120 static EFrameworkunifiedStatus CANThreadCanClose(HANDLE h_app) {
121   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
122   const char *result = "Unknown";
123   const char *called = "Unknown";
124   UI_16 z = ZONE_ERR;
125
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;
129   called = "CanClose";
130   FRAMEWORKUNIFIEDLOG(z, __func__, "%s %s", called, result);
131   return e_status;
132 }
133
134 EFrameworkunifiedStatus CANThreadStop(HANDLE h_app) {
135   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
136
137   CommClrAvailabilityCurrent(CAN_AVAILABILITY);
138   e_status = CommonThreadStop(h_app, kCanPcbhsD, _countof(kCanPcbhsD),
139                               NTFY_Communication_CAN_ISAVAILABLE,
140                               CANThreadCanClose);
141   return e_status;
142 }
143
144 static EFrameworkunifiedStatus CANHalRcvProcess(HANDLE h_app) {
145   CanMessage canhal_recv_data;
146   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusFail;
147   uint8_t cmd = 0;
148
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);
153   }
154
155   if (e_status == eFrameworkunifiedStatusInvldBufSize) {
156     FrameworkunifiedClearMsgData(h_app);
157     goto cleanup;
158   } else if (e_status != eFrameworkunifiedStatusOK) {
159     goto cleanup;
160   }
161
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;
165   }
166
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());
171
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);
176     break;
177   case CAN_CMDID_MRST_INFO_RESP_RX:
178     e_status = CANCommandDeliveryRcvProcess(h_app, &canhal_recv_data, cmd);
179     break;
180   case CAN_CMDID_VERSION_RESP_RX:
181     e_status = CANCommandDeliveryRcvProcess(h_app, &canhal_recv_data, cmd);
182     break;
183   case CAN_CMDID_CONNECTION_NODE_RESP_RX:
184     e_status = CANCommandDeliveryRcvProcess(h_app, &canhal_recv_data, cmd);
185     break;
186   case CAN_CMDID_FUELCALC_RST_REQ_RX:
187     CANCommandSetFuelCalcRstReq();
188     e_status = CANCommandDeliveryRcvProcess(h_app, &canhal_recv_data, cmd);
189     break;
190   default:
191     e_status = CANDeliveryRcvProcess(h_app, &canhal_recv_data);
192   }
193 cleanup:
194   return e_status;
195 }
196
197 static EFrameworkunifiedStatus CANHalSndStsProcess(HANDLE h_app) {
198   CanSendResult canhal_recv_data;
199   EFrameworkunifiedStatus err = eFrameworkunifiedStatusOK;
200   uint8_t ui_can_rid;
201   PS_CommunicationProtocol cid;
202
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);
207   }
208
209   if (err == eFrameworkunifiedStatusInvldBufSize) {
210     FrameworkunifiedClearMsgData(h_app);
211     return eFrameworkunifiedStatusFail;
212   } else if (err != eFrameworkunifiedStatusOK) {
213     return eFrameworkunifiedStatusFail;
214   }
215
216   ui_can_rid = canhal_recv_data.rid;
217   if (CAN_RID_NOTUSE_CODE == ui_can_rid) {
218     return eFrameworkunifiedStatusOK;
219   }
220
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;
226     break;
227   default:
228     cid = CID_CAN_TX_RESULT;
229     break;
230   }
231
232   return CANSndStsProcess(h_app, &canhal_recv_data, cid);
233 }
234
235 EFrameworkunifiedStatus CANCallbackForTimeOut(HANDLE h_app) {
236   CANCommWatchTimeout(h_app);
237   return eFrameworkunifiedStatusOK;
238 }