Skip to content

Commit 9f0805f

Browse files
xiaoxiang781216xucheng5
authored andcommitted
net/can: Save simple options to socket_conn_s
like other protocols(e.g. ip, tcp, udp) Change-Id: Ia62558272c4a41178d3ed176bb19d32cb6d4969a Signed-off-by: Xiang Xiao <[email protected]>
1 parent 5e96e72 commit 9f0805f

File tree

8 files changed

+64
-167
lines changed

8 files changed

+64
-167
lines changed

net/can/can.h

-11
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,11 @@ struct can_conn_s
104104
struct can_poll_s pollinfo[4]; /* FIXME make dynamic */
105105

106106
#ifdef CONFIG_NET_CANPROTO_OPTIONS
107-
int32_t loopback;
108-
int32_t recv_own_msgs;
109-
# ifdef CONFIG_NET_CAN_CANFD
110-
int32_t fd_frames;
111-
# endif
112107
struct can_filter filters[CONFIG_NET_CAN_RAW_FILTER_MAX];
113108
int32_t filter_count;
114109
# ifdef CONFIG_NET_CAN_ERRORS
115110
can_err_mask_t err_mask;
116111
# endif
117-
# ifdef CONFIG_NET_CAN_RAW_TX_DEADLINE
118-
int32_t tx_deadline;
119-
# endif
120-
#endif
121-
#ifdef CONFIG_NET_TIMESTAMP
122-
int32_t timestamp; /* Socket timestamp enabled/disabled */
123112
#endif
124113
};
125114

