menu: added new Keywords tag to .desktop files
[barry.git] / examples / usbclient.cc
blob04642c4e90e554ea574bb2bebbf00e48a3aa0d41
1 ///
2 /// \file usbclient.cc
3 /// Example code using the Barry library to interface
4 /// with a custom USB channel on a Blackberry device.
5 ///
7 /*
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>
24 #include <iostream>
25 #include <cstring>
26 #include <string>
28 using namespace std;
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
53 // reply.
54 // To do this take the first two bytes as a length value and see
55 // if they match the data length.
56 string strdata;
57 if(data.GetSize() < GCF_HEADER_SIZE ||
58 GCF_SIZE(data.GetData()) != (data.GetSize() - GCF_HEADER_SIZE) ) {
59 // USBPort data
60 strdata = string((const char*)data.GetData(), data.GetSize());
62 else {
63 // GCF header present
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[])
73 try {
74 Barry::Init();
76 Barry::Probe probe;
77 if( probe.GetCount() == 0 ) {
78 cout << "No Blackberry found!" << endl;
79 return 1;
81 else {
82 cout << "Using PIN: "
83 << probe.Get(0).m_pin.Str() << endl;
86 // Create the router
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);
106 printReply(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);
114 printReply(data);
116 cout << "All send and received OK, exiting" << endl;
119 catch( Barry::Error &e ) {
120 cerr << "Exception caught: " << e.what() << endl;
121 return 1;
123 catch( std::exception &e ) {
124 cerr << "Exception caught: " << e.what() << endl;
125 return 1;
128 return 0;