#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 MPKT meshbuffer[MESHBUFSIZE]; uint32_t const meshkey[4] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; struct NRF_CFG oldconfig; void initMesh(void){ for(int i=0;iSECS_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; }; if(MO_TYPE(buf)=='T'){ time_t toff=MO_TIME(buf)-((getTimer()-(200/SYSTICKSPEED))/(1000/SYSTICKSPEED)); if (toff>_timet) // Do not live in the past. _timet = toff; continue; }; // Safety: Truncate ascii packets by 0-ing the CRC if (MO_TYPE(buf) >='A' && MO_TYPE(buf) <='Z'){ 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; }; }; }; if(free==-1){ // Buffer full. Ah well. Kill a random packet free=1; // XXX: GetRandom()? }; if(free<0) continue; memcpy(meshbuffer[free].pkt,buf,MESHPKTSIZE); meshbuffer[free].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