2 * Copyright (C) 2018, 2019 "IoT.bzh"
3 * Author "Arthur Guyader" <arthur.guyader@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include "socketcan-j1939-data.hpp"
20 #include "socketcan-j1939-addressclaiming.hpp"
21 #include "../../binding/application.hpp"
26 * @brief Open a socket for receive or send data
28 * @param device_name The device name to open the socket
29 * @param pgn The pgn to receive only him
30 * @return int Return the number of the socket
32 int socketcan_j1939_data_t::open(std::string device_name, pgn_t pgn)
34 int ret = socketcan_j1939_t::open(device_name,htole64(J1939_NAME_ECU),pgn,J1939_NO_ADDR);
37 if(tx_address_.can_addr.j1939.pgn != J1939_NO_PGN)
38 add_filter(J1939_NO_NAME,tx_address_.can_addr.j1939.pgn,J1939_NO_ADDR,J1939_NO_NAME,J1939_NO_PGN,J1939_NO_ADDR);
46 * @brief Write a message but check if the address claiming is operation before
48 * @param obj A j1939 message
49 * @return int 0 if the write is ok
51 int socketcan_j1939_data_t::write_message(message_t& obj)
53 std::unique_lock<std::mutex> lock(mutex_claiming_);
54 application_t &application = application_t::instance();
55 socketcan_j1939_addressclaiming_t *socket_addr_claimed = static_cast<socketcan_j1939_addressclaiming_t*>(application.get_socket_address_claiming().get());
56 while(socket_addr_claimed->get_state() != claiming_state::OPERATIONAL)
58 socketcan_j1939_t::signal_address_claiming_.wait(lock);
59 if(socket_addr_claimed->get_state() == claiming_state::INVALID)
61 AFB_ERROR("Invalid state");
65 return socketcan_j1939_t::write_message(obj);