Skip to content

Commit 51a845c

Browse files
jarivanewijkPetervdPerk-NXP
authored andcommitted
SocketCAN: add non-blocking write
Co-authored-by: Peter van der Perk <[email protected]>
1 parent d3b1ee9 commit 51a845c

File tree

4 files changed

+99
-37
lines changed

4 files changed

+99
-37
lines changed

net/can/can.h

+26
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,32 @@ ssize_t can_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
267267

268268
void can_poll(FAR struct net_driver_s *dev, FAR struct can_conn_s *conn);
269269

270+
/****************************************************************************
271+
* Name: psock_can_cansend
272+
*
273+
* Description:
274+
* psock_can_cansend() returns a value indicating if a write to the socket
275+
* would block. It is still possible that the write may block if another
276+
* write occurs first.
277+
*
278+
* Input Parameters:
279+
* psock An instance of the internal socket structure.
280+
*
281+
* Returned Value:
282+
* OK
283+
* At least one byte of data could be successfully written.
284+
* -EWOULDBLOCK
285+
* There is no room in the output buffer.
286+
* -EBADF
287+
* An invalid descriptor was specified.
288+
*
289+
* Assumptions:
290+
* None
291+
*
292+
****************************************************************************/
293+
294+
int psock_can_cansend(FAR struct socket *psock);
295+
270296
/****************************************************************************
271297
* Name: can_sendmsg
272298
*

net/can/can_input.c

+24-28
Original file line numberDiff line numberDiff line change
@@ -155,48 +155,44 @@ int can_input(struct net_driver_s *dev)
155155
{
156156
FAR struct can_conn_s *conn = NULL;
157157
int ret = OK;
158+
uint16_t buflen = dev->d_len;
158159

159160
do
160161
{
161-
/* FIXME Support for multiple sockets??? */
162-
163162
conn = can_nextconn(conn);
164-
}
165-
while (conn && conn->dev != 0 && dev != conn->dev);
166-
167-
if (conn)
168-
{
169-
uint16_t flags;
170163

171-
/* Setup for the application callback */
164+
if (conn && (conn->dev == 0x0 || dev == conn->dev))
165+
{
166+
uint16_t flags;
172167

173-
dev->d_appdata = dev->d_buf;
174-
dev->d_sndlen = 0;
168+
/* Setup for the application callback */
175169

176-
/* Perform the application callback */
170+
dev->d_appdata = dev->d_buf;
171+
dev->d_sndlen = 0;
172+
dev->d_len = buflen;
177173

178-
flags = can_callback(dev, conn, CAN_NEWDATA);
174+
/* Perform the application callback */
179175

180-
/* If the operation was successful, the CAN_NEWDATA flag is removed
181-
* and thus the packet can be deleted (OK will be returned).
182-
*/
176+
flags = can_callback(dev, conn, CAN_NEWDATA);
183177

184-
if ((flags & CAN_NEWDATA) != 0)
185-
{
186-
/* No.. the packet was not processed now. Return -EAGAIN so
187-
* that the driver may retry again later. We still need to
188-
* set d_len to zero so that the driver is aware that there
189-
* is nothing to be sent.
178+
/* If the operation was successful, the CAN_NEWDATA flag is removed
179+
* and thus the packet can be deleted (OK will be returned).
190180
*/
191181

192-
nwarn("WARNING: Packet not processed\n");
193-
ret = -EAGAIN;
182+
if ((flags & CAN_NEWDATA) != 0)
183+
{
184+
/* No.. the packet was not processed now. Return -EAGAIN so
185+
* that the driver may retry again later. We still need to
186+
* set d_len to zero so that the driver is aware that there
187+
* is nothing to be sent.
188+
*/
189+
190+
nwarn("WARNING: Packet not processed\n");
191+
ret = -EAGAIN;
192+
}
194193
}
195194
}
196-
else
197-
{
198-
ninfo("No CAN listener\n");
199-
}
195+
while (conn);
200196

201197
return ret;
202198
}

net/can/can_sendmsg.c

+46-2
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,17 @@ ssize_t can_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
259259
netdev_txnotify_dev(dev);
260260

261261
/* Wait for the send to complete or an error to occur.
262-
* net_lockedwait will also terminate if a signal is received.
262+
* net_timedwait will also terminate if a signal is received.
263263
*/
264264

265-
ret = net_lockedwait(&state.snd_sem);
265+
if (_SS_ISNONBLOCK(conn->sconn.s_flags) || (flags & MSG_DONTWAIT) != 0)
266+
{
267+
ret = net_timedwait(&state.snd_sem, 0);
268+
}
269+
else
270+
{
271+
ret = net_timedwait(&state.snd_sem, UINT_MAX);
272+
}
266273

267274
/* Make sure that no further events are processed */
268275

@@ -296,4 +303,41 @@ ssize_t can_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
296303
return state.snd_sent;
297304
}
298305

306+
/****************************************************************************
307+
* Name: psock_can_cansend
308+
*
309+
* Description:
310+
* psock_can_cansend() returns a value indicating if a write to the socket
311+
* would block. No space in the buffer is actually reserved, so it is
312+
* possible that the write may still block if the buffer is filled by
313+
* another means.
314+
*
315+
* Input Parameters:
316+
* psock An instance of the internal socket structure.
317+
*
318+
* Returned Value:
319+
* OK
320+
* At least one byte of data could be successfully written.
321+
* -EWOULDBLOCK
322+
* There is no room in the output buffer.
323+
* -EBADF
324+
* An invalid descriptor was specified.
325+
*
326+
****************************************************************************/
327+
328+
int psock_can_cansend(FAR struct socket *psock)
329+
{
330+
/* Verify that we received a valid socket */
331+
332+
if (psock == NULL || psock->s_conn == NULL)
333+
{
334+
nerr("ERROR: Invalid socket\n");
335+
return -EBADF;
336+
}
337+
338+
/* TODO Query CAN driver mailboxes to see if there's mailbox available */
339+
340+
return OK;
341+
}
342+
299343
#endif /* CONFIG_NET && CONFIG_NET_CAN */

net/can/can_sockif.c

+3-7
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,13 @@ static uint16_t can_poll_eventhandler(FAR struct net_driver_s *dev,
139139
eventset |= (POLLHUP | POLLERR);
140140
}
141141

142-
#if 0
143142
/* A poll is a sign that we are free to send data. */
144143

145144
else if ((flags & CAN_POLL) != 0 &&
146-
psock_udp_cansend(info->psock) >= 0)
145+
psock_can_cansend(info->psock) >= 0)
147146
{
148147
eventset |= (POLLOUT & info->fds->events);
149148
}
150-
#endif
151149

152150
/* Awaken the caller of poll() is requested event occurred. */
153151

@@ -608,14 +606,12 @@ static int can_poll_local(FAR struct socket *psock, FAR struct pollfd *fds,
608606
fds->revents |= (POLLRDNORM & fds->events);
609607
}
610608

611-
#if 0
612-
if (psock_udp_cansend(psock) >= 0)
609+
if (psock_can_cansend(psock) >= 0)
613610
{
614-
/* Normal data may be sent without blocking (at least one byte). */
611+
/* A CAN frame may be sent without blocking. */
615612

616613
fds->revents |= (POLLWRNORM & fds->events);
617614
}
618-
#endif
619615

620616
/* Check if any requested events are already in effect */
621617

0 commit comments

Comments
 (0)