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.

CC3235S: Sending and Receiving in P2P mode using Raw Socket

Part Number: CC3235S

I am trying to build a project that makes two boards communicate with each others using P2P mode and the data exchanged between them is using the MAC address only, I established the connection between the two boards successfully, one as a client and the other as a group owner. Now I am in the phase of exchanging data between them, I followed the steps shown in the related thread (https://e2e.ti.com/support/wireless-connectivity/wifi/f/968/t/845693#), but the code does not return from the sl_Recv function and I do not know what is the issue?

the code for the client and the group owner is shown below:


Group Owner:

#define ARP_ETHIP_LEN 20 /* base ARP message length */

#define ARP_HRD_ETH 0x0001 /* ethernet hardware */
#define ARP_PRO_IP 0x0800 /* IP protocol */
#define ETH_ADDR_LEN 6
#define IP_ADDR_LEN 4
#define ARP_OP_REQUEST 1 /* request to resolve ha given pa */

struct arp_hdr {
    uint16_t ar_hrd; /* format of hardware address */
    uint16_t ar_pro; /* format of protocol address */
    uint8_t ar_hln;  /* length of hardware address (ETH_ADDR_LEN) */
    uint8_t ar_pln;  /* length of protocol address (IP_ADDR_LEN) */
    uint16_t ar_op;  /* operation */
}arp_hdr;

struct arp_ethip {
    uint8_t ar_sha[ETH_ADDR_LEN]; /* sender hardware address */
    uint8_t ar_spa[IP_ADDR_LEN];  /* sender protocol address */
    uint8_t ar_tha[ETH_ADDR_LEN]; /* target hardware address */
    uint8_t ar_tpa[IP_ADDR_LEN];  /* target protocol address */
}arp_ethip;

struct arp_eth_hdr {
    uint8_t dst_mac[ETH_ADDR_LEN];
    uint8_t src_mac[ETH_ADDR_LEN];
    uint8_t type[2];
}arp_eth_hdr;

#define ARP_HDR_LEN sizeof(arp_hdr)
#define ETH_HDR_LEN sizeof(arp_eth_hdr)

void Wireless_Build_Arp_Packet(char* buf)
{
    struct arp_hdr* arp1;
    struct arp_ethip* ethip;
    struct arp_eth_hdr* ethhdr1;

    unsigned char mac[6];
    memcpy(mac, app_CB.CON_CB.srcBSSID, ETH_ADDR_LEN);
    unsigned char src_ip[4];
//    unsigned char src_ip[4] = {0xc0, 0xa8, 0x2b, 0xda};
    memcpy(src_ip, app_CB.CON_CB.IpAddr, 4);
    unsigned char dest_mac[6] = {0XFF, 0XFF, 0XFF, 0XFF, 0XFF, 0XFF};
    unsigned char dest_ip[4];
//    unsigned char dest_ip[4] = {0xc0, 0xa8, 0x2b, 0x25};
    memcpy(dest_ip, app_CB.CON_CB.DestinationIp, 4);
    unsigned char eth_type[2] = {0x08, 0x06};

    ethhdr1 = (struct arp_eth_hdr*)buf;
    memcpy(ethhdr1->dst_mac, dest_mac, ETH_ADDR_LEN);
    memcpy(ethhdr1->src_mac, mac, ETH_ADDR_LEN);
    memcpy(ethhdr1->type, eth_type, 2);


    arp1 = (struct arp_hdr*)(buf+ ETH_HDR_LEN);
    arp1->ar_hrd = sl_Htons(ARP_HRD_ETH);
    arp1->ar_pro = sl_Htons(ARP_PRO_IP);
    arp1->ar_hln = ETH_ADDR_LEN;
    arp1->ar_pln = IP_ADDR_LEN;
    arp1->ar_op = sl_Htons(ARP_HRD_ETH);

    ethip = (struct arp_ethip*)(buf + ARP_HDR_LEN + ETH_HDR_LEN);
    memcpy(ethip->ar_sha, mac, ETH_ADDR_LEN);
    memcpy(ethip->ar_spa, src_ip, IP_ADDR_LEN);
    memcpy(ethip->ar_tha, dest_mac, ETH_ADDR_LEN);
    memcpy(ethip->ar_tpa, dest_ip, IP_ADDR_LEN);
}

void Wireless_Decode_Arp_Packet(char* buf){
    struct arp_hdr* arp1;
    struct arp_ethip* ethip;
    struct arp_eth_hdr* ethhdr1;

    ethhdr1 = (struct arp_eth_hdr*)buf;
    arp1 = (struct arp_hdr*)(buf+ ETH_HDR_LEN);
    ethip = (struct arp_ethip*)(buf + ARP_HDR_LEN + ETH_HDR_LEN);
    UART_PRINT("\n\rGot ARP response!\n\r");
    UART_PRINT("\n\rIP addr %i.%i.%i.%i ",ethip->ar_spa[0],ethip->ar_spa[1],ethip->ar_spa[2],ethip->ar_spa[3]);
    UART_PRINT("maps to MAC addr %x:%x:%x:%x:%x:%x!\n\r",ethip->ar_sha[0],ethip->ar_sha[1],ethip->ar_sha[2],ethip->ar_sha[3],ethip->ar_sha[4],ethip->ar_sha[5]);


}

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_GROUP_OWNER,
                                 SL_WLAN_P2P_NEG_INITIATOR_ACTIVE),
                         NULL, 0);
    ASSERT_ON_ERROR(ret, WLAN_ERROR);

    /* 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);

    sl_NetAppSet (SL_NETAPP_DEVICE_ID,SL_NETAPP_DEVICE_URN, strlen(P2P_DEVICE_NAME), (_u8 *)P2P_DEVICE_NAME);
    /* 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;
    }

    return(ret);
}

void * mainThread(void *arg)
{
    int32_t             RetVal ;
    pthread_attr_t      pAttrs_spawn;
    struct sched_param  priParam;
    struct timespec     ts = {0};
    int32_t             ret;
    int fd, len;
    uint32_t header = 1;
    char buf[100];

    /* Initializes the SPI interface to the Network 
       Processor and peripheral SPI (if defined in the board file) */
    SPI_init();
    GPIO_init();

    /* Init Application variables */
    RetVal = initAppVariables();

    /* Init Terminal UART */
    InitTerm();

    /* initialize the realtime clock */
    clock_settime(CLOCK_REALTIME, &ts);

    /* Switch off all LEDs on boards */
    GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_OFF);
    GPIO_write(CONFIG_GPIO_LED_1, CONFIG_GPIO_LED_OFF);
    GPIO_write(CONFIG_GPIO_LED_2, CONFIG_GPIO_LED_OFF);

    /* Create the sl_Task internal spawn thread */
    pthread_attr_init(&pAttrs_spawn);
    priParam.sched_priority = SPAWN_TASK_PRIORITY;
    RetVal = pthread_attr_setschedparam(&pAttrs_spawn, &priParam);
    RetVal |= pthread_attr_setstacksize(&pAttrs_spawn, TASK_STACK_SIZE);

    /* The SimpleLink host driver architecture mandate spawn 
       thread to be created prior to calling Sl_start (turning the NWP on). */
    /* The purpose of this thread is to handle
       asynchronous events sent from the NWP.
     * Every event is classified and later handled 
       by the Host driver event handlers. */
    RetVal = pthread_create(&gSpawn_thread, &pAttrs_spawn, sl_Task, NULL);
    if(RetVal != 0)
    {
        /* Handle Error */
        UART_PRINT("Network Terminal - Unable to create spawn thread \n");
        return(NULL);
    }

    /* Before turning on the NWP on,
       reset any previously configured parameters */
    /*
         IMPORTANT NOTE - This is an example reset function,
         user must update this function to match the application settings.
     */
    RetVal = sl_WifiConfig();
    if(RetVal < 0)
    {
        /* Handle Error */
        UART_PRINT("Network Terminal - Couldn't configure Network Processor - %d\n",RetVal);
        return(NULL);
    }

    /* Turn NWP on */
    RetVal = sl_Start(NULL, NULL, NULL);
    if(RetVal < 0)
    {
        /* Handle Error */
        UART_PRINT("sl_start failed - %d\n",RetVal);
        return(NULL);
    }

    /* sl_Start returns on success the role that device started on */
    app_CB.Role = RetVal;


    /* Output device information to the UART terminal */
    RetVal = DisplayAppBanner(APPLICATION_NAME, APPLICATION_VERSION);

    if(RetVal < 0)
    {
        /* Handle Error */
        UART_PRINT(
            "Network Terminal - Unable to retrieve device information \n");
        return(NULL);
    }

    ret = setP2Pparams();

    if(ret < 0)
    {
        UART_PRINT("\n\r[p2pstart] : Failed to configure P2P parameters.\n\r");
        return(NULL);
    }

    ret = sl_WlanPolicySet(SL_WLAN_POLICY_CONNECTION,SL_WLAN_CONNECTION_POLICY(0,0,1,0),NULL,0);
    if(ret < 0)
    {
        UART_PRINT("\n\rwlan policy set error.\n\r");
        return(NULL);
    }
    UART_PRINT(
        "\n\r[p2pstart] : User can stop the P2P connection process by typing"
        " 'p2pstop' command.\n\r");

    /* This would cause the NWP to send P2P probes, and listen on channel 6 */
    ret = sl_WlanConnect((int8_t*)P2P_REMOTE_DEVICE, strlen(
                             P2P_REMOTE_DEVICE), 0,&app_CB.P2P_CB.P2PsecParams,
                         0);

    if(ret < 0)
    {
        SHOW_WARNING(ret, WLAN_ERROR);
        return (NULL);
    }

    UART_PRINT("\n\r[p2pstart] : Waiting for device to be found ...\n\r");
    UART_PRINT(cmdPromptStr);
    ret = sem_wait(&app_CB.P2P_CB.DeviceFound);

    if(ret < 0)
    {
        SHOW_WARNING(ret, OS_ERROR);
        return (NULL);
    }

    /* Replay with connection request */
    UART_PRINT("\n\r[p2pstart] : Connecting ...\n\r");

