aboutsummaryrefslogtreecommitdiffstats
path: root/low-can-binding/utils/socketcan-j1939/socketcan-j1939.cpp
blob: bb74c7666c95339daf6e10ab875b166326c0731b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright (C) 2018, 2019 "IoT.bzh"
 * Author "Arthur Guyader" <arthur.guyader@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	 http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include "./socketcan-j1939.hpp"
#include "socketcan-j1939-addressclaiming.hpp"

namespace utils
{
	std::mutex socketcan_j1939_t::mutex_claiming_;
	std::condition_variable socketcan_j1939_t::signal_address_claiming_;

	/**
	 * @brief Apply a filter to the socket
	 *
	 * @param name - The name you want to receive
	 * @param pgn - The pgn you want to receive
	 * @param addr - The addr you want to receive
	 * @param name_mask - The mask to apply to the name (No mask : J1939_NO_NAME)
	 * @param pgn_mask - The mask to apply to the pgn (No mask : J1939_NO_PGN)
	 * @param addr_mask - The mask to apply to the addr (No mask : J1939_NO_ADDR)
	 */
	void socketcan_j1939_t::add_filter(name_t name, pgn_t pgn, uint8_t addr, name_t name_mask, pgn_t pgn_mask, uint8_t addr_mask)
	{
	//	AFB_DEBUG("[socketcan_j1939_t][add_filter] PGN : %" PRIu32 " ; NAME : %" PRIu64 " ; ADDR : %" PRIu8,pgn,(long unsigned int)name,addr);
	//	AFB_DEBUG("PGN_MASK : %" PRIu32 " ; NAME_MASK : %" PRIu64 "; ADDR_MASK : %" PRIu8,pgn_mask,(long unsigned int)name_mask,addr_mask);
		int filter_on = 0;
		struct j1939_filter filter;
		memset(&filter, 0, sizeof(filter));
		if (name)
		{
			filter.name = name;
			if(name_mask != J1939_NO_NAME)
			{
				filter.name_mask = name_mask;
			} else
			{
				filter.name_mask = ~0ULL;
			}
			++filter_on;
		}

		if (addr < 0xff)
		{
			filter.addr = addr;
			if(addr_mask != J1939_NO_ADDR)
			{
				filter.addr_mask = addr_mask;
			} else
			{
				filter.addr_mask = ~0;
			}
			++filter_on;
		}
		if (pgn <= J1939_PGN_MAX)
		{
			filter.pgn = pgn;
			if(pgn_mask != J1939_NO_PGN)
			{
				filter.pgn_mask = pgn_mask;
			} else
			{
				filter.pgn_mask = ~0;
			}
			++filter_on;
		}
		if(filter_on)
		{
			setopt(SOL_CAN_J1939, SO_J1939_FILTER, &filter, sizeof(filter));
		}
	}

	/**
	 * @brief Define some parameters on the socket
	 *
	 * @param promisc - Allows to accept all packets that the socket receives even if they are not addressed directly to it
	 * @param recv_own_msgs - Allows you to receive your own packets
	 * @param broadcast - Allows to write message with address brodcast (255)
	 */
	void socketcan_j1939_t::define_opt(bool promisc, bool recv_own_msgs, bool broadcast)
	{
		int promisc_i = 0;
		int recv_own_msgs_i = 0;
		int broadcast_i = 0;

		if(promisc) promisc_i = 1;
		if(recv_own_msgs) recv_own_msgs_i=1;
		if(broadcast) broadcast_i = 1;

		setopt(SOL_CAN_J1939, SO_J1939_PROMISC, &promisc_i, sizeof(promisc_i));
		setopt(SOL_CAN_J1939, SO_J1939_RECV_OWN, &recv_own_msgs_i, sizeof(recv_own_msgs_i));
		setopt(SOL_SOCKET, SO_BROADCAST, &broadcast_i, sizeof(broadcast_i));
	}


	/**
	 * @brief Define the tx address for the bind function
	 *
	 * @param device_name - The device can that you want to bind
	 * @param name - The name that you want to bind
	 * @param pgn - The pgn that you want to bind
	 * @param addr - The addr that you want to bindthat you want to bind
	 */
	void socketcan_j1939_t::define_tx_address(std::string device_name, name_t name, pgn_t pgn, uint8_t addr)
	{

		::strcpy(ifr_.ifr_name, device_name.c_str());
		AFB_DEBUG("ifr_name is : %s", ifr_.ifr_name);


		if(::ioctl(socket_, SIOCGIFINDEX, &ifr_) < 0)
		{
			AFB_ERROR("ioctl failed. Error was : %s", strerror(errno));
			close();
		}
		else
		{
			tx_address_.can_ifindex = ifr_.ifr_ifindex;
		}

		tx_address_.can_family = AF_CAN;


		if(addr <= 0 || addr >= UINT8_MAX )
		{
			tx_address_.can_addr.j1939.addr = J1939_NO_ADDR;
		}
		else
		{
			tx_address_.can_addr.j1939.addr = addr;
		}

		if(name <= 0 || name >= UINT64_MAX )
		{
			tx_address_.can_addr.j1939.name = J1939_NO_NAME;
		}
		else
		{
			tx_address_.can_addr.j1939.name = name;
		}

		if(pgn <= 0 || pgn > J1939_PGN_MAX)
		{
			tx_address_.can_addr.j1939.pgn = J1939_NO_PGN;
		}
		else
		{
			tx_address_.can_addr.j1939.pgn = pgn;
		}

	}


