summaryrefslogtreecommitdiffstats
path: root/low-can-binding
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-07-13 18:51:20 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-07-21 12:02:11 +0200
commit2408a177e8260dcda266df9993e6acd528bebbbc (patch)
tree32fbc74e96659d701496a3687ef9c0496dd4fd07 /low-can-binding
parent0dc8b87f5d8a03183c1b947640f6315545e4207b (diff)
Be able to write raw frame & encode value to sent
Check whether the argument is a raw frame or a Signal + Value to encode then send. 1 new class to handle encoding part of a value as well as a new method for can_signal_t which return index of a map from its value. Change-Id: I288d2aeec28ce74c9ca35750db18557c9251e1df Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'low-can-binding')
-rw-r--r--low-can-binding/CMakeLists.txt1
-rw-r--r--low-can-binding/binding/low-can-cb.cpp94
-rw-r--r--low-can-binding/can/can-encoder.cpp159
-rw-r--r--low-can-binding/can/can-encoder.hpp33
-rw-r--r--low-can-binding/can/can-signals.cpp14
-rw-r--r--low-can-binding/can/can-signals.hpp1
6 files changed, 272 insertions, 30 deletions
diff --git a/low-can-binding/CMakeLists.txt b/low-can-binding/CMakeLists.txt
index fd68e6cf..44ae9ef8 100644
--- a/low-can-binding/CMakeLists.txt
+++ b/low-can-binding/CMakeLists.txt
@@ -34,6 +34,7 @@ PROJECT_TARGET_ADD(low-can)
can/can-message.cpp
can/can-signals.cpp
can/can-decoder.cpp
+ can/can-encoder.cpp
diagnostic/diagnostic-message.cpp
diagnostic/diagnostic-manager.cpp
diagnostic/active-diagnostic-request.cpp
diff --git a/low-can-binding/binding/low-can-cb.cpp b/low-can-binding/binding/low-can-cb.cpp
index a7e4396c..5910f6a2 100644
--- a/low-can-binding/binding/low-can-cb.cpp
+++ b/low-can-binding/binding/low-can-cb.cpp
@@ -28,6 +28,7 @@
#include "openxc.pb.h"
#include "application.hpp"
+#include "../can/can-encoder.hpp"
#include "../can/can-bus.hpp"
#include "../can/can-signals.hpp"
#include "../can/can-message.hpp"
@@ -372,54 +373,87 @@ void swrite(struct afb_req request)
int rc = 0;
struct can_frame cf;
struct utils::signals_found sf;
- struct json_object *args, *json_can_socket, *json_can_id, *json_can_dlc, *json_can_data;
+ struct json_object* args = nullptr,
+ *json_name = nullptr,
+ *json_value = nullptr;
+ std::map<std::string, std::shared_ptr<low_can_socket_t> >& cd = application_t::instance().get_can_devices();
::memset(&cf, 0, sizeof(cf));
args = afb_req_json(request);
- if (args == NULL || (
- ((!json_object_object_get_ex(args, "canbus_name", &json_can_socket)) && json_object_get_type(json_can_socket) == json_type_string) &&
- ((!json_object_object_get_ex(args, "can_id", &json_can_id)) && json_object_get_type(json_can_id) == json_type_int) &&
- ((!json_object_object_get_ex(args, "can_dlc", &json_can_dlc)) && json_object_get_type(json_can_dlc) == json_type_int)))
- {
- cf.can_id = json_object_get_int(json_can_id);
- cf.can_dlc = (uint8_t)json_object_get_int(json_can_dlc);
- openxc_DynamicField search_key = build_DynamicField((double)cf.can_id);
- sf = utils::signals_manager_t::instance().find_signals(search_key);
- }
- if((args == NULL || !json_object_object_get_ex(args, "can_data", &json_can_data)) && json_object_get_type(json_can_data) == json_type_array)
+ // Process about Raw CAN message on CAN bus directly
+ if (args != NULL &&
+ (json_object_object_get_ex(args, "bus_name", &json_name) && json_object_is_type(json_name, json_type_string) ) &&
+ (json_object_object_get_ex(args, "frame", &json_value) && json_object_is_type(json_value, json_type_object) ))
{
- struct json_object *x;
+ struct json_object* json_can_id = nullptr,
+ *json_can_dlc = nullptr,
+ *json_can_data = nullptr;
- int n = json_object_array_length(json_can_data);
- if(n < 8)
+ if( (json_object_object_get_ex(json_value, "can_id", &json_can_id) && (json_object_is_type(json_can_id, json_type_double) || json_object_is_type(json_can_id, json_type_int))) &&
+ (json_object_object_get_ex(json_value, "can_dlc", &json_can_dlc) && (json_object_is_type(json_can_dlc, json_type_double) || json_object_is_type(json_can_dlc, json_type_int))) &&
+ (json_object_object_get_ex(json_value, "can_data", &json_can_data) && json_object_is_type(json_can_data, json_type_array) ))
{
- for (int i = 0 ; i < n ; i++)
+ cf.can_id = json_object_get_int(json_can_id);
+ cf.can_dlc = (uint8_t)json_object_get_int(json_can_dlc);
+
+ struct json_object *x;
+ int n = json_object_array_length(json_can_data);
+ if(n <= 8)
{
- x = json_object_array_get_idx(json_can_data, i);
- cf.data[i] = json_object_get_type(x) == json_type_int ? (uint8_t)json_object_get_int(x) : 0;
+ for (int i = 0 ; i < n ; i++)
+ {
+ x = json_object_array_get_idx(json_can_data, i);
+ cf.data[i] = json_object_get_type(x) == json_type_int ? (uint8_t)json_object_get_int(x) : 0;
+ }
}
- }
- }
- if (sf.can_signals.empty() && sf.diagnostic_messages.empty())
- {
- AFB_WARNING("No signal(s) found for id %d. Message not sent.", cf.can_id);
- rc = -1;
+ const std::string bus_name = json_object_get_string(json_name);
+ const std::string found_device = application_t::instance().get_can_bus_manager().get_can_device_name(bus_name);
+ if( ! found_device.empty())
+ {
+ if( cd.count(bus_name) == 0)
+ {cd[bus_name] = std::make_shared<low_can_socket_t>(low_can_socket_t());}
+ rc = cd[bus_name]->tx_send(cf, found_device);
+ }
+ }
+ else
+ {
+ AFB_ERROR("Frame object malformed (must be \n \"frame\": {\"can_id\": int, \"can_dlc\": int, \"can_data\": [ int, int , int, int ,int , int ,int ,int]}");
+ rc = -1;
+ }
}
- else
+ // Search signal then encode value.
+ else if(args != NULL &&
+ (json_object_object_get_ex(args, "signal_name", &json_name) && json_object_is_type(json_name, json_type_string)) &&
+ (json_object_object_get_ex(args, "signal_value", &json_value) && (json_object_is_type(json_value, json_type_double) || json_object_is_type(json_value, json_type_int))))
{
- std::map<std::string, std::shared_ptr<low_can_socket_t> >& cd = application_t::instance().get_can_devices();
- const char* can_socket = json_object_get_string(json_can_socket);
- for(const auto& sig: sf.can_signals)
+ openxc_DynamicField search_key = build_DynamicField(json_object_get_string(json_name));
+ sf = utils::signals_manager_t::instance().find_signals(search_key);
+
+ if (sf.can_signals.empty())
+ {
+ AFB_WARNING("No signal(s) found for id %d. Message not sent.", cf.can_id);
+ rc = -1;
+ }
+ else
{
- if (sig->get_message()->get_bus_name().c_str() == can_socket)
+ for(const auto& sig: sf.can_signals)
{
- rc = cd[can_socket]->tx_send(cf, sig);
+ cf = encoder_t::build_frame(sig, (uint64_t)json_object_get_double(json_value));
+ const std::string bus_name = sig->get_message()->get_bus_name();
+ if( cd.count(bus_name) == 0)
+ {cd[bus_name] = std::make_shared<low_can_socket_t>(low_can_socket_t());}
+ rc = cd[bus_name]->tx_send(cf, sig);
}
}
}
+ else
+ {
+ AFB_ERROR("Request argument malformed. Please use the following syntax:");
+ rc = -1;
+ }
if (rc >= 0)
afb_req_success(request, NULL, NULL);
diff --git a/low-can-binding/can/can-encoder.cpp b/low-can-binding/can/can-encoder.cpp
new file mode 100644
index 00000000..b87407f1
--- /dev/null
+++ b/low-can-binding/can/can-encoder.cpp
@@ -0,0 +1,159 @@
+/*
+ * 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 "can-message-definition.hpp"
+#include "../binding/low-can-hat.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)
+{
+ 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;
+}
+
+/// @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 can_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 can_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 can_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 can_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( can_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;
+}
diff --git a/low-can-binding/can/can-encoder.hpp b/low-can-binding/can/can-encoder.hpp
new file mode 100644
index 00000000..f7e8394f
--- /dev/null
+++ b/low-can-binding/can/can-encoder.hpp
@@ -0,0 +1,33 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include "can-signals.hpp"
+#include "can-message.hpp"
+#include "openxc.pb.h"
+
+class encoder_t
+{
+public:
+ static const can_frame build_frame(const can_signal_t& signal, uint64_t value);
+ static uint64_t encode_state(const can_signal_t& signal, const std::string& value, bool* send);
+ static uint64_t encode_boolean(const can_signal_t& signal, bool value, bool* send);
+ static uint64_t encode_number(const can_signal_t& signal, float value, bool* send);
+
+ static uint64_t encode_DynamicField(can_signal_t& signal, const openxc_DynamicField& field, bool* send);
+};
diff --git a/low-can-binding/can/can-signals.cpp b/low-can-binding/can/can-signals.cpp
index 573808aa..45c54cb4 100644
--- a/low-can-binding/can/can-signals.cpp
+++ b/low-can-binding/can/can-signals.cpp
@@ -141,6 +141,20 @@ const std::string can_signal_t::get_states(uint8_t value)
return std::string();
}
+uint64_t can_signal_t::get_states(const std::string& value) const
+{
+ uint64_t ret = -1;
+ for( const auto& state: states_)
+ {
+ if(state.second == value)
+ {
+ ret = (uint64_t)state.first;
+ break;
+ }
+ }
+ return ret;
+}
+
size_t can_signal_t::get_state_count() const
{
return states_.size();
diff --git a/low-can-binding/can/can-signals.hpp b/low-can-binding/can/can-signals.hpp
index f7ff0372..1b27622e 100644
--- a/low-can-binding/can/can-signals.hpp
+++ b/low-can-binding/can/can-signals.hpp
@@ -133,6 +133,7 @@ public:
bool get_force_send_changed() const;
const std::map<uint8_t, std::string>& get_states() const;
const std::string get_states(uint8_t value);
+ uint64_t get_states(const std::string& value) const;
size_t get_state_count() const;
bool get_writable() const;
signal_decoder& get_decoder();