Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
tunnel_uavcan.c
Go to the documentation of this file.
1/*
2 * device over uavcan tunnel.
3 * Copyright (C) 2026 Fabien-B <fabien-b@github.com>
4 * This file is part of paparazzi. See LICENCE file.
5 */
6
7#include "tunnel_uavcan.h"
8#include "uavcan/uavcan.h"
9#include <uavcan.tunnel.Protocol.h>
10
11#if !defined(TUNNEL_UAVCAN_IFACE) && USE_CAN1
12#define TUNNEL_UAVCAN_IFACE uavcan1
13#endif
14
15#if !defined(TUNNEL_UAVCAN_IFACE) && USE_CAN2
16#define TUNNEL_UAVCAN_IFACE uavcan2
17#endif
18
19#if !defined(TUNNEL_UAVCAN_PROTOCOL)
20#define TUNNEL_UAVCAN_PROTOCOL 3
21#endif
22
23#if !defined(TUNNEL_UAVCAN_CHANNEL_ID)
24#define TUNNEL_UAVCAN_CHANNEL_ID 0
25#endif
26
28
29
30// Serial device functions
32static void tunnel_uavcan_put_byte(struct tunnel_uavcan_periph* up, long fd, uint8_t c);
33static void tunnel_uavcan_put_buffer(struct tunnel_uavcan_periph* up, long fd, const uint8_t *data, uint16_t len);
34static void tunnel_uavcan_send_message(struct tunnel_uavcan_periph* up, long fd);
38
40
42
46
48 ucdl->channel_id = channel_id;
49 ucdl->iface = &TUNNEL_UAVCAN_IFACE;
50 ring_buffer_init(&ucdl->tx_rb, ucdl->_tbuf, TX_BUFFER_SIZE);
51 ring_buffer_init(&ucdl->rx_rb, ucdl->_rbuf, RX_BUFFER_SIZE);
52 pprz_mtx_init(&ucdl->tx_mtx);
53 pprz_mtx_init(&ucdl->rx_mtx);
54
56
57 // setup device
58 ucdl->device.check_free_space = (check_free_space_t)tunnel_uavcan_check_free_space;
59 ucdl->device.put_byte = (put_byte_t)tunnel_uavcan_put_byte;
60 ucdl->device.put_buffer = (put_buffer_t)tunnel_uavcan_put_buffer;
61 ucdl->device.send_message = (send_message_t)tunnel_uavcan_send_message;
62 ucdl->device.char_available = (char_available_t)tunnel_uavcan_char_available;
63 ucdl->device.get_byte = (get_byte_t)tunnel_uavcan_get_byte;
64 ucdl->device.set_baudrate = (set_baudrate_t)tunnel_uavcan_set_baudrate;
65 ucdl->device.periph = ucdl;
66}
67
69 (void)iface;
72 // decode error
73 return;
74 }
75
76 if(utb_msg.protocol.protocol != TUNNEL_UAVCAN_PROTOCOL) {
77 return;
78 }
79
80 if(tunnel_uavcan0.channel_id == utb_msg.channel_id) {
82 ring_buffer_write(&tunnel_uavcan0.rx_rb, utb_msg.buffer.data, utb_msg.buffer.len);
84 }
85 }
86}
87
93 int free_space = 0;
94 if(pprz_mtx_lock(&up->tx_mtx) == 0) {
96 if(len < free_space) {
97 // keep the device locked and set the flag in fd.
98 *fd = 1;
99 } else {
100 // no space available, unlock.
101 free_space = 0;
102 pprz_mtx_unlock(&up->tx_mtx);
103 }
104
105 }
106 return free_space;
107}
108
110 tunnel_uavcan_put_buffer(up, fd, (const uint8_t*)&c, 1);
111}
112
113static void tunnel_uavcan_put_buffer(struct tunnel_uavcan_periph* up, long fd, const uint8_t *data, uint16_t len) {
114 if(fd == 0) {
115 // if fd is zero, assume the driver is not already locked
116 pprz_mtx_lock(&up->tx_mtx);
117 }
118
119 ring_buffer_write(&up->tx_rb, data, len);
120
121 if(fd == 0) {
122 // send immediately. mutex is already locked, so set flag.
124 }
125}
126
130 msg.protocol.protocol = TUNNEL_UAVCAN_PROTOCOL;
131
132 if(fd == 0) {
133 // flag not set, lock and unlock
134 pprz_mtx_lock(&up->tx_mtx);
135 }
136
137 // Empty the buffer
138 while(ring_buffer_available(&up->tx_rb)) {
139 size_t len = ring_buffer_read(&up->tx_rb, msg.buffer.data, sizeof(msg.buffer.data));
140 msg.buffer.len = len;
141 msg.channel_id = up->channel_id;
142
143 // Encode the message
144 uint32_t total_size = uavcan_tunnel_Broadcast_encode(&msg, buffer);
145 // Then send it
147 }
148
149 pprz_mtx_unlock(&up->tx_mtx);
150}
151
152
154 int available = 0;
155 if(pprz_mtx_lock(&up->rx_mtx) == 0) {
156 available = (int)ring_buffer_available(&up->rx_rb);
157 pprz_mtx_unlock(&up->rx_mtx);
158 }
159 return available;
160}
161
163 uint8_t c;
164 if(pprz_mtx_lock(&up->rx_mtx) == 0) {
165 ring_buffer_read(&up->rx_rb, &c, 1);
166 pprz_mtx_unlock(&up->rx_mtx);
167 }
168 return c;
169}
170
171
173
174}
int pprz_mtx_trylock(pprz_mutex_t *mtx)
Performs a nonblocking lock on the mutex.
int pprz_mtx_unlock(pprz_mutex_t *mtx)
int pprz_mtx_init(pprz_mutex_t *mtx)
int pprz_mtx_lock(pprz_mutex_t *mtx)
uint8_t msg[10]
Buffer used for general comunication over SPI (out buffer)
uint16_t up[LIGHT_NB]
Definition light_solar.c:48
uint16_t foo
Definition main_demo5.c:58
size_t ring_buffer_free_space(ring_buffer_t *ring_buffer)
Definition ring_buffer.c:29
size_t ring_buffer_write(ring_buffer_t *rb, const uint8_t *data, size_t len)
Write.
Definition ring_buffer.c:33
size_t ring_buffer_read(ring_buffer_t *rb, uint8_t *read_buffer, size_t len)
Read.
Definition ring_buffer.c:58
size_t ring_buffer_available(ring_buffer_t *rb)
Definition ring_buffer.c:20
void ring_buffer_init(ring_buffer_t *rb, uint8_t *buf, size_t size)
Init.
Definition ring_buffer.c:13
int fd
Definition serial.c:26
static void tunnel_uavcan_put_byte(struct tunnel_uavcan_periph *up, long fd, uint8_t c)
void tunnel_uavcan_init(void)
void tunnel_uavcan_set_baudrate(struct tunnel_uavcan_periph *up, uint32_t baudrate)
#define TUNNEL_UAVCAN_PROTOCOL
static int tunnel_uavcan_char_available(struct tunnel_uavcan_periph *up)
static int tunnel_uavcan_check_free_space(struct tunnel_uavcan_periph *up, long *fd, uint16_t len)
static void tunnel_uavcan_put_buffer(struct tunnel_uavcan_periph *up, long fd, const uint8_t *data, uint16_t len)
static void tunnel_uavcan_send_message(struct tunnel_uavcan_periph *up, long fd)
static void tunnel_uavcan_cb(struct uavcan_iface_t *iface, CanardRxTransfer *transfer)
void tunnel_uavcan_init_periph(struct tunnel_uavcan_periph *ucdl, uint8_t channel_id)
static uint8_t tunnel_uavcan_get_byte(struct tunnel_uavcan_periph *up)
struct tunnel_uavcan_periph tunnel_uavcan0
#define TUNNEL_UAVCAN_CHANNEL_ID
#define RX_BUFFER_SIZE
ring_buffer_t rx_rb
#define TX_BUFFER_SIZE
struct uavcan_iface_t * iface
void uavcan_broadcast(struct uavcan_iface_t *iface, uint64_t data_type_signature, uint16_t data_type_id, uint8_t priority, const void *payload, uint16_t payload_len)
Legacy function Broadcast an uavcan message to a specific interface.
Definition uavcan.c:313
void uavcan_bind(uint16_t data_type_id, uint64_t data_type_signature, uavcan_event *ev, uavcan_callback cb)
Bind to a receiving message from uavcan.
Definition uavcan.c:240
uavcan interface structure
Definition uavcan.h:44
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
int transfer(const Mat *from, const image_t *to)