home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ACDI.ZIP / ACDI / ACDI_C / ACDICRCV.C < prev    next >
Text File  |  1991-09-05  |  37KB  |  826 lines

  1. /************************************************************************/
  2. /*                                                                      */
  3. /*   MODULE NAME: ACDICRCV.C                                            */
  4. /*                                                                      */
  5. /*   DESCRIPTIVE NAME: ACDI C SAMPLE RECEIVE PROGRAM                    */
  6. /*                     OS/2 EXTENDED SERVICES                           */
  7. /*                                                                      */
  8. /*   COPYRIGHT:  (C) COPYRIGHT IBM CORP. 1988, 1991                     */
  9. /*               LICENSED MATERIAL - PROGRAM PROPERTY OF IBM            */
  10. /*               ALL RIGHTS RESERVED                                    */
  11. /*                                                                      */
  12. /*   STATUS:   LPP Release 1.0 Modification 0                           */
  13. /*                                                                      */
  14. /*   FUNCTION: The sample program will use the ACDI interface to echo   */
  15. /*             line by line screen image from the first Personal        */
  16. /*             Computer on the second Personal Computer connected       */
  17. /*             through asynchronous line.                               */
  18. /*             The RECEIVE PROGRAM, is executed in the second Personal  */
  19. /*             Computer.                                                */
  20. /*                                                                      */
  21. /*              Uses the following ACDI Verbs:                          */
  22. /*                                                                      */
  23. /*                COMOPEN                                               */
  24. /*                COMDEFOUTPUTBUFF                                      */
  25. /*                COMDEFINPUT                                           */
  26. /*                COMSETBITRATE                                         */
  27. /*                COMSETLINECTRL                                        */
  28. /*                COMCONNECT                                            */
  29. /*                COMSETTIMEOUTS                                        */
  30. /*                COMREADEVENT                                          */
  31. /*                COMREADCHARSTRING                                     */
  32. /*                COMDISCONNECT                                         */
  33. /*                COMCLOSE                                              */
  34. /*                                                                      */
  35. /*   NOTE:     The asynchronous device name to be used in this          */
  36. /*             program is COM1. If this is to be changed it must        */
  37. /*             be changed in the main header.                           */
  38. /*                                                                      */
  39. /*             This program is designed to run with connect type 4      */
  40. /*             only.                                                    */
  41. /*                                                                      */
  42. /*   MODULE TYPE = Micosoft C Compiler Version 6.00A                    */
  43. /*                 (Compiler options - /ALw /G2 /Zp)                    */
  44. /*                                                                      */
  45. /*   PREREQS = Requires Message file "ACX.MSG".                         */
  46. /*                                                                      */
  47. /************************************************************************/
  48.  
  49. #include <ACDI_C.H>
  50. #include <STDIO.H>
  51. #include <STDDEF.H>
  52. #include <STRING.H>
  53. #include <DOS.H>
  54. #include <DOSCALLS.H>
  55. #include <SUBCALLS.H>
  56.  
  57. #define TRUE 1
  58. #define AA_OK 0
  59.  
  60. #define LINT_ARGS
  61.  
  62. #define clear_vcb()   memset(&vcb,(int)'\0',sizeof(vcb))
  63.                                             /* macro to clear control block */
  64. #define STACK_LGTH 4096                     /* len of stack for thread proc */
  65.  
  66. #define session_active 0
  67. #define session_ended 1
  68.  
  69. char sys_err       = 0;                     /* system error flag            */
  70. char dev_name[6]   ="COM1\0";               /* communication device name    */
  71.  
  72. unsigned short handle;                      /* communication device handle  */
  73.  
  74. unsigned short ret_code;                    /* save area for ACDI return    */
  75. unsigned char func_code;                    /* and function codes           */
  76.  
  77. unsigned short dos_rc;                      /* save area for dos and        */
  78. unsigned short dos_vio_rc;                  /* vio return codes             */
  79.  
  80. char far *i_anbptr;                         /* pointer to input             */
  81.                                             /* AlphaNumeric buffer          */
  82.  
  83. char far *o_anbptr;                         /* pointer to output            */
  84.                                             /* AlphaNumeric buffer          */
  85.  
  86. char far *s_anbptr;                         /* pointer to stack for the     */
  87.                                             /* thread process-ACDIEVENT     */
  88.  
  89. unsigned short thread_id;                   /* returned by DosCreateThread  */
  90.  
  91. unsigned long event_sem;                    /* ram semaphore for signaling  */
  92.                                             /* occurance of an event        */
  93.  
  94. unsigned short concttimeout1 = 0;           /* connection timeout parameters*/
  95. unsigned short concttimeout2 = 30;          /* for comconnect verb          */
  96.  
  97. unsigned short inf_rdblocktimeout = 0;      /* infinite timeout             */
  98. unsigned short rdchartimeout = 1;           /* parameters for the verb      */
  99. unsigned short inf_wrttimeout = 0;          /* comsettimeouts               */
  100.  
  101. char msg_buff[100];
  102. char msg_filename[] = "ACX.MSG";
  103.  
  104. char ACDIEVENTFLAG;                         /* event occurance flag         */
  105.  
  106. extern void pascal far ACDI(unsigned far *);
  107. void far ACDIEVENT() ;
  108.  
  109. /****************************************************************************/
  110. /*  ACDI communications control block                                       */
  111. /****************************************************************************/
  112.  
  113. union {
  114.    struct comopen_cb open_cb;
  115.    struct comdefinput_cb definputbuff_cb;
  116.    struct comdefoutputbuff_cb defoutputbuff_cb;
  117.    struct comsetbitrate_cb setbitrate_cb;
  118.    struct comsetlinectrl_cb setlinectrl_cb;
  119.    struct comconnect_cb connect_cb;
  120.    struct comsettimeouts_cb settimeouts_cb;
  121.    struct comreadcharstring_cb readcharstring_cb;
  122.    struct comdisconnect_cb disconnect_cb;
  123.    struct comclose_cb close_cb;
  124. }vcb;
  125.  
  126. unsigned far *vcbptr;                       /* pointer to vcb               */
  127.  
  128. struct event_struct event_types;            /* structures to be used in the */
  129. struct masks event_masks;                   /* thread process-ACDIEVENT()   */
  130. struct comreadevent_cb readevent_cb;
  131.  
  132. #ifdef LINT_ARGS
  133. /****************************************************************************/
  134. /*  Function prototypes                                                     */
  135. /****************************************************************************/
  136. void main(void);
  137. void ACDIREADLINES(void);
  138. void ACDIEVENT(void);
  139.  
  140. void SHOW_ERR(void);
  141. void SHOW_DOS_ERR(void);
  142. void SHOW_VIO_ERR(void);
  143. void SHOW_MSG(unsigned short);
  144.  
  145. void set_sem(unsigned long);
  146. void sem_wait(unsigned long);
  147. void clear_screen(void);
  148. void set_crsr_pos(int, int);
  149.  
  150. void comopen(void);
  151. void comdefinput(unsigned short, unsigned char far *, unsigned short,
  152.                  unsigned char);
  153. void comdefoutputbuff(unsigned short, unsigned char far *, unsigned short);
  154. void comsetbitrate(unsigned short, unsigned short, unsigned short);
  155. void comsetlinectrl(unsigned short, unsigned short, unsigned short,
  156.                     unsigned short);
  157. void comconnect(unsigned short, unsigned short, unsigned short,
  158.                 unsigned short);
  159. void comsettimeouts(unsigned short, unsigned short, unsigned short,
  160.                     unsigned short);
  161. void comreadcharstring(unsigned short, unsigned short, unsigned short,
  162.                        unsigned short);
  163. void comreadevent(unsigned short, unsigned long, struct event_struct *,
  164.                   struct masks *, unsigned far *);
  165. void comdisconnect(unsigned short);
  166. void comclose(unsigned short);
  167.  
  168. #endif
  169. /****************************************************************************/
  170. /*   The main program.                                                      */
  171. /****************************************************************************/
  172.  
  173. void
  174. main()
  175. {
  176.   SHOW_MSG(7);                              /* msg 7-'ACDI sample recv.prog'*/
  177.   vcbptr = (unsigned far *)&vcb;            /* initialize vcbptr            */
  178.  
  179.   if (! sys_err)
  180.     comopen();                              /* issue com_open verb          */
  181.  
  182.   if (! sys_err)  {
  183.     i_anbptr = 0;                           /* zero out the pointer         */
  184.     dos_rc = DOSALLOCSEG(82, (unsigned far *)&FP_SEG(i_anbptr), 0);
  185.                                             /* doscall to get input buffer  */
  186.     if (dos_rc != 0)  {
  187.       SHOW_DOS_ERR();
  188.       sys_err = TRUE;
  189.     }
  190.   }
  191.  
  192.   if (! sys_err)                            /* issue com_def_input          */
  193.     comdefinput(handle, i_anbptr, 82, AA_CHAR_MODE);
  194.  
  195.   if (! sys_err)  {
  196.     o_anbptr = 0;                           /* zero out the pointer         */
  197.     dos_rc = DOSALLOCSEG(82, (unsigned far *)&FP_SEG(o_anbptr), 0);
  198.                                             /* doscall to get output buffer */
  199.     if (dos_rc != 0)  {
  200.       SHOW_DOS_ERR();
  201.       sys_err = TRUE;
  202.     }
  203.   }
  204.  
  205.   if (!sys_err)                             /* issue com_def_output_buff    */
  206.     comdefoutputbuff(handle, o_anbptr, 82);
  207.  
  208.   if (! sys_err)                            /* issue com_set_bit_rate       */
  209.     comsetbitrate(handle, AA_300_BPS, AA_300_BPS);
  210.  
  211.   if (! sys_err)                            /* issue com_set_line_ctrl      */
  212.      comsetlinectrl(handle, AA_1_STOP_BIT, AA_EVEN_PARITY, AA_7_DATA_BITS);
  213.  
  214.   if (! sys_err)                            /* issue com_connect            */
  215.      comconnect(handle,AA_CONNECT_TYPE_4, concttimeout1, concttimeout2);
  216.  
  217.   if (! sys_err)                            /* issue com_set_timeouts       */
  218.      comsettimeouts(handle, inf_rdblocktimeout, rdchartimeout,
  219.                     inf_wrttimeout);
  220.  
  221.   if (! sys_err)  {
  222.     s_anbptr = 0;                           /* zero out the pointer         */
  223.     dos_rc = DOSALLOCSEG(STACK_LGTH,(unsigned far *)&FP_SEG(s_anbptr), 3);
  224.                                             /* doscall to get a buffer to be*/
  225.                                             /* used as stack for thread proc*/
  226.     if (dos_rc != 0)  {
  227.       SHOW_DOS_ERR();
  228.       sys_err = TRUE;
  229.     }
  230.     s_anbptr = s_anbptr + (STACK_LGTH - 1);
  231.   }
  232.  
  233.   if (! sys_err)  {
  234.     dos_rc = DOSCREATETHREAD(ACDIEVENT, (unsigned far *)&thread_id, s_anbptr);
  235.                                             /* doscall to start thread proc */
  236.     if (dos_rc != 0)  {
  237.       SHOW_DOS_ERR();
  238.       sys_err = TRUE;
  239.     }
  240.   }
  241.  
  242.   if (!sys_err)                             /* call subroutine-read message */
  243.     ACDIREADLINES();
  244.  
  245.   if (! sys_err)                            /* issue com_disconnect         */
  246.      comdisconnect(handle);
  247.  
  248.   if (! sys_err)                            /* issue com_close              */
  249.      comclose(handle);
  250.  
  251. }   /* end of main */
  252.  
  253. /*****************************************************************************/
  254. /*   Thread process                                                          */
  255. /*****************************************************************************/
  256.  
  257. void far ACDIEVENT()
  258. /*  This is the thread process. It will set the EVENTFLAG to zero to enable */
  259. /*  receiving characters by the main process, then set the semaphore; issue */
  260. /*  comreadevent verb, and wait for the semaphore to be cleared by async    */
  261. /*  subsystem - which will happen when any one of three events specified -  */
  262. /*  break signal, disconnect or a stop is received. When this occurs it     */
  263. /*  will set the EVENTFLAG to signal end of session.                        */
  264. /*  This process runs asynchronously with the main process so that receiving*/
  265. /*  message, and watching for the break signal can be done simultaneously   */
  266.  
  267. {
  268.   ACDIEVENTFLAG = session_active;           /* set event flag to active     */
  269.  
  270.   set_sem((unsigned long)&event_sem);       /* set event semaphore          */
  271.  
  272.   if (! sys_err) {
  273.     event_masks.event_mask_1[0] = 0x00;
  274.     event_masks.event_mask_1[1] = 0x00;
  275.     event_masks.event_mask_1[2] = AA_BREAK_RECEIVED | AA_CONNECTION_LOST;
  276.     event_masks.event_mask_1[3] = AA_STOP_ISSUED;
  277.     event_masks.event_mask_2[0] = 0x00;
  278.     event_masks.event_mask_2[1] = 0x00;
  279.     event_masks.event_mask_2[2] = 0x00;
  280.     event_masks.event_mask_2[3] = 0x00;     /* set up masks for COMREADEVENT*/
  281.  
  282.     comreadevent(handle,(unsigned long) &event_sem, &event_types,
  283.                  &event_masks, (unsigned far *)&readevent_cb );
  284.                                             /* issue comreadevent verb      */
  285.   }
  286.   if (!sys_err)
  287.     sem_wait((unsigned long)&event_sem);    /* wait for sem. to be cleared  */
  288.  
  289.   if (! sys_err)                            /* semaphore has been cleared so*/
  290.     ACDIEVENTFLAG = session_ended;          /* set flag to session_ended    */
  291. }   /* end of thread process */
  292.  
  293. /****************************************************************************/
  294. /*  Subroutine to read message.                                             */
  295. /****************************************************************************/
  296.  
  297. void
  298. ACDIREADLINES()
  299. /* This subroutine will clear the screen in preparation for receiving the   */
  300. /* message, issue com_read_char_string verb as long as ACDIEVENTFLAG is on, */
  301. /* read the message character by character, display it on the screen. It    */
  302. /* will quit when the flag is set to session ended.                         */
  303.  
  304. {
  305. #define CAR_RET 0x0D                        /* the Enter/Carriage return key*/
  306.  
  307. unsigned short bytesfreed = 0;              /* parameters for com_read_char_*/
  308. unsigned short readbytesneeded = 1;         /* string verb                  */
  309. unsigned short initialwait = 0;
  310.  
  311. int row = 0;                                /* variables to keep track of   */
  312. int col = 0;                                /* and alter cursor position    */
  313.  
  314. int ch_atr = 0x720;                         /* parameter for vioscrollup    */
  315.  
  316.   clear_screen();                           /* clear the screen             */
  317.  
  318.   if (!sys_err)
  319.     set_crsr_pos(row, col);                 /* set cursor to row0 column0   */
  320.  
  321.     while ((ACDIEVENTFLAG == session_active)&&(! sys_err))  {
  322.        comreadcharstring(handle, bytesfreed, readbytesneeded,initialwait);
  323.                                             /* issue com_read_char_string   */
  324.  
  325.        if (ret_code == 0x54) {
  326.                                             /* if timeout occurred or discon*/
  327.          bytesfreed = 0;                    /* set bytesfreed to zero,      */
  328.          continue;                          /* try to issue verb again      */
  329.        }
  330.        else  {                              /* if no timeout occured and no */
  331.          if (!sys_err)  {                   /* error occured-character has  */
  332.                                             /* been received                */
  333.            i_anbptr = vcb.readcharstring_cb.next_avail_read_ptr;
  334.                                             /* copy ptr to received char.   */
  335.            if (*i_anbptr != CAR_RET)  {     /* if rec'd char is not car_ret */
  336.              dos_vio_rc = VIOWRTNCHAR(i_anbptr, 1, row, col, 0);
  337.                                             /* write character on screen    */
  338.              if (dos_vio_rc != 0)  {        /* check viocall return code &  */
  339.                SHOW_VIO_ERR();              /* handle any error             */
  340.                sys_err = TRUE;
  341.              }
  342.              ++col;                         /* incr col-next pos on screen  */
  343.            }
  344.            else  {                          /* otherwise, car_ret was rec'd */
  345.                                             /* it means-start a new line    */
  346.              dos_vio_rc = VIOGETCURPOS(&row, &col, 0) ;
  347.                                             /* doscall-get current crsr pos */
  348.              col = 0;                       /* set column to 0 of next line */
  349.              ++row;                         /* increment current row to next*/
  350.              if (row == 24)  {              /* if current row is 24, scroll */
  351.                                             /* up screen by one row and...  */
  352.                dos_rc = VIOSCROLLUP(0, 0, 24, 79, 1, (char *)&ch_atr,0);
  353.                row = 23;                    /* set current row to 23 again  */
  354.              }
  355.            }
  356.            if (col == 80)                   /* 79th column is last on screen*/
  357.              col = 0;                       /* so if col 79 is reached,     */
  358.                                             /* set it back to zero          */
  359.            set_crsr_pos(row, col) ;         /* set cursor to new position   */
  360.            bytesfreed = 1;                  /* indicate one byte is read    */
  361.          }   /* end of if (! sys_err) */
  362.        }     /* end of else           */
  363.     }        /* end of while loop     */
  364. }            /* end of subroutine     */
  365. /* end of subroutine */
  366.  
  367. /****************************************************************************/
  368. /*  Utility Functions                                                       */
  369. /****************************************************************************/
  370.  
  371. void
  372. SHOW_ERR()
  373. /* This function shows errors relating to ACDI verbs.                       */
  374.  
  375. {
  376.   SHOW_MSG(2);
  377.   printf ("%04X\n", func_code);
  378.   SHOW_MSG(3);
  379.   printf ("%04X\n", ret_code);
  380. }
  381.  
  382. void
  383. SHOW_DOS_ERR()
  384. /* This function shows errors relating to DOS function calls.               */
  385.  
  386. {
  387.   SHOW_MSG(4);
  388.   printf ("%04X\n", dos_rc);
  389. }
  390.  
  391. void
  392. SHOW_VIO_ERR()
  393. /* This function shows errors relating to Vio function calls.               */
  394.  
  395. {
  396.   SHOW_MSG(5);
  397.   printf ("%04X\n", dos_vio_rc);
  398. }
  399.  
  400. void
  401. SHOW_MSG(MSGNO)
  402. /* This function displays error messages using argument MSGNO ( message     */
  403. /* number) from the message file "ACX.MSG"                                  */
  404. unsigned short MSGNO;
  405.  
  406. {
  407.   unsigned msg_rc;
  408.   unsigned msg_len;
  409.  
  410.   msg_rc = DOSGETMESSAGE ((char far *)0, 0, (char far *)msg_buff,
  411.                           sizeof(msg_buff), MSGNO, (char far *)msg_filename,
  412.                           (unsigned far *)&msg_len);
  413.                                             /* doscall to get message from  */
  414.   if (msg_rc != AA_OK) {
  415.     printf("Unable to process message file -DOSGETMESSAGE function call \n");
  416.     printf("in error. Return code = %04d  Message Number = %04d \n",
  417.             msg_rc,MSGNO);
  418.     return;
  419.   }
  420.   msg_buff[msg_len] = 0x00;
  421.   printf("%s",msg_buff);
  422. }
  423.  
  424. /****************************************************************************/
  425. /*  ACDI related subroutines                                                */
  426. /****************************************************************************/
  427.  
  428. /* To issue an acdi verb the program has to build the control block struc.  */
  429. /* with the parameters and pass the pointer to the control block to the     */
  430. /* acdi subsystem. Each of following subroutines, when called, will build   */
  431. /* the control block structure and then call acdi. When the acdi subsystem  */
  432. /* returns the subroutine will check the return code, call SHOW_ERR process */
  433. /* if the return code is bad, otherwise return to the calling process.      */
  434.  
  435. void
  436. comopen()
  437. /* This subroutine will issue com_open verb to open the specified com.      */
  438. /* device for communication                                                 */
  439.  
  440. {
  441.   clear_vcb();                              /* zero out the control block   */
  442.  
  443.   vcb.open_cb.common.function_code = COM_OPEN;
  444.                                             /* verb - com_open              */
  445.   strcpy(vcb.open_cb.com_dev_name,dev_name);
  446.                                             /* copy communication device    */
  447.                                             /* name into the control block  */
  448.   ACDI(vcbptr);                             /* issue the acdi verb          */
  449.   handle = vcb.open_cb.common.com_dev_handle;
  450.                                             /* copy device handle returned  */
  451.                                             /* by acdi to use in other verbs*/
  452.   if (vcb.open_cb.common.return_code != AA_OK) {
  453.                                             /* check if return code is zero */
  454.                                             /* if not, copy ret & func code */
  455.      ret_code = vcb.open_cb.common.return_code;
  456.      func_code = vcb.open_cb.common.function_code;
  457.      SHOW_ERR();                            /* show error  and              */
  458.      sys_err = TRUE;                        /* set the system error         */
  459.   }
  460. }
  461.  
  462. void
  463. comdefoutputbuff(handle, outputbuff, outbufflength)
  464. /* This subroutine will issue com_def_output_buff to define output buffer   */
  465.  
  466. unsigned short handle;                      /* device handle                */
  467. unsigned char far *outputbuff;              /* pointer to output buffer     */
  468. unsigned short outbufflength;               /* length of output buffer      */
  469.  
  470. {
  471.   clear_vcb();                              /* zero out the control block   */
  472.  
  473.   vcb.defoutputbuff_cb.common.com_dev_handle = handle;
  474.   vcb.defoutputbuff_cb.common.function_code = COM_DEF_OUTPUT_BUFF;
  475.   vcb.defoutputbuff_cb.output_buff = outputbuff;
  476.   vcb.defoutputbuff_cb.out_buff_length = outbufflength;
  477.                                             /* fill in control block with   */
  478.                                             /* function code and parameters */
  479.   ACDI(vcbptr);                             /* issue the verb               */
  480.  
  481.   if (vcb.defoutputbuff_cb.common.return_code != AA_OK)  {
  482.     ret_code = vcb.defoutputbuff_cb.common.return_code;
  483.     func_code = vcb.defoutputbuff_cb.common.function_code;
  484.     SHOW_ERR();
  485.     sys_err = TRUE;                         /* check return code and handle */
  486.                                             /* any errors                   */
  487.   }
  488. }
  489.  
  490. void
  491. comdefinput(handle, inputbuff, inbufflength, inputmode)
  492. /* This subroutine will issue com_def_input verb to define input buffer     */
  493.  
  494. unsigned short handle;                      /* device handle                */
  495. unsigned char far *inputbuff;               /* pointer to input buffer      */
  496. unsigned short inbufflength;                /* length of input buffer       */
  497. unsigned char inputmode;                    /* mode of input                */
  498.  
  499. {
  500.   clear_vcb();                              /* zero out the control block   */
  501.  
  502.   vcb.definputbuff_cb.common.com_dev_handle = handle;
  503.   vcb.definputbuff_cb.common.function_code = COM_DEF_INPUT;
  504.   vcb.definputbuff_cb.input_buff = inputbuff;
  505.   vcb.definputbuff_cb.in_buff_length = inbufflength;
  506.   vcb.definputbuff_cb.input_mode = inputmode;
  507.                                             /* fill in control block with   */
  508.                                             /* function code and parameters */
  509.   ACDI(vcbptr);                             /* issue the verb               */
  510.  
  511.   if (vcb.definputbuff_cb.common.return_code != AA_OK) {
  512.     ret_code = vcb.definputbuff_cb.common.return_code;
  513.     func_code = vcb.definputbuff_cb.common.function_code;
  514.     SHOW_ERR();
  515.     sys_err = TRUE;                         /* check return code and handle */
  516.                                             /* any errors                   */
  517.   }
  518. }
  519.  
  520. void
  521. comsetbitrate(handle, rcv_bps, trans_bps)
  522. /* This subroutine will issue com_set_bit_rate verb to set up the line      */
  523. /* data rates (bps).                                                        */
  524.  
  525. unsigned short handle;                      /* device handle                */
  526. unsigned short rcv_bps, trans_bps;          /* recv. and trans. data rates  */
  527.  
  528. {
  529.   clear_vcb();                              /* zero out the control block   */
  530.  
  531.   vcb.setbitrate_cb.common.com_dev_handle = handle;
  532.   vcb.setbitrate_cb.common.function_code = COM_SET_BIT_RATE;
  533.   vcb.setbitrate_cb.bit_rate_rcv = rcv_bps;
  534.   vcb.setbitrate_cb.bit_rate_send = trans_bps;
  535.                                             /* fill in control block with   */
  536.                                             /* function code and parameters */
  537.   ACDI(vcbptr);                             /* issue the verb               */
  538.  
  539.   if (vcb.setbitrate_cb.common.return_code != AA_OK)  {
  540.     ret_code = vcb.setbitrate_cb.common.return_code;
  541.     func_code = vcb.setbitrate_cb.common.function_code;
  542.     SHOW_ERR();
  543.     sys_err = TRUE;                         /* check return code and handle */
  544.                                             /* any errors                   */
  545.   }
  546. }
  547.  
  548. void
  549. comsetlinectrl(handle, stopbits, par, databits)
  550. /* This subroutine will issue com_set_line_ctrl to set up line control      */
  551. /* values.                                                                  */
  552.  
  553. unsigned short handle;                      /* device handle                */
  554. unsigned short stopbits, par, databits;     /* line control parameters      */
  555.  
  556. {
  557.   clear_vcb();                              /* zero out the control block   */
  558.  
  559.   vcb.setlinectrl_cb.common.com_dev_handle = handle;
  560.   vcb.setlinectrl_cb.common.function_code = COM_SET_LINE_CTRL;
  561.   vcb.setlinectrl_cb.stop_bits = stopbits;
  562.   vcb.setlinectrl_cb.parity = par;
  563.   vcb.setlinectrl_cb.data_bits = databits;
  564.                                             /* fill in control block with   */
  565.                                             /* function code and parameters */
  566.   ACDI(vcbptr);                             /* issue the verb               */
  567.  
  568.   if (vcb.setlinectrl_cb.common.return_code != AA_OK)  {
  569.     ret_code = vcb.setlinectrl_cb.common.return_code;
  570.     func_code = vcb.setlinectrl_cb.common.function_code;
  571.     SHOW_ERR();
  572.     sys_err = TRUE;                         /* check return code and handle */
  573.                                             /* any errors                   */
  574.   }
  575. }
  576.  
  577. void
  578. comconnect(handle, connecttype, connecttimeout1, connecttimeout2)
  579. /* This subroutine will issue com_connect to establish connection           */
  580.  
  581. unsigned short handle;                      /* device handle                */
  582. unsigned short connecttype;                 /* type of connection           */
  583. unsigned short connecttimeout1;             /* timeout values to set amount */
  584. unsigned short connecttimeout2;             /* of time to wait before
  585.                                             /* "hanging up"                 */
  586.  
  587. {
  588.   clear_vcb();                              /* zero out the control block   */
  589.  
  590.   vcb.connect_cb.common.com_dev_handle = handle;
  591.   vcb.connect_cb.common.function_code = COM_CONNECT;
  592.   vcb.connect_cb.connect_type = connecttype;
  593.   vcb.connect_cb.connect_timeout_1 = connecttimeout1;
  594.   vcb.connect_cb.connect_timeout_2 = connecttimeout2;
  595.                                             /* fill in control block with   */
  596.                                             /* function code and parameters */
  597.   ACDI(vcbptr);                             /* issue the verb               */
  598.  
  599.   if (vcb.connect_cb.common.return_code != AA_OK)  {
  600.     ret_code = vcb.connect_cb.common.return_code;
  601.     func_code = vcb.connect_cb.common.function_code;
  602.     SHOW_ERR();
  603.     sys_err = TRUE;                         /* check return code and handle */
  604.                                             /* any errors                   */
  605.   }
  606. }
  607.  
  608. void
  609. comsettimeouts(handle, rdtimoutblock, rdtimoutchar, wrttimout)
  610. /* This subroutine will issue com_set_timeouts to set the timeout values    */
  611. /* for read and write.                                                      */
  612.  
  613. unsigned short handle;                      /* device handle                */
  614. unsigned short rdtimoutblock;               /* block mode read timeout      */
  615. unsigned short rdtimoutchar;                /* char mode read timeout       */
  616. unsigned short wrttimout;                   /* write timeout                */
  617.  
  618. {
  619.   clear_vcb();                              /* zero out the control block   */
  620.  
  621.   vcb.settimeouts_cb.common.com_dev_handle = handle;
  622.   vcb.settimeouts_cb.common.function_code = COM_SET_TIMEOUTS;
  623.   vcb.settimeouts_cb.read_timeout_block = rdtimoutblock;
  624.   vcb.settimeouts_cb.read_timeout_char = rdtimoutchar;
  625.   vcb.settimeouts_cb.write_timeout = wrttimout;
  626.                                             /* fill in control block with   */
  627.                                             /* function code and parameters */
  628.   ACDI(vcbptr);                             /* issue the verb               */
  629.  
  630.   if (vcb.settimeouts_cb.common.return_code != AA_OK)  {
  631.     ret_code = vcb.settimeouts_cb.common.return_code;
  632.     func_code = vcb.settimeouts_cb.common.function_code;
  633.     SHOW_ERR();
  634.     sys_err = TRUE;                         /* check return code and handle */
  635.                                             /* any errors                   */
  636.   }
  637. }
  638.  
  639. void
  640. comreadevent(handle, semhandle, bufferaddress, masksaddress, vcbaddress)
  641. /* This subroutine will issue comreadevent verb so that the calling process */
  642. /* can wait on the semaphore to be cleared.                                 */
  643.  
  644. unsigned short handle;                      /* device handle                */
  645. unsigned long semhandle;                    /* sem handle-identifier or addr*/
  646. struct event_struct *bufferaddress;         /* event_struct buffer address  */
  647. struct masks *masksaddress;                 /* masks structure address      */
  648. unsigned far *vcbaddress;                   /* control block address        */
  649.  
  650. {
  651.   readevent_cb.common.com_dev_handle = handle;
  652.   readevent_cb.common.function_code = COM_READ_EVENT;
  653.   readevent_cb.sem_handle = semhandle;
  654.   readevent_cb.buffer_address = bufferaddress;
  655.   readevent_cb.event_masks = masksaddress;
  656.                                             /* fill in control block with   */
  657.                                             /* function code and parameters */
  658.   ACDI(vcbaddress);                         /* issue the verb               */
  659.  
  660.   if (readevent_cb.common.return_code != AA_OK) {
  661.     ret_code = readevent_cb.common.return_code;
  662.     func_code = readevent_cb.common.function_code;
  663.     SHOW_ERR();
  664.     sys_err = TRUE;                         /* check return code and handle */
  665.                                             /* any errors                   */
  666.   }
  667. }
  668.  
  669. void
  670. comreadcharstring(handle, bytesfreed, readbytesneeded, initialwait)
  671. /* This subroutine will issue com_read_char_string verb to enable program   */
  672. /* to read data that has been received over the line and is put in the      */
  673. /* input buffer.                                                            */
  674.  
  675. unsigned short handle;                      /* device handle                */
  676. unsigned short bytesfreed;                  /* num. of bytes read last time */
  677. unsigned short readbytesneeded;             /* return when these many read  */
  678.                                             /* bytes are available          */
  679. unsigned short initialwait;                 /* initial delay for timeout    */
  680.  
  681. {
  682.   clear_vcb();                              /* zero out the control block   */
  683.  
  684.   vcb.readcharstring_cb.common.com_dev_handle = handle;
  685.   vcb.readcharstring_cb.common.function_code = COM_READ_CHAR_STRING;
  686.   vcb.readcharstring_cb.bytes_freed = bytesfreed;
  687.   vcb.readcharstring_cb.read_bytes_needed = readbytesneeded;
  688.   vcb.readcharstring_cb.initial_wait = initialwait;
  689.                                             /* fill in control block with   */
  690.                                             /* function code and parameters */
  691.   ACDI(vcbptr);                             /* issue the verb               */
  692.  
  693.  
  694.   if (vcb.readcharstring_cb.common.return_code != AA_OK) {
  695.                                             /* if return is not good....    */
  696.     ret_code = vcb.readcharstring_cb.common.return_code;
  697.     func_code = vcb.readcharstring_cb.common.function_code;
  698.                                             /* copy return and func. codes  */
  699.     if ( ret_code == 0x54)                  /* if ret_code is 54H - timeout */
  700.       return;                               /* occured - just return        */
  701.     else {                                  /* otherwise an error occured - */
  702.       SHOW_ERR();                           /* so handle the error          */
  703.       sys_err = TRUE;
  704.       return;
  705.     }
  706.   }
  707.   else                                      /* if return is good - data is  */
  708.     ret_code = AA_OK;                       /* received; set ret_code to ok */
  709. }
  710.  
  711. void
  712. comdisconnect(handle)
  713. /* This subroutine will issue com_disconnect to break the connection.       */
  714.  
  715. unsigned short handle;                      /* device handle                */
  716.  
  717. {
  718.   clear_vcb();                              /* zero out the control block   */
  719.  
  720.   vcb.disconnect_cb.common.com_dev_handle = handle;
  721.   vcb.disconnect_cb.common.function_code = COM_DISCONNECT;
  722.                                             /* fill in control block with   */
  723.                                             /* function code and parameters */
  724.   ACDI(vcbptr);                             /* issue the verb               */
  725.  
  726.  
  727.   if (vcb.disconnect_cb.common.return_code != AA_OK)  {
  728.     ret_code = vcb.disconnect_cb.common.return_code;
  729.     func_code = vcb.disconnect_cb.common.function_code;
  730.     SHOW_ERR();
  731.     sys_err = TRUE;                         /* check return code and handle */
  732.                                             /* any errors                   */
  733.   }
  734. }
  735.  
  736. void
  737. comclose(handle)
  738. /* This subroutine will issue com_close to close the communication device.  */
  739.  
  740. unsigned short handle;                      /* device handle                */
  741.  
  742. {
  743.   clear_vcb();                              /* zero out the control block   */
  744.  
  745.   vcb.close_cb.common.com_dev_handle = handle;
  746.   vcb.close_cb.common.function_code = COM_CLOSE;
  747.                                             /* fill in control block with   */
  748.                                             /* function code and parameters */
  749.   ACDI(vcbptr);                             /* issue the verb               */
  750.  
  751.   if (vcb.close_cb.common.return_code != AA_OK)  {
  752.     ret_code = vcb.close_cb.common.return_code;
  753.     func_code = vcb.close_cb.common.function_code;
  754.     SHOW_ERR();
  755.     sys_err = TRUE;                         /* check return code and handle */
  756.                                             /* any errors                   */
  757.   }
  758. }
  759.  
  760. /*****************************************************************************/
  761. /*  OS/2 Related Functions                                                   */
  762. /*****************************************************************************/
  763.  
  764. void
  765. set_sem(sem_handle)
  766. /* This function sets semaphore.                                             */
  767.  
  768. unsigned long sem_handle;
  769.  
  770. {
  771.   dos_rc = DOSSEMSET(sem_handle);
  772.   if (dos_rc != AA_OK) {
  773.     SHOW_DOS_ERR();
  774.     sys_err = TRUE;
  775.   }
  776. }
  777.  
  778. void
  779. sem_wait(sem_handle)
  780. /* This function waits till the semaphore is cleared.                       */
  781.  
  782. unsigned long sem_handle;
  783.  
  784. {
  785. unsigned long inf_wait_timeout = -1;        /* cause thread to be blocked   */
  786.                                             /* indef. till sem. is cleared  */
  787.   dos_rc = DOSSEMWAIT(sem_handle,inf_wait_timeout);
  788.   if (dos_rc != AA_OK) {
  789.     SHOW_DOS_ERR();
  790.     sys_err = TRUE;
  791.   }
  792. }
  793.  
  794. void
  795. clear_screen()
  796. /* This function clears the screen to prepare to display message received.   */
  797. /* Writes null on the whole screen                                           */
  798.  
  799. {
  800. #define SCREENSIZE 2000
  801.  
  802. char null = 0x20;
  803.  
  804.   dos_vio_rc = VIOWRTNCHAR (&null, SCREENSIZE, 0, 0, 0);
  805.                                             /* vio call to write on screen  */
  806.   if (dos_vio_rc != 0)  {
  807.     SHOW_VIO_ERR();
  808.     sys_err = TRUE;
  809.   }                                         /* handle any error             */
  810. }
  811.  
  812. void
  813. set_crsr_pos(row_no, col_no)
  814. /* This function sets the cursor position at the specified row and column.  */
  815. int row_no, col_no;
  816.  
  817. {
  818.   dos_vio_rc = VIOSETCURPOS(row_no, col_no,0);
  819.                                             /* vio call to set cursor pos   */
  820.   if (dos_vio_rc != 0)  {
  821.     SHOW_VIO_ERR();
  822.     sys_err = TRUE;                         /* handle any error             */
  823.   }
  824. }
  825.  
  826.