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