From c07d0c2d2f6f7b0eb6e92cc6204bf05037957e82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Mon, 1 Sep 2014 15:43:52 +0200 Subject: Imported Upstream version 1.6.3 --- spectro/icoms.c | 134 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 105 insertions(+), 29 deletions(-) (limited to 'spectro/icoms.c') diff --git a/spectro/icoms.c b/spectro/icoms.c index 0dc815c..0166e21 100644 --- a/spectro/icoms.c +++ b/spectro/icoms.c @@ -43,17 +43,64 @@ /* ----------------------------------------------------- */ -/* Fake device */ -icompath icomFakeDevice = { "Fake Display Device" }; +/* Fake display & instrument device */ +icompath icomFakeDevice = { instFakeDisp, "Fake Display Device" }; + + +/* Free an icompath */ +static +void icompath_del_contents(icompath *p) { + + if (p->name != NULL) + free(p->name); +#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL) + if (p->spath != NULL) + free(p->spath); +#endif /* ENABLE_SERIAL */ +#ifdef ENABLE_USB + usb_del_usb_idevice(p->usbd); + hid_del_hid_idevice(p->hidd); +#endif /* ENABLE_USB */ +} + +/* Free an icompath */ +static void icompath_del(icompath *p) { + + icompath_del_contents(p); + free(p); +} + +/* Delete the last path */ +static void icompaths_del_last_path( + icompaths *p +) { + if (p->npaths == 0) + return; + + icompath_del(p->paths[p->npaths-1]); + p->paths[p->npaths-1] = NULL; + p->npaths--; +} + +/* Return the last path */ +static icompath *icompaths_get_last_path( + icompaths *p +) { + if (p->npaths == 0) + return NULL; + + return p->paths[p->npaths-1]; +} /* Return the path corresponding to the port number, or NULL if out of range */ static icompath *icompaths_get_path( icompaths *p, int port /* Enumerated port number, 1..n */ ) { - if (port == -99) + if (port == FAKE_DEVICE_PORT) return &icomFakeDevice; + if (port <= 0 || port > p->npaths) return NULL; @@ -63,20 +110,10 @@ static icompath *icompaths_get_path( static void icompaths_clear(icompaths *p) { /* Free any old list */ - if (p->paths != NULL) { + if (p != NULL && p->paths != NULL) { int i; for (i = 0; i < p->npaths; i++) { - if (p->paths[i]->name != NULL) - free(p->paths[i]->name); -#ifdef ENABLE_SERIAL - if (p->paths[i]->spath != NULL) - free(p->paths[i]->spath); -#endif /* ENABLE_SERIAL */ -#ifdef ENABLE_USB - usb_del_usb_idevice(p->paths[i]->usbd); - hid_del_hid_idevice(p->paths[i]->hidd); -#endif /* ENABLE_USB */ - free(p->paths[i]); + icompath_del(p->paths[i]); } free(p->paths); p->npaths = 0; @@ -111,11 +148,11 @@ static int icompaths_add_path(icompaths *p) { return ICOM_OK; } -#ifdef ENABLE_SERIAL +#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL) /* Add a serial path */ /* return icom error */ -static int icompaths_add_serial(icompaths *p, char *name, char *spath) { +static int icompaths_add_serial(icompaths *p, char *name, char *spath, int fast) { int rv; if ((rv = icompaths_add_path(p)) != ICOM_OK) @@ -129,6 +166,28 @@ static int icompaths_add_serial(icompaths *p, char *name, char *spath) { a1loge(p->log, ICOM_SYS, "icompaths: strdup failed!\n"); return ICOM_SYS; } + p->paths[p->npaths-1]->fast = fast; + + return ICOM_OK; +} + +/* Modify a serial path to add the instrument type */ +int icompaths_set_serial_itype(icompath *p, instType itype) { + char pname[400], *cp; + + p->itype = itype; + + /* Strip any existing description in brackets */ + if ((cp = strchr(p->name, '(')) != NULL && cp > p->name) + cp[-1] = '\000'; + sprintf(pname,"%s (%s)", p->name, inst_name(itype)); + cp = p->name; + if ((p->name = strdup(pname)) == NULL) { + p->name = cp; + a1loge(g_log, ICOM_SYS, "icompaths_set_serial_itype: strdup path failed!\n"); + return ICOM_SYS; + } + free(cp); return ICOM_OK; } @@ -138,6 +197,7 @@ static int icompaths_add_serial(icompaths *p, char *name, char *spath) { /* Set an icompath details */ /* return icom error */ +static int icompath_set_usb(a1log *log, icompath *p, char *name, unsigned int vid, unsigned int pid, int nep, struct usb_idevice *usbd, instType itype) { int rv; @@ -195,10 +255,12 @@ static int icompaths_add_hid(icompaths *p, char *name, unsigned int vid, unsigne #endif /* ENABLE_USB */ static void icompaths_del(icompaths *p) { - icompaths_clear(p); - - p->log = del_a1log(p->log); /* Unreference it */ - free(p); + if (p != NULL) { + icompaths_clear(p); + + p->log = del_a1log(p->log); /* Unreference it and set to NULL */ + free(p); + } } int icompaths_refresh_paths(icompaths *p); /* Defined in platform specific */ @@ -216,8 +278,10 @@ icompaths *new_icompaths(a1log *log) { p->clear = icompaths_clear; p->refresh = icompaths_refresh_paths; + p->del_last_path = icompaths_del_last_path; + p->get_last_path = icompaths_get_last_path; p->get_path = icompaths_get_path; -#ifdef ENABLE_SERIAL +#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL) p->add_serial = icompaths_add_serial; #endif /* ENABLE_SERIAL */ #ifdef ENABLE_USB @@ -228,6 +292,7 @@ icompaths *new_icompaths(a1log *log) { if (icompaths_refresh_paths(p)) { a1loge(log, ICOM_SYS, "new_icompaths: icompaths_refresh_paths failed!\n"); + free(p); return NULL; } @@ -242,16 +307,19 @@ icompaths *new_icompaths(a1log *log) { static int icom_copy_path_to_icom(icoms *p, icompath *pp) { int rv; + if (p->name != NULL) + free(p->name); if ((p->name = strdup(pp->name)) == NULL) { a1loge(p->log, ICOM_SYS, "copy_path_to_icom: malloc name failed\n"); return ICOM_SYS; } -#ifdef ENABLE_SERIAL +#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL) if (pp->spath != NULL) { if ((p->spath = strdup(pp->spath)) == NULL) { a1loge(p->log, ICOM_SYS, "copy_path_to_icom: malloc spath failed\n"); return ICOM_SYS; } + p->fast = pp->fast; } else { p->spath = NULL; } @@ -274,12 +342,14 @@ static int icom_copy_path_to_icom(icoms *p, icompath *pp) { static icom_type icoms_port_type( icoms *p ) { + #ifdef ENABLE_USB if (p->hidd != NULL) return icomt_hid; if (p->usbd != NULL) return icomt_usb; #endif + return icomt_serial; } @@ -301,8 +371,8 @@ icoms *p, char *wbuf, /* Write puffer */ char *rbuf, /* Read buffer */ int bsize, /* Buffer size */ -char tc, /* Terminating characer */ -int ntc, /* Number of terminating characters */ +char *tc, /* Terminating characers, NULL for none */ +int ntc, /* Number of terminating characters needed to terminate */ double tout ) { int rv = ICOM_OK; @@ -314,10 +384,11 @@ double tout return ICOM_NOTS; } -#ifdef ENABLE_SERIAL +#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL) /* Flush any stray chars if serial */ if (p->usbd == NULL && p->hidd == NULL) { int debug = p->log->debug; + if (debug < 8) p->log->debug = 0; for (; rv == ICOM_OK;) /* Until we get a timeout */ @@ -364,11 +435,12 @@ icoms *new_icoms( /* Copy ipath info to this icoms */ if (icom_copy_path_to_icom(p, ipath)) { + free(p->name); free(p); return NULL; } -#ifdef ENABLE_SERIAL +#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL) #ifdef NT p->phandle = NULL; #endif @@ -383,7 +455,7 @@ icoms *new_icoms( #endif /* ENABLE_SERIAL */ p->lserr = 0; - p->tc = -1; +// p->tc = -1; p->log = new_a1log_d(log); p->debug = p->log->debug; /* Legacy code */ @@ -391,7 +463,7 @@ icoms *new_icoms( p->close_port = icoms_close_port; p->port_type = icoms_port_type; -#ifdef ENABLE_SERIAL +#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL) p->set_ser_port = icoms_set_ser_port; #endif /* ENABLE_SERIAL */ @@ -442,6 +514,10 @@ icoms_fix(char *ss) { unsigned char *s = (unsigned char *)ss; if (++ix >= 5) ix = 0; + if (ss == NULL) { + strcpy((char *)buf[ix],"(null)"); + return (char *)buf[ix]; + } for(d = buf[ix]; (d - buf[ix]) < 1000;) { if (*s < ' ' && *s > '\000') { *d++ = '^'; -- cgit v1.2.3