This repository was archived by the owner on Sep 1, 2023. It is now read-only.
forked from nRF24/RF24Mesh
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRF24AddressBook.cpp
155 lines (136 loc) · 4.54 KB
/
RF24AddressBook.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "RF24AddressBook.h"
void AddressBook::begin() {
list = (MeshAddress *) malloc(sizeof(MeshAddress));
top = 0;
}
int AddressBook::alloc(nodeid_t nodeID, address_t address) {
for (uint8_t i=0; i < top; i++)
if (list[i].nodeID == nodeID) {
list[i].address = address;
list[i].lastRenew = millis();
return 1; // For reusing current address
}
// Now handle getting a new address
list[top].address = address;
list[top].nodeID = nodeID;
list[top].lastRenew = millis();
top ++;
list = (MeshAddress*)realloc(list, (top+1)*sizeof(MeshAddress));
return 1;
}
address_t AddressBook::lookup_addr(nodeid_t nodeID) {
for (uint8_t i=0; i < top; i++)
if (list[i].nodeID == nodeID)
return list[i].address;
return 0;
}
nodeid_t AddressBook::lookup_id(address_t address) {
for (uint8_t i=0; i < top; i++)
if (list[i].address == address)
return list[i].nodeID;
return 0;
}
int AddressBook::renew(nodeid_t nodeID) {
for (uint8_t i=0; i < top; i++)
if (list[i].nodeID == nodeID) {
list[i].lastRenew = millis();
return 0;
}
return -1;
}
int AddressBook::release(address_t address) {
for (uint8_t i=0; i<top; i++)
if (list[i].address == address) {
// Now free the memory from the stack
for (uint8_t j=i+1; j<top; j++)
list[j-1] = list[j];
top --;
list = (MeshAddress*) realloc(list, (top+1)*sizeof(MeshAddress));
return 0;
}
return -1;
}
// WARNING: Time consuming operation. Only call once in a while.
int AddressBook::prune() {
int counter = 0;
uint32_t time_ms = millis();
// Pass 1: Count number of nodes STAYING in the list
// It should not have expired and should not have registered AFTER current time
for (uint8_t i = 0; i < top; i++) {
if ((long)(time_ms - list[i].lastRenew) < MESH_ADDRESS_EXPIRY && list[i].lastRenew < time_ms) {
counter ++;
} else {
#if defined (MESH_DEBUG_PRINTF)
printf("[DHCP] Pruning address 0x%x\n", list[i].nodeID);
#elif defined (MESH_DEBUG_SERIAL)
Serial.print(F("[DHCP] Pruning address 0x"));
Serial.println(list[i].nodeID, HEX);
#endif
}
}
if (top == counter) return counter;
auto newlist = (MeshAddress*) malloc((counter + 1) * sizeof(MeshAddress));
// Serial.print(F("Pruning addressbook to "));
// Serial.println(top);
counter = 0;
for (uint8_t i = 0; i < top; i++) {
if ((long)(time_ms - list[i].lastRenew) < MESH_ADDRESS_EXPIRY && list[i].lastRenew < time_ms) {
newlist[counter] = list[i];
counter ++;
}
}
top = counter;
free(list);
list = newlist;
return counter;
}
void AddressBook::loadFromFile() {
#if defined (__linux) && !defined(__ARDUINO_X86__)
std::ifstream infile ("dhcplist.txt",std::ifstream::binary);
if(!infile){ return; }
list[top].nodeID = 255;
list[top].address = 01114;
infile.seekg(0,infile.end);
int length = infile.tellg();
infile.seekg(0,infile.beg);
list = (MeshAddress*) realloc(list,length + sizeof(MeshAddress));
top = length/sizeof(MeshAddress);
for(int i=0; i<top; i++) {
infile.read( (char*)&list[i],sizeof(MeshAddress));
}
infile.close();
#endif
}
void AddressBook::saveToFile() {
#if defined (__linux) && !defined(__ARDUINO_X86__)
std::ofstream outfile ("dhcplist.txt",std::ofstream::binary | std::ofstream::trunc);
//printf("writingToFile %d 0%o size %d\n",list[0].nodeID,list[0].address,sizeof(MeshAddress));
for(int i=0; i< top; i++){
outfile.write( (char*)&list[i],sizeof(MeshAddress));
}
outfile.close();
/*MeshAddress aList;
std::ifstream infile ("dhcplist.txt",std::ifstream::binary);
infile.seekg(0,infile.end);
int length = infile.tellg();
infile.seekg(0,infile.beg);
//list = (MeshAddress*)malloc(length);
//infile.read( (char*)&list,length);
infile.read( (char*)&aList,sizeof(MeshAddress));
//top = length/sizeof(MeshAddress);
//for(int i=0; i< top; i++){
printf("ID: %d ADDR: 0%o \n",aList.nodeID,aList.address);
//}
infile.close();*/
#endif
}
int AddressBook::count() {
return top;
}
MeshAddress& AddressBook::operator[](int index) {
if (index >= top) {
// Handle overflow case
return list[0];
}
return list[index];
}