//    /* Remote's SSID known - Establish connection */
    ret = sl_WlanConnect((signed char*)&app_CB.P2P_CB.p2pPeerDeviceName[0],
                                strlen(
                                (char*)&app_CB.P2P_CB.p2pPeerDeviceName[0]),
                                0,
                                &(app_CB.P2P_CB.P2PsecParams),
                                0);

    /* Wait for connect event */
        ret = sem_wait(&app_CB.P2P_CB.RcvConReq);

        if(ret < 0)
        {
            SHOW_WARNING(ret, OS_ERROR);
            return (NULL);
        }


    SET_STATUS_BIT(app_CB.Status, STATUS_BIT_CONNECTION);

    int rc;
    SlNetCfgIpV4Args_t ipAddr = {0};
    SlWlanRxFilterIdMask_t FilterIdMask;
    len = sizeof(SlWlanRxFilterIdMask_t);
    _i16 RawPacketSD;
    SlWlanSecParams_t secParams;
    memset(&ipAddr, 0, sizeof(SlNetCfgIpV4Args_t));
    rc= sl_NetCfgSet(SL_NETCFG_IPV4_AP_ADDR_MODE, SL_NETCFG_ADDR_STATIC, sizeof(SlNetCfgIpV4Args_t),(_u8 *)&ipAddr);
    if(rc < 0){
        UART_PRINT("\n\r error in seting IP to 0.0.0.0\n\r");
    }
    // disable network applications
    rc = sl_NetAppStop(SL_NETAPP_HTTP_SERVER_ID | SL_NETAPP_DHCP_SERVER_ID | SL_NETAPP_MDNS_ID);
    if(rc < 0){
        UART_PRINT("\n\r error in disabling network applications\n\r");
    }
    // restart NWP by calling stop then start to init with static IP 0.0.0.0
    rc = sl_Stop(1000);
    if(rc < 0){
        UART_PRINT("\n\r error in start to init with static IP 0.0.0.0\n\r");
    }
    rc = sl_Start(NULL, NULL, NULL);
    if(rc < 0){
        UART_PRINT("\n\r error starting NWP\n\r");
    // error starting NWP
    }

    // open RAW socket
    RawPacketSD = sl_Socket(SL_AF_PACKET,SL_SOCK_RAW, 0);
    if(RawPacketSD < 0){
        UART_PRINT("\n\r error opening raw socket\n\r");
    // error opening raw socket
    }

    Wireless_Build_Arp_Packet(buf);
    len = ARP_HDR_LEN + ARP_ETHIP_LEN + ETH_HDR_LEN;
    ret = sl_Send(RawPacketSD, buf, len, 0);
    ret = sl_Recv(RawPacketSD, buf, 40,0);
    Wireless_Decode_Arp_Packet(buf);

    UART_PRINT(
                "\n\r:"
                " BSSID: %x:%x:%x:%x:%x:%x\n\r",
                app_CB.CON_CB.ConnectionBSSID[0],
                app_CB.CON_CB.ConnectionBSSID[1],
                app_CB.CON_CB.ConnectionBSSID[2],
                app_CB.CON_CB.ConnectionBSSID[3],
                app_CB.CON_CB.ConnectionBSSID[4],
                app_CB.CON_CB.ConnectionBSSID[5]);
    UART_PRINT(
                "\n\r:"
                " ConnectionSSID: %x:%x:%x:%x:%x:%x\n\r",
                app_CB.CON_CB.srcBSSID[0],
                app_CB.CON_CB.srcBSSID[1],
                app_CB.CON_CB.srcBSSID[2],
                app_CB.CON_CB.srcBSSID[3],
                app_CB.CON_CB.srcBSSID[4],
                app_CB.CON_CB.srcBSSID[5]);
    UART_PRINT(
                "\n\r:"
                " DestinationIp: %x\n\r",
                app_CB.CON_CB.DestinationIp);
    UART_PRINT(
                "\n\r:"
                " IpAddr: %x\n\r",
                app_CB.CON_CB.IpAddr);

    return(0);
}



