diff --git a/firmware/funk/mesh.c b/firmware/funk/mesh.c index 0bffc34..8e001e6 100644 --- a/firmware/funk/mesh.c +++ b/firmware/funk/mesh.c @@ -25,8 +25,26 @@ void initMesh(void){ meshbuffer[0].flags=MF_USED; }; -inline void blink(char a, char b){ - gpioSetValue (a,b, 1-gpioGetValue(a,b)); +MPKT * meshGetMessage(uint8_t type){ + int free=-1; + for(int i=0;iSECS_DAY) - 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)SECS_DAY) + meshbuffer[i].flags=MF_FREE; + }; }; }; }; @@ -83,37 +106,22 @@ void mesh_recvloop(void){ }; // Safety: Truncate ascii packets by 0-ing the CRC - if (MO_TYPE(buf) >='A' && MO_TYPE(buf) <='Z'){ - buf[MESHPKTSIZE-2]=0; - }; + buf[MESHPKTSIZE-2]=0; - // Store packet in a free slot - int free=-1; - for(int i=0;i - MO_TIME(meshbuffer[i].pkt)){ - free=i; - break; - }else{ - free=-2; - break; - }; - }; - }; + // Store packet in a same/free slot + MPKT* mpkt=meshGetMessage(MO_TYPE(buf)); - if(free==-1){ // Buffer full. Ah well. Kill a random packet - free=1; // XXX: GetRandom()? - }; - - if(free<0) + // Skip locked packet + if(mpkt->flags&MF_LOCK) continue; - memcpy(meshbuffer[free].pkt,buf,MESHPKTSIZE); - meshbuffer[free].flags=MF_USED; + // only accept newer/better packets + if(mpkt->flags==MF_USED) + if(MO_TIME(buf)pkt)) + continue; + + memcpy(mpkt->pkt,buf,MESHPKTSIZE); + mpkt->flags=MF_USED; }while(getTimer()MESHBUFSIZE); @@ -137,6 +145,8 @@ void mesh_sendloop(void){ for (int i=0;i