encoder: use switch case to handle CAN protocols
[apps/agl-service-can-low-level.git] / low-can-binding / can / can-encoder.cpp
index b87407f..f0ea092 100644 (file)
 
 #include "canutil/write.h"
 #include "../utils/openxc-utils.hpp"
-#include "can-message-definition.hpp"
-#include "../binding/low-can-hat.hpp"
+#include "message-definition.hpp"
+#include "../utils/converter.hpp"
 
-/// @brief Write a value into a CAN signal in the destination buffer.
-///
-/// @param[in] signal - The CAN signal to write, including the bit position and bit size.
-/// @param[in] value - The encoded integer value to write into the CAN signal.
-/// @param[out] data - The destination buffer.
-/// @param[in] length - The length of the destination buffer.
-///
-/// @return Returns a can_frame struct initialized and ready to be send.
-const can_frame build_frame(const can_signal_t& signal, uint64_t value)
+/**
+ * @brief Allows to encode data for a signal
+ *
+ * @param sig The signal to know its location
+ * @param data The data to encod
+ * @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)
 {
-       struct can_frame cf;
-       ::memset(&cf, 0, sizeof(cf));
-
-       cf.can_id = signal.get_message()->get_id();
-       cf.can_dlc = CAN_MAX_DLEN;
-       bitfield_encode_float((float)value,
-                                               signal.get_bit_position(),
-                                               signal.get_bit_size(),
-                                               signal.get_factor(),
-                                               signal.get_offset(),
-                                               cf.data,
-                                               CAN_MAX_DLEN);
-
-       return cf;
+       uint32_t bit_size = sig->get_bit_size();
+       uint32_t bit_position = sig->get_bit_position();
+       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);
+
+       int len_signal_bytes_tmp = new_end_byte - new_start_byte + 1;
+
+       uint8_t len_signal_bytes = 0;
+       if(len_signal_bytes_tmp > 255)
+       {
+               AFB_ERROR("Error signal %s too long", sig->get_name().c_str());
+       }
+       else
+       {
+               len_signal_bytes = (uint8_t) len_signal_bytes_tmp;
+       }
+/*
+       if(new_start_bit > 255)
+       {
+               AFB_ERROR("Error signal %s too long", sig->get_name().c_str());
+       }
+*/
+       uint8_t new_bit_size = 0;
+       if(bit_size > 255)
+       {
+               AFB_ERROR("Error signal %s to long bit size", sig->get_name().c_str());
+       }
+       else
+       {
+               new_bit_size = (uint8_t) bit_size;
+       }
+
+       uint8_t data_signal[len_signal_bytes] = {0};
+       float factor_v = 1;
+       if(factor)
+       {
+               factor_v = sig->get_factor();
+       }
+
+       float offset_v = 0;
+       if(factor)
+       {
+               offset_v = sig->get_offset();
+       }
+
+       if(filter)
+       {
+               uint8_t tmp = 0;
+               int j=0;
+               for(int i=0;i<new_bit_size;i++)
+               {
+                       int mask = 0x80 >> ((i%8)+new_start_bit);
+
+                       uint8_t mask_v = 0;
+                       if(mask > 255)
+                       {
+                               AFB_ERROR("Error mask too large");
+                       }
+                       else
+                       {
+                               mask_v = (uint8_t) mask;
+                       }
+                       tmp = tmp|mask_v;
+
+                       if(i%8 == 7)
+                       {
+                               data_signal[j] = tmp;
+                               tmp = 0;
+                               j++;
+                       }
+               }
+               data_signal[j]=tmp;
+       }
+       else
+       {
+               bitfield_encode_float(  sig->get_last_value(),
+                                               new_start_bit,
+                                               new_bit_size,
+                                               factor_v,
+                                               offset_v,
+                                               data_signal,
+                                               len_signal_bytes);
+       }
+
+       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((float)value);
+       std::vector<uint8_t> data;
+       for(int i = 0; i<message->get_length();i++)
+       {
+               data.push_back(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.
@@ -62,7 +224,7 @@ const can_frame build_frame(const can_signal_t& signal, uint64_t value)
 /// @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 can_signal_t& signal, bool value, bool* send)
+uint64_t encoder_t::encode_boolean(const signal_t& signal, bool value, bool* send)
 {
        return encode_number(signal, float(value), send);
 }
@@ -80,7 +242,7 @@ uint64_t encoder_t::encode_boolean(const can_signal_t& signal, bool value, bool*
 /// @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 can_signal_t& signal, float value, bool* send)
+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());
 }
@@ -102,7 +264,7 @@ uint64_t encoder_t::encode_number(const can_signal_t& signal, float value, bool*
 /// @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 can_signal_t& signal, const std::string& state, bool* send)
+uint64_t encoder_t::encode_state(const signal_t& signal, const std::string& state, bool* send)
 {
        uint64_t value = 0;
        if(state == "")
@@ -126,7 +288,7 @@ uint64_t encoder_t::encode_state(const can_signal_t& signal, const std::string&
 /// @brief Parse a signal from a CAN message and apply any required
 /// transforations to get a human readable value.
 ///
-/// If the can_signal_t has a non-NULL 'decoder' field, the raw CAN signal 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.
@@ -137,7 +299,7 @@ uint64_t encoder_t::encode_state(const can_signal_t& signal, const std::string&
 /// @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( can_signal_t& signal, const openxc_DynamicField& field, bool* send)
+uint64_t encoder_t::encode_DynamicField( signal_t& signal, const openxc_DynamicField& field, bool* send)
 {
        uint64_t value = 0;
        switch(field.type) {