Init basesystem source codes.
[staging/basesystem.git] / can_hal / src / can_hal_api.cpp
1 /*
2  * @copyright Copyright (c) 2017-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 /*
18  * Information.
19  * This source code is a sample source code .
20  * Implementation of the function must be performed by the vendor.
21  */
22
23 #include "can_hal_core.h"
24 #include "can_hal_stm.h"
25 #include <stdio.h>
26 #include <pthread.h>
27 #include <string.h>
28 #include <assert.h>
29
30 CANHAL_RET_API CanOpen(HANDLE h_app, CanHalType type) {
31   CANHAL_RET_API ret = CANHAL_RET_ERR_ERR;
32
33   if (!TypeIsValid(type)) {
34     return CANHAL_RET_ERR_PARAM;
35   }
36
37   if (!h_app) {
38     return CANHAL_RET_ERR_PARAM;
39   }
40
41   if (IsCanHalOpened(type)) {
42     return CANHAL_RET_NORMAL;
43   }
44
45   if (IsDeviceEnabled(type)) {
46     return CANHAL_RET_NORMAL;
47   }
48
49   ret = CanOpenCore(type);
50   if (CANHAL_RET_NORMAL != ret) {
51     return ret;
52   }
53
54   ret = CanHalCreateInternalThread(h_app, type);
55   if (CANHAL_RET_NORMAL != ret) {
56     CanCloseCore(type);
57     return ret;
58   }
59
60   SetDeviceStateEnable(type);
61   SetCanHalStateOpen(type);
62   InvokeStateCallback(type);
63   return ret;
64 }
65
66 CANHAL_RET_API CanClose(HANDLE h_app, CanHalType type) {
67   CANHAL_RET_API ret = CANHAL_RET_ERR_ERR;
68   if (!TypeIsValid(type)) {
69     return CANHAL_RET_ERR_PARAM;
70   }
71
72   if (!h_app) {
73     return CANHAL_RET_ERR_PARAM;
74   }
75
76   if (!IsCanHalOpened(type)) {
77     return CANHAL_RET_NORMAL;
78   }
79
80   if (IsDeviceEnabled(type)) {
81     ret = CanCloseCore(type);
82     if (CANHAL_RET_NORMAL != ret)
83       return ret;
84   }
85     
86   ret = CanHalDestroyInternalThread(h_app, type);
87   if (CANHAL_RET_NORMAL != ret)
88     return ret;
89
90   SetDeviceStateDisable(type);
91   SetCanHalStateClose(type);
92   InvokeErrorCallback(h_app, type);
93
94   return ret;
95 }
96
97 static CANHAL_RET_API CanSendCommonCheck(const void *message, 
98                                                         enum CanHalType type) {
99   if (!TypeIsValid(type)) {
100     return CANHAL_RET_ERR_PARAM;
101   }
102
103   if (!message) {
104     return CANHAL_RET_ERR_PARAM;
105   }
106
107   if (!IsCanHalOpened(type)) {
108     return CANHAL_RET_ERR_STATE;
109   }
110
111   if (!IsDeviceEnabled(type)) {
112     return CANHAL_RET_ERR_STATE;
113   }
114
115   return CANHAL_RET_NORMAL;
116 }
117
118 CANHAL_RET_API CanSend(HANDLE h_app,
119                              const CanMessage *message, enum CanHalType type) {
120   CANHAL_RET_API ret = CANHAL_RET_ERR_ERR;
121   void *_message = NULL;
122   ssize_t sz = 0;
123
124   ret = CanSendCommonCheck(message, type);
125   if (ret != CANHAL_RET_NORMAL)
126     return ret;
127
128   if (!h_app) {
129     return CANHAL_RET_ERR_PARAM;
130   }
131
132   if (CAN_NORMAL_MESSAGE_LEN < message->dlc) {
133     return CANHAL_RET_ERR_PARAM;
134   }
135
136   _message = CanHalPackMessage(h_app, (const void *)message,
137                                sizeof(CanMessage), &sz);
138   if (!_message)
139     return CANHAL_RET_ERR_ERR;
140
141   ret = CanHalInternalSend(type, _message, sz);
142   CanHalDestroyPackedMessage(_message);
143   return ret;
144 }
145
146 CANHAL_RET_API CanGetVersion(HANDLE h_app, std::string *p_version) {
147   CanHalType type = CAN_HAL_TYPE_CAN;
148   if (!h_app) {
149     return CANHAL_RET_ERR_PARAM;
150   }
151
152   if (!IsCanHalOpened(type)) {
153     return CANHAL_RET_ERR_STATE;
154   }
155
156   if (!IsDeviceEnabled(type)) {
157     return CANHAL_RET_ERR_STATE;
158   }
159
160   if (NULL == p_version) {
161     return CANHAL_RET_ERR_PARAM;
162   }
163
164   *p_version = "FFFF";
165   return CANHAL_RET_NORMAL;
166 }