summaryrefslogtreecommitdiffstats
path: root/peripheralservice/communication/server/src/threads/CAN_Thread.cpp
blob: 1b327e7bb2908386ed089883e3440db2a206e84a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * @copyright Copyright (c) 2016-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_Thread.h"

extern "C" {
#include <poll.h>
#include <sys/timerfd.h>
}
#include <native_service/frameworkunified_types.h>
#include <native_service/frameworkunified_framework_if.h>
#include <native_service/frameworkunified_timer.h>
#include <peripheral_service/Canif_API.h>
#include <peripheral_service/communication_notifications.h>
#include <can_hal.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <agl_thread.h>
#include <string>
#include "CAN_Delivery.h"
#include "CAN_CommWatch.h"
#include "CAN_TxMsg.h"

#include "communication_communicationlog.h"
#include "communication_cid.h"
#include "Thread_Common.h"

static EFrameworkunifiedStatus CANStateCallback(HANDLE h_app);
static EFrameworkunifiedStatus CANHalRcvProcess(HANDLE h_app);
static EFrameworkunifiedStatus CANHalSndStsProcess(HANDLE h_app);

static const FrameworkunifiedProtocolCallbackHandler kCanPcbhs[] = {
    {CID_CANIF_DELIVERY_ENTRY, CANDeliveryEntry},
    {CID_CANIF_DELIVERY_ERASE, CANClearEntry},
    {CID_COMMSYS_TIMEOUT, CANCallbackForTimeOut},
    {CID_CANIF_TX_START, CANTxMsg},
    {CID_CANIF_TX_BIT_START, CANTxMsgBit},
    {CID_CANIF_COMM_WATCH, CANCommWatch},
    {CID_CANIF_CMD_CTRL, CANTxMsgCommand},
    {CID_CANHAL_CMD_CAN_READY, CANStateCallback},
    {CID_CANHAL_CMD_ERROR_NOTIFY, CommonCanHalErrorNotify},
    {CID_CANHAL_CMD_CAN_RECV, CANHalRcvProcess},
    {CID_CANHAL_CMD_CAN_SEND_STATUS, CANHalSndStsProcess},
};

static UI_32 kCanPcbhsD[] = {
    CID_CANIF_DELIVERY_ENTRY,
    CID_CANIF_DELIVERY_ERASE,
    CID_COMMSYS_TIMEOUT,
    CID_CANIF_TX_START,
    CID_CANIF_TX_BIT_START,
    CID_CANIF_COMM_WATCH,
    CID_CANIF_CMD_CTRL,
    CID_CANHAL_CMD_CAN_READY,
    CID_CANHAL_CMD_ERROR_NOTIFY,
    CID_CANHAL_CMD_CAN_RECV,
    CID_CANHAL_CMD_CAN_SEND_STATUS
};

static EFrameworkunifiedStatus CANStateCallback(HANDLE h_app) {
  bool result;
  EFrameworkunifiedStatus err = eFrameworkunifiedStatusOK;

  err = FrameworkunifiedGetMsgDataOfSize(h_app, &result, sizeof(result), eSMRRelease);
  if (err != eFrameworkunifiedStatusOK) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error: FrameworkunifiedGetMsgDataOfSize failed");
  }
  if (err == eFrameworkunifiedStatusInvldBufSize) {
    FrameworkunifiedClearMsgData(h_app);
    return eFrameworkunifiedStatusFail;
  } else if (err != eFrameworkunifiedStatusOK) {
    return eFrameworkunifiedStatusFail;
  }

  CommSetAvailabilityCurrent(CAN_AVAILABILITY);
  return CommonStartNotify(h_app, NTFY_Communication_CAN_ISAVAILABLE);
}

static EFrameworkunifiedStatus CANThreadCanOpen(HANDLE h_app) {
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
  const char *result = "Unknown";
  const char *called = "Unknown";
  UI_16 z = ZONE_ERR;

  e_status = ConvRet(CanOpen(h_app, CAN_HAL_TYPE_CAN));
  result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
  z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
  called = "CanOpen";
  FRAMEWORKUNIFIEDLOG(z, __func__, "%s %s", called, result);
  return e_status;
}

EFrameworkunifiedStatus CANThreadStart(HANDLE h_app) {
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;

  CommClrAvailabilityCurrent(CAN_AVAILABILITY);
  CANDeliveryInit();
  CANCommWatchInit();
  CANTxMsgInit();

  e_status = CommonThreadStart(h_app, kCanPcbhs, _countof(kCanPcbhs),
                          NTFY_Communication_CAN_ISAVAILABLE, CANThreadCanOpen);
  return e_status;
}

static EFrameworkunifiedStatus CANThreadCanClose(HANDLE h_app) {
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
  const char *result = "Unknown";
  const char *called = "Unknown";
  UI_16 z = ZONE_ERR;

  e_status = ConvRet(CanClose(h_app, CAN_HAL_TYPE_CAN));
  result = (e_status != eFrameworkunifiedStatusOK) ? "failed" : "success";
  z = (e_status != eFrameworkunifiedStatusOK) ? ZONE_INFO : ZONE_ERR;
  called = "CanClose";
  FRAMEWORKUNIFIEDLOG(z, __func__, "%s %s", called, result);
  return e_status;
}

