xref: /freebsd/usr.sbin/bluetooth/bthidd/session.c (revision c0b9f4fe659b6839541970eb5675e57f4d814969)
1 /*
2  * session.c
3  *
4  * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $Id: session.c,v 1.2 2004/11/17 21:59:42 max Exp $
29  * $FreeBSD$
30  */
31 
32 #include <sys/queue.h>
33 #include <assert.h>
34 #include <bluetooth.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include "bthidd.h"
40 #include "kbd.h"
41 
42 /*
43  * Create new session
44  */
45 
46 bthid_session_p
47 session_open(bthid_server_p srv, bdaddr_p bdaddr)
48 {
49 	bthid_session_p	s = NULL;
50 
51 	assert(srv != NULL);
52 	assert(bdaddr != NULL);
53 
54 	if ((s = (bthid_session_p) malloc(sizeof(*s))) != NULL) {
55 		s->srv = srv;
56 		memcpy(&s->bdaddr, bdaddr, sizeof(s->bdaddr));
57 		s->ctrl = -1;
58 		s->intr = -1;
59 		s->state = CLOSED;
60 		s->keys = bit_alloc(kbd_maxkey());
61 		if (s->keys == NULL) {
62 			free(s);
63 			s = NULL;
64 		} else
65 			LIST_INSERT_HEAD(&srv->sessions, s, next);
66 	}
67 
68 	return (s);
69 }
70 
71 /*
72  * Lookup session by bdaddr
73  */
74 
75 bthid_session_p
76 session_by_bdaddr(bthid_server_p srv, bdaddr_p bdaddr)
77 {
78 	bthid_session_p	s = NULL;
79 
80 	assert(srv != NULL);
81 	assert(bdaddr != NULL);
82 
83 	LIST_FOREACH(s, &srv->sessions, next)
84 		if (memcmp(&s->bdaddr, bdaddr, sizeof(s->bdaddr)) == 0)
85 			break;
86 
87 	return (s);
88 }
89 
90 /*
91  * Lookup session by fd
92  */
93 
94 bthid_session_p
95 session_by_fd(bthid_server_p srv, int fd)
96 {
97 	bthid_session_p	s = NULL;
98 
99 	assert(srv != NULL);
100 	assert(fd >= 0);
101 
102 	LIST_FOREACH(s, &srv->sessions, next)
103 		if (s->ctrl == fd || s->intr == fd)
104 			break;
105 
106 	return (s);
107 }
108 
109 /*
110  * Close session
111  */
112 
113 void
114 session_close(bthid_session_p s)
115 {
116 	assert(s != NULL);
117 	assert(s->srv != NULL);
118 
119 	LIST_REMOVE(s, next);
120 
121 	if (s->intr != -1) {
122 		FD_CLR(s->intr, &s->srv->rfdset);
123 		FD_CLR(s->intr, &s->srv->wfdset);
124 		close(s->intr);
125 
126 		if (s->srv->maxfd == s->intr)
127 			s->srv->maxfd --;
128 	}
129 
130 	if (s->ctrl != -1) {
131 		FD_CLR(s->ctrl, &s->srv->rfdset);
132 		FD_CLR(s->ctrl, &s->srv->wfdset);
133 		close(s->ctrl);
134 
135 		if (s->srv->maxfd == s->ctrl)
136 			s->srv->maxfd --;
137 	}
138 
139 	free(s->keys);
140 
141 	memset(s, 0, sizeof(*s));
142 	free(s);
143 }
144 
145