Cynerd opened a new pull request, #17360:
URL: https://github.com/apache/nuttx/pull/17360
## Summary
This is a follow-up on https://github.com/apache/nuttx/pull/17283. The
@ppisa comments there sent me in a different direction altogether, and thus I
am creating a new PR with a different approach.
## Impact
This preserves the old behavior for reading and writing for default message
alignment value, but allows more versatile usage such as reading of a single
message and reading to the array of messages.
## Testing
Tested on a custom SAMv7 board.
```c
int fd = open("/dev/can0", O_RDWR);
if (fd < 0) {
printf("Failed to open can: %s", strerror(errno));
return -1;
}
unsigned msgalign = 0;
ioctl(fd, CANIOC_SET_MSGALIGN, &msgalign);
while (true) {
sleep(1); /* Give us some time to use cansend multiple times*/
char buf[200];
size_t n = read(fd, buf, 200);
printf("Read %d", n);
size_t offset = 0;
while (offset < n) {
struct can_msg_s *msg = (void*)&buf[offset];
printf(" id%d", msg->cm_hdr.ch_id);
offset += CAN_MSGLEN(can_dlc2bytes(msg->cm_hdr.ch_dlc));
if (msgalign)
offset = roundup(offset, msgalign);
}
printf("\n");
}
```
```bash
for i in {0..4}; do cansend can0 "00$i##0"; done
```
For `msgalign == 0`:
```
Read 4 id0
Read 4 id1
Read 4 id2
Read 4 id3
Read 4 id4
```
For `msgalign == 1`:
```
Read 4 id0
Read 16 id1 id2 id3 id4
```
(Note: the first message is handled before the rest are received so that is
why it is separate)
For `msgalign == 3`::
```
Read 6 id0
Read 24 id1 id2 id3 id4
```
The write tests follow. They are in pair of C code and candump log.
```c
int fd = open("/dev/can0", O_RDWR);
unsigned msgalign = 1;
ioctl(fd, CANIOC_SET_MSGALIGN, &msgalign);
char buf[9];
struct can_msg_s *msg1 = (void*)&buf[0];
msg1->cm_hdr = (struct can_hdr_s) {.ch_id = 1, .ch_dlc = 0};
struct can_msg_s *msg2 = (void*)&buf[4];
msg2->cm_hdr = (struct can_hdr_s) {.ch_id = 2, .ch_dlc = 0};
size_t n = write(fd, buf, sizeof buf);
printf("Written %d\n", n); // Written 8
```
```
can0 001 [0] ''
can0 002 [0] ''
```
(The same behavior is for `msgalign == 0`, I tested it but the output is the
same so not posting it here.)
```c
int fd = open("/dev/can0", O_RDWR);
unsigned msgalign = sizeof(struct can_msg_s);
ioctl(fd, CANIOC_SET_MSGALIGN, &msgalign);
struct can_msg_s msgs[2];
msgs[0].cm_hdr = (struct can_hdr_s) {.ch_id = 1, .ch_dlc = 0};
msgs[1].cm_hdr = (struct can_hdr_s) {.ch_id = 2, .ch_dlc = 0};
size_t n = write(fd, msgs, sizeof msgs);
printf("Written %d\n", n); // Written 136
```
```
can0 001 [0] ''
can0 002 [0] ''
```
The write size is 136 because I have CAN-FD enabled, and thus `struct
can_msg_s` has space for 64 bytes of data. That makes it 68 times two.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]