From e03937fecb74cf50d70721ea2a0b14fa5e12153a Mon Sep 17 00:00:00 2001
From: Gokul Sivakumar <gokulkumar.sivakumar@infineon.com>
Date: Mon, 11 Jul 2022 11:17:04 +0530
Subject: [PATCH 43/49] Add support to configure TWT of a session using offset
 in microseconds

Introduce a new cmd line argument "twt_offset=<u64>" in the existing list
of arguments supported in "$ wpa_cli twt_setup <args>" cmd to set the TWT
in terms of offset from the current remote TSF.

Example: To set a Target Wake Time of 102.4ms from the current remote TSF

$ wpa_cli twt_setup setup_cmd=0 min_twt=80 mantissa=38400 exponent=3 \
	twt_offset=102400 trigger=0 implicit=1 flow_type=0 flow_id=5 \
	control=0


Signed-off-by: Gokul Sivakumar <gokulkumar.sivakumar@infineon.com>
Signed-off-by: Ian Lin <ian.lin@infineon.com>
---
 src/common/ifx_vendor.h           | 3 +++
 src/drivers/driver.h              | 1 +
 src/drivers/driver_nl80211.c      | 4 ++++
 wpa_supplicant/ctrl_iface.c       | 9 +++++++--
 wpa_supplicant/twt.c              | 8 +++++---
 wpa_supplicant/wpa_cli.c          | 2 +-
 wpa_supplicant/wpa_supplicant_i.h | 6 +++---
 7 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/src/common/ifx_vendor.h b/src/common/ifx_vendor.h
