mirror of
https://github.com/SpacehuhnTech/esp8266_deauther.git
synced 2025-12-23 07:29:20 -08:00
Used SimpleList for Accesspoints
I've wrote my own linked-list library. It can do a few things more than the Arduino LinkedList lib. The code is a bit cleaner and it doesn't collide with other LinkedList libraries (i.e. ESP32-IDF)
This commit is contained in:
@@ -1,90 +1,25 @@
|
|||||||
#include "Accesspoints.h"
|
#include "Accesspoints.h"
|
||||||
|
|
||||||
Accesspoints::Accesspoints() {
|
Accesspoints::Accesspoints() {
|
||||||
|
list = new SimpleList<AP>;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Accesspoints::sort() {
|
void Accesspoints::sort() {
|
||||||
// bubble sort because I was lazy
|
list->sort([](AP &a, AP &b) -> bool{
|
||||||
|
return WiFi.RSSI(a.id) < WiFi.RSSI(b.id);
|
||||||
AP* aAP; // prev
|
});
|
||||||
AP* bAP; // to compare with c
|
changed = true;
|
||||||
AP* cAP; // to compare with b
|
|
||||||
AP* dAP; // next
|
|
||||||
int c = listSize;
|
|
||||||
while(c--){
|
|
||||||
for(int i = 1; i <= c; i++){
|
|
||||||
aAP = getAP(i-2); // prev
|
|
||||||
bAP = aAP ? aAP->next : getAP(i-1); // to be compared
|
|
||||||
cAP = bAP ? bAP->next : getAP(i); // to be compared
|
|
||||||
dAP = cAP ? cAP->next : getAP(i+1); // next
|
|
||||||
|
|
||||||
// a -> b -> c -> d
|
|
||||||
|
|
||||||
if(WiFi.RSSI(bAP->id) < WiFi.RSSI(cAP->id)) {
|
|
||||||
// a -> b <-> c
|
|
||||||
cAP->next = bAP;
|
|
||||||
|
|
||||||
// a -> b -> d
|
|
||||||
if(dAP){
|
|
||||||
bAP->next = dAP;
|
|
||||||
} else {
|
|
||||||
bAP->next = NULL;
|
|
||||||
listEnd = bAP;
|
|
||||||
}
|
|
||||||
|
|
||||||
// a -> c -> b -> d
|
|
||||||
if(aAP)
|
|
||||||
aAP->next = cAP;
|
|
||||||
else
|
|
||||||
listBegin = cAP;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Accesspoints::sortAfterChannel() {
|
void Accesspoints::sortAfterChannel() {
|
||||||
AP* aAP;
|
list->sort([](AP &a, AP &b) -> bool{
|
||||||
AP* bAP;
|
return WiFi.channel(a.id) > WiFi.channel(b.id);
|
||||||
AP* cAP;
|
});
|
||||||
AP* dAP;
|
|
||||||
int c = listSize;
|
|
||||||
while(c--){
|
|
||||||
for(int i = 1; i <= c; i++){
|
|
||||||
aAP = getAP(i-2);
|
|
||||||
bAP = aAP ? aAP->next : getAP(i-1);
|
|
||||||
cAP = bAP ? bAP->next : getAP(i);
|
|
||||||
dAP = cAP ? cAP->next : getAP(i+1);
|
|
||||||
if(WiFi.channel(bAP->id) > WiFi.channel(cAP->id)) {
|
|
||||||
cAP->next = bAP;
|
|
||||||
|
|
||||||
if(dAP){
|
|
||||||
bAP->next = dAP;
|
|
||||||
} else {
|
|
||||||
bAP->next = NULL;
|
|
||||||
listEnd = bAP;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(aAP) aAP->next = cAP;
|
|
||||||
else listBegin = cAP;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Accesspoints::add(uint8_t id, bool selected) {
|
void Accesspoints::add(uint8_t id, bool selected) {
|
||||||
if(!listEnd){
|
list->add(AP{id, selected});
|
||||||
listEnd = new AP{id, selected};
|
|
||||||
} else {
|
|
||||||
listEnd->next = new AP{id, selected};
|
|
||||||
listEnd = listEnd->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!listBegin)
|
|
||||||
listBegin = listEnd;
|
|
||||||
|
|
||||||
listSize++;
|
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,12 +161,12 @@ bool Accesspoints::getHidden(int num) {
|
|||||||
|
|
||||||
bool Accesspoints::getSelected(int num) {
|
bool Accesspoints::getSelected(int num) {
|
||||||
if (!check(num)) return false;
|
if (!check(num)) return false;
|
||||||
return getAP(num)->selected;
|
return list->get(num).selected;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Accesspoints::getID(int num){
|
int Accesspoints::getID(int num){
|
||||||
if (!check(num)) return -1;
|
if (!check(num)) return -1;
|
||||||
return getAP(num)->id;
|
return list->get(num).id;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Accesspoints::select(int num) {
|
void Accesspoints::select(int num) {
|
||||||
@@ -268,51 +203,34 @@ void Accesspoints::remove(int num) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Accesspoints::selectAll() {
|
void Accesspoints::selectAll() {
|
||||||
int i = 0;
|
for(int i=0;i<count();i++)
|
||||||
AP* hAP = listBegin;
|
list->replace(i,AP{list->get(i).id,true});
|
||||||
while(i < listSize){
|
|
||||||
hAP->selected = true;
|
|
||||||
hAP = hAP->next;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
prntln(AP_SELECTED_ALL);
|
prntln(AP_SELECTED_ALL);
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Accesspoints::deselectAll() {
|
void Accesspoints::deselectAll() {
|
||||||
int i = 0;
|
for(int i=0;i<count();i++)
|
||||||
AP* hAP = listBegin;
|
list->replace(i,AP{list->get(i).id,false});
|
||||||
while(i < listSize){
|
|
||||||
hAP->selected = false;
|
|
||||||
hAP = hAP->next;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
prntln(AP_DESELECTED_ALL);
|
prntln(AP_DESELECTED_ALL);
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Accesspoints::removeAll() {
|
void Accesspoints::removeAll() {
|
||||||
while(listSize > 0){
|
while(count() > 0)
|
||||||
internal_remove(0);
|
internal_remove(0);
|
||||||
}
|
|
||||||
prntln(AP_REMOVED_ALL);
|
prntln(AP_REMOVED_ALL);
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Accesspoints::count() {
|
int Accesspoints::count() {
|
||||||
return listSize;
|
return list->size();
|
||||||
}
|
}
|
||||||
|
|
||||||
int Accesspoints::selected() {
|
int Accesspoints::selected() {
|
||||||
int num = 0;
|
return list->count([](AP &a)->bool{
|
||||||
int i = 0;
|
return a.selected;
|
||||||
AP* hAP = listBegin;
|
});
|
||||||
while(i < listSize){
|
|
||||||
num += hAP->selected;
|
|
||||||
hAP = hAP->next;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return num;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Accesspoints::check(int num) {
|
bool Accesspoints::check(int num) {
|
||||||
@@ -322,50 +240,19 @@ bool Accesspoints::check(int num) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
AP* Accesspoints::getAP(int num){
|
|
||||||
if(num < 0 || num >= listSize) return NULL;
|
|
||||||
|
|
||||||
AP* hAP = listBegin;
|
|
||||||
int i = 0;
|
|
||||||
while(hAP->next && i < num){
|
|
||||||
hAP = hAP->next;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return hAP;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Accesspoints::internal_check(int num) {
|
bool Accesspoints::internal_check(int num) {
|
||||||
return num >= 0 && num < count();
|
return num >= 0 && num < count();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Accesspoints::internal_select(int num) {
|
void Accesspoints::internal_select(int num) {
|
||||||
getAP(num)->selected = true;
|
list->replace(num,AP{list->get(num).id,true});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Accesspoints::internal_deselect(int num) {
|
void Accesspoints::internal_deselect(int num) {
|
||||||
getAP(num)->selected = false;
|
list->replace(num,AP{list->get(num).id,false});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Accesspoints::internal_remove(int num) {
|
void Accesspoints::internal_remove(int num) {
|
||||||
AP* aAP = getAP(num-1); // prev
|
list->remove(num);
|
||||||
AP* bAP = aAP ? aAP->next : getAP(num); // to-delete
|
|
||||||
AP* cAP = bAP ? bAP->next : getAP(num+1); // next
|
|
||||||
|
|
||||||
if(aAP && cAP) { // a -> b -> c = a -> c
|
|
||||||
aAP->next = cAP; //
|
|
||||||
} else if(aAP) { // a -> b = a
|
|
||||||
aAP->next = NULL;
|
|
||||||
listEnd = aAP;
|
|
||||||
} else if(cAP) { // b -> c = c
|
|
||||||
listBegin = cAP;
|
|
||||||
} else { // b = EMPTY
|
|
||||||
listBegin = NULL;
|
|
||||||
listEnd = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(bAP);
|
|
||||||
|
|
||||||
listSize--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include "Names.h"
|
#include "Names.h"
|
||||||
#include "language.h"
|
#include "language.h"
|
||||||
|
#include "SimpleList.h"
|
||||||
|
|
||||||
extern Names names;
|
extern Names names;
|
||||||
|
|
||||||
@@ -15,7 +16,6 @@ String fixUtf8(String str);
|
|||||||
struct AP{
|
struct AP{
|
||||||
uint8_t id;
|
uint8_t id;
|
||||||
bool selected;
|
bool selected;
|
||||||
AP* next;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Accesspoints {
|
class Accesspoints {
|
||||||
@@ -58,12 +58,7 @@ class Accesspoints {
|
|||||||
bool check(int num);
|
bool check(int num);
|
||||||
bool changed = false;
|
bool changed = false;
|
||||||
private:
|
private:
|
||||||
AP* listBegin = NULL;
|
SimpleList<AP>* list;
|
||||||
AP* listEnd = NULL;
|
|
||||||
int listSize = 0;
|
|
||||||
//LinkedList<AP>* listA;
|
|
||||||
|
|
||||||
AP* getAP(int num);
|
|
||||||
|
|
||||||
bool internal_check(int num);
|
bool internal_check(int num);
|
||||||
void internal_select(int num);
|
void internal_select(int num);
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
#ifndef SimpleList_h
|
|
||||||
#define SimpleList_h
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
struct Node {
|
|
||||||
T data;
|
|
||||||
Node<T> *next;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
class SimpleList{
|
|
||||||
public:
|
|
||||||
SimpleList();
|
|
||||||
~SimpleList();
|
|
||||||
|
|
||||||
int size();
|
|
||||||
void add(int index, T obj);
|
|
||||||
void add(T obj);
|
|
||||||
bool set(int index, T obj);
|
|
||||||
void remove(int index);
|
|
||||||
T shift();
|
|
||||||
T pop();
|
|
||||||
T get(int index);
|
|
||||||
void clear();
|
|
||||||
void sort(int (*cmp)(T &nodeA, T &nodeB));
|
|
||||||
void swap(int x, int y);
|
|
||||||
private:
|
|
||||||
int listSize;
|
|
||||||
Node<T> *listBegin;
|
|
||||||
Node<T> *listEnd;
|
|
||||||
|
|
||||||
// Helps get() method by saving last position
|
|
||||||
Node<T> *lastNodeGot;
|
|
||||||
int lastIndexGot;
|
|
||||||
bool isCached;
|
|
||||||
|
|
||||||
Node<T>* getNode(int index);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
Reference in New Issue
Block a user