aaab99f5066ea78e473e3eccc97aa9455b977786
[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          flags_{INVALID_FLAG},
32          timestamp_{0},
33          sub_id_{-1}
34 {}
35
36 /**
37  * @brief Construct a new message t::message t object
38  *
39  * @param maxdlen The maxdlen of a message
40  * @param length The length of the message
41  * @param format The format of the message
42  * @param data The data vector of the message
43  * @param timestamp The timestamp of the message
44  */
45 message_t::message_t(uint32_t maxdlen,
46         uint32_t length,
47         uint32_t flags,
48         std::vector<uint8_t>& data,
49         uint64_t timestamp)
50         : maxdlen_{maxdlen},
51         length_{length},
52         flags_{flags},
53         data_{data},
54         timestamp_{timestamp},
55         sub_id_{-1}
56 {}
57
58 /**
59  * @brief Return the sub_id of the message
60  *
61  * @return int The sub_id of the message
62  */
63 int message_t::get_sub_id() const
64 {
65         return sub_id_;
66 }
67
68 ///
69 /// @brief Retrieve data_ member value.
70 ///
71 /// @return pointer to the first element
72 ///  of class member data_
73 ///
74 const uint8_t* message_t::get_data() const
75 {
76         return data_.data();
77 }
78
79
80 ///
81 /// @brief Retrieve data_ member value.
82 ///
83 /// @return pointer to the first element
84 ///  of class member data_
85 ///
86 const std::vector<uint8_t> message_t::get_data_vector(int start,int end) const
87 {
88         std::vector<uint8_t> ret;
89         if(start >= 0)
90         {
91                 if(end<length_)
92                 {
93                         for(int i=start;i<=end;i++)
94                         {
95                                 ret.push_back(data_[i]);
96                         }
97                 }
98                 else
99                 {
100                         for(int i=start;i<length_;i++)
101                         {
102                                 ret.push_back(data_[i]);
103                         }
104                 }
105         }
106         else
107         {
108                 AFB_ERROR("Error index to get data vector, [%d-%d] - for length %d",start,end,length_);
109         }
110         return ret;
111 }
112
113 ///
114 /// @brief Retrieve data_ member whole vector
115 ///
116 /// @return the vector as is
117 ///
118 const std::vector<uint8_t> message_t::get_data_vector() const
119 {
120         return data_;
121 }
122
123 ///
124 /// @brief Retrieve length_ member value.
125 ///
126 /// @return length_ class member
127 ///
128 uint32_t message_t::get_length() const
129 {
130         return length_;
131 }
132
133 /**
134  * @brief Set data vector of the message
135  *
136  * @param data A vector of data
137  */
138 void message_t::set_data(std::vector<uint8_t> &data)
139 {
140         data_ = data;
141 }
142
143 /**
144  * @brief Set sub_id of the message
145  *
146  * @param sub_id The sub_id to set
147  */
148 void message_t::set_sub_id(int sub_id)
149 {
150         sub_id_ = sub_id;
151 }
152
153 /**
154  * @brief Return the timestamp of the message
155  *
156  * @return uint64_t The timestamp
157  */
158 uint64_t message_t::get_timestamp() const
159 {
160         return timestamp_;
161 }
162
163 uint32_t message_t::get_flags()
164 {
165         return flags_;
166 }
167
168 void message_t::set_flags(uint32_t flags)
169 {
170         flags_ = flags_ | flags;
171 }
172
173 uint32_t message_t::get_maxdlen()
174 {
175         return maxdlen_;
176 }
177
178
179 void message_t::set_maxdlen(uint32_t maxdlen)
180 {
181         maxdlen_ = maxdlen;
182 }