munin plugin
[trinked.git] / trinket.c
index 6ff5e4d..8f2cdc9 100644 (file)
--- a/trinket.c
+++ b/trinket.c
@@ -7,10 +7,10 @@
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
+ *       notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
  *
  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@@ -75,7 +75,8 @@ typedef struct {
        uint32_t                respLen; /* 0 means variable, > 0 fixed */
 } TrinketProtoDesc;
 
-static const TrinketProtoDesc protoDesc[] = { 
+/* hardcoded knowledge from  docs/geyger.pdf */
+static const TrinketProtoDesc protoDesc[] = {
        {CMD_PING,                      4,              5                               },
        {CMD_VCC_VALUE,         4,              8                               },
        {CMD_VCC_ALARM,         8,              8                               },
@@ -104,6 +105,7 @@ dumpBuf(char *msg, uint8_t *buf, int len) {
 
 TrinketErrorCode
 trinketOpen() {
+       TrinketErrorCode        r;
 #ifndef NDEBUG
        int i;
 
@@ -118,7 +120,9 @@ trinketOpen() {
                return ERR_ERROR;
        }
 
-       return trinketPing();
+       if ((r = trinketPing()) != ERR_OK)
+               trinketClose();
+       return r;
 }
 
 void
@@ -174,7 +178,7 @@ trinketPing() {
        }
 
        if (r != sizeof(resp)) {
-               fprintf(stderr, "hid_read_timeout  returns %d instead of %u\n", r, sizeof(resp));
+               fprintf(stderr, "hid_read_timeout  returns %d instead of %u\n", r, (unsigned int)sizeof(resp));
                return ERR_ERROR;
        }
 
@@ -247,7 +251,7 @@ trinketGetCumDose(double *value) {
        }
 
        if (r != sizeof(resp)) {
-               fprintf(stderr, "hid_read_timeout  returns %d instead of %u\n", r, sizeof(resp));
+               fprintf(stderr, "hid_read_timeout  returns %d instead of %u\n", r, (unsigned int)sizeof(resp));
                return ERR_ERROR;
        }