-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHAL.c
92 lines (74 loc) · 1.69 KB
/
HAL.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "HAL.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <sys/stat.h>
bool HAL_FileExist(const char *filename)
{
struct stat st;
return (stat(filename, &st) == 0);
}
int HAL_FileRead(const char *filename, char *buf, size_t size)
{
size_t len = 0;
FILE *fp = fopen(filename, "r");
if(fp != NULL)
{
len = fread(buf, sizeof(char), size, fp);
if(ferror(fp) != 0 || len != size)
{
fprintf(stderr, "Error reading file %s: %s\n",
filename, strerror(errno));
}
else
{
buf[len++] = '\0'; /* Just to be safe */
}
fclose(fp);
}
return (int)len;
}
int HAL_FileWrite(const char *filename, char *buf, size_t size)
{
size_t len = 0;
FILE *fp = fopen(filename, "w");
if(fp != NULL)
{
len = fwrite(buf, sizeof(char), size, fp);
if(ferror(fp) != 0 || len != size)
{
fprintf(stderr, "Error writing into file %s: %s\n",
filename, strerror(errno));
}
fclose(fp);
}
return (int)len;
}
bool HAL_TRNG(char *UN, size_t size)
{
FILE *fDriver = fopen("/dev/urandom", "r");
if(NULL != fDriver)
{
fread(UN, 1, size, fDriver);
fclose(fDriver);
}
else
{
fprintf(stderr, "Random Driver file error\n");
return false;
}
return true;
}
unsigned long HAL_Tick()
{
struct timespec now;
if(clock_gettime(CLOCK_MONOTONIC, &now))
{
return 0;
}
return (unsigned long)((now.tv_sec * 1000000) + (now.tv_nsec / 1000));
}