-
Notifications
You must be signed in to change notification settings - Fork 24
/
linked_list.cpp
147 lines (127 loc) · 4.03 KB
/
linked_list.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
// **********************************************************************************
// ULPNode RF Gateway Basic Linked List source file
// **********************************************************************************
// Creative Commons Attrib Share-Alike License
// You are free to use/extend this library but please abide with the CC-BY-SA license:
// http://creativecommons.org/licenses/by-sa/4.0/
//
// For any explanation of ULPNode RF Protocol see
// https://hallard.me/ulpnode-rf-protocol/
//
// For any explanation of ULPNode see
// https://hallard.me/category/ulpnode
//
// Written by Charles-Henri Hallard (http://hallard.me)
//
// History : V1.10 2015-09-05 - Creation
//
// All text above must be included in any redistribution.
//
// **********************************************************************************
#include "./linked_list.h"
/* ======================================================================
Function: ll_Delete
Purpose : Delete the ENTIRE Linked List, not a value
Input : pointer to the top of the linked list
Output : True if Ok False Otherwise
Comments: -
====================================================================== */
boolean ll_Delete(NodeList * me)
{
// Got a pointer
if (me) {
NodeList *current;
// For each linked list
while ((current = me->next)) {
// Get the next
me->next = current->next;
// Free the current
free(current);
}
// Free the top element
me->next = NULL ;
return (true);
}
return (false);
}
/* ======================================================================
Function: ll_Add
Purpose : Add element to the Linked List
Input : pointer to the top of the linked list
network ID
node ID
RSSI
second ellapsed since start
Output : pointer to the new node (or founded one)
Comments: last seen is filled with old value in return
====================================================================== */
NodeList * ll_Add(NodeList * me, uint8_t groupid, uint8_t nodeid, int8_t rssi, unsigned long * sec)
{
// Create pointer on the new node
NodeList *newNode = NULL;
// Something to use
if (me) {
// Loop thru the node to see if we know it
while (me->next) {
// go to next node
me = me->next;
// Check if we already have this node id and group id
if ( me->groupid == groupid && me->nodeid == nodeid) {
// Save old value
unsigned long old_sec = me->lastseen;
// Update data
me->rssi = rssi ;
me->lastseen = *sec;
// Return old value
*sec = old_sec;
// That's all
return (me);
}
}
// We did not find this node, it's new
// Create new node with size to store data
if ((newNode = (NodeList *) malloc(sizeof(NodeList)) ) != NULL) {
// Setup our new node values
newNode->next = NULL;
newNode->groupid = groupid ;
newNode->nodeid = nodeid;
newNode->rssi = rssi ;
newNode->lastseen = *sec;
// add the new node on the list
me->next = newNode;
// return pointer on the new node
return (newNode);
}
}
// Error
return ( (NodeList *) NULL );
}
/* ======================================================================
Function: ll_Dump
Purpose : dump linked list content
Input : pointer on the linked list
current seconds ellapsed
Output : total number of values
Comments: -
====================================================================== */
uint8_t ll_Dump(NodeList * me, unsigned long sec)
{
// Get our linked list
uint8_t index = 0;
// Got one ?
if (me) {
// Loop thru the node
while (me->next) {
// go to next node
me = me->next;
index++;
Debug(index); DebugF(") ") ;
DebugF("Group:"); DEBUG_SERIAL.print(me->groupid, DEC) ;
DebugF(" Node:"); DEBUG_SERIAL.print(me->nodeid, DEC) ;
DebugF(" RSSI:"); DEBUG_SERIAL.print(me->rssi, DEC) ;
DebugF(" seen:"); Debug(sec-me->lastseen) ;
DebuglnF("") ;
}
}
return index;
}