8 Replies Latest reply on Jan 29, 2020 2:17 AM by AnjanaM_61

    Need to send the hci commands to CYW920719 board with help of python libusb module. Which interface we need to use for sending the HCI commands

    soG_4608441

      Hii,

       

      I am struck with sending the HCI commands to the CYW920719 board . CYW920719 is attached to linux machine it is coming like ttyWICED_HCI_UART10 . Using hci attach i am attaching the device to bluez stack . I am able to get CYW920719 in hci0. Manually i can able to sen the hci commands using hcitool.

      I want send the commands using python script. I am using libusb to send the commands. Unable to send the hci commands geting USB pipe error.

       

      Here is my python script:

       

      import usb.core

      import usb.util

      import subprocess

      import logging

       

       

       

      class Result(object):

         """Command execution result.
        Contains information of executed command by subprocess.

        Attributes:
        command: command as a string.
        exit_status: Integer exit code of the process.
        stdout: The output from standard out.
        stderr: The output from standard error.

        """

         def __init__(self, command, stdout, stderr, exit_status):

         self.command = command

         self.stdout = stdout

         self.stderr = stderr

         self.exit_status = exit_status

       

         def __repr__(self):

         return ('Result(command=%r, stdout=%r, stderr=%r, exit_status=%r)'
         ) % (self.command, self.stdout, self.stderr, self.exit_status)

       

       

      def run(command, subprocess_input=""):

         """ Executes the commands on shell.

        Args:
        command: Command to be executed.
        subprocess_input: Input to be given for the subprocess

        Returns:
        True: command line output on success or False otherwise
        """
         proc = subprocess.Popen(

        command,

         stdout=subprocess.PIPE,

         stderr=subprocess.PIPE,

         shell=True)

       

        (out, err) = proc.communicate(subprocess_input.encode())

       

        result = Result(

         command=command,

         stdout=out.decode("utf-8").strip(),

         stderr=err.decode("utf-8").strip(),

         exit_status=proc.returncode

        )

       

         if proc.returncode != 0:

        logging.error("ERROR: Command Failed - %s" % result)

         return False
         return out

       

       

      def create_hci_command_packet(ogf, ocf, data=()):

         """ Prepare the formatted hci command packet

        Args:
        ogf (int): Opcode Group Field
        ocf (int): Opcode Command Field
        data (list): Command Parameters

        Return:
        hci_command_packet (Byte array): on success
        False (bool): otherwise
        """
         opcode = (((ogf << 10) & 0xffff) | ocf)

        byte_str = bytes.fromhex('0' + str(hex(opcode)).strip("0x"))

        opcode = bytes([c for t in zip(byte_str[1::2],

        byte_str[::2]) for c in t])

        packet = bytearray()

        [packet.append(itr) for itr in opcode]

        packet.append(len(data))

        [packet.append(itr) for itr in data]

         if packet:

         return packet

         return False

      print(run("sudo hciattach /dev/ttyWICED_HCI_UART11 -t 10 any 115200 noflow nosleep"))

      dev = usb.core.find(idVendor=0x04b4, idProduct=0x009b)

      print("dev", dev)

       

       

      #assert dev.ctrl_transfer(0x40, 0x80, 0, 0, msg) == len(msg)
      packet = create_hci_command_packet(0x03, 0x0019)

      print("packet", packet)

      print(dev.ctrl_transfer(0x20, 0, 0, 0, bytes(packet), 1000))

      #print(dev.ctrl_transfer(0x20, 0, 0, 0, bytes(packet), 1000))
      print(read_endpoint.read(size_or_buffer=10,

         timeout=10000))


      Result:

      b'Device setup complete\n'

      dev DEVICE ID 04b4:009b on Bus 002 Address 021 =================

      bLength                :   0x12 (18 bytes)

      bDescriptorType        :    0x1 Device

      bcdUSB                 :  0x200 USB 2.0

      bDeviceClass           :    0x0 Specified at interface

      bDeviceSubClass        :    0x0

      bDeviceProtocol        :    0x0

      bMaxPacketSize0        :   0x40 (64 bytes)

      idVendor               : 0x04b4

      idProduct              : 0x009b

      bcdDevice              :  0x700 Device 7.0

      iManufacturer          :    0x1 Cypress

      iProduct               :    0x2 WICED USB <-> Serial Converter

      iSerialNumber          :    0x0

      bNumConfigurations     :    0x1

        CONFIGURATION 1: 100 mA ==================================

         bLength              :    0x9 (9 bytes)

         bDescriptorType      :    0x2 Configuration

         wTotalLength         :   0x37 (55 bytes)

         bNumInterfaces       :    0x2

         bConfigurationValue  :    0x1

         iConfiguration       :    0x0

         bmAttributes         :   0x80 Bus Powered

         bMaxPower            :   0x32 (100 mA)

          INTERFACE 0: Vendor Specific ===========================

           bLength            :    0x9 (9 bytes)

           bDescriptorType    :    0x4 Interface

           bInterfaceNumber   :    0x0

           bAlternateSetting  :    0x0

           bNumEndpoints      :    0x2

           bInterfaceClass    :   0xff Vendor Specific

           bInterfaceSubClass :   0xff

           bInterfaceProtocol :   0xff

           iInterface         :    0x2 WICED USB <-> Serial Converter

            ENDPOINT 0x81: Bulk IN ===============================

             bLength          :    0x7 (7 bytes)

             bDescriptorType  :    0x5 Endpoint

             bEndpointAddress :   0x81 IN

             bmAttributes     :    0x2 Bulk

             wMaxPacketSize   :  0x200 (512 bytes)

             bInterval        :    0x0

            ENDPOINT 0x2: Bulk OUT ===============================

             bLength          :    0x7 (7 bytes)

             bDescriptorType  :    0x5 Endpoint

             bEndpointAddress :    0x2 OUT

             bmAttributes     :    0x2 Bulk

             wMaxPacketSize   :  0x200 (512 bytes)

             bInterval        :    0x0

          INTERFACE 1: Vendor Specific ===========================

           bLength            :    0x9 (9 bytes)

           bDescriptorType    :    0x4 Interface

           bInterfaceNumber   :    0x1

           bAlternateSetting  :    0x0

           bNumEndpoints      :    0x2

           bInterfaceClass    :   0xff Vendor Specific

           bInterfaceSubClass :   0xff

           bInterfaceProtocol :   0xff

           iInterface         :    0x2 WICED USB <-> Serial Converter

            ENDPOINT 0x83: Bulk IN ===============================

             bLength          :    0x7 (7 bytes)

             bDescriptorType  :    0x5 Endpoint

             bEndpointAddress :   0x83 IN

             bmAttributes     :    0x2 Bulk

             wMaxPacketSize   :  0x200 (512 bytes)

             bInterval        :    0x0

            ENDPOINT 0x4: Bulk OUT ===============================

             bLength          :    0x7 (7 bytes)

             bDescriptorType  :    0x5 Endpoint

             bEndpointAddress :    0x4 OUT

             bmAttributes     :    0x2 Bulk

             wMaxPacketSize   :  0x200 (512 bytes)

             bInterval        :    0x0

      packet bytearray(b'\x19\x0c\x00')

      Traceback (most recent call last):

        File "Hci_commands.py", line 94, in <module>

          print(dev.ctrl_transfer(0x20, 0, 0, 0, bytes(packet), 1000))

        File "/home/vijay/.local/lib/python3.5/site-packages/usb/core.py", line 1043, in ctrl_transfer

          self.__get_timeout(timeout))

        File "/home/vijay/.local/lib/python3.5/site-packages/usb/backend/libusb1.py", line 883, in ctrl_transfer

          timeout))

        File "/home/vijay/.local/lib/python3.5/site-packages/usb/backend/libusb1.py", line 595, in _check

          raise USBError(_strerror(ret), ret, _libusb_errno[ret])

      usb.core.USBError: [Errno 32] Pipe error

       


      How to solve the Pipe error.