Commit 77e522a5 authored by Jukka Rissanen's avatar Jukka Rissanen Committed by Carles Cufi
Browse files

net: context: Refactor option setters



Set separate option setters for bool, uint8_t and uint16_t
values. Use those generic setters when storing the desired
option value.

The uint16_t option setter stores the value to uint16_t variable
and expects that user supplies int value.

For uint8_t value, it is expected that uint8_t value is supplied
instead of int.

Signed-off-by: default avatarJukka Rissanen <jukka.rissanen@nordicsemi.no>
parent 55958d85
Loading
Loading
Loading
Loading
+60 −101
Original line number Diff line number Diff line
@@ -2481,33 +2481,75 @@ int net_context_update_recv_wnd(struct net_context *context,
	return ret;
}

static int set_context_priority(struct net_context *context,
				const void *value, size_t len)
__maybe_unused static int set_bool_option(bool *option, const void *value, size_t len)
{
#if defined(CONFIG_NET_CONTEXT_PRIORITY)
	if (value == NULL) {
		return -EINVAL;
	}

	if (len != sizeof(int)) {
		return -EINVAL;
	}

	*option = !!*((int *)value);

	return 0;
}

__maybe_unused static int set_uint8_option(uint8_t *option, const void *value, size_t len)
{
	if (value == NULL) {
		return -EINVAL;
	}

	if (len > sizeof(uint8_t)) {
		return -EINVAL;
	}

	context->options.priority = *((uint8_t *)value);
	*option = *((uint8_t *)value);

	return 0;
#else
	return -ENOTSUP;
#endif
}

static int set_context_txtime(struct net_context *context,
			      const void *value, size_t len)
__maybe_unused static int set_uint16_option(uint16_t *option, const void *value, size_t len)
{
#if defined(CONFIG_NET_CONTEXT_TXTIME)
	int v;

	if (value == NULL) {
		return -EINVAL;
	}

	if (len != sizeof(int)) {
		return -EINVAL;
	}

	context->options.txtime = !!*((int *)value);
	v = *((int *)value);

	if (v < 0 || v > UINT16_MAX) {
		return -EINVAL;
	}

	*option = (uint16_t)v;

	return 0;

}

static int set_context_priority(struct net_context *context,
				const void *value, size_t len)
{
#if defined(CONFIG_NET_CONTEXT_PRIORITY)
	return set_uint8_option(&context->options.priority, value, len);
#else
	return -ENOTSUP;
#endif
}

static int set_context_txtime(struct net_context *context,
			      const void *value, size_t len)
{
#if defined(CONFIG_NET_CONTEXT_TXTIME)
	return set_bool_option(&context->options.txtime, value, len);
#else
	return -ENOTSUP;
#endif
@@ -2572,19 +2614,7 @@ static int set_context_rcvbuf(struct net_context *context,
				const void *value, size_t len)
{
#if defined(CONFIG_NET_CONTEXT_RCVBUF)
	int rcvbuf_value = *((int *)value);

	if (len != sizeof(int)) {
		return -EINVAL;
	}

	if ((rcvbuf_value < 0) || (rcvbuf_value > UINT16_MAX)) {
		return -EINVAL;
	}

	context->options.rcvbuf = (uint16_t) rcvbuf_value;

	return 0;
	return set_uint16_option(&context->options.rcvbuf, value, len);
#else
	return -ENOTSUP;
#endif
@@ -2594,18 +2624,7 @@ static int set_context_sndbuf(struct net_context *context,
				const void *value, size_t len)
{
#if defined(CONFIG_NET_CONTEXT_SNDBUF)
	int sndbuf_value = *((int *)value);

	if (len != sizeof(int)) {
		return -EINVAL;
	}

	if ((sndbuf_value < 0) || (sndbuf_value > UINT16_MAX)) {
		return -EINVAL;
	}

	context->options.sndbuf = (uint16_t) sndbuf_value;
	return 0;
	return set_uint16_option(&context->options.sndbuf, value, len);
#else
	return -ENOTSUP;
#endif
@@ -2615,19 +2634,7 @@ static int set_context_dscp_ecn(struct net_context *context,
				const void *value, size_t len)
{
#if defined(CONFIG_NET_CONTEXT_DSCP_ECN)
	int dscp_ecn = *((int *)value);

	if (len != sizeof(int)) {
		return -EINVAL;
	}

	if ((dscp_ecn < 0) || (dscp_ecn > UINT8_MAX)) {
		return -EINVAL;
	}

	context->options.dscp_ecn = (uint8_t)dscp_ecn;

	return 0;
	return set_uint8_option(&context->options.dscp_ecn, value, len);
#else
	return -ENOTSUP;
#endif
@@ -2637,19 +2644,7 @@ static int set_context_reuseaddr(struct net_context *context,
				 const void *value, size_t len)
{
#if defined(CONFIG_NET_CONTEXT_REUSEADDR)
	bool reuseaddr = false;

	if (len != sizeof(int)) {
		return -EINVAL;
	}

	if (*((int *) value) != 0) {
		reuseaddr = true;
	}

	context->options.reuseaddr = reuseaddr;

	return 0;
	return set_bool_option(&context->options.reuseaddr, value, len);
#else
	return -ENOTSUP;
#endif
@@ -2659,19 +2654,7 @@ static int set_context_reuseport(struct net_context *context,
				 const void *value, size_t len)
{
#if defined(CONFIG_NET_CONTEXT_REUSEPORT)
	bool reuseport = false;

	if (len != sizeof(int)) {
		return -EINVAL;
	}

	if (*((int *) value) != 0) {
		reuseport = true;
	}

	context->options.reuseport = reuseport;

	return 0;
	return set_bool_option(&context->options.reuseport, value, len);
#else
	return -ENOTSUP;
#endif
@@ -2681,19 +2664,7 @@ static int set_context_ipv6_v6only(struct net_context *context,
				   const void *value, size_t len)
{
#if defined(CONFIG_NET_IPV4_MAPPING_TO_IPV6)
	bool v6only = false;

	if (len != sizeof(int)) {
		return -EINVAL;
	}

	if (*((int *) value) != 0) {
		v6only = true;
	}

	context->options.ipv6_v6only = v6only;

	return 0;
	return set_bool_option(&context->options.ipv6_v6only, value, len);
#else
	ARG_UNUSED(context);
	ARG_UNUSED(value);
@@ -2707,19 +2678,7 @@ static int set_context_recv_pktinfo(struct net_context *context,
				    const void *value, size_t len)
{
#if defined(CONFIG_NET_CONTEXT_RECV_PKTINFO)
	bool pktinfo = false;

	if (len != sizeof(int)) {
		return -EINVAL;
	}

	if (*((int *) value) != 0) {
		pktinfo = true;
	}

	context->options.recv_pktinfo = pktinfo;

	return 0;
	return set_bool_option(&context->options.recv_pktinfo, value, len);
#else
	ARG_UNUSED(context);
	ARG_UNUSED(value);