forked from DLTcollab/TA-endpoint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hardware abstraction layer for create unify device operators. Closes DLTcollab#31
- Loading branch information
Showing
7 changed files
with
217 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
DEVICES_OBJ = device.o | ||
INCLUDES += -I/$(CURDIR) | ||
export DEVICES_OBJ | ||
|
||
all: $(DEVICES_OBJ) | ||
|
||
device.o: device.c | ||
$(CC) $(CFLAGS) $(INCLUDES) -c $^ | ||
|
||
clean: | ||
find $(DEVICES_PATH) -name "*.o" -exec rm -f {} \; | ||
rm *.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include "device.h" | ||
|
||
static struct device_type *devices; | ||
|
||
static struct device_type **find_device(const char *name, unsigned len) { | ||
struct device_type **p; | ||
for (p = &devices; *p; p = &(*p)->next) | ||
if (strlen((*p)->name) == len && strncmp((*p)->name, name, len) == 0) break; | ||
return p; | ||
} | ||
|
||
device_t *device(const char *type) { | ||
struct device_type **p; | ||
if (devices->next) { | ||
fprintf(stderr, "No device type registered!"); | ||
return NULL; | ||
} | ||
p = find_device(type, strlen(type)); | ||
if (*p) { | ||
fprintf(stderr, "Device type %s not found", type); | ||
} | ||
return *p; | ||
} | ||
|
||
retcode_t register_device(struct device_type *dv) { | ||
retcode_t res = RET_OK; | ||
struct device_type **p; | ||
if (dv->next) { | ||
return -DEVICE_REG; | ||
} | ||
p = find_device(dv->name, strlen(dv->name)); | ||
if (*p) { | ||
res = -DEVICE_REG; | ||
} else { | ||
*p = dv; | ||
} | ||
return res; | ||
} | ||
|
||
retcode_t unregister_device(struct device_type *dv) { | ||
for (struct device_type **tmp = &devices; *tmp != NULL; tmp = &(*tmp)->next) { | ||
if (dv == *tmp) { | ||
*tmp = dv->next; | ||
dv->next = NULL; | ||
return RET_OK; | ||
} | ||
} | ||
return -DEVICE_UNREG; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright (C) 2019-2020 BiiLabs Co., Ltd. and Contributors | ||
* All Rights Reserved. | ||
* This is free software; you can redistribute it and/or modify it under the | ||
* terms of the MIT license. A copy of the license can be found in the file | ||
* "LICENSE" at the root of this distribution. | ||
*/ | ||
|
||
#ifndef HAL_DEVICE_H | ||
#define HAL_DEVICE_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "defined_error.h" | ||
|
||
/*! device initialization entry point */ | ||
#define DECLARE_DEVICE(name) \ | ||
void init_##name##_init(void) __attribute__((constructor)); \ | ||
void init_##name##_init(void) { name##_device_type.op->init(); } | ||
|
||
typedef struct device_type device_t; | ||
|
||
struct device_operations { | ||
retcode_t (*init)(void); /**< initialize for device */ | ||
void (*fini)(void); /**< destructor for device */ | ||
retcode_t (*get_key)(uint8_t *); /**< get device private key */ | ||
retcode_t (*get_device_id)(char *); /**< get device id */ | ||
}; | ||
|
||
struct uart_operations { | ||
retcode_t (*init)(const char *device); /**< init uart */ | ||
void (*write)(const int fd, const char *cmd); /**< write command to uart */ | ||
char *(*read)(const int fd); /**< read from uart */ | ||
void (*clean)(const int fd); /**< flush uart buffer */ | ||
}; | ||
|
||
struct secure_store_operations { | ||
retcode_t (*init)(void); /**< init secure storage */ | ||
/** | ||
* @brief Write an item to secure storage | ||
* | ||
* @param[in] name Name of item | ||
* @param[in] bufPtr Pointer to data | ||
* @param[in] bufSize Length of data | ||
* | ||
* @return | ||
* - #RET_OK on success | ||
* - #NO_MEMORY on no memory error | ||
* - #UNAVAILABLE on secure storage is currently unavailable | ||
* - #FAULT on some other error | ||
*/ | ||
retcode_t (*write)(const char *name, const uint8_t *bufPtr, size_t bufSize); | ||
/** | ||
* @brief Read an item from secure storage | ||
* | ||
* @param[in] name Name of item | ||
* @param[out] bufPtr Buffer to store the data in | ||
* @param[out] bufSizePtr Pointer to length of data | ||
* | ||
* @return | ||
* - #RET_OK on success | ||
* - #OVERFLOW error on the buffer is too small | ||
* - #NOT_FOUND error on the item not found inside secure storage | ||
* - #UNAVAILABLE error if the storage is currently unavailable | ||
* - #FAULT if there was some other error | ||
*/ | ||
retcode_t (*read)(const char *name, uint8_t *bufPtr, size_t *bufSizePtr); | ||
/** | ||
* @brief Delete item in secure storage | ||
* | ||
* @param[in] name Name of item | ||
* | ||
* @return | ||
* - #RET_OK on success | ||
* - #NOT_FOUND error on the item not found inside secure storage | ||
* - #UNAVAILABLE error if the storage is currently unavailable | ||
* - #FAULT if there was some other error | ||
*/ | ||
retcode_t (*delete)(const char *name); | ||
}; | ||
|
||
struct device_type { | ||
const char *name; /**< device type, a string */ | ||
int uart_fd; /**< uart file descriptor */ | ||
const struct device_operations *op; /**< device operations handler */ | ||
const struct uart_operations *uart; /**< uart operations handler */ | ||
const struct secure_store_operations *sec_ops; /**< secure storage operations handler */ | ||
device_t *next; /**< Pointer to next device type, don't use this directly */ | ||
}; | ||
|
||
/** | ||
* @brief Obtain specific device handler | ||
* | ||
* @param[in] device Device type | ||
* | ||
* @return | ||
* - device handler on success | ||
* - NULL on failed | ||
* @see #device_type | ||
*/ | ||
device_t *device(const char *device); | ||
|
||
/** | ||
* @brief Register device | ||
* | ||
* @param[in] dv Device to register | ||
* | ||
* @return | ||
* - #RET_OK on success | ||
* - #DEVICE_REG on failed | ||
*/ | ||
retcode_t register_device(struct device_type *dv); | ||
|
||
/** | ||
* @brief Unregister device | ||
* | ||
* @param[in] dv Device to unregister | ||
* | ||
* @return | ||
* - #RET_OK on success | ||
* - #DEVICE_UNREG on failed | ||
*/ | ||
retcode_t unregister_device(struct device_type *dv); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // HAL_DEVICE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters