/* NOTE: This is quite an ineffective algorithm as it might need to look through the array of nodes N+2 times. A more effective algorithm could be used if the nodes were stored in a sorted list.  */
static uint16_t find_lowest_free_addr(uint8_t num_elem)
/* NOTE: This is quite an ineffective algorithm as it might need to look through the array of nodes N+2 times. A more effective algorithm could be used if the nodes were stored in a sorted list.  */
static uint16_t find_lowest_free_addr(uint8_t num_elem)
{
	uint16_t addr = bt_mesh_cdb.lowest_avail_addr;
	uint16_t next;
	int err, i;
	for (i = 0; i < ARRAY_SIZE(bt_mesh_cdb.nodes) + 2; ++i) {
		err = addr_is_free(addr, num_elem, &next);
		if (err == 0) {
			break;
		} else if (err != -EAGAIN) {
			addr = BT_MESH_ADDR_UNASSIGNED;
			break;
		}
		addr = next;
	}
	return addr;
}