33 #include <sys/ioctl.h>
40 static std::unique_ptr<PegasusUCH> uch(
new PegasusUCH());
72 IUFillSwitchVector(&PowerLEDSP, PowerLEDS, 2,
getDeviceName(),
"POWER_LED",
"LED",
MAIN_CONTROL_TAB,
IP_RW,
ISR_1OFMANY, 60,
78 IUFillSwitchVector(&USBPort1SP, USBPort1S, 2,
getDeviceName(),
"USBPort1",
"", USB_TAB,
IP_RW,
ISR_1OFMANY, 0,
IPS_IDLE);
82 IUFillSwitchVector(&USBPort2SP, USBPort2S, 2,
getDeviceName(),
"USBPort2",
"", USB_TAB,
IP_RW,
ISR_1OFMANY, 0,
IPS_IDLE);
86 IUFillSwitchVector(&USBPort3SP, USBPort3S, 2,
getDeviceName(),
"USBPort3",
"", USB_TAB,
IP_RW,
ISR_1OFMANY, 0,
IPS_IDLE);
90 IUFillSwitchVector(&USBPort4SP, USBPort4S, 2,
getDeviceName(),
"USBPort4",
"", USB_TAB,
IP_RW,
ISR_1OFMANY, 0,
IPS_IDLE);
94 IUFillSwitchVector(&USBPort5SP, USBPort5S, 2,
getDeviceName(),
"USBPort5",
"", USB_TAB,
IP_RW,
ISR_1OFMANY, 0,
IPS_IDLE);
98 IUFillSwitchVector(&USBPort6SP, USBPort6S, 2,
getDeviceName(),
"USBPort6",
"", USB_TAB,
IP_RW,
ISR_1OFMANY, 0,
IPS_IDLE);
105 IUFillText(&InfoT[INFO_VERSION],
"VERSION",
"Version",
"NA");
106 IUFillText(&InfoT[INFO_UPTIME],
"UPTIME",
"Uptime (h)",
"NA");
107 IUFillText(&InfoT[INFO_USBVOLTAGE],
"USBVOLTAGE",
"USB Voltage",
"NA");
193 if (!strcmp(name, RebootSP.
name))
204 if (!strcmp(name, PowerLEDSP.
name))
208 if (setPowerLEDEnabled(PowerLEDS[0].s ==
ISS_ON))
215 PowerLEDS[prevIndex].s =
ISS_ON;
223 if (!strcmp(name, USBPort1SP.
name))
225 return this->setUSBPort(1, USBPort1S, USBPort1SP, states, names, n);
227 else if(!strcmp(name, USBPort2SP.
name))
229 return this->setUSBPort(2, USBPort2S, USBPort2SP, states, names, n);
231 else if(!strcmp(name, USBPort3SP.
name))
233 return this->setUSBPort(3, USBPort3S, USBPort3SP, states, names, n);
235 else if(!strcmp(name, USBPort4SP.
name))
237 return this->setUSBPort(4, USBPort4S, USBPort4SP, states, names, n);
239 else if(!strcmp(name, USBPort5SP.
name))
241 return this->setUSBPort(5, USBPort5S, USBPort5SP, states, names, n);
243 else if(!strcmp(name, USBPort6SP.
name))
245 return this->setUSBPort(6, USBPort6S, USBPort6SP, states, names, n);
260 if (setUSBPortEnabled(port, usbPortS[1].s ==
ISS_ON))
267 usbPortS[prevIndex].s =
ISS_ON;
281 return "Pegasus UCH";
308 this->updateUSBPower();
309 this->updateUpTime();
318 std::vector<std::string> PegasusUCH::split(
const std::string &input,
const std::string ®ex)
321 std::regex re(regex);
322 std::sregex_token_iterator
323 first{input.begin(), input.end(), re, -1},
325 return {first, last};
332 bool PegasusUCH::Handshake()
336 char response[PEGASUS_LEN] = {0};
337 if(sendCommand(
"P#", response))
339 if(strstr(
"UCH_OK", response) !=
nullptr)
341 setFirmwareVersion();
343 this->initialized =
true;
352 this->initialized =
false;
360 bool PegasusUCH::reboot()
362 return sendCommand(
"PF",
nullptr);
369 bool PegasusUCH::setPowerLEDEnabled(
bool enabled)
371 char cmd[PEGASUS_LEN] = {0}, res[PEGASUS_LEN] = {0};
372 snprintf(
cmd, PEGASUS_LEN,
"PL:%d", enabled ? 1 : 0);
373 if (sendCommand(
cmd, res))
375 return (!strcmp(res,
cmd));
385 bool PegasusUCH::setUSBPortEnabled(uint8_t port,
bool enabled)
387 char cmd[PEGASUS_LEN] = {0}, expected[PEGASUS_LEN] = {0}, res[PEGASUS_LEN] = {0};
388 snprintf(
cmd, PEGASUS_LEN,
"U%d:%d", port, enabled ? 1 : 0);
389 snprintf(expected, PEGASUS_LEN,
"U%d:%d", port, enabled ? 1 : 0);
390 if (sendCommand(
cmd, res))
392 return (!strcmp(res, expected));
402 void PegasusUCH::setFirmwareVersion()
404 char response[PEGASUS_LEN] = {0};
406 if(sendCommand(
"PV", response))
423 void PegasusUCH::setBootstrapUSB()
425 char response[PEGASUS_LEN] = {0};
426 if(sendCommand(
"PA", response))
428 std::vector<std::string> result = split(response,
":");
429 auto usbPortStatus = result[2];
432 for(std::string::size_type i = 0; i < usbPortStatus.size(); ++i)
435 bool s = usbPortStatus[i] ==
'1' ? true :
false;
448 void PegasusUCH::updateUSBPower()
450 char response[PEGASUS_LEN] = {0};
452 if(sendCommand(
"PA", response))
454 std::vector<std::string> result = split(response,
":");
457 if(result.size() != 3)
459 LOGF_WARN(
"Received wrong number (%i) of data (%s). Retrying...", result.size(), response);
463 std::string usbBusVoltage = result[1];
466 IUSaveText(&InfoT[INFO_USBVOLTAGE], usbBusVoltage.c_str());
475 void PegasusUCH::updateUpTime()
477 char response[PEGASUS_LEN] = {0};
479 if(sendCommand(
"PC", response))
481 std::vector<std::string> result = split(response,
":");
484 if(result.size() != 2)
486 LOGF_WARN(
"Received wrong number (%i) of data (%s). Retrying...", result.size(), response);
490 std::chrono::milliseconds uptime(std::stol(result[1]));
491 using dhours = std::chrono::duration<double, std::ratio<3600>>;
492 std::stringstream ss;
493 ss << std::fixed << std::setprecision(3) << dhours(uptime).count();
494 IUSaveText(&InfoT[INFO_UPTIME], ss.str().c_str());
502 bool PegasusUCH::sendCommand(
const char * command,
char * res)
504 int nbytes_read = 0, nbytes_written = 0, rc = -1;
509 char cmd[PEGASUS_LEN] = {0};
510 snprintf(
cmd, 10,
"%s\n", command);
514 tcflush(PortFD, TCIOFLUSH);
531 res[nbytes_read - 1] = 0;
533 if(res[nbytes_read - 2] ==
'\r')
534 res[nbytes_read - 2] = 0;
538 tcflush(PortFD, TCIOFLUSH);
547 void PegasusUCH::cleanupResponse(
char *response)
549 std::string s(response);
550 s.erase(std::remove_if(s.begin(), s.end(),
553 return std::isspace(x);
555 strncpy(response, s.c_str(), PEGASUS_LEN);
void registerHandshake(std::function< bool()> callback)
registerHandshake Register a handshake function to be called once the intial connection to the device...
The Serial class manages connection with serial devices including Bluetooth. Serial communication is ...
void setDefaultBaudRate(BaudRate newRate)
setDefaultBaudRate Set default baud rate. The default baud rate is 9600 unless otherwise changed by t...
const char * getDeviceName() const
virtual bool updateProperties()
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
void registerConnection(Connection::Interface *newConnection)
registerConnection Add new connection plugin to the existing connection pool. The connection type sha...
void setVersion(uint16_t vMajor, uint16_t vMinor)
Set driver version information to be defined in DRIVER_INFO property as vMajor.vMinor.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
virtual bool saveConfigItems(FILE *fp)
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
uint32_t getCurrentPollingPeriod() const
getCurrentPollingPeriod Return the current polling period.
virtual bool initProperties()
Initilize properties initial state and value. The child class must implement this function.
void addAuxControls()
Add Debug, Simulation, and Configuration options to the driver.
void setDriverInterface(uint16_t value)
setInterface Set driver interface. By default the driver interface is set to GENERAL_DEVICE....
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
virtual void TimerHit() override
Callback function to be called once SetTimer duration elapses.
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
const char * getDefaultName() override
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
void ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Update the value of an existing switch vector property.
int tty_read_section(int fd, char *buf, char stop_char, int timeout, int *nbytes_read)
read buffer from terminal with a delimiter
int tty_write(int fd, const char *buf, int nbytes, int *nbytes_written)
Writes a buffer to fd.
void tty_error_msg(int err_code, char *err_msg, int err_msg_len)
Retrieve the tty error message.
Implementations for common driver routines.
void IUSaveConfigSwitch(FILE *fp, const ISwitchVectorProperty *svp)
Add a switch vector property value to the configuration file.
int IUFindOnSwitchIndex(const ISwitchVectorProperty *svp)
Returns the index of first ON switch it finds in the vector switch property.
void IUResetSwitch(ISwitchVectorProperty *svp)
Reset all switches in a switch vector property to OFF.
void IUFillTextVector(ITextVectorProperty *tvp, IText *tp, int ntp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a text vector property. The vector's auxiliary elements will be set to NULL.
void IUSaveText(IText *tp, const char *newtext)
Function to reliably save new text in a IText.
void IUFillSwitch(ISwitch *sp, const char *name, const char *label, ISState s)
Assign attributes for a switch property. The switch's auxiliary elements will be set to NULL.
void IUFillText(IText *tp, const char *name, const char *label, const char *initialText)
Assign attributes for a text property. The text's auxiliary elements will be set to NULL.
void IUFillSwitchVector(ISwitchVectorProperty *svp, ISwitch *sp, int nsp, const char *dev, const char *name, const char *label, const char *group, IPerm p, ISRule r, double timeout, IPState s)
Assign attributes for a switch vector property. The vector's auxiliary elements will be set to NULL.
int IUUpdateSwitch(ISwitchVectorProperty *svp, ISState *states, char *names[], int n)
Update all switches in a switch vector property.
void IDSetSwitch(const ISwitchVectorProperty *svp, const char *fmt,...)
void IDSetText(const ITextVectorProperty *tvp, const char *fmt,...)
#define LOGF_WARN(fmt,...)
#define LOGF_DEBUG(fmt,...)
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
#define LOGF_ERROR(fmt,...)
Switch vector property descriptor.