Commit 0f126a7a authored by Krishna T's avatar Krishna T Committed by Fabio Baltieri
Browse files

wifi_mgmt: Add new API for Wi-Fi statistics



Networking statistics framework is used to define handler and the data
structure, Wi-Fi management layer implements the handler and also adds a
new offload API to get statistics from the Wi-Fi driver.

Signed-off-by: default avatarKrishna T <krishna.t@nordicsemi.no>
parent 1ecca688
Loading
Loading
Loading
Loading
+32 −1
Original line number Diff line number Diff line
@@ -472,6 +472,29 @@ struct net_stats_ppp {
	net_stats_t chkerr;
};

/**
 * @brief All Wi-Fi management statistics
 */
struct net_stats_sta_mgmt {
	/** Number of received beacons */
	net_stats_t beacons_rx;

	/** Number of missed beacons */
	net_stats_t beacons_miss;
};

/**
 * @brief All Wi-Fi specific statistics
 */
struct net_stats_wifi {
	struct net_stats_sta_mgmt sta_mgmt;
	struct net_stats_bytes bytes;
	struct net_stats_pkts pkts;
	struct net_stats_pkts broadcast;
	struct net_stats_pkts multicast;
	struct net_stats_pkts errors;
};

#if defined(CONFIG_NET_STATISTICS_USER_API)
/* Management part definitions */

@@ -493,7 +516,8 @@ enum net_request_stats_cmd {
	NET_REQUEST_STATS_CMD_GET_TCP,
	NET_REQUEST_STATS_CMD_GET_ETHERNET,
	NET_REQUEST_STATS_CMD_GET_PPP,
	NET_REQUEST_STATS_CMD_GET_PM
	NET_REQUEST_STATS_CMD_GET_PM,
	NET_REQUEST_STATS_CMD_GET_WIFI,
};

#define NET_REQUEST_STATS_GET_ALL				\
@@ -581,6 +605,13 @@ NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_PPP);
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_PM);
#endif /* CONFIG_NET_STATISTICS_POWER_MANAGEMENT */

#if defined(CONFIG_NET_STATISTICS_WIFI)
#define NET_REQUEST_STATS_GET_WIFI				\
	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_WIFI)

NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_WIFI);
#endif /* CONFIG_NET_STATISTICS_WIFI */

/**
 * @}
 */
+3 −0
Original line number Diff line number Diff line
@@ -174,6 +174,9 @@ struct net_wifi_mgmt_offload {
			 struct wifi_connect_req_params *params);
	int (*ap_disable)(const struct device *dev);
	int (*iface_status)(const struct device *dev, struct wifi_iface_status *status);
#ifdef CONFIG_NET_STATISTICS_WIFI
	int (*get_stats)(const struct device *dev, struct net_stats_wifi *stats);
#endif /* CONFIG_NET_STATISTICS_WIFI */
};

/* Make sure that the network interface API is properly setup inside
+9 −0
Original line number Diff line number Diff line
@@ -125,4 +125,13 @@ config NET_STATISTICS_POWER_MANAGEMENT
	  This will provide how many time a network interface went
	  suspended, for how long the last time and on average.

config NET_STATISTICS_WIFI
	bool "Wi-Fi statistics"
	depends on NET_L2_WIFI_MGMT
	default y
	help
	  Keep track of Wi-Fi related statistics. Note that this
	  requires support from the Wi-Fi driver. The driver needs
	  to collect the statistics.

endif # NET_STATISTICS
+29 −0
Original line number Diff line number Diff line
@@ -199,3 +199,32 @@ void wifi_mgmt_raise_iface_status_event(struct net_if *iface,
					iface, iface_status,
					sizeof(struct wifi_iface_status));
}

#ifdef CONFIG_NET_STATISTICS_WIFI
static int wifi_iface_stats(uint32_t mgmt_request, struct net_if *iface,
			  void *data, size_t len)
{
	int ret;
	const struct device *dev = net_if_get_device(iface);
	struct net_wifi_mgmt_offload *off_api =
		(struct net_wifi_mgmt_offload *) dev->api;
	struct net_stats_wifi *stats = data;

	if (off_api == NULL || off_api->get_stats == NULL) {
		return -ENOTSUP;
	}

	if (!data || len != sizeof(*stats)) {
		return -EINVAL;
	}

	ret = off_api->get_stats(dev, stats);

	if (ret) {
		return ret;
	}

	return 0;
}
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_WIFI, wifi_iface_stats);
#endif /* CONFIG_NET_STATISTICS_WIFI */
+50 −0
Original line number Diff line number Diff line
@@ -333,6 +333,55 @@ static int cmd_wifi_status(const struct shell *sh, size_t argc, char *argv[])
	return 0;
}