Client: 

#define ARP_ETHIP_LEN 20 /* base ARP message length */

#define ARP_HRD_ETH 0x0001 /* ethernet hardware */
#define ARP_PRO_IP 0x0800 /* IP protocol */
#define ETH_ADDR_LEN 6
#define IP_ADDR_LEN 4
#define ARP_OP_REQUEST 1 /* request to resolve ha given pa */

struct arp_hdr {
    uint16_t ar_hrd; /* format of hardware address */
    uint16_t ar_pro; /* format of protocol address */
    uint8_t ar_hln;  /* length of hardware address (ETH_ADDR_LEN) */
    uint8_t ar_pln;  /* length of protocol address (IP_ADDR_LEN) */
    uint16_t ar_op;  /* operation */
}arp_hdr;

struct arp_ethip {
    uint8_t ar_sha[ETH_ADDR_LEN]; /* sender hardware address */
    uint8_t ar_spa[IP_ADDR_LEN];  /* sender protocol address */
    uint8_t ar_tha[ETH_ADDR_LEN]; /* target hardware address */
    uint8_t ar_tpa[IP_ADDR_LEN];  /* target protocol address */
}arp_ethip;

struct arp_eth_hdr {
    uint8_t dst_mac[ETH_ADDR_LEN];
    uint8_t src_mac[ETH_ADDR_LEN];
    uint8_t type[2];
}arp_eth_hdr;

