Well, I suppose we all have mommies and daddies
This commit is contained in:
		
							parent
							
								
									02bda087b9
								
							
						
					
					
						commit
						e0da9b0cfb
					
				
					 3 changed files with 127 additions and 2 deletions
				
			
		
							
								
								
									
										40
									
								
								include/hexagram/can.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								include/hexagram/can.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,40 @@
 | 
			
		|||
#ifndef _HEXAGRAM_CAN_H
 | 
			
		||||
#define _HEXAGRAM_CAN_H
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <linux/can.h>
 | 
			
		||||
 | 
			
		||||
#define HEXAGRAM_CAN_DUMP_MAGIC  "CAAN"
 | 
			
		||||
#define HEXAGRAM_CAN_DUMP_ENDIAN 0x0a0b0c0d
 | 
			
		||||
#define HEXAGRAM_CAN_DUMP_ENDIAN_SWAPPED 0x0d0c0b0a
 | 
			
		||||
 | 
			
		||||
#define HEXAGRAM_CAN_DUMP_TYPE_UNKNOWN    0
 | 
			
		||||
#define HEXAGRAM_CAN_DUMP_TYPE_SOCKETCAN 29
 | 
			
		||||
 | 
			
		||||
typedef struct _hexagram_can_dump {
 | 
			
		||||
    char magic[4];
 | 
			
		||||
    uint32_t endian;
 | 
			
		||||
    uint8_t type,
 | 
			
		||||
            tsresol;
 | 
			
		||||
    char iface[38];
 | 
			
		||||
} hexagram_can_dump;
 | 
			
		||||
 | 
			
		||||
typedef struct _hexagram_can_frame {
 | 
			
		||||
    uint32_t timestamp_hi,
 | 
			
		||||
             timestamp_lo;
 | 
			
		||||
    struct can_frame frame;
 | 
			
		||||
} hexagram_can_frame;
 | 
			
		||||
 | 
			
		||||
typedef struct _hexagram_can_stream hexagram_can_stream;
 | 
			
		||||
 | 
			
		||||
hexagram_can_stream *hexagram_can_stream_open_fd(int fd);
 | 
			
		||||
 | 
			
		||||
void hexagram_can_stream_destroy(hexagram_can_stream *stream);
 | 
			
		||||
 | 
			
		||||
int hexagram_can_stream_read(hexagram_can_stream *stream,
 | 
			
		||||
                             uint32_t *timestamp_hi,
 | 
			
		||||
                             uint32_t *timestamp_lo,
 | 
			
		||||
                             struct can_frame *frame);
 | 
			
		||||
 | 
			
		||||
#endif /* _HEXAGRAM_CAN_H */
 | 
			
		||||
| 
						 | 
				
			
			@ -7,10 +7,10 @@ CC		= $(CROSS)cc
 | 
			
		|||
CFLAGS		+= -fPIC -I$(INCLUDE_PATH)
 | 
			
		||||
LDFLAGS		=
 | 
			
		||||
 | 
			
		||||
HEADERS		= pcapng.h
 | 
			
		||||
HEADERS		= can.h pcapng.h
 | 
			
		||||
HEADERS_LOCAL	= util.h
 | 
			
		||||
 | 
			
		||||
OBJS		= pcapng.o
 | 
			
		||||
OBJS		= can.o pcapng.o
 | 
			
		||||
 | 
			
		||||
VERSION_MAJOR	= 0
 | 
			
		||||
VERSION_MINOR	= 0.1
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										85
									
								
								src/can.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								src/can.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,85 @@
 | 
			
		|||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
#include <hexagram/can.h>
 | 
			
		||||
 | 
			
		||||
struct _hexagram_can_stream {
 | 
			
		||||
    int fd;
 | 
			
		||||
    uint32_t endian;
 | 
			
		||||
    uint8_t tsresol;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
hexagram_can_stream *hexagram_can_stream_open_fd(int fd) {
 | 
			
		||||
    hexagram_can_stream *stream;
 | 
			
		||||
    hexagram_can_dump dump;
 | 
			
		||||
    ssize_t readlen;
 | 
			
		||||
 | 
			
		||||
    if ((stream = malloc(sizeof(*stream))) == NULL) {
 | 
			
		||||
        goto error_malloc;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ((readlen = read(fd, &dump, sizeof(dump))) < 0) {
 | 
			
		||||
        goto error_read;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (memcmp(dump.magic, HEXAGRAM_CAN_DUMP_MAGIC, sizeof(dump.magic)) != 0) {
 | 
			
		||||
        goto error_invalid_format;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    switch (dump.endian) {
 | 
			
		||||
        case HEXAGRAM_CAN_DUMP_ENDIAN:
 | 
			
		||||
        case HEXAGRAM_CAN_DUMP_ENDIAN_SWAPPED:
 | 
			
		||||
            break;
 | 
			
		||||
 | 
			
		||||
        default:
 | 
			
		||||
            goto error_invalid_format;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (dump.type != HEXAGRAM_CAN_DUMP_TYPE_SOCKETCAN) {
 | 
			
		||||
        goto error_invalid_format;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    stream->fd      = fd;
 | 
			
		||||
    stream->endian  = dump.endian;
 | 
			
		||||
    stream->tsresol = dump.tsresol;
 | 
			
		||||
 | 
			
		||||
    return stream;
 | 
			
		||||
 | 
			
		||||
error_invalid_format:
 | 
			
		||||
error_read:
 | 
			
		||||
    free(stream);
 | 
			
		||||
 | 
			
		||||
error_malloc:
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void hexagram_can_stream_destroy(hexagram_can_stream *stream) {
 | 
			
		||||
    memset(stream, '\0', sizeof(*stream));
 | 
			
		||||
 | 
			
		||||
    free(stream);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int hexagram_can_stream_read(hexagram_can_stream *stream,
 | 
			
		||||
                             uint32_t *timestamp_hi,
 | 
			
		||||
                             uint32_t *timestamp_lo,
 | 
			
		||||
                             struct can_frame *frame) {
 | 
			
		||||
    ssize_t len;
 | 
			
		||||
    hexagram_can_frame data;
 | 
			
		||||
 | 
			
		||||
    if ((len = read(stream->fd, &data, sizeof(data))) < 0) {
 | 
			
		||||
        goto error_io;
 | 
			
		||||
    } else if (len < sizeof(data)) {
 | 
			
		||||
        goto error_io;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    *timestamp_hi = data.timestamp_hi;
 | 
			
		||||
    *timestamp_lo = data.timestamp_lo;
 | 
			
		||||
 | 
			
		||||
    memcpy(frame, &data.frame, sizeof(data.frame));
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
error_io:
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		
		Reference in a new issue