3 /// Example code using the Barry library to interface
4 /// with a custom USB channel on a Blackberry device.
8 Copyright (C) 2010, RealVNC Ltd.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include <barry/barry.h>
29 using namespace Barry
;
31 // This example is intended to provide similar functionality to the 'usbclient'
32 // example provided as a sample with BlackBerry JDEs. It is intended to
33 // be run with the 'usbdemo' sample application running on the BlackBerry. The
34 // 'usbdemo' sample application is provided as a sample with BlackBerry JDEs.
36 // A more complex use of Barry::RawChannel can be found in tools/brawchannel.cc
38 #define CHANNEL_NAME "JDE_USBClient"
40 #define GCF_HEADER_SIZE 2
42 #define GCF_SIZE(buf) ((size_t)((buf[0] << 8) | (buf[1])))
44 #define HELLO_TO_BB "Hello from PC"
45 #define HELLO_FROM_BB "Hello from Device"
46 #define BYE_TO_BB "Goodbye from PC"
47 #define BYE_FROM_BB "Goodbye from Device"
49 // Function to print the data read
50 static void printReply(Data
&data
)
52 // Need to work out if it's a GCF wrapped reply or a USBPort based
54 // To do this take the first two bytes as a length value and see
55 // if they match the data length.
57 if(data
.GetSize() < GCF_HEADER_SIZE
||
58 GCF_SIZE(data
.GetData()) != (data
.GetSize() - GCF_HEADER_SIZE
) ) {
60 strdata
= string((const char*)data
.GetData(), data
.GetSize());
64 strdata
= string((const char*)data
.GetData() + GCF_HEADER_SIZE
,
65 data
.GetSize() - GCF_HEADER_SIZE
);
67 cout
<< "Received data: " << strdata
<< endl
;
71 int main(int argc
, char *argv
[])
77 if( probe
.GetCount() == 0 ) {
78 cout
<< "No Blackberry found!" << endl
;
83 << probe
.Get(0).m_pin
.Str() << endl
;
87 auto_ptr
<SocketRoutingQueue
> router
;
88 router
.reset(new SocketRoutingQueue());
89 router
->SpinoffSimpleReadThread();
91 // Create our controller object
92 Barry::Controller
con(probe
.Get(0), *router
);
94 // Create the raw channel
95 Barry::Mode::RawChannel
rawChannel(con
);
97 cout
<< "Connecting to device channel with an empty password" << endl
;
98 rawChannel
.Open("", CHANNEL_NAME
);
100 cout
<< "Sending hello to device: " << HELLO_TO_BB
<< endl
;
101 Data
data(HELLO_TO_BB
, sizeof(HELLO_TO_BB
) - 1);
102 rawChannel
.Send(data
);
104 cout
<< "Waiting for reply from device" << endl
;
105 rawChannel
.Receive(data
);
108 cout
<< "Sending bye bye to the device: " << BYE_TO_BB
<< endl
;
109 data
= Data(BYE_TO_BB
, sizeof(BYE_TO_BB
) - 1);
110 rawChannel
.Send(data
);
112 cout
<< "Waiting for response from device" << endl
;
113 rawChannel
.Receive(data
);
116 cout
<< "All send and received OK, exiting" << endl
;
119 catch( Barry::Error
&e
) {
120 cerr
<< "Exception caught: " << e
.what() << endl
;
123 catch( std::exception
&e
) {
124 cerr
<< "Exception caught: " << e
.what() << endl
;