void mesh_cleanup(void){ time_t now=getSeconds(); for(int i=1;i<MESHBUFSIZE;i++){ if(meshbuffer[i].flags&MF_USED){ if (MO_GEN(meshbuffer[i].pkt)<meshgen) meshbuffer[i].flags=MF_FREE; if (MO_TYPE(meshbuffer[i].pkt)>='a' && MO_TYPE(meshbuffer[i].pkt)<='z'){ ; }else{ if (MO_TIME(meshbuffer[i].pkt)<now) meshbuffer[i].flags=MF_FREE; if (MO_TIME(meshbuffer[i].pkt)-now>SECS_DAY) meshbuffer[i].flags=MF_FREE; }; }; }; };
uint8_t mesh_recvqloop_work(void){ __attribute__ ((aligned (4))) uint8_t buf[32]; int len; len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,meshkey); // Receive if(len<=0){ return 0; }; if(MO_GEN(buf)>meshgen){ if(meshgen) meshgen++; else meshgen=MO_GEN(buf); _timet=0; meshincctr=0; meshnice=0; }; if(MO_TYPE(buf)=='T'){ time_t toff=MO_TIME(buf)-((getTimer()+(600/SYSTICKSPEED))/(1000/SYSTICKSPEED)); if (toff>_timet){ // Do not live in the past. _timet = toff; meshincctr++; }; if(MO_BODY(buf)[4] > meshnice) meshnice=MO_BODY(buf)[4]; return 1; }; // Safety: Truncate ascii packets by 0-ing the CRC buf[MESHPKTSIZE-2]=0; // Store packet in a same/free slot MPKT* mpkt=meshGetMessage(MO_TYPE(buf)); // Skip locked packet if(mpkt->flags&MF_LOCK) return 2; // only accept newer/better packets if(mpkt->flags==MF_USED) if(MO_TIME(buf)<=MO_TIME(mpkt->pkt)) return 2; if((MO_TYPE(buf)>='A' && MO_TYPE(buf)<='C') || (MO_TYPE(buf)>='A' && MO_TYPE(buf)<='C')) meshmsg=1; memcpy(mpkt->pkt,buf,MESHPKTSIZE); mpkt->flags=MF_USED; return 1; };
static int mesh_sanity(uint8_t * pkt){ if(MO_TYPE(pkt)>='A' && MO_TYPE(pkt)<='Z'){ if(MO_TIME(pkt)>1313803870) return 1; if(MO_TIME(pkt)<1312075898) return 1; }else if(MO_TYPE(pkt)>='a' && MO_TYPE(pkt)<='z'){ if(MO_TIME(pkt)>16777216) return 1; if(MO_TIME(pkt)<0) return 1; }; if(MO_TYPE(pkt)>0x7f || MO_TYPE(pkt)<0x20) return 1; return 0; };
MPKT * meshGetMessage(uint8_t type){ int free=-1; for(int i=0;i<MESHBUFSIZE;i++){ if ( ((meshbuffer[i].flags&MF_USED)==0) && free<0 ) free=i; if ( (meshbuffer[i].flags&MF_USED) && (MO_TYPE(meshbuffer[i].pkt) == type)){ free=i; break; }; }; if(free==-1){ // Buffer full. Ah well. Kill a random packet free=1; // XXX: GetRandom()? meshbuffer[free].flags=MF_FREE; }; if(meshbuffer[free].flags==MF_FREE){ memset(&meshbuffer[free],0,sizeof(MPKT)); MO_TYPE_set(meshbuffer[free].pkt,type); MO_GEN_set(meshbuffer[free].pkt,meshgen); meshbuffer[free].flags=MF_USED; }; return &meshbuffer[free]; };