Skip to content

Instantly share code, notes, and snippets.

@agagniere
Last active January 17, 2023 14:47
Show Gist options
  • Save agagniere/fe8a4580d29b33415eab77985c2657c1 to your computer and use it in GitHub Desktop.
Save agagniere/fe8a4580d29b33415eab77985c2657c1 to your computer and use it in GitHub Desktop.
Mock ptp
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <linux/ptp_clock.h>
struct external_timestamp
{
int64_t timestamp; // ns
int index;
};
#define EVAL(expr) #expr, expr
#define READ 0
#define WRITE 1
int main()
{
int fd[2];
printf("%-50s: %zu\n", EVAL(sizeof(struct external_timestamp)));
printf("%-50s: %zu\n", EVAL(sizeof(struct ptp_extts_event)));
printf("\n");
pipe(fd);
if (!fork())
{
struct external_timestamp A = { .timestamp = 'n' + ('s' << 24), .index = 'I'};
struct ptp_extts_event B = {
.t = {.sec = 's', .nsec = 'n'},
.index = 'I', .flags = 'F'};
close(fd[READ]);
write(fd[WRITE], &A, sizeof(A));
write(fd[WRITE], &B, sizeof(B));
exit(0);
}
else
{
close(fd[WRITE]);
uint8_t buffer[1024];
ssize_t count = read(fd[READ], buffer, sizeof(buffer));
print_memory(buffer, count);
}
return 0;
}
@agagniere
Copy link
Author

agagniere commented Jan 17, 2023

$ ./demo_ptp 
sizeof(struct external_timestamp)                 : 16
sizeof(struct ptp_extts_event)                    : 32

6e00 0073 0000 0000 4900 0000 0000 0000 n..s....I.......
7300 0000 0000 0000 6e00 0000 0000 0000 s.......n.......
4900 0000 4600 0000 0000 0000 0000 0000 I...F...........

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment