j1939: Following kernel updates
[apps/agl-service-can-low-level.git] / low-can-binding / utils / socketcan-j1939 / socketcan-j1939-data.cpp
1 /*
2  * Copyright (C) 2018, 2019 "IoT.bzh"
3  * Author "Arthur Guyader" <arthur.guyader@iot.bzh>
4  *
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18
19 #include "socketcan-j1939-data.hpp"
20 #include "socketcan-j1939-addressclaiming.hpp"
21 #include "../../binding/application.hpp"
22
23 namespace utils
24 {
25         /**
26          * @brief Open a socket for receive or send data
27          *
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
31          */
32         int socketcan_j1939_data_t::open(std::string device_name, pgn_t pgn)
33         {
34                 int ret = socketcan_j1939_t::open(device_name,htole64(J1939_NAME_ECU),pgn,J1939_NO_ADDR);
35                 if(ret >= 0)
36                 {
37                         if(tx_address_.can_addr.j1939.pgn != J1939_NO_PGN)
38                         {
39                                 add_filter(J1939_NO_NAME,tx_address_.can_addr.j1939.pgn,J1939_NO_ADDR,J1939_NO_NAME,J1939_NO_PGN,J1939_NO_ADDR);
40                         }
41                         define_opt();
42                 }
43                 return ret;
44         }
45
46         /**
47          * @brief Write a message but check if the address claiming is operation before
48          *
49          * @param obj A j1939 message
50          * @return int 0 if the write is ok
51          */
52         int socketcan_j1939_data_t::write_message(message_t& obj)
53         {
54                 std::unique_lock<std::mutex> lock(mutex_claiming_);
55                 application_t &application = application_t::instance();
56                 socketcan_j1939_addressclaiming_t *socket_addr_claimed = static_cast<socketcan_j1939_addressclaiming_t*>(application.get_socket_address_claiming().get());
57                 while(socket_addr_claimed->get_state() != claiming_state::OPERATIONAL)
58                 {
59                         socketcan_j1939_t::signal_address_claiming_.wait(lock);
60                         if(socket_addr_claimed->get_state() == claiming_state::INVALID)
61                         {
62                                 AFB_ERROR("Invalid state");
63                                 return -1;
64                         }
65                 }
66                 return socketcan_j1939_t::write_message(obj);
67         }
68 }