#define ARP_HDR_LEN sizeof(arp_hdr)
#define ETH_HDR_LEN sizeof(arp_eth_hdr)


void Wireless_Build_Arp_Packet(char* buf)
{
    struct arp_hdr* arp1;
    struct arp_ethip* ethip;
    struct arp_eth_hdr* ethhdr1;

    unsigned char mac[6];
    memcpy(mac, app_CB.CON_CB.srcBSSID, ETH_ADDR_LEN);
    unsigned char src_ip[4];
//    unsigned char src_ip[4] = {0xc0, 0xa8, 0x2b, 0xda};
    memcpy(src_ip, app_CB.CON_CB.IpAddr, 4);
    unsigned char dest_mac[6] = {0XFF, 0XFF, 0XFF, 0XFF, 0XFF, 0XFF};
//    unsigned char dest_ip[4] = {0xc0, 0xa8, 0x2b, 0x25};
    unsigned char dest_ip[4];
    memcpy(dest_ip, app_CB.CON_CB.DestinationIp, 4);
    unsigned char eth_type[2] = {0x08, 0x06};

    ethhdr1 = (struct arp_eth_hdr*)buf;
    memcpy(ethhdr1->dst_mac, dest_mac, ETH_ADDR_LEN);
    memcpy(ethhdr1->src_mac, mac, ETH_ADDR_LEN);
    memcpy(ethhdr1->type, eth_type, 2);


    arp1 = (struct arp_hdr*)(buf+ ETH_HDR_LEN);
    arp1->ar_hrd = sl_Htons(ARP_HRD_ETH);
    arp1->ar_pro = sl_Htons(ARP_PRO_IP);
    arp1->ar_hln = ETH_ADDR_LEN;
    arp1->ar_pln = IP_ADDR_LEN;
    arp1->ar_op = sl_Htons(ARP_HRD_ETH);

    ethip = (struct arp_ethip*)(buf + ARP_HDR_LEN + ETH_HDR_LEN);
    memcpy(ethip->ar_sha, mac, ETH_ADDR_LEN);
    memcpy(ethip->ar_spa, src_ip, IP_ADDR_LEN);
    memcpy(ethip->ar_tha, dest_mac, ETH_ADDR_LEN);
    memcpy(ethip->ar_tpa, dest_ip, IP_ADDR_LEN);
}

