summaryrefslogtreecommitdiff
path: root/digital/avr/modules/host/mex.h
blob: 82e651039f70a7c8bfe0b5eaac4cc7553993628a (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
#ifndef mex_h
#define mex_h
/* mex.h - mex library support. */
/* avr.host - Host fonctions modules. {{{
 *
 * Copyright (C) 2008 Nicolas Schodet
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */

/** This module provides mex Node support.  In each function, any error will
 * stop the program (using assert). */

/** Define message type identifiers. */
enum mex_mtype_t
{
    MEX_MTYPE_IDLE = 0,
    MEX_MTYPE_DATE = 1,
    MEX_MTYPE_REQ = 2,
    MEX_MTYPE_RSP = 3,
    MEX_MTYPE_RES = 4,
};

/** Message structure opaque definition. */
typedef struct mex_msg_t mex_msg_t;

/** Message handler. */
typedef void (mex_handler_t) (void *user, mex_msg_t *msg);

/** Create a new message with the given type. */
mex_msg_t *
mex_msg_new (u8 mtype);

/** Release a message. */
void
mex_msg_delete (mex_msg_t *msg);

/** Add data to the message payload.
 *
 * The fmt string describes the provided data which must follow:
 *  - b: 8 bits.
 *  - h: 16 bits.
 *  - l: 32 bits.
 *
 * Uppercase is used for unsigned (but who cares?). */
void
mex_msg_push (mex_msg_t *msg, const char *fmt, ...);

/** Add data to the message payload, using a buffer. */
void
mex_msg_push_buffer (mex_msg_t *msg, const u8 *buffer, int size);

/** Get data from the message payload.
 *
 * The fmt string follows the same syntax as mex_msg_push, but pointers are
 * provided as extra arguments. */
void
mex_msg_pop (mex_msg_t *msg, const char *fmt, ...);

/** Get remaining payload from the message. */
const u8 *
mex_msg_pop_buffer (mex_msg_t *msg);

/** Get payload remaining length. */
int
mex_msg_len (mex_msg_t *msg);

/** Get message type. */
u8
mex_msg_mtype (mex_msg_t *msg);

/** Connect to the mex Hub. */
void
mex_node_connect (void);

/** Close connection. */
void
mex_node_close (void);

/** Wait forever. */
void
mex_node_wait (void);

/** Wait until a date is reached. */
void
mex_node_wait_date (u32 date);

/** Return current date. */
u32
mex_node_date (void);

/** Send a message, msg is released. */
void
mex_node_send (mex_msg_t *msg);

/** Send a request and return response, msg is released. */
mex_msg_t *
mex_node_request (mex_msg_t *msg);

/** Send a response to the currently serviced request, msg is released. */
void
mex_node_response (mex_msg_t *msg);

/** Register a handler for the given message type. */
void
mex_node_register (u8 mtype, mex_handler_t *handler, void *user);

/** Request a message type reservation. */
u8
mex_node_reserve (const char *mtype_str);

/** Request a message type reservation, using formated string. */
u8
mex_node_reservef (const char *mtype_fmt, ...)
    __attribute__ ((format (printf, 1, 2)));

#endif /* mex_h */