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.
19 #include <native_service/frameworkunified_framework_if.h>
20 #include <peripheral_service/Canif_API.h>
22 #include "API_Local_Common.h"
23 #include "Canif_API_Local.h"
24 #include "Canif_TransmissionData.h"
25 #include "com_error_type.h"
27 static __thread HANDLE g_sender = NULL;
29 static EFrameworkunifiedStatus CanifMsgToSrv(HANDLE h, UI_32 cmd, size_t len, PCVOID data) {
31 return eFrameworkunifiedStatusFail;
34 return eFrameworkunifiedStatusFail;
36 if (g_sender == NULL) {
37 g_sender = FrameworkunifiedMcOpenSender(h, LAN_SERVICE_CAN);
39 return eFrameworkunifiedStatusFail;
41 return FrameworkunifiedSendMsg(g_sender, cmd, (UI_32)len, data);
44 CANIF_RET_API Canif_DeliveryEntry(HANDLE h_app, PCSTR notify_name,
45 uint8_t can_num, CANID *p_can_id) {
47 CANIF_RET_API l_ret = CANIF_RET_ERROR_PARAM;
48 CAN_DELIVERY_ENTRY pst_delivery_entry;
49 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
50 PS_Communication_InternalProtocol cmd = CID_CANIF_DELIVERY_ENTRY;
51 void *tx = reinterpret_cast<void *>(&pst_delivery_entry);
52 size_t len = sizeof(pst_delivery_entry);
62 if (!Canif_CheckNotifyName(notify_name))
68 if (CAN_DELIVERY_CANID_ENTRY_MAX < can_num)
71 for (i = 0; i < (int32_t)can_num; i++) {
72 if (!Canif_CheckCanID(p_can_id[i]))
76 Canif_CopyNotifyName(pst_delivery_entry.notifyName, notify_name);
77 pst_delivery_entry.usCanNum = (uint16_t)can_num;
78 memcpy(pst_delivery_entry.ulCanid, p_can_id, sizeof(CANID) * can_num);
80 e_status = CanifMsgToSrv(h_app, cmd, len, tx);
81 if (e_status != eFrameworkunifiedStatusOK) {
82 l_ret = CANIF_RET_ERROR_CANCEL;
86 l_ret = CANIF_RET_NORMAL;
91 CANIF_RET_API Canif_TransmissionStart(HANDLE h_app, PCSTR notify_name,
92 uint8_t rid, uint16_t freq, CAN_DATA *p_data) {
94 CANIF_RET_API l_ret = CANIF_RET_ERROR_PARAM;
95 CAN_TRANSMISSION_START_MSG_DAT pst_transmission_start;
96 PS_Communication_InternalProtocol cmd = CID_CANIF_TX_START;
97 void *tx = reinterpret_cast<void *>(&pst_transmission_start);
98 size_t len = sizeof(pst_transmission_start);
99 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
100 PCSTR _notify_name = notify_name;
101 memset(tx, 0x0, len);
110 _notify_name = ""; // using dummy name;
113 if (!Canif_CheckNotifyName(_notify_name))
116 if (CAN_TXDATA_SIZE < (p_data->dlc))
119 if (!Canif_CheckCanID(p_data->can_id))
122 if (!Canif_InitDataIsDefined21PF(p_data->can_id))
125 Canif_CopyNotifyName(pst_transmission_start.notifyName, _notify_name);
126 pst_transmission_start.ucRid = rid;
127 pst_transmission_start.usFreq = freq;
128 memcpy(reinterpret_cast<void *>(&pst_transmission_start.stCandata),
129 reinterpret_cast<int8_t *>(p_data), (size_t)sizeof(CAN_DATA));
130 e_status = CanifMsgToSrv(h_app, cmd, len, tx);
131 if (e_status != eFrameworkunifiedStatusOK) {
132 l_ret = CANIF_RET_ERROR_CANCEL;
136 l_ret = CANIF_RET_NORMAL;
141 CANIF_RET_API Canif_CommandCtrl(HANDLE h_app, PCSTR notify_name, uint8_t rid,
144 CANIF_RET_API l_ret = CANIF_RET_ERROR_PARAM;
145 CAN_CMD_CTRL_MSG_DAT pst_cmd_ctrl_msg;
146 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
147 PS_Communication_InternalProtocol cmd = CID_CANIF_CMD_CTRL;
148 void *tx = reinterpret_cast<void *>(&pst_cmd_ctrl_msg);
149 size_t len = sizeof(pst_cmd_ctrl_msg);
150 PCSTR _notify_name = notify_name;
151 memset(tx, 0x0, len);
153 if (!Canif_IsCommand(cmd_id))
160 _notify_name = ""; // using dummy name;
163 if (!Canif_CheckNotifyName(_notify_name))
166 Canif_CopyNotifyName(pst_cmd_ctrl_msg.notifyName, _notify_name);
167 pst_cmd_ctrl_msg.ucRid = rid;
168 pst_cmd_ctrl_msg.ucCmdid = (uint8_t)cmd_id;
170 e_status = CanifMsgToSrv(h_app, cmd, len, tx);
171 if (e_status != eFrameworkunifiedStatusOK) {
172 l_ret = CANIF_RET_ERROR_CANCEL;
176 l_ret = CANIF_RET_NORMAL;
181 CANIF_RET_API Canif_CommWatch(HANDLE h_app, PCSTR notify_name, CANID can_id,
182 DID did, uint16_t watch_time) {
184 CANIF_RET_API l_ret = CANIF_RET_ERROR_PARAM;
185 CAN_COMM_WATCHEXT_MSG_DAT pst_comm_watch_msg;
186 PS_Communication_InternalProtocol cmd = CID_CANIF_COMM_WATCH;
187 void *tx = reinterpret_cast<void *>(&pst_comm_watch_msg);
188 size_t len = sizeof(pst_comm_watch_msg);
189 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
190 memset(tx, 0x0, len);
195 if (!Canif_CheckNotifyName(notify_name))
198 if (!Canif_CheckCanID(can_id))
201 Canif_CopyNotifyName(pst_comm_watch_msg.notifyName, notify_name);
202 pst_comm_watch_msg.ulCanid = can_id;
203 pst_comm_watch_msg.ulDid = did;
204 pst_comm_watch_msg.ucIg = CAN_IG_COOPERATION_OFF;
205 pst_comm_watch_msg.usWatchTime = watch_time;
207 e_status = CanifMsgToSrv(h_app, cmd, len, tx);
208 if (e_status != eFrameworkunifiedStatusOK) {
209 l_ret = CANIF_RET_ERROR_CANCEL;
213 l_ret = CANIF_RET_NORMAL;
218 CANIF_RET_API Canif_TransStart(HANDLE h_app, CANID can_id,
219 CAN_DATA_MASK *mask, CAN_DATA_BIT *dat, uint32_t freq) {
220 CANIF_RET_API l_ret = CANIF_RET_ERROR_PARAM;
221 CAN_TRANS_START_MSG_DAT pst_trans_start;
222 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
223 PS_Communication_InternalProtocol cmd = CID_CANIF_TX_BIT_START;
224 void *tx = reinterpret_cast<void *>(&pst_trans_start);
225 size_t len = sizeof(pst_trans_start);
226 memset(tx, 0x0, len);
231 if (!Canif_CheckCanID(can_id))
234 if (!Canif_InitDataIsDefined21PF(can_id))
246 pst_trans_start.id = can_id;
247 pst_trans_start.freq = freq;
248 memcpy(&pst_trans_start.mask.dat, mask->dat, sizeof(mask->dat));
249 memcpy(&pst_trans_start.dat.dat, dat->dat, sizeof(dat->dat));
251 e_status = CanifMsgToSrv(h_app, cmd, len, tx);
252 if (e_status != eFrameworkunifiedStatusOK) {
253 l_ret = CANIF_RET_ERROR_CANCEL;
257 l_ret = CANIF_RET_NORMAL;
262 CANIF_RET_API Canif_DeliveryEraseAll(HANDLE h_app, PCSTR notify_name) {
263 CANIF_RET_API l_ret = CANIF_RET_ERROR_PARAM;
264 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
265 HANDLE handle[NUM_OF_CANIF_PROTOCOL];
268 for (i = 0; i < NUM_OF_CANIF_PROTOCOL; i++) {
275 if (!Canif_CheckNotifyName(notify_name))
278 l_ret = CANIF_RET_ERROR_CANCEL;
279 for (i = 0; i < NUM_OF_CANIF_PROTOCOL; i++) {
280 handle[i] = FrameworkunifiedMcOpenSender(h_app,
281 Canif_PidxTosname((enum CANIF_PROTOCOL_IDX)i));
286 for (i = 0; i < NUM_OF_CANIF_PROTOCOL; i++) {
287 e_status = FrameworkunifiedSendMsg(handle[i], CID_CANIF_DELIVERY_ERASE,
288 CANIF_NOTIFY_NAME_MAX_SIZE, (PCVOID)notify_name);
289 if (e_status != eFrameworkunifiedStatusOK)
293 l_ret = CANIF_RET_NORMAL;
295 for (i = 0; i < NUM_OF_CANIF_PROTOCOL; i++) {
296 if (handle[i] != NULL)
297 FrameworkunifiedMcClose(handle[i]);