summaryrefslogtreecommitdiff
path: root/noncore/net/opietooth/lib
Unidiff
Diffstat (limited to 'noncore/net/opietooth/lib') (more/less context) (show whitespace changes)
-rw-r--r--noncore/net/opietooth/lib/bt-serial.c308
-rw-r--r--noncore/net/opietooth/lib/bt-serial.h57
-rw-r--r--noncore/net/opietooth/lib/forwarder.cc203
-rw-r--r--noncore/net/opietooth/lib/forwarder.h45
-rw-r--r--noncore/net/opietooth/lib/lib.pro6
5 files changed, 617 insertions, 2 deletions
diff --git a/noncore/net/opietooth/lib/bt-serial.c b/noncore/net/opietooth/lib/bt-serial.c
new file mode 100644
index 0000000..d1a65a2
--- a/dev/null
+++ b/noncore/net/opietooth/lib/bt-serial.c
@@ -0,0 +1,308 @@
1/* $Id$
2 *Bluetooth serial forwarder functions implementation
3 *
4 *(c) Copyright 2006 GPL
5 *
6 *This software is provided under the GNU public license, incorporated
7 *herein by reference. The software is provided without warranty or
8 *support.
9 */
10#include "bt-serial.h"
11#include <errno.h>
12#include <unistd.h>
13#include <sys/time.h>
14#include <sys/types.h>
15#include <sys/socket.h>
16#include <bluetooth/bluetooth.h>
17#include <bluetooth/rfcomm.h>
18#include <bluetooth/sdp.h>
19#include <bluetooth/sdp_lib.h>
20#include <termios.h>
21#include <sys/stat.h>
22#include <fcntl.h>
23#include <stdio.h>
24
25static int hserv = -1; //Server socket
26sdp_session_t* session = NULL; //session with an SDP server
27
28static sdp_session_t* register_service(uint8_t rfchannel)
29{
30 int err = 0;
31 sdp_profile_desc_t profile[1];
32 uint32_t service_uuid_int[] = { 0, 0, 0, 0xABCD };
33 const char *service_name = "Serial forwarder";
34 const char *service_dsc = "Serial forwarder";
35 const char *service_prov = "OPIE team";
36 uuid_t root_uuid, l2cap_uuid, rfcomm_uuid, svc_uuid, service_uuid;
37 sdp_list_t* l2cap_list = 0;
38 sdp_list_t* rfcomm_list = 0;
39 sdp_list_t* root_list = 0;
40 sdp_list_t* proto_list = 0;
41 sdp_list_t* access_proto_list = 0;
42 sdp_list_t* profile_list = 0;
43 sdp_list_t* service_list = 0;
44 sdp_data_t* channel = 0;
45 sdp_record_t* record = sdp_record_alloc();
46 sdp_session_t* lsession = 0;
47
48 // set the general service ID
49 sdp_uuid128_create(&svc_uuid, &service_uuid_int);
50 sdp_set_service_id(record, svc_uuid);
51 // make the service record publicly browsable
52 sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
53 root_list = sdp_list_append(0, &root_uuid);
54 sdp_set_browse_groups( record, root_list );
55
56 // set l2cap information
57 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
58 l2cap_list = sdp_list_append( 0, &l2cap_uuid );
59 proto_list = sdp_list_append( 0, l2cap_list );
60
61 // set rfcomm information
62 sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
63 channel = sdp_data_alloc(SDP_UINT8, &rfchannel);
64 rfcomm_list = sdp_list_append( 0, &rfcomm_uuid );
65 sdp_list_append( rfcomm_list, channel );
66 sdp_list_append( proto_list, rfcomm_list );
67
68 // attach protocol information to service record
69 access_proto_list = sdp_list_append( 0, proto_list );
70 sdp_set_access_protos( record, access_proto_list );
71
72 sdp_uuid16_create(&service_uuid, SERIAL_PORT_SVCLASS_ID);
73 service_list = sdp_list_append( 0, &service_uuid );
74 sdp_set_service_classes(record, service_list);
75
76 profile[0].version = 0x0100;
77 sdp_uuid16_create(&profile[0].uuid, SERIAL_PORT_PROFILE_ID);
78 profile_list = sdp_list_append(0, &profile[0]);
79 sdp_set_profile_descs(record, profile_list);
80
81 // set the name, provider, and description
82 sdp_set_info_attr(record, service_name, service_prov, service_dsc);
83
84 // connect to the local SDP server, register the service record, and
85 // disconnect
86 lsession = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, SDP_RETRY_IF_BUSY);
87 if (lsession == NULL)
88 goto errout;
89 err = sdp_record_register(lsession, record, 0);
90 if (err) {
91 sdp_close(lsession);
92 lsession = NULL;
93 }
94errout:
95 // cleanup
96 sdp_data_free( channel );
97 sdp_list_free( l2cap_list, 0 );
98 sdp_list_free( rfcomm_list, 0 );
99 sdp_list_free( root_list, 0 );
100 sdp_list_free( access_proto_list, 0 );
101 sdp_list_free( profile_list, 0 );
102 sdp_list_free( service_list, 0 );
103
104 return lsession;
105}
106
107/*
108 * Function opens and configures serial port
109 * portName - name of the serial port
110 * return 0 on success, -1 on error
111 */
112int openSerial(const char* portName)
113{
114 struct termios tc; //port attributes
115 int hserial = -1; //serial port handler
116 int result; //function call result
117 if ((hserial = open(portName, O_RDWR)) < 0)
118 goto errout;
119 if ((result = tcgetattr(hserial, &tc)) < 0)
120 goto errout;
121 cfmakeraw(&tc);
122 cfsetispeed(&tc, B9600);
123 cfsetospeed(&tc, B9600);
124 if ((result = tcsetattr(hserial, TCSANOW, &tc)) < 0)
125 goto errout;
126 if (result == 0)
127 errno = 0;
128errout:
129 if (errno) {
130 if (hserial >= 0) {
131 close(hserial);
132 hserial = -1;
133 }
134 }
135 return hserial;
136}
137
138/*
139 * bt_serialStart
140 * Function starts bt-serial service
141 * return 0 success -1 on error
142 */
143int bt_serialStart(void)
144{
145 struct sockaddr_rc loc_addr; //server address
146 int i; //just an index variable
147 int result = 0; //function call result
148 if (hserv >= 0)
149 return 0;
150 hserv = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
151 // bind socket to port 1 of the first available
152 // local bluetooth adapter
153 memset(&loc_addr, 0, sizeof(struct sockaddr_rc));
154 loc_addr.rc_family = AF_BLUETOOTH;
155 loc_addr.rc_bdaddr = *BDADDR_ANY;
156 for (i = 1; i < 32; i++) {
157 loc_addr.rc_channel = (uint8_t)i;
158 if (!(result = bind(hserv,
159 (struct sockaddr *)&loc_addr,
160 sizeof(loc_addr))) || errno == EINVAL) {
161 break;
162 }
163 }
164 if (result != 0)
165 goto errout;
166 else
167 errno = 0;
168 if (listen(hserv, 1) < 0)
169 goto errout;
170 session = register_service(loc_addr.rc_channel);
171errout:
172 if (errno) {
173 result = errno;
174 close(hserv);
175 hserv = -1;
176 if (session != NULL)
177 sdp_close(session);
178 errno = result;
179 result = -1;
180 }
181 return result;
182}
183
184/*
185 * bt_serialStop
186 * Function stops bt-serial service
187 * return device handler on success -1 on error
188 */
189int bt_serialStop(void)
190{
191 int result = 0; //Function call result
192
193 if (hserv >= 0) {
194 result = close(hserv);
195 hserv = -1;
196 if (session != NULL)
197 sdp_close(session);
198 }
199 return result;
200}
201
202/*
203 * btWrite
204 * hbt - handler of the BT connection
205 * buf - buffer to write
206 * plen - total length to write (and zero it!!!)
207 * return number of bytes written on success or -1
208 */
209int btWrite(int hbt, uint8_t* buf, int* plen)
210{
211 int result; //Function call result
212 const suseconds_t writeDelay = 100000L; //wait after writing
213 result = write(hbt, buf, *plen);
214#ifdef _DEBUG_
215 printf("ser->bt %d\n", *plen);
216#endif
217 *plen = 0;
218 usleep(writeDelay);
219 return result;
220}
221
222/*
223 * bt_serialForward
224 * Function forwards data received from bt-connection to serial and backward
225 * conn - connection handler
226 * portName - name of the serial port to open
227 * return 0 success -1 on error
228 */
229/*
230 * This function has a hack. My BT adapter hangs if you try to write small
231 * portions of data to it to often. That's why we either wait for big enough
232 * (> wrThresh) portion of data from a serial port and write it to BT or
233 * wait for a timeout (tv).
234 */
235int bt_serialForward(BTSerialConn* conn, const char* portName)
236{
237 int result; //Function call result
238 fd_set inSet; //Set we scan for input
239 uint8_t inBuf[1500]; //buffer we read and write
240 uint8_t outBuf[1500]; //buffer we read and write
241 int outBytes; //bytes to be written to bt
242 int nbytes = 0; //number of bytes we could read
243 int maxfd; //maximal filehandler
244 struct timeval tv; //time we shall wait for select
245 const int wrThresh = 250; //threshold after which we send packet to bt
246 const suseconds_t waitDelay = 200000L; //Time (us) we wait for data
247 struct sockaddr_rc rem_addr; //client address
248 int len = sizeof(rem_addr); //argument length
249
250 if (conn == NULL) {
251 errno = EINVAL;
252 return -1;
253 }
254 memset(&rem_addr, 0, sizeof(struct sockaddr_rc));
255 conn->bt_handler = -1;
256 conn->ser_handler = -1;
257 conn->bt_handler = accept(hserv, (struct sockaddr *)&rem_addr, &len);
258 if (conn->bt_handler < 0)
259 return -1;
260 conn->ser_handler = openSerial(portName);
261 if (conn->ser_handler < 0)
262 return -1;
263#ifdef _DEBUG_
264 printf("Connect!\n");
265#endif
266
267 FD_ZERO(&inSet);
268 maxfd = (conn->bt_handler > conn->ser_handler)? conn->bt_handler:
269 conn->ser_handler;
270 outBytes = 0;
271 do {
272 FD_SET(conn->bt_handler, &inSet);
273 FD_SET(conn->ser_handler, &inSet);
274 tv.tv_sec = 0;
275 tv.tv_usec = waitDelay;
276 result = select(maxfd + 1, &inSet, NULL, NULL, &tv);
277 if (result > 0) {
278 if (FD_ISSET(conn->bt_handler, &inSet)) {
279 if ((nbytes = read(conn->bt_handler, inBuf, sizeof(inBuf))) > 0)
280 result = write(conn->ser_handler, inBuf, nbytes);
281#ifdef _DEBUG_
282 printf("bt->ser %d\n", nbytes);
283#endif
284 }
285 if (FD_ISSET(conn->ser_handler, &inSet)) {
286 if ((nbytes = read(conn->ser_handler,
287 outBuf + outBytes, sizeof(outBuf) - outBytes)) > 0) {
288 outBytes += nbytes;
289 if (outBytes > wrThresh)
290 result = btWrite(conn->bt_handler, outBuf, &outBytes);
291 }
292 }
293 } else if (result == 0) {
294 if (outBytes > 0)
295 result = btWrite(conn->bt_handler, outBuf, &outBytes);
296 }
297 } while (result == 0 || (result > 0 && nbytes > 0));
298 if (nbytes <= 0)
299 result = -1;
300 close(conn->bt_handler);
301 close(conn->ser_handler);
302#ifdef _DEBUG_
303 printf("Disconnect!\n");
304#endif
305 return 0;
306}
307
308//eof
diff --git a/noncore/net/opietooth/lib/bt-serial.h b/noncore/net/opietooth/lib/bt-serial.h
new file mode 100644
index 0000000..737e2a0
--- a/dev/null
+++ b/noncore/net/opietooth/lib/bt-serial.h
@@ -0,0 +1,57 @@
1/* $Id$
2 *Bluetooth serial forwarder functions declaration
3 *
4 *(c) Copyright 2006 GPL
5 *
6 *This software is provided under the GNU public license, incorporated
7 *herein by reference. The software is provided without warranty or
8 *support.
9 */
10#ifndef _BT_SERIAL_H_
11#define _BT_SERIAL_H_
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16typedef struct //bt-serial connection handler
17{
18 int bt_handler; //Bluetooth connection handler
19 int ser_handler; //serial port handler
20} BTSerialConn;
21
22/*
23 * bt_serialStart
24 * Function starts bt-serial service
25 * return 0 success -1 on error
26 */
27int bt_serialStart(void);
28
29/*
30 * bt_serialForward
31 * Function forwards data received from bt-connection to serial and backward
32 * conn - connection handler
33 * portName - name of the port to connect
34 * return 0 success -1 on error
35 */
36int bt_serialForward(BTSerialConn* conn, const char* portName);
37
38/*
39 * bt_serialStop
40 * Function stops bt-serial service
41 * return device handler on success -1 on error
42 */
43int bt_serialStop(void);
44
45/*
46 * Function opens and configures serial port
47 * portName - name of the serial port
48 * return 0 on success, -1 on error
49 */
50int openSerial(const char* portName);
51
52#ifdef __cplusplus
53}
54#endif
55
56#endif
57//eof
diff --git a/noncore/net/opietooth/lib/forwarder.cc b/noncore/net/opietooth/lib/forwarder.cc
new file mode 100644
index 0000000..c38f5b8
--- a/dev/null
+++ b/noncore/net/opietooth/lib/forwarder.cc
@@ -0,0 +1,203 @@
1/* $Id$
2 *Bluetooth serial forwarder class implementation
3 *
4 *(c) Copyright 2006 GPL
5 *
6 *This software is provided under the GNU public license, incorporated
7 *herein by reference. The software is provided without warranty or
8 *support.
9 */
10#include "bt-serial.h"
11#include <sys/types.h>
12#include <sys/wait.h>
13#include <errno.h>
14#include <stdlib.h>
15#include <fcntl.h>
16#include <qapplication.h>
17#include <opie2/oprocctrl.h>
18#include "forwarder.h"
19using namespace OpieTooth;
20using namespace Opie::Core;
21using namespace Opie::Core::Internal;
22
23SerialForwarder::SerialForwarder(QString& devName, int dspeed) :
24 OProcess(), device(devName), speed(dspeed)
25{
26 status = false;
27}
28
29SerialForwarder::~SerialForwarder()
30{
31 stop();
32}
33
34
35bool SerialForwarder::start(RunMode runmode, Communication comm)
36{
37 int htmp; //temporary device
38 int result; //call result
39
40 if ( runs )
41 {
42 return false; // cannot start a process that is already running
43 // or if no executable has been assigned
44 }
45 //First, check if serial device is usable
46 htmp = ::openSerial(device);
47 if (htmp < 0)
48 return false;
49 close(htmp);
50
51 run_mode = runmode;
52 status = 0;
53
54 if(::bt_serialStart() < 0)
55 return false;
56
57 if ( !setupCommunication( comm ) )
58 qWarning( "Could not setup Communication!" );
59
60 // We do this in the parent because if we do it in the child process
61 // gdb gets confused when the application runs from gdb.
62 uid_t uid = getuid();
63 gid_t gid = getgid();
64#ifdef HAVE_INITGROUPS
65
66 struct passwd *pw = getpwuid( uid );
67#endif
68
69 int fd[ 2 ];
70 if ( 0 > pipe( fd ) )
71 {
72 fd[ 0 ] = fd[ 1 ] = 0; // Pipe failed.. continue
73 }
74
75 runs = true;
76
77 QApplication::flushX();
78
79 // WABA: Note that we use fork() and not vfork() because
80 // vfork() has unclear semantics and is not standardized.
81 pid_ = fork();
82
83 if ( 0 == pid_ )
84 {
85 if ( fd[ 0 ] )
86 close( fd[ 0 ] );
87 if ( !runPrivileged() )
88 {
89 setgid( gid );
90#if defined( HAVE_INITGROUPS)
91
92 if ( pw )
93 initgroups( pw->pw_name, pw->pw_gid );
94#endif
95
96 setuid( uid );
97 }
98 // The child process
99 if ( !commSetupDoneC() )
100 qWarning( "Could not finish comm setup in child!" );
101
102 setupEnvironment();
103
104 // Matthias
105 if ( run_mode == DontCare )
106 setpgid( 0, 0 );
107 // restore default SIGPIPE handler (Harri)
108 struct sigaction act;
109 sigemptyset( &( act.sa_mask ) );
110 sigaddset( &( act.sa_mask ), SIGPIPE );
111 act.sa_handler = SIG_DFL;
112 act.sa_flags = 0;
113 sigaction( SIGPIPE, &act, 0L );
114
115 // We set the close on exec flag.
116 // Closing of fd[1] indicates that the execvp succeeded!
117 if ( fd[ 1 ] )
118 fcntl( fd[ 1 ], F_SETFD, FD_CLOEXEC );
119 do {
120 BTSerialConn conn; //Connection handler
121 if ( fd[ 1 ] ) {
122 ::close(fd[1]);
123 fd[1] = 0;
124 }
125 result = ::bt_serialForward(&conn, device);
126 } while(result == 0);
127
128 char resultByte = 1;
129 if ( fd[ 1 ] )
130 write( fd[ 1 ], &resultByte, 1 );
131 _exit( -1 );
132 }
133 else if ( -1 == pid_ )
134 {
135 // forking failed
136
137 runs = false;
138 return false;
139 }
140 else
141 {
142 if ( fd[ 1 ] )
143 close( fd[ 1 ] );
144 // the parent continues here
145
146 // Discard any data for stdin that might still be there
147 input_data = 0;
148
149 // Check whether client could be started.
150 if ( fd[ 0 ] )
151 for ( ;; )
152 {
153 char resultByte;
154 int n = ::read( fd[ 0 ], &resultByte, 1 );
155 if ( n == 1 )
156 {
157 // Error
158 runs = false;
159 close( fd[ 0 ] );
160 pid_ = 0;
161 return false;
162 }
163 if ( n == -1 )
164 {
165 if ( ( errno == ECHILD ) || ( errno == EINTR ) )
166 continue; // Ignore
167 }
168 break; // success
169 }
170 if ( fd[ 0 ] )
171 close( fd[ 0 ] );
172
173 if ( !commSetupDoneP() ) // finish communication socket setup for the parent
174 qWarning( "Could not finish comm setup in parent!" );
175
176 if ( run_mode == Block )
177 {
178 commClose();
179
180 // The SIGCHLD handler of the process controller will catch
181 // the exit and set the status
182 while ( runs )
183 {
184 OProcessController::theOProcessController->
185 slotDoHousekeeping( 0 );
186 }
187 runs = FALSE;
188 emit processExited( this );
189 }
190 }
191 return true;
192}
193
194/*
195 * Stop forwarding process
196 */
197int SerialForwarder::stop()
198{
199 kill(SIGTERM);
200 ::bt_serialStop();
201 return 0;
202}
203//eof
diff --git a/noncore/net/opietooth/lib/forwarder.h b/noncore/net/opietooth/lib/forwarder.h
new file mode 100644
index 0000000..f90ed70
--- a/dev/null
+++ b/noncore/net/opietooth/lib/forwarder.h
@@ -0,0 +1,45 @@
1/* $Id$
2 *Bluetooth serial forwarder class declaration
3 *
4 *(c) Copyright 2006 GPL
5 *
6 *This software is provided under the GNU public license, incorporated
7 *herein by reference. The software is provided without warranty or
8 *support.
9 */
10#ifndef OpieTooth_Forwarder_H
11#define OpieTooth_Forwarder_H
12#include <qobject.h>
13#include <qstring.h>
14#include <unistd.h>
15#include <signal.h>
16#include <opie2/oprocess.h>
17namespace Opie {
18 namespace Core {
19 class OProcess;
20 namespace Internal {
21 class OProcessController;
22 }
23 }
24};
25
26namespace OpieTooth {
27 class SerialForwarder : public Opie::Core::OProcess {
28
29 Q_OBJECT
30
31 protected:
32 QString device; //Name of the device
33 int speed; //device speed to set
34 public:
35 SerialForwarder(QString& devName, int speed);
36 ~SerialForwarder();
37 //Function starts the forwarding process
38 virtual bool start( RunMode runmode = NotifyOnExit,
39 Communication comm = NoCommunication );
40 //Stop the forwarding process
41 int stop();
42 };
43};
44#endif
45//eof
diff --git a/noncore/net/opietooth/lib/lib.pro b/noncore/net/opietooth/lib/lib.pro
index 9d3c14d..781bf15 100644
--- a/noncore/net/opietooth/lib/lib.pro
+++ b/noncore/net/opietooth/lib/lib.pro
@@ -1,7 +1,9 @@
1TEMPLATE = lib 1TEMPLATE = lib
2CONFIG += qte warn_on 2CONFIG += qte warn_on
3 HEADERS = connection.h parser.h device.h manager.h remotedevice.h services.h startpanconnection.h startdunconnection.h 3 HEADERS = connection.h parser.h device.h manager.h remotedevice.h services.h \
4 SOURCES = connection.cpp parser.cc device.cc manager.cc remotedevice.cc services.cc startpanconnection.cpp startdunconnection.cpp 4 startpanconnection.h startdunconnection.h bt-serial.h forwarder.h
5 SOURCES = connection.cpp parser.cc device.cc manager.cc remotedevice.cc services.cc \
6 startpanconnection.cpp startdunconnection.cpp bt-serial.c forwarder.cc
5 TARGET = opietooth1 7 TARGET = opietooth1
6INCLUDEPATH += $(OPIEDIR)/include . 8INCLUDEPATH += $(OPIEDIR)/include .
7 DESTDIR = $(OPIEDIR)/lib 9 DESTDIR = $(OPIEDIR)/lib