23
23
/* *
24
24
* Allows iteration through the list of message headers received in the control parameter of the
25
25
* socket_sendto_control / socket_recvfrom_control methods.
26
+ *
27
+ * @par Members types
28
+ *
29
+ * MsgHeaderIterator works on the list which members are of type nsapi_msghdr_t or other types
30
+ * extending this struct. For example nsapi_pktinfo:
31
+ *
32
+ * @code
33
+ typedef struct nsapi_pktinfo {
34
+ nsapi_msghdr_t hdr;
35
+ nsapi_addr_t ipi_addr;
36
+ int ipi_ifindex;
37
+ void *network_interface;
38
+ } nsapi_pktinfo_t;
39
+ * @endcode
40
+ *
41
+ * There are two requirements for such structs to work well with MsgHeaderIterator:
42
+ * first element needs to of type nsapi_msghdr_t
43
+ * value of the field len of the nsapi_msghdr_t needs to bet set to the size of the whole extending type. For example:
44
+ *
45
+ * @code
46
+ nsapi_pktinfo_t pkt_info;
47
+ pkt_info.hdr.len = sizeof(nsapi_pktinfo_t);
48
+ * @endcode
49
+ *
50
+ * This value is used in the MsgHeaderIterator to calculate proper addresses of the list elements.
51
+ *
52
+ * @par Example
53
+ *
54
+ * Code presenting minimal usage example.
55
+ *
56
+ * @code
57
+ *
58
+ struct default_buffer_t {
59
+ default_buffer_t()
60
+ {
61
+ el1.hdr.len = sizeof(nsapi_pktinfo_t);
62
+ el2.len = sizeof(nsapi_msghdr_t);
63
+ el3.len = sizeof(nsapi_msghdr_t);
64
+ el4.hdr.len = sizeof(nsapi_pktinfo_t);
65
+ }
66
+ nsapi_pktinfo_t el1;
67
+ nsapi_msghdr_t el2;
68
+ nsapi_msghdr_t el3;
69
+ nsapi_pktinfo_t el4;
70
+ };
71
+
72
+ default_buffer buff;
73
+ nsapi_msghdr_t *hdr_p = reinterpret_cast<nsapi_msghdr_t *>(&buff);
74
+
75
+ MsgHeaderIterator it(hdr_p, sizeof(buff));
76
+
77
+ it.has_next() // returns true
78
+ auto p1 = it.next() // returns pointer to el1
79
+ auto p2 = it.next() // returns pointer to el2
80
+ auto p3 = it.next() // returns pointer to el3
81
+ auto p4 = it.next() // returns pointer to el4
82
+
83
+ it.has_next() // returns false
84
+ auto p5 = it.next() // returns nullptr
85
+ * @endcode
86
+ *
87
+ * @note More usage examples are implemented in the `MsgHeaderIterator` unit test
88
+ * in `netsocket/tests/UNITTESTS/NetworkStack/test_MsgHeaderIterator.cpp`
26
89
*/
27
90
28
91
struct MsgHeaderIterator {
29
- // Constructor takes pointer to the first header element and size of the whole list.
92
+ /* * Create a MsgHeaderIterator over given nsapi_msghdr_t list.
93
+ *
94
+ * @param hdr Pointer to the first list element.
95
+ * @param size Size of the whole list.
96
+ */
30
97
MsgHeaderIterator (nsapi_msghdr_t *hdr, nsapi_size_t size) :
31
98
start (hdr),
32
99
current (nullptr ),
33
100
size (size)
34
101
{}
35
102
36
- // Checks if the next address of the iterator is a valid list member.
103
+ /* * Checks if the next address of the iterator is a valid list member.
104
+ *
105
+ * @retval True if the next address is a valid member.
106
+ * @retval False otherwise.
107
+ */
37
108
bool has_next ()
38
109
{
39
110
if (current == nullptr ) {
@@ -55,8 +126,11 @@ struct MsgHeaderIterator {
55
126
return true ;
56
127
}
57
128
58
- // Returns pointer to the next member of the list.
59
- // If next member doesn't exist nullptr is returned.
129
+ /* * Returns next element of the list.
130
+ *
131
+ * @retval nullptr if the list doesn't contain next element.
132
+ * @retval Pointer to the next element otherwise.
133
+ */
60
134
nsapi_msghdr_t *next ()
61
135
{
62
136
if (!has_next ()) {
0 commit comments