summaryrefslogtreecommitdiffstats
path: root/low-can-binding/can/can-encoder.cpp
blob: 596cc7e20b6bd9f06a88da6bb751aefd76a93416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright (C) 2015, 2016 "IoT.bzh"
 * Author "Romain Forlot" <romain.forlot@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	 http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "can-encoder.hpp"

#include "canutil/write.h"
#include "../utils/openxc-utils.hpp"
#include "message-definition.hpp"
#include "../utils/converter.hpp"

/**
 * @brief Allows to encode data for a signal
 *
 * @param sig The signal to know its location
 * @param data The data to encode
 * @param filter If true that will generate the filter BCM for the signal
 * @param factor If true that will use the factor of the signal else 1
 * @param offset If true that will use the offset of the signal else 0
 */
void encoder_t::encode_data(std::shared_ptr<signal_t> sig, std::vector<uint8_t> &data, bool filter, bool factor, bool offset)
{
	uint32_t bit_size = sig->get_bit_size();
	uint32_t bit_position = sig->get_bit_position();
	float factor_v = factor ? sig->get_factor() : 1;
	float offset_v = offset ? sig->get_offset() : 0;

	int new_start_byte = 0;
	int new_end_byte = 0;
	uint8_t new_start_bit = 0;
	uint8_t new_end_bit = 0;

	converter_t::signal_to_bits_bytes(bit_position, bit_size, new_start_byte, new_end_byte, new_start_bit, new_end_bit);
	std::vector<uint8_t> data_signal(new_end_byte - new_start_byte + 1);

	if(filter)
	{
		for (auto& elt: data_signal)
			elt = 0xFF;
		uint8_t mask_first_v = static_cast<uint8_t>(0xFF << new_start_bit);
		uint8_t mask_last_v = static_cast<uint8_t>(0xFF >> (7 - new_end_bit));

		if(new_start_byte == new_end_byte)
		{
			data_signal[0] = mask_first_v & mask_last_v;
		}
		else
		{
			data_signal[0] = mask_first_v;
			data_signal[new_end_byte - new_start_byte] = mask_last_v;
		}
	}
	else
	{
		bitfield_encode_float(sig->get_last_value(),
				      new_start_bit,
				      bit_size,
				      factor_v,
				      offset_v,
				      data_signal.data(),
				      bit_size);
	}

	for(size_t i = new_start_byte; i <= new_end_byte ; i++)
		data[i] = data[i] | data_signal[i-new_start_byte];
}

/**
 * @brief Allows to build a multi frame message with correct data to be send
 *
 * @param signal The CAN signal to write, including the bit position and bit size.
 * @param value The encoded integer value to write in the CAN signal.
 * @param message A multi frame message to complete
 * @param factor If true that will use the factor of the signal else 1
 * @param offset If true that will use the offset of the signal else 0
 * @return message_t*  The message that is generated
 */
message_t* encoder_t::build_frame(const std::shared_ptr<signal_t>& signal, uint64_t value, message_t *message, bool factor, bool offset)
{
	signal->set_last_value(static_cast<float>(value));
	std::vector<uint8_t> data(message->get_length(), 0);

	for(const auto& sig: signal->get_message()->get_signals())
		encode_data(sig, data, false, factor, offset);

	message->set_data(data);
	return message;
}

/**
 * @brief Allows to build a message_t with correct data to be send
 *
 * @param signal The CAN signal to write, including the bit position and bit size.
 * @param value The encoded integer value to write in the CAN signal.
 * @param factor If true that will use the factor of the signal else 1
 * @param offset If true that will use the offset of the signal else 0
 * @return message_t* The message that is generated
 */
message_t* encoder_t::build_message(const std::shared_ptr<signal_t>& signal, uint64_t value, bool factor, bool offset)
{
	message_t *message;
	std::vector<uint8_t> data;
	switch(signal->get_message()->get_flags())
	{
		case CAN_PROTOCOL_WITH_FD_FRAME:
			message = new can_message_t(CANFD_MAX_DLEN,
						    signal->get_message()->get_id(),
						    CANFD_MAX_DLEN,
						    false,
						    signal->get_message()->get_flags(),
						    data,
						    0);
			return build_frame(signal, value, message, factor, offset);
#ifdef USE_FEATURE_J1939
		case J1939_PROTOCOL:
			message = new j1939_message_t(signal->get_message()->get_length(),
						      data,
						      0,
						      J1939_NO_NAME,
						      signal->get_message()->get_id(),
						      J1939_NO_ADDR);
			return build_frame(signal, value, message, factor, offset);
#endif
		case CAN_PROTOCOL:
			message = new can_message_t(CAN_MAX_DLEN,
						    signal->get_message()->get_id(),
						    CAN_MAX_DLEN,
						    false,
						    signal->get_message()->get_flags(),
						    data,
						    0);
			return build_frame(signal, value, message, factor, offset);
		default:
			message = new can_message_t(CAN_MAX_DLEN,
						    signal->get_message()->get_id(),
						    CAN_MAX_DLEN,
						    false,
						    signal->get_message()->get_flags(),
						    data,
						    0);
			return build_frame(signal, value, message, factor, offset);
	}

}

