summaryrefslogtreecommitdiffstats
path: root/low-can-binding/utils/socketcan-j1939
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2019-11-26 16:18:41 +0100
committerRomain Forlot <romain.forlot@iot.bzh>2019-11-28 16:11:46 +0100
commit451598fa9ce4e57a00c9312c04839e0b3dcdf320 (patch)
tree49a6341d98f1a6ca34320d5f6512ac08df1cccfd /low-can-binding/utils/socketcan-j1939
parent68d8bc019fab3e73654a330baf6b8dd7c11859ca (diff)
Integrate the J1939 features : address claiming and write.
This commit allows to write J1939 messages and add the management of the address claiming. Bug-AGL: SPEC-2386 Bug-AGL: SPEC-2976 Signed-off-by: Arthur Guyader <arthur.guyader@iot.bzh> Change-Id: I1bb95a7ba6f6ebe463319c3972d9d46897181d51 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'low-can-binding/utils/socketcan-j1939')
-rw-r--r--low-can-binding/utils/socketcan-j1939/socketcan-j1939-addressclaiming.cpp414
-rw-r--r--low-can-binding/utils/socketcan-j1939/socketcan-j1939-addressclaiming.hpp52
-rw-r--r--low-can-binding/utils/socketcan-j1939/socketcan-j1939-data.cpp68
-rw-r--r--low-can-binding/utils/socketcan-j1939/socketcan-j1939-data.hpp31
-rw-r--r--low-can-binding/utils/socketcan-j1939/socketcan-j1939.cpp288
-rw-r--r--low-can-binding/utils/socketcan-j1939/socketcan-j1939.hpp68
6 files changed, 921 insertions, 0 deletions
diff --git a/low-can-binding/utils/socketcan-j1939/socketcan-j1939-addressclaiming.cpp b/low-can-binding/utils/socketcan-j1939/socketcan-j1939-addressclaiming.cpp
new file mode 100644
index 00000000..df994f59
--- /dev/null
+++ b/low-can-binding/utils/socketcan-j1939/socketcan-j1939-addressclaiming.cpp
@@ -0,0 +1,414 @@
+/*
+ * Copyright (C) 2018, 2019 "IoT.bzh"
+ * Author "Arthur Guyader" <arthur.guyader@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.
+ */
+
+#include <unistd.h>
+#include "./socketcan-j1939-addressclaiming.hpp"
+
+namespace utils
+{
+ /**
+ * @brief Construct a new socketcan j1939 addressclaiming t::socketcan j1939 addressclaiming t object
+ *
+ */
+ socketcan_j1939_addressclaiming_t::socketcan_j1939_addressclaiming_t():
+ socketcan_j1939_t(),
+ table_j1939_address_{{std::make_pair(0,false)}},
+ signal_stop_{false},
+ claiming_state_{claiming_state::INITIAL}
+ {}
+
+ /**
+ * @brief Allows to read the claiming message and react to them
+ *
+ * @return std::shared_ptr<message_t> Return an INVALID j1939 message because no need of return (only signature of the function)
+ */
+ std::shared_ptr<message_t> socketcan_j1939_addressclaiming_t::read_message()
+ {
+ AFB_DEBUG("[socketcan_j1939_addressclaiming_t] read_message");
+ std::shared_ptr<message_t> invalid_message = std::make_shared<j1939_message_t>();
+ std::shared_ptr<message_t> m;
+ j1939_message_t* jm;
+
+ m = socketcan_j1939_t::read_message();
+ jm = static_cast<j1939_message_t*>(m.get());
+
+ if(jm->get_pgn() == J1939_PGN_ADDRESS_CLAIMED)
+ {
+ if(jm->get_addr() >= J1939_IDLE_ADDR)
+ {
+ AFB_DEBUG("Get invalid address claiming by name : %x",(unsigned int)jm->get_name());
+ return invalid_message;
+ }
+
+ if(jm->get_name() == htole64(J1939_NAME_ECU))
+ {
+ AFB_DEBUG("Get own address claiming");
+ return invalid_message;
+ }
+
+ AFB_DEBUG("Get address claiming from %x",(unsigned int)jm->get_name());
+
+ if(jm->get_addr() != current_address_)
+ {
+ save_addr_name(jm->get_addr(),jm->get_name());
+ return invalid_message;
+ }
+
+ if(claiming_state_ == claiming_state::CLAIMING)
+ {
+ if(jm->get_name() > htole64(J1939_NAME_ECU))
+ {
+ AFB_WARNING("Error from %x to use j1939 protocol",(unsigned int)htole64(J1939_NAME_ECU));
+ return invalid_message;
+ }
+
+ save_addr_name(jm->get_addr(),jm->get_name());
+
+
+ if(timer_handle_->evtSource)
+ {
+ TimerEvtStop(timer_handle_);
+ timer_handle_ = nullptr;
+ }
+
+ if(claim_address(false, true) < 0)
+ {
+ AFB_ERROR("Claim address failed");
+ change_state(claiming_state::INVALID);
+ return invalid_message;
+ }
+ }
+ else if(claiming_state_ == claiming_state::OPERATIONAL)
+ {
+ AFB_DEBUG("Address colision");
+ if(jm->get_name() > htole64(J1939_NAME_ECU))
+ {
+ if(claim_address(false,false) < 0)
+ {
+ AFB_ERROR("Claim address failed");
+ change_state(claiming_state::INVALID);
+ return invalid_message;
+ }
+ return invalid_message;
+ }
+
+ save_addr_name(jm->get_addr(),jm->get_name());
+
+ if(claim_address(false,true) < 0)
+ {
+ AFB_ERROR("Claim address failed");
+ change_state(claiming_state::INVALID);
+ return invalid_message;
+ }
+ }
+ }
+ return invalid_message;
+ }
+
+ /**
+ * @brief Initialize the table j1939 with the valid address posible
+ *
+ */
+ void socketcan_j1939_addressclaiming_t::initialize_table_j1939_address()
+ {
+ int start_addr = 128;
+ int end_addr = 247;
+ if(start_addr < 0)
+ {
+ AFB_ERROR("[socketcan-j1939-addressclaiming][initialize_table_j1939_address] Invalid start address");
+ }
+ else
+ {
+ if(end_addr >= J1939_IDLE_ADDR)
+ {
+ AFB_ERROR("[socketcan-j1939-addressclaiming][initialize_table_j1939_address] Invalid end address");
+ }
+ else
+ {
+ for (int i = start_addr; i <= end_addr; i++) {
+ table_j1939_address_[i] = std::make_pair(0,true);
+ }
+ }
+ }
+ }
+
+ /**
+ * @brief Save at an address a name
+ *
+ * @param addr The adress where you want to save name
+ * @param name The name of the ECU that is in the address
+ * @return int 0 if save is ok
+ */
+ int socketcan_j1939_addressclaiming_t::save_addr_name(uint8_t addr,name_t name)
+ {
+ if(addr < J1939_IDLE_ADDR)
+ {
+ if(table_j1939_address_[addr].first < name)
+ {
+ table_j1939_address_[addr].first = name;
+ AFB_DEBUG("[socketcan-j1939-addressclaiming][save_addr_name] NAME : %x <--> ADDR : %d",(unsigned int)name,addr);
+ }
+ else if(table_j1939_address_[addr].first == name)
+ {
+ AFB_WARNING("Name %x has already adress %d",(unsigned int)name,addr);
+ }
+ }
+ else
+ {
+ AFB_ERROR("Invalid address to save");
+ return -1;
+ }
+ return 0;
+ }
+
+ /**
+ * @brief Choose new address for the ECU check in the table the best place to claim
+ *
+ * @return uint8_t The new address choosen
+ */
+ uint8_t socketcan_j1939_addressclaiming_t::choose_new_address()
+ {
+ for (int i = 0; i < J1939_IDLE_ADDR; i++)
+ {
+ if(table_j1939_address_[i].second && i!=current_address_)
+ {
+ if( table_j1939_address_[i].first >= htole64(J1939_NAME_ECU) || table_j1939_address_[i].first == 0)
+ {
+ return (uint8_t) i;
+ }
+ }
+ }
+ return J1939_IDLE_ADDR;
+ }
+
+ /**
+ * @brief The function that destoy the timer
+ *
+ * @param timer_context The timer context to destroy
+ * @return int
+ */
+ int socketcan_j1939_addressclaiming_t::free_timer_handle(void *timer_context)
+ {
+ socketcan_j1939_addressclaiming_t *addressclaiming_socket = (socketcan_j1939_addressclaiming_t*) timer_context;
+ addressclaiming_socket->timer_handle_ = nullptr;
+ return 0;
+ }
+
+ /**
+ * @brief The function is call when at the end of the timer, the socket has don't receive
+ *
+ * @param timerhandle The timerhandle of the timer
+ * @return int 1 it's all it's ok
+ */
+ int socketcan_j1939_addressclaiming_t::no_response_claiming(TimerHandleT *timerhandle)
+ {
+ socketcan_j1939_addressclaiming_t *addressclaiming_socket = (socketcan_j1939_addressclaiming_t*) timerhandle->context;
+ // If the cache is cleared :
+ addressclaiming_socket->change_state(claiming_state::OPERATIONAL);
+ addressclaiming_socket->save_addr_name(addressclaiming_socket->current_address_,htole64(J1939_NAME_ECU));
+ AFB_DEBUG("Get address %d for this ecu", addressclaiming_socket->current_address_);
+ /*Else :
+
+ uint8_t data[3]= { 0, 0, 0, };
+ std::vector<uint8_t> data_v(data,data+3);
+ int res = addressclaiming_socket->write_j1939_message(J1939_PGN_REQUEST,data_v,3);
+ if(res < 0)
+ {
+ if(res == -99)
+ {
+ addressclaiming_socket->save_addr_name(addressclaiming_socket->current_address_,htole64(1));
+ AFB_DEBUG("Address busy but no claming request from other ECU");
+ addressclaiming_socket->claim_address(false,true);
+ }
+ else
+ {
+ AFB_ERROR("ERROR");
+ }
+ }
+ else
+ {
+ addressclaiming_socket->change_state(claiming_state::OPERATIONAL);
+ addressclaiming_socket->save_addr_name(addressclaiming_socket->current_address_,htole64(J1939_NAME_ECU));
+ AFB_DEBUG("Get address %d for this ecu", addressclaiming_socket->current_address_);
+ }*/
+
+ return 1;
+ }
+
+ /**
+ * @brief Launch timer when an address is claimed
+ *
+ */
+ void socketcan_j1939_addressclaiming_t::launch_timer()
+ {
+ timer_handle_ = (TimerHandleT*) malloc(sizeof(TimerHandleT));
+ timer_handle_->uid = "claiming_wait";
+ timer_handle_->delay = 250;
+ timer_handle_->count = 1;
+ timer_handle_->freeCB = free_timer_handle;
+ TimerEvtStart(afbBindingV3root, timer_handle_, no_response_claiming, (void *) this);
+ }
+
+
+ /**
+ * @brief Allows to claim a new address
+ *
+ * @param first_claim If true, the socket is open
+ * @param new_address If true, claim a new address, else only resend a claim with same address
+ * @return int -1 if fail
+ */
+ int socketcan_j1939_addressclaiming_t::claim_address(bool first_claim,bool new_address)
+ {
+ if(new_address)
+ {
+ AFB_DEBUG("New address");
+ current_address_ = choose_new_address();
+ change_state(claiming_state::CLAIMING);
+ launch_timer();
+ }
+
+ if(current_address_ == J1939_IDLE_ADDR)
+ {
+ AFB_ERROR("No address left");
+ return -1;
+ }
+
+ if(first_claim)
+ {
+ int ret = socketcan_j1939_t::open(device_name_,htole64(J1939_NAME_ECU),J1939_NO_PGN,current_address_);
+
+ if(ret < 0)
+ {
+ AFB_ERROR("Error open socket address claiming");
+ return -1;
+ }
+
+ AFB_DEBUG("[socketcan-j1939-addressclaiming][claim_address] Success open socket address claiming");
+ add_filter(J1939_NO_NAME,J1939_PGN_ADDRESS_CLAIMED,J1939_NO_ADDR,J1939_NO_NAME,J1939_PGN_PDU1_MAX,J1939_NO_ADDR);
+ define_opt();
+ }
+ else
+ {
+ tx_address_.can_addr.j1939.addr = current_address_;
+ if(bind((struct sockaddr *)&tx_address_, sizeof(tx_address_)) < 0)
+ {
+ AFB_ERROR("rebind() fail");
+ return -1;
+ }
+ }
+
+ uint64_t name = htole64(J1939_NAME_ECU);
+ uint8_t dat[8] = {0};
+ memcpy(dat, &name, 8);
+ struct sockaddr_can sockname;
+ memset(&sockname, 0, sizeof(sockname));
+ sockname.can_family = AF_CAN;
+ sockname.can_addr.j1939.pgn = J1939_PGN_ADDRESS_CLAIMED;
+ sockname.can_addr.j1939.addr = J1939_NO_ADDR;
+ socklen_t socklen = sizeof(sockname);
+
+ ssize_t ret = sendto(socket_, &dat, sizeof(dat), 0, (const struct sockaddr *)&sockname, socklen);
+
+ if(ret < 0 )
+ {
+ AFB_ERROR("Address claimed fail : %s", strerror(errno));
+ return -1;
+ }
+
+ AFB_DEBUG("[socketcan-j1939-addressclaiming][claim_address] Send address claiming request");
+
+ return (int)ret;
+ }
+
+/* int socketcan_j1939_addressclaiming_t::pgn_request()
+ {
+ static const uint8_t dat[3] = { 0, 0xee, 0, };
+ static struct sockaddr_can peername;
+ peername.can_family = AF_CAN;
+ peername.can_addr.j1939.pgn = J1939_PGN_REQUEST;
+ peername.can_addr.j1939.addr = J1939_NO_ADDR;
+ int ret = sendto(socket_, dat, sizeof(dat), 0, (const struct sockaddr *)&peername, sizeof(peername));
+
+ if(ret < 0)
+ {
+ AFB_ERROR("Error pgn_request()");
+ }
+ return ret;
+ }
+ */
+
+ /**
+ * @brief Return the address associate to a name
+ *
+ * @param name The name you are looking for
+ * @return uint8_t The address if it is present, else J1939_IDLE_ADDR
+ */
+ uint8_t socketcan_j1939_addressclaiming_t::get_addr_table(name_t name)
+ {
+ for(int i = 0; i < J1939_IDLE_ADDR; i++)
+ {
+ if(table_j1939_address_[i].first == name)
+ {
+ return (uint8_t) i;
+ }
+ }
+ return J1939_IDLE_ADDR;
+ }
+
+ /**
+ * @brief Allows to open a J1939 socket address claiming
+ *
+ * @param device_name The name of the device on which to open the socket
+ * @param pgn NO_PGN
+ * @return int Return 0 if ok else -1
+ */
+ int socketcan_j1939_addressclaiming_t::open(std::string device_name, pgn_t pgn)
+ {
+ device_name_ = device_name;
+ initialize_table_j1939_address();
+ if(claim_address(true,true) < 0)
+ {
+ AFB_ERROR("Claim address failed");
+ return -1;
+ }
+ return 0;
+ }
+
+ /**
+ * @brief Allows to change the state of the socket address claiming
+ * When the state change a mutex is lock
+ *
+ * @param new_state The new state
+ */
+ void socketcan_j1939_addressclaiming_t::change_state(claiming_state new_state)
+ {
+ std::unique_lock<std::mutex> lock(socketcan_j1939_t::mutex_claiming_);
+ claiming_state_ = new_state;
+ socketcan_j1939_t::signal_address_claiming_.notify_one();
+ }
+
+ /**
+ * @brief Allows to get the states of the socket
+ *
+ * @return claiming_state The state of the socket
+ */
+ claiming_state socketcan_j1939_addressclaiming_t::get_state()
+ {
+ return claiming_state_;
+ }
+
+} \ No newline at end of file
diff --git a/low-can-binding/utils/socketcan-j1939/socketcan-j1939-addressclaiming.hpp b/low-can-binding/utils/socketcan-j1939/socketcan-j1939-addressclaiming.hpp
new file mode 100644
index 00000000..92c44c8c
--- /dev/null
+++ b/low-can-binding/utils/socketcan-j1939/socketcan-j1939-addressclaiming.hpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2018, 2019 "IoT.bzh"
+ * Author "Arthur Guyader" <arthur.guyader@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
+#include <afb-timer.h>
+
+#include "./socketcan-j1939.hpp"
+
+namespace utils
+{
+ class socketcan_j1939_addressclaiming_t : public socketcan_j1939_t
+ {
+ public:
+ socketcan_j1939_addressclaiming_t();
+ virtual std::shared_ptr<message_t> read_message();
+ virtual int open(std::string device_name, pgn_t pgn);
+ virtual claiming_state get_state();
+ TimerHandleT *timer_handle_;
+ std::pair<uint64_t, bool> table_j1939_address_[J1939_IDLE_ADDR];
+
+ private:
+ int claim_address(bool first_claim,bool new_address);
+ int pgn_request();
+ void initialize_table_j1939_address();
+ int save_addr_name(uint8_t addr,name_t name);
+ uint8_t choose_new_address();
+ uint8_t get_addr_table(name_t name);
+ void change_state(claiming_state new_state);
+ void launch_timer();
+ static int no_response_claiming(TimerHandleT *timerhandle);
+ static int free_timer_handle(void *timer_context);
+
+ uint8_t current_address_;
+ bool signal_stop_;
+ claiming_state claiming_state_;
+ std::string device_name_;
+ };
+}
diff --git a/low-can-binding/utils/socketcan-j1939/socketcan-j1939-data.cpp b/low-can-binding/utils/socketcan-j1939/socketcan-j1939-data.cpp
new file mode 100644
index 00000000..c9d3e0a6
--- /dev/null
+++ b/low-can-binding/utils/socketcan-j1939/socketcan-j1939-data.cpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2018, 2019 "IoT.bzh"
+ * Author "Arthur Guyader" <arthur.guyader@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.
+ */
+
+
+#include "socketcan-j1939-data.hpp"
+#include "socketcan-j1939-addressclaiming.hpp"
+#include "../../binding/application.hpp"
+
+namespace utils
+{
+ /**
+ * @brief Open a socket for receive or send data
+ *
+ * @param device_name The device name to open the socket
+ * @param pgn The pgn to receive only him
+ * @return int Return the number of the socket
+ */
+ int socketcan_j1939_data_t::open(std::string device_name, pgn_t pgn)
+ {
+ int ret = socketcan_j1939_t::open(device_name,htole64(J1939_NAME_ECU),pgn,J1939_NO_ADDR);
+ if(ret >= 0)
+ {
+ if(tx_address_.can_addr.j1939.pgn != J1939_NO_PGN)
+ {
+ add_filter(J1939_NO_NAME,tx_address_.can_addr.j1939.pgn,J1939_NO_ADDR,J1939_NO_NAME,J1939_NO_PGN,J1939_NO_ADDR);
+ }
+ define_opt();
+ }
+ return ret;
+ }
+
+ /**
+ * @brief Write a message but check if the address claiming is operation before
+ *
+ * @param obj A j1939 message
+ * @return int 0 if the write is ok
+ */
+ int socketcan_j1939_data_t::write_message(message_t& obj)
+ {
+ std::unique_lock<std::mutex> lock(mutex_claiming_);
+ application_t &application = application_t::instance();
+ socketcan_j1939_addressclaiming_t *socket_addr_claimed = static_cast<socketcan_j1939_addressclaiming_t*>(application.get_socket_address_claiming().get());
+ while(socket_addr_claimed->get_state() != claiming_state::OPERATIONAL)
+ {
+ socketcan_j1939_t::signal_address_claiming_.wait(lock);
+ if(socket_addr_claimed->get_state() == claiming_state::INVALID)
+ {
+ AFB_ERROR("Invalid state");
+ return -1;
+ }
+ }
+ return socketcan_j1939_t::write_message(obj);
+ }
+}
diff --git a/low-can-binding/utils/socketcan-j1939/socketcan-j1939-data.hpp b/low-can-binding/utils/socketcan-j1939/socketcan-j1939-data.hpp
new file mode 100644
index 00000000..0cb0b8f2
--- /dev/null
+++ b/low-can-binding/utils/socketcan-j1939/socketcan-j1939-data.hpp
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2018, 2019 "IoT.bzh"
+ * Author "Arthur Guyader" <arthur.guyader@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
+
+#include "./socketcan-j1939.hpp"
+
+namespace utils
+{
+ class socketcan_j1939_data_t : public socketcan_j1939_t
+ {
+ public:
+ using socketcan_j1939_t::socketcan_j1939_t;
+ virtual int open(std::string device_name, pgn_t pgn);
+ virtual int write_message(message_t& obj);
+ };
+} \ No newline at end of file
diff --git a/low-can-binding/utils/socketcan-j1939/socketcan-j1939.cpp b/low-can-binding/utils/socketcan-j1939/socketcan-j1939.cpp
new file mode 100644
index 00000000..25d0f76e
--- /dev/null
+++ b/low-can-binding/utils/socketcan-j1939/socketcan-j1939.cpp
@@ -0,0 +1,288 @@
+/*
+ * Copyright (C) 2018, 2019 "IoT.bzh"
+ * Author "Arthur Guyader" <arthur.guyader@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.
+ */
+
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <iostream>
+#include <algorithm>
+#include <vector>
+#include "./socketcan-j1939.hpp"
+#include "socketcan-j1939-addressclaiming.hpp"
+
+namespace utils
+{
+ std::mutex socketcan_j1939_t::mutex_claiming_;
+ std::condition_variable socketcan_j1939_t::signal_address_claiming_;
+
+ /**
+ * @brief Apply a filter to the socket
+ *
+ * @param name - The name you want to receive
+ * @param pgn - The pgn you want to receive
+ * @param addr - The addr you want to receive
+ * @param name_mask - The mask to apply to the name (No mask : J1939_NO_NAME)
+ * @param pgn_mask - The mask to apply to the pgn (No mask : J1939_NO_PGN)
+ * @param addr_mask - The mask to apply to the addr (No mask : J1939_NO_ADDR)
+ */
+ void socketcan_j1939_t::add_filter(name_t name, pgn_t pgn, uint8_t addr, name_t name_mask, pgn_t pgn_mask, uint8_t addr_mask)
+ {
+ // AFB_DEBUG("[socketcan_j1939_t][add_filter] PGN : %" PRIu32 " ; NAME : %" PRIu64 " ; ADDR : %" PRIu8,pgn,(long unsigned int)name,addr);
+ // AFB_DEBUG("PGN_MASK : %" PRIu32 " ; NAME_MASK : %" PRIu64 "; ADDR_MASK : %" PRIu8,pgn_mask,(long unsigned int)name_mask,addr_mask);
+ int filter_on = 0;
+ struct j1939_filter filter;
+ memset(&filter, 0, sizeof(filter));
+ if (name)
+ {
+ filter.name = name;
+ if(name_mask != J1939_NO_NAME)
+ {
+ filter.name_mask = name_mask;
+ } else
+ {
+ filter.name_mask = ~0ULL;
+ }
+ ++filter_on;
+ }
+
+ if (addr < 0xff)
+ {
+ filter.addr = addr;
+ if(addr_mask != J1939_NO_ADDR)
+ {
+ filter.addr_mask = addr_mask;
+ } else
+ {
+ filter.addr_mask = ~0;
+ }
+ ++filter_on;
+ }
+ if (pgn <= J1939_PGN_MAX)
+ {
+ filter.pgn = pgn;
+ if(pgn_mask != J1939_NO_PGN)
+ {
+ filter.pgn_mask = pgn_mask;
+ } else
+ {
+ filter.pgn_mask = ~0;
+ }
+ ++filter_on;
+ }
+ if(filter_on)
+ {
+ setopt(SOL_CAN_J1939, SO_J1939_FILTER, &filter, sizeof(filter));
+ }
+ }
+
+ /**
+ * @brief Define some parameters on the socket
+ *
+ * @param promisc - Allows to accept all packets that the socket receives even if they are not addressed directly to it
+ * @param recv_own_msgs - Allows you to receive your own packets
+ * @param broadcast - Allows to write message with address brodcast (255)
+ */
+ void socketcan_j1939_t::define_opt(bool promisc, bool recv_own_msgs, bool broadcast)
+ {
+ int promisc_i = 0;
+ int recv_own_msgs_i = 0;
+ int broadcast_i = 0;
+
+ if(promisc) promisc_i = 1;
+ if(recv_own_msgs) recv_own_msgs_i=1;
+ if(broadcast) broadcast_i = 1;
+
+ setopt(SOL_CAN_J1939, SO_J1939_PROMISC, &promisc_i, sizeof(promisc_i));
+ setopt(SOL_CAN_J1939, SO_J1939_RECV_OWN, &recv_own_msgs_i, sizeof(recv_own_msgs_i));
+ setopt(SOL_SOCKET, SO_BROADCAST, &broadcast_i, sizeof(broadcast_i));
+ }
+
+
+ /**
+ * @brief Define the tx address for the bind function
+ *
+ * @param device_name - The device can that you want to bind
+ * @param name - The name that you want to bind
+ * @param pgn - The pgn that you want to bind
+ * @param addr - The addr that you want to bindthat you want to bind
+ */
+ void socketcan_j1939_t::define_tx_address(std::string device_name, name_t name, pgn_t pgn, uint8_t addr)
+ {
+
+ ::strcpy(ifr_.ifr_name, device_name.c_str());
+ AFB_DEBUG("ifr_name is : %s", ifr_.ifr_name);
+
+
+ if(::ioctl(socket_, SIOCGIFINDEX, &ifr_) < 0)
+ {
+ AFB_ERROR("ioctl failed. Error was : %s", strerror(errno));
+ close();
+ }
+ else
+ {
+ tx_address_.can_ifindex = ifr_.ifr_ifindex;
+ }
+
+ tx_address_.can_family = AF_CAN;
+
+
+ if(addr <= 0 || addr >= UINT8_MAX )
+ {
+ tx_address_.can_addr.j1939.addr = J1939_NO_ADDR;
+ }
+ else
+ {
+ tx_address_.can_addr.j1939.addr = addr;
+ }
+
+ if(name <= 0 || name >= UINT64_MAX )
+ {
+ tx_address_.can_addr.j1939.name = J1939_NO_NAME;
+ }
+ else
+ {
+ tx_address_.can_addr.j1939.name = name;
+ }
+
+ if(pgn <= 0 || pgn > J1939_PGN_MAX)
+ {
+ tx_address_.can_addr.j1939.pgn = J1939_NO_PGN;
+ }
+ else
+ {
+ tx_address_.can_addr.j1939.pgn = pgn;
+ }
+
+ }
+
+
+ /**
+ * @brief Open default socket
+ *
+ * @param device_name The device name to open the socket
+ * @return int The number of the socket
+ */
+ int socketcan_j1939_t::open(std::string device_name)
+ {
+ return open(device_name,0,0,0);
+ }
+
+ /**
+ * @brief Open a socket with name pgn and address
+ *
+ * @param device_name The device name to open the socket
+ * @param name - The name that you want to bind
+ * @param pgn - The pgn that you want to bind
+ * @param addr - The addr that you want to bindthat you want to bind
+ * @return int The number of the socket
+ */
+ int socketcan_j1939_t::open(std::string device_name, name_t name, pgn_t pgn, uint8_t addr)
+ {
+
+ socket_ = socketcan_t::open(PF_CAN, SOCK_DGRAM, CAN_J1939);
+
+ define_tx_address(device_name,name,pgn,addr);
+
+ if(bind((struct sockaddr *)&tx_address_, sizeof(tx_address_)) < 0)
+ {
+ AFB_ERROR("Bind failed. %s", strerror(errno));
+ close();
+ }
+
+ return socket_;
+ }
+
+ /**
+ * @brief Launch recvfrom on the socket with blocking flag
+ *
+ * @return std::shared_ptr<message_t> The j1939 message that is read
+ */
+ std::shared_ptr<message_t> socketcan_j1939_t::read_message()
+ {
+ return read_message(0);
+ }
+
+ /**
+ * @brief Launch recvfrom on the socket with the flag you want
+ *
+ * @param flag The flag param for the recvfrom
+ * @return std::shared_ptr<message_t> The j1939 message that is read
+ */
+ std::shared_ptr<message_t> socketcan_j1939_t::read_message(int flag)
+ {
+ socklen_t peernamelen;
+ std::shared_ptr<j1939_message_t> jm = std::make_shared<j1939_message_t>();
+ uint8_t data[128] = {0};
+
+ struct sockaddr_can peername;
+ peernamelen = sizeof(peername);
+ ssize_t nbytes = recvfrom(socket_, &data, sizeof(data), flag, (struct sockaddr *)&peername, &peernamelen);
+
+ if(nbytes < 0)
+ {
+ return nullptr;
+ }
+ //AFB_DEBUG("Data available: %i bytes read", (int)nbytes);
+
+ struct timeval tv;
+ ioctl(socket(), SIOCGSTAMP, &tv);
+ uint64_t timestamp = 1000000 * tv.tv_sec + tv.tv_usec;
+ jm = j1939_message_t::convert_from_addr(peername, data , nbytes, timestamp);
+ jm->set_sub_id((int)socket());
+ return jm;
+ }
+
+ /**
+ * @brief Write a j1939 message
+ *
+ * @param pgn The pgn that you want to emmit
+ * @param data The data that you want to emmit
+ * @param len_data The size of the data to emmit
+ * @return int 0 if the message is correctly send
+ */
+ int socketcan_j1939_t::write_j1939_message(pgn_t pgn, std::vector<uint8_t> &data, uint32_t len_data)
+ {
+ j1939_message_t msg = j1939_message_t(J1939_MAX_DLEN, len_data, message_format_t::J1939, data, 0, 0, pgn, 0);
+ msg.set_sockname(pgn,J1939_NO_NAME,J1939_NO_ADDR);
+ return write_message(msg);
+ }
+
+ /**
+ * @brief Write a j1939 message
+ *
+ * @param m A j1939 message
+ * @return int 0 if the message is correctly send
+ */
+ int socketcan_j1939_t::write_message(message_t& m)
+ {
+ j1939_message_t& jm = reinterpret_cast<j1939_message_t&>(m);
+ sockaddr_can sockname = jm.get_sockname();
+ uint8_t data[jm.get_data_vector().size()];
+
+ for(int i=0; i<jm.get_data_vector().size(); i++)
+ {
+ data[i] = jm.get_data_vector()[i];
+ }
+
+ if (sendto(socket_, &data, sizeof(data), 0, (const struct sockaddr *)&sockname, sizeof(sockname)) < 0)
+ {
+ AFB_ERROR("Error sending : %i %s", errno, ::strerror(errno));
+ return -errno;
+ }
+ return 0;
+ }
+} \ No newline at end of file
diff --git a/low-can-binding/utils/socketcan-j1939/socketcan-j1939.hpp b/low-can-binding/utils/socketcan-j1939/socketcan-j1939.hpp
new file mode 100644
index 00000000..6c2ea696
--- /dev/null
+++ b/low-can-binding/utils/socketcan-j1939/socketcan-j1939.hpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2018, 2019 "IoT.bzh"
+ * Author "Arthur Guyader" <arthur.guyader@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
+
+#include <thread>
+#include <mutex>
+#include <condition_variable>
+#include <linux/can/j1939.h>
+#include <net/if.h>
+#include "../socketcan.hpp"
+#include "../../can/message/j1939-message.hpp"
+
+#ifndef J1939_NAME_ECU
+#define J1939_NAME_ECU 0x1234
+#endif
+
+namespace utils
+{
+
+ /**
+ * @enum state
+ * @brief The state of the address claiming
+ */
+ enum class claiming_state {
+ INITIAL, ///< INITIAL - INITIAL state
+ CLAIMING, ///< CLAIMING - CLAIMING state
+ OPERATIONAL, ///< OPERATIONAL - OPERATIONAL state
+ INVALID
+ };
+
+ class socketcan_j1939_addressclaiming_t;
+ class socketcan_j1939_t : public socketcan_t
+ {
+ public:
+ using socketcan_t::socketcan_t;
+ virtual int open(std::string device_name);
+ virtual int open(std::string device_name, name_t name, pgn_t pgn, uint8_t addr);
+ virtual std::shared_ptr<message_t> read_message();
+ virtual std::shared_ptr<message_t> read_message(int flag);
+ virtual int write_message(message_t& obj);
+ virtual int write_j1939_message(pgn_t pgn, std::vector<uint8_t> &data, uint32_t len_data);
+
+ protected:
+ struct ifreq ifr_;
+ static std::mutex mutex_claiming_;
+ static std::condition_variable signal_address_claiming_;
+ void define_tx_address(std::string device_name, name_t name, pgn_t pgn, uint8_t addr);
+ void add_filter(name_t name, pgn_t pgn, uint8_t addr, name_t name_mask, pgn_t pgn_mask, uint8_t addr_mask);
+ void define_opt(bool promisc = true, bool recv_own_msgs = true, bool broadcast = true);
+
+ };
+}
+