Using the wpa_ctrl socket interface from an external project
Holger Schurig
holgerschurig
Mon May 13 02:25:20 PDT 2013
You can simply put wpa_ctrl.c into your own project, it's license is BSD,
so it will work nicely with proprietary or GPLed code. I'm using that than
in a C++/Qt4.x project, were I open the controlling interface via
wpa_ctrl_open() and use that with a QSocketNotifier to react asynchronously.
I than made it compiled, with something like this (beware, this is a
cut-and-paste patch, so probably whitespace damaged):
--- ../mkimage/hostap/src/common/wpa_ctrl.c 2013-02-07 10:03:39.111172856
+0100
+++ wpa_ctrl.c 2012-03-23 14:10:17.000000000 +0100
@@ -6,18 +6,23 @@
* See README for more details.
*/
+#ifdef ORIG
#include "includes.h"
+#else
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+#endif
+
#ifdef CONFIG_CTRL_IFACE
#ifdef CONFIG_CTRL_IFACE_UNIX
#include <sys/un.h>
-#include <unistd.h>
-#include <fcntl.h>
#endif /* CONFIG_CTRL_IFACE_UNIX */
-#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
-#include <netdb.h>
-#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
#ifdef ANDROID
#include <dirent.h>
@@ -26,7 +31,41 @@
#endif /* ANDROID */
#include "wpa_ctrl.h"
+#ifdef ORIG
#include "common.h"
+#else
+#define os_malloc(size) malloc(size)
+#define os_memset(s, c, n) memset(s, c, n)
+#define os_free(ptr) free(ptr)
+#define os_snprintf snprintf
+#define os_getpid getpid
+#define os_memcmp(s1, s2, n) memcmp(s1, s2, n)
+
+size_t os_strlcpy(char *dest, const char *src, size_t siz)
+{
+ const char *s = src;
+ size_t left = siz;
+
+ if (left) {
+ /* Copy string up to the maximum size of the dest buffer */
+ while (--left != 0) {
+ if ((*dest++ = *s++) == '\0')
+ break;
+ }
+ }
+
+ if (left == 0) {
+ /* Not enough room for the string; force NUL-termination */
+ if (siz != 0)
+ *dest = '\0';
+ while (*s++)
+ ; /* determine total src string length */
+ }
+
+ return s - src - 1;
+}
+#endif
+
#if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP)
2013/5/10 John Beard <john.j.beard at gmail.com>
> Hi,
>
> I am hoping to use the wpa_ctrl socket interface to communicate with
> wpa_supplicant in order to get information about wifi signal strengths.
>
> I have tried to use the DBus interface, but I couldn't find much in the
> way of useful information about the library I was using (libdbus-c++)
> and when I asked further, I was told it is no longer usefully maintained
> and that I should look elsewhere. I'd rather keep things smaller than
> pulling in big deps, so I don't really want to use something like GDBus.
> The control socket should be fine for my purposes, but I'm having a bit
> of trouble using it.
>
> I can compile wpa_supplicant just fine, but I don't know how to compile
> into a library suitable for linking into my project. I looked in
> libutils.a, but that doesn't seem to have the wpa_ctrl_open() function
> in it.
>
> I can see how I can add my own code to the wpa_supplicant tree (like
> wpa_cli), but I'm more looking to add to another project, rather than
> extend wpa_supplicant, and I couldn't see any likely-looking targets in
> the Makefiles.
>
> The minimal code that I am trying to get to work is below (wpa_ctrl.h is
> copied into my working dir for now):
>
> ===============
>
> #include <stdio.h>
> #include "wpa_ctrl.h"
>
> #define CTRL_PATH "/var/run/wpa_supplicant.pid"
>
> int main(int argc, char** argv)
> {
> struct wpa_ctrl* wpac;
>
> wpac = wpa_ctrl_open(CTRL_PATH);
>
> if (!wpac){
> printf("Could not get ctrl interface!\n");
> return -1;
> }
>
> return 0;
> }
>
> ===============
>
> Could someone provide a quick pointer on how to correctly link against
> wpa_ctrl for control socket access to wpa_supplicant?
>
> Thanks and best regards,
>
> John Beard
> _______________________________________________
> HostAP mailing list
> HostAP at lists.shmoo.com
> http://lists.shmoo.com/mailman/listinfo/hostap
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.shmoo.com/pipermail/hostap/attachments/20130513/b664f850/attachment.htm>
More information about the Hostap
mailing list