X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=ir-tcl.c;h=9eddd5f47ea5e70a9822917ae45e347333a28057;hb=c10f5ecfbd81e3a9bda4cca310e13b066a520f4a;hp=50cf9182aeea69e24dbf1f24aef702688f56f57e;hpb=b83014af59e2736d11f47cdb8da577e917a84d8f;p=ir-tcl-moved-to-github.git diff --git a/ir-tcl.c b/ir-tcl.c index 50cf918..9eddd5f 100644 --- a/ir-tcl.c +++ b/ir-tcl.c @@ -5,7 +5,24 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: ir-tcl.c,v $ - * Revision 1.70 1996-01-10 09:18:34 adam + * Revision 1.75 1996-02-19 15:41:53 adam + * Better log messages. + * Minor improvement of connect method. + * + * Revision 1.74 1996/02/05 17:58:03 adam + * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1. + * + * Revision 1.73 1996/01/29 11:35:19 adam + * Bug fix: cs_type member renamed to comstackType to avoid conflict with + * cs_type macro defined by YAZ. + * + * Revision 1.72 1996/01/19 17:45:34 quinn + * Added debugging output + * + * Revision 1.71 1996/01/19 16:22:38 adam + * New method: apduDump - returns information about last incoming APDU. + * + * Revision 1.70 1996/01/10 09:18:34 adam * PDU specific callbacks implemented: initRespnse, searchResponse, * presentResponse and scanResponse. * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1. @@ -251,11 +268,8 @@ #include #include -#ifdef WINDOWS +#include #include -#else -#include -#endif #include #define CS_BLOCK 0 @@ -684,6 +698,54 @@ static int do_options (void *obj, Tcl_Interp *interp, } /* + * do_apduInfo: Get APDU information + */ +static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv) +{ + char buf[16]; + FILE *apduf; + IrTcl_Obj *p = obj; + + if (argc <= 0) + return TCL_OK; + sprintf (buf, "%d", p->apduLen); + Tcl_AppendElement (interp, buf); + sprintf (buf, "%d", p->apduOffset); + Tcl_AppendElement (interp, buf); + if (!p->buf_in) + { + Tcl_AppendElement (interp, ""); + return TCL_OK; + } + apduf = fopen ("apdu.tmp", "w"); + if (!apduf) + { + Tcl_AppendElement (interp, ""); + return TCL_OK; + } + odr_dumpBER (apduf, p->buf_in, p->apduLen); + fclose (apduf); + if (!(apduf = fopen ("apdu.tmp", "r"))) + Tcl_AppendElement (interp, ""); + else + { + int c; + + Tcl_AppendResult (interp, " {", NULL); + while ((c = getc (apduf)) != EOF) + { + buf[0] = c; + buf[1] = '\0'; + Tcl_AppendResult (interp, buf, NULL); + } + fclose (apduf); + Tcl_AppendResult (interp, "}", NULL); + } + unlink ("apdu.tmp"); + return TCL_OK; +} + +/* * do_failInfo: Get fail information */ static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv) @@ -964,9 +1026,7 @@ static int do_connect (void *obj, Tcl_Interp *interp, interp->result = "already connected"; return TCL_ERROR; } - if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR) - return TCL_ERROR; - if (!strcmp (p->cs_type, "tcpip")) + if (!strcmp (p->comstackType, "tcpip")) { p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type); addr = tcpip_strtoaddr (argv[2]); @@ -977,7 +1037,7 @@ static int do_connect (void *obj, Tcl_Interp *interp, } logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]); } - else if (!strcmp (p->cs_type, "mosi")) + else if (!strcmp (p->comstackType, "mosi")) { #if MOSI p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type); @@ -996,20 +1056,39 @@ static int do_connect (void *obj, Tcl_Interp *interp, else { Tcl_AppendResult (interp, "Bad comstack type: ", - p->cs_type, NULL); + p->comstackType, NULL); return TCL_ERROR; } + if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR) + return TCL_ERROR; +#if IRTCL_GENERIC_FILES +#ifdef WINDOWS + p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_WIN_SOCKET); +#else + p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_UNIX_FD); +#endif +#endif if ((r=cs_connect (p->cs_link, addr)) < 0) { interp->result = "connect fail"; do_disconnect (p, NULL, 2, NULL); return TCL_ERROR; } + logf(LOG_DEBUG, "cs_connect() returned %d fd=%d", r, + cs_fileno(p->cs_link)); p->eventType = "connect"; +#if IRTCL_GENERIC_FILES + ir_select_add (p->csFile, p); +#else ir_select_add (cs_fileno (p->cs_link), p); +#endif if (r == 1) { +#if IRTCL_GENERIC_FILES + ir_select_add_write (p->csFile, p); +#else ir_select_add_write (cs_fileno (p->cs_link), p); +#endif p->state = IR_TCL_R_Connecting; } else @@ -1038,20 +1117,33 @@ static int do_disconnect (void *obj, Tcl_Interp *interp, p->eventType = NULL; p->hostname = NULL; p->cs_link = NULL; +#if IRTCL_GENERIC_FILES + p->csFile = 0; +#endif return TCL_OK; } if (p->hostname) { + logf(LOG_DEBUG, "Closing connection to %s", p->hostname); free (p->hostname); p->hostname = NULL; +#if IRTCL_GENERIC_FILES + ir_select_remove_write (p->csFile, p); + ir_select_remove (p->csFile, p); +#else ir_select_remove_write (cs_fileno (p->cs_link), p); ir_select_remove (cs_fileno (p->cs_link), p); +#endif odr_reset (p->odr_in); assert (p->cs_link); cs_close (p->cs_link); p->cs_link = NULL; +#if IRTCL_GENERIC_FILES + Tcl_FreeFile (p->csFile); + p->csFile = NULL; +#endif ODR_MASK_ZERO (&p->options); ODR_MASK_SET (&p->options, 0); @@ -1078,16 +1170,16 @@ static int do_comstack (void *o, Tcl_Interp *interp, IrTcl_Obj *obj = o; if (argc == 0) - return ir_tcl_strdup (interp, &obj->cs_type, "tcpip"); + return ir_tcl_strdup (interp, &obj->comstackType, "tcpip"); else if (argc == -1) - return ir_tcl_strdel (interp, &obj->cs_type); + return ir_tcl_strdel (interp, &obj->comstackType); else if (argc == 3) { - free (obj->cs_type); - if (ir_tcl_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR) + free (obj->comstackType); + if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR) return TCL_ERROR; } - Tcl_AppendElement (interp, obj->cs_type); + Tcl_AppendElement (interp, obj->comstackType); return TCL_OK; } @@ -1565,6 +1657,7 @@ static IrTcl_Method ir_method_tab[] = { { 1, "protocol", do_protocol }, { 0, "failback", do_failback }, { 0, "failInfo", do_failInfo }, +{ 0, "apduInfo", do_apduInfo }, { 0, "logLevel", do_logLevel }, { 0, "eventType", do_eventType }, @@ -1684,7 +1777,7 @@ static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp, } #endif - logf (LOG_DEBUG, "ir object create"); + logf (LOG_DEBUG, "ir object create %s", argv[1]); obj->odr_in = odr_createmem (ODR_DECODE); obj->odr_out = odr_createmem (ODR_ENCODE); obj->odr_pr = odr_createmem (ODR_PRINT); @@ -3287,8 +3380,10 @@ void ir_select_read (ClientData clientData) Tcl_CmdInfo cmd_info; const char *apdu_call; + logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link)); if (p->state == IR_TCL_R_Connecting) { + logf(LOG_DEBUG, "Connect handler"); r = cs_rcvconnect (p->cs_link); if (r == 1) { @@ -3296,7 +3391,11 @@ void ir_select_read (ClientData clientData) return; } p->state = IR_TCL_R_Idle; +#if IRTCL_GENERIC_FILES + ir_select_remove_write (p->csFile, p); +#else ir_select_remove_write (cs_fileno (p->cs_link), p); +#endif if (r < 0) { logf (LOG_DEBUG, "cs_rcvconnect error"); @@ -3326,7 +3425,11 @@ void ir_select_read (ClientData clientData) if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0) { logf (LOG_DEBUG, "cs_get failed, code %d", r); +#if IRTCL_GENERIC_FILES + ir_select_remove (p->csFile, p); +#else ir_select_remove (cs_fileno (p->cs_link), p); +#endif do_disconnect (p, NULL, 2, NULL); if (p->failback) { @@ -3338,23 +3441,31 @@ void ir_select_read (ClientData clientData) return; } if (r == 1) + { + logf(LOG_DEBUG, "PDU Fraction read"); return ; + } /* got complete APDU. Now decode */ + p->apduLen = r; + p->apduOffset = -1; odr_setbuf (p->odr_in, p->buf_in, r, 0); - logf (LOG_DEBUG, "cs_get ok, got %d", r); + logf (LOG_DEBUG, "cs_get ok, total size %d", r); if (!z_APDU (p->odr_in, &apdu, 0)) { - logf (LOG_DEBUG, "%s", odr_errmsg (odr_geterror (p->odr_in))); + logf (LOG_DEBUG, "cs_get failed: %s", + odr_errmsg (odr_geterror (p->odr_in))); do_disconnect (p, NULL, 2, NULL); if (p->failback) { p->failInfo = IR_TCL_FAIL_IN_APDU; + p->apduOffset = odr_offset (p->odr_in); IrTcl_eval (p->interp, p->failback); } /* release ir object now if failback deleted it */ ir_obj_delete (p); return; } + logf(LOG_DEBUG, "Decoded ok"); /* handle APDU and invoke callback */ rq = p->request_queue; if (!rq) @@ -3439,9 +3550,10 @@ void ir_select_write (ClientData clientData) int r; IrTcl_Request *rq; - logf (LOG_DEBUG, "In write handler"); + logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link)); if (p->state == IR_TCL_R_Connecting) { + logf(LOG_DEBUG, "Connect handler"); r = cs_rcvconnect (p->cs_link); if (r == 1) return; @@ -3449,7 +3561,11 @@ void ir_select_write (ClientData clientData) if (r < 0) { logf (LOG_DEBUG, "cs_rcvconnect error"); +#if IRTCL_GENERIC_FILES + ir_select_remove_write (p->csFile, p); +#else ir_select_remove_write (cs_fileno (p->cs_link), p); +#endif if (p->failback) { p->failInfo = IR_TCL_FAIL_CONNECT; @@ -3458,7 +3574,11 @@ void ir_select_write (ClientData clientData) do_disconnect (p, NULL, 2, NULL); return; } +#if IRTCL_GENERIC_FILES + ir_select_remove_write (p->csFile, p); +#else ir_select_remove_write (cs_fileno (p->cs_link), p); +#endif if (p->callback) IrTcl_eval (p->interp, p->callback); return; @@ -3469,7 +3589,7 @@ void ir_select_write (ClientData clientData) assert (rq); if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0) { - logf (LOG_DEBUG, "select write fail"); + logf (LOG_DEBUG, "cs_put write fail"); if (p->failback) { p->failInfo = IR_TCL_FAIL_WRITE; @@ -3481,8 +3601,13 @@ void ir_select_write (ClientData clientData) } else if (r == 0) /* remove select bit */ { + logf(LOG_DEBUG, "Write completed"); p->state = IR_TCL_R_Waiting; +#if IRTCL_GENERIC_FILES + ir_select_remove_write (p->csFile, p); +#else ir_select_remove_write (cs_fileno (p->cs_link), p); +#endif free (rq->buf_out); rq->buf_out = NULL; }