--- embedtools/src/athctl.c 2010/10/22 09:16:27 1.1.2.1 +++ embedtools/src/athctl.c 2010/10/23 01:50:49 1.1.2.2 @@ -1,8 +1,192 @@ +/************************************************************************* + * (C) 2010 AITNET - Sofia/Bulgaria - + * by Michael Pounov + * + * $Author: misho $ + * $Id: athctl.c,v 1.1.2.2 2010/10/23 01:50:49 misho Exp $ + * + *************************************************************************/ #include "global.h" +#include "athctl.h" +int Verbose; +extern char compiled[], compiledby[], compilehost[]; + + +static void +Usage() +{ + printf( "athCtl is tool for Atheros WiFi cards managment \n" + "=== %s === %s@%s ===\n\n" + " Syntax: athctl [options] [exec_file]\n\n" + "\t-v\t\tVerbose ...\n" + "\t-t\t\tGet current Atheros maximum range in meters\n" + "\t-i \tApply to this Atheros interface number (like ath0 == 0)\n" + "\t-d \tMode distance, meters to target\n" + "\t-c \tMode distance, mS timeouts correction\n" + "\n", compiled, compiledby, compilehost); +} + +static int +calcDistance(int ifid, int dist, int cor) +{ + int slottime[2], timeout[2]; + size_t len; + char szStr[STRSIZ]; + + slottime[0] = 9 + (dist / 300) + (dist % 300 ? 1 : 0); + timeout[0] = slottime[0] * 2 + 3 + cor; + VERB(3) printf("Info:: slottime=%d timeout=%d\n", slottime[0], timeout[0]); + + memset(szStr, 0, STRSIZ); + snprintf(szStr, STRSIZ, SC_SLOTTIME, ifid); + if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) { + printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno)); + return -1; + } else if (sysctlbyname(szStr, &slottime[1], &len, NULL, 0) == -1) { + printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno)); + return -1; + } else if (sysctlbyname(szStr, NULL, NULL, &slottime[0], sizeof slottime[0]) == -1) { + printf("Error:: set sysctl %s from %d #%d - %s\n", szStr, slottime[1], + errno, strerror(errno)); + return -1; + } else + VERB(1) printf("Info:: set slottime(%d) from %d to %d ... OK!\n", len, + slottime[1], slottime[0]); + + memset(szStr, 0, STRSIZ); + snprintf(szStr, STRSIZ, SC_ACKTIMEOUT, ifid); + if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) { + printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno)); + return -1; + } else if (sysctlbyname(szStr, &timeout[1], &len, NULL, 0) == -1) { + printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno)); + return -1; + } else if (sysctlbyname(szStr, NULL, NULL, &timeout[0], sizeof timeout[0]) == -1) { + printf("Error:: set sysctl %s from %d #%d - %s\n", szStr, timeout[1], + errno, strerror(errno)); + return -1; + } else + VERB(1) printf("Info:: set acktimeout(%d) from %d to %d ... OK!\n", len, + timeout[1], timeout[0]); + + memset(szStr, 0, STRSIZ); + snprintf(szStr, STRSIZ, SC_CTSTIMEOUT, ifid); + if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) { + printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno)); + return -1; + } else if (sysctlbyname(szStr, &timeout[1], &len, NULL, 0) == -1) { + printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno)); + return -1; + } else if (sysctlbyname(szStr, NULL, NULL, &timeout[0], sizeof timeout[0]) == -1) { + printf("Error:: set sysctl %s from %d #%d - %s\n", szStr, timeout[1], + errno, strerror(errno)); + return -1; + } else + VERB(1) printf("Info:: set ctstimeout(%d) from %d to %d ... OK!\n", len, + timeout[1], timeout[0]); + + return timeout[0]; +} + + +static int +calcTimeout(int ifid, int cor) +{ + int slottime[2], timeout[2]; + size_t len; + char szStr[STRSIZ]; + + memset(szStr, 0, STRSIZ); + snprintf(szStr, STRSIZ, SC_SLOTTIME, ifid); + if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) { + printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno)); + return -1; + } else if (sysctlbyname(szStr, &slottime[1], &len, NULL, 0) == -1) { + printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno)); + return -1; + } else + VERB(1) printf("Info:: get slottime(%d) %d ... OK!\n", len, slottime[1]); + + memset(szStr, 0, STRSIZ); + snprintf(szStr, STRSIZ, SC_ACKTIMEOUT, ifid); + if (sysctlbyname(szStr, NULL, &len, NULL, 0) == -1) { + printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno)); + return -1; + } else if (sysctlbyname(szStr, &timeout[1], &len, NULL, 0) == -1) { + printf("Error:: get sysctl %s #%d - %s\n", szStr, errno, strerror(errno)); + return -1; + } else + VERB(1) printf("Info:: get acktimeout(%d) %d ... OK!\n", len, timeout[1]); + + slottime[0] = (timeout[1] - 3 - cor) / 2; + VERB(3) printf("Info:: calculated slottime=%d\n", slottime[0]); + timeout[0] = (slottime[0] - 10) * 300; + VERB(3) printf("Info:: calculated timeout=%d\n", timeout[0]); + + return timeout[0]; +} +// ---------------------------------------------------- + int main(int argc, char **argv) { + char ch, mode = 0; + int ret = 0, dist = 0, cor = 0, ino = 0; + + while ((ch = getopt(argc, argv, "hvti:d:c:")) != -1) + switch (ch) { + case 'v': + Verbose++; + break; + case 't': + mode |= 2; + break; + case 'i': + ino = strtol(optarg, NULL, 0); + if (ino < 0) { + printf("Error:: in interface number %d\n", ino); + return 1; + } + break; + case 'd': + mode |= 1; + dist = strtol(optarg, NULL, 0); + if (dist < 1) { + printf("Error:: in distance meters %d\n", dist); + return 1; + } + break; + case 'c': + mode |= 1; + cor = strtol(optarg, NULL, 0); + break; + case 'h': + default: + Usage(); + return 1; + } + argc -= optind; + argv += optind; + if (!mode) { + printf("Error:: not selected mode for operation ...\n"); + return 1; + } + + if (mode & 1) + if ((ret = calcDistance(ino, dist, cor)) < 1) + return 2; + if (mode & 2) { + if ((ret = calcTimeout(ino, cor)) < 1) + return 2; + else { + VERB(1) + printf("Maximum approximate distance ~%d meters\n", ret); + else + printf("~%d\n", ret); + } + } + return 0; }