Mon Jan 26 00:56:15 2009 UTC ()
Pull up following revision(s) (requested by plunky in ticket #319):
	usr.sbin/btpand/server.c: revision 1.2
The "Service Availability" value in the SDP record should be a number
from 0-255 indicating how much availability the service has, rather
than the "Load Factor (0-7)" as used in the "Class of Device" setting.


(snj)
diff -r1.1 -r1.1.6.1 src/usr.sbin/btpand/server.c

cvs diff -r1.1 -r1.1.6.1 src/usr.sbin/btpand/server.c (expand / switch to unified diff)

--- src/usr.sbin/btpand/server.c 2008/08/17 13:20:57 1.1
+++ src/usr.sbin/btpand/server.c 2009/01/26 00:56:15 1.1.6.1
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: server.c,v 1.1 2008/08/17 13:20:57 plunky Exp $ */ 1/* $NetBSD: server.c,v 1.1.6.1 2009/01/26 00:56:15 snj Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2008 Iain Hibbert 4 * Copyright (c) 2008 Iain Hibbert
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -16,41 +16,41 @@ @@ -16,41 +16,41 @@
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27 27
28#include <sys/cdefs.h> 28#include <sys/cdefs.h>
29__RCSID("$NetBSD: server.c,v 1.1 2008/08/17 13:20:57 plunky Exp $"); 29__RCSID("$NetBSD: server.c,v 1.1.6.1 2009/01/26 00:56:15 snj Exp $");
30 30
31#include <sys/ioctl.h> 31#include <sys/ioctl.h>
32 32
33#include <bluetooth.h> 33#include <bluetooth.h>
34#include <errno.h> 34#include <errno.h>
35#include <sdp.h> 35#include <sdp.h>
36#include <unistd.h> 36#include <unistd.h>
37 37
38#include "btpand.h" 38#include "btpand.h"
39#include "bnep.h" 39#include "bnep.h"
40 40
41static struct event server_ev; 41static struct event server_ev;
42static int server_fd; 42static int server_fd;
43static int server_load; 43static int server_avail;
44 44
45static void * server_ss; 45static void * server_ss;
46static uint32_t server_handle; 46static uint32_t server_handle;
47 47
48static void server_open(void); 48static void server_open(void);
49static void server_close(void); 49static void server_close(void);
50static void server_read(int, short, void *); 50static void server_read(int, short, void *);
51static void server_register(void); 51static void server_register(void);
52 52
53void 53void
54server_init(void) 54server_init(void)
55{ 55{
56 56
@@ -61,33 +61,33 @@ server_init(void) @@ -61,33 +61,33 @@ server_init(void)
61 * The server_update() function is called whenever the channel count is 61 * The server_update() function is called whenever the channel count is
62 * changed. We maintain the SDP record and open or close the server socket 62 * changed. We maintain the SDP record and open or close the server socket
63 * as required. 63 * as required.
64 */ 64 */
65void 65void
66server_update(int count) 66server_update(int count)
67{ 67{
68 68
69 if (server_limit == 0) 69 if (server_limit == 0)
70 return; 70 return;
71 71
72 log_debug("count %d", count); 72 log_debug("count %d", count);
73 73
74 server_load = (count - 1) * 100 / server_limit; 74 server_avail = UINT8_MAX - (count - 1) * UINT8_MAX / server_limit;
75 log_info("server_load: %d%%", server_load); 75 log_info("Service Availability: %d/%d", server_avail, UINT8_MAX);
76 76
77 if (server_load > 99 && server_fd != -1) 77 if (server_avail == 0 && server_fd != -1)
78 server_close(); 78 server_close();
79 79
80 if (server_load < 100 && server_fd == -1) 80 if (server_avail > 0 && server_fd == -1)
81 server_open(); 81 server_open();
82 82
83 if (service_name) 83 if (service_name)
84 server_register(); 84 server_register();
85} 85}
86 86
87static void 87static void
88server_open(void) 88server_open(void)
89{ 89{
90 struct sockaddr_bt sa; 90 struct sockaddr_bt sa;
91 uint16_t mru; 91 uint16_t mru;
92 92
93 server_fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); 93 server_fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
@@ -250,40 +250,30 @@ static void @@ -250,40 +250,30 @@ static void
250server_register(void) 250server_register(void)
251{ 251{
252 sdp_nap_profile_t p; 252 sdp_nap_profile_t p;
253 int rv; 253 int rv;
254 254
255 if (server_ss == NULL) { 255 if (server_ss == NULL) {
256 server_ss = sdp_open_local(control_path); 256 server_ss = sdp_open_local(control_path);
257 if (server_ss == NULL || sdp_error(server_ss) != 0) { 257 if (server_ss == NULL || sdp_error(server_ss) != 0) {
258 log_err("failed to contact SDP server"); 258 log_err("failed to contact SDP server");
259 return; 259 return;
260 } 260 }
261 } 261 }
262 262
263 memset(&p, 0, sizeof(p)); 263 memset(&p, 0, sizeof(p));
264 264 p.psm = l2cap_psm;
265 p.psm = l2cap_psm; 265 p.load_factor = server_avail;
266 266 p.security_description = (l2cap_mode == 0 ? 0x0000 : 0x0001);
267 if (server_load < 1) p.load_factor = 0; 
268 else if (server_load <= 17) p.load_factor = 1; 
269 else if (server_load <= 33) p.load_factor = 2; 
270 else if (server_load <= 50) p.load_factor = 3; 
271 else if (server_load <= 67) p.load_factor = 4; 
272 else if (server_load <= 83) p.load_factor = 5; 
273 else if (server_load <= 99) p.load_factor = 6; 
274 else p.load_factor = 7; 
275 
276 if (l2cap_mode != 0) p.security_description = 0x0001; 
277 267
278 if (server_handle) 268 if (server_handle)
279 rv = sdp_change_service(server_ss, server_handle, 269 rv = sdp_change_service(server_ss, server_handle,
280 (uint8_t *)&p, sizeof(p)); 270 (uint8_t *)&p, sizeof(p));
281 else 271 else
282 rv = sdp_register_service(server_ss, service_class, 272 rv = sdp_register_service(server_ss, service_class,
283 &local_bdaddr, (uint8_t *)&p, sizeof(p), &server_handle); 273 &local_bdaddr, (uint8_t *)&p, sizeof(p), &server_handle);
284 274
285 if (rv != 0) { 275 if (rv != 0) {
286 errno = sdp_error(server_ss); 276 errno = sdp_error(server_ss);
287 log_err("%s: %m", service_name); 277 log_err("%s: %m", service_name);
288 exit(EXIT_FAILURE); 278 exit(EXIT_FAILURE);
289 } 279 }