#if defined(CONFIG_NET_STATISTICS_WIFI) && \
					defined(CONFIG_NET_STATISTICS_USER_API)
static void print_wifi_stats(struct net_if *iface, struct net_stats_wifi *data,
			    const struct shell *sh)
{
	shell_fprintf(sh, SHELL_NORMAL, "Statistics for Wi-Fi interface %p [%d]\n", iface,
	       net_if_get_by_iface(iface));

	shell_fprintf(sh, SHELL_NORMAL, "Bytes received   : %u\n", data->bytes.received);
	shell_fprintf(sh, SHELL_NORMAL, "Bytes sent       : %u\n", data->bytes.sent);
	shell_fprintf(sh, SHELL_NORMAL, "Packets received : %u\n", data->pkts.rx);
	shell_fprintf(sh, SHELL_NORMAL, "Packets sent     : %u\n", data->pkts.tx);
	shell_fprintf(sh, SHELL_NORMAL, "Bcast received   : %u\n", data->broadcast.rx);
	shell_fprintf(sh, SHELL_NORMAL, "Bcast sent       : %u\n", data->broadcast.tx);
	shell_fprintf(sh, SHELL_NORMAL, "Mcast received   : %u\n", data->multicast.rx);
	shell_fprintf(sh, SHELL_NORMAL, "Mcast sent       : %u\n", data->multicast.tx);
	shell_fprintf(sh, SHELL_NORMAL, "Beacons received : %u\n",	data->sta_mgmt.beacons_rx);
	shell_fprintf(sh, SHELL_NORMAL, "Beacons missed   : %u\n",
				data->sta_mgmt.beacons_miss);
}
#endif /* CONFIG_NET_STATISTICS_WIFI && CONFIG_NET_STATISTICS_USER_API */

static int cmd_wifi_stats(const struct shell *sh, size_t argc, char *argv[])
{
#if defined(CONFIG_NET_STATISTICS_WIFI) && \
					defined(CONFIG_NET_STATISTICS_USER_API)
	struct net_if *iface = net_if_get_default();
	struct net_stats_wifi stats = { 0 };
	int ret;

	ret = net_mgmt(NET_REQUEST_STATS_GET_WIFI, iface,
				&stats, sizeof(stats));
	if (!ret) {
		print_wifi_stats(iface, &stats, sh);
	}
#else
	ARG_UNUSED(argc);
	ARG_UNUSED(argv);

	shell_fprintf(sh, SHELL_INFO, "Set %s to enable %s support.\n",
		"CONFIG_NET_STATISTICS_WIFI and CONFIG_NET_STATISTICS_USER_API",
		"statistics");
#endif /* CONFIG_NET_STATISTICS_WIFI && CONFIG_NET_STATISTICS_USER_API */

	return 0;
}


static int cmd_wifi_ap_enable(const struct shell *shell, size_t argc,
			      char *argv[])
{
@@ -395,6 +444,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(wifi_commands,
		  cmd_wifi_disconnect),
	SHELL_CMD(scan, NULL, "Scan Wifi AP", cmd_wifi_scan),
	SHELL_CMD(status, NULL, "Status of Wi-Fi interface", cmd_wifi_status),
	SHELL_CMD(statistics, NULL, "Wi-Fi statistics", cmd_wifi_stats),
	SHELL_CMD(ap, &wifi_cmd_ap, "Access Point mode commands", NULL),
	SHELL_SUBCMD_SET_END
);