]> err.no Git - gant/commitdiff
Fix up debug handling somewhat
authorTollef Fog Heen <tfheen@err.no>
Sun, 28 Aug 2011 19:26:29 +0000 (21:26 +0200)
committerTollef Fog Heen <tfheen@err.no>
Sun, 28 Aug 2011 19:26:29 +0000 (21:26 +0200)
fr60.c

diff --git a/fr60.c b/fr60.c
index eceebb5f3f5789d4e65a96a4cb339d88d9abbf12..c1b8c6f47481b408141352d9d624eadb5fd94f52 100644 (file)
--- a/fr60.c
+++ b/fr60.c
@@ -140,6 +140,18 @@ static int antfsdir_used = 0;
 #define ANTFSHEADERSIZE 16
 #define ANTFSDIRSIZE 16
 
+/* Debug print */
+static void D(const char *fmt, ...) {
+       va_list ap;
+
+       if (!dbg)
+               return;
+
+       va_start(ap, fmt);
+       vprintf(fmt, ap);
+       va_end(ap);
+}
+
 void
 usage(void)
 {
@@ -262,7 +274,7 @@ decode_antfs_dir(const uchar* blast, uint blsize)
                }
                ++antfsdir_used;
        }
-       if (dbg) printf("%d entries in directory\n", antfsdir_used);
+       D("%d entries in directory\n", antfsdir_used);
 }
 
 void
@@ -273,9 +285,9 @@ save_antfs_file(const uchar* blast, uint blsize)
        char filename[256];
        int nw;
 
-       if (dbg) printf("received ant-fs file %d\n", antfsdir[downloadingfilenum].fileno);
+       D("received ant-fs file %d\n", antfsdir[downloadingfilenum].fileno);
        if (blsize < 24) {
-               if (dbg) printf("didn't receive full ant-fs header\n");
+               fprintf(stderr, "didn't receive full ant-fs header\n");
                exit(1);
        }
        size = blast[12] + blast[13]*256 + blast[14]*256*256 + blast[15]*256*256*256;
@@ -310,15 +322,14 @@ chevent(uchar chan, uchar event)
        uint id;
        int i;
        uint cid;
-       if (dbg) printf("chevent %02x %02x\n", chan, event);
+       D("chevent %02x %02x\n", chan, event);
 
        if (event == EVENT_RX_BROADCAST) {
        }
        cid = cbuf[4]+cbuf[5]*256+cbuf[6]*256*256+cbuf[7]*256*256*256;
        memcpy((void *)&id, cbuf+4, 4);
 
-       if (dbg)
-               fprintf(stderr, "cid %08x myid %08x\n", cid, myid);
+       D("cid %08x myid %08x\n", cid, myid);
        if (dbg && event != EVENT_RX_BURST_PACKET) {
                fprintf(stderr, "chan %d event %02x channel open: ", chan, event);
                for (i = 0; i < 8; i++)
@@ -336,12 +347,11 @@ chevent(uchar chan, uchar event)
                        break;
                }
                lastphase = phase; // store the last phase we see the watch broadcast
-               if (dbg) printf("lastphase %d\n", lastphase);
+               D("lastphase %d\n", lastphase);
                if (!pairing && !nopairing)
                        pairing = cbuf[1] & 8;
 
-               if (dbg)
-                       printf("watch status %02x phase %d id %08x\n", status, phase, id);
+               D("watch status %02x phase %d id %08x\n", status, phase, id);
 
                if (!sentid) {
                        sentid = 1;
@@ -370,7 +380,7 @@ chevent(uchar chan, uchar event)
                                newfreq = 0;
                        }
                        if (0 && !newdata) {
-                               if (dbg) printf("device has no new data, not downloading\n");
+                               D("device has no new data, not downloading\n");
                                break;
                        }
                        // phase 0 seen after reset at end of download
@@ -408,9 +418,8 @@ chevent(uchar chan, uchar event)
                                memcpy((void *)&myauth4, authdata+28, 4);
                                memcpy((void *)&mydev, authdata+12, 4);
                                memcpy((void *)&myid, authdata+4, 4);
-                               if (dbg)
-                                       fprintf(stderr, "dev %d auth %08x %08x %08x %08x id %08x\n",
-                                               mydev, myauth1, myauth2, myauth3, myauth4, myid);
+                               D("dev %d auth %08x %08x %08x %08x id %08x\n",
+                                 mydev, myauth1, myauth2, myauth3, myauth4, myid);
                        }
                        // bind to watch
                        if (!donebind && devid) {
@@ -420,18 +429,18 @@ chevent(uchar chan, uchar event)
                                ack.id = myid;
                                ANT_SendAcknowledgedData(chan, (void *)&ack); // bind
                        } else {
-                               if (dbg) printf("donebind %d devid %x\n", donebind, devid);
+                               D("donebind %d devid %x\n", donebind, devid);
                        }
                        break;
                case 1:
