]> sigrok.org Git - libsigrok.git/commitdiff
Add a public API to list available serial ports.
authorAurelien Jacobs <redacted>
Sat, 6 Dec 2014 23:56:22 +0000 (00:56 +0100)
committerUwe Hermann <redacted>
Sat, 3 Jan 2015 16:51:51 +0000 (17:51 +0100)
Makefile.am
bindings/cxx/classes.cpp
bindings/cxx/include/libsigrok/libsigrok.hpp
bindings/java/org/sigrok/core/classes/classes.i
include/libsigrok/libsigrok.h
include/libsigrok/proto.h
src/fallback.c [new file with mode: 0644]
src/serial.c

index cab73cb160603e1217cb680e3b8d74a1b12ed234..13debd1e7c9076b8b57117ea714aee0dbc9e130b 100644 (file)
@@ -36,6 +36,7 @@ libsigrok_la_SOURCES = \
        src/trigger.c \
        src/soft-trigger.c \
        src/analog.c \
+       src/fallback.c \
        src/strutil.c \
        src/log.c \
        src/version.c \
index efb4995db6152f705053f041830daae6c04fbdd6..78f484fa9dafffb8a188fd235c7cbf19d7f7e104 100644 (file)
@@ -318,6 +318,20 @@ shared_ptr<Input> Context::open_stream(string header)
                new Input(shared_from_this(), input), Input::Deleter());
 }
 
+map<string, string> Context::serials(shared_ptr<Driver> driver)
+{
+       GSList *serial_list = sr_serial_list(driver ? driver->_structure : NULL);
+       map<string, string> serials;
+
+       for (GSList *serial = serial_list; serial; serial = serial->next) {
+               struct sr_serial_port *port = (sr_serial_port *) serial->data;
+               serials[string(port->name)] = string(port->description);
+       }
+
+       g_slist_free_full(serial_list, (GDestroyNotify)sr_serial_free);
+       return serials;
+}
+
 Driver::Driver(struct sr_dev_driver *structure) :
        ParentOwned(structure),
        Configurable(structure, NULL, NULL),
index e338f782a6b5fa65f5f5e31c2f4a936eadc66662..d287504d6d3a16fc300627faecac99d785e7d1da 100644 (file)
@@ -292,6 +292,7 @@ public:
        /** Open an input stream based on header data.
         * @param header Initial data from stream. */
        shared_ptr<Input> open_stream(string header);
+       map<string, string> serials(shared_ptr<Driver> driver);
 protected:
        map<string, Driver *> _drivers;
        map<string, InputFormat *> _input_formats;
index 413fb93ab44c6813672df832f51250dbc48ab371..867e8fcaaae53d20a9db466f1eaa89990fcdacf9 100644 (file)
@@ -137,6 +137,11 @@ VECTOR(std::shared_ptr<sigrok::HardwareDevice>, HardwareDevice)
 
 MAP_COMMON(std::string, std::string, String, String)
 
+%typemap(jni) std::map<std::string, std::string>
+  "jobject"
+%typemap(jtype) std::map<std::string, std::string>
+  "java.util.Map<String,String>"
+
 %typemap(out) std::map<std::string, std::string> {
   jclass HashMap = jenv->FindClass("java/util/HashMap");
   jmethodID init = jenv->GetMethodID(HashMap, "<init>", "()V");
index f05fb47bcc8349f204268d60f5746b3bfc9ce8bc..df1333a9e79d938acb9e4ca5f54415932b98083e 100644 (file)
@@ -1043,6 +1043,14 @@ struct sr_dev_driver {
  */
 struct sr_session;
 
+/** Serial port descriptor. */
+struct sr_serial_port {
+       /** The OS dependent name of the serial port. */
+       char *name;
+       /** An end user friendly description for the serial port. */
+       char *description;
+};
+
 #include "proto.h"
 #include "version.h"
 
index 8179c8153df450ca9bd414f0d18b4fe7091dcec6..3d3e354a1baf93a74d5e5041b35a1c878f04a16e 100644 (file)
@@ -185,6 +185,11 @@ SR_API struct sr_trigger_stage *sr_trigger_stage_add(struct sr_trigger *trig);
 SR_API int sr_trigger_match_add(struct sr_trigger_stage *stage,
                struct sr_channel *ch, int trigger_match, float value);
 
+/*--- serial.c --------------------------------------------------------------*/
+
+SR_API GSList *sr_serial_list(const struct sr_dev_driver *driver);
+SR_API void sr_serial_free(struct sr_serial_port *serial);
+
 /*--- strutil.c -------------------------------------------------------------*/
 
 SR_API char *sr_si_string_u64(uint64_t x, const char *unit);
diff --git a/src/fallback.c b/src/fallback.c
new file mode 100644 (file)
index 0000000..897694d
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * This file is part of the libsigrok project.
+ *
+ * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glib.h>
+#include "config.h"
+#include "libsigrok.h"
+
+#ifndef HAVE_LIBSERIALPORT
+
+SR_API GSList *sr_serial_list(const struct sr_dev_driver *driver)
+{
+    return NULL;
+}
+
+SR_API void sr_serial_free(struct sr_serial_port *serial)
+{
+}
+
+#endif
index 192b18a7245a4caffa17eb3a23ab3801bd16c33e..883bf27a020b26e6204aad3a27308142500a4921 100644 (file)
@@ -808,6 +808,57 @@ SR_PRIV int serial_source_remove(struct sr_session *session,
        return SR_OK;
 }
 
+static struct sr_serial_port *sr_serial_new(const char *name,
+               const char *description)
+{
+       struct sr_serial_port *serial;
+
+       if (!name)
+               return NULL;
+
+       serial = g_malloc(sizeof(*serial));
+       serial->name = g_strdup(name);
+       serial->description = g_strdup(description ? description : "");
+       return serial;
+}
+
+SR_API void sr_serial_free(struct sr_serial_port *serial)
+{
+       if (serial == NULL)
+               return;
+       g_free(serial->name);
+       g_free(serial->description);
+       g_free(serial);
+}
+
+/**
+ * List available serial devices.
+ *
+ * @return A GSList of strings containing the path of the serial devices or
+ *         NULL if no serial device is found. The returned list must be freed
+ *         by the caller.
+ */
+SR_API GSList *sr_serial_list(const struct sr_dev_driver *driver)
+{
+       GSList *tty_devs = NULL;
+       struct sp_port **ports;
+       int i;
+
+       (void)driver;
+
+       if (sp_list_ports(&ports) != SP_OK)
+               return NULL;
+
+       for (i=0; ports[i]; i++) {
+               struct sr_serial_port *port = sr_serial_new(sp_get_port_name(ports[i]),
+                                                    sp_get_port_description(ports[i]));
+               tty_devs = g_slist_append(tty_devs, port);
+       }
+
+       sp_free_port_list(ports);
+       return tty_devs;
+}
+
 /**
  * Find USB serial devices via the USB vendor ID and product ID.
  *