This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

LAUNCHXL-CC3235SF: Peer scan and connect in p2p mode(WiFi direct)

Part Number: LAUNCHXL-CC3235SF
Other Parts Discussed in Thread: CC3235SF

https://e2e.ti.com/support/wireless-connectivity/wi-fi-group/wifi/f/wi-fi-forum/997637/launchxl-cc3235sf-p2p-wifi-direct-in-network-terminal

As advised in the link above, I am trying to scan and connect manually for p2p connection between two CC3235SF LaunchPads.

According to 4.5.4 in the swru445, network processor guide document

To retrieve for peers, activate the WiFi direct scan policy and then there are two ways to search for peers, calling the P2P_DEVFOUND event or sl_WlanGetNetworkList API.

I had try two ways to retrieve peers. I can find nearby peers with the P2P_DEVFOUND event, but couldn't find with sl_WlanGetNetworkList (It is return value is 0)

and It is my code.(I modify the 'network_terminal' example)

(WiFi direct policy set ROLE_NEGOTIATE and NEG_INITIATOR_RAND_BACKOFF)

/* start p2p mode */
int32_t _setP2Pparams(void)
{
    int32_t    ret ;
    int32_t    Mode;
    uint8_t    channels[4] = {0};
    uint8_t    nameLen = 0;

    /* Sets role negotiations: P2P intent and negotiation initiator */
    ret =
        sl_WlanPolicySet(SL_WLAN_POLICY_P2P,
                         SL_WLAN_P2P_POLICY(
                             SL_WLAN_P2P_ROLE_NEGOTIATE,
                             SL_WLAN_P2P_NEG_INITIATOR_RAND_BACKOFF),
                         NULL, 0);
    ASSERT_ON_ERROR(ret, WLAN_ERROR);

    // connection policy for auto connection : auto, fast, anyp2p, auto provisioning(not p2p)
//    ret = sl_WlanPolicySet(SL_WLAN_POLICY_CONNECTION, SL_WLAN_CONNECTION_POLICY(1, 0, 1, 0), NULL, 0);
//    ret = sl_WlanPolicySet(SL_WLAN_POLICY_CONNECTION, SL_WLAN_CONNECTION_POLICY(1, 1, 0, 0), NULL, 0);
//    if (ret < 0)
//    {
//        UART_PRINT(
//            "Network Terminal - p2p wlanPolicy(connect) set fail \n");
//    }

    /* Set the P2P device type,
     * which is a part of the discovery process. Default is 1-0050F204-1 */
    ret =
        sl_WlanSet(SL_WLAN_CFG_P2P_PARAM_ID, SL_WLAN_P2P_OPT_DEV_TYPE,
                   strlen(P2P_DEVICE_TYPE),
                   (const  uint8_t *)P2P_DEVICE_TYPE);
    ASSERT_ON_ERROR(ret, WLAN_ERROR);

    /* Configure device listen and operation channels */
    channels[0] = LISTEN_CHANNEL;
    channels[1] = LISTEN_REGULATORY_CLASS;
    channels[2] = OPRA_CHANNEL;
    channels[3] = OPRA_REGULATORY_CLASS;

    ret =
        sl_WlanSet(SL_WLAN_CFG_P2P_PARAM_ID, SL_WLAN_P2P_OPT_CHANNEL_N_REGS,
                   sizeof(channels),
                   channels);
    ASSERT_ON_ERROR(ret, WLAN_ERROR);

    nameLen = sizeof(app_CB.P2P_CB.p2pDeviceName);

    /* Print the device's name */
    ret =
        sl_NetAppGet(SL_NETAPP_DEVICE_ID, SL_NETAPP_DEVICE_URN, &nameLen,
                     (uint8_t*)&app_CB.P2P_CB.p2pDeviceName[0]);
    ASSERT_ON_ERROR(ret, NETAPP_ERROR);

    UART_PRINT("\n\r[p2pstart] : Starting NWP in P2P role..\n\r");

    UART_PRINT("\n\rDevice name: %s\n\r", &app_CB.P2P_CB.p2pDeviceName);

    /* Set role to P2P and perform NWP reset */
    ret = sl_WlanSetMode(ROLE_P2P);
    ASSERT_ON_ERROR(ret, WLAN_ERROR);

    ret = sl_Stop(SL_STOP_TIMEOUT);
    ASSERT_ON_ERROR(ret, DEVICE_ERROR);

    Mode = sl_Start(0, 0, 0);

    if(Mode != ROLE_P2P)
    {
        UART_PRINT(
            "\r\n[p2pstart] : Failed to configure device to P2P mode...\r\n");
        ASSERT_ON_ERROR(Mode, DEVICE_ERROR);
        return(-1);
    }
    else
    {
        app_CB.P2P_CB.P2PsecParams.Key = (signed char *)"";
        app_CB.P2P_CB.P2PsecParams.KeyLen = strlen(
            (const char*)app_CB.P2P_CB.P2PsecParams.Key);
        app_CB.P2P_CB.P2PsecParams.Type = SL_WLAN_SEC_TYPE_P2P_PBC;
        app_CB.Role = ROLE_P2P;
    }

    /* enable scan policy */
    _u32 intervalInSeconds = 10;
    ret = sl_WlanPolicySet(SL_WLAN_POLICY_SCAN, SL_WLAN_SCAN_POLICY(1, 1), (_u8*)&intervalInSeconds, sizeof(intervalInSeconds));
    if (ret < 0)
    {
        UART_PRINT(
                "Network Terminal - p2p wlanPolicy(scan) set fail \n");
    }

    return(ret);
}