/// @brief Encode a boolean into an integer, fit for a CAN signal bitfield.
///
/// This is a shortcut for encodeDynamicField(CanSignal*, openxc_DynamicField*,
/// bool*) that takes care of creating the DynamicField object for you with the
/// boolean value.
///
/// @param[in] signal  - The CAN signal to encode this value for..
/// @param[in] value - The boolean value to encode
/// @param[out] send - An output argument that will be set to false if the value should
///     not be sent for any reason.
///
/// @return Returns the encoded integer. If 'send' is changed to false, the field could
/// not be encoded and the return value is undefined.
///
uint64_t encoder_t::encode_boolean(const signal_t& signal, bool value, bool* send)
{
	return encode_number(signal, float(value), send);
}
/// @brief Encode a float into an integer, fit for a CAN signal's bitfield.
///
/// This is a shortcut for encodeDynamicField(CanSignal*, openxc_DynamicField*,
/// bool*) that takes care of creating the DynamicField object for you with the
/// float value.
///
/// @param[in] signal  - The CAN signal to encode this value for.
/// @param[in] value - The float value to encode.
/// @param[out] send - This output argument will always be set to false, so the caller will
///      know not to publish this value to the pipeline.
///
/// @return Returns the encoded integer. If 'send' is changed to false, the field could
/// not be encoded and the return value is undefined.
///
uint64_t encoder_t::encode_number(const signal_t& signal, float value, bool* send)
{
	return float_to_fixed_point(value, signal.get_factor(), signal.get_offset());
}

/// @brief Encode a string into an integer, fit for a CAN signal's bitfield.
///
/// Be aware that the behavior is undefined if there are multiple values assigned
/// to a single state. See https://github.com/openxc/vi-firmware/issues/185.
///
/// This is a shortcut for encodeDynamicField(CanSignal*, openxc_DynamicField*,
/// bool*) that takes care of creating the DynamicField object for you with the
/// string state value.
///
/// @param[in] signal  - The details of the signal that contains the state mapping.
/// @param[in] value - The string state value to encode.
/// @param[out] send - An output argument that will be set to false if the value should
///     not be sent for any reason.
///
/// @return Returns the encoded integer. If 'send' is changed to false, the field could
/// not be encoded and the return value is undefined.
///
uint64_t encoder_t::encode_state(const signal_t& signal, const std::string& state, bool* send)
{
	uint64_t value = 0;
	if(state == "")
	{
		AFB_DEBUG("Can't write state of "" -- not sending");
		*send = false;
	}
	else
	{
		uint64_t signal_state = signal.get_states(state);
		if(signal_state != -1) {
			value = signal_state;
		} else {
			AFB_DEBUG("Couldn't find a valid signal state for %s", state.c_str());
			*send = false;
		}
	}
	return value;
}

/// @brief Parse a signal from a CAN message and apply any required
/// transforations to get a human readable value.
///
/// If the signal_t has a non-NULL 'decoder' field, the raw CAN signal value
/// will be passed to the decoder before returning.
///
/// @param[in] signal - The details of the signal to decode and forward.
/// @param[in] value - The numerical value that will be converted to a boolean.
/// @param[out] send - An output parameter that will be flipped to false if the value could
///      not be decoded.
///
/// @return The decoder returns an openxc_DynamicField, which may contain a number,
/// string or boolean. If 'send' is false, the return value is undefined.
///
uint64_t encoder_t::encode_DynamicField( signal_t& signal, const openxc_DynamicField& field, bool* send)
{
	uint64_t value = 0;
	switch(field.type) {
		case openxc_DynamicField_Type_STRING:
			value = encode_state(signal, field.string_value, send);
			break;
		case openxc_DynamicField_Type_NUM:
			value = encode_number(signal, (float)field.numeric_value, send);
			break;
		case openxc_DynamicField_Type_BOOL:
			value = encode_boolean(signal, field.boolean_value, send);
			break;
		default:
			AFB_DEBUG("Dynamic field didn't have a value, can't encode");
			*send = false;
			break;
	}
	return value;
}