All: Make format coherent with the whole project
[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                                 add_filter(J1939_NO_NAME, tx_address_.can_addr.j1939.pgn, J1939_NO_ADDR, J1939_NO_NAME, J1939_NO_PGN, J1939_NO_ADDR);
39
40                         define_opt();
41                 }
42                 return ret;
43         }
44
45         /**
46          * @brief Write a message but check if the address claiming is operation before
47          *
48          * @param obj A j1939 message
49          * @return int 0 if the write is ok
50          */
51         int socketcan_j1939_data_t::write_message(message_t& obj)
52         {
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)
57                 {
58                         socketcan_j1939_t::signal_address_claiming_.wait(lock);
59                         if(socket_addr_claimed->get_state() == claiming_state::INVALID)
60                         {
61                                 AFB_ERROR("Invalid state");
62                                 return -1;
63                         }
64                 }
65                 return socketcan_j1939_t::write_message(obj);
66         }
67 }