39 static std::unique_ptr<SQM> sqm(
new SQM());
41 #define UNIT_TAB "Unit"
53 IUFillNumber(&AverageReadingN[SKY_BRIGHTNESS],
"SKY_BRIGHTNESS",
"Quality (mag/arcsec^2)",
"%6.2f", -20, 30, 0, 0);
54 IUFillNumber(&AverageReadingN[SENSOR_FREQUENCY],
"SENSOR_FREQUENCY",
"Freq (Hz)",
"%6.2f", 0, 1000000, 0, 0);
55 IUFillNumber(&AverageReadingN[SENSOR_COUNTS],
"SENSOR_COUNTS",
"Period (counts)",
"%6.2f", 0, 1000000, 0, 0);
56 IUFillNumber(&AverageReadingN[SENSOR_PERIOD],
"SENSOR_PERIOD",
"Period (s)",
"%6.2f", 0, 1000000, 0, 0);
57 IUFillNumber(&AverageReadingN[SKY_TEMPERATURE],
"SKY_TEMPERATURE",
"Temperature (C)",
"%6.2f", -50, 80, 0, 0);
62 IUFillNumber(&UnitInfoN[UNIT_PROTOCOL],
"UNIT_PROTOCOL",
"Protocol",
"%.f", 0, 1000000, 0, 0);
63 IUFillNumber(&UnitInfoN[UNIT_MODEL],
"UNIT_MODEL",
"Model",
"%.f", 0, 1000000, 0, 0);
64 IUFillNumber(&UnitInfoN[UNIT_FEATURE],
"UNIT_FEATURE",
"Feature",
"%.f", 0, 1000000, 0, 0);
65 IUFillNumber(&UnitInfoN[UNIT_SERIAL],
"UNIT_SERIAL",
"Serial",
"%.f", 0, 1000000, 0, 0);
73 return getDeviceInfo();
86 return getDeviceInfo();
119 bool SQM::ISNewNumber(
const char *dev,
const char *name,
double values[],
char *names[],
int n)
124 if (!strcmp(name,
"POLLING_PERIOD"))
126 uint32_t seconds = values[0] / 1000;
128 LOGF_WARN(
"Make sure SQM web timeout is configured for %ld seconds or more. Otherwise SQM will disconnect prematurely.",
136 bool SQM::getReadings()
138 char res[DRIVER_LEN] = {0};
140 if (!sendCommand(
"rx", res))
143 float mpsas, period_seconds, temperature;
144 int frequency, period_counts;
145 int rc = sscanf(res,
"r,%fm,%dHz,%dc,%fs,%fC", &mpsas, &frequency, &period_counts, &period_seconds, &temperature);
149 rc = sscanf(res,
"r,%fm,%dHz,%dc,%fs,%fC,%*d", &mpsas, &frequency, &period_counts, &period_seconds, &temperature);
157 AverageReadingN[0].value = mpsas;
158 AverageReadingN[1].value = frequency;
159 AverageReadingN[2].value = period_counts;
160 AverageReadingN[3].value = period_seconds;
161 AverageReadingN[4].value = temperature;
171 bool SQM::getDeviceInfo()
182 char res[DRIVER_LEN] = {0};
183 for (
int i = 0; i < 3; i++)
185 if (!sendCommand(
"ix", res))
195 LOGF_ERROR(
"Error getting device info while reading response: %s", strerror(
errno));
199 int protocol, model, feature, serial;
200 int rc = sscanf(res,
"i,%d,%d,%d,%d", &protocol, &model, &feature, &serial);
208 UnitInfoN[0].value = protocol;
209 UnitInfoN[1].value = model;
210 UnitInfoN[2].value = feature;
211 UnitInfoN[3].value = serial;
221 bool rc = getReadings();
232 bool SQM::sendCommand(
const char *
cmd,
char * res,
int cmd_len,
int res_len)
234 int nbytes_written = 0, nbytes_read = 0, rc = -1;
236 tcflush(PortFD, TCIOFLUSH);
240 char hex_cmd[DRIVER_LEN * 3] = {0};
241 hexDump(hex_cmd,
cmd, cmd_len);
255 LOGF_ERROR(
"Serial write error: %s.", errstr);
263 rc =
tty_read(PortFD, res, res_len, DRIVER_TIMEOUT, &nbytes_read);
265 rc =
tty_nread_section(PortFD, res, DRIVER_LEN, DRIVER_STOP_CHAR, DRIVER_TIMEOUT, &nbytes_read);
277 char hex_res[DRIVER_LEN * 3] = {0};
278 hexDump(hex_res, res, res_len);
284 res[nbytes_read - 2] = 0;
288 tcflush(PortFD, TCIOFLUSH);
296 void SQM::hexDump(
char * buf,
const char * data,
int size)
298 for (
int i = 0; i < size; i++)
299 sprintf(buf + 3 * i,
"%02X ",
static_cast<uint8_t
>(data[i]));
302 buf[3 * size - 1] =
'\0';
308 std::vector<std::string> SQM::split(
const std::string &input,
const std::string ®ex)
311 std::regex re(regex);
312 std::sregex_token_iterator
313 first{input.begin(), input.end(), re, -1},
315 return {first, last};
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...
The TCP class manages connection with devices over the network via TCP/IP. Upon successfull connectio...
void setDefaultHost(const char *addressHost)
void setDefaultPort(uint32_t addressPort)
const char * getDeviceName() const
void addPollPeriodControl()
Add Polling period control to the driver.
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)
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.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
Process the client newNumber command.
Connection::Interface * getActiveConnection()
int SetTimer(uint32_t ms)
Set a timer to call the function TimerHit after ms milliseconds.
void addDebugControl()
Add Debug control to the driver.
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
const char * getDefaultName() override
void TimerHit() override
Callback function to be called once SetTimer duration elapses.
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.
int tty_write(int fd, const char *buf, int nbytes, int *nbytes_written)
Writes a buffer to fd.
int tty_read(int fd, char *buf, int nbytes, int timeout, int *nbytes_read)
read buffer from terminal
int tty_write_string(int fd, const char *buf, int *nbytes_written)
Writes a null terminated string to fd.
void tty_error_msg(int err_code, char *err_msg, int err_msg_len)
Retrieve the tty error message.
int tty_nread_section(int fd, char *buf, int nsize, char stop_char, int timeout, int *nbytes_read)
read buffer from terminal with a delimiter
Implementations for common driver routines.
void IUFillNumberVector(INumberVectorProperty *nvp, INumber *np, int nnp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a number vector property. The vector's auxiliary elements will be set to NULL.
void IUFillNumber(INumber *np, const char *name, const char *label, const char *format, double min, double max, double step, double value)
Assign attributes for a number property. The number's auxiliary elements will be set to NULL.
void IDSetNumber(const INumberVectorProperty *nvp, const char *fmt,...)
#define LOGF_WARN(fmt,...)
#define LOGF_DEBUG(fmt,...)
#define LOGF_ERROR(fmt,...)