#include <thread>
#include <wrap-json.h>
#include <systemd/sd-event.h>
-
#include "openxc.pb.h"
#include "application.hpp"
#include "../can/can-encoder.hpp"
#include "../diagnostic/diagnostic-message.hpp"
#include "../utils/openxc-utils.hpp"
+#ifdef USE_FEATURE_J1939
+ #include "../can/message/j1939-message.hpp"
+ #include <linux/can/j1939.h>
+#endif
///******************************************************************************
///
/// SystemD event loop Callbacks
int read_message(sd_event_source *event_source, int fd, uint32_t revents, void *userdata)
{
+
low_can_subscription_t* can_subscription = (low_can_subscription_t*)userdata;
if ((revents & EPOLLIN) != 0)
{
- std::shared_ptr<utils::socketcan_t> s = can_subscription->get_socket();
- std::shared_ptr<message_t> message = s->read_message();
-
- // Sure we got a valid CAN message ?
- if (! message->get_id() == 0 && ! message->get_length() == 0)
+ utils::signals_manager_t& sm = utils::signals_manager_t::instance();
+ std::lock_guard<std::mutex> subscribed_signals_lock(sm.get_subscribed_signals_mutex());
+ if(can_subscription->get_index() != -1)
{
- push_n_notify(message);
+ std::shared_ptr<utils::socketcan_t> s = can_subscription->get_socket();
+ if(s->socket() && s->socket() != -1)
+ {
+ std::shared_ptr<message_t> message = s->read_message();
+
+ // Sure we got a valid CAN message ?
+ if (! message->get_id() == 0 && ! message->get_length() == 0 && message->get_msg_format() != message_format_t::INVALID)
+ {
+ push_n_notify(message);
+ }
+ }
}
}
}
// Event doesn't exist , so let's create it
- if (! subscription_exists &&
- (ret = can_subscription->subscribe(request)) < 0)
+ if ((ret = can_subscription->subscribe(request)) < 0)
return ret;
if(! subscription_exists)
if( (ret = s[sub_index]->unsubscribe(request)) < 0)
return ret;
+ s.find(sub_index)->second->set_index(-1);
s.erase(sub_index);
-
return ret;
}
event_filter.frequency = event_filter.frequency == 0 ? sig->get_frequency() : event_filter.frequency;
std::shared_ptr<low_can_subscription_t> can_subscription;
- auto it = std::find_if(s.begin(), s.end(), [&sig](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub){ return (! sub.second->get_diagnostic_message().empty());});
+ auto it = std::find_if(s.begin(), s.end(), [&sig](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub)
+ {
+ return (! sub.second->get_diagnostic_message().empty());
+ });
can_subscription = it != s.end() ?
it->second :
std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
else
{
if(sig->get_supported())
- {AFB_DEBUG("%s cancelled due to unsubscribe", sig->get_name().c_str());}
+ {
+ AFB_DEBUG("%s cancelled due to unsubscribe", sig->get_name().c_str());
+ }
else
{
AFB_WARNING("signal: %s isn't supported. Canceling operation.", sig->get_name().c_str());
rets++;
}
-
return rets;
}
int rets = 0;
for(const auto& sig: signals)
{
- auto it = std::find_if(s.begin(), s.end(), [&sig, &event_filter](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub){ return sub.second->is_signal_subscription_corresponding(sig, event_filter) ; });
+ auto it = std::find_if(s.begin(), s.end(), [&sig, &event_filter](std::pair<int, std::shared_ptr<low_can_subscription_t> > sub)
+ {
+ return sub.second->is_signal_subscription_corresponding(sig, event_filter) ;
+ });
std::shared_ptr<low_can_subscription_t> can_subscription;
if(it != s.end())
{can_subscription = it->second;}
// 2 cases : ID(PGN) and event
- json_bool test_event = json_object_object_get_ex(args,"event",&event);
+ json_object_object_get_ex(args,"event",&event);
json_bool test_id = json_object_object_get_ex(args,"id",&id);
if(!test_id)
{
- json_object_object_get_ex(args,"pgn",&id);
+ test_id = json_object_object_get_ex(args,"pgn",&id);
}
- if( args == NULL || (!test_event && !id))
+ if( args == NULL || (id && ((std::string)json_object_get_string(id)).compare("*") == 0))
{
rc = one_subscribe_unsubscribe_events(request, subscribe, "*", args);
}
do_subscribe_unsubscribe(request, false);
}
-static int send_frame(struct canfd_frame& cfd, const std::string& bus_name)
+/*
+static int send_frame(struct canfd_frame& cfd, const std::string& bus_name, socket_type type)
{
- if(bus_name.empty()) {
+ if(bus_name.empty())
+ {
return -1;
}
std::map<std::string, std::shared_ptr<low_can_subscription_t> >& cd = application_t::instance().get_can_devices();
if( cd.count(bus_name) == 0)
- {cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t());}
+ {
+ cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t());
+ }
- return low_can_subscription_t::tx_send(*cd[bus_name], cfd, bus_name);
-}
-static void write_raw_frame(afb_req_t request, const std::string& bus_name, json_object *json_value)
+ if(type == socket_type::BCM)
+ {
+ return low_can_subscription_t::tx_send(*cd[bus_name], cfd, bus_name);
+ }
+ else if(type == socket_type::J1939)
+ {
+ return low_can_subscription_t::j1939_send(*cd[bus_name], cfd, bus_name);
+ }
+ else{
+ return -1;
+ }
+}
+*/
+static int send_message(message_t *message, const std::string& bus_name, socket_type type)
{
- struct canfd_frame cfd;
- struct json_object *can_data = nullptr;
+ if(bus_name.empty())
+ {
+ return -1;
+ }
- ::memset(&cfd, 0, sizeof(cfd));
+ std::map<std::string, std::shared_ptr<low_can_subscription_t> >& cd = application_t::instance().get_can_devices();
- if(wrap_json_unpack(json_value, "{si, si, so !}",
- "can_id", &cfd.can_id,
- "can_dlc", &cfd.len,
- "can_data", &can_data))
+ if( cd.count(bus_name) == 0)
{
- afb_req_fail(request, "Invalid", "Frame object malformed");
- return;
+ cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t());
}
- if(cfd.len <= 8 && cfd.len > 0)
+
+ if(type == socket_type::BCM)
+ {
+ return low_can_subscription_t::tx_send(*cd[bus_name], message, bus_name);
+ }
+#ifdef USE_FEATURE_J1939
+ else if(type == socket_type::J1939)
{
- for (int i = 0 ; i < cfd.len ; i++)
+ return low_can_subscription_t::j1939_send(*cd[bus_name], message, bus_name);
+ }
+#endif
+ else
+ {
+ return -1;
+ }
+}
+
+
+static void write_raw_frame(afb_req_t request, const std::string& bus_name, message_t *message, struct json_object *can_data, socket_type type)
+{
+ if((message->get_length() <= 8 && message->get_length() > 0 && type == socket_type::BCM)
+#ifdef USE_FEATURE_J1939
+ || (message->get_length() < J1939_MAX_DLEN && type == socket_type::J1939)
+#endif
+ )
+ {
+
+ std::vector<uint8_t> data;
+ for (int i = 0 ; i < message->get_length() ; i++)
{
struct json_object *one_can_data = json_object_array_get_idx(can_data, i);
- cfd.data[i] = (json_object_is_type(one_can_data, json_type_int)) ?
- (uint8_t)json_object_get_int(one_can_data) : 0;
+ data.push_back((json_object_is_type(one_can_data, json_type_int)) ?
+ (uint8_t)json_object_get_int(one_can_data) : 0);
}
+ message->set_data(data);
}
else
{
- afb_req_fail(request, "Invalid", "Data array must hold 1 to 8 values.");
+ if(type == socket_type::BCM)
+ {
+ afb_req_fail(request, "Invalid", "Data array must hold 1 to 8 values.");
+ }
+ else if(type == socket_type::J1939)
+ {
+ afb_req_fail(request, "Invalid", "Data array too large");
+ }
+ else
+ {
+ afb_req_fail(request, "Invalid", "Invalid socket type");
+ }
return;
}
- if(! send_frame(cfd, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name)))
+ if(! send_message(message, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name),type))
+ {
afb_req_success(request, nullptr, "Message correctly sent");
+ }
else
+ {
afb_req_fail(request, "Error", "sending the message. See the log for more details.");
+ }
+}
+
+static void write_frame(afb_req_t request, const std::string& bus_name, json_object *json_value)
+{
+ message_t *message;
+ int id;
+ int length;
+ struct json_object *can_data = nullptr;
+ std::vector<uint8_t> data;
+
+ AFB_DEBUG("JSON content %s",json_object_get_string(json_value));
+
+ if(!wrap_json_unpack(json_value, "{si, si, so !}",
+ "can_id", &id,
+ "can_dlc", &length,
+ "can_data", &can_data))
+ {
+ message = new can_message_t(CANFD_MAX_DLEN,(uint32_t)id,(uint32_t)length,message_format_t::STANDARD,false,0,data,0);
+ write_raw_frame(request,bus_name,message,can_data,socket_type::BCM);
+ }
+#ifdef USE_FEATURE_J1939
+ else if(!wrap_json_unpack(json_value, "{si, si, so !}",
+ "pgn", &id,
+ "length", &length,
+ "data", &can_data))
+ {
+ message = new j1939_message_t(J1939_MAX_DLEN,(uint32_t)length,message_format_t::J1939,data,0,J1939_NO_NAME,(pgn_t)id,J1939_NO_ADDR);
+ write_raw_frame(request,bus_name,message,can_data,socket_type::J1939);
+ }
+#endif
+ else
+ {
+ afb_req_fail(request, "Invalid", "Frame object malformed");
+ return;
+ }
+ delete message;
}
static void write_signal(afb_req_t request, const std::string& name, json_object *json_value)
encoder(*sig, dynafield_value, &send) :
encoder_t::encode_DynamicField(*sig, dynafield_value, &send);
- cfd = encoder_t::build_frame(sig, value);
- if(! send_frame(cfd, sig->get_message()->get_bus_device_name()) && send)
+ socket_type type = socket_type::INVALID;
+
+ if(sig->get_message()->is_j1939())
+ {
+ type = socket_type::J1939;
+ }
+ else
+ {
+ type = socket_type::BCM;
+ }
+
+// cfd = encoder_t::build_frame(sig, value);
+ message_t *message = encoder_t::build_message(sig,value);
+
+ if(! send_message(message, sig->get_message()->get_bus_device_name(), type) && send)
+ {
afb_req_success(request, nullptr, "Message correctly sent");
+ }
else
+ {
afb_req_fail(request, "Error", "Sending the message. See the log for more details.");
+ }
+
+ if(sig->get_message()->is_j1939())
+ {
+#ifdef USE_FEATURE_J1939
+ delete (j1939_message_t*) message;
+#endif
+ }
+ else
+ {
+ delete (can_message_t*) message;
+ }
}
void write(afb_req_t request)
if (args != NULL && ! wrap_json_unpack(args, "{ss, so !}",
"bus_name", &name,
"frame", &json_value))
- write_raw_frame(request, name, json_value);
+ write_frame(request, name, json_value);
// Search signal then encode value.
else if(args != NULL &&
rc = -1;
if (rc >= 0)
+ {
afb_req_success(request, ans, NULL);
+ }
else
+ {
afb_req_fail(request, "error", NULL);
+ }
}
/// @brief Initialize the binding.
/// @return Exit code, zero if success.
int init_binding(afb_api_t api)
{
- uint32_t ret = 1;
- can_bus_t& can_bus_manager = application_t::instance().get_can_bus_manager();
+ int ret = 1;
+ application_t& application = application_t::instance();
+ can_bus_t& can_bus_manager = application.get_can_bus_manager();
can_bus_manager.set_can_devices();
can_bus_manager.start_threads();
subscribe_unsubscribe_diagnostic_messages(request, true, sf.diagnostic_messages, event_filter, s, true);
}
+
+#ifdef USE_FEATURE_J1939
+ std::vector<std::shared_ptr<message_definition_t>> current_messages_definition = application.get_messages_definition();
+ for(std::shared_ptr<message_definition_t> message_definition: current_messages_definition)
+ {
+ if(message_definition->is_j1939())
+ {
+ std::shared_ptr<low_can_subscription_t> low_can_j1939 = std::make_shared<low_can_subscription_t>();
+
+ application.set_subscription_address_claiming(low_can_j1939);
+
+ ret = low_can_subscription_t::open_socket(*low_can_j1939,
+ message_definition->get_bus_device_name(),
+ socket_type::J1939_ADDR_CLAIM);
+ if(ret < 0)
+ {
+ AFB_ERROR("Error open socket address claiming for j1939 protocol");
+ return -1;
+ }
+
+// std::shared_ptr<low_can_subscription_t> saddrclaim = application.get_subscription_address_claiming();
+
+ add_to_event_loop(low_can_j1939);
+ break;
+ }
+ }
+#endif
+
if(ret)
+ {
AFB_ERROR("There was something wrong with CAN device Initialization.");
+ }
return ret;
}