From 8cae731729c152b7fa4943b8086acb2130436e2d Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Wed, 9 Jun 2021 07:41:08 +0200 Subject: [PATCH] Default driver --- inc/board.h | 12 +++++ inc/driver.h | 28 +++++++++++ inc/test_drv.h | 20 ++++++++ src/driver.c | 32 +++++++++++++ src/main.c | 124 ++----------------------------------------------- src/test_drv.c | 27 +++++++++++ 6 files changed, 124 insertions(+), 119 deletions(-) create mode 100644 inc/board.h create mode 100644 inc/driver.h create mode 100644 inc/test_drv.h create mode 100644 src/driver.c create mode 100644 src/test_drv.c diff --git a/inc/board.h b/inc/board.h new file mode 100644 index 0000000..0526e4b --- /dev/null +++ b/inc/board.h @@ -0,0 +1,12 @@ +#ifndef __BOARD_H__ +#define __BOARD_H__ + +#include "driver.h" +#include "test_drv.h" + +static const struct driver tst_drv = { + .fp = &tst_fp, + .dev = NULL +}; + +#endif diff --git a/inc/driver.h b/inc/driver.h new file mode 100644 index 0000000..75752d4 --- /dev/null +++ b/inc/driver.h @@ -0,0 +1,28 @@ +#ifndef __DRIVER_H__ +#define __DRIVER_H__ + +struct driver; + +typedef int (*fp_open_t)(const struct driver *); +typedef int (*fp_close_t)(const struct driver *); +typedef int (*fp_read_t)(const struct driver *, char *, unsigned int); +typedef int (*fp_write_t)(const struct driver *, const char *, unsigned int); + +struct driver_fp { + fp_open_t open; + fp_close_t close; + fp_read_t read; + fp_write_t write; +}; + +struct driver { + const struct driver_fp *fp; + const void *dev; +}; + +int drv_open(const struct driver *drv); +int drv_close(const struct driver *drv); +int drv_read(const struct driver *drv, char *buffer, unsigned int length); +int drv_write(const struct driver *drv, const char *buffer, unsigned int length); + +#endif diff --git a/inc/test_drv.h b/inc/test_drv.h new file mode 100644 index 0000000..73fd320 --- /dev/null +++ b/inc/test_drv.h @@ -0,0 +1,20 @@ +#ifndef __TEST_DRV_H__ +#define __TEST_DRV_H__ + +#include + +#include "driver.h" + +int tst_open(const struct driver *drv); +int tst_close(const struct driver *drv); +int tst_read(const struct driver *drv, char *buffer, unsigned int length); +int tst_write(const struct driver *drv, const char *buffer, unsigned int length); + +static const struct driver_fp tst_fp = { + .open = tst_open, + .close = tst_close, + .read = tst_read, + .write = tst_write +}; + +#endif diff --git a/src/driver.c b/src/driver.c new file mode 100644 index 0000000..000a7c4 --- /dev/null +++ b/src/driver.c @@ -0,0 +1,32 @@ +#include +#include + +#include "driver.h" + +int drv_open(const struct driver *drv) +{ + assert(drv != NULL); + + return drv->fp->open(drv); +} + +int drv_close(const struct driver *drv) +{ + assert(drv != NULL); + + return drv->fp->close(drv); +} + +int drv_read(const struct driver *drv, char *buffer, unsigned int length) +{ + assert(drv != NULL); + + return drv->fp->read(drv, buffer, length); +} + +int drv_write(const struct driver *drv, const char *buffer, unsigned int length) +{ + assert(drv != NULL); + + return drv->fp->write(drv, buffer, length); +} diff --git a/src/main.c b/src/main.c index b84debf..573744d 100644 --- a/src/main.c +++ b/src/main.c @@ -8,129 +8,15 @@ #include #include -#define ASCII_TO_NUMBER(num) ((num) - 48) //Converts an ascii digit to the corresponding number - -#define TAR_FILE "test.tar" -#define BLOCK_SIZE 512 -#define TAR_FILE_HEADER 512 -struct tar_file_header { - char filename[100]; - char mode[8]; - char uid[8]; - char gid[8]; - char file_size[12]; - char last_modification[12]; - char checksum[8]; - char type_flag; - char linked_file_name[100]; - char ustar_indicator[6]; - char ustar_version[2]; - char owner_user_name[32]; - char owner_group_name[32]; - char device_major_number[8]; - char device_minor_number[8]; - char filename_prefix[155]; - char padding[12]; -}; - -/** - * Decode a TAR octal number. - * Ignores everything after the first NUL or space character. - * @param data A pointer to a size-byte-long octal-encoded - * @param size The size of the field pointer to by the data pointer - * @return - */ -static uint64_t decode_tar_octal(char* data, size_t size) -{ - unsigned char *current_ptr = (unsigned char *) data + size; - uint64_t sum = 0; - uint64_t current_multiplier = 1; - - // Skip everything after the last NUL/space character - // In some TAR archives the size field has non-trailing NULs/spaces, so - // thisis neccessary. - unsigned char* check_ptr = current_ptr; //This is used to check where the last NUL/space char is - for(; check_ptr >= (unsigned char *) data; --check_ptr) { - if((*check_ptr) == 0 || (*check_ptr) == ' ') { - current_ptr = check_ptr - 1; - } - } - for(; current_ptr >= (unsigned char *) data; --current_ptr) { - sum += ASCII_TO_NUMBER(*current_ptr) * current_multiplier; - current_multiplier *= 8; - } - return sum; -} - -bool check_checksum(struct tar_file_header *tar_header) -{ - assert(tar_header != NULL); - - char original_checksum[8]; - memcpy(original_checksum, tar_header->checksum, 8); - memset(tar_header->checksum, ' ', 8); - - int64_t unsigned_sum = 0; - int64_t signed_sum = 0; - unsigned char *uc_tar = (unsigned char *)tar_header; - signed char *sc_tar = (signed char *)tar_header; - for(int i = 0; i < TAR_FILE_HEADER; i++) { - unsigned_sum += uc_tar[i]; - signed_sum += sc_tar[i]; - } - //Copy back the checksum - memcpy(tar_header->checksum, original_checksum, 8); - //Decode the original checksum - uint64_t reference_checksum = decode_tar_octal(original_checksum, sizeof(original_checksum)); - return (reference_checksum == unsigned_sum || reference_checksum == signed_sum); -} int main(void) { - int fd; + printf("Hello\r\r"); + unsigned long stamp = 55; - fd = open(TAR_FILE, O_RDONLY); - if(fd < 0) { - fprintf(stderr, "Unable to open %s\r\n", TAR_FILE); - return fd; - } + char buffer[80]; - int res; - char block[BLOCK_SIZE]; - uint64_t read_count = 0, next_tar_header = 0, file_size, tmp, loop_count = 1; - do { - res = read(fd, block, sizeof(block)); - if(next_tar_header == read_count) { - struct tar_file_header tar_header; - if(res != sizeof(tar_header)) { - fprintf(stderr, "tar header size wrong\r\n"); - close(fd); - return -1; - } - memcpy(&tar_header, block, sizeof(tar_header)); - if(!check_checksum(&tar_header)) { - fprintf(stderr, "checksum missmatch\r\n"); - close(fd); - return -1; - } - file_size = decode_tar_octal(tar_header.file_size, sizeof(tar_header.file_size)); - printf("%s - size: %lu\r\n", tar_header.filename, file_size); - next_tar_header = (file_size / BLOCK_SIZE + 1) * BLOCK_SIZE + loop_count * BLOCK_SIZE; - } - else { - for(int i = 0; i < BLOCK_SIZE; i++) { - if(block[i] == '\0') { - break; - } - printf("%c", block[i]); - } - } - read_count += res; - loop_count++; - tmp = next_tar_header + ((file_size / BLOCK_SIZE + 1) * BLOCK_SIZE); - } while(tmp >= read_count); + snprintf(buffer, sizeof(buffer), "%lu",stamp); - close(fd); - - return 0; + printf(buffer); } diff --git a/src/test_drv.c b/src/test_drv.c new file mode 100644 index 0000000..cbcb332 --- /dev/null +++ b/src/test_drv.c @@ -0,0 +1,27 @@ +#include + +#include "test_drv.h" + +int tst_open(const struct driver *drv) +{ + printf("%s called\n", __func__); + return 0; +} + +int tst_close(const struct driver *drv) +{ + printf("%s called\n", __func__); + return 0; +} + +int tst_read(const struct driver *drv, char *buffer, unsigned int length) +{ + printf("%s called\n", __func__); + return 0; +} + +int tst_write(const struct driver *drv, const char *buffer, unsigned int length) +{ + printf("%s called\n", __func__); + return 0; +}