1 /*
2  * usb.h
3  *
4  * Public USB driver interface exposed to the driver management layer.
5  */
6 
7 /*
8  * Copyright (C) 2001-2004 Kern Sibbald
9  * Copyright (C) 2004-2005 Adam Kropelin
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of version 2 of the GNU General
13  * Public License as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program; if not, write to the Free
22  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23  * MA 02110-1335, USA.
24  */
25 
26 #ifndef _USB_H
27 #define _USB_H
28 
29 #include "usb_common.h"
30 
31 class UsbUpsDriver: public UpsDriver
32 {
33 public:
34    UsbUpsDriver(UPSINFO *ups);
~UsbUpsDriver()35    virtual ~UsbUpsDriver() {}
36 
37    static UpsDriver *Factory(UPSINFO *ups);
38 
39    // UpsDriver functions impemented in UsbUpsDriver base class
40    virtual bool get_capabilities();
41    virtual bool read_volatile_data();
42    virtual bool read_static_data();
43    virtual bool kill_power();
44    virtual bool shutdown();
45    virtual bool entry_point(int command, void *data);
46 
47    // Extra functions exported for use by apctest
48    // Implemented by derived XXXUsbUpsDriver class
49    virtual int write_int_to_ups(int ci, int value, char const* name) = 0;
50    virtual int read_int_from_ups(int ci, int *value) = 0;
51 
52 protected:
53 
54    typedef struct s_usb_value {
55       s_usb_value();                /* Constructor */
56       int value_type;               /* Type of returned value */
57       double dValue;                /* Value if double */
58       int iValue;                   /* Integer value */
59       const char *UnitName;         /* Name of units */
60       char sValue[MAXSTRING];       /* Value if string */
61    } USB_VALUE;
62 
63    // Helper functions implemented in UsbUpsDriver
64    bool usb_get_value(int ci, USB_VALUE *uval);
65    bool usb_process_value_bup(int ci, USB_VALUE* uval);
66    void usb_process_value(int ci, USB_VALUE* uval);
67    bool usb_update_value(int ci);
68    bool usb_report_event(int ci, USB_VALUE *uval);
69    double pow_ten(int exponent);
70 
71    struct s_known_info {
72       int ci;                       /* Command index */
73       unsigned usage_code;          /* Usage code */
74       unsigned physical;            /* Physical usage */
75       unsigned logical;             /* Logical usage */
76       int data_type;                /* Data type expected */
77       bool isvolatile;              /* Volatile data item */
78    };
79 
80    static const s_known_info _known_info[];
81 
82    // Functions implemented in derived XXXUsbUpsDriver class
83    virtual bool pusb_ups_get_capabilities() = 0;
84    virtual bool pusb_get_value(int ci, USB_VALUE *uval) = 0;
85 
86    bool _quirk_old_backups_pro;
87    struct timeval _prev_time;
88    int _bpcnt;
89 };
90 
91 /* Max rate to update volatile data */
92 #define MAX_VOLATILE_POLL_RATE 5
93 
94 /* How often to retry the link (seconds) */
95 #define LINK_RETRY_INTERVAL    5
96 
97 /* These are the data_type expected for our know_info */
98 #define T_NONE     0          /* No units */
99 #define T_INDEX    1          /* String index */
100 #define T_CAPACITY 2          /* Capacity (usually %) */
101 #define T_BITS     3          /* Bit field */
102 #define T_UNITS    4          /* Use units/exponent field */
103 #define T_DATE     5          /* Date */
104 #define T_APCDATE  6          /* APC date */
105 
106 /* These are the resulting value types returned */
107 #define V_DEFAULT  0          /* Unknown type */
108 #define V_DOUBLE   1          /* Double */
109 #define V_STRING   2          /* String pointer */
110 #define V_INTEGER  3          /* Integer */
111 
112 /* These are the desired Physical usage values we want */
113 #define P_ANY     0           /* Any value */
114 #define P_OUTPUT  0x84001c    /* Output values */
115 #define P_BATTERY 0x840012    /* Battery values */
116 #define P_INPUT   0x84001a    /* Input values */
117 #define P_PWSUM   0x840024    /* Power summary */
118 #define P_APC1    0xff860007  /* From AP9612 environmental monitor */
119 
120 /* No Command Index, don't save this value */
121 #define CI_NONE -1
122 
123 /* Check if the UPS has the given capability */
124 #define UPS_HAS_CAP(ci) (_ups->UPS_Cap[ci])
125 
126 #endif  /* _USB_H */
127