Add feature to build messages and fix some functions
[apps/agl-service-can-low-level.git] / low-can-binding / utils / socketcan.hpp
1 #pragma once
2
3 /*
4  * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
5  * Author "Romain Forlot" <romain.forlot@iot.bzh>
6  * Author "Loïc Collignon" <loic.collignon@iot.bzh>
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <vector>
21
22 #include <sys/socket.h>
23 #include <linux/can/bcm.h>
24 #include <string.h>
25
26 #include "../binding/low-can-hat.hpp"
27 #include "../can/message/can-message.hpp"
28
29 #define INVALID_SOCKET -1
30
31 /**
32  * @enum socket_type
33  * @brief The type of socket
34  */
35 enum class socket_type {
36         BCM, ///< BCM - Socket BCM
37         J1939_ADDR_CLAIM, ///< J1939 - Socket J1939
38         J1939, ///< J1939 - Socket J1939
39         INVALID
40 };
41
42 namespace utils
43 {
44
45         class socketcan_t
46         {
47         public:
48                 socketcan_t();
49                 socketcan_t(const socketcan_t& s) = delete;
50                 socketcan_t(socketcan_t&&);
51                 socketcan_t& operator=(const socketcan_t& s);
52                 virtual ~socketcan_t();
53
54                 const struct sockaddr_can& get_tx_address() const;
55                 explicit operator bool() const;
56
57                 int socket() const;
58                 virtual int open(std::string device_name) = 0;
59                 int setopt(int level, int optname, const void* optval, socklen_t optlen);
60                 virtual int close();
61                 virtual std::shared_ptr<message_t> read_message() = 0;
62                 virtual int write_message(message_t& obj) = 0;
63                 virtual int write_message(std::vector<message_t>& vobj);
64
65         protected:
66                 int socket_;
67                 struct sockaddr_can tx_address_;
68                 int open(int domain, int type, int protocol);
69                 int bind(const struct sockaddr* addr, socklen_t len);
70                 int connect(const struct sockaddr* addr, socklen_t len);
71         };
72
73
74 }