aboutsummaryrefslogtreecommitdiffstats
path: root/linux.c
diff options
context:
space:
mode:
authorGravatar Danilo Cesar Lemes de Paula <danilo.eu@gmail.com>2013-09-16 18:04:42 -0300
committerGravatar Danilo Cesar Lemes de Paula <danilo.eu@gmail.com>2013-09-16 22:40:47 -0300
commita8d33f80b0e8da4acb84028be0da8f33b81de0f9 (patch)
treed3813d817fd8407e79f58d68cffb393d8741db3d /linux.c
parentf5b33dede359dccc31fafa72e0e2550868d8edd2 (diff)
downloadsubsurface-a8d33f80b0e8da4acb84028be0da8f33b81de0f9.tar.gz
implement device probe in C
It's an attempt to build auto-completion for the dive-computers based on unpublished code inside libdivecomputer[1] [1] - http://git.libdivecomputer.org/?p=libdivecomputer.git;a=commitdiff;h=d44053a99435fb9fc1f408fb3f1629a54c938afc Signed-off-by: Danilo Cesar Lemes de Paula <danilo.eu@gmail.com>
Diffstat (limited to 'linux.c')
-rw-r--r--linux.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/linux.c b/linux.c
index 33a846f9b..f8d645abc 100644
--- a/linux.c
+++ b/linux.c
@@ -7,6 +7,9 @@
#endif
#include <gconf/gconf-client.h>
#include <string.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <fnmatch.h>
const char system_divelist_default_font[] = "Sans 8";
@@ -211,3 +214,46 @@ gboolean subsurface_launch_for_uri(const char* uri)
return TRUE;
}
#endif /* USE_GTK_UI */
+
+
+int enumerate_devices (device_callback_t callback, void *userdata)
+{
+ int index = -1;
+ DIR *dp = NULL;
+ struct dirent *ep = NULL;
+ size_t i;
+ const char *dirname = "/dev";
+ const char *patterns[] = {
+ "ttyUSB*",
+ "ttyS*",
+ "ttyACM*",
+ "rfcomm*",
+ NULL
+ };
+
+ dp = opendir (dirname);
+ if (dp == NULL) {
+ return -1;
+ }
+
+ while ((ep = readdir (dp)) != NULL) {
+ for (i = 0; patterns[i] != NULL; ++i) {
+ if (fnmatch (patterns[i], ep->d_name, 0) == 0) {
+ char filename[1024];
+ int n = snprintf (filename, sizeof (filename), "%s/%s", dirname, ep->d_name);
+ if (n >= sizeof (filename)) {
+ closedir (dp);
+ return -1;
+ }
+ callback (filename, userdata);
+ if (is_default_dive_computer_device(filename))
+ index = i;
+ break;
+ }
+ }
+ }
+ // TODO: list UEMIS mount point from /proc/mounts
+
+ closedir (dp);
+ return index;
+}