14 #define osso_context_t DBusConnection
15 typedef struct osso_rpc_t_values {
19 typedef struct osso_rpc_t {
21 osso_rpc_t_values value;
25 #define WIFISCAND_VERSION_STRING "1.1"
27 #define OSSO_NAME "wifiscan"
28 #define OSSO_SERVICE "org.javiplx."OSSO_NAME
29 #define OSSO_OBJECT "/org/javiplx/"OSSO_NAME
30 #define OSSO_IFACE "org.javiplx."OSSO_NAME
40 /* --------------------------------------------------------------------------- WirelessInterface */
47 /* ------------------------------------------------------------------ WirelessInterface_ScanItem */
49 static int WirelessInterface_ScanItem(struct iw_event* event, ScanInfo **scaninfo) {
55 *scaninfo = (ScanInfo*) malloc( sizeof(ScanInfo) );
56 memset(*scaninfo, 0, sizeof(ScanInfo) );
58 iw_ether_ntop( (const struct ether_addr*)(event->u.ap_addr.sa_data), (*scaninfo)->mac);
63 (*scaninfo)->rssi = event->u.qual.level - 0x100;
73 /* ---------------------------------------------------------------------- */
75 void* makeScan(WirelessInterface* coso, gchar *message) {
77 unsigned char* buffer = NULL;
78 int buflen = IW_SCAN_MAX_DATA;
82 int timeout = 10000000;
84 ScanInfo **scan = NULL;
86 has_range = (iw_get_range_info(coso->sock, coso->ifname, &range) >= 0);
90 wrq.u.data.pointer = NULL;
92 wrq.u.data.length = 0;
94 if(iw_set_ext(coso->sock, coso->ifname, SIOCSIWSCAN, &wrq) < 0) {
96 message = "Interface doesn't support scanning";
101 timeout -= tv.tv_usec;
111 ret = select(last_fd + 1, &rfds, NULL, NULL, &tv);
114 if(errno == EAGAIN || errno == EINTR) continue;
117 message = "Unknown scanning error";
123 unsigned char* newbuf;
126 newbuf = (unsigned char*) realloc(buffer, buflen);
129 if(buffer) free(buffer);
130 message = "Memory allocation failure in scan";
136 wrq.u.data.pointer = buffer;
137 wrq.u.data.flags = 0;
138 wrq.u.data.length = buflen;
140 if(iw_get_ext(coso->sock, coso->ifname, SIOCGIWSCAN, &wrq) < 0) {
141 if((errno == E2BIG)) {
142 if(wrq.u.data.length > buflen) buflen = wrq.u.data.length;
148 if(errno == EAGAIN) {
151 timeout -= tv.tv_usec;
153 if(timeout > 0) continue;
158 message = "Unable to read scan data";
168 if(wrq.u.data.length) {
172 void* scan_dict = NULL;
174 iw_init_event_stream(&stream, (char*)(buffer), wrq.u.data.length);
176 scan = (ScanInfo**) malloc( 25 * sizeof(ScanInfo *) );
177 memset(scan, 0, 25 * sizeof(ScanInfo *) );
180 for (i=0; i<25; i++) *(scan+i) = NULL;
185 ret = iw_extract_event_stream(&stream, &iwe, range.we_version_compiled);
188 int sr = WirelessInterface_ScanItem(&iwe, &sc);
199 message = "Unknown error";
208 static void refresh(WirelessInterface* coso) {
211 iw_get_basic_config(coso->sock, coso->ifname, &(coso->info.b));
212 iw_get_range_info(coso->sock, coso->ifname, &(coso->info.range));
214 iw_get_ext(coso->sock, coso->ifname, SIOCGIWRATE, &wrq);
215 memcpy(&(coso->info.bitrate), &wrq.u.bitrate, sizeof(iwparam));
217 iw_get_ext(coso->sock, coso->ifname, SIOCGIWAP, &wrq);
218 memcpy(&(coso->info.ap_addr), &wrq.u.ap_addr, sizeof (sockaddr));
221 coso->sock, coso->ifname, &(coso->info.stats),
222 &(coso->info.range), coso->info.has_range
226 /* Application UI data struct */
227 typedef struct _AppData AppData;
229 WirelessInterface iface;
230 osso_context_t *osso_context;
234 static GMainLoop *event_loop = NULL;
236 static short int start_flags = 0;
239 int submit_reply_message(DBusConnection *connection, DBusMessage *message, char *string) {
240 DBusMessage *reply = dbus_message_new_method_return (message);
243 if (!dbus_message_append_args (reply, DBUS_TYPE_STRING, &string, DBUS_TYPE_INVALID))
245 if (!dbus_connection_send (connection, reply, NULL))
247 dbus_message_unref (reply);
248 return DBUS_HANDLER_RESULT_HANDLED;
251 static DBusObjectPathVTable *vtable = NULL;
253 int dbus_set_cb_f( DBusConnection *context,
254 const char *service, const char *object,
255 const char *interface, void *handler,
258 /* First, we prepare the complex dbus handler structures */
259 DBusObjectPathVTable *vtable = malloc( sizeof(DBusObjectPathVTable) );
260 memset(vtable , '\0', sizeof(DBusObjectPathVTable) );
261 vtable->message_function = (DBusObjectPathMessageFunction) handler;
263 if (!dbus_connection_register_fallback(context, object, vtable, user_data)) {
268 dbus_error_init(&error);
270 dbus_bus_request_name (context, service, 0, &error);
271 if ( dbus_error_is_set(&error) ) {
272 dbus_error_free (&error);
276 dbus_error_free (&error);
280 void dbus_deinitialize( DBusConnection *context ) {
281 free(vtable); vtable = NULL;
282 dbus_connection_unref(context);
287 /* Callback for normal D-BUS messages */
289 gint dbus_req_handler(const gchar * interface, const gchar * method,
290 GArray * arguments, gpointer data,
293 static DBusHandlerResult dbus_req_handler (DBusConnection *connection,
294 DBusMessage *message,
299 osso_rpc_t *retval = malloc(sizeof(osso_rpc_t));
303 appdata = (AppData *) data;
305 retval->type = DBUS_TYPE_STRING;
306 retval->value.s = (gchar*) malloc( sizeof(gchar *) );
307 retval->value.s[0] = '\0';
310 if ( strcmp(method,"wakeup")==0 ) {
312 if (dbus_message_is_method_call(message, OSSO_IFACE, "wakeup")) {
314 retval->value.s = (gchar *) realloc(retval->value.s,16*sizeof(gchar *));
315 snprintf(retval->value.s,16,"WifiScand ready");
319 return submit_reply_message(connection, message, retval->value.s);
324 if ( strcmp(method,"start")==0 ) {
326 if (dbus_message_is_method_call(message, OSSO_IFACE, "start")) {
329 if( (appdata->iface.sock=iw_sockets_open()) < 0) {
330 retval->value.s = (gchar *) realloc(retval->value.s,33*sizeof(gchar *));
331 snprintf(retval->value.s,33,"Failure in socket initialization");
335 return submit_reply_message(connection, message, retval->value.s);
340 strncpy(frq.ifr_name, appdata->iface.ifname, IFNAMSIZ);
341 if(ioctl(appdata->iface.sock, SIOCGIFFLAGS, &frq)) {
342 retval->value.s = (gchar *) realloc(retval->value.s,28*sizeof(gchar *));
343 snprintf(retval->value.s,28,"Cannot get interface status");
347 return submit_reply_message(connection, message, retval->value.s);
351 start_flags = frq.ifr_flags;
352 frq.ifr_flags |= IFF_UP | IFF_RUNNING;
354 if(ioctl(appdata->iface.sock, SIOCSIFFLAGS, &frq)) {
355 retval->value.s = (gchar *) realloc(retval->value.s,27*sizeof(gchar *));
356 snprintf(retval->value.s,27,"Cannot set interface state");
360 return submit_reply_message(connection, message, retval->value.s);
364 refresh(&appdata->iface);
365 retval->value.s = (gchar *) realloc(retval->value.s,22*sizeof(gchar *));
366 snprintf(retval->value.s,22,"Interface initialized");
370 return submit_reply_message(connection, message, retval->value.s);
375 if ( strcmp(method,"stop")==0 ) {
377 if (dbus_message_is_method_call(message, OSSO_IFACE, "stop")) {
380 strncpy(frq.ifr_name, appdata->iface.ifname, IFNAMSIZ);
381 if(!ioctl(appdata->iface.sock, SIOCGIFFLAGS, &frq)) {
382 frq.ifr_flags = start_flags;
383 if(!ioctl(appdata->iface.sock, SIOCSIFFLAGS, &frq))
384 refresh(&appdata->iface);
386 iw_sockets_close(appdata->iface.sock);
387 appdata->iface.sock = 0;
389 osso_deinitialize(appdata->osso_context);
391 dbus_deinitialize(appdata->osso_context);
393 /* Instead of exiting, signaling finish to main loop could be better
394 retval->value.s = (gchar *) realloc(retval->value.s,34*sizeof(gchar *));
395 snprintf(retval->value.s,34,"Interface moved to original state");
401 if ( strcmp(method,"scan")==0 ) {
403 if (dbus_message_is_method_call(message, OSSO_IFACE, "scan")) {
406 ScanInfo **scan = (ScanInfo **) makeScan(&appdata->iface,retval->value.s);
408 retval->value.s = (gchar *) realloc(retval->value.s,64*sizeof(gchar *));
409 snprintf(retval->value.s,64,"ERROR");
413 return submit_reply_message(connection, message, retval->value.s);
418 for (i=0; i<25&&*(scan+i)!=NULL; i++) {
419 ScanInfo* sc = *(scan+i);
420 retval->value.s = (gchar *) realloc(retval->value.s,23*(i+1)*sizeof(gchar *));
421 if ( retval->value.s == NULL ) {
422 retval->value.s = "Error allocating memory";
426 return submit_reply_message(connection, message, retval->value.s);
429 sprintf(retval->value.s+strlen(retval->value.s),"%s:%d ",sc->mac,sc->rssi);
432 retval->value.s[strlen(retval->value.s)-1] = '\0';
436 return submit_reply_message(connection, message, retval->value.s);
440 retval->value.s = (gchar *) realloc(retval->value.s,64*sizeof(gchar *));
441 snprintf(retval->value.s,64,"Unknown method");
445 return submit_reply_message(connection, message, retval->value.s);
451 osso_context_t *osso_context;
456 /* Initialize maemo application */
458 osso_context = osso_initialize(OSSO_NAME, WIFISCAND_VERSION_STRING, TRUE, NULL);
460 dbus_error_init(&error);
461 osso_context = dbus_bus_get(DBUS_BUS_STARTER, &error);
464 /* Check that initialization was ok */
465 if (osso_context == NULL) {
467 fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n", error.message);
468 dbus_error_free (&error);
475 appdata = g_new0(AppData, 1);
476 appdata->osso_context = osso_context;
478 memset(&(appdata->iface.info), 0, sizeof(wireless_info));
479 appdata->iface.ifname = "wlan0";
480 appdata->iface.sock = 0;
482 /* Add handler for hello D-BUS messages */
484 osso_return_t result = osso_rpc_set_cb_f(osso_context, OSSO_SERVICE, OSSO_OBJECT, OSSO_IFACE, dbus_req_handler, appdata);
486 int result = dbus_set_cb_f(osso_context, OSSO_SERVICE, OSSO_OBJECT, OSSO_IFACE, dbus_req_handler, appdata);
489 if (result != OSSO_OK) {
491 osso_system_note_infoprint(appdata->osso_context, "Failure while setting OSSO callback", NULL);
496 /* INITIALIZATION FINISH */
499 event_loop = g_main_loop_new(NULL, FALSE);
500 g_main_loop_run(event_loop);
502 while (dbus_connection_read_write_dispatch (osso_context, -1)) {}
505 /* Deinitialize OSSO */
507 osso_deinitialize(osso_context);
509 dbus_deinitialize(osso_context);