void Wireless_Decode_Arp_Packet(char* buf){
    struct arp_hdr* arp1;
    struct arp_ethip* ethip;
    struct arp_eth_hdr* ethhdr1;

    ethhdr1 = (struct arp_eth_hdr*)buf;
    arp1 = (struct arp_hdr*)(buf+ ETH_HDR_LEN);
    ethip = (struct arp_ethip*)(buf + ARP_HDR_LEN + ETH_HDR_LEN);
    UART_PRINT("\n\rGot ARP response!\n\r");
    UART_PRINT("\n\rIP addr %i.%i.%i.%i ",ethip->ar_spa[0],ethip->ar_spa[1],ethip->ar_spa[2],ethip->ar_spa[3]);
    UART_PRINT("maps to MAC addr %x:%x:%x:%x:%x:%x!\n\r",ethip->ar_sha[0],ethip->ar_sha[1],ethip->ar_sha[2],ethip->ar_sha[3],ethip->ar_sha[4],ethip->ar_sha[5]);


}

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_CLIENT,
                                 SL_WLAN_P2P_NEG_INITIATOR_PASSIVE),
                         NULL, 0);
    ASSERT_ON_ERROR(ret, WLAN_ERROR);

    /* 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);

    sl_NetAppSet (SL_NETAPP_DEVICE_ID,SL_NETAPP_DEVICE_URN, strlen(P2P_DEVICE_NAME), (_u8 *)P2P_DEVICE_NAME);
    /* 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;
    }

    return(ret);
}

void * mainThread(void *arg)
{
    int32_t             RetVal ;
    pthread_attr_t      pAttrs_spawn;
    struct sched_param  priParam;
    struct timespec     ts = {0};
    int32_t             ret;
    int fd, len;
    uint32_t header = 1;
    char buf[100];

    /* Initializes the SPI interface to the Network 
       Processor and peripheral SPI (if defined in the board file) */
    SPI_init();
    GPIO_init();

    /* Init Application variables */
    RetVal = initAppVariables();

    /* Init Terminal UART */
    InitTerm();

    /* initialize the realtime clock */
    clock_settime(CLOCK_REALTIME, &ts);

    /* Switch off all LEDs on boards */
    GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_OFF);
    GPIO_write(CONFIG_GPIO_LED_1, CONFIG_GPIO_LED_OFF);
    GPIO_write(CONFIG_GPIO_LED_2, CONFIG_GPIO_LED_OFF);

    /* Create the sl_Task internal spawn thread */
    pthread_attr_init(&pAttrs_spawn);
    priParam.sched_priority = SPAWN_TASK_PRIORITY;
    RetVal = pthread_attr_setschedparam(&pAttrs_spawn, &priParam);
    RetVal |= pthread_attr_setstacksize(&pAttrs_spawn, TASK_STACK_SIZE);

    /* The SimpleLink host driver architecture mandate spawn 
       thread to be created prior to calling Sl_start (turning the NWP on). */
    /* The purpose of this thread is to handle
       asynchronous events sent from the NWP.
     * Every event is classified and later handled 
       by the Host driver event handlers. */
    RetVal = pthread_create(&gSpawn_thread, &pAttrs_spawn, sl_Task, NULL);
    if(RetVal != 0)
    {
        /* Handle Error */
        UART_PRINT("Network Terminal - Unable to create spawn thread \n");
        return(NULL);
    }

    /* Before turning on the NWP on,
       reset any previously configured parameters */
    /*
         IMPORTANT NOTE - This is an example reset function,
         user must update this function to match the application settings.
     */
    RetVal = sl_WifiConfig();
    if(RetVal < 0)
    {
        /* Handle Error */
        UART_PRINT("Network Terminal - Couldn't configure Network Processor - %d\n",RetVal);
        return(NULL);
    }

    /* Turn NWP on */
    RetVal = sl_Start(NULL, NULL, NULL);
    if(RetVal < 0)
    {
        /* Handle Error */
        UART_PRINT("sl_start failed - %d\n",RetVal);
        return(NULL);
    }

    /* sl_Start returns on success the role that device started on */
    app_CB.Role = RetVal;

    /* Output device information to the UART terminal */
    RetVal = DisplayAppBanner(APPLICATION_NAME, APPLICATION_VERSION);

    if(RetVal < 0)
    {
        /* Handle Error */
        UART_PRINT(
            "Network Terminal - Unable to retrieve device information \n");
        return(NULL);
    }

    ret = setP2Pparams();

    if(ret < 0)
    {
        UART_PRINT("\n\r[p2pstart] : Failed to configure P2P parameters.\n\r");
        return(-1);
    }


    ret = sl_WlanPolicySet(SL_WLAN_POLICY_CONNECTION,SL_WLAN_CONNECTION_POLICY(0,0,1,0),NULL,0);
    if(ret < 0)
    {
        UART_PRINT("\n\rwlan policy set error.\n\r");
        return(NULL);
    }

    UART_PRINT(
        "\n\r[p2pstart] : User can stop the P2P connection process by typing"
        " 'p2pstop' command.\n\r");

    /* This would cause the NWP to send P2P probes, and listen on channel 6 */
    ret = sl_WlanConnect((int8_t*)P2P_REMOTE_DEVICE, strlen(
                             P2P_REMOTE_DEVICE), 0,&app_CB.P2P_CB.P2PsecParams,
                         0);

    if(ret < 0)
    {
        SHOW_WARNING(ret, WLAN_ERROR);
        return (NULL);
    }

    UART_PRINT("\n\r[p2pstart] : Waiting for device to be found ...\n\r");
    UART_PRINT(cmdPromptStr);
    ret = sem_wait(&app_CB.P2P_CB.DeviceFound);

    if(ret < 0)
    {
        SHOW_WARNING(ret, OS_ERROR);
        return (NULL);
    }

    UART_PRINT("\n\r[p2pstart] : Waiting for Negotiation request ...\n\r");
    UART_PRINT(cmdPromptStr);
    ret = sem_wait(&app_CB.P2P_CB.RcvNegReq);

    if(ret < 0)
    {
        SHOW_WARNING(ret, OS_ERROR);
        return (NULL);
    }

    /* Wait for connect event */
    ret = sem_wait(&app_CB.P2P_CB.RcvConReq);

    if(ret < 0)
    {
        SHOW_WARNING(ret, OS_ERROR);
        return (NULL);
    }
    SET_STATUS_BIT(app_CB.Status, STATUS_BIT_CONNECTION);

    int rc;
    SlNetCfgIpV4Args_t ipAddr = {0};
    SlWlanRxFilterIdMask_t FilterIdMask;
    len = sizeof(SlWlanRxFilterIdMask_t);
    _i16 RawPacketSD;
    SlWlanSecParams_t secParams;
    memset(&ipAddr, 0, sizeof(SlNetCfgIpV4Args_t));
    rc= sl_NetCfgSet(SL_NETCFG_IPV4_STA_ADDR_MODE, SL_NETCFG_ADDR_STATIC, sizeof(SlNetCfgIpV4Args_t), (_u8 *)&ipAddr);
    if(rc < 0)
    {
        UART_PRINT("\n\r error in seting IP to 0.0.0.0\n\r");
    }
    // disable network applications
    rc = sl_NetAppStop(SL_NETAPP_HTTP_SERVER_ID | SL_NETAPP_DHCP_SERVER_ID | SL_NETAPP_MDNS_ID);
    if(rc < 0){
        UART_PRINT("\n\r error in disabling network applications\n\r");
    }
    // restart NWP by calling stop then start to init with static IP 0.0.0.0
    rc = sl_Stop(1000);
    if(rc < 0){
        UART_PRINT("\n\r error in start to init with static IP 0.0.0.0\n\r");
    }
    rc = sl_Start(NULL, NULL, NULL);
    if(rc < 0){
        UART_PRINT("\n\r error starting NWP\n\r");
    // error starting NWP
    }

    // open RAW socket
    RawPacketSD = sl_Socket(SL_AF_PACKET,SL_SOCK_RAW, 0);
    if(RawPacketSD < 0){
        UART_PRINT("\n\r error opening raw socket\n\r");
    // error opening raw socket
    }



    ret = sl_Recv(RawPacketSD, buf, 40,0);
    Wireless_Decode_Arp_Packet(buf);
    Wireless_Build_Arp_Packet(buf);
    len = ARP_HDR_LEN + ARP_ETHIP_LEN + ETH_HDR_LEN;
    ret = sl_Send(RawPacketSD, buf, len, 0);

    UART_PRINT(
                "\n\r:"
                " BSSID: %x:%x:%x:%x:%x:%x\n\r",
                app_CB.CON_CB.ConnectionBSSID[0],
                app_CB.CON_CB.ConnectionBSSID[1],
                app_CB.CON_CB.ConnectionBSSID[2],
                app_CB.CON_CB.ConnectionBSSID[3],
                app_CB.CON_CB.ConnectionBSSID[4],
                app_CB.CON_CB.ConnectionBSSID[5]);
    UART_PRINT(
                "\n\r:"
            " ConnectionSSID: %x:%x:%x:%x:%x:%x\n\r",
            app_CB.CON_CB.srcBSSID[0],
            app_CB.CON_CB.srcBSSID[1],
            app_CB.CON_CB.srcBSSID[2],
            app_CB.CON_CB.srcBSSID[3],
            app_CB.CON_CB.srcBSSID[4],
            app_CB.CON_CB.srcBSSID[5]);
    UART_PRINT(
                "\n\r:"
                " DestinationIp: %x\n\r",
                app_CB.CON_CB.DestinationIp);
    UART_PRINT(
                "\n\r:"
                " IpAddr: %x\n\r",
                app_CB.CON_CB.IpAddr);

    return(0);
}

  • Did you see any valid packet sent over the air from the peer device? (i.e. with destination mac that matches the recipient address).

    please refer to chapter 6.5.4 in the programmers guide (http://www.ti.com/lit/ug/swru455j/swru455j.pdf) to get more info on the setup for the raw ethernet socket.

    br,

    Kobi

  • No, and I have a problem that the MAC address appears on the client side does not match that of the GO side but the retrieved MAC address that appears in the GO side matches that of the client side

  • I'm not sure i understand.

    how do you read local mac address?

  • Please check my question here ()

    The mac address of the peer device is get once connected in the handler, right? And the local mac address of the board itself is got from netcfgget function in the function that prints the information of the device in the network terminal example

  • Please send me just the code where you read the BSSID for the p2p client.

  • case SL_WLAN_EVENT_P2P_CONNECT:
        {
            UART_PRINT(
                "n\r[WLAN EVENT] P2P connection was successfully completed as "
                "CLIENT\n\r");
            UART_PRINT("n\rBSSID is %02x:%02x:%02x:%02x:%02x:%02x\n\r",
                       pWlanEvent->Data.STAAdded.Mac[0],
                       pWlanEvent->Data.STAAdded.Mac[1],
                       pWlanEvent->Data.STAAdded.Mac[2],
                       pWlanEvent->Data.STAAdded.Mac[3],
                       pWlanEvent->Data.STAAdded.Mac[4],
                       pWlanEvent->Data.STAAdded.Mac[5]);
            memcpy(app_CB.CON_CB.ConnectionBSSID, pWlanEvent->Data.STAAdded.Mac, SL_WLAN_BSSID_LENGTH);
            sem_post(&app_CB.P2P_CB.RcvConReq);
        }
        break;
    
        case SL_WLAN_EVENT_P2P_CLIENT_ADDED:
        {
            UART_PRINT(
                "n\r[WLAN EVENT] P2P connection was successfully completed "
                "as GO\n\r");
            UART_PRINT("n\rBSSID is %02x:%02x:%02x:%02x:%02x:%02x\n\r",
                       pWlanEvent->Data.P2PClientAdded.Mac[0],
                       pWlanEvent->Data.P2PClientAdded.Mac[1],
                       pWlanEvent->Data.P2PClientAdded.Mac[2],
                       pWlanEvent->Data.P2PClientAdded.Mac[3],
                       pWlanEvent->Data.P2PClientAdded.Mac[4],
                       pWlanEvent->Data.P2PClientAdded.Mac[5]);
            memcpy(app_CB.CON_CB.ConnectionBSSID, pWlanEvent->Data.P2PClientAdded.Mac, SL_WLAN_BSSID_LENGTH);
                sem_post(&app_CB.P2P_CB.RcvConReq);
            }
            break;
    

  • You are not parsing the right structure.

    In the SL_WLAN_EVENT_P2P_CONNECT case, you should retrieve the value of "pWlanEvent->Data.P2PConnect.Bssid" (and not pWlanEvent->Data.STAAdded.Mac).

    Make sure that structure parsed corresponds to the event type.

  • This is the code where I print the device MAC address:

    int32_t DisplayAppBanner(char* appName, char* appVersion) { int32_t ret = 0; uint8_t macAddress[SL_MAC_ADDR_LEN]; uint16_t macAddressLen = SL_MAC_ADDR_LEN; uint16_t ConfigSize = 0; uint8_t ConfigOpt = SL_DEVICE_GENERAL_VERSION; SlDeviceVersion_t ver = {0}; ConfigSize = sizeof(SlDeviceVersion_t); /* Print device version info. */ ret = sl_DeviceGet(SL_DEVICE_GENERAL, &ConfigOpt, &ConfigSize, (uint8_t*)(&ver)); ASSERT_ON_ERROR(ret, DEVICE_ERROR); /* Print device Mac address */ ret = sl_NetCfgGet(SL_NETCFG_MAC_ADDRESS_GET, 0, &macAddressLen, &macAddress[0]); ASSERT_ON_ERROR(ret, WLAN_ERROR); memcpy(app_CB.CON_CB.srcBSSID, macAddress, SL_WLAN_BSSID_LENGTH); UART_PRINT(lineBreak); UART_PRINT("\t"); printBorder('=', 44); UART_PRINT(lineBreak); UART_PRINT("\t %s Example Ver: %s",appName, appVersion); UART_PRINT(lineBreak); UART_PRINT("\t"); printBorder('=', 44); UART_PRINT(lineBreak); UART_PRINT(lineBreak); UART_PRINT("\t CHIP: 0x%x",ver.ChipId); UART_PRINT(lineBreak); UART_PRINT("\t MAC: %d.%d.%d.%d",ver.FwVersion[0],ver.FwVersion[1], ver.FwVersion[2], ver.FwVersion[3]); UART_PRINT(lineBreak); UART_PRINT("\t PHY: %d.%d.%d.%d",ver.PhyVersion[0],ver.PhyVersion[1], ver.PhyVersion[2], ver.PhyVersion[3]); UART_PRINT(lineBreak); UART_PRINT("\t NWP: %d.%d.%d.%d",ver.NwpVersion[0],ver.NwpVersion[1], ver.NwpVersion[2], ver.NwpVersion[3]); UART_PRINT(lineBreak); UART_PRINT("\t ROM: %d",ver.RomVersion); UART_PRINT(lineBreak); UART_PRINT("\t HOST: %s", SL_DRIVER_VERSION); UART_PRINT(lineBreak); UART_PRINT("\t MAC address: %02x:%02x:%02x:%02x:%02x:%02x", macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]); UART_PRINT(lineBreak); UART_PRINT(lineBreak); UART_PRINT("\t"); printBorder('=', 44); UART_PRINT(lineBreak); UART_PRINT(lineBreak); return(ret); }

  • Ok I will try it and get back to you 

    Does this affect the sending and receiving of the raw socket?

  • I want to exchange data between two devices using P2P mode but with the MAC address only without the transport and network layer. That's why I used raw socket and I do not want to even use the ARP as the ARP uses the IP addresses and I want to use only the MAC addresses. Can you help me through this?

  • Theoretically if you know the peer address and local address, you can generate and send an Ethernet packet using the Ethernet Raw Socket that will be received by the other side. 

    I'm not sure this raw socket was ever tested with a P2P connection. 

  • Is there an example to test the raw socket in general and I will try it with the P2P connection?

  • There is no available example for the network bypass.

    Br,

    Kobi

  • I got the following code from the programmer's guide:

    Group Owner:

    int rc;
    SlNetCfgIpV4Args_t ipAddr = {0};
    SlWlanRxFilterIdMask_t FilterIdMask;
    len = sizeof(SlWlanRxFilterIdMask_t);
    _i16 RawPacketSD;
    SlWlanSecParams_t secParams;
    memset(&ipAddr, 0, sizeof(SlNetCfgIpV4Args_t));
    rc= sl_NetCfgSet(SL_NETCFG_IPV4_AP_ADDR_MODE, SL_NETCFG_ADDR_STATIC, sizeof(SlNetCfgIpV4Args_t),(_u8 *)&ipAddr);
    if(rc < 0){
      UART_PRINT("\n\r error in seting IP to 0.0.0.0\n\r");
    }
    // disable network applications
        rc = sl_NetAppStop(SL_NETAPP_HTTP_SERVER_ID | SL_NETAPP_DHCP_SERVER_ID | SL_NETAPP_MDNS_ID);
        if(rc < 0){
            UART_PRINT("\n\r error in disabling network applications\n\r");
        }
        // restart NWP by calling stop then start to init with static IP 0.0.0.0
        rc = sl_Stop(1000);
        if(rc < 0){
            UART_PRINT("\n\r error in start to init with static IP 0.0.0.0\n\r");
        }
        rc = sl_Start(NULL, NULL, NULL);
        if(rc < 0){
            UART_PRINT("\n\r error starting NWP\n\r");
        // error starting NWP
        }
    //    memset(FilterIdMask,0,sizeof(FilterIdMask));
    //    rc = sl_WlanSet(SL_WLAN_RX_FILTERS_ID, SL_WLAN_RX_FILTER_STATE , len, (_u8* )FilterIdMask);
    //    if(rc < 0){
    //        UART_PRINT("\n\r error disabling internal filter\n\r");
    //    // error disabling internal filter
    //    }
        // open RAW socket
        RawPacketSD = sl_Socket(SL_AF_PACKET,SL_SOCK_RAW, 0);
        if(RawPacketSD < 0){
            UART_PRINT("\n\r error opening raw socket\n\r");
        // error opening raw socket
        }

    Client:

    int rc;
    SlNetCfgIpV4Args_t ipAddr = {0};
    SlWlanRxFilterIdMask_t FilterIdMask;
    len = sizeof(SlWlanRxFilterIdMask_t);
    _i16 RawPacketSD;
    SlWlanSecParams_t secParams;
    memset(&ipAddr, 0, sizeof(SlNetCfgIpV4Args_t));
    rc= sl_NetCfgSet(SL_NETCFG_IPV4_STA_ADDR_MODE, SL_NETCFG_ADDR_STATIC, sizeof(SlNetCfgIpV4Args_t),(_u8 *)&ipAddr);
    if(rc < 0){
      UART_PRINT("\n\r error in seting IP to 0.0.0.0\n\r");
    }
    // disable network applications
        rc = sl_NetAppStop(SL_NETAPP_HTTP_SERVER_ID | SL_NETAPP_DHCP_SERVER_ID | SL_NETAPP_MDNS_ID);
        if(rc < 0){
            UART_PRINT("\n\r error in disabling network applications\n\r");
        }
        // restart NWP by calling stop then start to init with static IP 0.0.0.0
        rc = sl_Stop(1000);
        if(rc < 0){
            UART_PRINT("\n\r error in start to init with static IP 0.0.0.0\n\r");
        }
        rc = sl_Start(NULL, NULL, NULL);
        if(rc < 0){
            UART_PRINT("\n\r error starting NWP\n\r");
        // error starting NWP
        }
    //    memset(FilterIdMask,0,sizeof(FilterIdMask));
    //    rc = sl_WlanSet(SL_WLAN_RX_FILTERS_ID, SL_WLAN_RX_FILTER_STATE , len, (_u8* )FilterIdMask);
    //    if(rc < 0){
    //        UART_PRINT("\n\r error disabling internal filter\n\r");
    //    // error disabling internal filter
    //    }
        // open RAW socket
        RawPacketSD = sl_Socket(SL_AF_PACKET,SL_SOCK_RAW, 0);
        if(RawPacketSD < 0){
            UART_PRINT("\n\r error opening raw socket\n\r");
        // error opening raw socket
        }

    but I do not know how to send and receive in this case

    1. what are the arguments to pass to send or sendto function?
    2. How to pass the known MAC address to the send function to reach the other board?
    3. Should I disconnect the connection between the peers to send the data?

  • I managed to send raw socket from the GO and receive them in the Client, but I tried to reverse this and make the client send the raw socket to the GO, but the receive function does not return which means the GO did not receive anything from the client, so I have multiple questions:

    1. Can the client send data back to the GO or it is only one way of sending data from the GO to the client?
    2. when i tried to do so and send the data two times in the GO, one for rawsocket1 and the other for rawsocket2 and both send functions are done and returned without errors, but on the client side, it received once at the first raw socket and the receive function did not return from the second call of the receive function on rawsocket2, so Can I open more than one raw socket to send and receive more data or not? 

  • If the addresses are correct than the client  should be able to send to the GO. there is no limitations there as socket are on a different networking layer.

    For RAW usage, only one socket can be used.