fixed some errors, didn't check yet

This commit is contained in:
Edward Emelianov
2026-10-13 23:38:39 +03:00
parent 5785cf2fad
commit 6916e0bcd4
9 changed files with 238 additions and 64 deletions

View File

@@ -24,7 +24,7 @@
#include "angles.h"
static placeData_t place = {.slong = 0.7232763200, .slat = 0.7618977414, .salt = 2070.};
static placeData_t place = {.slong = -0.7232763200, .slat = 0.7618977414, .salt = 2070.};
static almDut_t AlmDut = {0};
// set/get polar/DUT1 data
@@ -189,9 +189,9 @@ void norm_RADECr(double *ra, double *dec){
// normalize angle to (-180, 180]
void norm_angle180(double *a){
if(!a) return;
double d = fmod(*a + 180.0, 360.0);
if(d < 0.) d += 360.0;
else d -= 180;
double d = fmod(*a, 360.0);
if(d < -180.) d += 360.0;
else if(d > 180.) d -= 360.;
*a = d;
}
@@ -381,6 +381,7 @@ bool get_LST(sMJD_t *mjd, double *LST){
return true;
}
#if 0
/**
* @brief get_ObsPlace - calculate observed place (without PM etc) for given date @550nm
* @param tval (i) - time
@@ -436,6 +437,99 @@ bool get_ObsPlace(struct timeval *tval, polarCrds_t *p2000, polarCrds_t *pnow, h
}
return true;
}
#endif
// used in two functions
static bool J2000to_ep_(const polarCrds_t *j2000, polarCrds_t *jepoch, double date1, double date2){
if(!j2000 || !jepoch) return false;
double r_j2000[3], r_ep[3], rmat[3][3];
// spherical coordinates to vector
eraS2c(j2000->ra, j2000->dec, r_j2000);
// rmat - prec/nut matrix from J2000 to given epoch
eraPnm06a(date1, date2, rmat);
// r_ep = rmat * r_j2000
eraRxp(rmat, r_j2000, r_ep);
// vector to coordinates
eraC2s(r_ep, &jepoch->ra, &jepoch->dec);
// normalize angle
jepoch->ra = eraAnp(jepoch->ra);
return true;
}
static bool ep_toJ2000_(const polarCrds_t *jepoch, polarCrds_t *j2000, double date1, double date2){
if(!j2000 || !jepoch) return false;
double r_ep[3], r_j2000[3], rmat[3][3];
eraS2c(jepoch->ra, jepoch->dec, r_ep);
eraPnm06a(date1, date2, rmat);
// use eraTrxp for multiplication by transposed matrix (reverse transform)
eraTrxp(rmat, r_ep, r_j2000);
eraC2s(r_j2000, &j2000->ra, &j2000->dec);
j2000->ra = eraAnp(j2000->ra);
return true;
}
/**
* @brief J2000_to_epoch_pn - convert J2000 to given epoch (prec/nut)
* @param j2000 (i) - catalog coordinates
* @param epoch - epoch (like 2050.)
* @param jepoch (o) - coordinates for epoch
*/
bool J2000_to_epoch_pn(const polarCrds_t *j2000, double epoch, polarCrds_t *jepoch){
double date1, date2;
// calculate JD for target epoch
eraEpj2jd(epoch, &date1, &date2);
return J2000to_ep_(j2000, jepoch, date1, date2);
}
/**
* @brief epoch_to_J2000_pn - convert coordinates for given epoch to J2000
* @param jepoch (i) - coordinates for epoch
* @param epoch (o) - epoch (like 2050.)
*/
bool epoch_to_J2000_pn(const polarCrds_t *jepoch, double epoch, polarCrds_t *j2000){
double date1, date2;
eraEpj2jd(epoch, &date1, &date2);
return ep_toJ2000_(jepoch, j2000, date1, date2);
}
/**
* @brief JnowtoJ2000 - convert coordinates from Jnow to J2000
* @param in - current coordinates
* @param out - catalog coordinates
* @return false if arguments are wrong
*/
bool JnowtoJ2000(const polarCrds_t *in, polarCrds_t *out){
sMJD_t MJD;
// calculate current MJD
if(!get_MJDt(NULL, &MJD)) return false;
return ep_toJ2000_(in, out, MJD.tt1, MJD.tt2);
}
/**
* @brief JXtoJnow - convert coordinates from any epoch to Jnow
* @param in - epoch coordinates
* @param out - current coordinates
* @param Jx - given epoch like 2050.
* @return false if arguments are wrong
*/
bool JXtoJnow(const polarCrds_t *in, polarCrds_t *out, double Jx){
sMJD_t MJD;
polarCrds_t j2000;
// calculate current MJD
if(!get_MJDt(NULL, &MJD)) return false;
// convert Jx to J2000
if(!epoch_to_J2000_pn(in, Jx, &j2000)) return false;
// and convert J2000 to Jnow
return J2000to_ep_(&j2000, out, MJD.tt1, MJD.tt2);
}
#if 0
/**
* convert geocentric coordinates (nowadays, CIRS) to mean (JD2000, ICRS)
@@ -489,3 +583,4 @@ bool JXtoJnow(const polarCrds_t *in, polarCrds_t *out, double Jx){
return true;
}
#endif

View File

@@ -83,7 +83,9 @@ void d2sDMS(double degrees, char *dms, int len);
bool get_MJDt(struct timeval *tval, sMJD_t *MJD);
bool get_LST(sMJD_t *mjd, double *LST);
#if 0
bool get_ObsPlace(struct timeval *tval, polarCrds_t *p2000, polarCrds_t *pnow, horizCrds_t *hnow);
#endif
bool setDUT(almDut_t *D);
void getDUT(almDut_t *D);
@@ -91,5 +93,8 @@ void getDUT(almDut_t *D);
bool setPlaceData(placeData_t *pd);
void getPlaceData(placeData_t *pd);
bool J2000_to_epoch_pn(const polarCrds_t *j2000, double epoch, polarCrds_t *jepoch);
bool epoch_to_J2000_pn(const polarCrds_t *jepoch, double epoch, polarCrds_t *j2000);
bool JnowtoJ2000(const polarCrds_t *in, polarCrds_t *out);
bool JXtoJnow(const polarCrds_t *in, polarCrds_t *out, double Jx);

View File

@@ -164,7 +164,7 @@ void wrhdr(fitsheader_t *HDR){
}
WRHDR("TELSTAT", mount_status_str(HDR->status), "Telescope mount status");
double mjd;
mount_getInpMJD(&mjd);
mount_getInpEpoch(&mjd);
if(mjd > 0.) snprintf(val, 22, "%.10f", 2000.+(mjd - ERFA_DJM00)/365.25); // calculate EPOCH/EQUINOX
else snprintf(val, 22, "%.10f", 2000.+(HDR->MJD.MJD - ERFA_DJM00)/365.25);
WRHDR("INPEQUIN", val, "Equinox of input celestial coordinate system");

View File

@@ -68,11 +68,6 @@ int main(int argc, char **argv){
WARNX("Can't set sleep time to %d", G->sleept);
return 3;
}
if(G->emulation) set_emulation_mode();
else if(!mount_set_dev(G->device, G->serspeed, G->sertmout)){
WARNX("Can't open device %s @ %d", G->device, G->serspeed);
return 4;
}
if(!mount_set_name(G->mountname)){
WARNX("Can't set mount name to %s", G->mountname);
return 5;
@@ -134,6 +129,13 @@ int main(int argc, char **argv){
}
if(!isrunning) return 0;
#endif
if(G->emulation) set_emulation_mode();
else if(!mount_set_dev(G->device, G->serspeed, G->sertmout)){
WARNX("Can't open device %s @ %d", G->device, G->serspeed);
return 4;
}
server_sock_t sockt = {
.cmd_isunix = G->isunix,
.cmdnode = G->cmdnode,

View File

@@ -52,10 +52,16 @@ static horizCrds_t ParkCoords = {.az = DEG2RAD(1.8105556), .zd = DEG2RAD(88.7752
static polarCrds_t InpCoords = {0}, // input as user give (for epoch InpMJD)
TagCoords = {0}; // target for Jnow after command "point to input"
static horizCrds_t InpHoriz = {0};
// input MJD (Modified Julian Date: started from ERFA_DJM0==2400000.5)
static double InpMJD = ERFA_DJM00; // J2000
// times of coords/mjd change
static double InpCTime = 0., TagTime = 0., InpHTime = 0., InpMTime = 0.;
// epoch for input coordinates
static double InpEpoch = 2000.; // J2000
// times of coords/epoch change
static double InpCTime = 0., TagTime = 0., InpHTime = 0., InpETime = 0.;
// safe strncpy
static void scpy(size_t len, char *dest, const char *src){
size_t l = snprintf(dest, len, "%s", src);
if(l >= len) dest[len-1] = 0;
}
// return time of modification
double mount_getInpCoords(polarCrds_t *c){
@@ -66,9 +72,9 @@ double mount_getTagCoords(polarCrds_t *c){
if(c) *c = TagCoords;
return TagTime;
}
double mount_getInpMJD(double *MJD){
if(MJD) *MJD = InpMJD;
return InpMTime;
double mount_getInpEpoch(double *epoch){
if(epoch) *epoch = InpEpoch;
return InpETime;
}
double mount_getInpHor(horizCrds_t *c){
if(c) *c = InpHoriz;
@@ -133,9 +139,9 @@ bool mount_setInpZ(double Z){
return true;
}
// without checking
bool mount_setInpMJD(double m){
InpMJD = m;
InpMTime = sl_dtime();
bool mount_setInpEpoch(double epoch){
InpEpoch = epoch;
InpETime = sl_dtime();
return true;
}
@@ -169,6 +175,7 @@ void mount_get_name(char *nm, size_t l){
* @return false if failed to open device `dev`
*/
bool mount_set_dev(char *dev, int speed, double timeout){
if(isemulated) return true;
pthread_mutex_lock(&mntdev_mutex);
if(mount_dev) sl_tty_close(&mount_dev);
mount_dev = sl_tty_new(dev, speed, 4096);
@@ -288,6 +295,7 @@ static bool write_cmd(const char *cmd, bool haveanswer){
// return current mount status
mount_status_t mount_status(){
if(isemulated) return emulation_status();
mount_status_t curst = (mount_status_t)atomic_load(&mountstatus);
if(curst == MNT_S_OFF) return curst; // do nothing when mount if off
if(!mount_dev){
@@ -588,7 +596,7 @@ bool mount_tracking_start(){
* @return false if failed to run command
*/
bool mount_park(){
if(isemulated) return pointAZ_emulation(ParkCoords.az, ParkCoords.zd);
if(isemulated) return pointAZ_emulation(RAD2DEG(ParkCoords.az), RAD2DEG(ParkCoords.zd));
char buf[64];
snprintf(buf, 63, CMD_SETMINALT, 0);
if(!write_cmd(buf, true)) return false;
@@ -633,6 +641,10 @@ void mount_getPark(horizCrds_t *c){
* @return false if failed
*/
bool mount_getpierside(char *Ps, size_t len){
if(isemulated){
scpy(len, Ps, "Emulation");
return true;
}
pthread_mutex_lock(&mntdev_mutex);
bool ret = send_cmd_resp(CMD_GETPS, Ps, len);
pthread_mutex_unlock(&mntdev_mutex);
@@ -645,6 +657,7 @@ bool mount_getpierside(char *Ps, size_t len){
* @return false if failed to connect
*/
bool mount_corrdata(weather_data_t *w){
if(isemulated) return true;
if(!w) return false;
bool ret = true; // returning value
mount_status_t curst = mount_status();
@@ -687,6 +700,7 @@ bool mount_corrdata(weather_data_t *w){
* @return false if failed
*/
bool mount_shutdown(){
if(isemulated) return false;
return write_cmd(CMD_SHUTDOWN, true);
}
@@ -698,5 +712,6 @@ bool mount_shutdown(){
* @return false if failed
*/
bool mount_rawcmd(const char *cmd, char *ans, size_t anslen){
if(isemulated) return false;
return send_cmd_resp(cmd, ans, anslen);
}

View File

@@ -54,11 +54,11 @@ bool mount_setInpRA(double ra);
bool mount_setInpDec(double dec);
bool mount_setInpA(double A);
bool mount_setInpZ(double Z);
bool mount_setInpMJD(double m);
bool mount_setInpEpoch(double epoch);
double mount_getInpCoords(polarCrds_t *c);
double mount_getTagCoords(polarCrds_t *c);
double mount_getInpMJD(double *MJD);
double mount_getInpEpoch(double *epoch);
double mount_getInpHor(horizCrds_t *c);

View File

@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 20.0.1, 2026-09-29T09:41:26. -->
<!-- Written by QtCreator 20.0.1, 2026-10-13T23:29:46. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
<value type="QByteArray">{cf63021e-ef53-49b0-b03b-2f2570cdf3b6}</value>
<value type="QByteArray">{7bd84e39-ca37-46d3-be9d-99ebea85bc0d}</value>
</data>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
@@ -30,13 +30,11 @@
<value type="QByteArray" key="EditorConfiguration.Codec">KOI8-R</value>
<value type="int" key="EditorConfiguration.IndentSize">4</value>
<value type="int" key="EditorConfiguration.PaddingMode">1</value>
<value type="bool" key="EditorConfiguration.ScrollWheelZooming">false</value>
<value type="int" key="EditorConfiguration.SmartBackspaceBehavior">1</value>
<value type="bool" key="EditorConfiguration.SpacesForTabs">true</value>
<value type="int" key="EditorConfiguration.TabSize">8</value>
<value type="bool" key="EditorConfiguration.UseGlobal">true</value>
<value type="bool" key="EditorConfiguration.addFinalNewLine">true</value>
<value type="bool" key="EditorConfiguration.inEntireDocument">true</value>
<value type="bool" key="EditorConfiguration.cleanIndentation">false</value>
</valuemap>
</data>
<data>
@@ -56,7 +54,6 @@
<value type="int" key="AutoTest.RunAfterBuild">0</value>
<value type="bool" key="AutoTest.UseGlobal">true</value>
<valuemap type="QVariantMap" key="ClangTools">
<value type="Utils::Id" key="ClangTools.DiagnosticConfig"></value>
<valuelist type="QVariantList" key="ClangTools.SelectedDirs"/>
<valuelist type="QVariantList" key="ClangTools.SelectedFiles"/>
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
@@ -72,12 +69,12 @@
<value type="bool" key="HasPerBcDcs">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{91347f2c-5221-46a7-80b1-0a054ca02f79}</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{65a14f9e-e008-4c1b-89df-4eaa4774b6e3}</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/home/eddy/Docs/SAO/10micron/C-sources/mountdaemon_10micron</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/tmp/r1/10micron_stellarium</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets">
@@ -109,7 +106,7 @@
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.CustomParsers"/>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ParseStandardOutput">false</value>
<value type="UnknownType" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">По умолчанию</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Default</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericBuildConfiguration</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
@@ -145,8 +142,26 @@
<value type="QString" key="ProjectExplorer.RunConfiguration.UniqueId"></value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">%{RunConfig:Executable:Path}</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.1">
<value type="bool" key="Analyzer.Perf.Settings.UseGlobalSettings">true</value>
<value type="bool" key="Analyzer.QmlProfiler.Settings.UseGlobalSettings">true</value>
<value type="int" key="Analyzer.Valgrind.Callgrind.CostFormat">0</value>
<value type="bool" key="Analyzer.Valgrind.Settings.UseGlobalSettings">true</value>
<valuelist type="QVariantList" key="Analyzer.Valgrind.SuppressionFiles"/>
<value type="QList&lt;int&gt;" key="Analyzer.Valgrind.VisibleErrorKinds"></value>
<valuelist type="QVariantList" key="CustomOutputParsers"/>
<value type="QString" key="PerfRecordArgsId">-e cpu-cycles --call-graph dwarf,4096 -F 250</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">RemoteDebugger.RunConfig</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="bool" key="ProjectExplorer.RunConfiguration.Customized">false</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.UniqueId"></value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">2</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.BuildConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0">
@@ -181,8 +196,26 @@
<value type="QString" key="ProjectExplorer.RunConfiguration.UniqueId"></value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">%{RunConfig:Executable:Path}</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.1">
<value type="bool" key="Analyzer.Perf.Settings.UseGlobalSettings">true</value>
<value type="bool" key="Analyzer.QmlProfiler.Settings.UseGlobalSettings">true</value>
<value type="int" key="Analyzer.Valgrind.Callgrind.CostFormat">0</value>
<value type="bool" key="Analyzer.Valgrind.Settings.UseGlobalSettings">true</value>
<valuelist type="QVariantList" key="Analyzer.Valgrind.SuppressionFiles"/>
<value type="QList&lt;int&gt;" key="Analyzer.Valgrind.VisibleErrorKinds"></value>
<valuelist type="QVariantList" key="CustomOutputParsers"/>
<value type="QString" key="PerfRecordArgsId">-e cpu-cycles --call-graph dwarf,4096 -F 250</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">RemoteDebugger.RunConfig</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="bool" key="ProjectExplorer.RunConfiguration.Customized">false</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.UniqueId"></value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">2</value>
</valuemap>
</data>
<data>

View File

@@ -53,8 +53,8 @@
#define CMD_STOPTRK "stoptrk"
#define CMD_TAGAZ "tagaz"
#define CMD_TAGDEC "tagdec"
#define CMD_TAGEPOCH "tagepoch"
#define CMD_TAGHA "tagha"
#define CMD_TAGMJD "tagmjd"
#define CMD_TAGRA "tagra"
#define CMD_TAGZD "tagzd"
#define CMD_TELAZ "telaz"
@@ -265,7 +265,7 @@ static sl_sock_hresult_e cmd_gotord(_U_ sl_sock_t *c, _U_ sl_sock_hitem_t *item,
polarCrds_t px, pnow;
mount_getInpCoords(&px);
double Epoch;
mount_getInpMJD(&Epoch);
mount_getInpEpoch(&Epoch);
if(Epoch > 0.){
if(!JXtoJnow(&px, &pnow, Epoch)) return RESULT_FAIL;
}else pnow = px;
@@ -363,24 +363,18 @@ static sl_sock_hresult_e cmd_parkzd(sl_sock_t *c, sl_sock_hitem_t *item, const c
return RESULT_SILENCE;
}
static sl_sock_hresult_e cmd_tagmjd(sl_sock_t *c, sl_sock_hitem_t *item, const char *req){
static sl_sock_hresult_e cmd_tagepoch(sl_sock_t *c, sl_sock_hitem_t *item, const char *req){
double val;
bool isyear = false;
if(req && *req == 'J'){ // e.g. J2050.
++req;
isyear = true;
}
int res = parse_key_value(req, &val);
if(res < 0) return RESULT_BADVAL;
if(res > 0){
if(isyear) val = (val - 2000.)*365.25 + ERFA_DJM00;
LOGDBG("Client %d set target epoch %g", c->fd, val);
if(mount_setInpMJD(val)) return RESULT_OK;
if(res > 0){ // setter
LOGDBG("Client %d set target epoch %g", res);
if(mount_setInpEpoch(val)) return RESULT_OK;
return RESULT_BADVAL;
}
mount_getInpMJD(&val);
mount_getInpEpoch(&val);
char buf[64];
snprintf(buf, 63, "%s=%.1f\n", item->key, val);
snprintf(buf, 63, "%s=%.6f\n", item->key, val);
sl_sock_sendstrmessage(c, buf);
return RESULT_SILENCE;
}
@@ -511,7 +505,7 @@ static sl_sock_hitem_t handlers[] = {
{cmd_tagaz, CMD_TAGAZ, "get/set target azimuth (Deg)", NULL},
{cmd_tagdec, CMD_TAGDEC, "get/set target declination (Deg)", NULL},
{cmd_tagha, CMD_TAGHA, "get/set target hour angle (Hrs)", NULL},
{cmd_tagmjd, CMD_TAGMJD, "get/set target MJD epoch (-1 for Jnow) or Jxxx for year, like J2050.", NULL},
{cmd_tagepoch, CMD_TAGEPOCH, "get/set target epoch (-1 for Jnow), like 2050.", NULL},
{cmd_tagra, CMD_TAGRA, "get/set target right acsention (Hrs)", NULL},
{cmd_tagzd, CMD_TAGZD, "get/set target zenith distance (Deg)", NULL},
{cmd_teldec, CMD_TELDEC, "get current telescope declination (Deg)", NULL},

View File

@@ -35,6 +35,8 @@
// running flag
extern volatile bool isrunning;
static pthread_t mainthread;
static int newsockfd = -1; // new socket fd for handler thread
static pthread_mutex_t sockmutex = PTHREAD_MUTEX_INITIALIZER; // mutex for copying previous variable
//read: 0x14 0x0 0x0 0x0 0x5b 0x5a 0x2e 0xc6 0x8c 0x23 0x5 0x0 0x23 0x9 0xe5 0xaf 0x23 0x2e 0x34 0xed
// command: goto 16h29 24.45 -26d25 55.62
@@ -129,7 +131,7 @@ static bool proc_data(uint8_t *data, ssize_t len){
double tagRA = RA2HRS(ra), tagDec = DEC2DEG(dec);
DBG("RA: %u (%g hrs), DEC: %d (%g degr)", ra, tagRA, dec, tagDec);
LOGMSG("(stellarium) RA: %u (%g hrs), DEC: %d (%g degr)", ra, tagRA, dec, tagDec);
return (mount_setInpRA(tagRA) && mount_setInpDec(tagDec) && mount_setInpMJD(ERFA_DJM00));
return (mount_setInpRA(tagRA) && mount_setInpDec(tagDec) && mount_setInpEpoch(ERFA_DJM00));
}
/**
@@ -151,14 +153,35 @@ static bool send_data(uint8_t *data, size_t dlen, int sockfd){
return true;
}
// read exactly dlen bytes of data with timeout 1s
static ssize_t get_data(int sockfd, uint8_t *data, size_t dlen){
double t0 = sl_dtime();
uint8_t *ptr = data;
ssize_t got = 0;
while(sl_dtime() - t0 < 1. && dlen){
if(!sl_canread(sockfd)) continue;
ssize_t rd = read(sockfd, ptr, dlen);
if(rd < 0) return -1;
else if(rd == 0) break;
dlen -= rd;
ptr += rd;
got += rd;
}
return got;
}
/**
* main socket service procedure
*/
static void *handle_socket(void *sockd){
FNAME();
if(!isrunning) return NULL;
if(!isrunning){
pthread_mutex_unlock(&sockmutex);
return NULL;
}
outdata dout;
int sock = *(int*)sockd;
pthread_mutex_unlock(&sockmutex); // copy done
dout.len = htole16(sizeof(outdata));
dout.type = 0;
while(isrunning){
@@ -174,8 +197,8 @@ static void *handle_socket(void *sockd){
dout.ra = htole32(RAD2RA(J2000.ra));
dout.dec = (int32_t)htole32(RAD2DEC(J2000.dec));
*/
dout.ra = htole32(RAD2RA(HRS2RAD(HDR.polar.ra)));
dout.dec = (int32_t)htole32(RAD2DEC(DEG2RAD(HDR.polar.dec)));
dout.ra = htole32(RAD2RA(HDR.polar.ra));
dout.dec = (int32_t)htole32(RAD2DEC(HDR.polar.dec));
/**/
if(!send_data((uint8_t*)&dout, sizeof(outdata), sock)) break; // send data even in `off` state
if(!sl_canread(sock)){
@@ -184,7 +207,7 @@ static void *handle_socket(void *sockd){
}
// fill incoming buffer
uint8_t buff[BUFLEN];
ssize_t rd = read(sock, buff, BUFLEN-1);
ssize_t rd = get_data(sock, buff, sizeof(indata));
if(rd <= 0){ // error or disconnect
DBG("Nothing to read from fd %d (ret: %zd)", sock, rd);
WARNX("Client disconnected?");
@@ -192,7 +215,7 @@ static void *handle_socket(void *sockd){
}
buff[rd] = 0;
DBG("read %zd (%s)", rd, buff);
if(!proc_data(buff, rd)) dout.status = -1;
if(!proc_data(buff, rd)) dout.status = htole32(-1);
else{
DBG("sent OK");
dout.status = 0;
@@ -218,10 +241,9 @@ static void* start(void *F){
while(isrunning){
socklen_t size = sizeof(struct sockaddr_in);
struct sockaddr_in myaddr;
int newsock;
DBG("ACCEPT");
newsock = accept(sockfd, (struct sockaddr*)&myaddr, &size);
if(newsock <= 0){
newsockfd = accept(sockfd, (struct sockaddr*)&myaddr, &size);
if(newsockfd <= 0){
if(errno == EAGAIN){
usleep(1000);
continue; // nothing available
@@ -233,14 +255,14 @@ static void* start(void *F){
}
struct sockaddr_in peer;
socklen_t peer_len = sizeof(peer);
if(getpeername(newsock, (struct sockaddr*)&peer, &peer_len) == -1){
if(getpeername(newsockfd, (struct sockaddr*)&peer, &peer_len) == -1){
LOGWARN("Stellarium socket error in getpeername()");
WARN("getpeername()");
close(newsock);
close(newsockfd);
continue;
}
int sockport = -1;
if(getsockname(newsock, (struct sockaddr*)&peer, &peer_len) == 0){
if(getsockname(newsockfd, (struct sockaddr*)&peer, &peer_len) == 0){
sockport = ntohs(peer.sin_port);
}
char *peerIP = inet_ntoa(peer.sin_addr);
@@ -252,21 +274,29 @@ static void* start(void *F){
continue;
}*/
pthread_t rthrd;
if(pthread_create(&rthrd, NULL, handle_socket, (void*)&newsock)){
pthread_mutex_lock(&sockmutex);
if(pthread_create(&rthrd, NULL, handle_socket, (void*)&newsockfd)){
WARN("Can't create socket thread");
LOGERR("Stellarium socket: error creating listen thread");
}else{
DBG("Thread created, detouch");
pthread_detach(rthrd); // don't care about thread state
// mutex unlocking will be in thread, but care for timeout
double t0 = sl_dtime();
while(sl_dtime() - t0 < 3.){
if(0 == pthread_mutex_trylock(&sockmutex)){ // released
break;
}
}
}
pthread_mutex_unlock(&sockmutex);
}
close(sockfd);
return NULL;
}
bool stellarium_start(int sockfd){
int fd = sockfd;
if(pthread_create(&mainthread, NULL, start, (void*)&fd)){
if(pthread_create(&mainthread, NULL, start, (void*)&sockfd)){
WARN("pthread_create()");
return false;
}