summaryrefslogtreecommitdiffstats
path: root/can-utils.cpp
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-02-13 23:26:58 +0000
committerRomain Forlot <romain.forlot@iot.bzh>2017-02-13 23:31:56 +0000
commit2fc26a117842428f4148621361c53082ac93722f (patch)
treea2b4335e7d47c8f51879198e66184d62ff366790 /can-utils.cpp
parent53f4e096efa72a05b1f469fe082e6fa4b55bca01 (diff)
New threads management, only one argument needed.
Now CanBus_c object is the main core part to handle queues and to follow CAN bus reading process. Change-Id: I33cdfadb06362da4330a572caa1c1cf61d3ab3fd Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'can-utils.cpp')
-rw-r--r--can-utils.cpp115
1 files changed, 80 insertions, 35 deletions
diff --git a/can-utils.cpp b/can-utils.cpp
index 53b4f46c..abfe0a04 100644
--- a/can-utils.cpp
+++ b/can-utils.cpp
@@ -6,7 +6,7 @@
* 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
+ * 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,
@@ -23,6 +23,11 @@
*
*********************************************************************************/
+CanBus_c::CanBus_c(afb_binding_interface *itf)
+{
+ interface = itf;
+}
+
int CanBus_c::open()
{
const int canfd_on = 1;
@@ -85,11 +90,41 @@ int CanBus_c::close()
void CanBus_c::start_threads()
{
- std::queue <CanMessage_t> can_message_q;
+ std::queue <CanMessage_c> can_message_q;
- th_reading = std::thread(can_reader, interface, socket, can_message_q);
- th_decoding = std::thread(can_decoder, interface, can_message_q, can_message_queue);
- th_pushing = std::thread(can_event_push, interface, can_message_queue);
+ th_reading = std::thread(can_reader, interface, socket, can_message_q);
+ th_decoding = std::thread(can_decoder, interface, can_message_q, can_message_q);
+ th_pushing = std::thread(can_event_push, interface, can_message_q);
+}
+
+/*
+ * Send a can message from a CanMessage_c object.
+ */
+int CanBus_c::send_can_message(CanMessage_c can_msg)
+{
+ int nbytes;
+ canfd_frame *f;
+
+ f = can_msg.convert_to_canfd_frame();
+
+ if(socket >= 0)
+ {
+ nbytes = sendto(socket, &f, sizeof(struct canfd_frame), 0,
+ (struct sockaddr*)&txAddress, sizeof(txAddress));
+
+ if (nbytes == -1)
+ {
+ ERROR(interface, "send_can_message: Sending CAN frame failed.");
+ return -1;
+ }
+ return nbytes;
+ }
+ else
+ {
+ ERROR(interface, "send_can_message: socket not initialized. Attempt to reopen can device socket.");
+ open_can_dev();
+ }
+ return 0;
}
/********************************************************************************
@@ -100,81 +135,91 @@ void CanBus_c::start_threads()
uint32_t CanMessage_c::get_id()
{
- return id;
+ return id;
}
int CanMessage_c::get_format()
{
- return format;
+ return format;
}
uint8_t CanMessage_c::get_data()
{
- return data;
+ return data;
}
uint8_t CanMessage_c::get_lenght()
{
- return 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.");
+ switch(format):
+ case CanMessageFormat::SIMPLE:
+ id = new_id & CAN_SFF_MASK;
+ case CanMessageFormat::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");
+ if(new_format == CanMessageFormat::SIMPLE || new_format == CanMessageFormat::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;
+ data = new_data;
}
void CanMessage_c::set_lenght(uint8_t new_length)
{
- lenght = new_lenght;
+ lenght = new_lenght;
}
/*
- * This is the prefered way to initialize a CanMessage object
+ * This is the preferred way to initialize a CanMessage object
* from a read canfd_frame message.
*
* params: canfd_frame pointer
*/
-void CanMessage_c::convert_canfd_frame_to_CanMessage(canfd_frame *frame)
+void CanMessage_c::convert_from_canfd_frame(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;
+ 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 = CanMessageFormat::EXTENDED;
break;
default:
- format = STANDARD;
+ format = CanMessageFormat::STANDARD;
id = canfd_frame->can_id & CAN_SFF_MASK;
break;
if (sizeof(canfd_frame->data) <= sizeof(data))
{
- for (i = 0; i < lenght; i++)
- can_message->data.bytes[i] = canfd_frame->data[i];
+ memcpy(data, canfd_frame->data, lenght);
return 0;
} else if (sizeof(canfd_frame->data) >= CAN_MAX_DLEN)
ERROR(interface, "CanMessage_c: canfd_frame data too long to be stored into CanMessage object");
-} \ No newline at end of file
+}
+
+canfd_frame* convert_to_canfd_frame()
+{
+ canfd_frame frame;
+
+ frame.id = can_msg.get_id();
+ frame.len = can_msg.get_lenght();
+ frame.data = can_msg.get_data();
+
+ return &frame;
+}