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';
269 if ( strcmp(method,"Introspect")==0 ) {
270 retval->value.s = (gchar *) realloc(retval->value.s,630*sizeof(gchar *));
271 snprintf(retval->value.s,strlen(WIFISCAN_INTROSPECTION),WIFISCAN_INTROSPECTION);
276 if ( strcmp(method,"wakeup")==0 ) {
277 retval->value.s = (gchar *) realloc(retval->value.s,16*sizeof(gchar *));
278 snprintf(retval->value.s,16,"WifiScand ready");
282 if ( strcmp(method,"start")==0 ) {
284 if( (appdata->iface.sock=iw_sockets_open()) < 0) {
285 retval->value.s = (gchar *) realloc(retval->value.s,33*sizeof(gchar *));
286 snprintf(retval->value.s,33,"Failure in socket initialization");
291 strncpy(frq.ifr_name, appdata->iface.ifname, IFNAMSIZ);
292 if(ioctl(appdata->iface.sock, SIOCGIFFLAGS, &frq)) {
293 retval->value.s = (gchar *) realloc(retval->value.s,28*sizeof(gchar *));
294 snprintf(retval->value.s,28,"Cannot get interface status");
298 start_flags = frq.ifr_flags;
299 frq.ifr_flags |= IFF_UP | IFF_RUNNING;
301 if(ioctl(appdata->iface.sock, SIOCSIFFLAGS, &frq)) {
302 retval->value.s = (gchar *) realloc(retval->value.s,27*sizeof(gchar *));
303 snprintf(retval->value.s,27,"Cannot set interface state");
307 refresh(&appdata->iface);
308 retval->value.s = (gchar *) realloc(retval->value.s,22*sizeof(gchar *));
309 snprintf(retval->value.s,22,"Interface initialized");
313 if ( strcmp(method,"stop")==0 ) {
315 strncpy(frq.ifr_name, appdata->iface.ifname, IFNAMSIZ);
316 if(!ioctl(appdata->iface.sock, SIOCGIFFLAGS, &frq)) {
317 frq.ifr_flags = start_flags;
318 if(!ioctl(appdata->iface.sock, SIOCSIFFLAGS, &frq))
319 refresh(&appdata->iface);
321 iw_sockets_close(appdata->iface.sock);
322 appdata->iface.sock = 0;
324 osso_deinitialize(appdata->osso_context);
325 /* Instead of exiting, signaling finish to main loop could be better
326 retval->value.s = (gchar *) realloc(retval->value.s,34*sizeof(gchar *));
327 snprintf(retval->value.s,34,"Interface moved to original state");
335 if ( strcmp(method,"scan")==0 ) {
337 ScanInfo **scan = (ScanInfo **) makeScan(&appdata->iface,retval->value.s);
339 retval->value.s = (gchar *) realloc(retval->value.s,64*sizeof(gchar *));
340 snprintf(retval->value.s,64,"ERROR");
345 for (i=0; i<25&&*(scan+i)!=NULL; i++) {
346 ScanInfo* sc = *(scan+i);
347 retval->value.s = (gchar *) realloc(retval->value.s,23*(i+1)*sizeof(gchar *));
348 if ( retval->value.s == NULL ) {
349 retval->value.s = "Error allocating memory";
352 sprintf(retval->value.s+strlen(retval->value.s),"%s:%d ",sc->mac,sc->rssi);
355 retval->value.s[strlen(retval->value.s)-1] = '\0';
359 retval->value.s = (gchar *) realloc(retval->value.s,64*sizeof(gchar *));
360 snprintf(retval->value.s,64,"Unknown method");
366 dbus_bool_t stopped_service = FALSE;
368 static DBusObjectPathVTable *vtable = NULL;
370 int dbus_set_cb_f( DBusConnection *context,
371 const char *service, const char *object,
372 const char *interface, void *handler, // FIXME : Set proper type for handler
375 /* First, we prepare the complex dbus handler structures */
376 DBusObjectPathVTable *vtable = malloc( sizeof(DBusObjectPathVTable) );
377 memset(vtable , '\0', sizeof(DBusObjectPathVTable) );
378 vtable->message_function = (DBusObjectPathMessageFunction) handler; // FIXME : Aqui va el nuevo handler/wrapper
380 if (!dbus_connection_register_fallback(context, object, vtable, user_data)) {
385 dbus_error_init(&error);
387 dbus_bus_request_name (context, service, 0, &error);
388 if ( dbus_error_is_set(&error) ) {
389 dbus_error_free (&error);
393 dbus_error_free (&error);
397 void dbus_deinitialize( DBusConnection *context ) {
398 free(vtable); vtable = NULL;
399 dbus_connection_unref(context);
403 static DBusHandlerResult handler_wrapper (DBusConnection *connection,
404 DBusMessage *message,
408 osso_rpc_t *retval = malloc(sizeof(osso_rpc_t));
410 if ( dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ) {
411 retcode = dbus_req_handler(dbus_message_get_interface(message),
412 dbus_message_get_member(message),
415 if ( strcmp(dbus_message_get_member(message),"stop")==0 )
416 stopped_service = TRUE;
418 if ( retval->value.s != NULL ) {
419 DBusMessage *reply = dbus_message_new_method_return (message);
422 if (!dbus_message_append_args (reply, DBUS_TYPE_STRING, &retval->value.s, DBUS_TYPE_INVALID))
424 if (!dbus_connection_send (connection, reply, NULL))
426 dbus_message_unref (reply);
431 return DBUS_HANDLER_RESULT_HANDLED;
438 osso_context_t *osso_context;
443 /* Initialize maemo application */
445 osso_context = osso_initialize(OSSO_NAME, WIFISCAND_VERSION_STRING, TRUE, NULL);
447 dbus_error_init(&error);
448 osso_context = dbus_bus_get(DBUS_BUS_SESSION, &error);
451 /* Check that initialization was ok */
452 if (osso_context == NULL) {
454 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n", error.message);
455 dbus_error_free (&error);
462 appdata = g_new0(AppData, 1);
463 appdata->osso_context = osso_context;
465 memset(&(appdata->iface.info), 0, sizeof(wireless_info));
466 appdata->iface.ifname = "wlan0";
467 appdata->iface.sock = 0;
469 /* Add handler for hello D-BUS messages */
471 osso_return_t result = osso_rpc_set_cb_f(osso_context, OSSO_SERVICE, OSSO_OBJECT, OSSO_IFACE, dbus_req_handler, appdata);
473 int result = dbus_set_cb_f(osso_context, OSSO_SERVICE, OSSO_OBJECT, OSSO_IFACE, handler_wrapper, appdata);
476 if (result != OSSO_OK) {
478 osso_system_note_infoprint(appdata->osso_context, "Failure while setting OSSO callback", NULL);
483 /* INITIALIZATION FINISH */
486 event_loop = g_main_loop_new(NULL, FALSE);
487 g_main_loop_run(event_loop);
489 while (dbus_connection_read_write_dispatch(osso_context, -1) && stopped_service==FALSE) {}
492 /* Deinitialize OSSO */
494 osso_deinitialize(osso_context);
496 dbus_deinitialize(osso_context);