Enable or disable TWT with notification

I am studying Lesson 6/wifi fund_1ess6uexer2 and I can run the provided code normally.

(academy.nordicsemi.com/.../)
But I still want to control whether to send packets by pressing button 2 when turning off the TWT function.
As shown in step 3 of the diagram, when I turn off TWT, I press button 2 to enable the send package, but no data packet is sent. I tried to modify the code as shown in step 4 of the diagram, but couldn't. Could you please provide some help and guidance? Thank you very much!

Parents
  • Hi,

    This is due to how the exercise is implemented in such a way that it only sends a message during the awake state when TWT is enabled:

    static void twt_mgmt_event_handler(struct net_mgmt_event_callback *cb, uint32_t mgmt_event,
    				   struct net_if *iface)
    {
    	switch (mgmt_event) {
    	/* STEP 3.2.1 - Upon a TWT event, call handle_wifi_twt_event() to handle the response */
    	case NET_EVENT_WIFI_TWT:
    		handle_wifi_twt_event(cb);
    		break;
    	/* STEP 3.2.2 -	Upon TWT sleep state event, inform the user of the current sleep state.
    	 * When the device is in the awake state, send a packet to the server and check for any
    	 * received packets if sending packets is enabled. */
    	case NET_EVENT_WIFI_TWT_SLEEP_STATE:
    		int *twt_state;
    		twt_state = (int *)(cb->info);
    		LOG_INF("TWT sleep state: %s", *twt_state ? "awake" : "sleeping");
    		if ((*twt_state == WIFI_TWT_STATE_AWAKE) & sending_packets_enabled) {
    			send_packet();
    			receive_packet();
    		}
    		break;
    	}
    }

    If you want to be able to send packets when TWT is not enabled, you can call the function to send the packet directly in the button_handler() or schedule that it should be called. This code snippet shows how to call it directly in the button_handler():

    static void button_handler(uint32_t button_state, uint32_t has_changed)
    {
    	uint32_t button = button_state & has_changed;
    
    	/* STEP 5.1 - Call wifi_set_twt() to enable or disable TWT when button 1 is pressed. */
    	if (button & DK_BTN1_MSK) {
    		wifi_set_twt();
    	}
    
    	/* STEP 5.2 - Enable or disable sending packets during TWT awake when button 2 is pressed.
    	 */
    	if (button & DK_BTN2_MSK) {
    		send_packet();
    	}
    }

    If you want to schedule it, you can take a look at the TWT example here: https://github.com/martelmy/NCS_examples/blob/main/wifi/twt_provisioning_demo. In this example, pressing button 2 will schedule a ping that will either be sent immediately when TWT is not enabled or during the next wakeup when TWT is enabled. You can use work queue to schedule the UDP packet in the DevAcademy exercise in a similar way.
    Please note that this example is not official and should be considered provided “as-is”.

    Best regards,
    Marte

  • Thank you, . Your guidance is detailed and easy to understand.

    I still have questions, as shown in the picture, TWT mode and DTIM mode. How is the peak in the image caused in TWT mode? 

  • OK, I see. As shown in the picture 1 and 2, the listen interval is 0x000a, and the beacon interval is 100ms.

    The sta will wake up 10 beacons(about 1 second ) to check if AP has data buffered for it.

    but why it is 100ms in the measurement of picture 3:

    picture 1:

    picture 2:

    picture 3:

    The capture package:

    listen interval .pcapng

  • Hi,

    Have you configured the device to use DTIM or listen interval as wakeup mode? You can change this by sending a wakeup mode request, as described in step 5 in Lesson 6, exercise 1 - Enabling power save modes in the Wi-Fi Fundamental course.

    Best regards,
    Marte

  • no, I am not, it is the default.

    so, why capture  listen interval is 0x000a (10 beacon about 1 second), but the picture 3 is 100ms

    are there some misunderstandings?

    the code is from:

    github.com/.../main.c

  • Hi,

    Because the device is using DTIM and not listen interval. If you want the device to use listen interval as wakeup mode you must change it by sending a wakeup mode request to the AP. You can do so like this:

    struct net_if *iface = net_if_get_default();
    struct wifi_ps_params params = { 0 };
    
    params.wakeup_mode = WIFI_PS_WAKEUP_MODE_LISTEN_INTERVAL;
    params.type = WIFI_PS_PARAM_WAKEUP_MODE;
    
    if (net_mgmt(NET_REQUEST_WIFI_PS, iface, &params, sizeof(params))) {
    	LOG_ERR("Setting wakeup mode failed. Reason %s",
    		wifi_ps_get_config_err_code_str(params.fail_reason));
    	return -1;
    }

    Best regards,
    Marte

  • hello, ,I want to send voice traffic, game traffic, or video traffic through TWT. Do you have any examples of this?

Reply Children
No Data
Related