Swicth to use standard wireless library
[wifihood] / wifiscand / wifiscand.c
1
2 #include <iwlib.h>
3
4 #ifdef HAVE_LIBOSSO
5 #include <libosso.h>
6 #else
7 #include <glib.h>
8 #include <dbus/dbus.h>
9
10 #define WIFISCAN_INTROSPECTION "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n\
11          \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n\
12 <node name=\"/org/javiplx/wifiscan\">\n\
13   <interface name=\"org.javiplx.wifiscan\">\n\
14     <method name=\"wakeup\">\n\
15       <arg name=\"result\" type=\"s\" direction=\"out\" />\n\
16     </method>\n\
17     <method name=\"start\">\n\
18       <arg name=\"result\" type=\"s\" direction=\"out\" />\n\
19     </method>\n\
20     <method name=\"scan\">\n\
21       <arg name=\"result\" type=\"s\" direction=\"out\" />\n\
22     </method>\n\
23     <method name=\"stop\">\n\
24       <arg name=\"result\" type=\"s\" direction=\"out\" />\n\
25     </method>\n\
26   </interface>\n\
27 </node>\n"
28
29 #define OSSO_OK 0
30 #define OSSO_ERROR 1
31
32 #define osso_context_t DBusConnection
33 typedef struct osso_rpc_t_values {
34   int i;
35   char *s;
36   } osso_rpc_t_values;
37 typedef struct osso_rpc_t {
38   int type;
39   osso_rpc_t_values value;
40   } osso_rpc_t;
41 #endif
42
43 #define WIFISCAND_VERSION_STRING  "1.1"
44
45 #define OSSO_NAME    "wifiscan"
46 #define OSSO_SERVICE "org.javiplx."OSSO_NAME
47 #define OSSO_OBJECT  "/org/javiplx/"OSSO_NAME
48 #define OSSO_IFACE   "org.javiplx."OSSO_NAME
49
50
51 typedef struct {
52         char          mac[18];
53         int           rssi;
54         int           noise;
55 } ScanInfo;
56
57
58 /* --------------------------------------------------------------------------- WirelessInterface */
59 typedef struct {
60         wireless_info info;
61         char*         ifname;
62         int           sock;
63 } WirelessInterface;
64
65 /* ------------------------------------------------------------------ WirelessInterface_ScanItem */
66
67 void WirelessInterface_ScanItem(struct wireless_scan* ap, ScanInfo **scaninfo) {
68
69                         *scaninfo = (ScanInfo*) malloc( sizeof(ScanInfo) );
70                         memset(*scaninfo, 0, sizeof(ScanInfo) );
71
72                         iw_sawap_ntop( &ap->ap_addr, (*scaninfo)->mac);
73
74                         (*scaninfo)->rssi = ap->stats.qual.level - 0x100;
75                         (*scaninfo)->noise = ap->stats.qual.level - 0x100;
76
77 }
78
79 /* ---------------------------------------------------------------------- */
80
81 void* makeScan(WirelessInterface* wlan, gchar *message) {
82         wireless_scan_head context;
83         iwrange        range;
84         int delay = 1;
85
86         ScanInfo        **scan = NULL;
87
88         iw_get_range_info(wlan->sock, wlan->ifname, &range);
89
90         /* We don't call iw_scan to allow fine control of delays and timeouts */
91         context.result = NULL;
92         context.retry = 0;
93
94         while(delay>0) {
95                 delay = iw_process_scan(wlan->sock, wlan->ifname, range.we_version_compiled, &context);
96                 if(delay < 0) break;
97                 usleep(delay * 1000);
98                 }
99
100         if ( delay == 0 ) {
101
102                 scan = (ScanInfo**) malloc( 25 * sizeof(ScanInfo *) );
103                 memset(scan, 0, 25 * sizeof(ScanInfo *) );
104                 *scan = NULL;
105                 int i;
106                 for (i=0; i<25; i++) *(scan+i) = NULL;
107
108                 i = 0;
109                 ScanInfo *sc = NULL;
110
111                 /* Extract values */
112                 struct wireless_scan *ap = context.result;
113                 while ( ap != NULL && i < 25 ) {
114                                 WirelessInterface_ScanItem(ap, &sc);
115                                         *(scan+i) = sc;
116                                         ap = ap->next;
117                                         i++;
118                         }
119
120         } else {
121                 message = "Unknown error";
122         }
123
124         return scan;
125 }
126
127 static void refresh(WirelessInterface* coso) {
128         struct iwreq wrq;
129         
130         iw_get_basic_config(coso->sock, coso->ifname, &(coso->info.b));
131         iw_get_range_info(coso->sock, coso->ifname, &(coso->info.range));
132
133         iw_get_ext(coso->sock, coso->ifname, SIOCGIWRATE, &wrq);
134         memcpy(&(coso->info.bitrate), &wrq.u.bitrate, sizeof(iwparam));
135
136         iw_get_ext(coso->sock, coso->ifname, SIOCGIWAP, &wrq);
137         memcpy(&(coso->info.ap_addr), &wrq.u.ap_addr, sizeof (sockaddr));
138
139         iw_get_stats(
140                 coso->sock, coso->ifname, &(coso->info.stats), 
141                 &(coso->info.range), coso->info.has_range
142         );
143 }
144
145 /* Application UI data struct */
146 typedef struct _AppData AppData;
147 struct _AppData {
148     WirelessInterface iface;
149     osso_context_t *osso_context;
150 };
151
152 #ifdef HAVE_LIBOSSO
153 static GMainLoop *event_loop = NULL;
154 #endif
155 static short int start_flags = 0;
156
157 /* Callback for normal D-BUS messages */
158 gint dbus_req_handler(const gchar * interface, const gchar * method,
159                       GArray * arguments, gpointer data,
160                       osso_rpc_t * retval)
161 {
162     AppData *appdata;
163     appdata = (AppData *) data;
164
165     retval->type = DBUS_TYPE_STRING;
166     retval->value.s = (gchar*) malloc( sizeof(gchar *) );
167     retval->value.s[0] = '\0';
168
169 #ifndef HAVE_LIBOSSO
170     if ( strcmp(method,"Introspect")==0 ) {
171         retval->value.s = (gchar *) realloc(retval->value.s,630*sizeof(gchar *));
172         snprintf(retval->value.s,strlen(WIFISCAN_INTROSPECTION),WIFISCAN_INTROSPECTION);
173         return OSSO_OK;
174     }
175 #endif
176
177     if ( strcmp(method,"wakeup")==0 ) {
178         retval->value.s = (gchar *) realloc(retval->value.s,16*sizeof(gchar *));
179         snprintf(retval->value.s,16,"WifiScand ready");
180         return OSSO_OK;
181     }
182
183     if ( strcmp(method,"start")==0 ) {
184
185         if( (appdata->iface.sock=iw_sockets_open()) < 0) {
186             retval->value.s = (gchar *) realloc(retval->value.s,33*sizeof(gchar *));
187             snprintf(retval->value.s,33,"Failure in socket initialization");
188             return OSSO_ERROR;
189         }
190
191         struct ifreq frq;
192         strncpy(frq.ifr_name, appdata->iface.ifname, IFNAMSIZ);
193         if(ioctl(appdata->iface.sock, SIOCGIFFLAGS, &frq)) {
194             retval->value.s = (gchar *) realloc(retval->value.s,28*sizeof(gchar *));
195             snprintf(retval->value.s,28,"Cannot get interface status");
196             return OSSO_ERROR;
197         }
198
199         start_flags = frq.ifr_flags;
200         frq.ifr_flags |= IFF_UP | IFF_RUNNING;
201
202         if(ioctl(appdata->iface.sock, SIOCSIFFLAGS, &frq)) {
203             retval->value.s = (gchar *) realloc(retval->value.s,27*sizeof(gchar *));
204             snprintf(retval->value.s,27,"Cannot set interface state");
205             return OSSO_ERROR;
206         }
207
208         refresh(&appdata->iface);
209         retval->value.s = (gchar *) realloc(retval->value.s,22*sizeof(gchar *));
210         snprintf(retval->value.s,22,"Interface initialized");
211         return OSSO_OK;
212     }
213
214     if ( strcmp(method,"stop")==0 ) {
215         struct ifreq frq;
216         strncpy(frq.ifr_name, appdata->iface.ifname, IFNAMSIZ);
217         if(!ioctl(appdata->iface.sock, SIOCGIFFLAGS, &frq)) {
218             frq.ifr_flags = start_flags;
219             if(!ioctl(appdata->iface.sock, SIOCSIFFLAGS, &frq))
220                 refresh(&appdata->iface);
221         }
222         iw_sockets_close(appdata->iface.sock);
223         appdata->iface.sock = 0;
224 #ifdef HAVE_LIBOSSO
225         osso_deinitialize(appdata->osso_context);
226         /* Instead of exiting, signaling finish to main loop could be better
227         retval->value.s = (gchar *) realloc(retval->value.s,34*sizeof(gchar *));
228         snprintf(retval->value.s,34,"Interface moved to original state");
229         */
230         exit(0);
231 #else
232         return OSSO_OK;
233 #endif
234     }
235
236     if ( strcmp(method,"scan")==0 ) {
237
238         ScanInfo **scan = (ScanInfo **) makeScan(&appdata->iface,retval->value.s);
239         if(scan == NULL) {
240             retval->value.s = (gchar *) realloc(retval->value.s,64*sizeof(gchar *));
241             snprintf(retval->value.s,64,"ERROR");
242             return OSSO_ERROR;
243         }
244
245         int i;
246         for (i=0; i<25&&*(scan+i)!=NULL; i++) {
247             ScanInfo* sc = *(scan+i);
248             retval->value.s = (gchar *) realloc(retval->value.s,23*(i+1)*sizeof(gchar *));
249             if ( retval->value.s == NULL ) {
250                 retval->value.s = "Error allocating memory";
251                 return OSSO_ERROR;
252             }
253             sprintf(retval->value.s+strlen(retval->value.s),"%s:%d ",sc->mac,sc->rssi);
254         }
255
256         retval->value.s[strlen(retval->value.s)-1]  = '\0';
257         return OSSO_OK;
258     }
259
260     retval->value.s = (gchar *) realloc(retval->value.s,64*sizeof(gchar *));
261     snprintf(retval->value.s,64,"Unknown method");
262     return OSSO_ERROR;
263 }
264
265 #ifndef HAVE_LIBOSSO
266
267 dbus_bool_t stopped_service = FALSE;
268
269 static DBusObjectPathVTable *vtable = NULL;
270
271 int dbus_set_cb_f( DBusConnection *context,
272                 const char *service, const char *object,
273                 const char *interface, void *handler, // FIXME : Set proper type for handler
274                 void *user_data) {
275
276         /* First, we prepare the complex dbus handler structures */
277         DBusObjectPathVTable *vtable = malloc( sizeof(DBusObjectPathVTable) );
278         memset(vtable , '\0', sizeof(DBusObjectPathVTable) );
279         vtable->message_function = (DBusObjectPathMessageFunction) handler; // FIXME : Aqui va el nuevo handler/wrapper
280
281         if (!dbus_connection_register_fallback(context, object, vtable, user_data)) {
282                 return OSSO_ERROR;
283                 }
284
285         DBusError error;
286         dbus_error_init(&error);
287
288         dbus_bus_request_name (context, service, 0, &error);
289         if ( dbus_error_is_set(&error) ) {
290                 dbus_error_free (&error);
291                 return OSSO_ERROR;
292                 }
293
294         dbus_error_free (&error);
295         return OSSO_OK;
296 }
297
298 void dbus_deinitialize( DBusConnection *context ) {
299         free(vtable); vtable = NULL;
300         dbus_connection_unref(context);
301         dbus_shutdown();
302 }
303
304 static DBusHandlerResult handler_wrapper (DBusConnection  *connection,
305                    DBusMessage     *message,
306                    void            *data) {
307
308         gint retcode;
309         osso_rpc_t *retval = malloc(sizeof(osso_rpc_t));
310
311         if ( dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ) {
312                 retcode = dbus_req_handler(dbus_message_get_interface(message),
313                                 dbus_message_get_member(message),
314                                 NULL, data, retval);
315
316                 if ( strcmp(dbus_message_get_member(message),"stop")==0 )
317                         stopped_service = TRUE;
318
319                 if ( retval->value.s != NULL ) {
320                         DBusMessage *reply = dbus_message_new_method_return (message);
321                         if (reply == NULL)
322                                 exit(0);
323                         if (!dbus_message_append_args (reply, DBUS_TYPE_STRING, &retval->value.s, DBUS_TYPE_INVALID))
324                                 exit(0);
325                         if (!dbus_connection_send (connection, reply, NULL))
326                                 exit(0);
327                         dbus_message_unref (reply);
328                         }
329
330                 }
331
332         return DBUS_HANDLER_RESULT_HANDLED;
333 }
334
335 #endif
336
337 int main( void ) {
338
339         osso_context_t *osso_context;
340 #ifndef HAVE_LIBOSSO
341         DBusError error;
342 #endif
343
344         /* Initialize maemo application */
345 #ifdef HAVE_LIBOSSO
346         osso_context = osso_initialize(OSSO_NAME, WIFISCAND_VERSION_STRING, TRUE, NULL);
347 #else
348         dbus_error_init(&error);
349         osso_context = dbus_bus_get(DBUS_BUS_SESSION, &error);
350 #endif
351
352         /* Check that initialization was ok */
353         if (osso_context == NULL) {
354 #ifndef HAVE_LIBOSSO
355                 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n", error.message);
356                 dbus_error_free (&error);
357 #endif
358                 exit(OSSO_ERROR);
359                 }
360
361         /* Create AppData */
362         AppData *appdata;
363         appdata = g_new0(AppData, 1);
364         appdata->osso_context = osso_context;
365
366         memset(&(appdata->iface.info), 0, sizeof(wireless_info));
367         appdata->iface.ifname = "wlan0";
368         appdata->iface.sock   = 0;
369
370         /* Add handler for hello D-BUS messages */
371 #ifdef HAVE_LIBOSSO
372         osso_return_t result = osso_rpc_set_cb_f(osso_context, OSSO_SERVICE, OSSO_OBJECT, OSSO_IFACE, dbus_req_handler, appdata);
373 #else
374         int result = dbus_set_cb_f(osso_context, OSSO_SERVICE, OSSO_OBJECT, OSSO_IFACE, handler_wrapper, appdata);
375 #endif
376
377         if (result != OSSO_OK) {
378 #ifdef HAVE_LIBOSSO
379                 osso_system_note_infoprint(appdata->osso_context, "Failure while setting OSSO callback", NULL);
380 #endif
381                 return OSSO_ERROR;
382         }
383
384         /* INITIALIZATION FINISH */
385
386 #ifdef HAVE_LIBOSSO
387         event_loop = g_main_loop_new(NULL, FALSE);
388         g_main_loop_run(event_loop);
389 #else
390         while (dbus_connection_read_write_dispatch(osso_context, -1) && stopped_service==FALSE) {}
391 #endif
392
393         /* Deinitialize OSSO */
394 #ifdef HAVE_LIBOSSO
395         osso_deinitialize(osso_context);
396 #else
397         dbus_deinitialize(osso_context);
398 #endif
399
400         exit(0);
401 }
402