1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
|
/*!
* @file libhid.c
* @brief NUT HID Library - User API (Generic HID Access using MGE HIDParser)
*
* @author Copyright (C) 2003 - 2007
* Arnaud Quette <arnaud.quette@free.fr> && <arnaud.quette@mgeups.com>
* John Stamp <kinsayder@hotmail.com>
* Peter Selinger <selinger@users.sourceforge.net>
* Arjen de Korte <adkorte-guest@alioth.debian.org>
*
* This program is sponsored by MGE UPS SYSTEMS - opensource.mgeups.com
*
* The logic of this file is ripped from mge-shut driver (also from
* Arnaud Quette), which is a "HID over serial link" UPS driver for
* Network UPS Tools <https://www.networkupstools.org/>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* -------------------------------------------------------------------------- */
#include "config.h" /* must be the first header */
#include <stdio.h>
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
/* #include "nut_float.h" */
#include "libhid.h"
#include "hidparser.h"
#include "common.h" /* for xmalloc, upsdebugx prototypes */
#include "nut_stdint.h"
/* Communication layers and drivers (USB and MGE SHUT) */
#if (defined SHUT_MODE) && SHUT_MODE
# include "libshut.h"
communication_subdriver_t *comm_driver = &shut_subdriver;
#else /* !SHUT_MODE => USB */
# include "nut_libusb.h"
communication_subdriver_t *comm_driver = &usb_subdriver;
#endif /* SHUT_MODE / USB */
/* support functions */
static double logical_to_physical(HIDData_t *Data, long logical);
static long physical_to_logical(HIDData_t *Data, double physical);
static const char *hid_lookup_path(const HIDNode_t usage, usage_tables_t *utab);
static long hid_lookup_usage(const char *name, usage_tables_t *utab);
static int string_to_path(const char *string, HIDPath_t *path, usage_tables_t *utab);
static int path_to_string(char *string, size_t size, const HIDPath_t *path, usage_tables_t *utab);
static int8_t get_unit_expo(const HIDData_t *hiddata);
static double exponent(double a, int8_t b);
/* Tweak flag for APC Back-UPS */
size_t max_report_size = 0;
/* Tweaks for Powercom, at least */
int interrupt_only = 0;
size_t interrupt_size = 0;
#define SMIN(a, b) ( ((intmax_t)(a) < (intmax_t)(b)) ? (a) : (b) )
#define UMIN(a, b) ( ((uintmax_t)(a) < (uintmax_t)(b)) ? (a) : (b) )
/* ---------------------------------------------------------------------- */
/* report buffering system */
/* HID data items are retrieved via "reports". Each report is
identified by a report ID, which is an integer in the range
0-255. Each report can hold several items. To avoid retrieving a
given report multiple times in short succession, we use a data
structure called a "report buffer". The functions in this group
operate on entire *reports*, not individual data items. */
void free_report_buffer(reportbuf_t *rbuf)
{
int i;
if (!rbuf)
return;
for (i=0; i<256; i++) {
free(rbuf->data[i]);
}
free(rbuf);
}
/* allocate a new report buffer. Return pointer on success, else NULL
with errno set. The returned data structure must later be freed
with free_report_buffer(). */
reportbuf_t *new_report_buffer(HIDDesc_t *arg_pDesc)
{
HIDData_t *pData;
reportbuf_t *rbuf;
int id;
size_t i;
if (!arg_pDesc)
return NULL;
rbuf = calloc(1, sizeof(*rbuf));
if (!rbuf) {
return NULL;
}
/* now go through all items that are part of this report */
for (i=0; i<arg_pDesc->nitems; i++) {
pData = &arg_pDesc->item[i];
id = pData->ReportID;
/* skip reports that already have been allocated */
if (rbuf->data[id])
continue;
/* first byte holds id */
rbuf->len[id] = arg_pDesc->replen[id] + 1;
/* skip zero length reports */
if (rbuf->len[id] < 1) {
continue;
}
rbuf->data[id] = calloc(rbuf->len[id], sizeof(*(rbuf->data[id])));
if (rbuf->data[id])
continue;
/* on failure, give up what we got so far */
free_report_buffer(rbuf);
return NULL;
}
return rbuf;
}
/* ---------------------------------------------------------------------- */
/* the functions in this next group operate on buffered reports, but
operate on individual items, not whole reports. */
/* refresh the report with the given id in the report buffer rbuf. If
the report is not yet in the buffer, or if it is older than "age"
seconds, then the report is freshly read from the USB
device. Otherwise, it is unchanged.
Return 0 on success, -1 on error with errno set. */
/* because buggy firmwares from APC return wrong report size, we either
ask the report with the found report size or with the whole buffer size
depending on the max_report_size flag */
static int refresh_report_buffer(reportbuf_t *rbuf, hid_dev_handle_t udev, HIDData_t *pData, time_t age)
{
usb_ctrl_repindex id = pData->ReportID;
int ret;
size_t r;
if (interrupt_only || rbuf->ts[id] + age > time(NULL)) {
/* buffered report is still good; nothing to do */
upsdebug_hex(3, "Report[buf]", rbuf->data[id], rbuf->len[id]);
return 0;
}
r = max_report_size ? sizeof(rbuf->data[id]) : rbuf->len[id];
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_TYPE_LIMIT_COMPARE) )
# pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS
# pragma GCC diagnostic ignored "-Wtype-limits"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-unsigned-zero-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_TYPE_LIMIT_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-type-limit-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
/* Older CLANG (e.g. clang-3.4) seems to not support the GCC pragmas above */
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#pragma clang diagnostic ignored "-Wtautological-compare"
#pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
if ((uintmax_t)r > (uintmax_t)USB_CTRL_CHARBUFSIZE_MAX) {
upsdebugx(2,
"%s: suggested buffer size %" PRIuSIZE " exceeds "
"USB_CTRL_CHARBUFSIZE_MAX %" PRIuMAX "; "
"report will be constrained",
__func__, r, (uintmax_t)USB_CTRL_CHARBUFSIZE_MAX);
if ((uintmax_t)USB_CTRL_CHARBUFSIZE_MAX <= (uintmax_t)SIZE_MAX
&& USB_CTRL_CHARBUFSIZE_MAX > 0) {
r = (size_t)USB_CTRL_CHARBUFSIZE_MAX - 1;
} else {
/* SIZE_MAX is obviously too much here; least common
* denominator across libs can be UINT8 or UINT16.
* We should never hit this codepath unless definition
* above is bonkers, anyway.
*/
r = (size_t)UINT8_MAX - 1;
}
/* Avoid overflowing known buffer size, to be sure: */
r = UMIN(r, sizeof(rbuf->data[id]));
if (!max_report_size) {
r = UMIN(r, rbuf->len[id]);
}
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_TYPE_LIMIT_COMPARE) )
# pragma GCC diagnostic pop
#endif
ret = comm_driver->get_report(udev, id,
(usb_ctrl_charbuf)rbuf->data[id],
(usb_ctrl_charbufsize)r);
if (ret <= 0) {
errno = -ret;
return -1;
}
r = (size_t)ret;
if (rbuf->len[id] != r) {
/* e.g. if max_report_size flag was set */
upsdebugx(2,
"%s: expected %" PRIuSIZE " bytes, but got %" PRIuSIZE " instead",
__func__, rbuf->len[id], r);
upsdebug_hex(3, "Report[err]",
(usb_ctrl_charbuf)rbuf->data[id], r);
} else {
upsdebug_hex(3, "Report[get]",
(usb_ctrl_charbuf)rbuf->data[id], rbuf->len[id]);
}
/* have (valid) report */
time(&rbuf->ts[id]);
return 0;
}
/* read the logical value for the given pData. No logical to physical
conversion is performed. If age>0, the read operation is buffered
if the item's age is less than "age". On success, return 0 and
store the answer in *value. On failure, return -1 and set errno. */
static int get_item_buffered(reportbuf_t *rbuf, hid_dev_handle_t udev, HIDData_t *pData, long *Value, time_t age)
{
int id = pData->ReportID;
int r;
r = refresh_report_buffer(rbuf, udev, pData, age);
if (r<0) {
return -1;
}
GetValue(rbuf->data[id], pData, Value);
return 0;
}
/* set the logical value for the given pData. No physical to logical
conversion is performed. On success, return 0, and failure, return
-1 and set errno. The updated value is sent to the device. */
static int set_item_buffered(reportbuf_t *rbuf, hid_dev_handle_t udev, HIDData_t *pData, long Value)
{
usb_ctrl_repindex id = pData->ReportID;
int ret;
size_t r = rbuf->len[id];
SetValue(pData, rbuf->data[id], Value);
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_TYPE_LIMIT_COMPARE) )
# pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS
# pragma GCC diagnostic ignored "-Wtype-limits"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-unsigned-zero-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_TYPE_LIMIT_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-type-limit-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
/* Older CLANG (e.g. clang-3.4) seems to not support the GCC pragmas above */
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#pragma clang diagnostic ignored "-Wtautological-compare"
#pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
if ((uintmax_t)r > (uintmax_t)USB_CTRL_CHARBUFSIZE_MAX) {
upsdebugx(2,
"%s: suggested buffer size %" PRIuSIZE " exceeds "
"USB_CTRL_CHARBUFSIZE_MAX %" PRIuMAX "; "
"item setting will be constrained",
__func__, r, (uintmax_t)USB_CTRL_CHARBUFSIZE_MAX);
if ((uintmax_t)USB_CTRL_CHARBUFSIZE_MAX <= (uintmax_t)SIZE_MAX
&& USB_CTRL_CHARBUFSIZE_MAX > 0) {
r = (size_t)USB_CTRL_CHARBUFSIZE_MAX - 1;
} else {
/* SIZE_MAX is obviously too much here; least common
* denominator across libs can be UINT8 or UINT16.
* We should never hit this codepath unless definition
* above is bonkers, anyway.
*/
r = (size_t)UINT8_MAX - 1;
}
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_TYPE_LIMIT_COMPARE) )
# pragma GCC diagnostic pop
#endif
ret = comm_driver->set_report(udev, id,
(usb_ctrl_charbuf)rbuf->data[id],
(usb_ctrl_charbufsize)r);
if (ret <= 0) {
return -1;
}
upsdebug_hex(3, "Report[set]", rbuf->data[id], r);
/* expire report */
rbuf->ts[id] = 0;
return 0;
}
/* file a given report in the report buffer. This is used when the
report has been obtained without having been explicitly requested,
e.g., it arrived through an interrupt transfer. Returns 0 on
success, -1 on error with errno set. */
static int file_report_buffer(reportbuf_t *rbuf, unsigned char *buf, size_t buflen)
{
int id = buf[0];
/* broken report descriptors are common, so store whatever we can */
memcpy(rbuf->data[id], buf, (buflen < rbuf->len[id]) ? buflen : rbuf->len[id]);
if (rbuf->len[id] != buflen) {
upsdebugx(2,
"%s: expected %" PRIuSIZE " bytes, but got %" PRIuSIZE " instead",
__func__, rbuf->len[id], buflen);
upsdebug_hex(3, "Report[err]", buf, buflen);
} else {
upsdebug_hex(3, "Report[int]", rbuf->data[id], rbuf->len[id]);
}
/* have (valid) report */
time(&rbuf->ts[id]);
return 0;
}
/* ---------------------------------------------------------------------- */
/* Units and exponents table (HID PDC, 3.2.3) */
#define NB_HID_UNITS 10
static struct {
const long Type;
const int8_t Expo;
} HIDUnits[NB_HID_UNITS] = {
{ 0x00000000, 0 }, /* None */
{ 0x00F0D121, 7 }, /* Voltage */
{ 0x00100001, 0 }, /* Ampere */
{ 0x0000D121, 7 }, /* VA */
{ 0x0000D121, 7 }, /* Watts */
{ 0x00001001, 0 }, /* second */
{ 0x00010001, 0 }, /* K */
{ 0x00000000, 0 }, /* percent */
{ 0x0000F001, 0 }, /* Hertz */
{ 0x00101001, 0 }, /* As */
};
/* ---------------------------------------------------------------------- */
/* CAUTION: be careful when modifying the output format of this function,
* since it's used to produce sub-drivers "stub" using
* scripts/subdriver/gen-usbhid-subdriver.sh
*/
void HIDDumpTree(hid_dev_handle_t udev, HIDDevice_t *hd, usage_tables_t *utab)
{
size_t i;
#if (defined SHUT_MODE) && SHUT_MODE
NUT_UNUSED_VARIABLE(hd);
#else /* !SHUT_MODE => USB */
/* extract the VendorId for further testing */
int vendorID = hd->VendorID;
int productID = hd->ProductID;
#endif /* SHUT_MODE / USB */
/* Do not go further if we already know nothing will be displayed.
* Some reports take a while before they timeout, so if these are
* not used in the driver, they should only be fetched when we're
* in debug mode
*/
if (nut_debug_level < 1) {
return;
}
upsdebugx(1, "%" PRIuSIZE " HID objects found", pDesc->nitems);
for (i = 0; i < pDesc->nitems; i++)
{
double value;
HIDData_t *pData = &pDesc->item[i];
/* skip reports 254/255 for Eaton / MGE / Dell due to special handling needs */
#if (defined SHUT_MODE) && SHUT_MODE
if ((pData->ReportID == 254) || (pData->ReportID == 255)) {
continue;
}
#else /* !SHUT_MODE => USB */
if ((vendorID == 0x0463) || (vendorID == 0x047c)) {
if ((pData->ReportID == 254) || (pData->ReportID == 255)) {
continue;
}
}
/* skip report 0x54 for Tripplite SU3000LCD2UHV due to firmware bug */
if ((vendorID == 0x09ae) && (productID == 0x1330)) {
if (pData->ReportID == 0x54) {
continue;
}
}
#endif /* SHUT_MODE / USB */
/* Get data value */
if (HIDGetDataValue(udev, pData, &value, MAX_TS) == 1) {
upsdebugx(1, "Path: %s, Type: %s, ReportID: 0x%02x, Offset: %i, Size: %i, Value: %g",
HIDGetDataItem(pData, utab), HIDDataType(pData), pData->ReportID, pData->Offset, pData->Size, value);
continue;
}
upsdebugx(1, "Path: %s, Type: %s, ReportID: 0x%02x, Offset: %i, Size: %i",
HIDGetDataItem(pData, utab), HIDDataType(pData), pData->ReportID, pData->Offset, pData->Size);
}
fflush(stdout);
}
/* Returns text string which can be used to display type of data
*/
const char *HIDDataType(const HIDData_t *hiddata)
{
switch (hiddata->Type)
{
case ITEM_FEATURE:
return "Feature";
case ITEM_INPUT:
return "Input";
case ITEM_OUTPUT:
return "Output";
default:
return "Unknown";
}
}
/* Returns pointer to the corresponding HIDData_t item
* or NULL if path is not found in report descriptor
*/
HIDData_t *HIDGetItemData(const char *hidpath, usage_tables_t *utab)
{
int r;
HIDPath_t Path;
HIDData_t *p;
r = string_to_path(hidpath, &Path, utab);
if (r <= 0) {
upsdebugx(4, "%s: string_to_path() failed to decipher '%s'", __func__, hidpath);
return NULL;
}
/* Get info on object (reportID, offset and size) */
p = FindObject_with_Path(pDesc, &Path, interrupt_only ? ITEM_INPUT:ITEM_FEATURE);
if (!p)
upsdebugx(4, "%s: FindObject_with_Path() failed to locate '%s'", __func__, hidpath);
return p;
}
char *HIDGetDataItem(const HIDData_t *hiddata, usage_tables_t *utab)
{
/* TODO: not thread safe! */
static char itemPath[128];
path_to_string(itemPath, sizeof(itemPath), &hiddata->Path, utab);
return itemPath;
}
/* Return the physical value associated with the given HIDData path.
* return 1 if OK, 0 on fail, -errno otherwise (ie disconnect).
*/
int HIDGetDataValue(hid_dev_handle_t udev, HIDData_t *hiddata, double *Value, time_t age)
{
int r;
long hValue;
if (hiddata == NULL) {
return 0;
}
r = get_item_buffered(reportbuf, udev, hiddata, &hValue, age);
if (r<0) {
upsdebug_with_errno(1, "Can't retrieve Report %02x", hiddata->ReportID);
return -errno;
}
*Value = hValue;
/* Convert Logical Min, Max and Value into Physical */
*Value = logical_to_physical(hiddata, hValue);
/* Process exponents and units */
*Value *= exponent(10, get_unit_expo(hiddata));
return 1;
}
/* Return the physical value associated with the given path.
* return 1 if OK, 0 on fail, -errno otherwise (ie disconnect).
*/
int HIDGetItemValue(hid_dev_handle_t udev, const char *hidpath, double *Value, usage_tables_t *utab)
{
return HIDGetDataValue(udev, HIDGetItemData(hidpath, utab), Value, MAX_TS);
}
/* Return pointer to indexed string (empty if not found)
*/
char *HIDGetIndexString(hid_dev_handle_t udev, const int Index, char *buf, size_t buflen)
{
usb_ctrl_strindex idx;
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_TYPE_LIMIT_COMPARE) )
# pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS
# pragma GCC diagnostic ignored "-Wtype-limits"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-unsigned-zero-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_TYPE_LIMIT_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-type-limit-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
/* Older CLANG (e.g. clang-3.4) seems to not support the GCC pragmas above */
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#pragma clang diagnostic ignored "-Wtautological-compare"
#pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
if ((uintmax_t)Index > (uintmax_t)USB_CTRL_STRINDEX_MAX
|| (intmax_t)Index < (intmax_t)USB_CTRL_STRINDEX_MIN
) {
upsdebugx(2,
"%s: requested index number is out of range, "
"expected %" PRIdMAX " < %i < %" PRIuMAX,
__func__,
(intmax_t)USB_CTRL_STRINDEX_MIN,
Index,
(uintmax_t)USB_CTRL_STRINDEX_MAX);
return NULL;
}
idx = (usb_ctrl_strindex)Index;
if ((uintmax_t)buflen > (uintmax_t)USB_CTRL_CHARBUFSIZE_MAX) {
upsdebugx(2,
"%s: suggested buffer size %" PRIuSIZE " exceeds "
"USB_CTRL_CHARBUFSIZE_MAX %" PRIuMAX "; "
"index string will be constrained",
__func__, buflen,
(uintmax_t)USB_CTRL_CHARBUFSIZE_MAX);
if ((uintmax_t)USB_CTRL_CHARBUFSIZE_MAX <= (uintmax_t)SIZE_MAX
&& USB_CTRL_CHARBUFSIZE_MAX > 0) {
buflen = (size_t)USB_CTRL_CHARBUFSIZE_MAX - 1;
} else {
/* SIZE_MAX is obviously too much here; least common
* denominator across libs can be UINT8 or UINT16.
* We should never hit this codepath unless definition
* above is bonkers, anyway.
*/
buflen = (size_t)UINT8_MAX - 1;
}
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_TYPE_LIMIT_COMPARE) )
# pragma GCC diagnostic pop
#endif
if (comm_driver->get_string(udev, idx, buf, (usb_ctrl_charbufsize)buflen) < 1)
buf[0] = '\0';
return str_rtrim(buf, '\n');
}
/* Return pointer to indexed string from HID path (empty if not found)
*/
char *HIDGetItemString(hid_dev_handle_t udev, const char *hidpath, char *buf, size_t buflen, usage_tables_t *utab)
{
double Index;
if (HIDGetDataValue(udev, HIDGetItemData(hidpath, utab), &Index, MAX_TS) != 1) {
buf[0] = '\0';
return buf;
}
return HIDGetIndexString(udev, Index, buf, buflen);
}
/* Set the given physical value for the variable associated with
* path. return 1 if OK, 0 on fail, -errno otherwise (ie disconnect).
*/
int HIDSetDataValue(hid_dev_handle_t udev, HIDData_t *hiddata, double Value)
{
int r;
long hValue;
if (hiddata == NULL) {
return 0;
}
/* Test if Item is settable */
/* FIXME: not constant == volatile, but
* it doesn't imply that it's RW! */
if (hiddata->Attribute == ATTR_DATA_CST) {
return 0;
}
/* Process exponents and units */
Value /= exponent(10, get_unit_expo(hiddata));
/* Convert Physical Min, Max and Value into Logical */
hValue = physical_to_logical(hiddata, Value);
r = set_item_buffered(reportbuf, udev, hiddata, hValue);
if (r<0) {
upsdebug_with_errno(1, "Can't set Report %02x", hiddata->ReportID);
return -errno;
}
/* flush the report buffer (data may have changed) */
memset(reportbuf->ts, 0, sizeof(reportbuf->ts));
upsdebugx(4, "Set report succeeded");
return 1;
}
bool_t HIDSetItemValue(hid_dev_handle_t udev, const char *hidpath, double Value, usage_tables_t *utab)
{
if (HIDSetDataValue(udev, HIDGetItemData(hidpath, utab), Value) != 1)
return FALSE;
return TRUE;
}
/* On success, return item count >0. When no notifications are available,
* return 'error' or 'no event' code.
*/
int HIDGetEvents(hid_dev_handle_t udev, HIDData_t **event, int eventsize)
{
unsigned char buf[SMALLBUF];
int itemCount = 0;
int buflen, ret;
size_t i, r;
HIDData_t *pData;
/* needs libusb-0.1.8 to work => use ifdef and autoconf */
r = (interrupt_size > 0 && interrupt_size < sizeof(buf))
? interrupt_size : sizeof(buf);
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_TYPE_LIMIT_COMPARE) )
# pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS
# pragma GCC diagnostic ignored "-Wtype-limits"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-unsigned-zero-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_TYPE_LIMIT_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-type-limit-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE
#pragma GCC diagnostic ignored "-Wunreachable-code"
#endif
/* Older CLANG (e.g. clang-3.4) seems to not support the GCC pragmas above */
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
#pragma clang diagnostic ignored "-Wtautological-compare"
#pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
if ((uintmax_t)r > (uintmax_t)USB_CTRL_CHARBUFSIZE_MAX) {
/* FIXME: Should we try here, or plain abort? */
upsdebugx(2,
"%s: suggested buffer size %" PRIuSIZE " exceeds "
"USB_CTRL_CHARBUFSIZE_MAX %" PRIuMAX "; "
"report will be constrained",
__func__, r, (uintmax_t)USB_CTRL_CHARBUFSIZE_MAX);
if ((uintmax_t)USB_CTRL_CHARBUFSIZE_MAX <= (uintmax_t)SIZE_MAX
&& USB_CTRL_CHARBUFSIZE_MAX > 0) {
r = (size_t)USB_CTRL_CHARBUFSIZE_MAX - 1;
} else {
/* SIZE_MAX is obviously too much here; least common
* denominator across libs can be UINT8 or UINT16.
* We should never hit this codepath unless definition
* above is bonkers, anyway.
*/
r = (size_t)UINT8_MAX - 1;
}
/* Avoid overflowing known buffer size, to be sure: */
r = UMIN(r, sizeof(buf));
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_UNSIGNED_ZERO_COMPARE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_UNREACHABLE_CODE) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_TYPE_LIMIT_COMPARE) )
# pragma GCC diagnostic pop
#endif
buflen = comm_driver->get_interrupt(
udev, (usb_ctrl_charbuf)buf,
(usb_ctrl_charbufsize)r,
750);
if (buflen <= 0) {
return buflen; /* propagate "error" or "no event" code */
}
ret = file_report_buffer(reportbuf, buf, (size_t)buflen);
if (ret < 0) {
upsdebug_with_errno(1, "%s: failed to buffer report", __func__);
return -errno;
}
/* now read all items that are part of this report */
for (i=0; i<pDesc->nitems; i++) {
pData = &pDesc->item[i];
/* Variable not part of this report */
if (pData->ReportID != buf[0])
continue;
/* Not an input report */
if (pData->Type != ITEM_INPUT)
continue;
/* maximum number of events reached? */
if (itemCount >= eventsize) {
upsdebugx(1, "%s: too many events (truncated)", __func__);
break;
}
event[itemCount++] = pData;
}
if (itemCount == 0) {
upsdebugx(1, "%s: unexpected input report (ignored)", __func__);
}
return itemCount;
}
/*******************************************************
* Support functions
*******************************************************/
static double logical_to_physical(HIDData_t *Data, long logical)
{
double physical;
double Factor;
upsdebugx(5, "PhyMax = %ld, PhyMin = %ld, LogMax = %ld, LogMin = %ld",
Data->PhyMax, Data->PhyMin, Data->LogMax, Data->LogMin);
/* HID spec says that if one or both are undefined, or if they are
* both 0, then PhyMin = LogMin, PhyMax = LogMax. */
if (!Data->have_PhyMax || !Data->have_PhyMin ||
(Data->PhyMax == 0 && Data->PhyMin == 0))
{
return (double)logical;
}
/* Paranoia */
if ((Data->PhyMax <= Data->PhyMin) || (Data->LogMax <= Data->LogMin))
{
/* this should not really happen */
upsdebugx(5, "Max was not greater than Min, returning logical value as is");
return (double)logical;
}
Factor = (double)(Data->PhyMax - Data->PhyMin) / (Data->LogMax - Data->LogMin);
/* Convert Value */
physical = (double)((logical - Data->LogMin) * Factor) + Data->PhyMin;
if (physical > Data->PhyMax) {
return Data->PhyMax;
}
if (physical < Data->PhyMin) {
return Data->PhyMin;
}
return physical;
}
static long physical_to_logical(HIDData_t *Data, double physical)
{
long logical;
double Factor;
upsdebugx(5, "PhyMax = %ld, PhyMin = %ld, LogMax = %ld, LogMin = %ld",
Data->PhyMax, Data->PhyMin, Data->LogMax, Data->LogMin);
/* HID spec says that if one or both are undefined, or if they are
* both 0, then PhyMin = LogMin, PhyMax = LogMax. */
if (!Data->have_PhyMax || !Data->have_PhyMin ||
(Data->PhyMax == 0 && Data->PhyMin == 0))
{
return (long)physical;
}
/* Paranoia */
if ((Data->PhyMax <= Data->PhyMin) || (Data->LogMax <= Data->LogMin))
{
/* this should not really happen */
upsdebugx(5, "Max was not greater than Min, returning physical value as is");
return (long)physical;
}
Factor = (double)(Data->LogMax - Data->LogMin) / (Data->PhyMax - Data->PhyMin);
/* Convert Value */
logical = (long)((physical - Data->PhyMin) * Factor) + Data->LogMin;
if (logical > Data->LogMax)
return Data->LogMax;
if (logical < Data->LogMin)
return Data->LogMin;
return logical;
}
static int8_t get_unit_expo(const HIDData_t *hiddata)
{
int i;
int8_t unit_expo = hiddata->UnitExp;
upsdebugx(5, "Unit = %08x, UnitExp = %d", (uint32_t)(hiddata->Unit), hiddata->UnitExp);
for (i = 0; i < NB_HID_UNITS; i++) {
if (HIDUnits[i].Type == hiddata->Unit) {
unit_expo -= HIDUnits[i].Expo;
break;
}
}
upsdebugx(5, "Exponent = %d", unit_expo);
return unit_expo;
}
/* exponent function: return a^b */
static double exponent(double a, int8_t b)
{
if (b>0)
return (a * exponent(a, --b)); /* a * a ... */
if (b<0)
return ((1/a) * exponent(a, ++b)); /* (1/a) * (1/a) ... */
return 1;
}
/* translate HID string path to numeric path and return path depth */
static int string_to_path(const char *string, HIDPath_t *path, usage_tables_t *utab)
{
int i = 0;
long usage;
char buf[SMALLBUF];
char *token, *last;
snprintf(buf, sizeof(buf), "%s", string);
for (token = strtok_r(buf, ".", &last); token != NULL; token = strtok_r(NULL, ".", &last))
{
/* lookup tables first (to override defaults) */
/* Note/FIXME?: we happen to process "usage" as a "signed long"
* while the HIDNode_t behind it is (currently) uint32_t.
* The method below returns `-1` for entries not found; however
* half of our permissible range may seem negative and be valid.
*/
if ((usage = hid_lookup_usage(token, utab)) != -1)
{
path->Node[i++] = (HIDNode_t)usage;
continue;
} else {
/* hid_lookup_usage() logs for itself: ... -> not found in lookup table */
upsdebugx(5, "string_to_path: hid_lookup_usage failed, "
"checking if token %s is a raw value", token);
}
/* translate unnamed path components such as "ff860024" */
if (strlen(token) == strspn(token, "1234567890abcdefABCDEF"))
{
long l = strtol(token, NULL, 16);
/* Note: currently per hidtypes.h, HIDNode_t == uint32_t */
if (l < 0 || (uintmax_t)l > (uintmax_t)UINT32_MAX) {
upsdebugx(5, "string_to_path: badvalue (pathcomp): "
"%ld negative or %" PRIuMAX " too large",
l, (uintmax_t)l);
goto badvalue;
}
path->Node[i++] = (HIDNode_t)l;
continue;
}
/* indexed collection */
if (strlen(token) == strspn(token, "[1234567890]"))
{
int l = atoi(token + 1); /* +1: skip the bracket */
if (l < 0 || (uintmax_t)l > (uintmax_t)UINT32_MAX) {
upsdebugx(5, "string_to_path: badvalue(indexed): "
"%d negative or %" PRIuMAX " too large",
l, (uintmax_t)l);
goto badvalue;
}
path->Node[i++] = 0x00ff0000 + (HIDNode_t)l;
continue;
}
badvalue:
/* Uh oh, typo in usage table? */
upsdebugx(1, "string_to_path: couldn't parse %s from %s", token, string);
}
if (i < 0 || i > (int)UINT8_MAX) {
fatalx(EXIT_FAILURE, "Error: string_to_path(): length exceeded");
}
path->Size = (uint8_t)i; /* by construct, i>=0; but anyway checked above to be sure */
upsdebugx(4, "string_to_path: depth = %d", path->Size);
return i;
}
/* translate HID numeric path to string path and return path depth */
static int path_to_string(char *string, size_t size, const HIDPath_t *path, usage_tables_t *utab)
{
int i;
const char *p;
snprintf(string, size, "%s", "");
for (i = 0; i < path->Size; i++)
{
if (i > 0)
snprintfcat(string, size, ".");
/* lookup tables first (to override defaults) */
if ((p = hid_lookup_path(path->Node[i], utab)) != NULL)
{
snprintfcat(string, size, "%s", p);
continue;
}
/* indexed collection */
if ((path->Node[i] & 0xffff0000) == 0x00ff0000)
{
snprintfcat(string, size, "[%u]", path->Node[i] & 0x0000ffff);
continue;
}
/* unnamed path components such as "ff860024" */
snprintfcat(string, size, "%08x", path->Node[i]);
}
return i;
}
/* usage conversion string -> numeric
* Returns -1 for error, or a (HIDNode_t) ranged code value
*/
static long hid_lookup_usage(const char *name, usage_tables_t *utab)
{
int i, j;
for (i = 0; utab[i] != NULL; i++)
{
for (j = 0; utab[i][j].usage_name != NULL; j++)
{
if (strcasecmp(utab[i][j].usage_name, name))
continue;
/* Note: currently per hidtypes.h, HIDNode_t == uint32_t */
upsdebugx(5, "hid_lookup_usage: %s -> %08x", name, (uint32_t)utab[i][j].usage_code);
return (long)(utab[i][j].usage_code);
}
}
upsdebugx(5, "hid_lookup_usage: %s -> not found in lookup table", name);
return -1;
}
/* usage conversion numeric -> string */
static const char *hid_lookup_path(const HIDNode_t usage, usage_tables_t *utab)
{
int i, j;
for (i = 0; utab[i] != NULL; i++)
{
for (j = 0; utab[i][j].usage_name != NULL; j++)
{
if (utab[i][j].usage_code != usage)
continue;
upsdebugx(5, "hid_lookup_path: %08x -> %s", (unsigned int)usage, utab[i][j].usage_name);
return utab[i][j].usage_name;
}
}
upsdebugx(5, "hid_lookup_path: %08x -> not found in lookup table", (unsigned int)usage);
return NULL;
}
/* Lookup this usage name to find its code (page + index) */
/* temporary usage code lookup */
/* FIXME: put as external data, like in usb.ids (or use
* this last?) */
/* Global usage table (from USB HID class definition) */
usage_lkp_t hid_usage_lkp[] = {
/* Power Device Page */
{ "Undefined", 0x00840000 },
{ "iName", 0x00840001 },
{ "PresentStatus", 0x00840002 },
{ "ChangedStatus", 0x00840003 },
{ "UPS", 0x00840004 },
{ "PowerSupply", 0x00840005 },
/* 0x00840006-0x0084000f => Reserved */
{ "BatterySystem", 0x00840010 },
{ "BatterySystemID", 0x00840011 },
{ "Battery", 0x00840012 },
{ "BatteryID", 0x00840013 },
{ "Charger", 0x00840014 },
{ "ChargerID", 0x00840015 },
{ "PowerConverter", 0x00840016 },
{ "PowerConverterID", 0X00840017 },
{ "OutletSystem", 0x00840018 },
{ "OutletSystemID", 0x00840019 },
{ "Input", 0x0084001a },
{ "InputID", 0x0084001b },
{ "Output", 0x0084001c },
{ "OutputID", 0x0084001d },
{ "Flow", 0x0084001e },
{ "FlowID", 0x0084001f },
{ "Outlet", 0x00840020 },
{ "OutletID", 0x00840021 },
{ "Gang", 0x00840022 },
{ "GangID", 0x00840023 },
{ "PowerSummary", 0x00840024 },
{ "PowerSummaryID", 0x00840025 },
/* 0x00840026-0x0084002f => Reserved */
{ "Voltage", 0x00840030 },
{ "Current", 0x00840031 },
{ "Frequency", 0x00840032 },
{ "ApparentPower", 0x00840033 },
{ "ActivePower", 0x00840034 },
{ "PercentLoad", 0x00840035 },
{ "Temperature", 0x00840036 },
{ "Humidity", 0x00840037 },
{ "BadCount", 0x00840038 },
/* 0x00840039-0x0084003f => Reserved */
{ "ConfigVoltage", 0x00840040 },
{ "ConfigCurrent", 0x00840041 },
{ "ConfigFrequency", 0x00840042 },
{ "ConfigApparentPower", 0x00840043 },
{ "ConfigActivePower", 0x00840044 },
{ "ConfigPercentLoad", 0x00840045 },
{ "ConfigTemperature", 0x00840046 },
{ "ConfigHumidity", 0x00840047 },
/* 0x00840048-0x0084004f => Reserved */
{ "SwitchOnControl", 0x00840050 },
{ "SwitchOffControl", 0x00840051 },
{ "ToggleControl", 0x00840052 },
{ "LowVoltageTransfer", 0x00840053 },
{ "HighVoltageTransfer", 0x00840054 },
{ "DelayBeforeReboot", 0x00840055 },
{ "DelayBeforeStartup", 0x00840056 },
{ "DelayBeforeShutdown", 0x00840057 },
{ "Test", 0x00840058 },
{ "ModuleReset", 0x00840059 },
{ "AudibleAlarmControl", 0x0084005a },
/* 0x0084005b-0x0084005f => Reserved */
{ "Present", 0x00840060 },
{ "Good", 0x00840061 },
{ "InternalFailure", 0x00840062 },
{ "VoltageOutOfRange", 0x00840063 },
{ "FrequencyOutOfRange", 0x00840064 },
{ "Overload", 0x00840065 },
/* Note: the correct spelling is "Overload", not "OverLoad",
* according to the official specification, "Universal Serial
* Bus Usage Tables for HID Power Devices", Release 1.0,
* November 1, 1997 */
{ "OverCharged", 0x00840066 },
{ "OverTemperature", 0x00840067 },
{ "ShutdownRequested", 0x00840068 },
{ "ShutdownImminent", 0x00840069 },
{ "SwitchOn/Off", 0x0084006b },
{ "Switchable", 0x0084006c },
{ "Used", 0x0084006d },
{ "Boost", 0x0084006e },
{ "Buck", 0x0084006f },
{ "Initialized", 0x00840070 },
{ "Tested", 0x00840071 },
{ "AwaitingPower", 0x00840072 },
{ "CommunicationLost", 0x00840073 },
/* 0x00840074-0x008400fc => Reserved */
{ "iManufacturer", 0x008400fd },
{ "iProduct", 0x008400fe },
{ "iSerialNumber", 0x008400ff },
/* Battery System Page */
{ "Undefined", 0x00850000 },
{ "SMBBatteryMode", 0x00850001 },
{ "SMBBatteryStatus", 0x00850002 },
{ "SMBAlarmWarning", 0x00850003 },
{ "SMBChargerMode", 0x00850004 },
{ "SMBChargerStatus", 0x00850005 },
{ "SMBChargerSpecInfo", 0x00850006 },
{ "SMBSelectorState", 0x00850007 },
{ "SMBSelectorPresets", 0x00850008 },
{ "SMBSelectorInfo", 0x00850009 },
/* 0x0085000A-0x0085000f => Reserved */
{ "OptionalMfgFunction1", 0x00850010 },
{ "OptionalMfgFunction2", 0x00850011 },
{ "OptionalMfgFunction3", 0x00850012 },
{ "OptionalMfgFunction4", 0x00850013 },
{ "OptionalMfgFunction5", 0x00850014 },
{ "ConnectionToSMBus", 0x00850015 },
{ "OutputConnection", 0x00850016 },
{ "ChargerConnection", 0x00850017 },
{ "BatteryInsertion", 0x00850018 },
{ "Usenext", 0x00850019 },
{ "OKToUse", 0x0085001a },
{ "BatterySupported", 0x0085001b },
{ "SelectorRevision", 0x0085001c },
{ "ChargingIndicator", 0x0085001d },
/* 0x0085001e-0x00850027 => Reserved */
{ "ManufacturerAccess", 0x00850028 },
{ "RemainingCapacityLimit", 0x00850029 },
{ "RemainingTimeLimit", 0x0085002a },
{ "AtRate", 0x0085002b },
{ "CapacityMode", 0x0085002c },
{ "BroadcastToCharger", 0x0085002d },
{ "PrimaryBattery", 0x0085002e },
{ "ChargeController", 0x0085002f },
/* 0x00850030-0x0085003f => Reserved */
{ "TerminateCharge", 0x00850040 },
{ "TerminateDischarge", 0x00850041 },
{ "BelowRemainingCapacityLimit", 0x00850042 },
{ "RemainingTimeLimitExpired", 0x00850043 },
{ "Charging", 0x00850044 },
{ "Discharging", 0x00850045 },
{ "FullyCharged", 0x00850046 },
{ "FullyDischarged", 0x00850047 },
{ "ConditioningFlag", 0x00850048 },
{ "AtRateOK", 0x00850049 },
{ "SMBErrorCode", 0x0085004a },
{ "NeedReplacement", 0x0085004b },
/* 0x0085004c-0x0085005f => Reserved */
{ "AtRateTimeToFull", 0x00850060 },
{ "AtRateTimeToEmpty", 0x00850061 },
{ "AverageCurrent", 0x00850062 },
{ "Maxerror", 0x00850063 },
{ "RelativeStateOfCharge", 0x00850064 },
{ "AbsoluteStateOfCharge", 0x00850065 },
{ "RemainingCapacity", 0x00850066 },
{ "FullChargeCapacity", 0x00850067 },
{ "RunTimeToEmpty", 0x00850068 },
{ "AverageTimeToEmpty", 0x00850069 },
{ "AverageTimeToFull", 0x0085006a },
{ "CycleCount", 0x0085006b },
/* 0x0085006c-0x0085007f => Reserved */
{ "BattPackModelLevel", 0x00850080 },
{ "InternalChargeController", 0x00850081 },
{ "PrimaryBatterySupport", 0x00850082 },
{ "DesignCapacity", 0x00850083 },
{ "SpecificationInfo", 0x00850084 },
{ "ManufacturerDate", 0x00850085 },
{ "SerialNumber", 0x00850086 },
{ "iManufacturerName", 0x00850087 },
{ "iDevicename", 0x00850088 }, /* sic! */
{ "iDeviceChemistry", 0x00850089 }, /* misspelled as "iDeviceChemistery" in spec. */
{ "ManufacturerData", 0x0085008a },
{ "Rechargeable", 0x0085008b },
{ "WarningCapacityLimit", 0x0085008c },
{ "CapacityGranularity1", 0x0085008d },
{ "CapacityGranularity2", 0x0085008e },
{ "iOEMInformation", 0x0085008f },
/* 0x00850090-0x008500bf => Reserved */
{ "InhibitCharge", 0x008500c0 },
{ "EnablePolling", 0x008500c1 },
{ "ResetToZero", 0x008500c2 },
/* 0x008500c3-0x008500cf => Reserved */
{ "ACPresent", 0x008500d0 },
{ "BatteryPresent", 0x008500d1 },
{ "PowerFail", 0x008500d2 },
{ "AlarmInhibited", 0x008500d3 },
{ "ThermistorUnderRange", 0x008500d4 },
{ "ThermistorHot", 0x008500d5 },
{ "ThermistorCold", 0x008500d6 },
{ "ThermistorOverRange", 0x008500d7 },
{ "VoltageOutOfRange", 0x008500d8 },
{ "CurrentOutOfRange", 0x008500d9 },
{ "CurrentNotRegulated", 0x008500da },
{ "VoltageNotRegulated", 0x008500db },
{ "MasterMode", 0x008500dc },
/* 0x008500dd-0x008500ef => Reserved */
{ "ChargerSelectorSupport", 0x008500f0 },
{ "ChargerSpec", 0x008500f1 },
{ "Level2", 0x008500f2 },
{ "Level3", 0x008500f3 },
/* 0x008500f4-0x008500ff => Reserved */
/* end of structure. */
{ NULL, 0 }
};
|