Move attributes flags and maxdlen
[apps/agl-service-can-low-level.git] / low-can-binding / can / message / message.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  *
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include "./message.hpp"
19
20 #include <cstring>
21
22 #include "../../binding/low-can-hat.hpp"
23
24 /**
25  * @brief Construct a new message t::message t object
26  *
27  */
28 message_t::message_t()
29         : maxdlen_{0},
30          length_{0},
31          format_{message_format_t::INVALID},
32          timestamp_{0},
33          sub_id_{-1},
34          flags_{0}
35 {}
36
37 /**
38  * @brief Construct a new message t::message t object
39  *
40  * @param maxdlen The maxdlen of a message
41  * @param length The length of the message
42  * @param format The format of the message
43  * @param data The data vector of the message
44  * @param timestamp The timestamp of the message
45  */
46 message_t::message_t(uint32_t maxdlen,
47         uint32_t length,
48         message_format_t format,
49         std::vector<uint8_t>& data,
50         uint64_t timestamp,
51         uint32_t flags)
52         : maxdlen_{maxdlen},
53         length_{length},
54         format_{format},
55         data_{data},
56         timestamp_{timestamp},
57         sub_id_{-1},
58         flags_{flags}
59 {}
60
61 /**
62  * @brief Return the sub_id of the message
63  *
64  * @return int The sub_id of the message
65  */
66 int message_t::get_sub_id() const
67 {
68         return sub_id_;
69 }
70
71 ///
72 /// @brief Retrieve data_ member value.
73 ///
74 /// @return pointer to the first element
75 ///  of class member data_
76 ///
77 const uint8_t* message_t::get_data() const
78 {
79         return data_.data();
80 }
81
82
83 ///
84 /// @brief Retrieve data_ member value.
85 ///
86 /// @return pointer to the first element
87 ///  of class member data_
88 ///
89 const std::vector<uint8_t> message_t::get_data_vector(int start,int end) const
90 {
91         std::vector<uint8_t> ret;
92         if(start >= 0)
93         {
94                 if(end<length_)
95                 {
96                         for(int i=start;i<=end;i++)
97                         {
98                                 ret.push_back(data_[i]);
99                         }
100                 }
101                 else
102                 {
103                         for(int i=start;i<length_;i++)
104                         {
105                                 ret.push_back(data_[i]);
106                         }
107                 }
108         }
109         else
110         {
111                 AFB_ERROR("Error index to get data vector, [%d-%d] - for length %d",start,end,length_);
112         }
113         return ret;
114 }
115
116 ///
117 /// @brief Retrieve data_ member whole vector
118 ///
119 /// @return the vector as is
120 ///
121 const std::vector<uint8_t> message_t::get_data_vector() const
122 {
123         return data_;
124 }
125
126 ///
127 /// @brief Retrieve length_ member value.
128 ///
129 /// @return length_ class member
130 ///
131 uint32_t message_t::get_length() const
132 {
133         return length_;
134 }
135
136 /**
137  * @brief Set data vector of the message
138  *
139  * @param data A vector of data
140  */
141 void message_t::set_data(std::vector<uint8_t> &data)
142 {
143         data_ = data;
144 }
145
146 /**
147  * @brief Set sub_id of the message
148  *
149  * @param sub_id The sub_id to set
150  */
151 void message_t::set_sub_id(int sub_id)
152 {
153         sub_id_ = sub_id;
154 }
155
156 /**
157  * @brief Return the timestamp of the message
158  *
159  * @return uint64_t The timestamp
160  */
161 uint64_t message_t::get_timestamp() const
162 {
163         return timestamp_;
164 }
165
166 /**
167  * @brief Return the format of the message
168  *
169  * @return message_format_t The message format
170  */
171 message_format_t message_t::get_msg_format()
172 {
173         return format_;
174 }
175
176
177 uint32_t message_t::get_flags()
178 {
179         return flags_;
180 }
181
182 void message_t::set_flags(uint32_t flags)
183 {
184         flags_ = flags_ | flags;
185 }
186
187 uint32_t message_t::get_maxdlen()
188 {
189         return maxdlen_;
190 }
191
192
193 void message_t::set_maxdlen(uint32_t maxdlen)
194 {
195         maxdlen_ = maxdlen;
196 }