1 /** \ingroup KeyGeneration
3 * @author David Llewellyn-Jones <david@flypig.co.uk>
8 * Copyright David Llewellyn-Jones, 2020
9 * Released under the GPLv2.
11 * @brief Provides a list of DTKs
12 * @section DESCRIPTION
14 * This class allows the simplified management of lists of Dtk objects. This is
15 * useful when checking DTKs received from a Diagnosis Server with RPIs
16 * captured over Bluetooth. Combined with the \ref RpiList class the two can
17 * be easily stored and passed into the \ref match_list_find_matches() function.
21 /** \addtogroup Containers
32 #include "contrac/contrac.h"
33 #include "contrac/utils.h"
34 #include "contrac/log.h"
36 #include "contrac/dtk_list.h"
43 * @brief A DTK list element
45 * This is an opaque structure that represents a single item in the list and
46 * contains a Dtk instance.
48 * The structure typedef is in dtk_list.h
56 * @brief The head of a DTK list
58 * This is an opaque structure that represents the head of a list of Dtk
61 * This is the object usually passed as the first parameter of every non-static
64 * The structure typedef is in dtk_list.h
71 // Function prototypes
73 // Function definitions
76 * Creates a new instance of the class.
78 * @return The newly created object.
80 DtkList
* dtk_list_new() {
83 data
= calloc(sizeof(DtkList
), 1);
89 * Deletes an instance of the class, freeing up the memory allocated to it.
91 * This will also delete all items contained in the list.
93 * @param data The instance to free.
95 void dtk_list_delete(DtkList
* data
) {
103 dtk_delete(item
->dtk
);
113 * Adds an item to the list.
115 * This adds a Dtk item to the list. It's primarily for internal use and when
116 * adding DTKs to the list it's usually more appropriate to use the
117 * \ref dtk_list_add_diagnosis() function.
119 * @param data The list to append to.
120 * @param dtk The DTK to append.
122 void dtk_list_append(DtkList
* data
, Dtk
* dtk
) {
125 item
= calloc(sizeof(DtkListItem
), 1);
128 if (data
->last
== NULL
) {
133 data
->last
->next
= item
;
139 * Returns the first item in the list.
141 * Useful for iterating through the items in the list.
143 * @param data The list to operate on.
144 * @return The first item of the list.
146 DtkListItem
const * dtk_list_first(DtkList
const * data
) {
151 * Returns the next item in the list.
153 * Useful for iterating through the items in the list.
155 * @param data The current item in the list.
156 * @return The next item in the list following the current item.
158 DtkListItem
const * dtk_list_next(DtkListItem
const * data
) {
163 * Returns the Dtk item contained in this list item.
165 * @param data The current item in the list.
166 * @return The Dtk instance stored in the list element.
168 Dtk
const * dtk_list_get_dtk(DtkListItem
const * data
) {
173 * Adds Dtk data to the list.
175 * The dtk_bytes buffer passed in must contain exactly DTK_SIZE (16) bytes of
176 * data. It doen't have to be null terminated.
178 * @param data The current list to operate on.
179 * @param dtk_bytes The DTK value to add, in binary format.
180 * @param day_number The day number to associate with the DTK.
182 void dtk_list_add_diagnosis(DtkList
* data
, unsigned char const * dtk_bytes
, uint32_t day_number
) {
183 Dtk
* dtk
= dtk_new();
184 dtk_assign(dtk
, dtk_bytes
, day_number
);
185 dtk_list_append(data
, dtk
);
188 /** @} addtogroup Containers*/