forked from denis/a811
116 lines
3.3 KiB
C++
116 lines
3.3 KiB
C++
#include "usb_thingy.h"
|
|
|
|
#include <getopt.h>
|
|
#include <iostream>
|
|
#include <string.h>
|
|
|
|
#define CHK(X) \
|
|
if ((r = (X)) != 0) \
|
|
return r;
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
// Define the long options
|
|
static struct option long_options[] = {
|
|
{"help", no_argument, nullptr, 'h'},
|
|
{"battery-capacity", no_argument, nullptr, 'c'},
|
|
{"battery-status", no_argument, nullptr, 's'},
|
|
{nullptr, 0, nullptr, 0}};
|
|
|
|
/*static struct option long_options[] = {
|
|
{"help", no_argument, nullptr, 'h'},
|
|
{"battery-capacity", no_argument, nullptr, 'c'},
|
|
{"battery-status", required_argument, nullptr, 's'},
|
|
{nullptr, 0, nullptr, 0}
|
|
};*/
|
|
|
|
int option_index = 0;
|
|
int opt;
|
|
bool getBatteryStatus = false;
|
|
bool getBatteryCapacity = false;
|
|
|
|
if (Connection::getInstance().findDevice())
|
|
{
|
|
std::cerr << "ERROR: Device not found!";
|
|
return 1;
|
|
}
|
|
|
|
// Parse command-line arguments
|
|
while ((opt = getopt_long(argc, argv, "hcs:", long_options, &option_index)) != -1)
|
|
{
|
|
switch (opt)
|
|
{
|
|
case 'h':
|
|
std::cout << "Usage: " << argv[0] << " [options]\n"
|
|
<< "Options:\n"
|
|
<< " -h, --help Show this help message\n"
|
|
<< " -c, --battery-capacity Print battery capacity\n"
|
|
<< " -s, --battery-status Print battery status\n";
|
|
return 0;
|
|
case 'c':
|
|
getBatteryCapacity = true;
|
|
break;
|
|
case 's':
|
|
getBatteryStatus = true;
|
|
break;
|
|
case '?': // Handle unknown options
|
|
std::cerr << "Unknown option. Use --help to see the usage.\n";
|
|
return 1;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Display the parsed arguments
|
|
if (getBatteryCapacity)
|
|
{
|
|
unsigned char dat[8];
|
|
memset(dat, 0, 8 * sizeof(unsigned char));
|
|
|
|
unsigned char _data[8] = {0x05, 0x90, 0, 0, 0, 0, 0, 0};
|
|
|
|
ssize_t r;
|
|
CHK(Connection::getInstance().open());
|
|
CHK(Connection::getInstance().setReport(0x0305, (unsigned char *)_data, 8));
|
|
CHK(Connection::getInstance().getReport(0x0305, (unsigned char *)dat, 8));
|
|
CHK(Connection::getInstance().close());
|
|
|
|
if (dat[2] == 0x10 && dat[3] == 0x01)
|
|
return -1;
|
|
|
|
std::cout << (int)dat[3] << "\n";
|
|
return 0;
|
|
}
|
|
if (getBatteryStatus)
|
|
{
|
|
unsigned char dat[8];
|
|
memset(dat, 0, 8 * sizeof(unsigned char));
|
|
|
|
unsigned char _data[8] = {0x05, 0x90, 0, 0, 0, 0, 0, 0};
|
|
|
|
ssize_t r;
|
|
CHK(Connection::getInstance().open());
|
|
CHK(Connection::getInstance().setReport(0x0305, (unsigned char *)_data, 8));
|
|
CHK(Connection::getInstance().getReport(0x0305, (unsigned char *)dat, 8));
|
|
CHK(Connection::getInstance().close());
|
|
|
|
if (dat[2] == 0x10 && dat[3] == 0x01)
|
|
{
|
|
std::cout << "loading"
|
|
<< "\n";
|
|
return 0;
|
|
}
|
|
|
|
std::cout << "discharging"
|
|
<< "\n";
|
|
return 0;
|
|
}
|
|
|
|
// Handle non-option arguments
|
|
for (int i = optind; i < argc; i++)
|
|
{
|
|
std::cout << "Non-option argument: " << argv[i] << "\n";
|
|
}
|
|
|
|
return 0;
|
|
} |