/* p2pscan command callback */
int32_t cmdP2PScancallback(void *arg)
{
    int32_t         ret = -1;
    uint8_t         triggeredScanTrials = 0;
    ScanCmd_t       ScanParams;
    _u32 intervalInSeconds = 10, getIntervalTime;
    _u32 length = sizeof(intervalInSeconds);
    _u8 policy = 0;

    /* set Scan policy */
    ret = sl_WlanPolicySet(SL_WLAN_POLICY_SCAN, SL_WLAN_SCAN_POLICY(1, 1), (_u8*)&intervalInSeconds, sizeof(intervalInSeconds));
    if (ret < 0)
    {
        UART_PRINT(
            "Network Terminal - p2p wlanPolicy(scan) set fail %d\n\r", ret);
    }

    ret = sl_WlanPolicyGet(SL_WLAN_POLICY_SCAN, &policy, (_u8*)&getIntervalTime, (_u8*)&length);
    if (ret < 0)
    {
        UART_PRINT(
            "Network Terminal - p2p wlanPolicy(scan) get fail %d\n\r", ret);
    }

    if (policy && SL_WLAN_SCAN_POLICY(0, 0))
    {
        UART_PRINT("scan policy is set to scan disable\n\r");
    }
    else if (policy && SL_WLAN_SCAN_POLICY(0, 1))
    {
        if (policy && SL_WLAN_SCAN_POLICY(1, 1))
        {
            UART_PRINT("scan policy is set to scan all ssid\n\r");
        }
        else
        {
            UART_PRINT("scan policy is set to scan hidden ssid\n\r");
        }
    }
    else if (policy && SL_WLAN_SCAN_POLICY(1, 0))
    {
        UART_PRINT("scan policy is set to scan visible ssid\n\r");
    }

    UART_PRINT("scan interval is %d\n\r", getIntervalTime);

    memset(&ScanParams, 0x0, sizeof(ScanParams));

    ScanParams.numOfentries = MAX_SSID_ENTRIES;

    /* Clear the results buffer */
    memset(&app_CB.gDataBuffer, 0x0, sizeof(app_CB.gDataBuffer));

    /* Get scan results from NWP -
        results would be placed inside the provided buffer */
    ret = sl_WlanGetNetworkList(ScanParams.index,
                                ScanParams.numOfentries,
                                &app_CB.gDataBuffer.netEntries[ScanParams.index]);

    /* If scan policy isn't set, invoking 'sl_WlanGetNetworkList()'
     * for the first time triggers 'one shot' scan.
     * The scan parameters would be according to the system persistent
     * settings on enabled channels.
     * For more information, see: <simplelink user guide, page: pr.>
     */
    if(SL_ERROR_WLAN_GET_NETWORK_LIST_EAGAIN == ret)
    {
        while(triggeredScanTrials < MAX_SCAN_TRAILS)
        {
            /* We wait for one second for the NWP to complete
             the initiated scan and collect results */
            sleep(1);

            /* Collect results form one-shot scans.*/
            UART_PRINT("trigger one-shot scan\n\r");

            /* Get scan results from NWP -
                results would be placed inside the provided buffer */
            ret = sl_WlanGetNetworkList(ScanParams.index,
                                        ScanParams.numOfentries,
                                        &app_CB.gDataBuffer.netEntries[ScanParams.index]);

            if(ret > 0)
            {
                break;
            }
            else
            {
                /* If NWP results aren't ready,
                 try 'MAX_SCAN_TRAILS' attempts to get results */
                triggeredScanTrials++ ;
            }
        }
    }

    if(ret <= 0)
    {
        UART_PRINT("\n\r[scan] : Unable to retrieve the network list %d\n\r", ret);
        return(-1);
    }
    /* Print the result table */
    printScanResults(ret);

    return ret;
}

        /* P2P_DEVFOUND event in slwlanevent handler */
        case SL_WLAN_EVENT_P2P_DEVFOUND:
        {
            UART_PRINT("\n\r[WLAN EVENT] P2P Remote device found\n\r");
            strncpy((char *)app_CB.P2P_CB.p2pPeerDeviceName, (char *)pWlanEvent->Data.P2PDevFound.GoDeviceName, pWlanEvent->Data.P2PDevFound.GoDeviceNameLen);
            app_CB.P2P_CB.p2pPeerDeviceName[pWlanEvent->Data.P2PDevFound.GoDeviceNameLen] = NULL;
            UART_PRINT("[WLAN EVENT] \tName: %s\n\r", app_CB.P2P_CB.p2pPeerDeviceName);
            UART_PRINT("[WLAN EVENT] \tName len: %d\n\r", pWlanEvent->Data.P2PDevFound.GoDeviceNameLen);
            UART_PRINT("[WLAN EVENT] \tMAC: %x:%x:%x:%x:%x:%x\n\r",
                       pWlanEvent->Data.P2PDevFound.Mac[0],
                       pWlanEvent->Data.P2PDevFound.Mac[1],
                       pWlanEvent->Data.P2PDevFound.Mac[2],
                       pWlanEvent->Data.P2PDevFound.Mac[3],
                       pWlanEvent->Data.P2PDevFound.Mac[4],
                       pWlanEvent->Data.P2PDevFound.Mac[5]);
            UART_PRINT("[WLAN EVENT] \tWpsMethod: %d\n\r", pWlanEvent->Data.P2PDevFound.WpsMethod);
            sem_post(&(app_CB.P2P_CB.DeviceFound));
        }

