Annotation of embedtools/src/athctl.c, revision 1.1.2.2

1.1.2.2 ! misho       1: /*************************************************************************
        !             2:  * (C) 2010 AITNET - Sofia/Bulgaria - <office@aitbg.com>
        !             3:  *  by Michael Pounov <misho@aitbg.com>
        !             4:  *
        !             5:  * $Author: misho $
        !             6:  * $Id: cfexec.c,v 1.1.1.1.2.5 2010/10/19 14:01:45 misho Exp $
        !             7:  *
        !             8:  *************************************************************************/
1.1.2.1   misho       9: #include "global.h"
1.1.2.2 ! misho      10: #include "athctl.h"
1.1.2.1   misho      11: 
                     12: 
1.1.2.2 ! misho      13: int Verbose;
        !            14: extern char compiled[], compiledby[], compilehost[];
        !            15: 
        !            16: 
        !            17: static void
        !            18: Usage()
        !            19: {
        !            20:        printf( "athCtl is tool for Atheros WiFi cards managment \n"
        !            21:                "=== %s === %s@%s ===\n\n"
        !            22:                "  Syntax: athctl [options] [exec_file]\n\n"
        !            23:                "\t-v\t\tVerbose ...\n"
        !            24:                "\t-t\t\tGet current Atheros maximum range in meters\n"
        !            25:                "\t-i <iface_no>\tApply to this Atheros interface number (like ath0 == 0)\n"
        !            26:                "\t-d <distance>\tMode distance, meters to target\n"
        !            27:                "\t-c <timeout>\tMode distance, mS timeouts correction\n"
        !            28:                "\n", compiled, compiledby, compilehost);
        !            29: }
        !            30: 
        !            31: static int
        !            32: calcDistance(int ifid, int dist, int cor)
        !            33: {
        !            34:        int slottime[2], timeout[2];
        !            35:        size_t len;
        !            36:        char szStr[STRSIZ];
        !            37: 
        !            38:        slottime[0] = 9 + (dist / 300) + (dist % 300 ? 1 : 0);
        !            39:        timeout[0] = slottime[0] * 2 + 3 + cor;
        !            40:        VERB(3) printf("Info:: slottime=%d timeout=%d\n", slottime[0], timeout[0]);
        !            41: 
        !            42:        memset(szStr, 0, STRSIZ);
        !            43:        snprintf(szStr, STRSIZ, SC_SLOTTIME, ifid);
        !            44:        if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) {
        !            45:                printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
        !            46:                return -1;
        !            47:        } else if (sysctlbyname(szStr, &slottime[1], &len, NULL, 0) == -1) {
        !            48:                printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
        !            49:                return -1;
        !            50:        } else if (sysctlbyname(szStr, NULL, NULL, &slottime[0], sizeof slottime[0]) == -1) {
        !            51:                printf("Error:: set sysctl %s from %d #%d - %s\n", szStr, slottime[1], 
        !            52:                                errno, strerror(errno));
        !            53:                return -1;
        !            54:        } else
        !            55:                VERB(1) printf("Info:: set slottime(%d) from %d to %d ... OK!\n", len, 
        !            56:                                slottime[1], slottime[0]);
        !            57: 
        !            58:        memset(szStr, 0, STRSIZ);
        !            59:        snprintf(szStr, STRSIZ, SC_ACKTIMEOUT, ifid);
        !            60:        if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) {
        !            61:                printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
        !            62:                return -1;
        !            63:        } else if (sysctlbyname(szStr, &timeout[1], &len, NULL, 0) == -1) {
        !            64:                printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
        !            65:                return -1;
        !            66:        } else if (sysctlbyname(szStr, NULL, NULL, &timeout[0], sizeof timeout[0]) == -1) {
        !            67:                printf("Error:: set sysctl %s from %d #%d - %s\n", szStr, timeout[1], 
        !            68:                                errno, strerror(errno));
        !            69:                return -1;
        !            70:        } else
        !            71:                VERB(1) printf("Info:: set acktimeout(%d) from %d to %d ... OK!\n", len, 
        !            72:                                timeout[1], timeout[0]);
        !            73: 
        !            74:        memset(szStr, 0, STRSIZ);
        !            75:        snprintf(szStr, STRSIZ, SC_CTSTIMEOUT, ifid);
        !            76:        if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) {
        !            77:                printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
        !            78:                return -1;
        !            79:        } else if (sysctlbyname(szStr, &timeout[1], &len, NULL, 0) == -1) {
        !            80:                printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
        !            81:                return -1;
        !            82:        } else if (sysctlbyname(szStr, NULL, NULL, &timeout[0], sizeof timeout[0]) == -1) {
        !            83:                printf("Error:: set sysctl %s from %d #%d - %s\n", szStr, timeout[1], 
        !            84:                                errno, strerror(errno));
        !            85:                return -1;
        !            86:        } else
        !            87:                VERB(1) printf("Info:: set ctstimeout(%d) from %d to %d ... OK!\n", len, 
        !            88:                                timeout[1], timeout[0]);
        !            89: 
        !            90:        return timeout[0];
        !            91: }
        !            92: 
        !            93: 
        !            94: static int
        !            95: calcTimeout(int ifid, int cor)
        !            96: {
        !            97:        int slottime[2], timeout[2];
        !            98:        size_t len;
        !            99:        char szStr[STRSIZ];
        !           100: 
        !           101:        memset(szStr, 0, STRSIZ);
        !           102:        snprintf(szStr, STRSIZ, SC_SLOTTIME, ifid);
        !           103:        if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) {
        !           104:                printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
        !           105:                return -1;
        !           106:        } else if (sysctlbyname(szStr, &slottime[1], &len, NULL, 0) == -1) {
        !           107:                printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
        !           108:                return -1;
        !           109:        } else
        !           110:                VERB(1) printf("Info:: get slottime(%d) %d ... OK!\n", len, slottime[1]);
        !           111: 
        !           112:        memset(szStr, 0, STRSIZ);
        !           113:        snprintf(szStr, STRSIZ, SC_ACKTIMEOUT, ifid);
        !           114:        if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) {
        !           115:                printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
        !           116:                return -1;
        !           117:        } else if (sysctlbyname(szStr, &timeout[1], &len, NULL, 0) == -1) {
        !           118:                printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno));
        !           119:                return -1;
        !           120:        } else
        !           121:                VERB(1) printf("Info:: get acktimeout(%d) %d ... OK!\n", len, timeout[1]);
        !           122: 
        !           123:        slottime[0] = (timeout[1] - 3 - cor) / 2;
        !           124:        VERB(3) printf("Info:: calculated slottime=%d\n", slottime[0]);
        !           125:        timeout[0] = (slottime[0] - 10) * 300;
        !           126:        VERB(3) printf("Info:: calculated timeout=%d\n", timeout[0]);
        !           127: 
        !           128:        return timeout[0];
        !           129: }
        !           130: // ----------------------------------------------------
        !           131: 
