-
Notifications
You must be signed in to change notification settings - Fork 7
/
common.h
138 lines (126 loc) · 3.72 KB
/
common.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* @file common.h
* Common header for Apteryx daemons.
*
* Copyright 2015, Allied Telesis Labs New Zealand, Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef _COMMON_H_
#define _COMMON_H_
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <inttypes.h>
#include <ctype.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <syslog.h>
#include <glib.h>
#include <lua.h>
/* Default UNIX socket path */
#define APTERYX_SERVER "unix:///tmp/apteryx"
/* Debug */
extern bool apteryx_debug;
static inline uint64_t
get_time_us (void)
{
struct timeval tv;
gettimeofday (&tv, NULL);
return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec);
}
static inline uint32_t htol32 (uint32_t v)
{
if (htons(1) == 1)
return ((v>>24)&0xff) | ((v>>8)&0xff00) | ((v<<8)&0xff0000) | ((v << 24)&0xff000000);
else
return v;
}
#define ltoh32 htol32
#define DEBUG(fmt, args...) \
if (apteryx_debug) \
{ \
syslog (LOG_DEBUG, fmt, ## args); \
printf ("[%"PRIu64":%d] ", get_time_us (), getpid ()); \
printf (fmt, ## args); \
}
#define NOTICE(fmt, args...) \
{ \
syslog (LOG_NOTICE, fmt, ## args); \
if (apteryx_debug) \
{ \
fprintf (stderr, "[%"PRIu64":%d] ", get_time_us (), getpid ()); \
fprintf (stderr, "NOTICE: "); \
fprintf (stderr, fmt, ## args); \
} \
}
#define ERROR(fmt, args...) \
{ \
syslog (LOG_ERR, fmt, ## args); \
if (apteryx_debug) \
{ \
fprintf (stderr, "[%"PRIu64":%d] ", get_time_us (), getpid ()); \
fprintf (stderr, "ERROR: "); \
fprintf (stderr, fmt, ## args); \
} \
}
#define ASSERT(assertion, rcode, fmt, args...) \
if (!(assertion)) \
{ \
syslog (LOG_ERR, fmt, ## args); \
if (apteryx_debug) \
{ \
fprintf (stderr, "[%"PRIu64":%d] ", get_time_us (), getpid ()); \
fprintf (stderr, "ASSERT: "); \
fprintf (stderr, fmt, ## args); \
} \
rcode; \
}
#define CRITICAL(fmt, args...) \
{ \
syslog (LOG_CRIT, fmt, ## args); \
fprintf (stderr, "[%"PRIu64":%d] ", get_time_us (), getpid ()); \
fprintf (stderr, "ERROR: "); \
fprintf (stderr, fmt, ## args); \
}
/* Callbacks */
#define CB_MATCH_PART (1<<0)
#define CB_MATCH_EXACT (1<<1)
#define CB_MATCH_WILD (1<<2)
#define CB_MATCH_CHILD (1<<3)
#define CB_MATCH_WILD_PATH (1<<4)
#define CB_PATH_MATCH_PART (1<<5)
typedef struct _cb_info_t
{
bool active;
lua_State *instance;
const char *guid;
const char *path;
const char *uri;
uint64_t id;
uint64_t cb;
GList **list;
int refcnt;
uint32_t count;
} cb_info_t;
void cb_init (void);
cb_info_t * cb_create (GList **list, lua_State *instance, const char *guid, const char *path, uint64_t id, uint64_t callback);
void cb_destroy (cb_info_t *cb);
void cb_release (cb_info_t *cb);
GList *cb_match (GList **list, const char *path, int critera);
#endif /* _COMMON_H_ */