Change content target and use a testing
[apps/low-level-can-service.git] / src / can-utils.hpp
index 4252b97..76dc75b 100644 (file)
 
 #pragma once
 
-#include <map>
 #include <queue>
-#include <vector>
-#include <cstdio>
-#include <string>
 #include <thread>
-#include <fcntl.h>
-#include <unistd.h>
-#include <net/if.h>
-#include <sys/ioctl.h>
 #include <linux/can.h>
-#include <sys/socket.h>
-#include <json-c/json.h>
-#include <linux/can/raw.h>
 
 #include "timer.hpp"
 #include "openxc.pb.h"
-
-extern "C"
-{
-       #include <afb/afb-binding.h>
-       #include <afb/afb-service-itf.h>
-}
+#include "low-can-binding.hpp"
 
 // TODO actual max is 32 but dropped to 24 for memory considerations
 #define MAX_ACCEPTANCE_FILTERS 24
@@ -99,11 +83,6 @@ typedef enum CanMessageFormat CanMessageFormat;
  *
  * @brief A compact representation of a single CAN message, meant to be used in in/out
  * buffers.
- *
- * param[in] 
- * param[in] 
- * param[in] 
- * @param[in]
  */
 
 /*************************
@@ -119,8 +98,6 @@ typedef struct CanMessage CanMessage;
 */
 class can_message_t {
        private:
-               const struct afb_binding_interface* interface_; /*!< afb_binding_interface interface between daemon and binding */ 
-
                uint32_t id_; /*!< uint32_t id - The ID of the message. */
                uint8_t length_; /*!<  uint8_t length - the length of the data array (max 8). */
                CanMessageFormat format_; /*!< CanMessageFormat format - the format of the message's ID.*/
@@ -131,10 +108,8 @@ class can_message_t {
                 * @brief Class constructor
                 *
                 * Constructor about can_message_t class.
-                *
-                * @param interface - const structafb_binding_interface pointer
                 */
-               can_message_t(const struct afb_binding_interface* interface);
+               can_message_t();
 
                /**
                 * @brief Retrieve id_ member value.
@@ -246,7 +221,9 @@ class can_bus_t {
                int conf_file_; /*!< conf_file_ - configuration file handle used to initialize can_bus_dev_t objects.*/
                
                std::thread th_decoding_; /*!< thread that'll handle decoding a can frame */
+               bool is_decoding_; /*!< boolean member controling thread while loop*/
                std::thread th_pushing_; /*!<  thread that'll handle pushing decoded can frame to subscribers */
+               bool is_pushing_; /*!< boolean member controling thread while loop*/
 
                bool has_can_message_; /*!< boolean members that control whether or not there is can_message into the queue */
                std::queue <can_message_t> can_message_q_; /*!< queue that'll store can_message_t to decoded */
@@ -255,15 +232,13 @@ class can_bus_t {
                std::queue <openxc_VehicleMessage> vehicle_message_q_; /*!< queue that'll store openxc_VehicleMessage to pushed */
 
        public:
-               const struct afb_binding_interface *interface_; /*!< interface_ - afb_binding_interface pointer to the binder. Used to log messages */
-
                /**
                 * @brief Class constructor
                 *
                 * @param struct afb_binding_interface *interface between daemon and binding
                 * @param int file handle to the json configuration file.
                 */
-               can_bus_t(const struct afb_binding_interface *interface, int& conf_file);
+               can_bus_t(int& conf_file);
                
                /**
                 * @brief Will initialize can_bus_dev_t objects after reading 
@@ -285,6 +260,32 @@ class can_bus_t {
                 */
                void start_threads();
 
+               /**
+                * @brief Will stop all threads holded by can_bus_t object
+                *  which are decoding and pushing threads.
+                */
+               void stop_threads();
+
+               /**
+                * @brief Telling if the decoding thread is running.
+                *  This is the boolean value on which the while loop
+                *  take its condition. Set it to false will stop the 
+                *  according thread.
+                *
+                * @return true if decoding thread is running, false if not.
+                */
+               bool is_decoding();
+
+               /**
+                * @brief Telling if the pushing thread is running
+                *  This is the boolean value on which the while loop
+                *  take its condition. Set it to false will stop the 
+                *  according thread.
+                *
+                * @return true if pushing thread is running, false if not.
+                */
+               bool is_pushing();
+
                /**
                 * @brief Return first can_message_t on the queue 
                 *
@@ -357,15 +358,30 @@ class can_bus_dev_t {
                 *
                 * @return 
                 */
-               int open(const struct afb_binding_interface* interface);
+               int open();
+               
+               /**
+                * @brief Open the can socket and returning it 
+                *
+                * @return 
+                */
                int close();
+               
+               /**
+                * @brief Telling if the reading thread is running
+                *  This is the boolean value on which the while loop
+                *  take its condition. Set it to false will stop the 
+                *  according thread.
+                *
+                * @return true if reading thread is running, false if not.
+                */
                bool is_running();
                
                /**
                * @brief start reading threads and set flag is_running_
                *
                * @param can_bus_t reference can_bus_t. it will be passed to the thread 
-               *  to allow using afb_binding_interface and can_bus_t queue.
+               *  to allow using can_bus_t queue.
                */
                void start_reading(can_bus_t& can_bus);
 
@@ -375,7 +391,7 @@ class can_bus_dev_t {
                * @param const struct afb_binding_interface* interface pointer. Used to be able to log 
                *  using application framework logger.
                */
-               canfd_frame read(const struct afb_binding_interface *interface);
+               canfd_frame read();
                
                /**
                * @brief Send a can message from a can_message_t object.
@@ -384,7 +400,7 @@ class can_bus_dev_t {
                * @param const struct afb_binding_interface* interface pointer. Used to be able to log 
                *  using application framework logger.
                */
-               int send_can_message(can_message_t& can_msg, const struct afb_binding_interface* interface);
+               int send_can_message(can_message_t& can_msg);
 };
 
 /**
@@ -440,53 +456,43 @@ struct CanSignal {
 typedef struct CanSignal CanSignal;
 
 /**
+ * @struct CanMessageDefinition
+ *
  * @brief The definition of a CAN message. This includes a lot of metadata, so
  * to save memory this struct should not be used for storing incoming and
  * outgoing CAN messages.
- *
- * @param[in] bus - A pointer to the bus this message is on.
- * @param[in] id - The ID of the message.
- * @param[in] format - the format of the message's ID.
- * @param[in] clock - an optional frequency clock to control the output of this
- *             message, if sent raw, or simply to mark the max frequency for custom
- *             handlers to retriec++ if ? syntaxve.
- * @param[in] forceSendChanged - If true, regardless of the frequency, it will send CAN
- *             message if it has changed when using raw passthrough.
- * @param[in] lastValue - The last received value of the message. Defaults to undefined.
- *             This is required for the forceSendChanged functionality, as the stack
- *             needs to compare an incoming CAN message with the previous frame.
  */
 struct CanMessageDefinition {
-       struct CanBus* bus;
-       uint32_t id;
-       CanMessageFormat format;
-       FrequencyClock frequencyClock;
-       bool forceSendChanged;
-       uint8_t lastValue[CAN_MESSAGE_SIZE];
+       struct CanBus* bus; /*!< bus - A pointer to the bus this message is on. */
+       uint32_t id; /*!<  id - The ID of the message.*/
+       CanMessageFormat format; /*!< format - the format of the message's ID.*/
+       FrequencyClock frequencyClock; /*!<  clock - an optional frequency clock to control the output of this
+                                                                       *       message, if sent raw, or simply to mark the max frequency for custom
+                                                                       *       handlers to retriec++ if ? syntaxve.*/
+       bool forceSendChanged; /*!< forceSendChanged - If true, regardless of the frequency, it will send CAN
+                                                       *       message if it has changed when using raw passthrough.*/
+       uint8_t lastValue[CAN_MESSAGE_SIZE]; /*!< lastValue - The last received value of the message. Defaults to undefined.
+                                                                               *       This is required for the forceSendChanged functionality, as the stack
+                                                                               *       needs to compare an incoming CAN message with the previous frame.*/
 };
 typedef struct CanMessageDefinition CanMessageDefinition;
 
 /**
+ * @struct CanMessageSet
+ *
  * @brief A parent wrapper for a particular set of CAN messages and associated
  *     CAN buses(e.g. a vehicle or program).
- *
- *     @param[in] index - A numerical ID for the message set, ideally the index in an array
- *             for fast lookup
- *     @param[in] name - The name of the message set.
- *     @param[in] busCount - The number of CAN buses defined for this message set.
- *     @param[in] messageCount - The number of CAN messages (across all buses) defined for
- *             this message set.
- *     @param[in] signalCount - The number of CAN signals (across all messages) defined for
- *             this message set.
- *     @param[in] commandCount - The number of CanCommmands defined for this message set.
  */
  typedef struct {
-       uint8_t index;
-       const char* name;
-       uint8_t busCount;
-       unsigned short messageCount;
-       unsigned short signalCount;
-       unsigned short commandCount;
+       uint8_t index; /*!<index - A numerical ID for the message set, ideally the index in an array
+                                       *       for fast lookup*/
+       const char* name; /*!< name - The name of the message set.*/
+       uint8_t busCount; /*!< busCount - The number of CAN buses defined for this message set.*/
+       unsigned short messageCount; /*!< messageCount - The number of CAN messages (across all buses) defined for
+                                                                       *       this message set.*/
+       unsigned short signalCount; /*!< signalCount - The number of CAN signals (across all messages) defined for
+                                                               *       this message set.*/
+       unsigned short commandCount; /*!< commandCount - The number of CanCommmands defined for this message set.*/
 } CanMessageSet;
 
 /**
@@ -503,9 +509,10 @@ typedef struct CanMessageDefinition CanMessageDefinition;
 typedef void (*CommandHandler)(const char* name, openxc_DynamicField* value,
                openxc_DynamicField* event, CanSignal* signals, int signalCount);
 
-/* Public: The structure to represent a supported custom OpenXC command.
+/* @struct CanCommand
+ * @brief The structure to represent a supported custom OpenXC command.
  *
- * For completely customized CAN commands without a 1-1 mapping between an
+ * @desc For completely customized CAN commands without a 1-1 mapping between an
  * OpenXC message from the host and a CAN signal, you can define the name of the
  * command and a custom function to handle it in the VI. An example is
  * the "turn_signal_status" command in OpenXC, which has a value of "left" or
@@ -515,17 +522,15 @@ typedef void (*CommandHandler)(const char* name, openxc_DynamicField* value,
  *
  * Command handlers are also useful if you want to trigger multiple CAN messages
  * or signals from a signal OpenXC message.
- *
- * genericName - The name of the command.
- * handler - An function to process the received command's data and perform some
- *             action.
  */
 typedef struct {
-       const char* genericName;
-       CommandHandler handler;
+       const char* genericName; /*!< genericName - The name of the command.*/
+       CommandHandler handler; /*!< handler - An function to process the received command's data and perform some
+                                                       *       action.*/
 } CanCommand;
 
 /**
+ * @fn void pre_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
  * @brief Pre initialize actions made before CAN bus initialization
  *
  * @param[in] can_bus_dev_t bus - A CanBus struct defining the bus's metadata
@@ -536,29 +541,34 @@ typedef struct {
  */
 void pre_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
 
-/* Post-initialize actions made after CAN bus initialization and before the
- * event loop connection.
+/**
+ * @fn void post_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
+ * @brief Post-initialize actions made after CAN bus initialization
  *
- * bus - A CanBus struct defining the bus's metadata
- * writable - configure the controller in a writable mode. If false, it will be
+ * @param[in] bus - A CanBus struct defining the bus's metadata
+ * @param[in] writable - configure the controller in a writable mode. If false, it will be
  *             configured as "listen only" and will not allow writes or even CAN ACKs.
- * buses - An array of all CAN buses.
- * busCount - The length of the buses array.
+ * @param[in] buses - An array of all CAN buses.
+ * @param[in] busCount - The length of the buses array.
  */
 void post_initialize(can_bus_dev_t* bus, bool writable, can_bus_dev_t* buses, const int busCount);
 
-/* Public: Check if the device is connected to an active CAN bus, i.e. it's
+/**
+ * @fn bool isBusActive(can_bus_dev_t* bus);
+ * @brief Check if the device is connected to an active CAN bus, i.e. it's
  * received a message in the recent past.
  *
- * Returns true if a message was received on the CAN bus within
+ * @return true if a message was received on the CAN bus within
  * CAN_ACTIVE_TIMEOUT_S seconds.
  */
 bool isBusActive(can_bus_dev_t* bus);
 
-/* Public: Log transfer statistics about all active CAN buses to the debug log.
+/**
+ * @fn void logBusStatistics(can_bus_dev_t* buses, const int busCount);
+ * @brief Log transfer statistics about all active CAN buses to the debug log.
  *
- * buses - an array of active CAN buses.
- * busCount - the length of the buses array.
+ * @param[in] buses - an array of active CAN buses.
+ * @param[in] busCount - the length of the buses array.
  */
 void logBusStatistics(can_bus_dev_t* buses, const int busCount);