/* * REDDOS7.C */ /*********************************************************** * Redcom switching system -- A simple call processing * * (Red V.1.3) DEC,1992 By Lu,C.C. * ***********************************************************/ #include "redcom.h" main() { int c,i,temp; char comm_str[30]; printf("A simple calling model of Redcom switching system !!\n"); printf(" (Red V.1.3) By Lu,C.C.\n"); printf(" Type 'Q' to exit the system !! \n\n"); printf("Off-Hook all phones, run IN, ST, remember to use passcode 1-2-3 \n"); printf("Features by: Diane Gruseck, Lou Guzik, Pete Hoskinson\n"); printf("Features: Callblock *# Redial *9 Callback *1 \n\n"); port_setup(); initial_table(); printf("Lu>"); if ((out=fopen("phone.dat","at"))==NULL) { printf("can't open file -- phone.dat !!\n"); } while(1) { for (i=0;i<=7;i++) { if (time_flag[i]>0) { time_control_process(i); } if (alarm_flag[i]>0) { alarm_control_process(i); } } if (in_ready()) { if (receive_process()) { temp=block_process(); printf("Lu>"); if (temp>=0) { call_process(temp); } } } if (kbhit()) { gets(comm_str); if (strlen(comm_str)<2) { switch(comm_str[0]) { case 'Q' : printf("BYE!!\n\n"); close_port(); fclose(out); exit(0); break; case 'R' : fclose(out); printf("From To Start End\n"); printf("----------------------------------------------------------------\n"); system("type phone.dat"); printf("\n Hit any key to continuous !!\n"); getch(); if ((out=fopen("phone.dat","at"))==NULL) { printf("can't open file -- phone.dat !!\n"); } break; } printf("Lu>"); } else { send_process(comm_str); } } } } /* block process */ int block_process() { int k=0,block_length,port; block_length=strlen(receive_block); printf(" %s\n",receive_block); if (block_length>=7) { command[0]=receive_block[0]; command[1]=receive_block[1]; port=(receive_block[5]-49)*4+(receive_block[6]-48); if (block_length>8) { while (receive_block[k+8]!=' ' && receive_block[k+8]!='\0') { r_digit[k]=receive_block[k+8]; k=k+1; } r_digit[k]='\0'; } return(port); } return(-1); } /*************************************************************/ /* serial communication function */ /*************************************************************/ port_setup() { open_port(PORT,0100); set_port(BAUDRATE, 7, EVEN_PARITY, 1); } int receive_process() { int r_c,i=0; char r_block[30]; r_c = get_serial(); if (r_c!=ENQ) { return(0); } put_serial(ACK); while (1) { if (serial_get_process()<0) { return(-1); } r_c = get_serial(); if (r_c!=STX) { return(0); } while (r_c!=ETX) { if (serial_get_process()<0) { return(-1); } r_c = get_serial(); r_block[i]=r_c; i=i+1; } r_block[i]='\0'; if (serial_get_process()<0) { return(-1); } r_c = get_serial(); if (r_c!=check_sum(r_block,i-1)) { put_serial(NAK); } else { put_serial(ACK); r_block[i-1]='\0'; strcpy(receive_block,r_block); return(1); } } } send_process(in_str) char in_str[30]; { int flag=0,r_c,s_length; char s_str[30]=" "; printf("%s",in_str); s_length=strlen(in_str); strcat(in_str," "); in_str[s_length]=ETX; in_str[s_length+1]=check_sum(in_str,s_length); s_str[0]=STX; strcat(s_str,in_str); s_length=s_length+2; while(flag<=0) { put_serial(ENQ); if (serial_get_process()) { r_c = get_serial(); if (r_c==ACK) flag=1; } } while(send_block(s_str,s_length)<0) {} put_serial(EOT); printf("\nLu>"); } int send_block(send_str,send_length) char send_str[30]; int send_length; { int i,r_c; for (i=0;i<=send_length;i++) { put_serial(send_str[i]); } if (serial_get_process()) { r_c = get_serial(); if (r_c==ACK) return(1); } return(-1); } int serial_get_process() { time_t clock_start,clock_count; int r_c; time(&clock_start); while (! in_ready()) { time(&clock_count); if((clock_count - clock_start) >= TIMEOUT) { return(-1); /* -9 indicates timeout*/ } } return(1); } /* caculat checksum value */ int check_sum(check_str,check_length) char check_str[30]; int check_length; { int i,sum; sum=0; for(i=0; i<=check_length; i++) sum = sum^check_str[i]; return(sum); } /*************************************************************/ /* Time control processing */ /*************************************************************/ time_control_process(int port) { time_t tnow; time(&tnow); switch(time_flag[port]) { case 1: /* off hook too long */ if (tnow-port_start_time[port]>OFF_TIMEOUT) { step[port]=8; strcpy(command,"OK"); call_process(port); time_flag[port]=0; } break; case 2: /* No body answer */ if (tnow-port_start_time[port]>RING_TIMEOUT) { step[port]=7; call_process(port); } break; } } alarm_control_process(int port) { time_t tnow; char s_tnow[30]; time(&tnow); strcpy(s_tnow,ctime(&tnow)); if (s_tnow[11]==alarm_time[port][0] && s_tnow[12]==alarm_time[port][1] && s_tnow[14]==alarm_time[port][2] && s_tnow[15]==alarm_time[port][3]) { if (step[port]==0) { send_alarm_signal(port); step[port]=8; } alarm_flag[port]=0; } } start_time_count(int port) { time_t tnow; time(&tnow); port_start_time[port]=tnow; time_flag[port]=1; } /*************************************************************/ /* calling Model */ /*************************************************************/ initial_table() { int i; phone_table[0].secrit=-1; phone_table[0].port=0; phone_table[0].next=0; phone_table[1].secrit=-1; for (i=1;i<=7;i++) { phone_table[i+1].secrit=123; phone_table[i+1].port=i; phone_table[i+1].next=0; } phone_table[8].port=6; } call_process(int port) { int i,p_port; int secrit_nb,new_nb,next_phone; char temp_str[20]; if (compare(command,"ON") && step[port]<=9) { release(port); return; } switch (step[port]) { case 0: /* off hook */ if (compare(command,"OF")) { step[port]=1; start_time_count(port); } break; case 1: /* get security digit */ if (compare(command,"DG")) { calling[port]=r_digit[0]-48; secrit_nb=atoi(r_digit)-1000*calling[port]; if (check_secret(port,secrit_nb)) { send_ok_tone(port); receive_digit(port,"01N"); step[port]=2; } else { send_fail_tone(port); step[port]=9; } } break; case 2: /* collecting information (get function digit) */ if (compare(command,"DG")) { start_time_count(port); switch(r_digit[0]) { case '1': receive_digit(port,"07Y"); step[port]=3; break; case '*': send_ok_tone(port); receive_digit(port,"01N"); step[port]=4; break; case '#': send_ok_tone(port); receive_digit(port,"06N"); step[port]=5; break; case '9': send_ok_tone(port); receive_digit(port,"04N"); step[port]=6; break; default: time_flag[port]=0; called[port]=r_digit[0]-48; if (calling[port]==called[port]) { send_busy_tone(port); step[port]=9; } /* ** NEW - Lou Guzik - record the called number for redial */ else { redial[ calling[port] ] = called[ port ]; /* ** NEW - DG - send busy tone if caller is blocked */ if( call_block[called[port]] == calling[port] ) { send_busy_tone(port); step[port]=9; } else { if (called[port]==8) { phone_record[port].whopay='R'; } else { phone_record[port].whopay='N'; } ringing(port); } } /* if (calling[port]==called[port]) */ break; /* default */ } /* switch(r_digit[0]) */ } /* if (compare(command,"DG")) */ break; /* case 2: */ case 3: /* collecting information (get outline digit) */ if (compare(command,"DG")) { } break; case 4: /* collecting information (get new address) */ if (compare(command,"DG")) { new_nb=r_digit[0]-48; switch(r_digit[0]) { case '*': phone_table[calling[port]].port=port; send_ok_tone(port); step[port] = 9; break; /* ** NEW - *# is block calls */ case '#': send_ok_tone(port); receive_digit( port, "01N" ); step[port] = 13; break; /* ** NEW - Lou Guzik - redial last #, make sure they dialed atleast once */ case '9': if (redial[ calling[port] ] == 0) /* NO CALLS MADE YET */ { send_fail_tone(port); step[port]=9; } else { start_time_count(port); time_flag[port]=0; called[port]=redial[ calling[port] ]; if (calling[port]==called[port]) { send_busy_tone(port); step[port]=9; } /* ** NEW - DG - send busy tone if caller is blocked */ else if( call_block[called[port]] == calling[port] ) { send_busy_tone(port); step[port]=9; } else { if (called[port]==8) { phone_record[port].whopay='R'; } else { phone_record[port].whopay='N'; } ringing(port); } } break; /* case '9': redial */ /* ** NEW - Lou Guzik - callback, make sure someone dialed us atleast once */ case '1': if (callback[ calling[port] ] == 0) /* NO CALLS MADE YET */ { send_fail_tone(port); step[port]=9; } else { start_time_count(port); time_flag[port]=0; called[port]=callback[ calling[port] ]; if (calling[port]==called[port]) { send_busy_tone(port); step[port]=9; } /* ** NEW - DG - send busy tone if caller is blocked */ else if( call_block[called[port]] == calling[port] ) { send_busy_tone(port); step[port]=9; } else { if (called[port]==8) { phone_record[port].whopay='R'; } else { phone_record[port].whopay='N'; } ringing(port); } } break; /* case '1': redial */ default: if (new_nb<=1 || new_nb>=9 || new_nb==calling[port]) { send_fail_tone(port); } else { phone_table[calling[port]].next=new_nb; send_ok_tone(port); } step[port] = 9; break; } } break; case 5: /* collecting information (get new secrit nb) */ if (compare(command,"DG")) { strcpy(temp_str,r_digit); temp_str[3]='\0'; secrit_nb=atoi(temp_str); if (check_secret(port,secrit_nb)) { if (r_digit[3]=='#' && r_digit[4]=='#' && r_digit[5]=='#') { print_record(calling[port]); send_ok_tone(port); } else { send_fail_tone(port); } } else { if (r_digit[0]==r_digit[3] && r_digit[1]==r_digit[4] && r_digit[2]==r_digit[5]) { r_digit[3]='\0'; phone_table[calling[port]].secrit=atoi(r_digit); send_ok_tone(port); } else { send_fail_tone(port); } } step[port]=9; } break; case 6: if (compare(command,"DG")) { if ((10*(r_digit[0]-48)+(r_digit[1]-48))<24 && (10*(r_digit[2]-48)+(r_digit[3]-48))<60) { send_ok_tone(port); alarm_flag[port]=1; for (i=0;i<=3;i++) { alarm_time[port][i]=r_digit[i]; } } else { send_fail_tone(port); } step[port]=9; } break; case 7: next_phone=phone_table[called[wait[port]]].next; p_port=phone_table[next_phone].port; if (step[p_port]==0) { release(port); called[wait[port]]=next_phone; ringing(wait[port]); } else { start_time_count(port); time_flag[port]=2; } break; case 8: if (!compare(command,"GA")) { send_fail_tone(port); step[port]=9; time_flag[0]=0; } break; case 9: /* DO NOTHING but waiting off hook */ break; case 10: /* exam called party */ if (compare(command,"OF")) { p_port=wait[port]; phone_record[p_port].calling=calling[p_port]; phone_record[p_port].called=called[p_port]; billing(p_port,0); step[port]=12; time_flag[port]=0; } break; case 11: /* calling party on hook */ if (compare(command,"ON")) { p_port=phone_table[called[port]].port; if (step[p_port]==12) { billing(port,1); step[p_port]=9; } else { release(p_port); } release(port); } break; case 12: /* called party on hook */ if (compare(command,"ON")) { billing(wait[port],1); release(port); step[wait[port]]=9; } break; /* ** NEW - get block number */ case 13: if( compare( command, "DG" ) ) { new_nb = r_digit[0] - 48; switch( r_digit[0] ) { case '*': call_block[calling[port]] = 0; if( calling[port] == 6 ) call_block[8] = 0; send_ok_tone( port ); break; default: if( (new_nb <= 1) || (new_nb >=9) || (new_nb == calling[port]) ) { send_fail_tone( port ); } else { call_block[calling[port]] = new_nb; if( calling[port] == 6 ) call_block[8] = new_nb; send_ok_tone( port); } break; } step[port] = 9; } break; } } /********************************************************/ /* Some functions of the call model */ /********************************************************/ int compare(char str_1[3],char str_2[3]) { if (str_1[0]==str_2[0] && str_1[1]== str_2[1]) return(1); else return(0); } release(int port) { int k; char out_command[30]="RL0000 04Y"; for (k=0;k<=3;k++) { out_command[k+2]=PORT_TABLE[port][k]; } send_process(out_command); step[port]=0; time_flag[port]=0; } int check_secret(int port,int secrit_nb) { if (phone_table[calling[port]].secrit==secrit_nb) { return(1); } return(0); } receive_digit(int port,char r_nb[4]) { int k; char out_command[30]="RD0000 00N"; for (k=0;k<=3;k++) { out_command[k+2]=PORT_TABLE[port][k]; } out_command[7]=r_nb[0]; out_command[8]=r_nb[1]; out_command[9]=r_nb[2]; send_process(out_command); } send_ok_tone(int port) { int k; char out_command[30]="GP0000 PH 0010 SI"; for (k=0;k<=3;k++) { out_command[k+2]=PORT_TABLE[port][k]; } send_process(out_command); } send_fail_tone(int port) { int k; char out_command[30]="GP0000 IN 0200 SI"; for (k=0;k<=3;k++) { out_command[k+2]=PORT_TABLE[port][k]; } send_process(out_command); } send_busy_tone(int port) { int k; char out_command[30]="GP0000 BT"; for (k=0;k<=3;k++) { out_command[2+k]=PORT_TABLE[port][k]; } send_process(out_command); } send_alarm_signal(int port) { int k; char out_command[30]="RI0000"; for (k=0;k<=3;k++) { out_command[2+k]=PORT_TABLE[port][k]; } send_process(out_command); } ringing(int port) { int k; int p_port,n_port; int next_phone; char out_command_1[30]="RI0000 0000"; char out_command_2[30]="GP0000 RB"; p_port=phone_table[called[port]].port; next_phone=phone_table[called[port]].next; n_port=phone_table[next_phone].port; if (step[p_port]==0 || (n_port>0 && step[n_port]==0 && next_phone!=calling[port])) { if (step[p_port]!=0) { called[port]=next_phone; p_port=n_port; next_phone=phone_table[called[port]].next; } wait[p_port]=port; step[p_port]=10; if (next_phone>0) { start_time_count(p_port); time_flag[p_port]=2; } for (k=0;k<=3;k++) { out_command_1[2+k]=PORT_TABLE[p_port][k]; out_command_1[7+k]=PORT_TABLE[port][k]; out_command_2[2+k]=PORT_TABLE[port][k]; } send_process(out_command_1); send_process(out_command_2); step[port]=11; /* ** NEW - Lou Guzik - record the called number for callback */ callback[ called[port] ] = calling[ port ]; } else { send_busy_tone(port); step[port]=9; } } billing(int port,int status) { time_t tnow; time(&tnow); if (status==0) { strcpy(phone_record[port].start,ctime(&tnow)); phone_record[port].start[24]='\0'; } else { strcpy(phone_record[port].end,ctime(&tnow)); phone_record[port].end[24]='\0'; fprintf(out,"%c %d %d %s %s\n",phone_record[port].whopay ,phone_record[port].calling ,phone_record[port].called ,phone_record[port].start ,phone_record[port].end); } } print_record(int user) { char c; int d1,d2; char s1[30],s2[30]; fclose(out); if ((temp_file=fopen("temp.dat","w"))==NULL) { printf("can't open !!\n"); } if ((out=fopen("phone.dat","r"))==NULL) { printf("can't open !!\n"); } fprintf(temp_file,"Telephone Records of the Phone Number - %i : \n\n",user); fprintf(temp_file,"From To Start End\n"); fprintf(temp_file, "----------------------------------------------------------------\n"); while (! feof(out)) { fscanf(out,"%c %d %d %24c %24c\n",&c,&d1,&d2,&s1,&s2); s1[24]='\0'; s2[24]='\0'; if (d1==user || d2==user) { fprintf(temp_file,"%c %d %d %s %s\n",c,d1,d2,s1,s2); } } fclose(out); fclose(temp_file); system("print temp.dat"); if ((out=fopen("phone.dat","at"))==NULL) { printf("can't open file -- phone.dat !!\n"); } }