dad8e7f1bf0cbcb87a45a2faa8852b0e2944fb19
[apps/agl-service-can-low-level.git] / low-can-binding / can / message / j1939-message.cpp
1 /*
2  * Copyright (C) 2018, 2019 "IoT.bzh"
3  * Author "Arthur Guyader" <arthur.guyader@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 <cstring>
19 #include "../../binding/low-can-hat.hpp"
20 #include "j1939-message.hpp"
21
22 ///
23 /// @brief Class constructor
24 ///
25 /// j1939_message_t class constructor.
26 ///
27 j1939_message_t::j1939_message_t():
28     message_t(),
29     name_{0},
30     pgn_{0},
31     addr_{0}
32 {}
33
34 j1939_message_t::j1939_message_t(uint8_t length,
35     message_format_t format,
36     std::vector<uint8_t>& data,
37     uint64_t timestamp,
38     name_t name,
39     pgn_t pgn,
40     uint8_t addr):
41     message_t(length, format, data, timestamp),
42     name_{name},
43     pgn_{pgn},
44     addr_{addr}
45 {}
46
47 ///
48 /// @brief Retrieve name_ member value.
49 ///
50 /// @return name_ class member
51 ///
52 uint64_t j1939_message_t::get_name() const {
53     return name_;
54 }
55
56 ///
57 /// @brief Retrieve pgn_ member value.
58 ///
59 /// @return pgn_ class member
60 ///
61 uint32_t j1939_message_t::get_pgn() const{
62     return pgn_;
63 }
64
65 ///
66 /// @brief Retrieve addr_ member value.
67 ///
68 /// @return addr_ class member
69 ///
70 uint8_t j1939_message_t::get_addr() const{
71     return addr_;
72 }
73
74
75 /// @brief Take a sockaddr_can struct and array of data to initialize class members
76 ///
77 /// This is the preferred way to initialize class members.
78 ///
79 /// @param[in] addr - sockaddr_can to get pgn, name and addr
80 /// @param[in] data - array of data get from the j1939 socket
81 /// @param[in] nbytes - size of the array of data
82 /// @param[in] timestamp - timestamp of the message
83 ///
84 /// @return A j1939_message_t object fully initialized with sockaddr_can and data values.
85 std::shared_ptr<j1939_message_t> j1939_message_t::convert_from_addr(struct sockaddr_can& addr, uint8_t (&data)[128],size_t nbytes, uint64_t timestamp)
86 {
87         uint8_t length = 0;
88         message_format_t format;
89         std::vector<uint8_t> dataVector;
90
91     if(nbytes > J1939_MAX_DLEN)
92     {
93         AFB_DEBUG("Unsupported j1939 frame");
94         format = message_format_t::INVALID;
95     }
96     else
97     {
98         AFB_DEBUG("Got a j1939 frame");
99         format = message_format_t::J1939;
100     }
101
102     length = (uint8_t) nbytes;
103     dataVector.reserve(length);
104     int i;
105     dataVector.clear();
106     for(i=0;i<length;i++)
107     {
108         dataVector.push_back(data[i]);
109     };
110
111     AFB_DEBUG("Found pgn: %X, format: %X, length: %X, data %02X%02X%02X%02X%02X%02X%02X%02X",
112                             addr.can_addr.j1939.pgn, (uint8_t)format, length, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
113
114         return std::make_shared<j1939_message_t>(j1939_message_t(length, format, dataVector, timestamp,addr.can_addr.j1939.name,addr.can_addr.j1939.pgn,addr.can_addr.j1939.addr));
115 }
116
117 /// @brief Test if members pgn_ and length are set.
118 ///
119 /// @return boolean - true = set - false = not set
120 bool j1939_message_t::is_set()
121 {
122         return (pgn_ != 0 && length_ != 0);
123 }
124
125 /// @brief Generate a string with informations about the message
126 ///
127 /// @return Debug message with informations about members
128 std::string j1939_message_t::get_debug_message()
129 {
130         std::string ret = "";
131     ret = ret + "Here is the next j1939 message : pgn " + std::to_string(pgn_)  + " length " + std::to_string(length_) + ", data ";
132     for (size_t i = 0; i < data_.size(); i++)
133     {
134         ret = ret + std::to_string(data_[i]);
135     }
136     return ret;
137 }
138
139 ///
140 /// @brief Retrieve pgn_ member value.
141 ///
142 /// @return pgn_ class member
143 ///
144 uint32_t j1939_message_t::get_id() const
145 {
146     AFB_WARNING("Prefer method get_pgn() for j1939 messages");
147         return get_pgn();
148 }
149
150
151 struct bcm_msg j1939_message_t::get_bcm_msg()
152 {
153     AFB_WARNING("Not implemented");
154     struct bcm_msg bcm_msg;
155     ::memset(&bcm_msg, 0, sizeof(struct bcm_msg));
156         return bcm_msg;
157 }
158
159 void j1939_message_t::set_bcm_msg(struct bcm_msg bcm_msg)
160 {
161         AFB_WARNING("Not implemented");
162 }