1 /* Metageek WiSPY interface
2  * Mike Kershaw/Dragorn <dragorn@kismetwireless.net>
3  *
4  * This code is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This code is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * Extra thanks to Ryan Woodings @ Metageek for interface documentation
15  */
16 
17 #include "config.h"
18 
19 #ifndef __SPECTOOL_GTK_HW_REGISTRY__
20 #define __SPECTOOL_GTK_HW_REGISTRY__
21 
22 #ifdef HAVE_GTK
23 
24 #include <spectool_container.h>
25 #include <glib.h>
26 #include <gtk/gtk.h>
27 #include "spectool_net_client.h"
28 
29 #define WDR_MAX_DEV		32
30 #define WDR_MAX_NET		32
31 
32 /* Sweep callback functions take a sweep and do (something) with it,
33  * and so need:
34  * int slot - slot # of device which triggered
35  * spectool_sample_sweep sweep - the sweep data
36  * void *aux - auxptr they provided (probably their main struct ref
37  *
38  * if sweep is NULL an error occurred and the device should be released.
39  */
40 typedef struct _wdr_reg_sweep_cb {
41 	void *aux;
42 	void (*cb)(int, int, spectool_sample_sweep *, void *);
43 	/* Local queue buf and number to aggregate together */
44 	spectool_sweep_cache *agg_sweep;
45 	int num_agg;
46 	int pos_agg;
47 } wdr_reg_sweep_cb;
48 
49 /* Item in the device registry */
50 typedef struct _wdr_reg_dev {
51 	/* Physical device */
52 	spectool_phy *phydev;
53 	/* List of sweep caches we write into */
54 	GList *sweep_cb_l;
55 	/* Reference count for this hw dev */
56 	int refcount;
57 	int poll_tag;
58 	void *poll_rec;
59 } wdr_reg_dev;
60 
61 typedef struct _wdr_reg_srv {
62 	spectool_server *srv;
63 	void *poll_rec;
64 
65 	int iowtag, iortag;
66 	GIOChannel *ioch;
67 
68 	/* GTK tracking bits for the network selector */
69 	GtkTreeRowReference *tree_row_ref;
70 
71 } wdr_reg_srv;
72 
73 /* Fixed-size device registry.  I don't think it's unreasonable to
74  * assume a user will never have more than 32 devices on a system,
75  * and if I'm wrong, we can mod it easily enough.  Saves a whole lot
76  * of glist mess */
77 typedef struct _spectool_device_registry {
78 	wdr_reg_dev *devices[WDR_MAX_DEV];
79 	int max_dev;
80 	int cur_dev;
81 
82 	wdr_reg_srv *netservers[WDR_MAX_NET];
83 	int max_srv;
84 	int cur_srv;
85 
86 	void *netmanager;
87 
88 	int bcastsock;
89 	GIOChannel *bcioc;
90 	int bcioc_rtag;
91 
92 } spectool_device_registry;
93 
94 /* Passed to gdk_input_add as aux pointer struct */
95 typedef struct _wdr_poll_rec {
96 	spectool_device_registry *wdr;
97 	int slot;
98 	int poll_tag;
99 	int poll_wtag;
100 	GIOChannel *ioch;
101 } wdr_poll_rec;
102 
103 void wdr_init(spectool_device_registry *wdr);
104 void wdr_free(spectool_device_registry *wdr);
105 
106 int wdr_open_add(spectool_device_registry *wdr, spectool_device_rec *devrec, int pos,
107 				 char *errstr);
108 
109 int wdr_open_phy(spectool_device_registry *wdr, spectool_phy *phydev, char *errstr);
110 
111 int wdr_open_net(spectool_device_registry *wdr, char *url, char *errstr);
112 int wdr_open_netptr(spectool_device_registry *wdr, spectool_server *netptr, char *errstr);
113 void wdr_close_net(spectool_device_registry *wdr, int slot);
114 
115 int wdr_enable_bcast(spectool_device_registry *wdr, char *errstr);
116 void wdr_disable_bcast(spectool_device_registry *wdr);
117 gboolean wdr_bcpoll(GIOChannel *ioch, GIOCondition cond, gpointer data);
118 
119 spectool_phy *wdr_get_phy(spectool_device_registry *wdr, int slot);
120 
121 void wdr_add_ref(spectool_device_registry *wdr, int slot);
122 void wdr_del_ref(spectool_device_registry *wdr, int slot);
123 
124 void wdr_add_sweepcb(spectool_device_registry *wdr, int slot,
125 					 void (*cb)(int, int, spectool_sample_sweep *, void *),
126 					 int nagg, void *aux);
127 void wdr_del_sweepcb(spectool_device_registry *wdr, int slot,
128 					 void (*cb)(int, int, spectool_sample_sweep *, void *),
129 					 void *aux);
130 
131 /* Polling function suitable for calling from gdk_input */
132 void wdr_poll(gpointer data, gint source, GdkInputCondition condition);
133 gboolean wdr_netrpoll(GIOChannel *ioch, GIOCondition cond, gpointer data);
134 
135 /* Struct used in menu callbacks from wdr-assisted popup
136  * menus.  Needed to be able to unref all the devices in
137  * the menu, etc.  Contains the slot # of the selected
138  * device plus enough data to unref the phys */
139 typedef struct _wdr_menu_rec {
140 	int slot;
141 	void *aux;
142 	spectool_device_registry *wdr;
143 } wdr_menu_rec;
144 
145 /* Populate a menu and return a reference we have to make sure to
146  * clean up */
147 GList *wdr_populate_menu(spectool_device_registry *wdr, GtkWidget *menu,
148 						 int sep, int fillnodev, GCallback cb, void *aux);
149 void wdr_free_menu(spectool_device_registry *wdr, GList *gl);
150 
151 /* Device picker aux data */
152 typedef struct _wdr_gtk_devpicker_aux {
153 	spectool_device_registry *wdr;
154 
155 	spectool_device_list devlist;
156 
157 	GtkWidget *picker_win;
158 
159 	GtkWidget *okbutton, *cancelbutton, *rescanbutton,
160 			  *scrolled_win;
161 	GtkTreeView *treeview;
162 	GtkTreeViewColumn *treecolumn;
163 	GtkListStore *treemodellist;
164 
165 	/* Callback accepts slot * and its own callback ptr */
166 	void (*pickcb)(int, void *);
167 	void *cbaux;
168 } wdr_gtk_devpicker_aux;
169 
170 /* Spawn a device picker window and call our CB when the user has picked one */
171 void wdr_devpicker_spawn(spectool_device_registry *wdr,
172 						 void (*cb)(int, void *),
173 						 void *aux);
174 
175 typedef struct _wdr_gtk_netmanager_aux {
176 	spectool_device_registry *wdr;
177 
178 	GtkWidget *picker_win;
179 
180 	GtkWidget *addbutton, *dconbutton, *openbutton, *closebutton,
181 			  *scrolled_win;
182 
183 	GtkTreeView *treeview;
184 	GtkTreeViewColumn *treecolumn;
185 	GtkTreeStore *treestore;
186 
187 	GtkTreeRowReference *noservers;
188 
189 	int timer_ref;
190 
191 	/* Callback accepts slot * and its own callback ptr */
192 	void (*pickcb)(int, void *);
193 	void *cbaux;
194 } wdr_gtk_netmanager_aux;
195 
196 void wdr_netmanager_spawn(spectool_device_registry *wdr,
197 						  void (*cb)(int, void *),
198 						  void *aux);
199 
200 typedef struct _wdr_gtk_netentry_aux {
201 	spectool_device_registry *wdr;
202 
203 	spectool_device_list devlist;
204 
205 	GtkWidget *picker_win;
206 	GtkWidget *hostentry, *portentry;
207 	GtkWidget *okbutton, *cancelbutton;
208 
209 } wdr_gtk_netentry_aux;
210 
211 void wdr_netentry_spawn(spectool_device_registry *wdr);
212 
213 #endif
214 
215 #endif
216 
217