Add a public API to list available serial ports.

This commit is contained in:
Aurelien Jacobs 2014-12-07 00:56:22 +01:00 committed by Uwe Hermann
parent 3fc66eda9f
commit 24287ea9e3
8 changed files with 120 additions and 0 deletions

View 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 \

View 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),

View 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;

View 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");

View 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"

View 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);

35
src/fallback.c Normal file
View File

@ -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

View 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.
*