SFBNet.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00027 #ifndef SFBNET_H_
00028 #define SFBNET_H_
00029
00030 #include <stdarg.h>
00031
00032 #include "SFBConstants.h"
00033 #include "SFBAlarm.h"
00034 #include "SFBPrint.h"
00035
00044 class SFBNet {
00045 public:
00046
00055 bool shouldTakeFirstTie() { return flags&TAKE_FIRST_TIE; }
00056
00064 void setShouldTakeFirstTie(bool on) {
00065 if (on)
00066 flags |= TAKE_FIRST_TIE;
00067 else
00068 flags &= ~TAKE_FIRST_TIE;
00069 }
00070
00075 void begin() ;
00076
00080 bool isActive() { return (flags&NET_ACTIVE)!=0; }
00081
00085 void end() ;
00086
00096 void offer(u8 service) ;
00097
00105 bool withhold(u8 service) ;
00106
00116 bool isOfferedByNode (u8 service) ;
00117
00128 bool isOfferedByNet(u8 service) ;
00129
00136 void openServiceStream(u8 service) ;
00137
00142 void printf ( const char * fmt, ...) ;
00143
00148 void vprintf(const char * fmt, va_list & ap) ;
00149
00153 void printf(u8 service, const char * fmt, ...) ;
00154
00162 void setBroadcastPeriod(u32 millis) ;
00163
00171 void setHorizon(u32 distance) ;
00172
00179 u32 getHorizon() { return defaultTTL; }
00180
00184 u32 getServiceStreamFace() {
00185 return net_printer_face;
00186 }
00187
00191 bool neighborActive(u8 face) ;
00192
00198 enum Sizes {
00199 MAX_TOTAL_SVCS=64,
00200 MAX_LOCAL_SVCS=32,
00201 MAX_RETURN_ROUTE_LENGTH=100
00202 };
00203
00204 void printPath(u8 toFace, bool skipFirst = true);
00205
00209 void dump(u8 face, const u8 * prefix = 0) ;
00210
00211 private:
00212
00220 enum Flags {
00221 TAKE_FIRST_TIE= 0x01,
00222 RESPONSE_SENT_HEADER=0x02,
00223 NET_ACTIVE= 0x04,
00224 IN_TRIGGER= 0x08,
00225 FLAG_RSRV4= 0x10,
00226 FLAG_RSRV5= 0x20,
00227 FLAG_RSRV6= 0x40,
00228 FLAG_RSRV7= 0x80
00229 };
00230
00234 u8 ticks() { return MsToTicks(millis()); }
00235
00236 u32 ticksToMs(u8 ticks) { return ticks<<8; }
00237
00238 u32 MsToTicks32(u32 ms) { return ms>>8; }
00239
00240 u8 MsToTicks(u32 ms) { return (u8) MsToTicks32(ms); }
00241
00242
00246 bool neighborActive_(u8 face) {
00247 u8 now = ticks();
00248 bool ret = IS_LATER8(neighborActiveTicks[face]+MsToTicks(2048),now);
00249 if (!ret) neighborActiveTicks[face] = now-10;
00250 return ret;
00251 }
00252
00253 static const FacePrinter net_printer;
00254 static const FacePrinter response_printer;
00255
00256 void functionPrint(u8 face, u8 byte) ;
00257 void functionPrintln(u8 face) ;
00258
00259 void printResponseHeader() ;
00260 void printResponseHeaderIfNeeded() { if ((flags&RESPONSE_SENT_HEADER)==0) printResponseHeader(); }
00261 void responsePrint(u8 face, u8 byte) ;
00262 void responsePrintln (u8 face) ;
00263
00264 static void NetBcastAlarm(u32) ;
00265 static void NetPacketHandler(u8 *) ;
00266
00267 static void NetFunctionPrint(u8 face, u8 byte ) ;
00268 static void NetFunctionPrintln( u8 face) ;
00269 static void NetResponsePrint(u8 face, u8 byte) ;
00270 static void NetResponsePrintln(u8 face) ;
00271
00275 void broadcastAlarm(u32) ;
00276
00280 void packetHandler(u8 *) ;
00281
00282 struct Entry {
00283 u8 func;
00284 u8 last_update[FACE_COUNT];
00285 u8 hops[FACE_COUNT];
00286 };
00287
00288
00289 u8 * net_packet;
00290
00291 Entry routes[MAX_TOTAL_SVCS];
00292
00293 SFBAlarmIndexType alarmNumber;
00294
00295 u8 last_route;
00296
00297 u8 offered[MAX_LOCAL_SVCS];
00298 u8 last_offered;
00299
00300 u8 bcastPeriodTicks;
00301 u8 ticksInGeneration;
00302
00303 u8 defaultTTL;
00304
00305 u8 neighborActiveTicks[FACE_COUNT];
00306 u8 bcastGeneration;
00307
00308 u8 flags;
00309
00310
00311 u8 packet_buffer[PACKET_PRINTER_PACKET_BUFFER_SIZE];
00312
00313
00314 u8 open_function;
00315
00316 u8 virtual_face;
00317
00318 u8 packet_printer_face;
00319
00320
00321
00322 u8 net_printer_face;
00323
00324
00325 u8 response_route[MAX_RETURN_ROUTE_LENGTH];
00326
00327
00328 s8 response_route_len;
00329
00330
00331 u8 response_src;
00332
00337 void ship() ;
00338
00339 void handleBroadcast(u8 *) ;
00340 void handleInvoke(u8 *) ;
00341
00342 Entry * lookup(u8 func, bool insert = false) ;
00343
00344 u8 pickDirection(u8 func, u8 origin) ;
00345
00346 u8 pickRandomFace(u8 func) ;
00347
00348 void trigger(u8 * packet, u8 * return_route, u8 hops) ;
00349
00350 };
00351
00355 extern SFBNet Net;
00356
00357 #endif