void RF24Mesh::setAddress(uint8_t nodeID, uint16_t address) { uint8_t position = addrListTop; for(uint8_t i=0; i<addrListTop; i++) { if( addrList[i].nodeID == nodeID) { position = i; break; } } addrList[position].nodeID = nodeID; addrList[position].address = address; if(position == addrListTop) { ++addrListTop; addrList = (addrListStruct*)realloc(addrList,(addrListTop + 1) * sizeof(addrListStruct)); } #if defined (__linux) && !defined(__ARDUINO_X86__) if(millis()-lastFileSave > 300) { lastFileSave = millis(); saveDHCP(); } #endif }
void RF24Mesh::DHCP(){ if(doDHCP){ doDHCP = 0; }else{ return; } RF24NetworkHeader header; memcpy(&header,network.frame_buffer,sizeof(RF24NetworkHeader)); struct addrResponseStruct{ uint16_t requester; uint16_t new_address; } addrResponse; // Get the unique id of the requester uint8_t from_id = header.reserved; if(!from_id){ #ifdef MESH_DEBUG_PRINTF printf("MSH: Invalid id 0 rcvd\n"); #endif //network.read(header,0,0); return; } //network.read(header,&addrResponse,sizeof(addrResponse)); memcpy(&addrResponse,network.frame_buffer+sizeof(RF24NetworkHeader),sizeof(addrResponse)); // Get the address of the sender (initial, or intermediary) uint16_t fwd_by = header.from_node; uint8_t shiftVal = 0; if(header.from_node == addrResponse.requester || header.from_node == MESH_DEFAULT_ADDRESS){ //Addresses 01-05 fwd_by = 0; // No forwarding address }else{ //Addresses 01111-05555 uint16_t m = fwd_by; uint8_t count = 0; while(m){ //Octal addresses convert nicely to binary in threes. Address 03 = B011 Address 033 = B011011 m >>= 3; //Find out how many digits are in the octal address count++; } shiftVal = count*3; //Now we know how many bits to shift when adding a child node 1-5 (B001 to B101) to any address } #ifdef MESH_DEBUG_PRINTF // printf("%u MSH: Rcv addr req from_id %d \n",millis(),from_id); #endif for(int i=MESH_MAX_CHILDREN; i> 0; i--){ // For each of the possible addresses (5 max) bool found = 0; addrResponse.new_address = fwd_by | (i << shiftVal); if(!addrResponse.new_address ){ /*printf("dumped 0%o\n",addrResponse.new_address);*/ continue; } for(uint8_t i=0; i < addrListTop; i++){ #if defined (MESH_DEBUG_MINIMAL) #if !defined (__linux) && !defined ARDUINO_SAM_DUE || defined TEENSY || defined(__ARDUINO_X86__) Serial.print("ID: ");Serial.print(addrList[i].nodeID,DEC);Serial.print(" ADDR: "); uint16_t newAddr = addrList[i].address; char addr[5] = " ", count=3, mask=7; while(newAddr){ addr[count] = (newAddr & mask)+48; //get the individual Octal numbers, specified in chunks of 3 bits, convert to ASCII by adding 48 newAddr >>= 3; count--; } Serial.println(addr); #else printf("ID: %d ADDR: 0%o\n", addrList[i].nodeID,addrList[i].address); #endif #endif if( (addrList[i].address == addrResponse.new_address && addrList[i].nodeID != from_id ) || addrResponse.new_address == MESH_DEFAULT_ADDRESS){ found = 1; break; } } if(!found){ header.type = NETWORK_ADDR_RESPONSE; header.to_node = header.from_node; //This is a routed request to 00 if(header.from_node != addrResponse.requester){ //Is NOT node 01 to 05 delay(2); if( network.write(header,&addrResponse,sizeof(addrResponse)) ){ //addrMap[from_id] = addrResponse.new_address; } }else{ delay(2); network.write(header,&addrResponse,sizeof(addrResponse),header.to_node); //addrMap[from_id] = addrResponse.new_address; } uint32_t timer=millis(); while(network.update() != MESH_ADDR_CONFIRM){ if(millis()-timer>900){ //printf("No addr confirmation from 0%o\n",header.to_node); return; } } //printf("Got addr confirmation from 0%o\n",header.to_node); found = 0; for(uint8_t i=0; i < addrListTop; i++){ if( addrList[i].nodeID == from_id ){ addrList[i].address = addrResponse.new_address; found = 1; #if defined (__linux) && !defined(__ARDUINO_X86__) if(millis()-lastFileSave > 300){ lastFileSave = millis(); saveDHCP(); } #endif break; } } if(!found){ addrList[addrListTop].nodeID = from_id; addrList[addrListTop].address = addrResponse.new_address; #if defined (__linux) && !defined(__ARDUINO_X86__) if(millis()-lastFileSave > 300){ lastFileSave = millis(); saveDHCP(); } #endif ++addrListTop; addrList = (addrListStruct*)realloc(addrList,(addrListTop+1) * sizeof(addrListStruct)); } #ifdef MESH_DEBUG_PRINTF printf("Sent to 0%o phys: 0%o new: 0%o id: %d\n", header.to_node,addrResponse.requester,addrResponse.new_address,header.reserved); #endif break; }else{ #if defined (MESH_DEBUG_PRINTF) printf("not allocated\n"); #endif } } //}else{ //break; //} }