And I tried to connect to the found peer(mysimplelink_Bf in above picture) using sl_WlanConnect, but no response.

How can retrieve(using getnetworklist api) and connect to peer? Please give me some advice.

Thanks.

  • Hi,

    Looking through your code, it seems to be largely correct. It looks like you've largely taken the code from the network terminal example, which should be fine.

    Have you also fully implemented Jesu's suggested changes to network terminal, as linked to in the thread you provided?

    Regards,

    Michael

  • Jesu's suggest is manually connect with hard coding, but SSID of device is change every time when boot.

    so, I added new commands "p2pScan" and "p2pConnect".

    Now, I have succeed in connecting the each CC3235 devices using "p2pConnect" command. I missed that each devices need calling 'sl_WlanConnect' API for connecting.

    But, I still don't understand "sl_WlanGetNetworkList" API's work.

    P2P_DEVFOUND event work well when found devices, but "sl_WlanGetNetworkList" API (called cmdP2PScancallback) is sometimes return 0 even though there are peers around it, and sometimes it can't find some peers, and sometimes it work well.

    Where am I missing?

    Thanks.

  • Hi,

    The scanning process will take time, and if you call the sl_WlanGetNetworkList API immediately after you enable the scan policy, you might not have received advertisements from P2P peers before the scan result API is returned. That is the most likely cause of why you cannot get consistent results in sl_WlanGetNetworkList().

    I suggest using the P2P_DEVFOUND event callback as a reliable method to scan for P2P peers.

    Regards,

    Michael

  • Thank you for reply.

    1. When using the P2P_DEVFOUND event callback, I think that It can't know whether peers still exist (When connect time)

    I want to get a list of peers that are still valid. How should I implement it?

    (I added "sleep(5)" between enabling the scan policy and calling "sl_WlanGetNetworkList", but API result did not change.)

     

    2. And, I try to change device name.

        /* Set the P2P device name */
        uint8_t deviceName[] = "TestDevice";
    
        nameLen = strlen((char*)deviceName);
    
        ret = sl_NetAppSet(SL_NETAPP_DEVICE_ID, SL_NETAPP_DEVICE_URN, nameLen, (uint8_t *)&deviceName[0]);

    According to the NWP guide document (4.5.3.1), device name is compound of the URN and two random characters.

    But my devices are not. It works right?

     

      

    Please give me advice.

    Thanks.

  • Hi,

    Did you change the URN of the CC3235 to "TestDevice"? Testing with my CC32xx launchpad I see the two random characters through my android phone.

    I think using the P2P_DEVFOUND event will be most reliable in your situation, and if you were to connect right after you get that event and see the desired P2P network nearby it should avoid any issue with the peer no longer existing. Restarting the NWP using sl_Stop/sl_Start should get the device to scan again and return that event if needed.

    Regards,

    Michael

  • Thank you for reply.

     

    Did you change the URN of the CC3235 to "TestDevice"?

    Yes, I changed the URN to "TestDevice". Also I try to change the URL to another sting, it is the same(It is not generate random characters.)

    My code is written in previous reply.

     

    Restarting the NWP using sl_Stop/sl_Start should get the device to scan again and return that event if needed.

    I'll try this solution.

     

    Thanks.

  • Any update?

    Still random characters are not created after the device name. Device name is only Device URN.

    And could I get the code you tested?

    Thanks.

  • Hi,

    Michael has been out of the office for a few days and will be back starting tomorrow. He will be able to follow-up soon after he gets back.

    Best Regards,

    Ben M

  • I'm still waiting for your answer...

  • Hi,

    The code I ran for testing is simply the p2pstart code in the network_terminal example of the SDK, as-is and without modification. So my URN is still the default. If you run the default network_terminal example, are you able to get those random characters?

    Also, do you get the P2P_DEVFOUND again if you perform an sl_Stop/sl_Start?

    Regards,

    Michael

  • If you run the default network_terminal example, are you able to get those random characters?

    Yes, I can get random characters when default network_terminal example.

    But, if I change the device URN using "sl_NetAppSet" api, It isn't.

    Is it correct that random characters do not occur when I change the device URL?

     

    Also, do you get the P2P_DEVFOUND again if you perform an sl_Stop/sl_Start?

    Yes, I can get P2P_DEVFOUND again after sl_Stop/sl_Start.

    Thanks.

  • Hi,

    I looked into the NWP ROM source code, and it turns out that the random characters are only added if the URN is still set as the default - once you change it away from the default device URN, then the random characters are no longer added. So the behavior you are seeing is correct and is the expected behavior.

    Regards,

    Michael

  • Thank you for your reply. All my questions have been resolved.