xref: /freebsd/usr.sbin/bluetooth/bthidd/session.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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.1 2004/02/12 22:46:59 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 
41 /*
42  * Create new session
43  */
44 
45 bthid_session_p
46 session_open(bthid_server_p srv, bdaddr_p bdaddr)
47 {
48 	bthid_session_p	s = NULL;
49 
50 	assert(srv != NULL);
51 	assert(bdaddr != NULL);
52 
53 	if ((s = (bthid_session_p) malloc(sizeof(*s))) != NULL) {
54 		s->srv = srv;
55 		memcpy(&s->bdaddr, bdaddr, sizeof(s->bdaddr));
56 		s->ctrl = s->intr = -1;
57 		s->state = CLOSED;
58 
59 		LIST_INSERT_HEAD(&srv->sessions, s, next);
60 	}
61 
62 	return (s);
63 }
64 
65 /*
66  * Lookup session by bdaddr
67  */
68 
69 bthid_session_p
70 session_by_bdaddr(bthid_server_p srv, bdaddr_p bdaddr)
71 {
72 	bthid_session_p	s = NULL;
73 
74 	assert(srv != NULL);
75 	assert(bdaddr != NULL);
76 
77 	LIST_FOREACH(s, &srv->sessions, next)
78 		if (memcmp(&s->bdaddr, bdaddr, sizeof(s->bdaddr)) == 0)
79 			break;
80 
81 	return (s);
82 }
83 
84 /*
85  * Lookup session by fd
86  */
87 
88 bthid_session_p
89 session_by_fd(bthid_server_p srv, int fd)
90 {
91 	bthid_session_p	s = NULL;
92 
93 	assert(srv != NULL);
94 	assert(fd >= 0);
95 
96 	LIST_FOREACH(s, &srv->sessions, next)
97 		if (s->ctrl == fd || s->intr == fd)
98 			break;
99 
100 	return (s);
101 }
102 
103 /*
104  * Close session
105  */
106 
107 void
108 session_close(bthid_session_p s)
109 {
110 	assert(s != NULL);
111 	assert(s->srv != NULL);
112 
113 	LIST_REMOVE(s, next);
114 
115 	if (s->intr != -1) {
116 		FD_CLR(s->intr, &s->srv->rfdset);
117 		FD_CLR(s->intr, &s->srv->wfdset);
118 		close(s->intr);
119 
120 		if (s->srv->maxfd == s->intr)
121 			s->srv->maxfd --;
122 	}
123 
124 	if (s->ctrl != -1) {
125 		FD_CLR(s->ctrl, &s->srv->rfdset);
126 		FD_CLR(s->ctrl, &s->srv->wfdset);
127 		close(s->ctrl);
128 
129 		if (s->srv->maxfd == s->ctrl)
130 			s->srv->maxfd --;
131 	}
132 
133 	memset(s, 0, sizeof(*s));
134 	free(s);
135 }
136 
137