home *** CD-ROM | disk | FTP | other *** search
- /*
- * Modems Module.c
- *
- * This program source code and it's compiled version is
- * Copyright (c) 1991 N. Hawthorn.
- * This program source code and it's compiled version IS NOT IN THE
- * PUBLIC DOMAIN ! Please read the "COPYRIGHT NOTICE / NH" file for details
- * regarding use of this program source code and it's compiled version.
- *
- * This module's name is "modems", it's type is "MOD1", use a resource mover
- * to assign a new number to it, that's why we name our modules !
- *
- * I don't recomend sysops using "no carrier detect". A cable SHOULD be made.
- * However, this module handles even the lame mode. MUBBS will probably hang
- * most of the time, and MUBBS will look real stupid for this, but the code
- * is in. It just tries to hang up in every baud rate ! This is totally LAME !
- *
- *
- * This is where it all starts...
- *
- */
-
- #define INMAIN
-
- #include <SetUpA4.h>
- #include "MUBBS Module.h"
-
- extern Ptr SCCRd : 0x1D8;
- extern Ptr SCCWr : 0x1DC;
-
- /* my globals for this module */
-
- int usercarr[maxport]; /* if TRUE then detect carrier, if not use LAME time out method*/
- char maxbaud[maxport][10]; /* the max baudrate for this modem */
-
- struct SSS{
- char string[64]; /* this is a struct to pass some variables to the modems module */
- int mode; /* the mode to use */
- };
-
-
- pascal void main (mode1,G1,P1)
- int mode1;
- struct GS *G1;
- Ptr P1;
- {
- Handle temph;
- float version = 0.5; /* what version of MUBBS you are compatable with IE: .5 and above */
- RememberA0(); SetUpA4(); /* This sets up the A4 register to access our globals */
- asm { _RecoverHandle }; asm {move.l a0,temph}; HLock(temph); /* locks our module, do this ! */
-
- G=G1; /* This MUST be the first thing you do in main only, it sets up the struct globals */
- mode[u]=mode1; /* set up our mode so that you can read it anywhere */
-
- switch (mode[u]) { /* any un-handled modes return error from this module */
-
- case 3:
- modem(P1); /* we need our globals, so we LOCK and stay that way */
- G->moduleresult=0;
- goto skip1; /* DON'T UNLOCK */
- break;
- case 98: /* NOTE that this DOESN'T unlock */
- versionck(version); /* just return after this call, don't modify anything */
- goto skip1; /* DON'T UNLOCK */
- break;
- case 0:
- strcpy (G->programmer,"N Hawthorn"); /* show the programmer's name up to 20 chars*/
- setupall(); /* set up the modem globals */
- G->moduleresult=99; /* we put 99 here because we need to be called to CLOSE */
- goto skip1; /* DON'T UNLOCK */
- break;
- case 1:
- G->moduleresult=0; /* bye bye call, UNLOCK NOW */
- break;
- default:
- G->moduleresult=1; /* return bad code */
- };
-
- HUnlock(temph); /* unlocks this module, do this ! */
-
- skip1:
- RestoreA4(); /* call this when you are all done */
- }
-
-
- modem(S)
- struct SSS *S;
- {
-
- switch (S->mode){
- case 0:
- connect(S);
- break;
- case 1:
- reset(S);
- break;
- case 2:
- hangup(S);
- break;
- }
- }
-
-
- connect(S)
- struct SSS *S;
- { /* mode = 0 for a CONNECT condition, ATA was already sent, figure out what baud */
-
- while (TRUE){ /* NUMBERS ARE FOR A USR HST MODEM */
- if (strcmp(S->string,"14\x0D") == 0) { /* result code */
- strcpy(G->userbaud[u],"19200"); /* set the baud rate to 19200 */
- break; /* YOU BETTER HAVE HARDWARE HANDSHAKING FOR THIS BAUD RATE ! */
- }
- if (strcmp(S->string,"13\x0D") == 0) { /* result code */
- strcpy(G->userbaud[u],"9600"); /* set the baud rate to 9600 */
- break;
- }
- if (strcmp(S->string,"10\x0D") == 0) { /* result code */
- strcpy(G->userbaud[u],"2400"); /* set the baud rate to 2400 */
- break;
- }
- if (strcmp(S->string,"5\x0D") == 0) {
- strcpy(G->userbaud[u],"1200"); /* set the baud rate to 1200 */
- break;
- }
- if (strcmp(S->string,"1\x0D") == 0) {
- strcpy(G->userbaud[u],"300"); /* set the baud rate to 300 */
- break;
- }
- S->mode=FALSE; /* show we have a bad baud rate, hang up */
- return;
- }
- G->carrdet[u]=usercarr[u]; /* set it up so we detect carrier loss now that we're connected*/
- S->mode=TRUE; /* show its OK */
-
- /* MUBBS main handles the rest from here, thanks for your help ! */
-
- }
-
-
- reset(S)
- struct SSS *S;
- { /* mode = 1 for a modem reset call, send the set up string */
- char modemstring[256];
- int times;
-
- setstuff(modemstring,u); /* get the strings for this line */
- G->serclose();
- G->seropen();
- print("C> Resetting modem on line %d (\"0\" below is OK). This line is now at %s baud\n",(u+1),G->userbaud[u]);
- times=0;
- while (++times<10) {
- sendnc("AT\r"); /* get it to see the new baud rate */
- wait(2);
- if (showstat()) break;
- sendnc("ATZ\r"); /* get desparate, try modem reset */
- wait(2);
- }
-
- times=0;
- while (++times<10) {
- sendnc(modemstring); /* NO CHECK OUTPUT DOES NOT CHECK FOR ANYTHING !! */
- wait(2);
- if (showstat() ==1) break; /* make sure it sees a "0" not a "OK" !! */
- wait(2);
- }
- if(times >=10) {
- print("C> ERROR! Modem on line %d doesn't want to reset properly!\n",(u+1));
- print("C> !!!!!! Port is set at %s baud, CHECK YOUR MODEM STRINGS!\n",G->userbaud[u]);
- sendnc("ATZ\r"); /* get desparate, try modem reset it may reset next time */
- }
- G->serflush(); /* dump any garbage characters */
- }
-
-
- hangup(S)
- struct SSS *S;
- {/* mode = 2 for a modem hangup call, try to hang up modem */
- int
- temp1 = 0,
- temp2,
- temp3 = 0,
- baud,
- flag = 1;
-
- Byte check;
- char modemstring[256], savebaud[maxnamelength];
-
- strcpy(savebaud,G->userbaud[u]); /* save the current baud rate */
- setstuff(modemstring,u); /* get the strings for this line */
-
- if (strcmp(maxbaud[u],"300") == 0) baud=5; /* find the max baud */
- else if (strcmp(maxbaud[u],"1200") == 0) baud=4; /* this sets it to this after first try */
- else if (strcmp(maxbaud[u],"2400") == 0) baud=3;
- else if (strcmp(maxbaud[u],"9600") == 0) baud=2;
- else if (strcmp(maxbaud[u],"19200") == 0) baud=1;
-
- temp2=baud;
- strcpy(G->userbaud[u],savebaud); /* recover the current baud rate */
-
- while (TRUE) {
- while (temp1++ < 8) {
- if (G->forgetusers && !G->local[u]) return; /* if we are not logged in locally and cancel all users */
- if (usercarr[u]) {
- if (u == 0) {if ((check = *(SCCRd + 2) & 0x20)) return; }/* CTS is bit 5, 1= carr loss */
- if (u == 1) {if ((check = *(SCCRd + 0) & 0x20)) return; }/* CTS is bit 5, 1= carr loss */
- print("C> CARRIER STILL PRESENT ON LINE %d, HANGING UP !!\n",(u+1));
- }
- else sendnc("Hardware carrier detect cable not installed.]Please hang up now.]"); /* try the good old standard */
- temp3++; /* try at current baud rate first, then goto highest next */
- print("C> Hanging up at %s baud, attempt=%d\n",G->userbaud[u],temp3);
- G->serclose();
- G->seropen();
- wait(2);
- if (flag) {sendnc("\x02\x02\x02");} /* send the ecape code and delay 2 seconds */
- else sendnc("+++"); /* try the good old standard */
- wait(2);
- if (!usercarr[u]) { /* for LAME mode */
- sendnc(modemstring); /* send the reset string, try to hang up now */
- wait(2);
- sendnc("+++"); /* try the good old standard just incase */
- wait(2);
- sendnc("\r"); /* send a return to clear things */
- wait(1);
- }
- showstat(); /* just prints any input to the screen */
- sendnc(modemstring); /* send the reset string */
- wait(1);
- showstat(); /* just prints any input to the screen */
- lll:
- if (temp2 == 1) strcpy(G->userbaud[u],"19200"); /* set the baud rate to 19200 */
- if (temp2 == 2) strcpy(G->userbaud[u],"9600"); /* set the baud rate to 9600 */
- if (temp2 == 3) strcpy(G->userbaud[u],"2400"); /* set the baud rate to 2400 */
- if (temp2 == 4) strcpy(G->userbaud[u],"1200"); /* set the baud rate to 1200 */
- if (temp2 == 5) strcpy(G->userbaud[u],"300"); /* set the baud rate to 300 */
- if (temp2 >= 6 && !usercarr[u]) return; /* if no carrier detect, we tried all assume hung up */
- if (temp2++ >= 6) {
- temp2=baud; /* set it back to the max baud and do it again */
- goto lll;
- }
- }
- flag = ! flag;
- temp1 = 0;
- if (temp3 >= 80) {
- print("\nC> ERROR !! Tried 80 times to reset modem, COULDN'T RESET LINE %d !!\n",(u+1));
- print("C> !!!!!!!! CHECK YOUR MODEM STRINGS FOR LINE %d !!\n",(u+1));
- return;
- }
- }
- }
-
- showstat() /* checks for OK or 0, sends any characters received to Mac screen */
- {
- char s[256],ck[8],check[8];
- int where,theone, x=0, i;
-
- strcpy(ck,"0~OK"); /* check for 0 or OK */
-
- G->carrdet[u]=FALSE;
- while (TRUE){
- i=0;
- while (G->serin()){ /* serin doesn't check for carrier here ! */
- if (G->input[u] == 0x0A) continue;
- print("%c",G->input[u]);
- s[i]=G->input[u];
- if (i++ > 250) break; /* don't overflow s !! */
- }
- s[i]='\0'; /* terminate the string */
- if (searchs (ck,s,check,&where,&theone)) return(theone);
- /* returns 0 if no match, 1 if "0", 2 if "OK" */
- if (x++>5) break; /* if we have waited long enough, forget it! */
- wait(1); /* wait just a bit longer */
- }
- return (0);
- }
-
- setupall(){ /* called up once at start to set up the modem globals */
- char modemstring[256];
- int user;
-
- user=0; /* note that this is not messing with "u" */
- do { /* sets the modem globals for each user */
- setstuff(modemstring,user); /* get the strings for this line */
- } while (++user < G->users); /* user = 0 to maxport (counting here starts at 0) */
- }
-
- setstuff(modemstring,user)
- char *modemstring;
- int user;
- { /* set up strings */
- char tempB[11],tempCD[11],tempDTR[11];
- int x;
- FILE *stream;
-
- strcpy(modemstring,"ATH0E0V0M1S0=0S2=2\r"); /* standard modem string, just incase */
- strcpy(tempB,"2400"); /* standard 2400, just incase */
- strcpy(tempCD,"1"); /* Carrier detect, just incase */
- strcpy(tempDTR,"0"); /* No DTR, just incase */
-
- if ((stream = fopen(":bbssupport:modems", "r")) == NULL) {
- print("C> FILE ERROR - cannot open :bbssupport:modems\n");
- print("C> SENDING A STANDARD STRING TO THE MODEM !!\n ");
- goto skip;
- }
-
-
- for(x=0; x<=user; x++){ /* get this line's modem string */
- if (fscanf(stream,"%10[^,],%10[^,],%10[^\n]\n%255[^\n]\n",tempB,tempCD,tempDTR,modemstring) == EOF) break; /* get the string */
- }
-
- fclose(stream);
- strcat(modemstring,"\r");
-
- skip:
- strcpy(G->userbaud[user],tempB); /* set to the MAX baud rate */
- strcpy(maxbaud[user],tempB); /* set to the MAX baud rate */
- usercarr[user]= strtoint(tempCD); /* see if we are to detect carrier or if it's LAME BBS time */
- if(usercarr[user] == 0) usercarr[user]=FALSE;
- else usercarr[user]=TRUE;
-
- G->usedtr[user]= strtoint(tempDTR); /* Use DTR hang up? Not used here yet, but it's set anyway */
- if(G->usedtr[user] == 0) G->usedtr[user]=FALSE;
- else G->usedtr[user]=TRUE;
- }
-
-