1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/bluetooth/mesh.h>
8 
9 /** @brief Slot iteration callback.
10  *
11  *  @param slot      A valid DFU image slot.
12  *  @param user_data User data passed to @ref bt_mesh_dfu_slot_foreach.
13  *
14  *  @return Iteration action determining next step.
15  */
16 typedef enum bt_mesh_dfu_iter (*bt_mesh_dfu_slot_cb_t)(
17 	const struct bt_mesh_dfu_slot *slot, void *user_data);
18 
19 /** @brief Get the number of slots committed to the firmware list.
20  *
21  *  @return Number of committed slots.
22  */
23 int bt_mesh_dfu_slot_count(void);
24 
25 /** @brief Reserve a new DFU image slot for a distributable image.
26  *
27  *  A DFU image slot represents a single distributable DFU image with all its
28  *  metadata. The slot data must be set using @ref bt_mesh_dfu_slot_info_set and
29  *  @ref bt_mesh_dfu_slot_fwid_set, and the slot committed using
30  *  @ref bt_mesh_dfu_slot_commit for the slot to be considered part of the slot
31  *  list.
32  *
33  *  @return A pointer to the reserved slot, or NULL if allocation failed.
34  */
35 struct bt_mesh_dfu_slot *bt_mesh_dfu_slot_reserve(void);
36 
37 /** @brief Set the size and metadata for a reserved slot.
38  *
39  *  @param dfu_slot     Pointer to the reserved slot for which to set the
40  *                      metadata.
41  *  @param size         The size of the image.
42  *  @param metadata     Metadata or NULL.
43  *  @param metadata_len Length of the metadata, at most @c
44  *                      CONFIG_BT_MESH_DFU_METADATA_MAXLEN.
45  *
46  *  @return 0 on success, (negative) error code otherwise.
47  */
48 int bt_mesh_dfu_slot_info_set(struct bt_mesh_dfu_slot *dfu_slot, size_t size,
49 			      const uint8_t *metadata, size_t metadata_len);
50 
51 /** @brief Set the new fwid for the incoming image for a reserved slot.
52  *
53  *  @param dfu_slot Pointer to the reserved slot for which to set the fwid.
54  *  @param fwid     Fwid to set.
55  *  @param fwid_len Length of the fwid, at most @c
56  *                  CONFIG_BT_MESH_DFU_FWID_MAXLEN.
57  *
58  *  @return 0 on success, (negative) error code otherwise.
59  */
60 int bt_mesh_dfu_slot_fwid_set(struct bt_mesh_dfu_slot *dfu_slot,
61 			      const uint8_t *fwid, size_t fwid_len);
62 
63 /** @brief Commit the reserved slot to the list of slots, and store it
64  *         persistently.
65  *
66  *  If the commit fails for any reason, the slot will still be in the reserved
67  *  state after this call.
68  *
69  *  @param dfu_slot Pointer to the reserved slot.
70  *
71  *  @return 0 on success, (negative) error code otherwise.
72  */
73 int bt_mesh_dfu_slot_commit(struct bt_mesh_dfu_slot *dfu_slot);
74 
75 /** @brief Release a reserved slot so that it can be reserved again.
76  *
77  *  @param dfu_slot Pointer to the reserved slot.
78  */
79 void bt_mesh_dfu_slot_release(const struct bt_mesh_dfu_slot *dfu_slot);
80 
81 /** @brief Delete a committed DFU image slot.
82  *
83  *  @param slot Slot to delete. Must be a valid pointer acquired from this
84  *              module.
85  *
86  *  @return 0 on success, or (negative) error code on failure.
87  */
88 int bt_mesh_dfu_slot_del(const struct bt_mesh_dfu_slot *slot);
89 
90 /** @brief Delete all DFU image slots.
91  *
92  *  @return 0 on success, or (negative) error code on failure.
93  */
94 void bt_mesh_dfu_slot_del_all(void);
95 
96 /** @brief Get the DFU image slot at the given firmware image list index.
97  *
98  *  @param idx DFU image slot index.
99  *
100  *  @return The DFU image slot at the given index, or NULL if no slot exists with the
101  *          given index.
102  */
103 const struct bt_mesh_dfu_slot *bt_mesh_dfu_slot_at(uint16_t img_idx);
104 
105 /** @brief Get the committed DFU image slot for the image with the given
106  *         firmware ID.
107  *
108  *  @param fwid     Firmware ID.
109  *  @param fwid_len Firmware ID length.
110  *  @param slot     Slot pointer to fill.
111  *
112  *  @return Slot index on success, or negative error code on failure.
113  */
114 int bt_mesh_dfu_slot_get(const uint8_t *fwid, size_t fwid_len, struct bt_mesh_dfu_slot **slot);
115 
116 /** @brief Get the index in the firmware image list for the given slot.
117  *
118  *  @param slot Slot to find.
119  *
120  *  @return Slot index on success, or negative error code on failure.
121  */
122 int bt_mesh_dfu_slot_img_idx_get(const struct bt_mesh_dfu_slot *slot);
123 
124 /** @brief Iterate through all DFU image slots.
125  *
126  *  Calls the callback for every DFU image slot or until the callback returns
127  *  something other than @ref BT_MESH_DFU_ITER_CONTINUE.
128  *
129  *  @param cb        Callback to call for each slot, or NULL to just count the
130  *                   number of slots.
131  *  @param user_data User data to pass to the callback.
132  *
133  *  @return The number of slots iterated over.
134  */
135 size_t bt_mesh_dfu_slot_foreach(bt_mesh_dfu_slot_cb_t cb, void *user_data);
136