summaryrefslogtreecommitdiffstats
path: root/include/dect/libdect.h
blob: 3b79e999b5b24c1dff53e1206be5b5dbd010ab4f (plain)
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
/*
 * libdect main header file
 *
 * Copyright (c) 2009 Patrick McHardy <kaber@trash.net>
 */

#ifndef _LIBDECT_DECT_LIBDECT_H
#define _LIBDECT_DECT_LIBDECT_H

#include <assert.h>
#include <stdbool.h>
#include <stdint.h>

#include <dect/identities.h>
#include <dect/cc.h>
#include <dect/mm.h>

struct dect_handle;

/**
 * struct dect_msg_buf - DECT message buffer
 *
 * @list:	Data link TX queue node
 * @type:	Message type
 * @len:	Data length
 * @data:	Data pointer
 * @head:	Storage area for on-stack buffers
 */
struct dect_msg_buf {
	struct list_head	list;
	uint8_t			type;
	uint8_t			len;
	uint8_t			*data;
	uint8_t			head[128];
};

extern struct dect_msg_buf *dect_mbuf_alloc(const struct dect_handle *dh);

static inline void dect_mbuf_pull(struct dect_msg_buf *mb, unsigned int len)
{
	assert(len <= mb->len);
	mb->data += len;
	mb->len -= len;
}

static inline void dect_mbuf_push(struct dect_msg_buf *mb, unsigned int len)
{
	mb->data -= len;
	mb->len += len;
	assert(mb->data >= mb->head);
}

static inline void dect_mbuf_reserve(struct dect_msg_buf *mb, unsigned int len)
{
	mb->data += len;
	assert(mb->data < mb->head + sizeof(mb->head));
}

/**
 * enum dect_fd_events - file descriptor events
 *
 * @DECT_FD_READ:	fd readable
 * @DECT_FD_WRITE:	fd writable
 */
enum dect_fd_events {
	DECT_FD_READ	= 0x1,
	DECT_FD_WRITE	= 0x2
};

/**
 * struct dect_fd - libdect file descriptor
 *
 * @callback:		callback to invoke for events
 * @fd:			file descriptor numer
 * @data:		libdect internal data
 * @priv:		libdect user private file-descriptor storage
 */
struct dect_fd {
	void		(*callback)(struct dect_handle *,
				    struct dect_fd *, uint32_t);
	int		fd;
	void		*data;
	uint8_t		priv[];
};

/**
 * struct dect_timer - libdect timer
 *
 * @callback:		callback to invoke on timer expiry
 * @data:		libdect internal data
 * @priv:		libdect user private timer storage
 */
struct dect_timer {
	void		(*callback)(struct dect_handle *,
				    struct dect_timer *);
	void		*data;
	uint8_t		priv[];
};

struct timeval;
struct dect_event_ops {
	size_t		fd_priv_size;
	size_t		timer_priv_size;

	int		(*register_fd)(const struct dect_handle *dh,
				       struct dect_fd *dfd,
				       uint32_t events);
	void		(*unregister_fd)(const struct dect_handle *dh,
					 struct dect_fd *dfd);

	void		(*start_timer)(const struct dect_handle *dh,
				       struct dect_timer *timer,
				       const struct timeval *tv);
	void		(*stop_timer)(const struct dect_handle *dh,
				      struct dect_timer *timer);
};

struct dect_ops {
	void				*(*malloc)(size_t size);
	void				(*free)(void *ptr);

	const struct dect_event_ops	*event_ops;
	const struct dect_cc_ops	*cc_ops;
	const struct dect_mm_ops	*mm_ops;
};

extern struct dect_handle *dect_alloc_handle(struct dect_ops *ops);
extern void dect_close_handle(struct dect_handle *dh);

extern int dect_init(struct dect_handle *dh);

extern void dect_set_debug_hook(void (*fn)(const char *fmt, va_list ap));

#endif /* _LIBDECT_DECT_LIBDECT_H */