home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 3 / FREEWARE.BIN / towns_os / whisper / source / rsb.c < prev    next >
Text File  |  1980-01-02  |  20KB  |  898 lines

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <string.h>
  4. #include    <time.h>
  5. #include    "graphic.h"
  6. #include    "event.h"
  7. #include    "rsblib.h"
  8. #include    "coldef.h"
  9.  
  10. #define    TRUE    1
  11. #define    FALSE    0
  12. #define    ERR    (-1)
  13.  
  14. #define    BUF_SIZ    (1024*5)    /* looking RSBLIB.ASM */
  15.  
  16. #define    UCHAR    unsigned char
  17.  
  18. char    *macget();
  19. char    *MSG_getstr();
  20.  
  21. extern int    port;        /* RSBLIB.ASM Look! */
  22. extern int    rsb_ovr_flg;
  23.        int    bps=ERR;
  24. static RSB_PARA    para;
  25.  
  26. void    RSB_end(void)
  27. {
  28.     if ( port != ERR )
  29.     RSB_close(port);
  30.     port = ERR;
  31. }
  32.  
  33. void    RSB_init(int pt)
  34. {
  35.     RSB_end();
  36.     port = pt;
  37.     RSB_rdpara(port,(char *)¶);
  38.     RSB_open(port);
  39.     bps = para.baud;
  40.     rsb_ovr_flg = FALSE;
  41. }
  42.  
  43. int    RSB_reopen(int md,int bp)
  44. {
  45.     int        er;
  46.  
  47.     para.mode = md;        /*  通信モード           */
  48.     para.baud = bp;        /*  ボーレート          */
  49.     para.rbuf = 0;        /*  受信バッファアドレス */
  50.     para.stime = 500;        /* 送信タイムアウト     */
  51.     para.rtime = 500;        /* 受信タイムアウト     */
  52.     para.rinfbuf = 0;        /*  受信通知アドレス     */
  53.     para.extmode = 0;        /*  拡張モード           */
  54.     para.xon = 0x11;        /*  XONコード            */
  55.     para.xoff = 0x13;        /*  XOFFコード           */
  56.  
  57.     RSB_dtr(port,1);        /* DTR信号の保持 */
  58.     RSB_close(port);
  59.     rsb_ovr_flg = FALSE;
  60.  
  61.     if ( (er = RSB_setpara(port,(char *)¶)) == 0 )
  62.     er = RSB_open(port);
  63.  
  64.     return er;
  65. }
  66.  
  67. void    RSB_get_para(int *pt,int *md,int *bp)
  68. {
  69.     *pt = port;
  70.     *md = para.mode;
  71.     *bp = para.baud;
  72. }
  73.  
  74. void    RSB_putc(int ch)
  75. {
  76.     unsigned st;
  77.  
  78.     RSB_send(port,ch,&st);
  79. }
  80.  
  81. int    RSB_chk(void)
  82. {
  83.     int     len;
  84.  
  85.     RSB_read(port,&len);
  86.     if ( rsb_ovr_flg == FALSE ) {
  87.     if ( len >= (BUF_SIZ-512) ) {
  88.         rsb_ovr_flg = TRUE;
  89.         RSB_ctrl(port,0x02);
  90.         DSP_string("\x1B\xED",370,4,COL_RED,COL_WHIS);
  91.     }
  92.     } else {
  93.         if ( len <= 512 ) {
  94.         rsb_ovr_flg = FALSE;
  95.         RSB_ctrl(port,0x22);
  96.         DSP_string(" ",370,4,COL_RED,COL_WHIS);
  97.     }
  98.     }
  99.     return len;
  100. }
  101.  
  102. int    RSB_getc(void)
  103. {
  104.     unsigned st;
  105.     int      ch;
  106.  
  107. RETRY:
  108.     RSB_receive(port,&ch,&st);
  109.     if ( ch == 0x05 ) {
  110.     BP_Term_ENQ();
  111.     goto RETRY;
  112.     } else if ( ch == 0x10 ) {
  113.     BP_DLE_Seen();
  114.     goto RETRY;
  115.     }
  116.     return ch;
  117. }
  118.  
  119. void    RSB_break_out(void)
  120. {
  121.     RSB_break(port,macval("_BREAK_TIME"));
  122. }
  123.  
  124. /*****************************************************
  125.  
  126.     RS232C MODE SETUP WIND
  127.  
  128. ******************************************************/
  129. #define    MENU_X        100
  130. #define    MENU_Y        70
  131. #define    MENU_COL    COL_JACK
  132. #define    MENU_CHR    COL_CHR
  133. #define    HIT_COL        COL_JACK2
  134. #define    TTL_COL        COL_CHR
  135.  
  136. static int  rsb_event=ERR;
  137. static char atcmd[3][80];
  138. static struct {
  139.     int     no;
  140.     int     max;
  141.     char    *ttl;
  142.     char    *sub[8];
  143.     } menu[]={
  144. { 0,5,"ポ-ト番号","   0  ","   1  ","   2  ","   3  ","   4  " },
  145. { 0,6,"  速  度  ","  300 ","  600 "," 1200 "," 2400 "," 4800 "," 9600 " },
  146. { 0,2," ビット長 ","7 ビット","8 ビット" },
  147. { 0,3,"パリティ-"," なし "," 奇数 "," 偶数 " },
  148. { 0,2," ストップビット","1 ビット","2 ビット" },
  149. { 0,2," Xフロ-制御 "," なし "," あり " },
  150. { 0,4,"ブレ-ク時間 "," 500ms"," 1sec ","1.5sec"," 2sec " },
  151. { 0,6," 改行待ち "," なし "," 100ms"," 200ms"," 300ms"," 400ms"," 500ms" },
  152. { 0,6," ログ行数 "," 1000 "," 2000 "," 3000 "," 4000 "," 5000 ","無制限" },
  153. { 0,3,"モデムの種類"," 1200 "," 2400 "," MNP5 " },
  154. { 0,2,"電話の種類"," パルス ","  ト-ン " },
  155. { 0,0,"ダイアルコマンド",atcmd[0] },
  156. { 0,0,"モデムコマンド1",atcmd[1] },
  157. { 0,0,"モデムコマンド2",atcmd[2] },
  158.     { 0,0,NULL,NULL }
  159.     };
  160. static char *help_msg[]={
  161.     "HELP_PORT", "HELP_BAUD",  "HELP_BIT",   "HELP_PARTY",
  162.     "HELP_STOP", "HELP_XFLOW", "HELP_BREAK", "HELP_WAIT",
  163.     "HELP_LOG",  "HELP_MODEM", "HELP_DIAL",  "HELP_ATCMD1",
  164.     "HELP_ATCMD2","HELP_ATCMD3" };
  165.  
  166. void    RSB_set_event(EVENT *ep)
  167. {
  168.     switch(ep->now) {
  169.     case EVT_CLIP_MOS:
  170.     EVT_clip_on(ep);
  171.     case EVT_ON_MOS:
  172.     DSP_mos(1);
  173.     break;
  174.  
  175.     case EVT_SELECT_MOS:
  176.     EVT_clip_off(ep);
  177.     DSP_mos(0);
  178.     rsb_event = ep->no;
  179.     break;
  180.  
  181.     case EVT_DOLACK_MOS:
  182.     ep->now = EVT_NON;
  183.     case EVT_MOVE_MOS:
  184.     EVT_clip_off(ep);
  185.     case EVT_OFF_MOS:
  186.     DSP_mos(0);
  187.     break;
  188.     }
  189. }
  190. void    RSB_dsp_menu(int no)
  191. {
  192.     int     j,x1,y1,x2,y2;
  193.  
  194.     y1 = MENU_Y+4+no*20;
  195.     y2 = y1+17;
  196.  
  197.     for ( j = 0 ; j < menu[no].max ; j++ ) {
  198.     x1 = MENU_X+96+j*56;
  199.     x2 = x1+strlen(menu[no].sub[j])*8+4;
  200.     DSP_box(x1,y1,x2,y2,
  201.         COL_LINE,(menu[no].no == j ? HIT_COL:MENU_COL));
  202.     wrtstr(menu[no].sub[j],page_ofs,(x1+2)/2 + (y1+1) * 512,
  203.            col_cnv[MENU_CHR],
  204.         col_cnv[menu[no].no == j ? HIT_COL:MENU_COL],
  205.         16);
  206.     }
  207. }
  208. void    RSB_dsp_string(int no)
  209. {
  210.     char    tmp[80];
  211.  
  212.     sprintf(tmp,"%-41.41s",menu[no].sub[0]);
  213.     wrtstr(tmp,page_ofs,
  214.     (MENU_X+98)/2+(MENU_Y+5+no*20)*512,
  215.     col_cnv[MENU_CHR],col_cnv[MENU_COL],16);
  216. }
  217. void    RSB_mode_set(void)
  218. {
  219.     int     i,j,mode;
  220.     BLOCK   *vram_save;
  221.     char    *p;
  222.     char    tmp[80];
  223.  
  224.     menu[0].no = port;
  225.     menu[1].no = para.baud;
  226.     menu[2].no = ((para.mode & 0x01) == 0 ? 0 : 1);
  227.     menu[3].no = ((para.mode & 0x02) == 0 ? 0:((para.mode & 0x04) == 0 ? 1:2));
  228.     menu[4].no = ((para.mode & 0x08) == 0 ? 0 : 1);
  229.     menu[5].no = ((para.mode & 0x10) == 0 ? 0 : 1);
  230.     menu[6].no = macval("_BREAK_TIME") / 500 - 1;
  231.     menu[7].no = macval("_CRWAIT") / 50;
  232.     menu[8].no = macval("_LOGMAX") / 1000 - 1;
  233.     menu[9].no = macval("_MODEM");
  234.     menu[10].no = macval("_POHN");
  235.     strcpy(atcmd[0],macget("_ATCMD1"));
  236.     strcpy(atcmd[1],macget("_ATCMD2"));
  237.     strcpy(atcmd[2],macget("_ATCMD3"));
  238.  
  239.     MOS_disp(FALSE);
  240.     MOS_push(MENU_X,MENU_Y,MENU_X+432,MENU_Y+312);
  241.     vram_save = DSP_push_vram(MENU_X,MENU_Y,MENU_X+432,MENU_Y+312);
  242.     DSP_rbox(MENU_X,MENU_Y,MENU_X+432,MENU_Y+312,7,8,MENU_COL);
  243.     MOS_setpos(MENU_X+432/2,MENU_Y+312/2);
  244.  
  245.     for ( i = 0 ; menu[i].ttl != NULL ; i++ ) {
  246.     EVT_big_sw(MENU_X+4,MENU_Y+4+i*20,menu[i].ttl,
  247.         TTL_COL,MENU_COL,600,RSB_set_event,i*10+9);
  248.     for ( j = 0 ; j < menu[i].max ; j++ ) {
  249.         EVT_big_sw(MENU_X+96+j*56,MENU_Y+4+i*20,menu[i].sub[j],
  250.             MENU_CHR,(menu[i].no == j ? HIT_COL:MENU_COL),
  251.             600,RSB_set_event,i*10+j);
  252.     }
  253.     if ( j == 0 ) {
  254.         sprintf(tmp,"%-41.41s",menu[i].sub[0]);
  255.         EVT_big_sw(MENU_X+96,MENU_Y+4+i*20,tmp,
  256.             MENU_CHR,MENU_COL,600,RSB_set_event,i*10);
  257.     }
  258.     }
  259.     EVT_big_sw(MENU_X+160,MENU_Y+290," 設定 ",
  260.         TTL_COL,MENU_COL,600,RSB_set_event,999);
  261.     EVT_big_sw(MENU_X+220,MENU_Y+290," 取消 ",
  262.         TTL_COL,MENU_COL,600,RSB_set_event,998);
  263.     MOS_disp(TRUE);
  264.  
  265.     for ( ; ; ) {
  266.     rsb_event = ERR;
  267.     EVT_loop(600);
  268.     if ( rsb_event == ERR )
  269.         continue;
  270.  
  271.     if ( rsb_event == 999 || rsb_event == 998 )
  272.         break;
  273.  
  274.     i = rsb_event / 10;
  275.     j = rsb_event % 10;
  276.  
  277.     if ( i >= 0 && i <= 8 ) {
  278.         if ( j == 9 ) {
  279.         MSG_wind(help_msg[i]);
  280.         } else if ( menu[i].no != j ) {
  281.         menu[i].no = j;
  282.         MOS_disp(FALSE);
  283.         RSB_dsp_menu(i);
  284.         MOS_disp(TRUE);
  285.         }
  286.  
  287.     } else if ( i == 9 || i == 10 ) {
  288.         if ( j == 9 ) {
  289.         MSG_wind(help_msg[i]);
  290.         } else {
  291.         menu[i].no = j;
  292.         macvalset("BPS",menu[1].no);
  293.         macvalset("MODEM",menu[9].no);
  294.         macvalset("POHN",menu[10].no);
  295.         MSG_wind("MODEM_CHENG");
  296.         strcpy(atcmd[0],macget("ATCMD1"));
  297.         strcpy(atcmd[1],macget("ATCMD2"));
  298.         strcpy(atcmd[2],macget("ATCMD3"));
  299.         MOS_disp(FALSE);
  300.         RSB_dsp_menu(i);
  301.         menu[1].no = macval("BPS");
  302.         RSB_dsp_menu(1);    /* Baud Cheng ? */
  303.         RSB_dsp_string(11);
  304.         RSB_dsp_string(12);
  305.         RSB_dsp_string(13);
  306.         MOS_disp(TRUE);
  307.         }
  308.  
  309.     } else if ( i >= 11 && i <= 13 ) {
  310.         if ( j == 9 ) {
  311.         MSG_wind(help_msg[i]);
  312.         } else {
  313.         MOS_disp(FALSE);
  314.         p = menu[i].sub[0];
  315.         menu[i].sub[0] = "";
  316.         RSB_dsp_string(i);
  317.         strcpy( p,MSG_getstr(MENU_X+98,MENU_Y+5+i*20,
  318.                      MENU_CHR,MENU_COL,p,40));
  319.         menu[i].sub[0] = p;
  320.         RSB_dsp_string(i);
  321.         MOS_disp(TRUE);
  322.         }
  323.     }
  324.     }
  325.  
  326.     EVT_level_free(600);
  327.     MOS_disp(FALSE);
  328.     DSP_pop_vram(vram_save);
  329.     MOS_push(ERR,ERR,ERR,ERR);
  330.  
  331.     if ( rsb_event != 999 )
  332.     return;
  333.  
  334.     mode = 0;
  335.     if ( menu[2].no != 0 ) mode |= 0x01;
  336.     if ( menu[3].no == 1 ) mode |= 0x02;
  337.     if ( menu[3].no == 2 ) mode |= 0x06;
  338.     if ( menu[4].no != 0 ) mode |= 0x08;
  339.     if ( menu[5].no != 0 ) mode |= 0x10;
  340.  
  341.     if ( menu[0].no != port )
  342.     RSB_init(menu[0].no);
  343.  
  344.     RSB_reopen(mode,menu[1].no);
  345.     macvalset("_BREAK_TIME",(menu[6].no + 1) * 500);
  346.     macvalset("_CRWAIT",menu[7].no * 50);
  347.     macvalset("_LOGMAX",(menu[8].no + 1) * 1000);
  348.     macvalset("_MODEM",menu[9].no);
  349.     macvalset("_POHN",menu[10].no);
  350.     macset("_ATCMD1",atcmd[0]);
  351.     macset("_ATCMD2",atcmd[1]);
  352.     macset("_ATCMD3",atcmd[2]);
  353. }
  354. /**********************************************************
  355.  
  356.     UP/DOWN LOAD WIND DISPLAY
  357.  
  358. ***********************************************************/
  359. #define    DSP_X    128
  360. #define    DSP_Y    124
  361. #define    DSP_COL    COL_JACK
  362.  
  363. #define    SIZ_X1    136
  364. #define    SIZ_Y1    324
  365. #define    SIZ_X2    478
  366. #define    SIZ_Y2    (SIZ_Y1+11)
  367.  
  368. #define    ABORT_X    484
  369. #define    ABORT_Y    324
  370.  
  371. #define STMsg        0
  372. #define STFile       1
  373. #define STUpDow      2
  374. #define STType       3
  375. #define STComSent    4
  376. #define STDataSent   5
  377. #define STErrSent    6
  378. #define STPacSent    7
  379. #define STComRate    8
  380. #define STDataRate   9
  381. #define STElapsed    10
  382.  
  383. #define STComRead    20
  384. #define STDataRead   21
  385. #define STErrRead    22
  386. #define STPacRead    23
  387. #define STUplSize    24
  388. #define STDowSize    25
  389. #define STUplRem     26
  390. #define STDowRem     27
  391. #define STRemTime    28
  392.  
  393. static int    st_event=FALSE;
  394. static BLOCK    *save=NULL;
  395. static int    grp_size=0;
  396.  
  397. void    cputs(char *str,int x,int y,int col)
  398. {
  399.     MOS_disp(FALSE);
  400.     wrtstr(str,page_ofs,x*4+y*16*512,
  401.         col_cnv[col],col_cnv[DSP_COL],16);
  402.     MOS_disp(TRUE);
  403. }
  404.  
  405. int  ST_Check_Abort()
  406. {
  407.     EVT_loop(600);
  408.  
  409.     if ( st_event != FALSE )
  410.     return 1;
  411.     else
  412.     return 0;
  413. }
  414.  
  415. void    ST_Yes_or_No(arg,yn)
  416. char    *arg;
  417. char    *yn;
  418. {
  419.     cputs(arg,17,20,COL_RED);
  420.     *yn = getch();
  421. }
  422.  
  423. void    ST_event(EVENT *ep)
  424. {
  425.     switch(ep->now) {
  426.     case EVT_CLIP_MOS:
  427.     EVT_clip_on(ep);
  428.     case EVT_ON_MOS:
  429.     DSP_mos(1);
  430.     break;
  431.  
  432.     case EVT_SELECT_MOS:
  433.     EVT_clip_off(ep);
  434.     DSP_mos(2);
  435.     st_event = TRUE;
  436.     break;
  437.  
  438.     case EVT_DOLACK_MOS:
  439.     ep->now = EVT_NON;
  440.     case EVT_MOVE_MOS:
  441.     EVT_clip_off(ep);
  442.     case EVT_OFF_MOS:
  443.     DSP_mos(2);
  444.     break;
  445.     }
  446. }
  447.  
  448. void    ST_Initialize(int no)
  449. {
  450.     int    i;
  451.     static char *menu[]={
  452.     "Msg     ","File    ","UpDow   ","Type    ","ComSent ",
  453.     "DataSent","ErrSent ","PacSent ","ComRate ","DataRate",
  454.     "Elapsed ",
  455.     "        ","        ","ComRead ","DataRead","ErrRead ",
  456.     "PacRead ","UplSize ","DowSize ","UplRem  ","DowRem  ",
  457.     "RemTime " };
  458.     static char *ttl[]={
  459.     "           <<< B Plus Protocol(SM) >>> ",
  460.     "           <<< B Plus Protocol(SM) t >>>  ",
  461.     "            <<< Nomal Text Upload >>>   ",
  462.     "           <<< X-Modem Protocol Up >>>  ",
  463.     "          <<< X-Modem Protocol Down >>> "
  464.     };
  465.  
  466.     if ( save != NULL )
  467.     return;
  468.  
  469.     MOS_disp(FALSE);
  470.     save = DSP_push_vram(DSP_X,DSP_Y,DSP_X+50*8+8,DSP_Y+13*16+8);
  471.     DSP_rbox(DSP_X,DSP_Y,DSP_X+50*8+8,DSP_Y+13*16+8,7,8,DSP_COL);
  472.  
  473.     cputs(ttl[no],17,8,COL_RED);
  474.  
  475.     for ( i = 0 ; i < 22 ; i++ )
  476.     cputs(menu[i],17+(i/11)*24,9+(i%11),COL_JACK2);
  477.  
  478.     EVT_level_free(600);
  479.     EVT_sw(ABORT_X,ABORT_Y,"ABORT",COL_RED,DSP_COL,600,ST_event,0);
  480.     st_event = FALSE;
  481.  
  482.     macvalset("PROTOCOL",no);
  483.     MSG_wind("updown_start");
  484.     grp_size = 0;
  485.  
  486.     DSP_mos(2);
  487.     MOS_disp(TRUE);
  488. }
  489.  
  490. void    ST_Terminate()
  491. {
  492.     if ( save != NULL ) {
  493.     MOS_disp(FALSE);
  494.     EVT_level_free(600);
  495.     DSP_pop_vram(save);
  496.     save = NULL;
  497.     MSG_wind("updown_end");
  498.     DSP_mos(0);
  499.     MOS_disp(TRUE);
  500.     }
  501. }
  502.  
  503. void    ST_Display_String(no,str)
  504. int    no;
  505. char    *str;
  506. {
  507.     if ( save != NULL )
  508.     cputs(str,27+(no/18)*24,9+(no%18),COL_JACK2);
  509. }
  510.  
  511. void    ST_Display_Value(no,val)
  512. int    no;
  513. long    val;
  514. {
  515.     int      n;
  516.     char     tmp[20];
  517.  
  518.     if ( no == STElapsed || no == STRemTime ) {
  519.     if ( val >= 3600 )
  520.         sprintf(tmp,"%2ld:%02ld:%02ld",val/3600,(val%3600)/60,val%60);
  521.     else if ( val >= 60 )
  522.         sprintf(tmp,"   %2ld:%02ld",val/60,val%60);
  523.     else
  524.         sprintf(tmp,"      %2ld",val);
  525.  
  526.     } else if ( no == STUplRem || no == STDowRem ) {
  527.     if ( grp_size == 0 ) {
  528.         grp_size = val;
  529.         DSP_xline(SIZ_X1,SIZ_Y1,SIZ_X2,SIZ_Y2,COL_LINE,0);
  530.  
  531.     } else {
  532.         n = (grp_size - val) * (SIZ_X2-SIZ_X1-2) / grp_size;
  533.         DSP_xline(SIZ_X1+1,SIZ_Y1+1,SIZ_X1+n+1,SIZ_Y2-1,COL_CHR,0);
  534.     }
  535.     sprintf(tmp,"%8ld",val);
  536.  
  537.     } else
  538.     sprintf(tmp,"%8ld",val);
  539.  
  540.     ST_Display_String(no,tmp);
  541. }
  542.  
  543. int    FILE_upload(char *file)
  544. {
  545.     FILE    *fp;
  546.     int     ch,ct=0;
  547.     int     file_size;
  548.     int     up_size;
  549.     int     byte_sec;
  550.     int     total_time;
  551.     clock_t st;
  552.  
  553.     if( (fp = fopen(file,"rb")) == NULL )
  554.     return ERR;
  555.     fseek(fp,0L,SEEK_END);
  556.     file_size = ftell(fp);
  557.     rewind(fp);
  558.  
  559.     ST_Initialize(2);
  560.     ST_Display_String (STFile, file);
  561.     ST_Display_String (STUpDow, "Uploading ");
  562.     ST_Display_String (STType, "ASCII");
  563.     ST_Display_Value (STUplRem, file_size);
  564.  
  565.     st = clock();
  566.     up_size = 0;
  567.     byte_sec = 200;
  568.  
  569.     while ( (ch = getc(fp)) != EOF ) {
  570.     if ( ch == '\x1A' ) {
  571.         ST_Display_String (STMsg, "*** Transfer Complete ***");
  572.         break;
  573.  
  574.     } else if ( ch == '\x0D' ) {
  575.         RSB_putc(ch);
  576.         if ( (total_time = (clock() - st) / CLK_TCK) > 0 ) {
  577.         byte_sec = up_size / total_time;
  578.         ST_Display_Value (STDataRate, byte_sec);
  579.         if ( byte_sec > 0 )
  580.             ST_Display_Value (STRemTime, file_size / byte_sec);
  581.         ST_Display_Value (STUplSize, up_size);
  582.         ST_Display_Value (STUplRem, file_size);
  583.         ST_Display_Value (STElapsed, total_time);
  584.         }
  585.         Soft_time(macval("_CRWAIT") * 100);
  586.  
  587.     } else if ( ch != '\x0A' )
  588.         RSB_putc(ch);
  589.  
  590.     up_size++;
  591.     file_size--;
  592.  
  593.     if ( ++ct >= 50 ) {
  594.         while ( RSB_chk() )
  595.             RSB_getc();
  596.         ct = 0;
  597.     }
  598.  
  599.     if ( ST_Check_Abort() ) {
  600.         ST_Display_String (STMsg, "Aborting download per your request");
  601.         break;
  602.     }
  603.     }
  604.  
  605.     fclose(fp);
  606.  
  607.     for ( ct = 0 ; ct < 50 ; ct++ ) {
  608.         while ( RSB_chk() )
  609.         RSB_getc();
  610.     }
  611.  
  612.     ST_Terminate();
  613.     return FALSE;
  614. }
  615.  
  616. /******************************************
  617.  
  618.     X-Modem
  619.  
  620. *******************************************/
  621.  
  622. #define SOH     '\x01'
  623. #define STX     '\x02'
  624. #define EOT     '\x04'
  625. #define ACK     '\x06'
  626. #define NAK     '\x15'
  627. #define CAN     '\x18'
  628. #define CRC     '\x43'
  629. #define    CR    '\x0D'
  630.  
  631. static void Send(ch)
  632. int    ch;
  633. {
  634.     unsigned int st;
  635.  
  636.     RSB_send(port,ch,&st);
  637. }
  638. static void Send_str(arg,n)
  639. UCHAR    *arg;
  640. int    n;
  641. {
  642.     unsigned int st;
  643.  
  644.     while ( n-- > 0 )
  645.     RSB_send(port,(int)*(arg++),&st);
  646. }
  647. static int Recive()
  648. {
  649.     int    i,ch;
  650.     unsigned int st;
  651.  
  652.     if ( RSB_receive(port,&ch,&st) != 0 )
  653.     return ERR;
  654.     else 
  655.         return (ch & 0xFF);
  656. }
  657. static int  Recive_str(arg,n)
  658. UCHAR    *arg;
  659. int    n;
  660. {
  661.     int        ch;
  662.  
  663.     while ( n-- > 0 ) {
  664.         if ( (ch = Recive()) == ERR )
  665.         return ERR;
  666.     *(arg++) = ch;
  667.     }
  668.     return FALSE;
  669. }
  670. static int Send_blk(fp,bk)
  671. FILE    *fp;
  672. int     bk;
  673. {
  674.     int     i,j;
  675.     UCHAR   head[3];
  676.     UCHAR   *p,tmp[128];
  677.  
  678.     head[0]=SOH;
  679.     head[1]=bk;
  680.     head[2]=255-bk;
  681.  
  682.     if ( (i=fread(tmp,1,128,fp)) <= 0 ) {
  683.     Send(EOT);
  684.         return ERR;
  685.     }
  686.  
  687.     for ( p=tmp+i ; i < 128 ; i++ )
  688.         *(p++)='\0';
  689.     for ( j = i = 0 ; i < 128 ; i++,j &= 0xff )
  690.         j += tmp[i];
  691.  
  692.     Send_str(head,3);
  693.     Send_str(tmp,128);
  694.     Send(j);
  695.  
  696.     return FALSE;
  697. }
  698.  
  699. int    XMODEM_upload(char *file)
  700. {
  701.     int     i,ch,bk,nk,ef,fg,md;
  702.     int     byte_sec,total_time;
  703.     unsigned int ec;
  704.     FILE    *fp;
  705.     long    up_size,file_size;
  706.     clock_t st;
  707.  
  708.     if ( (fp = fopen(file,"rb")) == NULL )
  709.     return ERR;
  710.  
  711.     fseek(fp,0L,2);
  712.     file_size = (long)ftell(fp);
  713.     fseek(fp,0L,0);
  714.  
  715.     ST_Initialize(3);
  716.     ST_Display_String (STFile, file);
  717.     ST_Display_String (STUpDow, "X-Modem Uploading ");
  718.     ST_Display_String (STType, "BINARY");
  719.     ST_Display_Value (STUplRem, file_size);
  720.  
  721.     st = clock();
  722.     byte_sec = 200;
  723.     fg = FALSE;
  724.  
  725.     md = para.mode;
  726.     RSB_reopen(md & 0xEF,para.baud);
  727.  
  728.     for ( bk = 1, nk = 0, ef = FALSE, up_size = 0 ; ; ) {
  729.     if ( ST_Check_Abort() ) {
  730.         ST_Display_String (STMsg, "Aborting download per your request");
  731.         break;
  732.     }
  733.  
  734.     for ( i = 0 ; i < 5 ; i++ ) {
  735.         if ( (ch = Recive()) != ERR ) 
  736.         break;
  737.         ST_Display_Value (STErrSent, i+1);
  738.     }
  739.     if ( i >= 5 ) {
  740.         ST_Display_String (STMsg, "Time over abort..");
  741.         break;
  742.     }
  743.     if ( ch == ACK ) {
  744.         up_size += 128;
  745.         file_size -= 128;
  746.         nk = 0;
  747.         if ( ++bk >= 256 )
  748.         bk = 0;
  749.  
  750.         if ( ef != FALSE ) {
  751.         ST_Display_String (STMsg, "*** Transfer Complete ***");
  752.         break;
  753.         }
  754.  
  755.         if ( Send_blk(fp,bk) != FALSE )
  756.         ef = TRUE;
  757.         else 
  758.         fg = TRUE;
  759.  
  760.     } else if ( ch == NAK ) {
  761.         nk++;
  762.         fseek(fp,up_size,SEEK_SET);
  763.         if ( Send_blk(fp,bk) != FALSE )
  764.         ef = TRUE;
  765.         else 
  766.         fg = TRUE;
  767.  
  768.     } else if ( ch == CAN ) {
  769.         ST_Display_String (STMsg, "Recive Cancel signal");
  770.             break;
  771.         }
  772.  
  773.         if ( nk >= 10 ) {
  774.         ST_Display_String (STMsg, "ReTry over abort !!");
  775.         Send(CAN);
  776.         break;
  777.     }
  778.  
  779.     if ( fg != FALSE && (total_time = (clock() - st) / CLK_TCK) > 0 ) {
  780.         byte_sec = (up_size + 128) / total_time;
  781.         ST_Display_Value (STDataRate, byte_sec);
  782.         if ( byte_sec > 0 )
  783.         ST_Display_Value (STRemTime, file_size / byte_sec);
  784.         ST_Display_Value (STUplSize, up_size + 128);
  785.         ST_Display_Value (STUplRem, file_size);
  786.         ST_Display_Value (STElapsed, total_time);
  787.         fg = FALSE;
  788.     }
  789.     }
  790.  
  791.     RSB_reopen(md,para.baud);
  792.     fclose(fp);
  793.     ST_Terminate();
  794.     return FALSE;
  795. }
  796.  
  797. int    XMODEM_download(char *file)
  798. {
  799.     int     i,j,ch,bk,nk,ef,md;
  800.     unsigned int ec;
  801.     long    size;
  802.     FILE    *fp;
  803.     clock_t st;
  804.     int     byte_sec,total_time;
  805.     UCHAR   tmp[128];
  806.  
  807.     if ( (fp = fopen(file,"wb")) == NULL )
  808.     return ERR;
  809.  
  810.     ST_Initialize(4);
  811.     ST_Display_String (STFile, file);
  812.     ST_Display_String (STUpDow, "X-Modem Downloading ");
  813.     ST_Display_String (STType, "BINARY");
  814.  
  815.     st = clock();
  816.     byte_sec = 200;
  817.  
  818.     md = para.mode;
  819.     RSB_reopen(md & 0xEF,para.baud);
  820.  
  821.     for ( ef = TRUE,bk = 1,size = 0,nk = 0 ; ; ) {
  822.     if ( ST_Check_Abort() ) {
  823.         ST_Display_String (STMsg, "Aborting download per your request");
  824.         break;
  825.     }
  826.  
  827.     for ( i = 0 ; i < 5 ; i++ ) {
  828.         if ( ef == FALSE )
  829.         Send(ACK);
  830.         else
  831.         Send(NAK);
  832.         if ( (ch = Recive()) != ERR ) 
  833.         break;
  834.         ST_Display_Value (STErrSent, i+1);
  835.     }
  836.  
  837.     if ( i >= 5 ) {
  838.         ST_Display_String (STMsg, "Time over abort..");
  839.         break;
  840.     }
  841.  
  842.     if ( ch == EOT ) {
  843.         ST_Display_String (STMsg, "*** Transfer Complete ***");
  844.         Send(ACK);
  845.         break;
  846.  
  847.     } else if ( ch == CAN ) {
  848.         ST_Display_String (STMsg, "Recive Cancel signal");
  849.             break;
  850.  
  851.         } else if ( ch == SOH ) {
  852.             if ( Recive() != bk )
  853.         goto ERROR;
  854.             if ( (255 - Recive()) != bk )
  855.         goto ERROR;
  856.         if ( Recive_str(tmp,128) != FALSE )
  857.         goto ERROR;
  858.         for ( j = i = 0 ; i < 128 ; i++,j &= 0xff )
  859.         j += tmp[i];
  860.         if ( Recive() != j )
  861.         goto ERROR;
  862.  
  863.         fwrite(tmp,1,128,fp);
  864.         size += 128;
  865.  
  866.         ST_Display_Value (STDowSize, size);
  867.  
  868.         if ( (total_time = (clock() - st) / CLK_TCK) > 0 ) {
  869.         byte_sec = (size + 128) / total_time;
  870.             ST_Display_Value (STDataRate, byte_sec);
  871.             ST_Display_Value (STElapsed, total_time);
  872.         }
  873.  
  874.         ef = FALSE;
  875.         nk = 0;
  876.         if ( ++bk >= 256 )
  877.         bk = 0;
  878.         continue;
  879.     }
  880.  
  881. ERROR:
  882.     Recive_str(tmp,128);    /* Dummy */
  883.     ef = TRUE;
  884.     ST_Display_Value (STErrRead, ++nk);
  885.  
  886.         if ( nk >= 10 ) {
  887.         ST_Display_String (STMsg, "ReTry over abort !!");
  888.         Send(CAN);
  889.         break;
  890.     }
  891.     }
  892.  
  893.     RSB_reopen(md,para.baud);
  894.     fclose(fp);
  895.     ST_Terminate();
  896.     return FALSE;
  897. }
  898.