diff options
author | 2019-11-26 16:18:41 +0100 | |
---|---|---|
committer | 2019-11-28 16:11:46 +0100 | |
commit | 451598fa9ce4e57a00c9312c04839e0b3dcdf320 (patch) | |
tree | 49a6341d98f1a6ca34320d5f6512ac08df1cccfd /low-can-binding/utils/socketcan-j1939/socketcan-j1939-data.cpp | |
parent | 68d8bc019fab3e73654a330baf6b8dd7c11859ca (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/socketcan-j1939-data.cpp')
-rw-r--r-- | low-can-binding/utils/socketcan-j1939/socketcan-j1939-data.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
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); + } +} |