EFrameworkunifiedStatus CANThreadStop(HANDLE h_app) {
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;

  CommClrAvailabilityCurrent(CAN_AVAILABILITY);
  e_status = CommonThreadStop(h_app, kCanPcbhsD, _countof(kCanPcbhsD),
                              NTFY_Communication_CAN_ISAVAILABLE,
                              CANThreadCanClose);
  return e_status;
}

static EFrameworkunifiedStatus CANHalRcvProcess(HANDLE h_app) {
  CanMessage canhal_recv_data;
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusFail;
  uint8_t cmd = 0;

  e_status = FrameworkunifiedGetMsgDataOfSize(h_app, &canhal_recv_data,
                            sizeof(canhal_recv_data), eSMRRelease);
  if (e_status != eFrameworkunifiedStatusOK) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error : FrameworkunifiedGetMsgDataOfSize is failed, e_status=%d", e_status);
  }

  if (e_status == eFrameworkunifiedStatusInvldBufSize) {
    FrameworkunifiedClearMsgData(h_app);
    goto cleanup;
  } else if (e_status != eFrameworkunifiedStatusOK) {
    goto cleanup;
  }

  if (canhal_recv_data.dlc > CAN_MESSAGE_LEN) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error : Invalid data length, dlc=%d", canhal_recv_data.dlc);
    return eFrameworkunifiedStatusFail;
  }

  FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "can_id=%x dlc=%d rid=%x",
         canhal_recv_data.can_id, canhal_recv_data.dlc, canhal_recv_data.rid);
  FRAMEWORKUNIFIEDLOG(ZONE_DEBUG, __func__, "msg_data=%s",
         MessageDataOutputLog(canhal_recv_data.data, canhal_recv_data.dlc).c_str());

  cmd = (uint8_t)(canhal_recv_data.can_id & 0xFF);
  switch (canhal_recv_data.can_id) {
  case CAN_CMDID_STARTUP_FIN_RESP_RX:
    e_status = CANCommandDeliveryRcvProcess(h_app, &canhal_recv_data, cmd);
    break;
  case CAN_CMDID_MRST_INFO_RESP_RX:
    e_status = CANCommandDeliveryRcvProcess(h_app, &canhal_recv_data, cmd);
    break;
  case CAN_CMDID_VERSION_RESP_RX:
    e_status = CANCommandDeliveryRcvProcess(h_app, &canhal_recv_data, cmd);
    break;
  case CAN_CMDID_CONNECTION_NODE_RESP_RX:
    e_status = CANCommandDeliveryRcvProcess(h_app, &canhal_recv_data, cmd);
    break;
  case CAN_CMDID_FUELCALC_RST_REQ_RX:
    CANCommandSetFuelCalcRstReq();
    e_status = CANCommandDeliveryRcvProcess(h_app, &canhal_recv_data, cmd);
    break;
  default:
    e_status = CANDeliveryRcvProcess(h_app, &canhal_recv_data);
  }
cleanup:
  return e_status;
}

static EFrameworkunifiedStatus CANHalSndStsProcess(HANDLE h_app) {
  CanSendResult canhal_recv_data;
  EFrameworkunifiedStatus err = eFrameworkunifiedStatusOK;
  uint8_t ui_can_rid;
  PS_CommunicationProtocol cid;

  err = FrameworkunifiedGetMsgDataOfSize(h_app, &canhal_recv_data,
                            sizeof(canhal_recv_data), eSMRRelease);
  if (err != eFrameworkunifiedStatusOK) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error : FrameworkunifiedGetMsgDataOfSize is failed, err=%d", err);
  }

  if (err == eFrameworkunifiedStatusInvldBufSize) {
    FrameworkunifiedClearMsgData(h_app);
    return eFrameworkunifiedStatusFail;
  } else if (err != eFrameworkunifiedStatusOK) {
    return eFrameworkunifiedStatusFail;
  }

  ui_can_rid = canhal_recv_data.rid;
  if (CAN_RID_NOTUSE_CODE == ui_can_rid) {
    return eFrameworkunifiedStatusOK;
  }

  switch (canhal_recv_data.can_id) {
  case CAN_CMDID_MRST_INFO_REQ_TX:
  case CAN_CMDID_CONNECTION_NODE_REQ_TX:
  case CAN_CMDID_FUELCALC_REQ_TX:
    cid = CID_CAN_CMD_TX_RESULT;
    break;
  default:
    cid = CID_CAN_TX_RESULT;
    break;
  }

  return CANSndStsProcess(h_app, &canhal_recv_data, cid);
}

EFrameworkunifiedStatus CANCallbackForTimeOut(HANDLE h_app) {
  CANCommWatchTimeout(h_app);
  return eFrameworkunifiedStatusOK;
}