Show serial_dump.c syntax highlighted
/*
*
* Copyright (c) 2006 The Regents of the University of California. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <libmisc/misc.h>
#include <libdev/glib_dev.h>
#include <emrun/emrun.h>
#include <termios.h>
#include <sys/uio.h>
static
int serial_ready(void *data, int fd, int cond, g_event_t *event)
{
if (cond == FUSD_NOTIFY_INPUT) {
char buf[4096];
int status = read(fd, buf, sizeof(buf));
if (status > 0) {
elog_raw(LOG_NOTICE, buf, status);
}
if (status < 0) {
elog(LOG_WARNING, "Read error. %m");
exit(1);
}
}
return EVENT_RENEW;
}
void usage(char *name)
{
misc_print_usage
(name,
"--port <serial port> --baud <baud>",
""
);
exit(1);
}
/* Callback when we are asked (by emrun) to shut down */
static void sl_shutdown(void *data)
{
elog(LOG_NOTICE, "Shutting down Setled example program");
exit(0);
}
int main(int argc, char *argv[])
{
/* generic init */
misc_init(&argc, argv, CVSTAG);
int fd = -1;
uint baud = 0;
char *port = misc_parse_out_option(&argc, argv, "port", 0);
misc_parse_option_as_uint(&argc, argv, "baud", 'b', &baud);
if (port == NULL) {
elog(LOG_CRIT, "please specify port.");
exit(1);
}
if (misc_args_remain(&argc, argv)) {
usage(argv[0]);
}
fd = open(port, O_RDWR);
if (fd < 0) {
elog(LOG_CRIT, "Failed to open port '%s': %m",
port);
exit(1);
}
/* configure the serial port: raw, set baud, no rtscts */
if (baud) misc_config_serial(fd, baud, 0);
/* flush output */
if (tcflush(fd, TCIFLUSH) < 0)
elog(LOG_WARNING, "can't tcflush serial port %s: %m", port);
if (g_event_add(fd, FUSD_NOTIFY_INPUT | FUSD_NOTIFY_EXCEPT,
serial_ready, NULL, NULL, NULL) < 0) {
elog(LOG_CRIT, "can't create event for %s: %m", port);
exit(1);
}
/* register with emrun */
emrun_opts_t emrun_opts = {
shutdown: sl_shutdown,
data: NULL
};
emrun_init(&emrun_opts); /* this should be the last initialization */
g_main();
elog(LOG_CRIT, "event loop exited unexpectedly!");
return 1;
}
See more files for this project here