libnm: add internal nmc_client_has_{version_info_v,version_info_capability,capability}() helper

In the end, it turned out I don't need them. They still seem useful,
because they show how to use this API. In particular for how the
bitfield should be parsed.
This commit is contained in:
Thomas Haller
2022-12-06 19:18:41 +01:00
parent a467f55bef
commit bc6098d441
2 changed files with 65 additions and 0 deletions

View File

@@ -135,3 +135,64 @@ nmc_client_new_waitsync(GCancellable *cancellable,
} }
return TRUE; return TRUE;
} }
/*****************************************************************************/
guint32
nmc_client_has_version_info_v(NMClient *nmc)
{
const guint32 *ver;
gsize len;
ver = nm_client_get_version_info(nmc, &len);
if (len < 1)
return 0;
return ver[0];
}
gboolean
nmc_client_has_version_info_capability(NMClient *nmc, NMVersionInfoCapability capability)
{
const guint32 *ver;
gsize len;
gsize idx;
gsize idx_hi;
gsize idx_lo;
ver = nm_client_get_version_info(nmc, &len);
if (len < 2)
return FALSE;
len--;
ver++;
idx = (gsize) capability;
if (idx >= G_MAXSIZE - 31u)
return FALSE;
idx_hi = ((idx + 31u) / 32u);
idx_lo = (idx % 32u);
if (idx_hi > len)
return FALSE;
return NM_FLAGS_ANY(ver[idx_hi], (1ull << idx_lo));
}
gboolean
nmc_client_has_capability(NMClient *nmc, NMCapability capability)
{
const guint32 *caps;
gsize len;
gsize i;
caps = nm_client_get_capabilities(nmc, &len);
for (i = 0; i < len; i++) {
if (caps[i] == capability)
return TRUE;
}
return FALSE;
}

View File

@@ -21,4 +21,8 @@ gboolean nmc_client_new_waitsync(GCancellable *cancellable,
const char *first_property_name, const char *first_property_name,
...); ...);
guint32 nmc_client_has_version_info_v(NMClient *nmc);
gboolean nmc_client_has_version_info_capability(NMClient *nmc, NMVersionInfoCapability capability);
gboolean nmc_client_has_capability(NMClient *nmc, NMCapability capability);
#endif /* __NM_LIBNM_AUX_H__ */ #endif /* __NM_LIBNM_AUX_H__ */