1.1.2.1   misho     132: int
                    133: main(int argc, char **argv)
                    134: {
1.1.2.2 ! misho     135:        char ch, mode = 0;
        !           136:        int ret = 0, dist = 0, cor = 0, ino = 0;
        !           137: 
        !           138:        while ((ch = getopt(argc, argv, "hvti:d:c:")) != -1)
        !           139:                switch (ch) {
        !           140:                        case 'v':
        !           141:                                Verbose++;
        !           142:                                break;
        !           143:                        case 't':
        !           144:                                mode |= 2;
        !           145:                                break;
        !           146:                        case 'i':
        !           147:                                ino = strtol(optarg, NULL, 0);
        !           148:                                if (ino < 0) {
        !           149:                                        printf("Error:: in interface number %d\n", ino);
        !           150:                                        return 1;
        !           151:                                }
        !           152:                                break;
        !           153:                        case 'd':
        !           154:                                mode |= 1;
        !           155:                                dist = strtol(optarg, NULL, 0);
        !           156:                                if (dist < 1) {
        !           157:                                        printf("Error:: in distance meters %d\n", dist);
        !           158:                                        return 1;
        !           159:                                }
        !           160:                                break;
        !           161:                        case 'c':
        !           162:                                mode |= 1;
        !           163:                                cor = strtol(optarg, NULL, 0);
        !           164:                                break;
        !           165:                        case 'h':
        !           166:                        default:
        !           167:                                Usage();
        !           168:                                return 1;
        !           169:                }
        !           170:        argc -= optind;
        !           171:        argv += optind;
        !           172:        if (!mode) {
        !           173:                printf("Error:: not selected mode for operation ...\n");
        !           174:                return 1;
        !           175:        }
        !           176: 
        !           177:        if (mode & 1)
        !           178:                if ((ret = calcDistance(ino, dist, cor)) < 1)
        !           179:                        return 2;
        !           180:        if (mode & 2) {
        !           181:                if ((ret = calcTimeout(ino, cor)) < 1)
        !           182:                        return 2;
        !           183:                else {
        !           184:                        VERB(1)
        !           185:                                printf("Maximum approximate distance ~%d meters\n", ret);
        !           186:                        else
        !           187:                                printf("~%d\n", ret);
        !           188:                }
        !           189:        }
        !           190: 
1.1.2.1   misho     191:        return 0;
                    192: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>