Diffstat (limited to 'noncore/net/opietooth/lib/bt-serial.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/opietooth/lib/bt-serial.c | 308 |
1 files changed, 308 insertions, 0 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 | |||
25 | static int hserv = -1; //Server socket | ||
26 | sdp_session_t* session = NULL; //session with an SDP server | ||
27 | |||
28 | static 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 | } | ||
94 | errout: | ||
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 | */ | ||
112 | int 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; | ||
128 | errout: | ||
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 | */ | ||
143 | int 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); | ||
171 | errout: | ||
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 | */ | ||
189 | int 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 | */ | ||
209 | int 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 | */ | ||
235 | int 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 | ||