forked from adlerweb/asysbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasb.h
311 lines (272 loc) · 10.9 KB
/
asb.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
aSysBus bus interface
@copyright 2015-2017 Florian Knodt, www.adlerweb.info
Based on iSysBus - 2010 Patrick Amrhein, www.isysbus.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 3 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef ASB__H
#define ASB__H
#include "asb_comm.h"
#include "asb_proto.h"
#include "asb_hook.h"
#include "asb_comm.h"
#include "asb_can.h"
#include "asb_uart.h"
#include "asb_io.h"
#include "asb_io_din.h"
#include "asb_io_dout.h"
/**
* Maximum number of parallel communication interfaces
*
* ASB_BUSNUM sets the maximum number of active communication interfaces
* in one instance. You can set it to a integer between 1 and 120. Keep
* in mind higher numbers will increase RAM and CPU usage. Default is 6.
*/
#ifndef ASB_BUSNUM
#define ASB_BUSNUM 6 //<120!
#endif
/**
* Activate debugging on Serial
*
*/
#ifndef ASB_DEBUG
//#define ASB_DEBUG
#endif
/**
* Maximum number of active hooks
*
* ASB_HOOKNUM sets the maximum number of active hooks in one instance.
* You can set it to a integer between 1 and 120. Keep
* in mind higher numbers will increase RAM and CPU usage. Default is 16.
*/
#ifndef ASB_HOOKNUM
#define ASB_HOOKNUM 16 //<120!
#endif
/**
* Maximum number of active modules
*
* ASB_MODNUM sets the maximum number of active modules in one instance.
* You can set it to a integer between 1 and 120. Keep in mind higher
* numbers will increase RAM and CPU usage. Default is 16.
*/
#ifndef ASB_MODNUM
#define ASB_MODNUM 16 //<120!
#endif
/**
* ASB main controller class
*
* This class stores all node parameters, handles packet routing between
* different communication interfaces, implements basic functions like
* sending boot messages, answering to PING and allows for attaching your
* own functions to various messages
*/
class ASB {
private:
/**
* Array of pointers to active communication interfaces
*/
ASB_COMM *_busAddr[ASB_BUSNUM];
/**
* Array of structs containing hooks
*/
asbHook _hooks[ASB_HOOKNUM];
/**
* Array of pointers to active modules
*/
ASB_IO *_module[ASB_MODNUM];
/**
* Bus address of this node
*/
unsigned int _nodeId=0;
/**
* First EEPROM address to use
*/
unsigned int _cfgAddrStart=0;
/**
* Last EEPROM address to use
*/
unsigned int _cfgAddrStop=511;
public:
/**
* Constructor reads configuration
* @param EEPROM start address, usually 0
* @param EEPROM stop address, usually 511
*/
ASB(unsigned int start, unsigned int stop);
/**
* "Light" Constructor without EEPROM-read
* @param Node-ID between 0x0001 and 0x07FF
*/
ASB(unsigned int id);
/**
* Initialize node if no configuration found in EEPROM
* @param function to execute
* @return true if initialized
*/
bool firstboot(void (*function)());
/**
* Change Node-ID
* @param id Node-ID between 0x0001 and 0x07FF
*/
bool setNodeId(unsigned int id);
/**
* Attach a bus-object to this controller
* @param bus Bus object, derived from ASB_COMM
* @return bus-ID, -1 on errors
* @see busDetach()
*/
char busAttach(ASB_COMM* bus);
/**
* Detach a bus-object from this controller
* @param busId ID of the bus-object as given by busAttach
* @return true if successful
* @see busAttach()
*/
bool busDetach(signed char busId);
/**
* First EEPROM address to use
*/
unsigned int cfsAddrStart=0;
/**
* Last EEPROM address to use
*/
unsigned int cfsAddrStop=511;
/**
* Find next free config block with >=size bytes
* @param lenth of requested block in bytes
* @param id configuration ID of requesting module
* @return unsigned int address, 0 for error
*/
unsigned int cfgFindFreeblock(byte bytes, byte id);
/**
* Send a message to the bus
* @param meta asbMeta object containing message metadata
* @param len number of data bytes to send
* @param data array of data bytes to send
* @return number of errors
*/
byte asbSend(asbMeta meta, byte len, byte *data);
/**
* Send a message to the bus
* @param type 2 bit message type (ASB_PKGTYPE_*)
* @param target target address between 0x0001 and 0x07FF/0xFFFF
* @param len number of data bytes to send
* @param data array of data bytes to send
* @return number of errors
*/
byte asbSend(byte type, unsigned int target, byte len, byte *data);
/**
* Send a message to the bus
* @param type 2 bit message type (ASB_PKGTYPE_*)
* @param target target address between 0x0001 and 0x07FF/0xFFFF
* @param port port address between 0x00 and 0x1F, Unicast only
* @param len number of data bytes to send
* @param data array of data bytes to send
* @return number of errors
*/
byte asbSend(byte type, unsigned int target, char port, byte len, byte *data);
/**
* Send a message to the bus
* @param type 2 bit message type (ASB_PKGTYPE_*)
* @param target target address between 0x0001 and 0x07FF/0xFFFF
* @param source source address between 0x0001 and 0x07FF, 0=self
* @param port port address between 0x00 and 0x1F, Unicast only
* @param len number of data bytes to send
* @param data array of data bytes to send
* @param skip busID as per busAttach to skip when sending
* @return number of errors
* @see busAttach()
*/
byte asbSend(byte type, unsigned int target, unsigned int source, char port, byte len, byte *data, signed char skip);
/**
* Receive a message from the bus
*
* This polls all attached communication interfaces for new messages
* The first received message will be passed to &pkg, if no messages
* are available the function will return false. If a message is
* received you are adviced to call the function again until no
* more messages are availabe on any interface. All received packets
* will be redistributed to all other attached interfaces
*
* @param pkg asbPacket-Reference to store received packet
* @return true if a message was received
*/
bool asbReceive(asbPacket &pkg);
/**
* Receive a message from the bus
*
* Same as above but doesn't return anything. Useful if you just use hooks
*
* @see asbReceive(asbPacket &pkg)
*/
void asbReceive(void);
/**
* Receive a message from the bus
*
* This polls all attached communication interfaces for new messages
* The first received message will be passed to &pkg, if no messages
* are available the function will return false. If a message is
* received you are adviced to call the function again until no
* more messages are availabe on any interface. All received packets
* will be redistributed to all other attached interfaces if routing
* is true
*
* @param pkg asbPacket-Reference to store received packet
* @param routing If false packet will not be redistributed
* @return true if a message was received
*/
bool asbReceive(asbPacket &pkg, bool routing);
/**
* Process incoming packet
* @param pkg Packet struct
*/
void asbProcess(asbPacket &pkg);
/**
* Attach a hook to a set of metadata
*
* If a received packet matches the type, target and port the supplied function
* will be called. This allows for custom actors. Note: There is no detach (yet?).
*
* @param type 2 bit message type (ASB_PKGTYPE_*), 0xFF = everything
* @param target target address between 0x0001 and 0x07FF/0xFFFF, 0x0 = everything
* @param port port address between 0x00 and 0x1F, Unicast only, -1 = everything
* @param First data byte (usually ASB_CMD_*), 0xFF = everything
* @param function to call when matched
* @return true if successfully added
*/
bool hookAttach(byte type, unsigned int target, char port, byte firstByte, void (*function)(asbPacket&));
/**
* Attach a module to this controller
*
* Attached modules will get initialized and later called for loop and received Packets
*
* @param ASB_IO based module
* @return true if successfully added
*/
bool hookAttachModule(ASB_IO *module);
/**
* Detach a module from this controller
*
* @param ASB_IO based module-ID
* @return true if successfully added
*/
bool hookDetachModule(byte id);
/**
* Main processing loop
*
* Receives and routes packets, checks inputs, etc
*
* @return asbPacket last received packet
*/
asbPacket loop(void);
};
#endif /* ASB__H */