-                       if (dbg) printf("case 1 %x\n", peerdev);
+                       D("case 1 %x\n", peerdev);
                        if (peerdev) {
-                               if (dbg) printf("case 1 peerdev\n");
+                               D("case 1 peerdev\n");
                                // if watch has sent id
                                if (mydev != 0 && peerdev != mydev) {
                                        fprintf(stderr, "Don't know this device %08x != %08x\n", peerdev, mydev);
                                } else if (!sentauth && !waitauth) {
-                                       if (dbg) printf("case 1 diffdev\n");
+                                       D("case 1 diffdev\n");
                                        assert(sizeof auth == AUTHSIZE);
                                        auth.code = 0x44; auth.atype = 4; auth.phase = 3; auth.u1 = 12;
                                        auth.id = myid; auth.auth1 = myauth1; auth.auth2 = myauth2;
@@ -441,10 +450,10 @@ chevent(uchar chan, uchar event)
                                        ANT_SendBurstTransfer(chan, (void *)&auth, (sizeof auth)/8); // send our auth data
                                }
                        }
-                       if (dbg) printf("case 1 cid %x myid %x\n", cid, myid);
+                       D("case 1 cid %x myid %x\n", cid, myid);
                        if (!sentack2 && cid == myid && !waitauth) {
                                sentack2 = 1;
-                               if (dbg) printf("sending ack2\n");
+                               D("sending ack2\n");
                                // if it did bind to me before someone else
                                ack.code = 0x44; ack.atype = 4; ack.c1 = 0x01; ack.c2 = 0x00;
                                ack.id = myid;
@@ -456,11 +465,11 @@ chevent(uchar chan, uchar event)
                        if (!downloadstarted) {
                                downloadstarted = 1;
                                downloadingfilenum = -1;
-                               if (dbg) printf("downloading directory\n");
+                               D("downloading directory\n");
                                start_download(chan, 0);
                        }
                        if (downloadfinished) {
-                               if (dbg) printf("finished download\n");
+                               D("finished download\n");
                                ack.code = 0x44; ack.atype = 3; ack.c1 = 0x00; ack.c2 = 0x00; ack.id = 0;
                                if (!passive) ANT_SendAcknowledgedData(chan, (void *)&ack); // tell garmin we're finished
                        }
@@ -471,7 +480,7 @@ chevent(uchar chan, uchar event)
                                printf("Please confirm pairing on watch\n");
                                waitauth = 2; // next burst data is auth data
                        } else {
-                               if (dbg) printf("not sure why in phase 3\n");
+                               D("not sure why in phase 3\n");
                                if (!sentgetv) {
                                        sentgetv = 1;
                                        //ANT_SendBurstTransferA(chan, getversion, strlen(getversion)/16);
@@ -479,20 +488,23 @@ chevent(uchar chan, uchar event)
                        }
                        break;
                default:
-                       if (dbg) fprintf(stderr, "Unknown phase %d\n", phase);
+                       D("Unknown phase %d\n", phase);
                        break;
                }
                break;
        }
        case EVENT_RX_BURST_PACKET:
                // now handled in coalesced burst below
-               if (dbg) printf("burst\n");
+               D("burst 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", cbuf[0], cbuf[1], cbuf[2], cbuf[3], cbuf[4], cbuf[5], cbuf[6], cbuf[7], cbuf[8], cbuf[9], cbuf[10]);
+               if (cbuf[10] == 0x80) {
+                       printf("\t=> channel id bytes: 0x%02x 0x%02x 0x%02x 0x%02x\n", cbuf[11], cbuf[12], cbuf[13], cbuf[14]);
+               }
                break;
        case EVENT_RX_FAKE_BURST:
-               if (dbg) printf("rxfake burst pairing %d waitauth %d\n", pairing, waitauth);
+               D("rxfake burst pairing %d waitauth %d\n", pairing, waitauth);
                blsize = *(int *)(cbuf+4);  // FIXME: not endian-safe
                memcpy(&blast, cbuf+8, 4);  // FIXME: not endian-safe
