Topic

FAQ
Login Register
How to read/write from/to 8258_mesh_gw.bin via usb in linux ?
Apr 02, 2024 17:41

/

A

/A
TL_Soyo Apr 03, 2024 10:38
0
/A

Hi,  

  Please contact sales to obtain the Linux version of the BDT tool.

5 replies
TL_Soyo Apr 03, 2024 10:38
0
/A

Hi,  

  Please contact sales to obtain the Linux version of the BDT tool.

JaeYong Lee [Author] Apr 15, 2024 16:27
0
/A

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<string.h>
#include"libusb.h"

#defineBULK_EP_OUT     (0x80|0x08) // Read from MESH GW to Linux
#defineBULK_EP_IN      0x05 // Write from Linux to MESH GW

intinterface_ref=0;
intalt_interface, interface_number;

intprint_configuration(structlibusb_device_handle*hDevice, structlibusb_config_descriptor*config);
structlibusb_endpoint_descriptor*active_config(structlibusb_device*dev, structlibusb_device_handle*handle);

staticinlinevoidmsleep(intmsecs)
{
#ifdefined(_WIN32)
        Sleep(msecs);
#else
        conststruct timespec ts = { msecs /1000, (msecs %1000) *1000000L };
        nanosleep(&ts, NULL);
#endif
}

intprint_configuration(structlibusb_device_handle*hDevice, structlibusb_config_descriptor*config)
{
    char*data;
    intindex;

    data= (char*)malloc(512);
    memset(data, 0, 512);

    index=config->iConfiguration;

    libusb_get_string_descriptor_ascii(hDevice, index, data, 512);

    printf("\nInterface Descriptors: ");
    printf("\n\tNumber of Interfaces: %d", config->bNumInterfaces);
    printf("\n\tLength: %d", config->bLength);
    printf("\n\tDesc_Type: %d", config->bDescriptorType);
    printf("\n\tConfig_index: %d", config->iConfiguration);
    printf("\n\tTotal length: %lu", config->wTotalLength);
    printf("\n\tConfiguration Value: %d", config->bConfigurationValue);
    printf("\n\tConfiguration Attributes: %d", config->bmAttributes);
    printf("\n\tMaxPower(mA): %d\n", config->MaxPower);

    free(data);
    data=NULL;
    return0;
}

structlibusb_endpoint_descriptor*active_config(structlibusb_device*dev, structlibusb_device_handle*handle)
{
    structlibusb_device_handle*hDevice_req;
    structlibusb_config_descriptor*config;
    structlibusb_endpoint_descriptor*endpoint;
    intaltsetting_index, interface_index=0, ret_active;
    inti, ret_print;

    hDevice_req=handle;

    endpoint=NULL;

    ret_active=libusb_get_active_config_descriptor(dev, &config);
    ret_print=print_configuration(hDevice_req, config);

    for (interface_index=0;interface_index<config->bNumInterfaces;interface_index++)
    {
        conststructlibusb_interface*iface=&config->interface[interface_index];
        for (altsetting_index=0; altsetting_index<iface->num_altsetting; altsetting_index++)
        {
            conststructlibusb_interface_descriptor*altsetting=&iface->altsetting[altsetting_index];

            intendpoint_index;
            for(endpoint_index=0; endpoint_index<altsetting->bNumEndpoints; endpoint_index++)
            {
                conststructlibusb_endpoint_desriptor*ep=&altsetting->endpoint[endpoint_index];
                endpoint=ep;
                alt_interface=altsetting->bAlternateSetting;
                interface_number=altsetting->bInterfaceNumber;
            }

            printf("\nEndPoint Descriptors: ");
            printf("\n\tSize of EndPoint Descriptor: %d", endpoint->bLength);
            printf("\n\tType of Descriptor: %d", endpoint->bDescriptorType);
            printf("\n\tEndpoint Address: 0x0%x", endpoint->bEndpointAddress);
            printf("\n\tMaximum Packet Size: %x", endpoint->wMaxPacketSize);
            printf("\n\tAttributes applied to Endpoint: %d", endpoint->bmAttributes);
            printf("\n\tInterval for Polling for data Tranfer: %d\n", endpoint->bInterval);
        }
    }
    libusb_free_config_descriptor(NULL);
    returnendpoint;
}

