Use Blackberry USB channels with Barry
Introduction

The Blackberry provides the ability for programs to send and receive data over the USB port via named channels. On device there are two APIs which provide access to these channels:

- net.rim.device.api.system.USBPort
- javax.microedition.io.Connector

Barry provides APIs and tools to access the PC side of these USB channels. The Barry APIs provide similar functionality to the Blackberry Desktop Manager API.

To use USB channels with Barry you will also need the following:

- a working Barry install, version 0.17 or later
- a program which uses USB channels installed on your Blackberry

Using USB channels to talk to 'USB Demo'

As part of the examples provided with the Blackberry JDEs there is a demo application called 'USB Demo' which can be found in `samples/com/rim/samples/device/usbdemo` in the installation folder of the JDE. You should build this and install it on your Blackberry.

The Blackberry JDEs come with an example application for the Blackberry Desktop Manager API in `samples/usbclient` in the installation folder. The equivalent PC side usbclient can be found in the Barry source in `examples/usbclient.cc`

Once the example is compiled it should be used by first running the 'USB Demo' application on the Blackberry and selecting one of the 'Connect' options from the menu. Once the 'USB Demo' application is waiting then it's possible to run the `usbclient` example to communicate with the Blackberry application.

Using USB channels as a stream

The raw channel API provided by Barry exposes the channel interface as sending and receiving of packets of data. While this closely reflects the USB channel protocol, it can be unhelpful for applications which are designed with streams as their communication mechanisms. To accommodate this Barry provides a tool called `brawchannel`. This tool redirects the named channel over STDIN and STDOUT, allowing easy communication with pre-existing tools.

For example the following shows an example of using `brawchannel` to talk to the 'USB Demo' application mentioned in the previous section.

First it's necessary to run 'USB Demo' on the Blackberry and select the 'Connect (low level)' menu option. With that running it's then possible to run `brawchannel` by supplying the channel name on the command line. For the 'USB Demo' application this channel name is `JDE_USBClient`.

    user@machine:~$ brawchannel JDE_USBClient

If all goes well then you should see nothing but a flashing cursor. The 'USB Demo' application expects us to say hello, so type:

    Hello from Barry.<RET>

You should then see two things, firstly your terminal should now look like the following:

    user@machine:~$ brawchannel JDE_USBClient
    Hello from Barry.
    Hello from Device

Secondly the message you typed should appear on the device (along with the demo application complaining that it's not the expected text). Next we need to say goodbye to the device, so type:

    Goodbye from Barry.<RET>

The device should then say goodbye back, followed by it closing the connection.

It's also possible to use other programs, such as `socat` to use the `brawchannel` tool to communicate with the USB channel via other mechanisms such as TCP/IP sockets.

For further information please see the man page for `brawchannel`.

Using USB channels in your application

It is also possible to use USB channels directly in your application by linking to the Barry library. To make use of these USB channels you will need to create an instance of the `Barry::Mode::RawChannel` class. The Barry source code demonstrates creation of a RawChannel in `examples/usbclient.cc` and `tools/brawchannel.cc`.

In it's most basic form this can be done with the following code:

    Barry::Init();
    Barry::Probe probe;
    if( probe.GetCount() <= 0 ) {
        // No blackberry found
        return 1;
    }

    auto_ptr<SocketRoutingQueue> router;
    router.reset(new SocketRoutingQueue());
    router->SpinoffSimpleReadThread();

    Barry::Controller con(probe.Get(0), *router);

    Barry::Mode::RawChannel rawChannel(con);

    rawChannel.Open("", CHANNEL_NAME);

With data then sent and received using `rawChannel.Send()` and `rawChannel.Receive()`.

Copyright © 2012 - Net Direct Inc.