net/can/can_callback.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ uint16_t can_callback(FAR struct net_driver_s *dev,
140140
* create timestamp and copy to iob
141141
*/
142142

143-
if (conn->timestamp)
143+
if (_SO_GETOPT(conn->sconn.s_options, SO_TIMESTAMP))
144144
{
145145
struct timeval tv;
146146
FAR struct timespec *ts = (FAR struct timespec *)&tv;

net/can/can_getsockopt.c

+19-84
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,6 @@ int can_getsockopt(FAR struct socket *psock, int level, int option,
8383
DEBUGASSERT(value != NULL && value_len != NULL);
8484
conn = psock->s_conn;
8585

86-
#ifdef CONFIG_NET_TIMESTAMP
87-
if (level == SOL_SOCKET && option == SO_TIMESTAMP)
88-
{
89-
if (*value_len != sizeof(int32_t))
90-
{
91-
return -EINVAL;
92-
}
93-
94-
*(FAR int32_t *)value = conn->timestamp;
95-
return OK;
96-
}
97-
#endif
98-
9986
if (level != SOL_CAN_RAW)
10087
{
10188
return -ENOPROTOOPT;
@@ -140,8 +127,8 @@ int can_getsockopt(FAR struct socket *psock, int level, int option,
140127
}
141128
break;
142129

143-
case CAN_RAW_ERR_FILTER:
144130
#ifdef CONFIG_NET_CAN_ERRORS
131+
case CAN_RAW_ERR_FILTER:
145132
if (*value_len < sizeof(can_err_mask_t))
146133
{
147134
return -EINVAL;
@@ -152,87 +139,35 @@ int can_getsockopt(FAR struct socket *psock, int level, int option,
152139
*mask = conn->err_mask;
153140
*value_len = sizeof(can_err_mask_t);
154141
}
155-
#endif
156142
break;
143+
#endif
157144

158145
case CAN_RAW_LOOPBACK:
159-
if (*value_len < sizeof(conn->loopback))
160-
{
161-
/* REVISIT: POSIX says that we should truncate the value if it
162-
* is larger than value_len. That just doesn't make sense
163-
* to me in this case.
164-
*/
165-
166-
ret = -EINVAL;
167-
}
168-
else
169-
{
170-
FAR int32_t *loopback = (FAR int32_t *)value;
171-
*loopback = conn->loopback;
172-
*value_len = sizeof(conn->loopback);
173-
}
174-
break;
175-
176146
case CAN_RAW_RECV_OWN_MSGS:
177-
if (*value_len < sizeof(conn->recv_own_msgs))
178-
{
179-
/* REVISIT: POSIX says that we should truncate the value if it
180-
* is larger than value_len. That just doesn't make sense
181-
* to me in this case.
182-
*/
183-
184-
ret = -EINVAL;
185-
}
186-
else
187-
{
188-
FAR int32_t *recv_own_msgs = (FAR int32_t *)value;
189-
*recv_own_msgs = conn->recv_own_msgs;
190-
*value_len = sizeof(conn->recv_own_msgs);
191-
}
192-
break;
193-
194147
#ifdef CONFIG_NET_CAN_CANFD
195148
case CAN_RAW_FD_FRAMES:
196-
if (*value_len < sizeof(conn->fd_frames))
197-
{
198-
/* REVISIT: POSIX says that we should truncate the value if it
199-
* is larger than value_len. That just doesn't make sense
200-
* to me in this case.
201-
*/
202-
203-
ret = -EINVAL;
204-
}
205-
else
206-
{
207-
FAR int32_t *fd_frames = (FAR int32_t *)value;
208-
*fd_frames = conn->fd_frames;
209-
*value_len = sizeof(conn->fd_frames);
210-
}
211-
break;
212149
#endif
213-
214-
case CAN_RAW_JOIN_FILTERS:
215-
break;
216-
217150
#ifdef CONFIG_NET_CAN_RAW_TX_DEADLINE
218151
case CAN_RAW_TX_DEADLINE:
219-
if (*value_len < sizeof(conn->tx_deadline))
220-
{
221-
/* REVISIT: POSIX says that we should truncate the value if it
222-
* is larger than value_len. That just doesn't make sense
223-
* to me in this case.
224-
*/
152+
#endif
153+
/* Verify that option is the size of an 'int'. Should also check
154+
* that 'value' is properly aligned for an 'int'
155+
*/
225156

226-
ret = -EINVAL;
227-
}
228-
else
157+
if (*value_len < sizeof(int))
229158
{
230-
FAR int32_t *tx_deadline = (FAR int32_t *)value;
231-
*tx_deadline = conn->tx_deadline;
232-
*value_len = sizeof(conn->tx_deadline);
233-
}
234-
break;
235-
#endif
159+
return -EINVAL;
160+
}
161+
162+
/* Sample the current options. This is atomic operation and so
163+
* should not require any special steps for thread safety. We
164+
* this outside of the macro because you can never be sure what
165+
* a macro will do.
166+
*/
167+
168+
*(FAR int *)value = _SO_GETOPT(conn->sconn.s_options, option);
169+
*value_len = sizeof(int);
170+
break;
236171

237172
#if CONFIG_NET_RECV_BUFSIZE > 0
238173
case SO_RCVBUF:

net/can/can_recvmsg.c

+13-10
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ static size_t can_recvfrom_newdata(FAR struct net_driver_s *dev,
131131
{
132132
unsigned int offset;
133133
size_t recvlen;
134-
135134
#ifdef CONFIG_NET_TIMESTAMP
136-
if (pstate->pr_conn->timestamp &&
135+
FAR struct can_conn_s *conn = pstate->pr_conn;
136+
137+
if (_SO_GETOPT(conn->sconn.s_options, SO_TIMESTAMP) &&
137138
pstate->pr_msglen == sizeof(struct timeval))
138139
{
139140
iob_copyout(pstate->pr_msgbuf, dev->d_iob, sizeof(struct timeval),
@@ -266,7 +267,8 @@ static inline int can_readahead(struct can_recvfrom_s *pstate)
266267
#endif
267268

268269
#ifdef CONFIG_NET_TIMESTAMP
269-
if (conn->timestamp && pstate->pr_msglen == sizeof(struct timeval))
270+
if (_SO_GETOPT(conn->sconn.s_options, SO_TIMESTAMP) &&
271+
pstate->pr_msglen == sizeof(struct timeval))
270272
{
271273
iob_copyout(pstate->pr_msgbuf, iob, sizeof(struct timeval),
272274
-CONFIG_NET_LL_GUARDSIZE);
@@ -313,7 +315,7 @@ static inline int can_readahead(struct can_recvfrom_s *pstate)
313315

314316
/* do not pass frames with DLC > 8 to a legacy socket */
315317
#if defined(CONFIG_NET_CANPROTO_OPTIONS) && defined(CONFIG_NET_CAN_CANFD)
316-
if (!conn->fd_frames)
318+
if (!_SO_GETOPT(conn->sconn.s_options, CAN_RAW_FD_FRAMES))
317319
#endif
318320
{
319321
if (recvlen > sizeof(struct can_frame))
@@ -398,14 +400,15 @@ static uint16_t can_recvfrom_eventhandler(FAR struct net_driver_s *dev,
398400

399401
/* do not pass frames with DLC > 8 to a legacy socket */
400402
#if defined(CONFIG_NET_CANPROTO_OPTIONS) && defined(CONFIG_NET_CAN_CANFD)
401-
if (!conn->fd_frames)
403+
if (!_SO_GETOPT(conn->sconn.s_options, CAN_RAW_FD_FRAMES))
402404
#endif
403405
{
404406
#ifdef CONFIG_NET_TIMESTAMP
405-
if ((conn->timestamp && (dev->d_len >
406-
sizeof(struct can_frame) + sizeof(struct timeval)))
407-
|| (!conn->timestamp && (dev->d_len >
408-
sizeof(struct can_frame))))
407+
if ((_SO_GETOPT(conn->sconn.s_options, SO_TIMESTAMP) &&
408+
dev->d_len > sizeof(struct can_frame) +
409+
sizeof(struct timeval)) ||
410+
(!_SO_GETOPT(conn->sconn.s_options, SO_TIMESTAMP) &&
411+
dev->d_len > sizeof(struct can_frame)))
409412
#else
410413
if (dev->d_len > sizeof(struct can_frame))
411414
#endif
@@ -534,7 +537,7 @@ ssize_t can_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
534537
state.pr_buffer = msg->msg_iov->iov_base;
535538

536539
#ifdef CONFIG_NET_TIMESTAMP
537-
if (conn->timestamp)
540+
if (_SO_GETOPT(conn->sconn.s_options, SO_TIMESTAMP))
538541
{
539542
state.pr_msgbuf = cmsg_append(msg, SOL_SOCKET, SO_TIMESTAMP,
540543
NULL, sizeof(struct timeval));

net/can/can_sendmsg.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ ssize_t can_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
202202
}
203203

204204
#if defined(CONFIG_NET_CANPROTO_OPTIONS) && defined(CONFIG_NET_CAN_CANFD)
205-
if (conn->fd_frames)
205+
if (_SO_GETOPT(conn->sconn.s_options, CAN_RAW_FD_FRAMES))
206206
{
207207
if (msg->msg_iov->iov_len != CANFD_MTU &&
208208
msg->msg_iov->iov_len != CAN_MTU)
@@ -236,7 +236,7 @@ ssize_t can_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
236236
if (msg->msg_controllen > sizeof(struct cmsghdr))
237237
{
238238
FAR struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg);
239-
if (conn->tx_deadline &&
239+
if (_SO_GETOPT(conn->sconn.s_options, CAN_RAW_TX_DEADLINE) &&
240240
cmsg->cmsg_level == SOL_CAN_RAW &&
241241
cmsg->cmsg_type == CAN_RAW_TX_DEADLINE &&
242242
cmsg->cmsg_len == sizeof(struct timeval))

net/can/can_setsockopt.c

+23-59
Original file line numberDiff line numberDiff line change
@@ -81,32 +81,6 @@ int can_setsockopt(FAR struct socket *psock, int level, int option,
8181

8282
conn = psock->s_conn;
8383

84-
#ifdef CONFIG_NET_TIMESTAMP
85-
86-
/* Generates a timestamp for each incoming packet */
87-
88-
if (level == SOL_SOCKET && option == SO_TIMESTAMP)
89-
{
90-
/* Verify that option is at least the size of an integer. */
91-
92-
if (value_len < sizeof(int32_t))
93-
{
94-
return -EINVAL;
95-
}
96-
97-
/* Lock the network so that we have exclusive access to the socket
98-
* options.
99-
*/
100-
101-
net_lock();
102-
103-
conn->timestamp = *((FAR int32_t *)value);
104-
105-
net_unlock();
106-
return OK;
107-
}
108-
#endif
109-
11084
if (level != SOL_CAN_RAW)
11185
{
11286
return -ENOPROTOOPT;
@@ -152,63 +126,53 @@ int can_setsockopt(FAR struct socket *psock, int level, int option,
152126
}
153127
break;
154128

155-
case CAN_RAW_ERR_FILTER:
156129
#ifdef CONFIG_NET_CAN_ERRORS
130+
case CAN_RAW_ERR_FILTER:
157131
if (value_len != sizeof(can_err_mask_t))
158132
{
159133
return -EINVAL;
160134
}
161135

162136
conn->err_mask = *(FAR can_err_mask_t *)value & CAN_ERR_MASK;
163-
#endif
164137
break;
138+
#endif
165139

166140
case CAN_RAW_LOOPBACK:
167-
if (value_len != sizeof(conn->loopback))
168-
{
169-
return -EINVAL;
170-
}
171-
172-
conn->loopback = *(FAR int32_t *)value;
173-
174-
break;
175-
176141
case CAN_RAW_RECV_OWN_MSGS:
177-
if (value_len != sizeof(conn->recv_own_msgs))
178-
{
179-
return -EINVAL;
180-
}
181-
182-
conn->recv_own_msgs = *(FAR int32_t *)value;
183-
184-
break;
185-
186142
#ifdef CONFIG_NET_CAN_CANFD
187143
case CAN_RAW_FD_FRAMES:
188-
if (value_len != sizeof(conn->fd_frames))
144+
#endif
145+
#ifdef CONFIG_NET_CAN_RAW_TX_DEADLINE
146+
case CAN_RAW_TX_DEADLINE:
147+
#endif
148+
/* Verify that option is the size of an 'int'. Should also check
149+
* that 'value' is properly aligned for an 'int'
150+
*/
151+
152+
if (value_len != sizeof(int))
189153
{
190154
return -EINVAL;
191155
}
192156

193-
conn->fd_frames = *(FAR int32_t *)value;
157+
/* Lock the network so that we have exclusive access to the socket
158+
* options.
159+
*/
194160

195-
break;
196-
#endif
161+
net_lock();
197162

198-
case CAN_RAW_JOIN_FILTERS:
199-
break;
163+
/* Set or clear the option bit */
200164

201-
#ifdef CONFIG_NET_CAN_RAW_TX_DEADLINE
202-
case CAN_RAW_TX_DEADLINE:
203-
if (value_len != sizeof(conn->tx_deadline))
165+
if (*(FAR int *)value)
204166
{
205-
return -EINVAL;
167+
_SO_SETOPT(conn->sconn.s_options, option);
168+
}
169+
else
170+
{
171+
_SO_CLROPT(conn->sconn.s_options, option);
206172
}
207173

208-
conn->tx_deadline = *(FAR int32_t *)value;
209-
174+
net_unlock();
210175
break;
211-
#endif
212176

213177
#if CONFIG_NET_RECV_BUFSIZE > 0
214178
case SO_RCVBUF:

net/socket/getsockopt.c

+3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ static int psock_socketlevel_option(FAR struct socket *psock, int option,
154154
* periodic transmission of probes */
155155
case SO_OOBINLINE: /* Leaves received out-of-band data inline */
156156
case SO_REUSEADDR: /* Allow reuse of local addresses */
157+
#ifdef CONFIG_NET_TIMESTAMP
158+
case SO_TIMESTAMP: /* Generates a timestamp for each incoming packet */
159+
#endif
157160
{
158161
sockopt_t optionset;
159162

net/socket/setsockopt.c

+3
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ static int psock_socketlevel_option(FAR struct socket *psock, int option,
137137
* periodic transmission of probes */
138138
case SO_OOBINLINE: /* Leaves received out-of-band data inline */
139139
case SO_REUSEADDR: /* Allow reuse of local addresses */
140+
#ifdef CONFIG_NET_TIMESTAMP
141+
case SO_TIMESTAMP: /* Generates a timestamp for each incoming packet */
142+
#endif
140143
{
141144
int setting;
142145

0 commit comments

Comments
 (0)