intmain(void)
{
    intr=1;
    structlibusb_device**devs;
    structlibusb_device_handle*handle=NULL, *hDevice_expected=NULL;
    structlibusb_device*dev, *dev_expected;

    structlibusb_device_descriptordesc;
    structlibusb_endpoint_descriptor*epdesc;
    structlibusb_interface_descriptor*intdesc;

    ssize_tcnt;
    inte=0, config2;
    inti=0, index;
    charstr1[64], str2[64];
    charfound=0;

    // Init libusb
    r=libusb_init(NULL);
    if(r<0)
    {
        printf("\nFailed to initialise libusb\n");
        return1;
    }
    else
        printf("\nInit successful!\n");

    // Get a list of USB devices
    cnt=libusb_get_device_list(NULL, &devs);
    if (cnt<0)
    {
        printf("\nThere are no USB devices on the bus\n");
        return-1;
    }
    printf("\nDevice count: %d\n-------------------------------\n", cnt);

    while ((dev=devs[i++]) !=NULL)
    {
        r=libusb_get_device_descriptor(dev, &desc);
        if (r<0)
            {
            printf("Failed to get device descriptor\n");
            libusb_free_device_list(devs, 1);
            libusb_close(handle);
            break;
        }

        e=libusb_open(dev, &handle);
        if (e<0)
        {
            printf("Error opening device\n");
            libusb_free_device_list(devs, 1);
            libusb_close(handle);
            break;
        }

        printf("\nDevice Descriptors: ");
        printf("\n\tVendor ID: %x", desc.idVendor);
        printf("\n\tProduct ID: %x", desc.idProduct);
        printf("\n\tSerial Number: %x", desc.iSerialNumber);
        printf("\n\tSize of Device Descriptor: %d", desc.bLength);
        printf("\n\tType of Descriptor: %d", desc.bDescriptorType);
        printf("\n\tUSB Specification Release Number: %d", desc.bcdUSB);
        printf("\n\tDevice Release Number: %d", desc.bcdDevice);
        printf("\n\tDevice Class: %d", desc.bDeviceClass);
        printf("\n\tDevice Sub-Class: %d", desc.bDeviceSubClass);
        printf("\n\tDevice Protocol: %d", desc.bDeviceProtocol);
        printf("\n\tMax. Packet Size: %d", desc.bMaxPacketSize0);
        printf("\n\tNumber of Configurations: %d\n", desc.bNumConfigurations);

        e=libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, (unsignedchar*) str1, sizeof(str1));
        if (e<0)
        {
        libusb_free_device_list(devs, 1);
            libusb_close(handle);
            break;
        }
        printf("\nManufactured: %s", str1);

        e=libusb_get_string_descriptor_ascii(handle, desc.iProduct, (unsignedchar*) str2, sizeof(str2));
        if(e<0)
        {
        libusb_free_device_list(devs, 1);
            libusb_close(handle);
            break;
        }
        printf("\nProduct: %s", str2);
        printf("\n----------------------------------------");

        if(desc.idVendor==0x248a&&desc.idProduct==0x5320)
        {
           found=1;
        break;
        }
    }//end of while
    if(found==0)
    {
        printf("\nDevice NOT found\n");
        libusb_free_device_list(devs, 1);
        libusb_close(handle);
        return1;
    }
    else
    {
        printf("\nDevice found");
        dev_expected=dev;
        hDevice_expected=handle;
    }

    e=libusb_get_configuration(handle, &config2);
    if(e!=0)
    {
        printf("\n***Error in libusb_get_configuration\n");
        libusb_free_device_list(devs, 1);
        libusb_close(handle);
        return-1;
    }
    printf("\nConfigured value: %d", config2);

    if(config2!=1)
    {
        libusb_set_configuration(handle, 1);
        if(e!=0)
        {
            printf("Error in libusb_set_configuration\n");
            libusb_free_device_list(devs, 1);
            libusb_close(handle);
            return-1;
        }
        else
            printf("\nDevice is in configured state!");
    }

    libusb_free_device_list(devs, 1);

    if(libusb_kernel_driver_active(handle, 0) ==1)
    {
        printf("\nKernel Driver Active");
        if(libusb_detach_kernel_driver(handle, 0) ==0)
            printf("\nKernel Driver Detached!");
        else
        {
            printf("\nCouldn't detach kernel driver!\n");
            libusb_free_device_list(devs, 1);
            libusb_close(handle);
            return-1;
        }
    }

    e=libusb_claim_interface(handle, 0);
    if(e<0)
    {
        printf("\nCannot Claim Interface");
        libusb_free_device_list(devs, 1);
        libusb_close(handle);
        return-1;
    }
    else
        printf("\nClaimed Interface\n");

    active_config(dev_expected, hDevice_expected);

    //   Communicate

intnbytes=256;
    char*my_string, *my_string1;
    inttransferred=0;
    intreceived=0;
    intlength=0;

    my_string= (char*)malloc(nbytes+1);
    my_string1= (char*)malloc(nbytes+1);

    memset(my_string, '\0', 64);
    memset(my_string1, '\0', 64);

