summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--can-utils.cpp175
-rw-r--r--can-utils.h51
-rw-r--r--can_reader.cpp11
-rw-r--r--low-can-binding.cpp77
-rw-r--r--timer.h2
5 files changed, 217 insertions, 99 deletions
diff --git a/can-utils.cpp b/can-utils.cpp
new file mode 100644
index 0000000..1e215ba
--- /dev/null
+++ b/can-utils.cpp
@@ -0,0 +1,175 @@
+/*
+ * 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
+
+/********************************************************************************
+*
+* CanBus method implementation
+*
+*********************************************************************************/
+
+int CanBus_c::open()
+{
+ const int canfd_on = 1;
+ struct ifreq ifr;
+ struct timeval timeout = {1, 0};
+
+ DEBUG(interface, "open_can_dev: CAN Handler socket : %d", socket);
+ if (socket >= 0)
+ close(socket);
+
+ socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+ if (socket < 0)
+ {
+ ERROR(interface, "open_can_dev: socket could not be created");
+ }
+ else
+ {
+ /* Set timeout for read */
+ setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
+ /* try to switch the socket into CAN_FD mode */
+ if (setsockopt(socket, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
+ {
+ NOTICE(interface, "open_can_dev: Can not switch into CAN Extended frame format.");
+ is_fdmode_on = false;
+ } else {
+ is_fdmode_on = true;
+ }
+
+ /* Attempts to open a socket to CAN bus */
+ strcpy(ifr.ifr_name, device);
+ if(ioctl(socket, SIOCGIFINDEX, &ifr) < 0)
+ ERROR(interface, "open_can_dev: ioctl failed");
+ else
+ {
+ txAddress.can_family = AF_CAN;
+ txAddress.can_ifindex = ifr.ifr_ifindex;
+
+ /* And bind it to txAddress */
+ if (bind(socket, (struct sockaddr *)&txAddress, sizeof(txAddress)) < 0)
+ {
+ ERROR(interface, "open_can_dev: bind failed");
+ }
+ else
+ {
+ fcntl(socket, F_SETFL, O_NONBLOCK);
+ return 0;
+ }
+ }
+ close(socket);
+ socket = -1;
+ }
+ return -1;
+}
+
+int CanBus_c::close()
+{
+ close(socket);
+ socket = -1;
+}
+
+void CanBus_c::start_threads()
+{
+ std::queue <canfd_frame> canfd_frame_queue;
+ std::queue <openxc_can_message_type> can_message_queue;
+
+ th_reading = std::thread(can_reader, interface, socket, canfd_frame_queue);
+ th_decoding = std::thread(can_decoder, interface, canfd_frame_queue, can_message_queue);
+ th_pushing = std::thread(can_event_push, interface, can_message_queue);
+}
+
+/********************************************************************************
+*
+* CanMessage method implementation
+*
+*********************************************************************************/
+
+uint32_t CanMessage_c::get_id()
+{
+ return id;
+}
+
+int CanMessage_c::get_format()
+{
+ return format;
+}
+
+uint8_t CanMessage_c::get_data()
+{
+ return data;
+}
+uint8_t CanMessage_c::get_lenght()
+{
+ return lenght;
+}
+
+void CanMessage_c::set_id(uint32_t new_id)
+{
+ switch(format):
+ case SIMPLE:
+ id = new_id & CAN_SFF_MASK;
+ case EXTENDED:
+ id = new_id & CAN_EFF_MASK;
+ default:
+ ERROR(interface, "ERROR: Can set id, not a compatible format or format not set prior to set id.");
+}
+
+void CanMessage_c::set_format(CanMessageFormat new_format)
+{
+ if(new_format == SIMPLE || new_format == EXTENDED)
+ format = new_format;
+ else
+ ERROR(interface, "ERROR: Can set format, wrong format chosen");
+}
+
+void CanMessage_c::set_data(uint8_t new_data)
+{
+ data = new_data;
+}
+
+void CanMessage_c::set_lenght(uint8_t new_length)
+{
+ lenght = new_lenght;
+}
+
+void CanMessage_c::convert_canfd_frame_to_CanMessage(canfd_frame *frame)
+{
+
+ lenght = (canfd_frame->len > maxdlen) ? maxdlen : canfd_frame->len;
+
+ switch (canfd_frame->can_id):
+ case (canfd_frame->can_id & CAN_ERR_FLAG):
+ id = canfd_frame->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG);
+ break;
+ case (canfd_frame->can_id & CAN_EFF_FLAG):
+ id = canfd_frame->can_id & CAN_EFF_MASK;
+ format = EXTENDED;
+ default:
+ format = STANDARD;
+ id = canfd_frame->can_id & CAN_SFF_MASK;
+
+ if (sizeof(canfd_frame->data) <= sizeof(data))
+ {
+ for (i = 0; i < lenght; i++)
+ can_message->data.bytes[i] = canfd_frame->data[i];
+ return 0;
+ } else if (sizeof(canfd_frame->data) >= CAN_MAX_DLEN)
+ {
+ ERROR(interface, "parse_can_frame: can_frame data too long to be stored into openxc_CanMessage data field");
+ }
+} \ No newline at end of file
diff --git a/can-utils.h b/can-utils.h
index 20e37fc..d2020a5 100644
--- a/can-utils.h
+++ b/can-utils.h
@@ -1,4 +1,3 @@
-
/*
* Copyright (C) 2015, 2016 "IoT.bzh"
* Author "Romain Forlot" <romain.forlot@iot.bzh>
@@ -84,7 +83,7 @@ typedef uint64_t (*SignalEncoder)(struct CanSignal* signal,
openxc_DynamicField* value, bool* send);
/* CanBus represent a can device definition gotten from configuraiton file */
-class CanBus {
+class CanBus_c {
private:
/* Got from conf file */
std::string deviceName;
@@ -109,11 +108,11 @@ class CanBus {
* STANDARD - standard 11-bit CAN arbitration ID.
* EXTENDED - an extended frame, with a 29-bit arbitration ID.
*/
-enum CanMessageFormat {
+enum CanMessageFormat_c {
STANDARD,
EXTENDED,
};
-typedef enum CanMessageFormat CanMessageFormat;
+typedef enum CanMessageFormat_c CanMessageFormat;
/* Public: A state encoded (SED) signal's mapping from numerical values to
* OpenXC state names.
@@ -126,11 +125,11 @@ struct CanSignalState {
};
typedef struct CanSignalState CanSignalState;
*/
- class CanSignalState {
+ class CanSignalState_c {
private:
const int value;
const char *name;
- }
+};
/* Public: A CAN signal to decode from the bus and output over USB.
*
@@ -188,8 +187,9 @@ struct CanSignal {
typedef struct CanSignal CanSignal;
*/
-class CanSignal {
+class CanSignal_c {
private:
+ CanMessageDefinition *message;
const char *generic_name;
uint8_t bit_position;
uint8_t bit_size;
@@ -197,7 +197,7 @@ class CanSignal {
float offset;
float min_value;
float max_value;
- FrequencyClock clock;
+ FrequencyClock_t clock;
bool send_same;
bool force_send_changed;
const CanSignalState *states;
@@ -207,7 +207,7 @@ class CanSignal {
SignalEncoder encoder;
bool received;
float last_value;
-}
+};
/* Public: 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
@@ -234,15 +234,15 @@ struct CanMessageDefinition {
};
typedef struct CanMessageDefinition CanMessageDefinition;
*/
- class CanMessageDefinition {
+ class CanMessageDefinition_c {
private:
CanBus *bus
uint32_t id;
CanMessageFormat format;
- FrequencyClock clock;
+ FrequencyClock_t clock;
bool force_send_changed;
uint8_t last_value[CAN_MESSAGE_SIZE];
- }
+ };
/* A compact representation of a single CAN message, meant to be used in in/out
* buffers.
@@ -259,15 +259,28 @@ struct CanMessage {
};
typedef struct CanMessage CanMessage;
*/
-class CanMessage {
+class CanMessage_c {
private:
uint32_t id;
CanMessageFormat format;
uint8_t data[CAN_MESSAGE_SIZE];
uint8_t length;
-}
-QUEUE_DECLARE(CanMessage, 8);
+ public:
+ uint32_t get_id();
+ int get_format();
+ uint8_t get_data();
+ uint8_t get_lenght();
+
+ void set_id(uint32_t id);
+ void set_format(CanMessageFormat format);
+ void set_data(uint8_t data);
+ void set_lenght(uint8_t length);
+
+ void convert_canfd_frame_to_CanMessage(canfd_frame frame);
+};
+
+QUEUE_DECLARE(CanMessage_c, 8);
/* Private: An entry in the list of acceptance filters for each CanBus.
*
@@ -316,7 +329,7 @@ typedef struct {
unsigned short commandCount;
} CanMessageSet;
*/
-class CanMessageSet {
+class CanMessageSet_c {
private:
uint8_t index;
const char * name;
@@ -324,7 +337,7 @@ class CanMessageSet {
unsigned short messageCount;
unsigned short signalCount;
unsigned short commandCount;
-}
+};
/* Public: The type signature for a function to handle a custom OpenXC command.
*
@@ -361,11 +374,11 @@ typedef struct {
} CanCommand;
*/
-class CanCommand {
+class CanCommand_c {
private:
const char* genericName;
CommandHandler handler;
-}
+};
/* Pre initialize actions made before CAN bus initialization
*
diff --git a/can_reader.cpp b/can_reader.cpp
index f5e2833..35ea79e 100644
--- a/can_reader.cpp
+++ b/can_reader.cpp
@@ -21,10 +21,11 @@
#include <afb/afb-binding.h>
-void can_reader(afb_binding_interface *interface, int socket, std::queue <canfd_frame>& canfd_frame_queue)
+void can_reader(afb_binding_interface *interface, int socket, std::queue <CanMessage_t>& can_message_q)
{
ssize_t nbytes;
int maxdlen;
+ CanMessage_t can_message;
/* Test that socket is really opened */
if ( socket < 0)
@@ -39,7 +40,7 @@ void can_reader(afb_binding_interface *interface, int socket, std::queue <canfd_
switch(nbytes)
{
- case CANFD_MTU:s
+ case CANFD_MTU:
DEBUG(interface, "read_can: Got an CAN FD frame with length %d", canfd_frame.len);
maxdlen = CANFD_MAX_DLEN;
break;
@@ -64,6 +65,10 @@ void can_reader(afb_binding_interface *interface, int socket, std::queue <canfd_
return -4;
}
*/
- canfd_frame_queue.push(canfd_frame);
+
+ can_message.convert_canfd_frame_to_CanMessage(canfd_frame);
+
+
+ can_message_q.push(can_message);
}
}
diff --git a/low-can-binding.cpp b/low-can-binding.cpp
index 53517a6..b523e9d 100644
--- a/low-can-binding.cpp
+++ b/low-can-binding.cpp
@@ -48,81 +48,6 @@
*/
static const struct afb_binding_interface *interface;
-/********************************************************************************
-*
-* CanBus method implementation
-*
-*********************************************************************************/
-
-int CanBus::open()
-{
- const int canfd_on = 1;
- struct ifreq ifr;
- struct timeval timeout = {1, 0};
-
- DEBUG(interface, "open_can_dev: CAN Handler socket : %d", socket);
- if (socket >= 0)
- close(socket);
-
- socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
- if (socket < 0)
- {
- ERROR(interface, "open_can_dev: socket could not be created");
- }
- else
- {
- /* Set timeout for read */
- setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
- /* try to switch the socket into CAN_FD mode */
- if (setsockopt(socket, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0)
- {
- NOTICE(interface, "open_can_dev: Can not switch into CAN Extended frame format.");
- is_fdmode_on = false;
- } else {
- is_fdmode_on = true;
- }
-
- /* Attempts to open a socket to CAN bus */
- strcpy(ifr.ifr_name, device);
- if(ioctl(socket, SIOCGIFINDEX, &ifr) < 0)
- ERROR(interface, "open_can_dev: ioctl failed");
- else
- {
- txAddress.can_family = AF_CAN;
- txAddress.can_ifindex = ifr.ifr_ifindex;
-
- /* And bind it to txAddress */
- if (bind(socket, (struct sockaddr *)&txAddress, sizeof(txAddress)) < 0)
- {
- ERROR(interface, "open_can_dev: bind failed");
- }
- else
- {
- fcntl(socket, F_SETFL, O_NONBLOCK);
- return 0;
- }
- }
- close(socket);
- socket = -1;
- }
- return -1;
-}
-
-int CanBus::close()
-{
- close(socket);
- socket = -1;
-}
-
-void CanBus::start_threads()
-{
- std::queue <canfd_frame> canfd_frame_queue;
- std::queue <openxc_can_message_type> can_message_queue;
-
- th_reading = std::thread(can_reader, interface, socket, canfd_frame_queue);
- th_decoding = std::thread(can_decoder, interface, canfd_frame_queue, can_message_queue);
- th_pushing = std::thread(can_event_push, interface, can_message_queue);
-}
/********************************************************************************
*
@@ -305,7 +230,7 @@ int afbBindingV1ServiceInit(struct afb_service service)
/* Open JSON conf file */
/* Open CAN socket */
- CanBus CanBus_handler;
+ CanBus_t CanBus_handler;
CanBus_handler.open();
CanBus_handler.start_threads();
diff --git a/timer.h b/timer.h
index 7c39364..fa329eb 100644
--- a/timer.h
+++ b/timer.h
@@ -32,7 +32,7 @@ typedef struct {
} FrequencyClock;
*/
-class FrequencyClock {
+class FrequencyClock_c {
private:
float frequency;
unsigned long last_tick;