/*
- * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Copyright (C) 2015, 2018 "IoT.bzh"
* Author "Romain Forlot" <romain.forlot@iot.bzh>
* Author "Loic Collignon" <loic.collignon@iot.bzh>
*
#include "../can/can-encoder.hpp"
#include "../can/can-bus.hpp"
#include "../can/can-signals.hpp"
-#include "../can/can-message.hpp"
+#include "../can/message/message.hpp"
#include "../utils/signals.hpp"
#include "../diagnostic/diagnostic-message.hpp"
#include "../utils/openxc-utils.hpp"
s.erase(it);
}
-static void push_n_notify(const can_message_t& cm)
+static void push_n_notify(std::shared_ptr<message_t> m)
{
can_bus_t& cbm = application_t::instance().get_can_bus_manager();
{
std::lock_guard<std::mutex> can_message_lock(cbm.get_can_message_mutex());
- cbm.push_new_can_message(cm);
+ cbm.push_new_can_message(m);
}
cbm.get_new_can_message_cv().notify_one();
}
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)
{
- can_message_t cm;
- utils::socketcan_bcm_t& s = can_subscription->get_socket();
- s >> cm;
+ 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(! cm.get_id() == 0 && ! cm.get_length() == 0)
- {push_n_notify(cm);}
+ if (! message->get_id() == 0 && ! message->get_length() == 0)
+ {
+ push_n_notify(message);
+ }
}
// check if error or hangup
if ((revents & (EPOLLERR|EPOLLRDHUP|EPOLLHUP)) != 0)
{
sd_event_source_unref(event_source);
- can_subscription->get_socket().close();
+ can_subscription->get_socket()->close();
}
+
return 0;
}
///
///*******************************************************************************/
-static int make_subscription_unsubscription(afb_req_t request,
- std::shared_ptr<low_can_subscription_t>& can_subscription,
- std::map<int, std::shared_ptr<low_can_subscription_t> >& s,
- bool subscribe)
-{
- /* Make the subscription or unsubscription to the event (if request is not null) */
- if(request &&
- ((subscribe ? afb_req_subscribe : afb_req_unsubscribe)(request, s[can_subscription->get_index()]->get_event())) < 0)
- {
- AFB_ERROR("Operation goes wrong for signal: %s", can_subscription->get_name().c_str());
- return -1;
- }
- return 0;
-}
-
-static int create_event_handle(std::shared_ptr<low_can_subscription_t>& can_subscription,
- std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
-{
- int sub_index = can_subscription->get_index();
- can_subscription->set_event(afb_daemon_make_event(can_subscription->get_name().c_str()));
- s[sub_index] = can_subscription;
- if (!afb_event_is_valid(s[sub_index]->get_event()))
- {
- AFB_ERROR("Can't create an event for %s, something goes wrong.", can_subscription->get_name().c_str());
- return -1;
- }
- return 0;
-}
-
/// @brief This will determine if an event handle needs to be created and checks if
/// we got a valid afb_event to get subscribe or unsubscribe. After that launch the subscription or unsubscription
/// against the application framework using that event handle.
std::shared_ptr<low_can_subscription_t>& can_subscription,
std::map<int, std::shared_ptr<low_can_subscription_t> >& s)
{
- int ret = -1;
+ int ret = 0;
int sub_index = can_subscription->get_index();
+ bool subscription_exists = s.count(sub_index);
- if (can_subscription && s.find(sub_index) != s.end())
+ // Susbcription part
+ if(subscribe)
{
- if (!afb_event_is_valid(s[sub_index]->get_event()) && !subscribe)
+ /* There is no valid request to subscribe so this must be an
+ * internal permanent diagnostic request. Skip the subscription
+ * part and don't register it into the current "low-can"
+ * subsciptions.
+ */
+ if(! request)
{
- AFB_NOTICE("Event isn't valid, no need to unsubscribed.");
- ret = -1;
+ return 0;
}
- ret = 0;
+
+ // Event doesn't exist , so let's create it
+ if (! subscription_exists &&
+ (ret = can_subscription->subscribe(request)) < 0)
+ return ret;
+
+ if(! subscription_exists)
+ s[sub_index] = can_subscription;
+
+ return ret;
}
- else
+
+ // Unsubscrition part
+ if(! subscription_exists)
{
- /* Event doesn't exist , so let's create it */
- s[sub_index] = can_subscription;
- ret = create_event_handle(can_subscription, s);
+ AFB_NOTICE("There isn't any valid subscriptions for that request.");
+ return ret;
+ }
+ else if (subscription_exists &&
+ ! afb_event_is_valid(s[sub_index]->get_event()) )
+ {
+ AFB_NOTICE("Event isn't valid, no need to unsubscribed.");
+ return ret;
}
- // Checks if the event handler is correctly created, if it is, it
- // performs the subscription or unsubscription operations.
- if (ret < 0)
+ if( (ret = s[sub_index]->unsubscribe(request)) < 0)
return ret;
- return make_subscription_unsubscription(request, can_subscription, s, subscribe);
+ s.erase(sub_index);
+
+ return ret;
}
static int add_to_event_loop(std::shared_ptr<low_can_subscription_t>& can_subscription)
struct sd_event_source* event_source = nullptr;
return ( sd_event_add_io(afb_daemon_get_event_loop(),
&event_source,
- can_subscription->get_socket().socket(),
+ can_subscription->get_socket()->socket(),
EPOLLIN,
read_message,
can_subscription.get()));
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;
- }
+ {can_subscription = it->second;}
else
{
can_subscription = std::make_shared<low_can_subscription_t>(low_can_subscription_t(event_filter));
{return -1;}
rets++;
- AFB_DEBUG("signal: %s subscribed", sig->get_name().c_str());
+ AFB_DEBUG("%s Signal: %s %ssubscribed", sig->get_message()->is_fd() ? "FD": "", sig->get_name().c_str(), subscribe ? "":"un");
}
return rets;
}
do_subscribe_unsubscribe(request, false);
}
-static int send_frame(const std::string& bus_name, const struct can_frame& cf)
+static int send_frame(struct canfd_frame& cfd, const std::string& bus_name)
{
if(bus_name.empty()) {
return -1;
}
- std::map<std::string, std::shared_ptr<low_can_socket_t> >& cd = application_t::instance().get_can_devices();
+ 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_socket_t>(low_can_socket_t());}
+ {cd[bus_name] = std::make_shared<low_can_subscription_t>(low_can_subscription_t());}
- return cd[bus_name]->tx_send(cf, bus_name);
+ return cd[bus_name]->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)
{
- struct can_frame cf;
- struct json_object *json_can_data = nullptr;
+ struct canfd_frame cfd;
+ struct json_object *can_data = nullptr;
- ::memset(&cf, 0, sizeof(cf));
+ ::memset(&cfd, 0, sizeof(cfd));
- if(! wrap_json_unpack(json_value, "{sF, sF, so !}",
- "can_id", &cf.can_id,
- "can_dlc", &cf.can_dlc,
- "can_data", &json_can_data))
+ if(wrap_json_unpack(json_value, "{si, si, so !}",
+ "can_id", &cfd.can_id,
+ "can_dlc", &cfd.len,
+ "can_data", &can_data))
{
- struct json_object *one_can_data;
- size_t n = json_object_array_length(json_can_data);
+ afb_req_fail(request, "Invalid", "Frame object malformed");
+ return;
+ }
- if(n <= 8 && n > 0)
- {
- for (int i = 0 ; i < n ; i++)
- {
- one_can_data = json_object_array_get_idx(json_can_data, i);
- cf.data[i] = json_object_get_type(one_can_data) == json_type_int ? (uint8_t)json_object_get_int(one_can_data) : 0;
- }
- }
- else
+ if(cfd.len <= 8 && cfd.len > 0)
+ {
+ for (int i = 0 ; i < cfd.len ; i++)
{
- afb_req_fail(request, "Error", "Data array must hold 1 to 8 values.");
- return;
+ 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;
}
-
- if(! send_frame(application_t::instance().get_can_bus_manager().get_can_device_name(bus_name), cf))
- afb_req_success(request, nullptr, "Message correctly sent");
- else
- afb_req_fail(request, "Error", "sending the message. See the log for more details.");
-
+ }
+ else
+ {
+ afb_req_fail(request, "Invalid", "Data array must hold 1 to 8 values.");
return;
}
- afb_req_fail(request, "Error", "Frame object malformed (must be \n \"frame\": {\"can_id\": int, \"can_dlc\": int, \"can_data\": [ int, int , int, int ,int , int ,int ,int]}");
+ if(! send_frame(cfd, application_t::instance().get_can_bus_manager().get_can_device_name(bus_name)))
+ 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_signal(afb_req_t request, const std::string& name, json_object *json_value)
{
- struct can_frame cf;
+ struct canfd_frame cfd;
struct utils::signals_found sf;
signal_encoder encoder = nullptr;
bool send = true;
- ::memset(&cf, 0, sizeof(cf));
+ ::memset(&cfd, 0, sizeof(cfd));
openxc_DynamicField search_key = build_DynamicField(name);
sf = utils::signals_manager_t::instance().find_signals(search_key);
encoder(*sig, dynafield_value, &send) :
encoder_t::encode_DynamicField(*sig, dynafield_value, &send);
- if(! send_frame(sig->get_message()->get_bus_device_name(), encoder_t::build_frame(sig, value)) &&
- send)
+ cfd = encoder_t::build_frame(sig, value);
+ if(! send_frame(cfd, sig->get_message()->get_bus_device_name()) && send)
afb_req_success(request, nullptr, "Message correctly sent");
else
afb_req_fail(request, "Error", "Sending the message. See the log for more details.");