-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
121 lines (96 loc) · 2.32 KB
/
util.h
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef __UTILS_H__
#define __UTILS_H__
/** @file Contains stuff formerly in xbh_utils.h */
#include <stdlib.h>
#include <stdbool.h>
#include <inttypes.h>
#include <unistd.h>
#include "hal/hal.h"
// For htonl/htons
#include <lwip/def.h>
/**
* Wrapper to printf to uart
* @param ... Same as printf
*/
#ifdef DEBUG
#define DEBUG_OUT(...) {uart_printf("%s: %d: ",__FILE__,__LINE__); uart_printf( __VA_ARGS__);}
#else
#define DEBUG_OUT(...)
#endif
/**
* Loop if error condition is true to preserve state for debugger, after
* outputting message
* @param x true if error condition
* @param ... Message to printf
*/
#define LOOP_ERRMSG(x,...) if(x){uart_printf(__VA_ARGS__); while(1);}
/**
* Outputs message if error condition happens
* @param x true if error condition
* @param ... Message to printf
*/
#define COND_ERRMSG(x,...) if(x){uart_printf(__VA_ARGS__);}
#ifdef DEBUG
/**
* Outputs message of error condition happens; only enabled if debug is on
* @param x true if error condition
* @param ... Message to printf
*/
#define ASSERT_MSG(x,...) if(x){DEBUG_OUT(__VA_ARGS);}
#else
#define ASSERT_MSG(x,...)
#endif
/*
* http://stackoverflow.com/questions/9907160/how-to-convert-enum-names-to-string-in-c
*/
#define GEN_ENUM(ENUM) ENUM,
#define GEN_STR(STRING) #STRING,
/*
* #define FOREACH_FRUIT(FRUIT) \
* FRUIT(apple) \
* FRUIT(orange) \
* FRUIT(grape) \
* FRUIT(banana)
*/
/*
* enum FRUIT_ENUM {
* FOREACH_FRUIT(GEN_ENUM)
* };
*/
/*
* static const char *FRUIT_STRING[] = {
* FOREACH_FRUIT(GEN_STR)
* };
*/
// Byte swap macros
#define htonll(x) __builtin_bswap64(x)
#define ntohll(x) __builtin_bswap64(x)
//char *ltoa(long val, char *str, int base, bool lowercase);
void uart_printf(const char *buffer,...) ;
ssize_t recv_waitall(int sock, void *data, size_t len, int flags);
ssize_t sendall(int sock, void *data, size_t len, int flags);
void len2hex(size_t len, uint8_t *buf);
size_t hex2len(uint8_t *buf);
/**
* Converts hex digit in ascii to numerical value
* From XBH
*/
inline uint8_t htoi(char h) {
if(h >= '0' && h <= '9')
return (h-'0');
if(h>= 'a' && h <= 'f')
return (h-'a'+10);
if(h>= 'A' && h <= 'F')
return (h-'A'+10);
return 255;
}
/**
* Converts nibble to hex
*/
inline char ntoa(uint8_t n){
if(n<=9)
return n+'0';
else
return n+'A'-10;
}
#endif