/*
 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved..
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/**
 * DOC: wlan_hdd_add_timestamp.c
 *
 * Implementation for creating sysfs files:
 *
 * dp_pkt_add_ts
 */

#include <wlan_hdd_includes.h>
#include "osif_psoc_sync.h"
#include <wlan_hdd_sysfs.h>
#include <wlan_hdd_sysfs_add_timestamp.h>
#include "qdf_trace.h"

#define MAX_USER_COMMAND_SIZE_TIMESTAMP 512

static int hdd_set_dp_pkt_add_ts_info(char *in_str)
{
	char *str, *token, *substr, *subtoken;
	enum qdf_pkt_supported_proto proto;
	uint16_t port, offset;
	int ret;

	str = in_str;
	while (1) {
		token = strsep(&str, ",");
		if (!token)
			break;

		substr = token;

		/* get protocol */
		subtoken = strsep(&substr, ":");
		if (!subtoken)
			return -EINVAL;

		if (strncmp(subtoken, "TCP", 3) == 0) {
			proto = QDF_PKT_PROTO_TCP;
		} else if (strncmp(subtoken, "UDP", 3) == 0) {
			proto = QDF_PKT_PROTO_UDP;
		} else {
			hdd_err("protocol not supported");
			return -EINVAL;
		}

		/* get destination port value */
		subtoken = strsep(&substr, ":");
		if (!subtoken)
			return -EINVAL;

		ret = kstrtou16(subtoken, 0, &port);
		if (ret) {
			hdd_err("Invalid port value ret %d", ret);
			return -EINVAL;
		}

		if (port < 1 || port > 65535) {
			hdd_err("port not valid");
			return -EINVAL;
		}

		/* get offset */
		subtoken = strsep(&substr, ":");
		if (!subtoken)
			return -EINVAL;

		ret = kstrtou16(subtoken, 0, &offset);
		if (ret) {
			hdd_err("Invalid offset value ret %d", ret);
			return -EINVAL;
		}

		ret = qdf_set_dp_pkt_add_ts_info(proto, port, offset);
		if (ret) {
			hdd_err("pkt info set failed");
			return -EINVAL;
		}
	}
	return 0;
}

static ssize_t
__hdd_sysfs_dp_pkt_add_ts_store(struct hdd_context *hdd_ctx,
				struct kobj_attribute *attr,
				char const *buf, size_t count)
{
	char buf_local[MAX_USER_COMMAND_SIZE_TIMESTAMP + 1];
	char *str;
	int ret;

	if (!wlan_hdd_validate_modules_state(hdd_ctx))
		return -EINVAL;

	ret = hdd_sysfs_validate_and_copy_buf(buf_local, sizeof(buf_local),
					      buf, count);
	if (ret) {
		hdd_err_rl("invalid input");
		return ret;
	}

	str = skip_spaces(buf_local);
	if (strlen(str) == 0) {
		qdf_clear_dp_pkt_add_ts_info();
		return count;
	}

	qdf_clear_dp_pkt_add_ts_info();
	ret = hdd_set_dp_pkt_add_ts_info(str);
	if (ret)
		hdd_err("all protocol do not set successfully");
	return count;
}

static ssize_t hdd_sysfs_dp_pkt_add_ts_store(struct kobject *kobj,
					     struct kobj_attribute *attr,
					     char const *buf, size_t count)
{
	struct osif_psoc_sync *psoc_sync;
	struct hdd_context *hdd_ctx = cds_get_context(QDF_MODULE_ID_HDD);
	ssize_t errno_size;
	int ret;

	ret = wlan_hdd_validate_context(hdd_ctx);
	if (ret != 0)
		return ret;

	errno_size = osif_psoc_sync_op_start(wiphy_dev(hdd_ctx->wiphy),
					     &psoc_sync);
	if (errno_size)
		return errno_size;

	errno_size = __hdd_sysfs_dp_pkt_add_ts_store(hdd_ctx, attr,
						     buf, count);

	osif_psoc_sync_op_stop(psoc_sync);

	return errno_size;
}

static ssize_t
__hdd_sysfs_dp_pkt_add_ts_show(struct hdd_context *hdd_ctx,
			       struct kobj_attribute *attr, char *buf)
{
	if (!wlan_hdd_validate_modules_state(hdd_ctx))
		return -EINVAL;

	return qdf_show_dp_pkt_add_ts_info(buf, PAGE_SIZE);
}

static ssize_t hdd_sysfs_dp_pkt_add_ts_show(struct kobject *kobj,
					    struct kobj_attribute *attr,
					    char *buf)
{
	struct osif_psoc_sync *psoc_sync;
	struct hdd_context *hdd_ctx = cds_get_context(QDF_MODULE_ID_HDD);
	ssize_t errno_size;
	int ret;

	ret = wlan_hdd_validate_context(hdd_ctx);
	if (ret != 0)
		return ret;

	errno_size = osif_psoc_sync_op_start(wiphy_dev(hdd_ctx->wiphy),
					     &psoc_sync);
	if (errno_size)
		return errno_size;

	errno_size = __hdd_sysfs_dp_pkt_add_ts_show(hdd_ctx, attr, buf);

	osif_psoc_sync_op_stop(psoc_sync);

	return errno_size;
}

static struct kobj_attribute dp_pkt_add_ts_attribute =
	__ATTR(dp_pkt_add_ts, 0660, hdd_sysfs_dp_pkt_add_ts_show,
	       hdd_sysfs_dp_pkt_add_ts_store);

int hdd_sysfs_dp_pkt_add_ts_create(struct kobject *driver_kobject)
{
	int error;

	if (!driver_kobject) {
		hdd_err("could not get driver kobject!");
		return -EINVAL;
	}

	error = sysfs_create_file(driver_kobject,
				  &dp_pkt_add_ts_attribute.attr);
	if (error)
		hdd_err("could not create dp_pkt_add_ts sysfs file");

	return error;
}

void
hdd_sysfs_dp_pkt_add_ts_destroy(struct kobject *driver_kobject)
{
	if (!driver_kobject) {
		hdd_err("could not get driver kobject!");
		return;
	}
	sysfs_remove_file(driver_kobject, &dp_pkt_add_ts_attribute.attr);
}