-               if (dbg) {
+               if (dbg > 2) {
                        printf("fake burst %d %lx ", blsize, (long)blast);
                        for (i = 0; i < blsize && i < 64; i++)
                                printf("%02x", blast[i]);
@@ -530,8 +542,7 @@ chevent(uchar chan, uchar event)
                if (sentauth) {
                } else if (!nopairing && pairing && blast) {
                        memcpy(&peerdev, blast+12, 4);
-                       if (dbg)
-                               printf("watch id %08x waitauth %d\n", peerdev, waitauth);
+                       D("watch id %08x waitauth %d\n", peerdev, waitauth);
                        if (mydev != 0 && peerdev != mydev) {
                                fprintf(stderr, "Don't know this device %08x != %08x\n", peerdev, mydev);
                                exit(1);
@@ -575,7 +586,7 @@ chevent(uchar chan, uchar event)
                                waitauth = 1;
                                ANT_SendBurstTransfer(chan, (void *)&pair, (sizeof pair)/8) ; // send pair data
                        } else {
-                               if (dbg) printf("not pairing\n");
+                               D("not pairing\n");
                        }
                } else if (!gotwatchid && (lastphase == 1)) {
                        static int once = 0;
@@ -583,23 +594,21 @@ chevent(uchar chan, uchar event)
                        // garmin sending authentication/identification data
                        if (!once) {
                                once = 1;
-                               if (dbg)
-                                       fprintf(stderr, "id data: ");
+                               D("id data: ");
                        }
-                       if (dbg)
+                       if (dbg) {
                                for (i = 0; i < blsize; i++)
                                        fprintf(stderr, "%02x", blast[i]);
-                       if (dbg)
-                                       fprintf(stderr, "\n");
+                               fprintf(stderr, "\n");
+                       }
                        memcpy(&peerdev, blast+12, 4);
-                       if (dbg)
-                               printf("watch id %08x\n", peerdev);
+                       D("watch id %08x\n", peerdev);
                        if (mydev != 0 && peerdev != mydev) {
                                fprintf(stderr, "Don't know this device %08x != %08x\n", peerdev, mydev);
                                exit(1);
                        }
                }
-               if (dbg) printf("continuing after burst\n");
+               D("continuing after burst\n");
                break;
        }
        return 1;
@@ -610,18 +619,16 @@ revent(uchar chan, uchar event)
 {
        int i;
 
-       if (dbg) printf("revent %02x %02x\n", chan, event);
+       D("revent %02x %02x\n", chan, event);
        switch (event) {
        case EVENT_TRANSFER_TX_COMPLETED:
-               if (dbg) printf("Transfer complete %02x\n", ebuf[1]);
+               D("Transfer complete %02x\n", ebuf[1]);
                break;
        case TRANSFER_SEQUENCE_NUMBER_ERROR:
-               if (dbg) printf("Transfer sequence number error %02x\n", ebuf[1]);
+               D("Transfer sequence number error %02x\n", ebuf[1]);
                break;
        case EVENT_TRANSFER_RX_FAILED:
-       case EVENT_TRANSFER_TX_FAILED:
-               if (dbg) printf("Transfer failed %d\n", ebuf[1]);
-
+               D("RX Transfer failed %d\n", ebuf[1]);
                // this may not be the best idea in all cases...
                if (downloadingfilenum >= 0) {
                        start_download(chan, antfsdir[downloadingfilenum].fileno);
@@ -633,21 +640,20 @@ revent(uchar chan, uchar event)
        case RESPONSE_NO_ERROR:
                switch (ebuf[1]) {
                case MESG_ASSIGN_CHANNEL_ID:
-                       ANT_AssignChannelEventFunction(chan, chevent, cbuf);
+                       ANT_AssignChannelEventFunction(chevent, cbuf);
                        break;
                case MESG_OPEN_CHANNEL_ID:
                        printf("channel open, waiting for broadcast...\n");
                        break;
                default:
-                       if (dbg) printf("Message %02x NO_ERROR\n", ebuf[1]);
+                       D("Message %02x NO_ERROR\n", ebuf[1]);
                        break;
                }
                break;
        case MESG_CHANNEL_ID_ID:
                devid = ebuf[1]+ebuf[2]*256;
                if (mydev == 0 || devid == mydev%65536) {
-                       if (dbg)
-                               printf("devid %08x myid %08x\n", devid, myid);
+                       D("devid %08x myid %08x\n", devid, myid);
                } else {
                        printf("Ignoring unknown device %08x, mydev %08x\n", devid, mydev);
                        devid = sentid = 0; // reset
@@ -777,8 +783,8 @@ main(int ac, char *av[])
        ANT_SetChannelId(chan, devno, devtype, manid);
        ANT_OpenChannel(chan);
        ANT_RequestMessage(chan, MESG_CHANNEL_STATUS_ID); //informative
-                       
-       if (dbg) printf("waiting...\n");
+
+       D("waiting...\n");
 
        // everything handled in event functions
        for(;;)