index 2e251c367..aa8e83bc7 100644
--- a/src/common/ifx_vendor.h
+++ b/src/common/ifx_vendor.h
@@ -175,6 +175,8 @@ enum ifx_twt_oper {
  *
  * @IFX_VENDOR_ATTR_TWT_PARAM_WAKE_TIME: Target Wake Time.
  *
+ * @IFX_VENDOR_ATTR_TWT_PARAM_WAKE_TIME_OFFSET: Target Wake Time Offset.
+ *
  * @IFX_VENDOR_ATTR_TWT_PARAM_MIN_WAKE_DURATION: Nominal Minimum TWT Wake Duration.
  *
  * @IFX_VENDOR_ATTR_TWT_PARAM_WAKE_INTVL_EXPONENT: TWT Wake Interval Exponent.
@@ -218,6 +220,7 @@ enum ifx_vendor_attr_twt_param {
 	IFX_VENDOR_ATTR_TWT_PARAM_SETUP_CMD_TYPE,
 	IFX_VENDOR_ATTR_TWT_PARAM_DIALOG_TOKEN,
 	IFX_VENDOR_ATTR_TWT_PARAM_WAKE_TIME,
+	IFX_VENDOR_ATTR_TWT_PARAM_WAKE_TIME_OFFSET,
 	IFX_VENDOR_ATTR_TWT_PARAM_MIN_WAKE_DURATION,
 	IFX_VENDOR_ATTR_TWT_PARAM_WAKE_INTVL_EXPONENT,
 	IFX_VENDOR_ATTR_TWT_PARAM_WAKE_INTVL_MANTISSA,
diff --git a/src/drivers/driver.h b/src/drivers/driver.h
index 4d810aaa8..23f599bef 100644
--- a/src/drivers/driver.h
+++ b/src/drivers/driver.h
@@ -2527,6 +2527,7 @@ struct drv_acs_params {
 struct drv_setup_twt_params {
 	u8 dtok;
 	u64 twt;
+	u64 twt_offset;
 	u8 min_twt;
 	u8 exponent;
 	u16 mantissa;
diff --git a/src/drivers/driver_nl80211.c b/src/drivers/driver_nl80211.c
index 8f8f7e2cd..3d98e5943 100644
--- a/src/drivers/driver_nl80211.c
+++ b/src/drivers/driver_nl80211.c
@@ -12328,6 +12328,10 @@ static int wpa_driver_nl80211_setup_twt(void *priv, struct drv_setup_twt_params
 	     nla_put_u64(msg, IFX_VENDOR_ATTR_TWT_PARAM_WAKE_TIME,
 			 params->twt)) ||
 
+	    (params->twt_offset &&
+	     nla_put_u64(msg, IFX_VENDOR_ATTR_TWT_PARAM_WAKE_TIME_OFFSET,
+			 params->twt_offset)) ||
+
 	    nla_put_u8(msg, IFX_VENDOR_ATTR_TWT_PARAM_MIN_WAKE_DURATION,
 		       params->min_twt) ||
 
diff --git a/wpa_supplicant/ctrl_iface.c b/wpa_supplicant/ctrl_iface.c
index 9964df6fc..1d57e393b 100644
--- a/wpa_supplicant/ctrl_iface.c
+++ b/wpa_supplicant/ctrl_iface.c
@@ -9999,6 +9999,7 @@ static int wpas_ctrl_iface_send_twt_setup(struct wpa_supplicant *wpa_s,
 	int mantissa = 8192;
 	u8 min_twt = 255;
 	unsigned long long twt = 0;
+	unsigned long long twt_offset = 0;
 	bool requestor = true;
 	int setup_cmd = 0;
 	bool trigger = true;
@@ -10035,6 +10036,10 @@ static int wpas_ctrl_iface_send_twt_setup(struct wpa_supplicant *wpa_s,
 	if (tok_s)
 		sscanf(tok_s + os_strlen(" twt="), "%llu", &twt);
 
+	tok_s = os_strstr(cmd, " twt_offset=");
+	if (tok_s)
+		sscanf(tok_s + os_strlen(" twt_offset="), "%llu", &twt_offset);
+
 	tok_s = os_strstr(cmd, " requestor=");
 	if (tok_s)
 		requestor = atoi(tok_s + os_strlen(" requestor="));
@@ -10069,8 +10074,8 @@ static int wpas_ctrl_iface_send_twt_setup(struct wpa_supplicant *wpa_s,
 
 #ifdef CONFIG_TWT_OFFLOAD_IFX
 	return wpas_twt_offload_send_setup(wpa_s, dtok, exponent, mantissa,
-					   min_twt, setup_cmd, twt, requestor,
-					   trigger, implicit, flow_type,
+					   min_twt, setup_cmd, twt, twt_offset,
+					   requestor, trigger, implicit, flow_type,
 					   flow_id, protection, twt_channel,
 					   control);
 #else
diff --git a/wpa_supplicant/twt.c b/wpa_supplicant/twt.c
index d653e2db8..b1727603f 100644
--- a/wpa_supplicant/twt.c
+++ b/wpa_supplicant/twt.c
@@ -22,6 +22,7 @@
  * @min_twt: Minimum TWT wake duration in units of 256 usec
  * @setup_cmd: 0 == request, 1 == suggest, etc.  Table 9-297
  * @twt: Target Wake Time
+ * @twt_offset: Target Wake Time TSF offset
  * @requestor: Specify this is a TWT Requesting / Responding STA
  * @trigger: Specify Trigger based / Non-Trigger based TWT Session
  * @implicit: Specify Implicit / Explicit TWT session
@@ -35,9 +36,9 @@
  */
 int wpas_twt_offload_send_setup(struct wpa_supplicant *wpa_s, u8 dtok, int exponent,
 				int mantissa, u8 min_twt, int setup_cmd, u64 twt,
-				bool requestor, bool trigger, bool implicit,
-				bool flow_type, u8 flow_id, bool protection,
-				u8 twt_channel, u8 control)
+				u64 twt_offset, bool requestor, bool trigger,
+				bool implicit, bool flow_type, u8 flow_id,
+				bool protection, u8 twt_channel, u8 control)
 {
 	int ret = 0;
 	struct drv_setup_twt_params params;
@@ -48,6 +49,7 @@ int wpas_twt_offload_send_setup(struct wpa_supplicant *wpa_s, u8 dtok, int expon
 	params.mantissa = (u16)mantissa;
 	params.min_twt = min_twt;
 	params.twt = twt;
+	params.twt_offset = twt_offset;
 	params.requestor = requestor ? 1 : 0;
 	params.trigger = trigger ? 1 : 0;
 	params.implicit = implicit ? 1 : 0;
diff --git a/wpa_supplicant/wpa_cli.c b/wpa_supplicant/wpa_cli.c
index 0f68d8d8a..07160b5da 100644
--- a/wpa_supplicant/wpa_cli.c
+++ b/wpa_supplicant/wpa_cli.c
@@ -3876,7 +3876,7 @@ static const struct wpa_cli_cmd wpa_cli_commands[] = {
 	},
 	{ "twt_setup",
 	  wpa_cli_cmd_twt_setup, NULL, cli_cmd_flag_none,
-	  "[dialog=<token>] [exponent=<exponent>] [mantissa=<mantissa>] [min_twt=<Min TWT>] [setup_cmd=<setup-cmd>] [twt=<u64>] [requestor=0|1] [trigger=0|1] [implicit=0|1] [flow_type=0|1] [flow_id=<3-bit-id>] [protection=0|1] [twt_channel=<twt chanel id>] [control=<control-u8>] = Send TWT Setup frame"
+	  "[dialog=<token>] [exponent=<exponent>] [mantissa=<mantissa>] [min_twt=<Min TWT>] [setup_cmd=<setup-cmd>] [twt=<u64>] [twt_offset=<u64> ][requestor=0|1] [trigger=0|1] [implicit=0|1] [flow_type=0|1] [flow_id=<3-bit-id>] [protection=0|1] [twt_channel=<twt chanel id>] [control=<control-u8>] = Send TWT Setup frame"
 	},
 	{ "twt_teardown",
 	  wpa_cli_cmd_twt_teardown, NULL, cli_cmd_flag_none,
diff --git a/wpa_supplicant/wpa_supplicant_i.h b/wpa_supplicant/wpa_supplicant_i.h
index d34383481..a4fca4b3a 100644
--- a/wpa_supplicant/wpa_supplicant_i.h
+++ b/wpa_supplicant/wpa_supplicant_i.h
@@ -1646,9 +1646,9 @@ int wpas_get_op_chan_phy(int freq, const u8 *ies, size_t ies_len,
 #ifdef CONFIG_TWT_OFFLOAD_IFX
 int wpas_twt_offload_send_setup(struct wpa_supplicant *wpa_s, u8 dtok, int exponent,
 				int mantissa, u8 min_twt, int setup_cmd, u64 twt,
-				bool requestor, bool trigger, bool implicit,
-				bool flow_type, u8 flow_id, bool protection,
-				u8 twt_channel, u8 control);
+				u64 twt_offset, bool requestor, bool trigger,
+				bool implicit, bool flow_type, u8 flow_id,
+				bool protection, u8 twt_channel, u8 control);
 int wpas_twt_offload_send_teardown(struct wpa_supplicant *wpa_s, u8 flags);
 #else
 int wpas_twt_send_setup(struct wpa_supplicant *wpa_s, u8 dtok, int exponent,
-- 
2.17.1

