Init basesystem source codes.
[staging/basesystem.git] / peripheralservice / communication / client_can / src / Canif_API.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 #include <stdio.h>
17 #include <string.h>
18 #include <fcntl.h>
19 #include <native_service/frameworkunified_framework_if.h>
20 #include <peripheral_service/Canif_API.h>
21
22 #include "API_Local_Common.h"
23 #include "Canif_API_Local.h"
24 #include "Canif_TransmissionData.h"
25 #include "com_error_type.h"
26
27 static __thread HANDLE g_sender = NULL;
28
29 static EFrameworkunifiedStatus CanifMsgToSrv(HANDLE h, UI_32 cmd, size_t len, PCVOID data) {
30   if (data == NULL)
31     return eFrameworkunifiedStatusFail;
32
33   if (len > UINT32_MAX)
34     return eFrameworkunifiedStatusFail;
35
36   if (g_sender == NULL) {
37     g_sender = FrameworkunifiedMcOpenSender(h, LAN_SERVICE_CAN);
38     if (g_sender == NULL)
39       return eFrameworkunifiedStatusFail;
40   }
41   return FrameworkunifiedSendMsg(g_sender, cmd, (UI_32)len, data);
42 }
43
44 CANIF_RET_API Canif_DeliveryEntry(HANDLE h_app, PCSTR notify_name, 
45                                   uint8_t can_num, CANID *p_can_id) {
46
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);
53   memset(tx, 0x0, len);
54   int32_t i;
55
56   if (NULL == h_app)
57     goto cleanup;
58
59   if (NULL == p_can_id)
60     goto cleanup;
61
62   if (!Canif_CheckNotifyName(notify_name))
63     goto cleanup;
64
65   if (0 == can_num)
66     goto cleanup;
67
68   if (CAN_DELIVERY_CANID_ENTRY_MAX < can_num)
69     goto cleanup;
70
71   for (i = 0; i < (int32_t)can_num; i++) {
72     if (!Canif_CheckCanID(p_can_id[i]))
73       goto cleanup;
74   }
75
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);
79
80   e_status = CanifMsgToSrv(h_app, cmd, len, tx);
81   if (e_status != eFrameworkunifiedStatusOK) {
82     l_ret = CANIF_RET_ERROR_CANCEL;
83     goto cleanup;
84   }
85
86   l_ret = CANIF_RET_NORMAL;
87 cleanup:
88   return l_ret;
89 }
90
91 CANIF_RET_API Canif_TransmissionStart(HANDLE h_app, PCSTR notify_name, 
92                                 uint8_t rid, uint16_t freq, CAN_DATA *p_data) {
93
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);
102
103   if (h_app == NULL)
104     goto cleanup;
105
106   if (p_data == NULL)
107     goto cleanup;
108
109   if (rid == 0xFF) {
110     _notify_name = "";  // using dummy name;
111   }
112
113   if (!Canif_CheckNotifyName(_notify_name))
114     goto cleanup;
115
116   if (CAN_TXDATA_SIZE < (p_data->dlc))
117     goto cleanup;
118
119   if (!Canif_CheckCanID(p_data->can_id))
120     goto cleanup;
121
122   if (!Canif_InitDataIsDefined21PF(p_data->can_id))
123     goto cleanup;
124   
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;
133     goto cleanup;
134   }
135
136   l_ret = CANIF_RET_NORMAL;
137 cleanup:
138   return l_ret;
139 }
140
141 CANIF_RET_API Canif_CommandCtrl(HANDLE h_app, PCSTR notify_name, uint8_t rid,
142                                 uint32_t cmd_id) {
143
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);
152
153   if (!Canif_IsCommand(cmd_id))
154     goto cleanup;
155
156   if (h_app == NULL)
157     goto cleanup;
158
159   if (rid == 0xFF) {
160     _notify_name = "";  // using dummy name;
161   }
162
163   if (!Canif_CheckNotifyName(_notify_name))
164     goto cleanup;
165
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;
169
170   e_status = CanifMsgToSrv(h_app, cmd, len, tx);
171   if (e_status != eFrameworkunifiedStatusOK) {
172     l_ret = CANIF_RET_ERROR_CANCEL;
173     goto cleanup;
174   }
175
176   l_ret = CANIF_RET_NORMAL;
177 cleanup:
178   return l_ret;
179 }
180
181 CANIF_RET_API Canif_CommWatch(HANDLE h_app, PCSTR notify_name, CANID can_id, 
182                               DID did, uint16_t watch_time) {
183
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);
191
192   if (h_app == NULL)
193     goto cleanup;
194
195   if (!Canif_CheckNotifyName(notify_name))
196     goto cleanup;
197
198   if (!Canif_CheckCanID(can_id))
199     goto cleanup;
200
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;
206
207   e_status = CanifMsgToSrv(h_app, cmd, len, tx);
208   if (e_status != eFrameworkunifiedStatusOK) {
209     l_ret = CANIF_RET_ERROR_CANCEL;
210     goto cleanup;
211   }
212
213   l_ret = CANIF_RET_NORMAL;
214 cleanup:
215   return l_ret;
216 }
217
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);
227
228   if (h_app == NULL)
229     goto cleanup;
230
231   if (!Canif_CheckCanID(can_id))
232     goto cleanup;
233
234   if (!Canif_InitDataIsDefined21PF(can_id))
235     goto cleanup;
236
237   if (mask == NULL)
238     goto cleanup;
239
240   if (dat == NULL)
241     goto cleanup;
242
243   if (0xFFFF < freq)
244     goto cleanup; 
245
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));
250
251   e_status = CanifMsgToSrv(h_app, cmd, len, tx);
252   if (e_status != eFrameworkunifiedStatusOK) {
253     l_ret = CANIF_RET_ERROR_CANCEL;
254     goto cleanup;
255   }
256
257   l_ret = CANIF_RET_NORMAL;
258 cleanup:
259   return l_ret;
260 }
261
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];
266   int i = 0;
267
268   for (i = 0; i < NUM_OF_CANIF_PROTOCOL; i++) {
269     handle[i] = NULL;
270   }
271
272   if (NULL == h_app)
273     goto cleanup;
274
275   if (!Canif_CheckNotifyName(notify_name))
276     goto cleanup; 
277
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));
282     if (!handle[i])
283       goto cleanup;
284   }
285
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)
290       goto cleanup;
291   }
292
293   l_ret = CANIF_RET_NORMAL;
294 cleanup:
295   for (i = 0; i < NUM_OF_CANIF_PROTOCOL; i++) {
296     if (handle[i] != NULL)
297       FrameworkunifiedMcClose(handle[i]);
298   }
299   return l_ret;
300 }
301