#ifndef __LOG_H
#define __LOG_H
-#include <stdio.h>
-#include <syslog.h>
+#include <libubox/ulog.h>
-#define LOG(fmt, ...) do { \
- syslog(LOG_INFO, fmt, ## __VA_ARGS__); \
- fprintf(stderr, "ugps: "fmt, ## __VA_ARGS__); \
- } while (0)
+#define DEBUG(level, fmt, ...) do { \
+ if (debug >= level) { \
+ ulog(LOG_DEBUG, fmt, ## __VA_ARGS__); \
+ } } while (0)
-#define ERROR(fmt, ...) do { \
- syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
- fprintf(stderr, "ugps: "fmt, ## __VA_ARGS__); \
- } while (0)
+#define LOG ULOG_INFO
+#define ERROR ULOG_ERR
extern unsigned int debug;
#include "log.h"
#include "nmea.h"
+unsigned int debug;
static struct ustream_fd stream;
static struct ubus_auto_conn conn;
static struct blob_buf b;
+static char *ubus_socket;
struct timespec stamp = { 0 };
void
}
static int
-usage(void)
+usage(const char *prog)
{
- LOG("ugps <device>\n");
+ fprintf(stderr, "Usage: %s [options] <device>\n"
+ "Options:\n"
+ " -s <path> Path to ubus socket\n"
+ " -d <level> Enable debug messages\n"
+ " -S Print messages to stdout\n"
+ "\n", prog);
return -1;
}
int
main(int argc, char ** argv)
{
+ int ch;
+ char *device = NULL;
+ char *dbglvl = getenv("DBGLVL");
+ int ulog_channels = ULOG_KMSG;
signal(SIGPIPE, SIG_IGN);
- if (argc != 2)
- return usage();
+ if (dbglvl) {
+ debug = atoi(dbglvl);
+ unsetenv("DBGLVL");
+ }
+
+ while ((ch = getopt(argc, argv, "d:D:s:S")) != -1) {
+ switch (ch) {
+ case 's':
+ ubus_socket = optarg;
+ break;
+ case 'd':
+ debug = atoi(optarg);
+ break;
+ case 'S':
+ ulog_channels = ULOG_STDIO;
+ break;
+ default:
+ return usage(argv[0]);
+ }
+ }
+
+ if (argc - optind < 1) {
+ fprintf(stderr, "ERROR: missing device parameter\n");
+ return usage(argv[0]);
+ }
+
+ device = argv[optind];
+ ulog_open(ulog_channels, LOG_DAEMON, "ugps");
uloop_init();
+ conn.path = ubus_socket;
conn.cb = ubus_connect_handler;
ubus_auto_connect(&conn);
nmea_open(argv[1], &stream, B4800);
if (nmea_params[3].num < 0 || nmea_params[3].num > 2)
nmea_params[3].num = 0;
- LOG("%s: %s\n", ids[nmea_params[3].num], nmea_params[4].str);
+ DEBUG(3, "%s: %s\n", ids[nmea_params[3].num], nmea_params[4].str);
}
static void
if (*nmea_params[2].str != 'A') {
gps_valid = 0;
- fprintf(stderr, "waiting for valid signal\n");
+ DEBUG(4, "waiting for valid signal\n");
return;
}
struct timeval cur;
strftime(tmp, 256, "%D %02H:%02M:%02S", &tm);
- LOG("date: %s UTC\n", tmp);
+ DEBUG(3, "date: %s UTC\n", tmp);
tv.tv_sec -= timezone;
if (daylight)
int latd, latm, lats;
int lngd, lngm, lngs;
float flats, flngs;
- LOG("position: %s, %s\n",
+ DEBUG(4, "position: %s, %s\n",
nmea_params[3].str, nmea_params[5].str);
latm = atoi(&nmea_params[3].str[2]);
nmea_params[3].str[2] = '\0';
#define ms_to_deg(x, y) (((x * 10000) + y) / 60)
- LOG("position: %d°%d.%04d, %d°%d.%04d\n",
+ DEBUG(4, "position: %d°%d.%04d, %d°%d.%04d\n",
latd, latm, lats, lngd, lngm, lngs);
- LOG("position: %d°%d'%.1f\" %d°%d'%.1f\"\n",
+ DEBUG(4, "position: %d°%d'%.1f\" %d°%d'%.1f\"\n",
latd, latm, flats, lngd, lngm, flngs);
snprintf(latitude, sizeof(latitude), "%d.%04d", latd, ms_to_deg(latm, lats));
snprintf(longitude, sizeof(longitude), "%d.%04d", lngd, ms_to_deg(lngm, lngs));
- LOG("position: %s %s\n", latitude, longitude);
+ DEBUG(3, "position: %s %s\n", latitude, longitude);
gps_timestamp();
}
}
if (!gps_valid)
return;
strncpy(elivation, nmea_params[9].str, sizeof(elivation));
- LOG("height: %s\n", elivation);
+ DEBUG(4, "height: %s\n", elivation);
}
static void
return;
strncpy(course, nmea_params[1].str, sizeof(course));
strncpy(speed, nmea_params[6].str, sizeof(speed));
- LOG("course: %s\n", course);
- LOG("speed: %s\n", speed);
+ DEBUG(4, "course: %s\n", course);
+ DEBUG(4, "speed: %s\n", speed);
}
static struct nmea_msg {