Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

usense: Update user interface to new API, and update README

Signed-off-by: Jason S. McMullan <jason.mcmullan@gmail.com>
  • Loading branch information...
commit 2222b234c511c1a49f197f9072987f32ae2a6f5c 1 parent fe736e4
@ezrec authored
Showing with 73 additions and 62 deletions.
  1. +24 −21 README
  2. +49 −41 src/main.c
View
45 README
@@ -27,32 +27,35 @@ Using
-----
$ usense
- Usage:
-
- $ usense
- usb:3.1
- usb:3.2
- usb:4.1
- $ usense usb:3.2
- device=usb:3.2
+ usb:003.1
+ usb:003.2
+ usb:004.1
+
+ $ usense usb:003.2
+ device=usb:003.2
+ gotemp.cal.add=-0.05
+ gotemp.cal.mul=1.002
reading=27.4
- vendor=Vernier
- device=Go!Temp
- calibration.mult=1.002
- calibration.add=-0.05
+ type=temp
+ units=C
+ usb.product=0002
+ usb.vendor=08f7
+
$ usense 3.2 reading
27.4
Configuration
-------------
- $ usense usb:3.2
- device=usb:3.2
+ $ usense usb:003.2
+ device=usb:003.2
+ gotemp.cal.add=-0.05
+ gotemp.cal.mul=1.002
reading=27.4
- vendor=Vernier
- device=Go!Temp
- calibration.mult=1.002
- calibration.add=-0.05
- $ usense usb:3.2 calibration.mult 1.003
- $ usense usb:3.2 calibration.mult
- calibration.mult=1.003
+ type=temp
+ units=C
+ usb.product=0002
+ usb.vendor=08f7
+
+ $ usense usb:003.2 gotemp.cal.mul=1.003 units=F reading
+ 106.97
View
90 src/main.c
@@ -1,6 +1,6 @@
/*
- * Copyright 2009, Netronome Systems
- * Author: Jason McMullan <jason.mcmullan@netronome.com>
+ * Copyright 2009, Jason S. McMullan
+ * Author: Jason S. McMullan <jason.mcmullan@gmail.com>
*
* 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
@@ -19,41 +19,38 @@
*/
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "usense.h"
-int list_devices(void)
+static const char *program;
+
+static int list_devices(void)
{
struct usense *usense;
struct usense_device *dev;
usense = usense_start(0, NULL);
if (usense == NULL) {
- return EXIT_SUCCESS;
+ fprintf(stderr, "%s: Can't create a new usense monitor\n", program);
+ return EXIT_FAILURE;
}
for (dev = usense_first(usense); dev != NULL; dev = usense_next(usense, dev)) {
printf("%s\n", usense_device_name(dev));
}
- return 0;
-
usense_stop(usense);
+
+ return 0;
}
-int list_device_props(const char *dev_name)
+static int list_device_props(struct usense_device *dev)
{
- struct usense_device *dev;
char value[PATH_MAX];
const char *key;
int err;
- dev = usense_open(dev_name);
- if (dev == NULL) {
- fprintf(stderr, "%s: No such sensor\n", dev_name);
- return EXIT_FAILURE;
- }
-
for (key = usense_prop_first(dev); key != NULL; key = usense_prop_next(dev, key)) {
err = usense_prop_get(dev, key, value, sizeof(value));
if (err < 0) {
@@ -65,21 +62,14 @@ int list_device_props(const char *dev_name)
return EXIT_SUCCESS;
}
-int show_device_prop(const char *dev_name, const char *prop_name)
+int show_device_prop(struct usense_device *dev, const char *prop_name)
{
- struct usense_device *dev;
char value[PATH_MAX];
int err;
- dev = usense_open(dev_name);
- if (dev == NULL) {
- fprintf(stderr, "%s: No such sensor\n", dev_name);
- return EXIT_FAILURE;
- }
-
err = usense_prop_get(dev, prop_name, value, sizeof(value));
if (err < 0) {
- fprintf(stderr, "%s: No such property \"%s\"\n", dev_name, prop_name);
+ fprintf(stderr, "%s: No such property \"%s\"\n", usense_device_name(dev), prop_name);
return EXIT_FAILURE;
}
printf("%s\n", value);
@@ -87,20 +77,13 @@ int show_device_prop(const char *dev_name, const char *prop_name)
return EXIT_SUCCESS;
}
-int set_device_prop(const char *dev_name, const char *prop_name, const char *value)
+int set_device_prop(struct usense_device *dev, const char *prop_name, const char *value)
{
- struct usense_device *dev;
int err;
- dev = usense_open(dev_name);
- if (dev == NULL) {
- fprintf(stderr, "%s: No such sensor\n", dev_name);
- return EXIT_FAILURE;
- }
-
err = usense_prop_set(dev, prop_name, value);
if (err < 0) {
- fprintf(stderr, "%s: Can't set property \"%s\"\n", dev_name, prop_name);
+ fprintf(stderr, "%s: Can't set property \"%s\" to \"%s\"\n", usense_device_name(dev), prop_name, value);
return EXIT_FAILURE;
}
@@ -109,25 +92,50 @@ int set_device_prop(const char *dev_name, const char *prop_name, const char *val
int main(int argc, char **argv)
{
+ struct usense_device *dev;
+ int i, err = 0;
+
+ program = argv[0];
+
if (argc == 1) {
/* List all */
return list_devices();
}
+ dev = usense_open(argv[1]);
+ if (dev == NULL) {
+ fprintf(stderr, "%s: No such sensor '%s'\n", program, argv[1]);
+ return EXIT_FAILURE;
+ }
+
if (argc == 2) {
/* List props of a device */
- return list_device_props(argv[1]);
+ return list_device_props(dev);
}
- if (argc == 3) {
- /* List single prop of a device */
- return show_device_prop(argv[1], argv[2]);
+ argc -= 1;
+ argv += 1;
+ for (i = 1; i < argc; i++) {
+ char *cp;
+
+ cp = strchr(argv[i],'=');
+ if (cp == NULL) {
+ /* List single prop of a device */
+ err = show_device_prop(dev, argv[i]);
+ if (err < 0) {
+ break;
+ }
+ } else {
+ *(cp++) = 0;
+ /* Set prop of a device */
+ err = set_device_prop(dev, argv[i], cp);
+ if (err < 0) {
+ break;
+ }
+ }
}
- if (argc == 4) {
- /* Set prop of a device */
- return set_device_prop(argv[1], argv[2], argv[3]);
- }
+ usense_close(dev);
- return EXIT_FAILURE;
+ return (err < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
}
Please sign in to comment.
Something went wrong with that request. Please try again.