Restore /etc/dev-mapping.conf support
[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                 return socketcan_j1939_t::open(device_name, htole64(J1939_NAME_ECU), pgn, J1939_NO_ADDR);
35         }
36
37         /**
38          * @brief Write a message but check if the address claiming is operation before
39          *
40          * @param obj A j1939 message
41          * @return int 0 if the write is ok
42          */
43         int socketcan_j1939_data_t::write_message(message_t& obj)
44         {
45                 std::unique_lock<std::mutex> lock(mutex_claiming_);
46                 application_t &application = application_t::instance();
47                 socketcan_j1939_addressclaiming_t *socket_addr_claimed = static_cast<socketcan_j1939_addressclaiming_t*>(application.get_socket_address_claiming().get());
48                 while(socket_addr_claimed->get_state() != claiming_state::OPERATIONAL)
49                 {
50                         socketcan_j1939_t::signal_address_claiming_.wait(lock);
51                         if(socket_addr_claimed->get_state() == claiming_state::INVALID)
52                         {
53                                 AFB_ERROR("Invalid state");
54                                 return -1;
55                         }
56                 }
57                 return socketcan_j1939_t::write_message(obj);
58         }
59 }