2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "can-message.hpp"
22 #include "low-can-binding.hpp"
24 /********************************************************************************
26 * CanMessage method implementation
28 *********************************************************************************/
30 can_message_t::can_message_t()
31 : id_{0}, length_{0}, format_{CanMessageFormat::ERROR}, data_{0,0,0,0,0,0,0,0}
34 uint32_t can_message_t::get_id() const
39 int can_message_t::get_format() const
41 if (format_ != CanMessageFormat::STANDARD || format_ != CanMessageFormat::EXTENDED)
42 return CanMessageFormat::ERROR;
46 const uint8_t* can_message_t::get_data() const
50 uint8_t can_message_t::get_length() const
55 bool can_message_t::is_correct_to_send()
57 if (id_ != 0 && length_ != 0 && format_ != CanMessageFormat::ERROR)
60 for(i=0;i<CAN_MESSAGE_SIZE;i++)
67 void can_message_t::set_id(const uint32_t new_id)
71 case CanMessageFormat::STANDARD:
72 id_ = new_id & CAN_SFF_MASK;
74 case CanMessageFormat::EXTENDED:
75 id_ = new_id & CAN_EFF_MASK;
78 ERROR(binder_interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
83 void can_message_t::set_format(const CanMessageFormat new_format)
85 if(new_format == CanMessageFormat::STANDARD || new_format == CanMessageFormat::EXTENDED)
88 ERROR(binder_interface, "ERROR: Can set format, wrong format chosen");
91 void can_message_t::set_data(const uint8_t new_data)
93 if ((sizeof(new_data) / sizeof(uint8_t) > CAN_MESSAGE_SIZE))
94 ERROR(binder_interface, "Can set data, your data array is too big");
97 ::memcpy(&data_, &new_data, sizeof(new_data));
98 length_ = sizeof(new_data);
102 void can_message_t::convert_from_canfd_frame(const canfd_frame& frame)
104 length_ = (frame.len > CAN_MAX_DLEN) ? (uint8_t)CAN_MAX_DLEN : frame.len;
105 length_ = (frame.len > CANFD_MAX_DLEN) ? (uint8_t)CANFD_MAX_DLEN : frame.len;
107 if (frame.can_id & CAN_ERR_FLAG)
109 id_ = frame.can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
110 format_ = CanMessageFormat::ERROR;
112 else if (frame.can_id & CAN_EFF_FLAG)
114 id_ = frame.can_id & CAN_EFF_MASK;
115 format_ = CanMessageFormat::EXTENDED;
119 id_ = frame.can_id & CAN_SFF_MASK;
120 format_ = CanMessageFormat::STANDARD;
122 DEBUG(binder_interface, "");
124 if (sizeof(frame.data) <= sizeof(data_))
125 ::memcpy(&data_, frame.data, length_);
126 else if (sizeof(frame.data) >= CAN_MAX_DLEN)
127 ERROR(binder_interface, "can_message_t: canfd_frame data too long to be stored into CanMessage object");
130 canfd_frame can_message_t::convert_to_canfd_frame()
134 if(is_correct_to_send())
136 frame.can_id = get_id();
137 frame.len = get_length();
138 ::memcpy(frame.data, get_data(), length_);
141 ERROR(binder_interface, "can_message_t not correctly initialized to be sent");