improve log
[trinked.git] / trinket.c
index 8f2cdc9..fa6598d 100644 (file)
--- a/trinket.c
+++ b/trinket.c
  *-------------------------------------------------------------------------
  */
 
-#include <assert.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <hidapi/hidapi.h>
+
+#include <tlog.h>
 #include <trinket.h>
 
 #define         packed_struct __attribute__((packed))
@@ -106,17 +107,15 @@ dumpBuf(char *msg, uint8_t *buf, int len) {
 TrinketErrorCode
 trinketOpen() {
        TrinketErrorCode        r;
-#ifndef NDEBUG
        int i;
 
        for(i = 0; i < sizeof(protoDesc) / sizeof(*protoDesc); i++)
-               assert(i == protoDesc[i].cmdId);
-#endif
+               tassert(i == protoDesc[i].cmdId);
 
        trinketDev = hid_open(0x2047, 0x0301, NULL);
 
        if (trinketDev == NULL) {
-               fprintf(stderr, "hid_open fails\n");
+               tlog(TL_WARN, "hid_open fails");
                return ERR_ERROR;
        }
 
@@ -157,14 +156,14 @@ trinketPing() {
        } resp;
        int     r;
 
-       assert(proto->cmdId == CMD_PING);
-       assert(sizeof(cmd) == cmd.len + sizeof(ProtoHeader));
-       assert(sizeof(resp) == proto->respLen + sizeof(ProtoHeader));
+       tassert(proto->cmdId == CMD_PING);
+       tassert(sizeof(cmd) == cmd.len + sizeof(ProtoHeader));
+       tassert(sizeof(resp) == proto->respLen + sizeof(ProtoHeader));
 
        r = hid_write(trinketDev, (unsigned char*)&cmd, sizeof(cmd));
 
        if (r < 0 || r != sizeof(cmd)) {
-               fprintf(stderr, "hid_write  returns %d instead of %u\n", r, cmd.len);
+               tlog(TL_WARN, "hid_write  returns %d instead of %u", r, cmd.len);
                return ERR_ERROR;
        }
 
@@ -173,32 +172,32 @@ trinketPing() {
 
        r = hid_read_timeout(trinketDev, (unsigned char*)&resp, sizeof(resp), DEV_TIMEOUT);
        if (r < 0) {
-               fprintf(stderr, "hid_read_timeout fails\n");
+               tlog(TL_WARN, "hid_read_timeout fails");
                return  ERR_NO_ANSWER;
        }
 
        if (r != sizeof(resp)) {
-               fprintf(stderr, "hid_read_timeout  returns %d instead of %u\n", r, (unsigned int)sizeof(resp));
+               tlog(TL_WARN, "hid_read_timeout  returns %d instead of %u", r, (unsigned int)sizeof(resp));
                return ERR_ERROR;
        }
 
        if (resp.header.reportId != REPORT_ID) {
-               fprintf(stderr, "hid_read_timeout  returns report id %02x instead of %02x\n", resp.header.reportId, REPORT_ID);
+               tlog(TL_WARN, "hid_read_timeout  returns report id %02x instead of %02x", resp.header.reportId, REPORT_ID);
                return ERR_ERROR;
        }
 
        if (resp.header.size != proto->respLen) {
-               fprintf(stderr, "hid_read_timeout returns length %u intsead if %u\n", resp.header.size, proto->respLen);
+               tlog(TL_WARN, "hid_read_timeout returns length %u intsead if %u", resp.header.size, proto->respLen);
                return ERR_PROTO;
        }
 
        if (resp.len != proto->respLen) {
-               fprintf(stderr, "hid_read_timeout response length %u instead of %u\n", resp.len, proto->respLen);
+               tlog(TL_WARN, "hid_read_timeout response length %u instead of %u", resp.len, proto->respLen);
                return ERR_PROTO;
        }
 
        if (!(resp.begin == ACK_ANSWER_OK && resp.payload == 0 && resp.end == END_ANSWER)) {
-               fprintf(stderr, "hid_read_timeout  ACK %02x with payload %u and ETB %02x\n", resp.begin, resp.payload, resp.end);
+               tlog(TL_WARN, "hid_read_timeout  ACK %02x with payload %u and ETB %02x", resp.begin, resp.payload, resp.end);
                return ERR_PROTO;
        }
 
@@ -230,14 +229,14 @@ trinketGetCumDose(double *value) {
        } resp;
        int     r;
 
-       assert(proto->cmdId == CMD_CUM_DOSE);
-       assert(sizeof(cmd) == cmd.len + sizeof(ProtoHeader));
-       assert(sizeof(resp) == proto->respLen + sizeof(ProtoHeader));
+       tassert(proto->cmdId == CMD_CUM_DOSE);
+       tassert(sizeof(cmd) == cmd.len + sizeof(ProtoHeader));
+       tassert(sizeof(resp) == proto->respLen + sizeof(ProtoHeader));
 
        r = hid_write(trinketDev, (unsigned char*)&cmd, sizeof(cmd));
 
        if (r < 0 || r != sizeof(cmd)) {
-               fprintf(stderr, "hid_send_feature_report  returns %d instead of %u\n", r, cmd.len);
+               tlog(TL_WARN, "hid_send_feature_report  returns %d instead of %u", r, cmd.len);
                return ERR_ERROR;
        }
 
@@ -246,32 +245,32 @@ trinketGetCumDose(double *value) {
 
        r = hid_read_timeout(trinketDev, (unsigned char*)&resp, sizeof(resp), DEV_TIMEOUT);
        if (r < 0) {
-               fprintf(stderr, "hid_read_timeout fails\n");
+               tlog(TL_WARN, "hid_read_timeout fails");
                return  ERR_NO_ANSWER;
        }
 
        if (r != sizeof(resp)) {
-               fprintf(stderr, "hid_read_timeout  returns %d instead of %u\n", r, (unsigned int)sizeof(resp));
+               tlog(TL_WARN, "hid_read_timeout  returns %d instead of %u", r, (unsigned int)sizeof(resp));
                return ERR_ERROR;
        }
 
        if (resp.header.reportId != REPORT_ID) {
-               fprintf(stderr, "hid_read_timeout  returns report id %02x instead of %02x\n", resp.header.reportId, REPORT_ID);
+               tlog(TL_WARN, "hid_read_timeout  returns report id %02x instead of %02x", resp.header.reportId, REPORT_ID);
                return ERR_ERROR;
        }
 
        if (resp.header.size != proto->respLen) {
-               fprintf(stderr, "hid_read_timeout returns length %u intsead if %u\n", resp.header.size, proto->respLen);
+               tlog(TL_WARN, "hid_read_timeout returns length %u intsead if %u", resp.header.size, proto->respLen);
                return ERR_PROTO;
        }
 
        if (resp.len != proto->respLen) {
-               fprintf(stderr, "hid_read_timeout response length %u instead of %u\n", resp.len, proto->respLen);
+               tlog(TL_WARN, "hid_read_timeout response length %u instead of %u", resp.len, proto->respLen);
                return ERR_PROTO;
        }
 
        if (!(resp.begin == ACK_ANSWER_OK && resp.end == END_ANSWER)) {
-               fprintf(stderr, "hid_read_timeout  ACK %02x and ETB %02x\n", resp.begin, resp.end);
+               tlog(TL_WARN, "hid_read_timeout  ACK %02x and ETB %02x", resp.begin, resp.end);
                return ERR_PROTO;
        }