elemy-utils 1.0.0
psl_ndx.h
Go to the documentation of this file.
1
14#ifndef LIBELUTILS_ELUTILS_PSL_NDX_H_
15#define LIBELUTILS_ELUTILS_PSL_NDX_H_
16
17#include "elutils/c_decls.h"
18__BEGIN_DECLS /* till __END_DECLS */
19
20typedef struct _psl_ndx_t {
21 int prev;
22 int next;
24static inline void psl_ndx_init(psl_ndx_t *ndx) {ndx->prev = ndx->next = -1;}
25
29#define PSL_NDX_ELEM_NEXT(ARR_BASE, LINK_FIELD_NAME, NDX) (ARR_BASE[NDX].LINK_FIELD_NAME.next)
30#define PSL_NDX_ELEM_PREV(ARR_BASE, LINK_FIELD_NAME, NDX) (ARR_BASE[NDX].LINK_FIELD_NAME.prev)
31
32#define PSL_NDX_ELEM_PREV_PTR(ARR_BASE, LINK_FIELD_NAME, NDX) (ARR_BASE[NDX].LINK_FIELD_NAME.prev != -1? &(ARR_BASE[ARR_BASE[NDX].LINK_FIELD_NAME.prev]) : NULL)
33#define PSL_NDX_ELEM_NEXT_PTR(ARR_BASE, LINK_FIELD_NAME, NDX) (ARR_BASE[NDX].LINK_FIELD_NAME.prev != -1? &(ARR_BASE[ARR_BASE[NDX].LINK_FIELD_NAME.next]) : NULL)
34
35
36static inline void psl_ndx_add_head(psl_ndx_t *base, psl_ndx_t *new_node, psl_ndx_t *next_node, int new_ndx)
37{
38 new_node->next = base->next;
39 new_node->prev = next_node->prev;
40 next_node->prev = new_ndx;
41 base->next = new_ndx;
42} //void psl_ndx_add_head(psl_ndx_t *base, psl_ndx_t *new_node, psl_ndx_t *next_node, int new_ndx) =================
43
44static inline void psl_ndx_add_tail(psl_ndx_t *base, psl_ndx_t *new_node, psl_ndx_t *prev_node, int new_ndx)
45{
46 new_node->prev = base->prev;
47 new_node->next = prev_node->next;
48 prev_node->next = new_ndx;
49 base->prev = new_ndx;
50} //void psl_ndx_add_head(psl_ndx_t *base, psl_ndx_t *new_node, psl_ndx_t *next_node, int new_ndx) =================
51
52
53//how to use ndx list
55//
57// * \brief add sensor <b>sensor_ndx</b> to node <b>node_ndx</b> at head
58// *
59// * Warning! NO checks.
60// */
61//void node_sensors_list_add_head(sensors_config_t *sc, int node_ndx, int sensor_ndx)
62//{
63// sensor_config_node_t *node = &sc->sensor_config_nodes[node_ndx];
64// if (node->sensors_list_cnt < 1) {
65// MB_DBG("This func work when have at least 1 element in list.");
66// return;
67// }
68//
69// io_config_t *add_io = &sc->io_configs[sensor_ndx];
70// io_config_t *t_io = &sc->io_configs[node->sensors_list.next];
71//
72//#pragma GCC diagnostic push
73//#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
74// psl_ndx_add_head(&node->sensors_list, &add_io->node_link, &t_io->node_link, sensor_ndx);
75//#pragma GCC diagnostic pop
76// node->sensors_list_cnt++;
77//
78// MB_DBG("set node(%i:%i) io[%i](%i:%i) prev io[%i](%i:%i)\n", node->sensors_list.prev, node->sensors_list.next, sensor_ndx, add_io->node_link.prev, add_io->node_link.next, add_io->node_link.prev,
79// t_io->node_link.prev, t_io->node_link.next);
80//} //void node_sensors_list_add_head(sensors_config_t *sc, int node_ndx, int sensor_ndx) =================
81//
83// * \brief add sensor <b>sensor_ndx</b> to node <b>node_ndx</b> at tail
84// *
85// * Warning! NO checks.
86// */
87//void node_sensors_list_add_tail(sensors_config_t *sc, int node_ndx, int sensor_ndx)
88//{
89// sensor_config_node_t *node = &sc->sensor_config_nodes[node_ndx];
90// if (node->sensors_list_cnt < 1) {
91// MB_DBG("This func work when have at least 1 element in list.");
92// return;
93// }
94//
95// io_config_t *add_io = &sc->io_configs[sensor_ndx];
96// io_config_t *t_io = &sc->io_configs[node->sensors_list.prev];
97//
98//#pragma GCC diagnostic push
99//#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
100// psl_ndx_add_tail(&node->sensors_list, &add_io->node_link, &t_io->node_link, sensor_ndx);
101//#pragma GCC diagnostic pop
102// node->sensors_list_cnt++;
103//
104// MB_DBG("set node(%i:%i) io[%i](%i:%i) prev io[%i](%i:%i)\n", node->sensors_list.prev, node->sensors_list.next, sensor_ndx, add_io->node_link.prev, add_io->node_link.next, add_io->node_link.prev,
105// t_io->node_link.prev, t_io->node_link.next);
106//} //void node_sensors_list_add_tail(sensors_config_t *sc, int node_ndx, int sensor_ndx) =================
107//
109// * \brief insert sensor into node's list according address which can be addr_sens_t or addr_disc_t
110// * \param sc ptr to global sensors config
111// * \return 0 on success
112// * \return -1 on error (not insert)
113// */
114//int node_sensors_list_insert(sensors_config_t *sc, int node_ndx, int io_ndx)
115//{
116// if (sc == NULL) {
117// MB_DBG("DBG: sensors confog == NULL\n");
118// return -1;
119// }
120// if (node_ndx < 0 || node_ndx >= sc->sensor_config_nodes_cnt) {
121// MB_DBG("node index exceed limits\n");
122// return -1;
123// }
124//
125// if (io_ndx < 0 || io_ndx >= sc->io_configs_cnt) {
126// MB_DBG("io index exceed limits\n");
127// return -1;
128// }
129//
130// sensor_config_node_t *node = &sc->sensor_config_nodes[node_ndx];
131// io_config_t *io = &sc->io_configs[io_ndx];
132//
133// //io_config_t *t_io;
134// int rcl;
135// cmp_io_addr_t addr_cmper;
136// int is_analog = 0;
137//
138// node_addr_t sens_node_addr = ((sens_addr_t*) &io->aio.addr)->addr;
139//
140// if (sens_node_addr.value != node->nodeInfo.addr.value) {
141// MB_DBG("DBG: node address of IO not match node address.\n");
142// return -1;
143// }
144//
145// if (node->sensors_list_cnt == 0) {
146// io->node_link.next = -1;
147// io->node_link.prev = -1;
148// node->sensors_list.next = io_ndx;
149// node->sensors_list.prev = io_ndx;
150// node->sensors_list_cnt = 1;
151// //sc_print_node_io(sc, node_ndx, "First element");
152// return 0;
153// }
154//
155// if (io->type == SN_VAL_ANALOG) {
156// addr_cmper = cmp_addr_sens;
157// is_analog = 1;
158// }
159// else if (io->type == SN_VAL_DISCRETE) {
160// addr_cmper = cmp_addr_disc;
161// is_analog = 0;
162// }
163// else {
164// MB_DBG("WRONG IO type '%i'\n", io->type);
165// return -1;
166// }
167//
168// //list_cnt >1 //node->sensors_list_cnt >= 1
169// if (io->type == SN_VAL_ANALOG) {
170// /// search posiotion to insert. 3 variants: put at first, put between, put last.
171// int t_ndx;
172// int t_i = node->sensors_list.next;
173// io_config_t *t_io = &sc->io_configs[t_i];
174//
175// rcl = cmp_addr_sens(&io->aio.addr, &t_io->aio.addr);
176// if (rcl == 0) {
177// print_addr_sens(&io->aio.addr, "Analog Node already have sensor with this address");
178// return -1;
179// }
180// //if addr of first element in list > io.addr then insert io befor t_io
181// if (rcl < 0) {
182// //node_sensors_list_add_head(sc, node_ndx, io_ndx)
185// psl_ndx_add_head(&node->sensors_list, &io->node_link, &t_io->node_link, io_ndx);
186// node->sensors_list_cnt++;
188// return 0;
189// }
190// //put io in list after current list element (first)
191// //go search
192// //here: node - ptr to node with need addr, io - ptr to new IO, ndx - index if new IO, t_io current IO
193// while (1) {
194// if (t_io->node_link.next == -1) {
195// //io.addr > t_io.addr of last element in list
199//#pragma GCC diagnostic push
200//#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
201// psl_ndx_add_tail(&node->sensors_list, &io->node_link, &t_io->node_link, io_ndx);
202// node->sensors_list_cnt++;
203//#pragma GCC diagnostic pop
207// return 0;
208// }
209//
210// io_config_t *t_ion = &sc->io_configs[t_io->node_link.next];
211// rcl = cmp_addr_sens(&io->aio.addr, &t_ion->aio.addr);
212// if (rcl == 0) {
213// print_addr_sens(&io->aio.addr, "Node already have sensor with this address");
214// return -1;
215// }
216//
217// if (rcl < 0) {
219//#pragma GCC diagnostic push
220//#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
221// psl_ndx_add_head(&t_io->node_link, &io->node_link, &t_ion->node_link, io_ndx);
222// node->sensors_list_cnt++;
223//#pragma GCC diagnostic pop
224// return 0;
225// }
226// //step to next element in list
227// t_io = t_ion;
228// } //walk through IO list in node ===
229// print_addr_sens(&io->aio.addr, "ANALOG IMPOSSIBLE");
230// } //if (io->type == SN_VAL_ANALOG) =========
231// else if (io->type == SN_VAL_DISCRETE) {
232// /// search posiotion to insert. 3 variants: put at first, put between, put last.
233// int t_ndx;
234// int t_i = node->sensors_list.next;
235// io_config_t *t_io = &sc->io_configs[t_i];
236//
237// rcl = cmp_addr_disc(&io->dio.addr, &t_io->dio.addr);
238// if (rcl == 0) {
239// print_addr_disc(&io->dio.addr, "Node already have sensor with this address");
240// return -1;
241// }
242// //if addr of first element in list > io.addr then insert io befor t_io
243// if (rcl < 0) {
244// //node_sensors_list_add_head(sc, node_ndx, io_ndx)
245//#pragma GCC diagnostic push
246//#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
247// psl_ndx_add_head(&node->sensors_list, &io->node_link, &t_io->node_link, io_ndx);
248// node->sensors_list_cnt++;
249//#pragma GCC diagnostic pop
250// return 0;
251// }
252// //put io in list after current list element (first)
253// //go search
254// //here: node - ptr to node with need addr, io - ptr to new IO, ndx - index if new IO, t_io current IO
255// while (1) {
256// if (t_io->node_link.next == -1) {
257// //io.addr > t_io.addr of last element in list
261//#pragma GCC diagnostic push
262//#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
263// psl_ndx_add_tail(&node->sensors_list, &io->node_link, &t_io->node_link, io_ndx);
264// node->sensors_list_cnt++;
265//#pragma GCC diagnostic pop
269// return 0;
270// }
271//
272// io_config_t *t_ion = &sc->io_configs[t_io->node_link.next];
273// rcl = cmp_addr_disc(&io->dio.addr, &t_ion->dio.addr);
274// if (rcl == 0) {
275// print_addr_disc(&io->dio.addr, "Node already have sensor with this address");
276// return -1;
277// }
278//
279// if (rcl < 0) {
281//#pragma GCC diagnostic push
282//#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
283// psl_ndx_add_head(&t_io->node_link, &io->node_link, &t_ion->node_link, io_ndx);
284// node->sensors_list_cnt++;
285//#pragma GCC diagnostic pop
286// return 0;
287// }
288// //step to next element in list
289// t_io = t_ion;
290// } //walk through IO list in node ===
291// print_addr_disc(&io->dio.addr, "DISCRETE IMPOSSIBLE");
292// } //if (io->type == SN_VAL_DISCRETE) ==========
293// else {
294// MB_DBG("Once more wrong sensor type\n");
295// } //if (node->nodeInfo.addr.value != io->)===================================================
296//
297// return 0;
298//} //void node_sensors_list_insert(sensors_config_t *sc, int node_ndx, int sensor_ndx) =====================
299//
301
303#endif /* LIBELUTILS_ELUTILS_PSL_NDX_H_ */
#define __END_DECLS
Definition: c_decls.h:10
#define __BEGIN_DECLS
Definition: c_decls.h:9
static void psl_ndx_init(psl_ndx_t *ndx)
Definition: psl_ndx.h:24
static void psl_ndx_add_head(psl_ndx_t *base, psl_ndx_t *new_node, psl_ndx_t *next_node, int new_ndx)
Definition: psl_ndx.h:36
__BEGIN_DECLS struct _psl_ndx_t psl_ndx_t
static void psl_ndx_add_tail(psl_ndx_t *base, psl_ndx_t *new_node, psl_ndx_t *prev_node, int new_ndx)
Definition: psl_ndx.h:44
Definition: psl_ndx.h:20
int prev
Definition: psl_ndx.h:21
int next
Definition: psl_ndx.h:22