Add remaining documentation
[libcontrac.git] / src / rpi_list.c
1 /** \ingroup KeyGeneration
2 * @file
3 * @author David Llewellyn-Jones <david@flypig.co.uk>
4 * @version $(VERSION)
5 *
6 * @section LICENSE
7 *
8 * Copyright David Llewellyn-Jones, 2020
9 * Released under the GPLv2.
10 *
11 * @brief Provides a list of RPIs
12 * @section DESCRIPTION
13 *
14 * This class allows the simplified management of lists of Rpi objects. This is
15 * useful when checking DTKs received from a Diagnosis Server with RPIs
16 * captured over Bluetooth. Combined with the \ref DtkList class the two can
17 * be easily stored and passed into the \ref match_list_find_matches() function.
18 *
19 */
20
21 /** \addtogroup Containers
22 * @{
23 */
24
25 // Includes
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stddef.h>
30 #include <stdint.h>
31
32 #include "contrac/contrac.h"
33 #include "contrac/utils.h"
34 #include "contrac/log.h"
35 #include "contrac/dtk_list.h"
36
37 #include "contrac/rpi_list.h"
38
39 // Defines
40
41 // Structures
42
43 /**
44 * @brief An RPI list element
45 *
46 * This is an opaque structure that represents a single item in the list and
47 * contains an Rpi instance.
48 *
49 * The structure typedef is in rpi_list.h
50 */
51 struct _RpiListItem {
52 Rpi * rpi;
53 RpiListItem * next;
54 };
55
56 /**
57 * @brief The head of an RPI list
58 *
59 * This is an opaque structure that represents the head of a list of Rpi
60 * items.
61 *
62 * This is the object usually passed as the first parameter of every non-static
63 * function.
64 *
65 * The structure typedef is in dtk_list.h
66 */
67 struct _RpiList {
68 RpiListItem * first;
69 RpiListItem * last;
70 };
71
72 // Function prototypes
73
74 // Function definitions
75
76 /**
77 * Creates a new instance of the class.
78 *
79 * @return The newly created object.
80 */
81 RpiList * rpi_list_new() {
82 RpiList * data;
83
84 data = calloc(sizeof(RpiList), 1);
85
86 return data;
87 }
88
89 /**
90 * Deletes an instance of the class, freeing up the memory allocated to it.
91 *
92 * This will also delete all items contained in the list.
93 *
94 * @param data The instance to free.
95 */
96 void rpi_list_delete(RpiList * data) {
97 RpiListItem * item;
98 RpiListItem * next;
99
100 if (data) {
101 item = data->first;
102 while (item) {
103 next = item->next;
104 rpi_delete(item->rpi);
105 free(item);
106 item = next;
107 }
108
109 free(data);
110 }
111 }
112
113 /**
114 * Adds an item to the list.
115 *
116 * This adds an Rpi item to the list. It's primarily for internal use and when
117 * adding RPIs to the list it's usually more appropriate to use the
118 * \ref rpi_list_add_beacon() function.
119 *
120 * @param data The list to append to.
121 * @param rpi The RPI to append.
122 */
123 void rpi_list_append(RpiList * data, Rpi * rpi) {
124 RpiListItem * item;
125
126 item = calloc(sizeof(RpiListItem), 1);
127 item->rpi = rpi;
128
129 if (data->last == NULL) {
130 data->first = item;
131 data->last = item;
132 }
133 else {
134 data->last->next = item;
135 data->last = item;
136 }
137 }
138
139 /**
140 * Returns the first item in the list.
141 *
142 * Useful for iterating through the items in the list.
143 *
144 * @param data The list to operate on.
145 * @return The first item of the list.
146 */
147 RpiListItem const * rpi_list_first(RpiList const * data) {
148 return data->first;
149 }
150
151 /**
152 * Returns the next item in the list.
153 *
154 * Useful for iterating through the items in the list.
155 *
156 * @param data The current item in the list.
157 * @return The next item in the list following the current item.
158 */
159 RpiListItem const * rpi_list_next(RpiListItem const * data) {
160 return data->next;
161 }
162
163 /**
164 * Returns the Rpi item contained in this list item.
165 *
166 * @param data The current item in the list.
167 * @return The Rpi instance stored in the list element.
168 */
169 Rpi const * rpi_list_get_rpi(RpiListItem const * data) {
170 return data->rpi;
171 }
172
173 /**
174 * Adds Rpi data to the list.
175 *
176 * The rpi_bytes buffer passed in must contain exactly RPI_SIZE (16) bytes of
177 * data. It doen't have to be null terminated.
178 *
179 * @param data The current list to operate on.
180 * @param rpi_bytes The RPI value to add, in binary format.
181 * @param time_interval_number The time interval number to associate with the
182 * RPI.
183 */
184 void rpi_list_add_beacon(RpiList * data, unsigned char const * rpi_bytes, uint8_t time_interval_number) {
185 Rpi * rpi = rpi_new();
186 rpi_assign(rpi, rpi_bytes, time_interval_number);
187 rpi_list_append(data, rpi);
188 }
189
190 /** @} addtogroup Containers*/
191