aboutsummaryrefslogtreecommitdiffstats
path: root/low-can-binding/utils/socketcan-isotp.cpp
blob: a3792a7004c9f7aa59e44e47d6b8d6b35ad2ff0e (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
/*
 * Copyright (C) 2019, 2020 "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 "socketcan-isotp.hpp"

#include <net/if.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include "../utils/converter.hpp"
#include "../binding/application.hpp"

namespace utils
{
	/**
	 * @brief Open ISOTP socket this default function will not work, because
	 * isotp need rx_id and tx_id
	 *
	 * @param device_name The device name where to open socket
	 * @return int -1 fail
	 */
	int socketcan_isotp_t::open(std::string device_name)
	{
		AFB_WARNING("NOT USE THIS FUNCTION !");
		return open(device_name,NO_CAN_ID,NO_CAN_ID);
	}

	/**
	 * @brief Open ISOTP socket, the socket will be open and bind
	 * with rx_id and tx_id
	 *
	 * @param device_name The device name where to open socket
	 * @param rx_id The source can_id
	 * @param tx_id The destination can_id
	 * @return int 0 if ok else -1
	 */
	int socketcan_isotp_t::open(std::string device_name, canid_t rx_id, canid_t tx_id)
	{
		close();
		socket_ = socketcan_t::open(PF_CAN, SOCK_DGRAM, CAN_ISOTP);

		if(socket_ < 0)
		{
			AFB_ERROR("Error open ISO TP socket");
			return -1;
		}

		if(define_tx_address(device_name,rx_id,tx_id) < 0)
			return -1;

		struct can_isotp_options opts;
		memset(&opts,0,sizeof(opts));
		setopt(SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));

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

		return socket_;
	}

	/**
	 * @brief Allows to read message
	 *
	 * @return std::shared_ptr<message_t> The message that was read
	 */
	std::shared_ptr<message_t> socketcan_isotp_t::read_message()
	{

		std::shared_ptr<can_message_t> cm = std::make_shared<can_message_t>();
		uint8_t msg[MAX_ISOTP_FRAMES];
		ssize_t nbytes = read(socket(),msg,MAX_ISOTP_FRAMES);

		cm->set_id(tx_address_.can_addr.tp.rx_id);

		if(nbytes < 0)
		{
			AFB_ERROR("Can't read the next message from socket '%d'. '%s'", socket(), strerror(errno));
			return cm;
		}

		std::vector<uint8_t> data;
		for (int i=0; i < nbytes; i++)
			data.push_back(msg[i]);

		std::string data_string;
		data_string = converter_t::to_hex(msg,nbytes);
		AFB_DEBUG("DATA ISO TP for id : %x = %s",cm->get_id(),data_string.c_str());


		cm->set_data(data);
		cm->erase_flags();
		cm->set_flags(ISOTP_PROTOCOL);
		cm->set_length((uint32_t)nbytes);
		cm->set_sub_id((int)socket());
		// Need to define behaviour

		return cm;
	}

	/**
	 * @brief Allows to write can message
	 *
	 * @param m The message to send
	 * @return int 0 if ok else -1
	 */
	int socketcan_isotp_t::write_message(message_t& m)
	{
		size_t size = m.get_length();
		if(size < MAX_ISOTP_FRAMES)
		{
			ssize_t ret = write(socket(),m.get_data(),size);
			if(ret < 0)
			{
				AFB_ERROR("Error sending : %i %s", errno, ::strerror(errno));
				return -1;
			}

			if(ret != size)
				AFB_WARNING("ISOTP wrote only %zd byte",ret);
		}
		else
		{
			AFB_ERROR("Error sending too much data");
			return -1;
		}

		return 0;
	}

	/**
	 * @brief Define the tx address for the bind function
	 *
	 * @param device_name The device can that you want to bind
	 * @param rx_id The source can_id
	 * @param tx_id The destination can_id
	 * @return int 0 if ok else -1
	 */
	int socketcan_isotp_t::define_tx_address(std::string device_name, canid_t rx_id, canid_t tx_id)
	{
		struct ifreq ifr;
		::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(tx_id == NO_CAN_ID || rx_id == NO_CAN_ID)
		{
			AFB_ERROR("Error tx_id or rx_id");
			return -1;
		}

		tx_address_.can_addr.tp.rx_id = rx_id;
		tx_address_.can_addr.tp.tx_id = tx_id;

		return 0;
	}
}