68 struct sockaddr_can addr;
76 dev_handler_ = socket(PF_CAN, SOCK_RAW, CAN_RAW);
77 if (dev_handler_ < 0) {
78 AERROR <<
"open device error code [" << dev_handler_ <<
"]: ";
79 return ErrorCode::CAN_CLIENT_ERROR_BASE;
88 struct can_filter filter[1];
91 filter[0].can_id = 0x000;
92 filter[0].can_mask = 0x000;
94 ret = setsockopt(dev_handler_, SOL_CAN_RAW, CAN_RAW_FILTER, &filter,
97 AERROR <<
"add receive msg id filter error code: " << ret;
98 return ErrorCode::CAN_CLIENT_ERROR_BASE;
104 ret = ::setsockopt(dev_handler_, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &enable,
107 AERROR <<
"enable reception of can frame error code: " << ret;
108 return ErrorCode::CAN_CLIENT_ERROR_BASE;
111 std::string interface_prefix;
113 interface_prefix =
"vcan";
115 interface_prefix =
"slcan";
117 interface_prefix =
"can";
120 const std::string can_name = absl::StrCat(interface_prefix, port_);
121 std::strncpy(ifr.ifr_name, can_name.c_str(), IFNAMSIZ);
122 if (ioctl(dev_handler_, SIOCGIFINDEX, &ifr) < 0) {
124 return ErrorCode::CAN_CLIENT_ERROR_BASE;
129 addr.can_family = AF_CAN;
130 addr.can_ifindex = ifr.ifr_ifindex;
131 ret = ::bind(dev_handler_,
reinterpret_cast<struct sockaddr *
>(&addr),
135 AERROR <<
"bind socket to network interface error code: " << ret;
136 return ErrorCode::CAN_CLIENT_ERROR_BASE;
140 return ErrorCode::OK;
158 int32_t *
const frame_num) {
159 CHECK_NOTNULL(frame_num);
160 CHECK_EQ(frames.size(),
static_cast<size_t>(*frame_num));
163 AERROR <<
"Nvidia can client has not been initiated! Please init first!";
164 return ErrorCode::CAN_CLIENT_ERROR_SEND_FAILED;
168 AERROR <<
"frames[" << i <<
"].len = " << frames[i].len
169 <<
", which is not equal to can message data length ("
171 return ErrorCode::CAN_CLIENT_ERROR_SEND_FAILED;
174 send_frames_[i].can_id = (frames[i].id & CAN_EFF_MASK) | CAN_EFF_FLAG;
176 send_frames_[i].can_id = (frames[i].id & CAN_SFF_MASK);
178 ADEBUG <<
"send can id is " << send_frames_[i].can_id;
179 send_frames_[i].can_dlc = frames[i].len;
180 std::memcpy(send_frames_[i].data, frames[i].data, frames[i].len);
183 int ret =
static_cast<int>(
184 write(dev_handler_, &send_frames_[i],
sizeof(send_frames_[i])));
186 AERROR <<
"send message failed, error code: " << ret;
187 return ErrorCode::CAN_CLIENT_ERROR_BASE;
191 return ErrorCode::OK;
196 int32_t *
const frame_num) {
198 AERROR <<
"Nvidia can client is not init! Please init first!";
199 return ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED;
204 <<
"], frame_num:" << *frame_num;
206 return ErrorCode::CAN_CLIENT_ERROR_FRAME_NUM;
211 auto ret = read(dev_handler_, &recv_frames_[i],
sizeof(recv_frames_[i]));
214 AERROR <<
"receive message failed, error code: " << ret;
215 return ErrorCode::CAN_CLIENT_ERROR_BASE;
218 recv_frames_[i].can_dlc < 0) {
219 AERROR <<
"recv_frames_[" << i
220 <<
"].can_dlc = " << recv_frames_[i].can_dlc
221 <<
", which is not equal to can message data length ("
223 return ErrorCode::CAN_CLIENT_ERROR_RECV_FAILED;
226 cf.
id = FLAGS_enable_can_err_check
227 ? recv_frames_[i].can_id & CAN_EFF_MASK | CAN_ERR_FLAG
228 : recv_frames_[i].can_id & CAN_EFF_MASK;
230 cf.
id = (recv_frames_[i].can_id & CAN_SFF_MASK);
232 ADEBUG <<
"Socket can receive can id is " << recv_frames_[i].can_id;
233 cf.
len = recv_frames_[i].can_dlc;
234 std::memcpy(cf.
data, recv_frames_[i].data, recv_frames_[i].can_dlc);
235 frames->push_back(cf);
237 return ErrorCode::OK;