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\
17 <method name=\"start\">\n\
18 <arg name=\"result\" type=\"s\" direction=\"out\" />\n\
20 <method name=\"scan\">\n\
21 <arg name=\"result\" type=\"s\" direction=\"out\" />\n\
23 <method name=\"stop\">\n\
24 <arg name=\"result\" type=\"s\" direction=\"out\" />\n\
32 #define osso_context_t DBusConnection
33 typedef struct osso_rpc_t_values {
37 typedef struct osso_rpc_t {
39 osso_rpc_t_values value;
43 #define WIFISCAND_VERSION_STRING "1.1"
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
58 /* --------------------------------------------------------------------------- WirelessInterface */
65 /* ------------------------------------------------------------------ WirelessInterface_ScanItem */
67 static int WirelessInterface_ScanItem(struct iw_event* event, ScanInfo **scaninfo) {
73 *scaninfo = (ScanInfo*) malloc( sizeof(ScanInfo) );
74 memset(*scaninfo, 0, sizeof(ScanInfo) );
76 iw_ether_ntop( (const struct ether_addr*)(event->u.ap_addr.sa_data), (*scaninfo)->mac);
81 (*scaninfo)->rssi = event->u.qual.level - 0x100;
91 /* ---------------------------------------------------------------------- */
93 void* makeScan(WirelessInterface* coso, gchar *message) {
95 unsigned char* buffer = NULL;
96 int buflen = IW_SCAN_MAX_DATA;
100 int timeout = 10000000;
102 ScanInfo **scan = NULL;
104 has_range = (iw_get_range_info(coso->sock, coso->ifname, &range) >= 0);
108 wrq.u.data.pointer = NULL;
109 wrq.u.data.flags = 0;
110 wrq.u.data.length = 0;
112 if(iw_set_ext(coso->sock, coso->ifname, SIOCSIWSCAN, &wrq) < 0) {
114 message = "Interface doesn't support scanning";
119 timeout -= tv.tv_usec;
129 ret = select(last_fd + 1, &rfds, NULL, NULL, &tv);
132 if(errno == EAGAIN || errno == EINTR) continue;
135 message = "Unknown scanning error";
141 unsigned char* newbuf;
144 newbuf = (unsigned char*) realloc(buffer, buflen);
147 if(buffer) free(buffer);
148 message = "Memory allocation failure in scan";
154 wrq.u.data.pointer = buffer;
155 wrq.u.data.flags = 0;
156 wrq.u.data.length = buflen;
158 if(iw_get_ext(coso->sock, coso->ifname, SIOCGIWSCAN, &wrq) < 0) {
159 if((errno == E2BIG)) {
160 if(wrq.u.data.length > buflen) buflen = wrq.u.data.length;
166 if(errno == EAGAIN) {
169 timeout -= tv.tv_usec;
171 if(timeout > 0) continue;
176 message = "Unable to read scan data";
186 if(wrq.u.data.length) {
190 void* scan_dict = NULL;
192 iw_init_event_stream(&stream, (char*)(buffer), wrq.u.data.length);
194 scan = (ScanInfo**) malloc( 25 * sizeof(ScanInfo *) );
195 memset(scan, 0, 25 * sizeof(ScanInfo *) );
198 for (i=0; i<25; i++) *(scan+i) = NULL;
203 ret = iw_extract_event_stream(&stream, &iwe, range.we_version_compiled);
206 int sr = WirelessInterface_ScanItem(&iwe, &sc);
217 message = "Unknown error";
226 static void refresh(WirelessInterface* coso) {
229 iw_get_basic_config(coso->sock, coso->ifname, &(coso->info.b));
230 iw_get_range_info(coso->sock, coso->ifname, &(coso->info.range));
232 iw_get_ext(coso->sock, coso->ifname, SIOCGIWRATE, &wrq);
233 memcpy(&(coso->info.bitrate), &wrq.u.bitrate, sizeof(iwparam));
235 iw_get_ext(coso->sock, coso->ifname, SIOCGIWAP, &wrq);
236 memcpy(&(coso->info.ap_addr), &wrq.u.ap_addr, sizeof (sockaddr));
239 coso->sock, coso->ifname, &(coso->info.stats),
240 &(coso->info.range), coso->info.has_range
244 /* Application UI data struct */
245 typedef struct _AppData AppData;
247 WirelessInterface iface;
248 osso_context_t *osso_context;
252 static GMainLoop *event_loop = NULL;
254 static short int start_flags = 0;
256 /* Callback for normal D-BUS messages */
257 gint dbus_req_handler(const gchar * interface, const gchar * method,
258 GArray * arguments, gpointer data,
262 appdata = (AppData *) data;
264 retval->type = DBUS_TYPE_STRING;
265 retval->value.s = (gchar*) malloc( sizeof(gchar *) );
266 retval->value.s[0] = '\0';
268 if ( strcmp(method,"Introspect")==0 ) {
269 retval->value.s = (gchar *) realloc(retval->value.s,630*sizeof(gchar *));
270 snprintf(retval->value.s,strlen(WIFISCAN_INTROSPECTION),WIFISCAN_INTROSPECTION);
274 if ( strcmp(method,"wakeup")==0 ) {
275 retval->value.s = (gchar *) realloc(retval->value.s,16*sizeof(gchar *));
276 snprintf(retval->value.s,16,"WifiScand ready");
280 if ( strcmp(method,"start")==0 ) {
282 if( (appdata->iface.sock=iw_sockets_open()) < 0) {
283 retval->value.s = (gchar *) realloc(retval->value.s,33*sizeof(gchar *));
284 snprintf(retval->value.s,33,"Failure in socket initialization");
289 strncpy(frq.ifr_name, appdata->iface.ifname, IFNAMSIZ);
290 if(ioctl(appdata->iface.sock, SIOCGIFFLAGS, &frq)) {
291 retval->value.s = (gchar *) realloc(retval->value.s,28*sizeof(gchar *));
292 snprintf(retval->value.s,28,"Cannot get interface status");
296 start_flags = frq.ifr_flags;
297 frq.ifr_flags |= IFF_UP | IFF_RUNNING;
299 if(ioctl(appdata->iface.sock, SIOCSIFFLAGS, &frq)) {
300 retval->value.s = (gchar *) realloc(retval->value.s,27*sizeof(gchar *));
301 snprintf(retval->value.s,27,"Cannot set interface state");
305 refresh(&appdata->iface);
306 retval->value.s = (gchar *) realloc(retval->value.s,22*sizeof(gchar *));
307 snprintf(retval->value.s,22,"Interface initialized");
311 if ( strcmp(method,"stop")==0 ) {
313 strncpy(frq.ifr_name, appdata->iface.ifname, IFNAMSIZ);
314 if(!ioctl(appdata->iface.sock, SIOCGIFFLAGS, &frq)) {
315 frq.ifr_flags = start_flags;
316 if(!ioctl(appdata->iface.sock, SIOCSIFFLAGS, &frq))
317 refresh(&appdata->iface);
319 iw_sockets_close(appdata->iface.sock);
320 appdata->iface.sock = 0;
322 osso_deinitialize(appdata->osso_context);
323 /* Instead of exiting, signaling finish to main loop could be better
324 retval->value.s = (gchar *) realloc(retval->value.s,34*sizeof(gchar *));
325 snprintf(retval->value.s,34,"Interface moved to original state");
333 if ( strcmp(method,"scan")==0 ) {
335 ScanInfo **scan = (ScanInfo **) makeScan(&appdata->iface,retval->value.s);
337 retval->value.s = (gchar *) realloc(retval->value.s,64*sizeof(gchar *));
338 snprintf(retval->value.s,64,"ERROR");
343 for (i=0; i<25&&*(scan+i)!=NULL; i++) {
344 ScanInfo* sc = *(scan+i);
345 retval->value.s = (gchar *) realloc(retval->value.s,23*(i+1)*sizeof(gchar *));
346 if ( retval->value.s == NULL ) {
347 retval->value.s = "Error allocating memory";
350 sprintf(retval->value.s+strlen(retval->value.s),"%s:%d ",sc->mac,sc->rssi);
353 retval->value.s[strlen(retval->value.s)-1] = '\0';
357 retval->value.s = (gchar *) realloc(retval->value.s,64*sizeof(gchar *));
358 snprintf(retval->value.s,64,"Unknown method");
364 dbus_bool_t stopped_service = FALSE;
366 static DBusObjectPathVTable *vtable = NULL;
368 int dbus_set_cb_f( DBusConnection *context,
369 const char *service, const char *object,
370 const char *interface, void *handler, // FIXME : Set proper type for handler
373 /* First, we prepare the complex dbus handler structures */
374 DBusObjectPathVTable *vtable = malloc( sizeof(DBusObjectPathVTable) );
375 memset(vtable , '\0', sizeof(DBusObjectPathVTable) );
376 vtable->message_function = (DBusObjectPathMessageFunction) handler; // FIXME : Aqui va el nuevo handler/wrapper
378 if (!dbus_connection_register_fallback(context, object, vtable, user_data)) {
383 dbus_error_init(&error);
385 dbus_bus_request_name (context, service, 0, &error);
386 if ( dbus_error_is_set(&error) ) {
387 dbus_error_free (&error);
391 dbus_error_free (&error);
395 void dbus_deinitialize( DBusConnection *context ) {
396 free(vtable); vtable = NULL;
397 dbus_connection_unref(context);
401 static DBusHandlerResult handler_wrapper (DBusConnection *connection,
402 DBusMessage *message,
406 osso_rpc_t *retval = malloc(sizeof(osso_rpc_t));
408 if ( dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ) {
409 retcode = dbus_req_handler(dbus_message_get_interface(message),
410 dbus_message_get_member(message),
413 if ( strcmp(dbus_message_get_member(message),"stop")==0 )
414 stopped_service = TRUE;
416 if ( retval->value.s != NULL ) {
417 DBusMessage *reply = dbus_message_new_method_return (message);
420 if (!dbus_message_append_args (reply, DBUS_TYPE_STRING, &retval->value.s, DBUS_TYPE_INVALID))
422 if (!dbus_connection_send (connection, reply, NULL))
424 dbus_message_unref (reply);
429 return DBUS_HANDLER_RESULT_HANDLED;
436 osso_context_t *osso_context;
441 /* Initialize maemo application */
443 osso_context = osso_initialize(OSSO_NAME, WIFISCAND_VERSION_STRING, TRUE, NULL);
445 dbus_error_init(&error);
446 osso_context = dbus_bus_get(DBUS_BUS_SESSION, &error);
449 /* Check that initialization was ok */
450 if (osso_context == NULL) {
452 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n", error.message);
453 dbus_error_free (&error);
460 appdata = g_new0(AppData, 1);
461 appdata->osso_context = osso_context;
463 memset(&(appdata->iface.info), 0, sizeof(wireless_info));
464 appdata->iface.ifname = "wlan0";
465 appdata->iface.sock = 0;
467 /* Add handler for hello D-BUS messages */
469 osso_return_t result = osso_rpc_set_cb_f(osso_context, OSSO_SERVICE, OSSO_OBJECT, OSSO_IFACE, dbus_req_handler, appdata);
471 int result = dbus_set_cb_f(osso_context, OSSO_SERVICE, OSSO_OBJECT, OSSO_IFACE, handler_wrapper, appdata);
474 if (result != OSSO_OK) {
476 osso_system_note_infoprint(appdata->osso_context, "Failure while setting OSSO callback", NULL);
481 /* INITIALIZATION FINISH */
484 event_loop = g_main_loop_new(NULL, FALSE);
485 g_main_loop_run(event_loop);
487 while (dbus_connection_read_write_dispatch(osso_context, -1) && stopped_service==FALSE) {}
490 /* Deinitialize OSSO */
492 osso_deinitialize(osso_context);
494 dbus_deinitialize(osso_context);