	/**
	 * @brief Open default socket
	 *
	 * @param device_name The device name to open the socket
	 * @return int The number of the socket
	 */
	int socketcan_j1939_t::open(std::string device_name)
	{
		return open(device_name,0,0,0);
	}

	/**
	 * @brief Open a socket with name pgn and address
	 *
	 * @param device_name The device name to open the socket
	 * @param name - The name that you want to bind
	 * @param pgn - The pgn that you want to bind
	 * @param addr - The addr that you want to bindthat you want to bind
	 * @return int The number of the socket
	 */
	int socketcan_j1939_t::open(std::string device_name, name_t name, pgn_t pgn, uint8_t addr)
	{

		socket_ = socketcan_t::open(PF_CAN, SOCK_DGRAM, CAN_J1939);

		define_tx_address(device_name,name,pgn,addr);

		if(bind((struct sockaddr *)&tx_address_, sizeof(tx_address_)) < 0)
		{
			AFB_ERROR("Bind failed. %s", strerror(errno));
			close();
		}

		return socket_;
	}

	/**
	 * @brief Launch recvfrom on the socket with blocking flag
	 *
	 * @return std::shared_ptr<message_t> The j1939 message that is read
	 */
	std::shared_ptr<message_t> socketcan_j1939_t::read_message()
	{
		return read_message(0);
	}

	/**
	 * @brief Launch recvfrom on the socket with the flag you want
	 *
	 * @param flag The flag param for the recvfrom
	 * @return std::shared_ptr<message_t> The j1939 message that is read
	 */
	std::shared_ptr<message_t> socketcan_j1939_t::read_message(int flag)
	{
		socklen_t peernamelen;
		std::shared_ptr<j1939_message_t> jm = std::make_shared<j1939_message_t>();
		uint8_t data[128] = {0};

		struct sockaddr_can peername;
		peernamelen = sizeof(peername);
		ssize_t nbytes = recvfrom(socket_, &data, sizeof(data), flag, (struct sockaddr *)&peername,  &peernamelen);

		if(nbytes < 0)
		{
			return nullptr;
		}
		//AFB_DEBUG("Data available: %i bytes read", (int)nbytes);

		struct timeval tv;
		ioctl(socket(), SIOCGSTAMP, &tv);
		uint64_t timestamp = 1000000 * tv.tv_sec + tv.tv_usec;
		jm = j1939_message_t::convert_from_addr(peername, data , nbytes, timestamp);
		jm->set_sub_id((int)socket());
		return jm;
	}

	/**
	 * @brief Write a j1939 message
	 *
	 * @param pgn The pgn that you want to emmit
	 * @param data The data that you want to emmit
	 * @param len_data The size of the data to emmit
	 * @return int 0 if the message is correctly send
	 */
	int socketcan_j1939_t::write_j1939_message(pgn_t pgn, std::vector<uint8_t> &data, uint32_t len_data)
	{
		j1939_message_t msg = j1939_message_t(J1939_MAX_DLEN, len_data, message_format_t::J1939, data, 0, 0, pgn, 0);
		msg.set_sockname(pgn,J1939_NO_NAME,J1939_NO_ADDR);
		return write_message(msg);
	}

	/**
	 * @brief Write a j1939 message
	 *
	 * @param m A j1939 message
	 * @return int 0 if the message is correctly send
	 */
	int socketcan_j1939_t::write_message(message_t& m)
	{
		j1939_message_t& jm = reinterpret_cast<j1939_message_t&>(m);
		sockaddr_can sockname =  jm.get_sockname();
		uint8_t data[jm.get_data_vector().size()];

		for(int i=0; i<jm.get_data_vector().size(); i++)
		{
			data[i] = jm.get_data_vector()[i];
		}

		if (sendto(socket_, &data, sizeof(data), 0, (const struct sockaddr *)&sockname, sizeof(sockname)) < 0)
		{
			AFB_ERROR("Error sending : %i %s", errno, ::strerror(errno));
			return -errno;
		}
		return 0;
	}
}