cancel
Showing results for 
Search instead for 
Did you mean: 

WICED Studio Bluetooth

soG_4608441
New Contributor

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.

0 Likes
1 Solution
AnjanaM_61
Moderator
Moderator

Hi Soumya,

I am not sure about the pipe error.

Probably you may check on USB port opening code. Please check if the device is enumerated correctly and try again.

And then as mentioned before ,

1. You have to use HCI UART port for sending HCI commands. Make sure HCI port is not opened by any other application

2. Send a HCI reset command first and check if you are getting correct response. Make sure you send the commands in correct format and then wait for the response from 20719

3. Only if you get a correct response for HCI reset command, then proceed with next commands.

Thanks,Anjana

View solution in original post

8 Replies
AnjanaM_61
Moderator
Moderator

Hi,

From the logs , seems like the issue is with     print(dev.ctrl_transfer(0x20, 0, 0, 0, bytes(packet), 1000)) . Can you please confirm what this function (ctrl_transfer) is doing?

Please note the below steps for sending HCI commands:

First program 20719 with an empty application project ((one time )) as mentioned in the previous thread

1. You have to use HCI UART port for sending HCI commands. Make sure HCI port is opened by only one application

2. Send a HCI reset command first and check if you are getting correct response.

3. Only if you get a correct response for HCI reset command, then proceed with next commands.

Regards,

Anjana   

soG_4608441
New Contributor

Do know any other way to send HCI commands using python

Regards,

Soumya Gadad

0 Likes
WinstonF_61
Moderator
Moderator

Hi Soumya,

I am a little confused with your used case. You mention that you have tested the BlueZ stack. Do you intend to use the BlueZ stack ? or do you want to write a new Python Code instead?

Any elaboration would help.

Winston.

Regards
Winston
0 Likes
soG_4608441
New Contributor

Hii Winston ,

    Thanks for reply

Using Hcitool i am able send the Hci Reset command

ex:

sudo hcitool cmd 0x03 0x0003

[sudo] password for vijay:

< HCI Command: ogf 0x03, ocf 0x0003, plen 0

> HCI Event: 0x0e plen 4

  01 03 0C 00

I need to send the HCI reset command and other HCI commands through python script using ocf and ogf without uisng hcitool .

In python there is a module called libusb where we can send the commands to the using ctrl_tranfer. But i am not able to send  the command getting pipe error . Please help me to solve this.

0 Likes
soG_4608441
New Contributor

Hi Anjana,

Thank you for your suggestion.

ctrl_transfer will send the commands to the device.

Regards,

Soumya Gadad

0 Likes
soG_4608441
New Contributor

ctrl_transfer is function in the pysub in python. Which is the method in libusb python library.

Regards,

Soumya Gadad

0 Likes
AnjanaM_61
Moderator
Moderator

Hi Soumya,

I am not sure about the pipe error.

Probably you may check on USB port opening code. Please check if the device is enumerated correctly and try again.

And then as mentioned before ,

1. You have to use HCI UART port for sending HCI commands. Make sure HCI port is not opened by any other application

2. Send a HCI reset command first and check if you are getting correct response. Make sure you send the commands in correct format and then wait for the response from 20719

3. Only if you get a correct response for HCI reset command, then proceed with next commands.

Thanks,Anjana

View solution in original post