#include #include #include #include "basic/basic.h" #include "funk/mesh.h" #include "funk/nrf24l01p.h" #include "basic/byteorder.h" #include "basic/random.h" char meshgen=0; // Generation char meshincctr=0; // Generation MPKT meshbuffer[MESHBUFSIZE]; uint32_t const meshkey[4] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; struct NRF_CFG oldconfig; void initMesh(void){ for(int i=0;i='a' && MO_TYPE(meshbuffer[i].pkt)<='z'){ ; }else{ if (MO_TIME(meshbuffer[i].pkt)SECS_DAY) meshbuffer[i].flags=MF_FREE; }; }; }; }; void mesh_recvloop(void){ __attribute__ ((aligned (4))) uint8_t buf[32]; int len; int recvend=M_RECVTIM/SYSTICKSPEED+getTimer(); int pktctr=0; nrf_config_get(&oldconfig); nrf_set_channel(MESH_CHANNEL); nrf_set_rx_mac(0,MESHPKTSIZE,strlen(MESH_MAC),(uint8_t*)MESH_MAC); mesh_cleanup(); nrf_rcv_pkt_start(); do{ len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,meshkey); // Receive if(len<=0){ delayms_power(10); continue; }; pktctr++; if(MO_GEN(buf)>meshgen){ if(meshgen) meshgen++; else meshgen=MO_GEN(buf); _timet=0; meshincctr=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++; }; continue; }; // 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) continue; // 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); nrf_rcv_pkt_end(); nrf_config_set(&oldconfig); } void mesh_sendloop(void){ int ctr=0; __attribute__ ((aligned (4))) uint8_t buf[32]; int status; nrf_config_get(&oldconfig); nrf_set_channel(MESH_CHANNEL); nrf_set_tx_mac(strlen(MESH_MAC),(uint8_t*)MESH_MAC); // Update [T]ime packet MO_TIME_set(meshbuffer[0].pkt,getSeconds()); MO_GEN_set(meshbuffer[0].pkt,meshgen); for (int i=0;i