summaryrefslogtreecommitdiffstats
path: root/peripheralservice/communication/client_can/src/Canif_API.cpp
blob: 499d187eb28f960ce342bb41031659747c4a768d (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * @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 <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <native_service/frameworkunified_framework_if.h>
#include <peripheral_service/Canif_API.h>

#include "API_Local_Common.h"
#include "Canif_API_Local.h"
#include "Canif_TransmissionData.h"
#include "com_error_type.h"

static __thread HANDLE g_sender = NULL;

static EFrameworkunifiedStatus CanifMsgToSrv(HANDLE h, UI_32 cmd, size_t len, PCVOID data) {
  if (data == NULL)
    return eFrameworkunifiedStatusFail;

  if (len > UINT32_MAX)
    return eFrameworkunifiedStatusFail;

  if (g_sender == NULL) {
    g_sender = FrameworkunifiedMcOpenSender(h, LAN_SERVICE_CAN);
    if (g_sender == NULL)
      return eFrameworkunifiedStatusFail;
  }
  return FrameworkunifiedSendMsg(g_sender, cmd, (UI_32)len, data);
}

CANIF_RET_API Canif_DeliveryEntry(HANDLE h_app, PCSTR notify_name, 
                                  uint8_t can_num, CANID *p_can_id) {

  CANIF_RET_API l_ret = CANIF_RET_ERROR_PARAM;
  CAN_DELIVERY_ENTRY pst_delivery_entry;
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
  PS_Communication_InternalProtocol cmd = CID_CANIF_DELIVERY_ENTRY;
  void *tx = reinterpret_cast<void *>(&pst_delivery_entry);
  size_t len = sizeof(pst_delivery_entry);
  memset(tx, 0x0, len);
  int32_t i;

  if (NULL == h_app)
    goto cleanup;

  if (NULL == p_can_id)
    goto cleanup;

  if (!Canif_CheckNotifyName(notify_name))
    goto cleanup;

  if (0 == can_num)
    goto cleanup;

  if (CAN_DELIVERY_CANID_ENTRY_MAX < can_num)
    goto cleanup;

  for (i = 0; i < (int32_t)can_num; i++) {
    if (!Canif_CheckCanID(p_can_id[i]))
      goto cleanup;
  }

  Canif_CopyNotifyName(pst_delivery_entry.notifyName, notify_name);
  pst_delivery_entry.usCanNum = (uint16_t)can_num;
  memcpy(pst_delivery_entry.ulCanid, p_can_id, sizeof(CANID) * can_num);

  e_status = CanifMsgToSrv(h_app, cmd, len, tx);
  if (e_status != eFrameworkunifiedStatusOK) {
    l_ret = CANIF_RET_ERROR_CANCEL;
    goto cleanup;
  }

  l_ret = CANIF_RET_NORMAL;
cleanup:
  return l_ret;
}

CANIF_RET_API Canif_TransmissionStart(HANDLE h_app, PCSTR notify_name, 
                                uint8_t rid, uint16_t freq, CAN_DATA *p_data) {

  CANIF_RET_API l_ret = CANIF_RET_ERROR_PARAM;
  CAN_TRANSMISSION_START_MSG_DAT pst_transmission_start;
  PS_Communication_InternalProtocol cmd = CID_CANIF_TX_START;
  void *tx = reinterpret_cast<void *>(&pst_transmission_start);
  size_t len = sizeof(pst_transmission_start);
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
  PCSTR _notify_name = notify_name;
  memset(tx, 0x0, len);

  if (h_app == NULL)
    goto cleanup;

  if (p_data == NULL)
    goto cleanup;

  if (rid == 0xFF) {
    _notify_name = "";  // using dummy name;
  }

  if (!Canif_CheckNotifyName(_notify_name))
    goto cleanup;

  if (CAN_TXDATA_SIZE < (p_data->dlc))
    goto cleanup;

  if (!Canif_CheckCanID(p_data->can_id))
    goto cleanup;

  if (!Canif_InitDataIsDefined21PF(p_data->can_id))
    goto cleanup;
  
  Canif_CopyNotifyName(pst_transmission_start.notifyName, _notify_name);
  pst_transmission_start.ucRid = rid;
  pst_transmission_start.usFreq = freq;
  memcpy(reinterpret_cast<void *>(&pst_transmission_start.stCandata),
         reinterpret_cast<int8_t *>(p_data), (size_t)sizeof(CAN_DATA));
  e_status = CanifMsgToSrv(h_app, cmd, len, tx);
  if (e_status != eFrameworkunifiedStatusOK) {
    l_ret = CANIF_RET_ERROR_CANCEL;
    goto cleanup;
  }

  l_ret = CANIF_RET_NORMAL;
cleanup:
  return l_ret;
}

CANIF_RET_API Canif_CommandCtrl(HANDLE h_app, PCSTR notify_name, uint8_t rid,
                                uint32_t cmd_id) {

  CANIF_RET_API l_ret = CANIF_RET_ERROR_PARAM;
  CAN_CMD_CTRL_MSG_DAT pst_cmd_ctrl_msg;
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
  PS_Communication_InternalProtocol cmd = CID_CANIF_CMD_CTRL;
  void *tx = reinterpret_cast<void *>(&pst_cmd_ctrl_msg);
  size_t len = sizeof(pst_cmd_ctrl_msg);
  PCSTR _notify_name = notify_name;
  memset(tx, 0x0, len);

  if (!Canif_IsCommand(cmd_id))
    goto cleanup;

  if (h_app == NULL)
    goto cleanup;

  if (rid == 0xFF) {
    _notify_name = "";  // using dummy name;
  }

  if (!Canif_CheckNotifyName(_notify_name))
    goto cleanup;

  Canif_CopyNotifyName(pst_cmd_ctrl_msg.notifyName, _notify_name);
  pst_cmd_ctrl_msg.ucRid = rid;
  pst_cmd_ctrl_msg.ucCmdid = (uint8_t)cmd_id;

  e_status = CanifMsgToSrv(h_app, cmd, len, tx);
  if (e_status != eFrameworkunifiedStatusOK) {
    l_ret = CANIF_RET_ERROR_CANCEL;
    goto cleanup;
  }

  l_ret = CANIF_RET_NORMAL;
cleanup:
  return l_ret;
}

CANIF_RET_API Canif_CommWatch(HANDLE h_app, PCSTR notify_name, CANID can_id, 
                              DID did, uint16_t watch_time) {

  CANIF_RET_API l_ret = CANIF_RET_ERROR_PARAM;
  CAN_COMM_WATCHEXT_MSG_DAT pst_comm_watch_msg;
  PS_Communication_InternalProtocol cmd = CID_CANIF_COMM_WATCH;
  void *tx = reinterpret_cast<void *>(&pst_comm_watch_msg);
  size_t len = sizeof(pst_comm_watch_msg);
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
  memset(tx, 0x0, len);

  if (h_app == NULL)
    goto cleanup;

  if (!Canif_CheckNotifyName(notify_name))
    goto cleanup;

  if (!Canif_CheckCanID(can_id))
    goto cleanup;

  Canif_CopyNotifyName(pst_comm_watch_msg.notifyName, notify_name);
  pst_comm_watch_msg.ulCanid = can_id;
  pst_comm_watch_msg.ulDid = did;
  pst_comm_watch_msg.ucIg = CAN_IG_COOPERATION_OFF;
  pst_comm_watch_msg.usWatchTime = watch_time;

  e_status = CanifMsgToSrv(h_app, cmd, len, tx);
  if (e_status != eFrameworkunifiedStatusOK) {
    l_ret = CANIF_RET_ERROR_CANCEL;
    goto cleanup;
  }

  l_ret = CANIF_RET_NORMAL;
cleanup:
  return l_ret;
}

CANIF_RET_API Canif_TransStart(HANDLE h_app, CANID can_id,
                      CAN_DATA_MASK *mask, CAN_DATA_BIT *dat, uint32_t freq) {
  CANIF_RET_API l_ret = CANIF_RET_ERROR_PARAM;
  CAN_TRANS_START_MSG_DAT pst_trans_start;
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
  PS_Communication_InternalProtocol cmd = CID_CANIF_TX_BIT_START;
  void *tx = reinterpret_cast<void *>(&pst_trans_start);
  size_t len = sizeof(pst_trans_start);
  memset(tx, 0x0, len);

  if (h_app == NULL)
    goto cleanup;

  if (!Canif_CheckCanID(can_id))
    goto cleanup;

  if (!Canif_InitDataIsDefined21PF(can_id))
    goto cleanup;

  if (mask == NULL)
    goto cleanup;

  if (dat == NULL)
    goto cleanup;

  if (0xFFFF < freq)
    goto cleanup; 

  pst_trans_start.id = can_id;
  pst_trans_start.freq = freq;
  memcpy(&pst_trans_start.mask.dat, mask->dat, sizeof(mask->dat));
  memcpy(&pst_trans_start.dat.dat, dat->dat, sizeof(dat->dat));

  e_status = CanifMsgToSrv(h_app, cmd, len, tx);
  if (e_status != eFrameworkunifiedStatusOK) {
    l_ret = CANIF_RET_ERROR_CANCEL;
    goto cleanup;
  }

  l_ret = CANIF_RET_NORMAL;
cleanup:
  return l_ret;
}

CANIF_RET_API Canif_DeliveryEraseAll(HANDLE h_app, PCSTR notify_name) {
  CANIF_RET_API l_ret = CANIF_RET_ERROR_PARAM;
  EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
  HANDLE handle[NUM_OF_CANIF_PROTOCOL];
  int i = 0;

  for (i = 0; i < NUM_OF_CANIF_PROTOCOL; i++) {
    handle[i] = NULL;
  }

  if (NULL == h_app)
    goto cleanup;

  if (!Canif_CheckNotifyName(notify_name))
    goto cleanup; 

  l_ret = CANIF_RET_ERROR_CANCEL;
  for (i = 0; i < NUM_OF_CANIF_PROTOCOL; i++) {
    handle[i] = FrameworkunifiedMcOpenSender(h_app,
                                             Canif_PidxTosname((enum CANIF_PROTOCOL_IDX)i));
    if (!handle[i])
      goto cleanup;
  }

  for (i = 0; i < NUM_OF_CANIF_PROTOCOL; i++) {
    e_status = FrameworkunifiedSendMsg(handle[i], CID_CANIF_DELIVERY_ERASE,
                                       CANIF_NOTIFY_NAME_MAX_SIZE, (PCVOID)notify_name);
    if (e_status != eFrameworkunifiedStatusOK)
      goto cleanup;
  }

  l_ret = CANIF_RET_NORMAL;
cleanup:
  for (i = 0; i < NUM_OF_CANIF_PROTOCOL; i++) {
    if (handle[i] != NULL)
      FrameworkunifiedMcClose(handle[i]);
  }
  return l_ret;
}