//    strcpy(my_string, "%c%c%c", 0xe9, 0xff, 0x10);
    sprintf(my_string, "%c%c%c", 0xe9, 0xff, 0x10);
    length=strlen(my_string);

    printf("\nTo be sent: %s", my_string);

    e=libusb_bulk_transfer(handle, BULK_EP_IN, my_string, length, &transferred, 0);
    if(e==0&&transferred==length)
    {
        printf("\nWrite successful!");
        printf("\nSent %d bytes with string: %x\n", transferred, my_string);
    }
    else
        printf("\nError in write! e = %d and transferred = %d\n", e, transferred);




    sprintf(my_string, "%c%c%c", 0xe9, 0xff, 0x0c);
    length=strlen(my_string);
    e=libusb_bulk_transfer(handle, BULK_EP_IN, my_string, length, &transferred, 0);
    if(e==0&&transferred==length)
    {
        printf("\nWrite successful!");
        printf("\nSent %d bytes with string: %x\n", transferred, my_string);
    }
    else
        printf("\nError in write! e = %d and transferred = %d\n", e, transferred);



    sprintf(my_string, "%c%c%c%c", 0xe9, 0xff, 0x16, 0x00);
    length=4;//strlen(my_string);
    e=libusb_bulk_transfer(handle, BULK_EP_IN, my_string, length, &transferred, 0);
    if(e==0&&transferred==length)
    {
        printf("\nWrite successful!");
        printf("\nSent %d bytes with string: %x\n", transferred, my_string);
    }
    else
        printf("\nError in write! e = %d and transferred = %d\n", e, transferred);





//    msleep(3000);
    i=0;

    intbytes=0;
    while(libusb_bulk_transfer(handle, BULK_EP_OUT, my_string1, 64, &received, 0) ==0)
    {
        if(received>0)
        {
            printf("\nReceived: %d, ", received);
        for(i=0; i<received; i++)
        {
                printf("%02x ", my_string1[i]); //Will read a string from LPC2148
        }
        printf("\r\n");
//            msleep(1000);
        }
        else
        {
            printf("\nError in read! e = %d and received = %d\n", e, received);
            return-1;
        }
    }

    e=libusb_release_interface(handle, 0);

    libusb_close(handle);
    libusb_exit(NULL);

    printf("\n");
    return0;
}




JaeYong Lee [Author] Apr 16, 2024 07:28
0
/A

c++



#include<iostream>
#include<libusb.h>

usingnamespacestd;

voidprintdev(libusb_device*dev);

intmain()
{
    libusb_device**devs;
    libusb_context  *ctx=NULL;
    intr;
    ssize_tcnt;
    r=libusb_init(&ctx);
    if(r<0)
    {
        cout<<"Init Error "<<r<<endl;
        return0;
    }
    libusb_set_debug(ctx, 3);
    cnt=libusb_get_device_list(ctx, &devs);
    if(cnt<0)
    {
        cout<<"Get Device  Error"<<endl;
    }
    cout<<cnt<<" Devices in list."<<endl;
    ssize_ti;
    for(i=0; i<cnt; i++)
    {
        printdev(devs[i]);
    }
    libusb_free_device_list(devs, 1);
    libusb_exit(ctx);
    return0;
}

voidprintdev(libusb_device*dev)
{
    libusb_device_descriptordesc;
    intr=libusb_get_device_descriptor(dev, &desc);
    if(r<0)
    {
        cout<<"failed to get descriptor"<<endl;
        return;
    }
    cout<<"Number of possible configurations: "<<(int)desc.bNumConfigurations<<"    ";
    cout<<"Device Class: "<<(int)desc.bDeviceClass<<"   ";
    cout<<"VendorID: "<<hex<<desc.idVendor<<"    ";
    cout<<"ProductID: "<<hex<<desc.idProduct<<endl;
    libusb_config_descriptor*config;
    libusb_get_config_descriptor(dev, 0, &config);
    cout<<"Interfaces: "<<(int)config->bNumInterfaces<<" ||| ";
    constlibusb_interface*inter;
    constlibusb_interface_descriptor*interdesc;
    constlibusb_endpoint_descriptor*epdesc;
    for(inti=0; i<(int)config->bNumInterfaces; i++)
    {
        inter=&config->interface[i];
        cout<<"number of alternate settings: "<<inter->num_altsetting<<" | ";
        for(intj=0; j<inter->num_altsetting; j++)
        {
            interdesc=&inter->altsetting[j];
            cout<<"Interface Number: "<<(int)interdesc->bInterfaceNumber<<" | ";
            cout<<"Number of endpoints: "<<(int)interdesc->bNumEndpoints<<" | ";
            for(intk=0; k<(int)interdesc->bNumEndpoints; k++)
            {
                epdesc=&interdesc->endpoint[k];
                cout<<"Descriptor Type: "<<(int)epdesc->bDescriptorType<<" | ";
                cout<<"EP Address: "<<hex<<(int)epdesc->bEndpointAddress<<" | ";
            }
        }
    }
    cout<<endl<<endl<<endl;
    libusb_free_config_descriptor(config);
}


Number of possible configurations: 1 Device Class: 0 VendorID: 248a ProductID: 5320
Interfaces: 1 ||| number of alternate settings: 1 | Interface Number: 0 | Number of endpoints: 2 | Descriptor Type: 5 | EP Address: 88 | Descriptor Type: 5 | EP Address: 5 |