From: Aurelien Jacobs Date: Sat, 6 Dec 2014 23:56:22 +0000 (+0100) Subject: Add a public API to list available serial ports. X-Git-Tag: libsigrok-0.4.0~696 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=24287ea9e3dd0c6f7fc2299eaf725346b8c1fea2;hp=3fc66eda9f62ed5520e237ad6bad036d27bf96c2 Add a public API to list available serial ports. --- diff --git a/Makefile.am b/Makefile.am index cab73cb1..13debd1e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index efb4995d..78f484fa 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -318,6 +318,20 @@ shared_ptr Context::open_stream(string header) new Input(shared_from_this(), input), Input::Deleter()); } +map Context::serials(shared_ptr driver) +{ + GSList *serial_list = sr_serial_list(driver ? driver->_structure : NULL); + map 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), diff --git a/bindings/cxx/include/libsigrok/libsigrok.hpp b/bindings/cxx/include/libsigrok/libsigrok.hpp index e338f782..d287504d 100644 --- a/bindings/cxx/include/libsigrok/libsigrok.hpp +++ b/bindings/cxx/include/libsigrok/libsigrok.hpp @@ -292,6 +292,7 @@ public: /** Open an input stream based on header data. * @param header Initial data from stream. */ shared_ptr open_stream(string header); + map serials(shared_ptr driver); protected: map _drivers; map _input_formats; diff --git a/bindings/java/org/sigrok/core/classes/classes.i b/bindings/java/org/sigrok/core/classes/classes.i index 413fb93a..867e8fca 100644 --- a/bindings/java/org/sigrok/core/classes/classes.i +++ b/bindings/java/org/sigrok/core/classes/classes.i @@ -137,6 +137,11 @@ VECTOR(std::shared_ptr, HardwareDevice) MAP_COMMON(std::string, std::string, String, String) +%typemap(jni) std::map + "jobject" +%typemap(jtype) std::map + "java.util.Map" + %typemap(out) std::map { jclass HashMap = jenv->FindClass("java/util/HashMap"); jmethodID init = jenv->GetMethodID(HashMap, "", "()V"); diff --git a/include/libsigrok/libsigrok.h b/include/libsigrok/libsigrok.h index f05fb47b..df1333a9 100644 --- a/include/libsigrok/libsigrok.h +++ b/include/libsigrok/libsigrok.h @@ -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" diff --git a/include/libsigrok/proto.h b/include/libsigrok/proto.h index 8179c815..3d3e354a 100644 --- a/include/libsigrok/proto.h +++ b/include/libsigrok/proto.h @@ -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 index 00000000..897694d7 --- /dev/null +++ b/src/fallback.c @@ -0,0 +1,35 @@ +/* + * This file is part of the libsigrok project. + * + * Copyright (C) 2014 Aurelien Jacobs + * + * 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 . + */ + +#include +#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 diff --git a/src/serial.c b/src/serial.c index 192b18a7..883bf27a 100644 --- a/src/serial.c +++ b/src/serial.c @@ -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. *