Show motelog.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 <stdio.h>
#include <stdlib.h>
#include "emrun/emrun.h"
#include "link/link.h"
#include "libmisc/misc.h"
#include "../tos-contrib/VehicleTest/apps/VehicleTypes.h"
#include <link/link_headers.h>
struct timeval start_time;
FILE *syncf;
int handle_input(void *data, int fd, int cond, g_event_t *event);
int request_speed(void * data, int interval, g_event_t *event);
int vehicle_config(int fd);
int dump_packet(lu_context_t *link, link_pkt_t *hdr, ssize_t data_len)
{
struct vehicle_sync *vs = (struct vehicle_sync *)(hdr->data);
struct vehicle_event *ve = (struct vehicle_event *)(hdr->data);
/* adjust times from start of test */
struct timeval ts = hdr->rcv_time;
misc_tv_sub(&ts, &start_time);
/* $$ if rate known could convert these timestamps to samples.. */
/* $$ maybe want absolute timestamps */
if (hdr->type == PKT_TYPE_TOS) {
switch (hdr->ext_type) {
case VEHICLE_SYNC:
fprintf(syncf, "%ld.%06ld %d 0 0 sync\n",
ts.tv_sec, ts.tv_usec, vs->mark);
break;
case VEHICLE_EVENT:
fprintf(syncf, "%ld.%06ld 0 %d 0 event\n",
ts.tv_sec, ts.tv_usec, ve->id);
break;
default:
break;
}
}
fflush(syncf);
free(hdr);
return EVENT_RENEW;
}
void usage(char *name)
{
misc_print_usage
(name,
"-U <link> ",
" --uses <link>: specify the link to use\n"
" --outfile: specify the log file name\n"
" --serial: specify serial port for obd tool\n"
"\n"
);
exit(1);
}
void dump_shutdown(void *data)
{
fclose(syncf);
elog(LOG_NOTICE, "linkdump shutting down");
exit(0);
}
int main(int argc, char **argv)
{
lu_opts_t opts = {
receive: dump_packet,
};
/* generic initialization common to all programs */
misc_init(&argc, argv, CVSTAG);
/* parse options */
opts.opts.name = link_parse_uses(&argc, argv, NULL);
char *sync_fn;
sync_fn = misc_parse_out_option(&argc, argv, "outfile", 0);
char *serial;
serial = misc_parse_out_option(&argc, argv, "serial", 0);
int reset = misc_parse_out_switch(&argc, argv, "reset", 0);
/* verify name present */
if (opts.opts.name == NULL) {
elog(LOG_CRIT, "Please specify --uses!");
usage(argv[0]);
}
/* add'l args? */
if (misc_args_remain(&argc, argv)) {
elog(LOG_CRIT, "Additional unparsed arguments!");
usage(argv[0]);
}
syncf = fopen(sync_fn, "w");
if (syncf == NULL) {
elog(LOG_CRIT, "unable to open output file %s: %m", sync_fn);
exit(1);
}
/* init link event */
lu_context_t *lu = NULL;
if (lu_open(&opts, &lu) < 0) {
elog(LOG_CRIT, "Unable to open link %s: %m", link_name(&(opts.opts), NULL));
usage(argv[0]);
}
if (reset) {
link_pkt_t pkt = {
dst: {
id: LINK_BROADCAST
},
type: PKT_TYPE_TOS,
ext_type: VEHICLE_RESET
};
int i=0;
for (i=0; i<5; i++) {
if (lu_send(lu, &pkt, 1) < 0) {
fprintf(stderr, "***Unable to send reset packet! %m \n");
}
}
exit(0);
}
gettimeofday(&start_time, NULL);
fprintf(syncf, "#h time mark id type\n");
if (serial) {
int fd = open(serial, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0) {
elog(LOG_CRIT, "Unable to open serial port %s: %m", serial);
exit(1);
}
vehicle_config(fd);
if (g_event_add(fd, FUSD_NOTIFY_INPUT, handle_input,
NULL, NULL, NULL) < 0) {
elog(LOG_CRIT, "Can't create event for new socket: %m");
exit(1);
}
g_timer_add(500, request_speed, NULL, NULL, NULL);
}
/*
* Start the event loop running - it should never exit (the shutdown
* handler is called when the program is supposed to stop)
*/
emrun_opts_t emrun_opts = {
shutdown: dump_shutdown,
silent: 1
};
emrun_init(&emrun_opts); /* this should be the last initialization */
g_main();
elog_g(LOG_CRIT, "event loop terminated abnormally!");
return 0;
}
See more files for this project here