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
00207 void dump(u8 face, const u8 * prefix = 0) ;
00208
00209 private:
00210
00218 enum Flags {
00219 TAKE_FIRST_TIE= 0x01,
00220 RESPONSE_SENT_HEADER=0x02,
00221 NET_ACTIVE= 0x04,
00222 FLAG_RSRV3= 0x08,
00223 FLAG_RSRV4= 0x10,
00224 FLAG_RSRV5= 0x20,
00225 FLAG_RSRV6= 0x40,
00226 FLAG_RSRV7= 0x80
00227 };
00228
00232 u8 ticks() { return MsToTicks(millis()); }
00233
00234 u32 ticksToMs(u8 ticks) { return ticks<<8; }
00235
00236 u32 MsToTicks32(u32 ms) { return ms>>8; }
00237
00238 u8 MsToTicks(u32 ms) { return (u8) MsToTicks32(ms); }
00239
00240
00244 bool neighborActive_(u8 face) {
00245 u8 now = ticks();
00246 bool ret = IS_LATER8(neighborActiveTicks[face]+MsToTicks(2048),now);
00247 if (!ret) neighborActiveTicks[face] = now-10;
00248 return ret;
00249 }
00250
00251 static const FacePrinter net_printer;
00252 static const FacePrinter response_printer;
00253
00254 void functionPrint(u8 face, u8 byte) ;
00255 void functionPrintln(u8 face) ;
00256
00257 void printResponseHeader() ;
00258 void printResponseHeaderIfNeeded() { if ((flags&RESPONSE_SENT_HEADER)==0) printResponseHeader(); }
00259 void responsePrint(u8 face, u8 byte) ;
00260 void responsePrintln (u8 face) ;
00261
00262 static void NetBcastAlarm(u32) ;
00263 static void NetPacketHandler(u8 *) ;
00264
00265 static void NetFunctionPrint(u8 face, u8 byte ) ;
00266 static void NetFunctionPrintln( u8 face) ;
00267 static void NetResponsePrint(u8 face, u8 byte) ;
00268 static void NetResponsePrintln(u8 face) ;
00269
00273 void broadcastAlarm(u32) ;
00274
00278 void packetHandler(u8 *) ;
00279
00280 struct Entry {
00281 u8 func;
00282 u8 last_update[FACE_COUNT];
00283 u8 hops[FACE_COUNT];
00284 };
00285
00286
00287 u8 * net_packet;
00288
00289 Entry routes[MAX_TOTAL_SVCS];
00290
00291 SFBAlarmIndexType alarmNumber;
00292
00293 u8 last_route;
00294
00295 u8 offered[MAX_LOCAL_SVCS];
00296 u8 last_offered;
00297
00298 u8 bcastPeriodTicks;
00299 u8 ticksInGeneration;
00300
00301 u8 defaultTTL;
00302
00303 u8 neighborActiveTicks[FACE_COUNT];
00304 u8 bcastGeneration;
00305
00306 u8 flags;
00307
00308
00309 u8 packet_buffer[PACKET_PRINTER_PACKET_BUFFER_SIZE];
00310
00311
00312 u8 open_function;
00313
00314 u8 virtual_face;
00315
00316 u8 packet_printer_face;
00317
00318
00319
00320 u8 net_printer_face;
00321
00322
00323 u8 response_route[MAX_RETURN_ROUTE_LENGTH];
00324
00325
00326 s8 response_route_len;
00327
00328
00329 u8 response_src;
00330
00335 void ship() ;
00336
00337 void handleBroadcast(u8 *) ;
00338 void handleInvoke(u8 *) ;
00339
00340 Entry * lookup(u8 func, bool insert = false) ;
00341
00342 u8 pickDirection(u8 func, u8 origin) ;
00343
00344 u8 pickRandomFace(u8 func) ;
00345
00346 void trigger(u8 * packet, u8 * return_route, u8 hops) ;
00347
00348 };
00349
00353 extern SFBNet Net;
00354
00355 #endif