home *** CD-ROM | disk | FTP | other *** search
/ TAP YIPL / TAP_and_YIPL_Collection_CD.iso / PHREAK / CELLULAR / CTEK.ZIP / TEL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-19  |  11.6 KB  |  476 lines

  1. /*
  2.   Cellular Call Control Program
  3.   Copyright (C) 1994 by Network Wizards
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <dos.h>
  8. #include <conio.h>
  9. #include <bios.h>
  10. #include <ctype.h>
  11. #include <math.h>
  12. #include <string.h>
  13.  
  14. #define cursor(x,y)    gotoxy(y,x)
  15. #define OUTPUT_WINDOW   window(1,4,80,24)
  16. #define FULL_WINDOW     window(1,1,80,25)
  17.  
  18. #define FALSE        0
  19. #define TRUE        1
  20.  
  21. #define VERSION            "2.0(940210)"   /* major.minor(last-edit-date) */
  22.  
  23. typedef unsigned char bool;
  24. typedef unsigned char byte;
  25. typedef unsigned int  word;
  26.  
  27. #include "ctlib.h"
  28.  
  29. #define BUFLEN        128
  30. #define ESC        0x1B
  31.  
  32. char buf[BUFLEN];
  33.  
  34. /* control channel scan mode storage */
  35. char ps_system;
  36. int  ps_cc;                /* control channel */
  37. int  ps_cc_rss;                /* control channel last rss */
  38.  
  39. char ournum[32];            /* our telephone number */
  40. unsigned long ouresn;            /* our esn */
  41.  
  42. byte testaddr = 0;
  43. byte testpos = 0;
  44.  
  45. main(argc,argv)
  46.   int argc;
  47.   char *argv[];
  48. {
  49.   int i;
  50.   
  51.   /* initialize ct library using the specified COM port */
  52.   if (argc > 1)
  53.   {
  54.     if (*argv[1] == '1')
  55.       ct_lib_init(900,0x3f8,4);
  56.     else if (*argv[1] == '2')
  57.       ct_lib_init(900,0x2f8,3);
  58.     else if (*argv[1] == '3')
  59.       ct_lib_init(900,0x3e8,5);
  60.     else
  61.     {
  62.       puts("Type 'TEL 2' to use COM2");
  63.       exit(0);
  64.     }
  65.   }
  66.   else
  67.     ct_lib_init(900,0x3f8,4);        /* com1 by default */
  68.  
  69.   cprintf("Wait...");
  70.   /* power up oki and tell it what mode to use */
  71.   if (!ct_on(MODE_TEST))
  72.   {
  73.     cprintf("?No response from OKI\r\n");
  74.     cprintf("  Make sure the phone is turned OFF and\r\n");
  75.     cprintf("  all cables are properly attached.\r\n");
  76.     cleanup();
  77.     exit(1);
  78.   }
  79.  
  80.   /* read in nam info to get our phone number */
  81.   if (!ct_get_nams())
  82.   {
  83.     cprintf("?Can't read NAM info\r\n");
  84.     cleanup();
  85.     exit(1);
  86.   }
  87.   strcpy(ournum,nam_info[ct_state.namindex].number);    /* use current nam */
  88.   clrscr(); cursor(1,1);
  89.   cprintf("Tel# is %s, ",ournum);
  90.  
  91.   if (!ct_get_esn(&ouresn))
  92.   {
  93.     cprintf("?Can't get ESN\r\n");
  94.     cleanup();
  95.     exit(1);
  96.   }
  97.   cprintf("ESN is %08lx\r\n",ouresn);
  98.  
  99.   if (ournum[13] & 0x01)        /* even or odd min? */
  100.     ct_stream(1);            /* watch odd stream */
  101.   else
  102.     ct_stream(0);            /* watch even stream */
  103.   
  104.   cursor(25,1);
  105.   cputs("N: rescan     S: start call    R: receive call    ESC exit");
  106.   OUTPUT_WINDOW;
  107.   cursor(21,1);
  108.  
  109.   cmd_page_scan();            /* enter main program loop */
  110.  
  111.   FULL_WINDOW;
  112.   cursor(25,1); clreol();
  113.   cursor(24,1);
  114.   cleanup();
  115.   exit(0);
  116. }
  117.  
  118. cleanup()
  119. {
  120.   ct_off();                /* turn off phone */
  121.   ct_lib_done();            /* cleanup library stuff */
  122. }
  123.  
  124. /*
  125.   scan control channel and display received messages,
  126.   while waiting for commands from pc keyboard.
  127.  */
  128. cmd_page_scan()
  129. {
  130.   byte cmd;
  131.   word key;
  132.   byte mems[16];
  133.   bool get_msg_sent;
  134.   byte *msg, *min;
  135.   int scc,dcc,order,ordq,local,chan,vmac;
  136.  
  137.   dcc = 0;
  138.   ct_set_rx_audio(FALSE);        /* mute audio */
  139.   get_msg_sent = FALSE;
  140.   ps_system = tolower(nam_info[ct_state.namindex].sys);
  141.   find_cc();
  142.   
  143.   /* main command loop */ 
  144.   for (;;)
  145.   {
  146.     if (bioskey(1) != 0)        /* if keypress waiting */
  147.     {
  148.       if (get_msg_sent)            /* and if we're waiting for a msg */
  149.       {
  150.     get_msg_sent = FALSE;
  151.     ct_fcc_msg(MSG_ABORT,NULL);    /* then abort waiting for msg */
  152.       }
  153.       key = bioskey(0);            /* read keypress codes */
  154.       cmd = key & 0x00ff;        /* get ascii code if any */
  155.       if (cmd == ESC)
  156.     break;
  157.       if (isupper(cmd))
  158.     cmd = tolower(cmd);
  159.       switch (cmd)
  160.       {
  161.     case 'n': find_cc(); break;
  162.     case 's': cmd_startcall(dcc); break;
  163.     case 'r': cmd_receivecall(dcc); break;
  164.     default:  break;
  165.       }
  166.     }
  167.     if (!get_msg_sent)
  168.     {
  169.       ct_fcc_msg(MSG_SETUP,NULL);    /* get ready for a control chnl msg */
  170.       get_msg_sent = TRUE;
  171.     }
  172.     if (!ct_fcc_msg(MSG_GET,&mems))    /* incoming msg received? */
  173.       continue;                /* no, keep waiting */
  174.     get_msg_sent = FALSE;
  175.  
  176.    if (!ct_decode_fcc_msg(mems,&scc,&dcc,&min,&order,&ordq,&local,&chan,&vmac))
  177.       continue;                /* ignore invalid msgs */
  178.  
  179.     /* display decoded msg (order or channel allocation) */
  180.     if (scc == 3)
  181.     {
  182.       cprintf("dcc %d: %s %s\r\n",dcc,min,ct_decode_order(order,ordq));
  183.     }
  184.     else
  185.       cprintf("dcc %d: %s setup: chan=%d, vmac=%d, scc=%d\r\n",
  186.           dcc,min,chan,vmac,scc);
  187.  
  188.     /* if we are paged, notify user */
  189.     if ((scc == 3) && (order == 0) && (!strncmp(min,ournum,14)))
  190.       cprintf("incoming call request (type 'R' to answer)\007\r\n");
  191.  
  192.     /* if a voice channel is allocated for us, then go for it */
  193.     if ((scc != 3) && (!strncmp(min,ournum,14)))
  194.     {
  195.       cprintf("  tune to channel %d\r\n",chan);
  196.       ct_set_channel(chan);
  197.       cprintf("  set power level to %d\r\n",vmac);
  198.       ct_set_tx_power(vmac);
  199.       cprintf("  carrier on\r\n");
  200.       ct_set_carrier(TRUE);
  201.       cprintf("  set sat to %d\r\n",scc);
  202.       ct_set_sat(scc);
  203.       cprintf("  audio on\r\n");
  204.       ct_set_audio_path(AUDIO_EARPIECE);
  205.       ct_set_volume(0);
  206.       ct_set_rx_audio(TRUE);
  207.       ct_set_tx_audio(TRUE);
  208.       conversation_mode();        /* enter conversation mode */
  209.       cprintf("  carrier off\r\n");
  210.       ct_set_carrier(FALSE);
  211.       cprintf("  audio off\r\n");
  212.       ct_set_rx_audio(FALSE);
  213.       ct_set_tx_audio(FALSE);
  214.       find_cc();            /* find a control channel */
  215.     }
  216.   }
  217. }
  218.  
  219. /* scan all control channels of a system for strongest one */
  220. find_cc()
  221. {
  222.   int chnl, rss, tmp;
  223.   int hi_chnl, hi_rss;
  224.   int beg, end;
  225.     
  226.   cprintf("Scanning for control channel:  ");
  227.  
  228.   /* set lower and upper control channel numbers depending on system type */
  229.   if (ps_system == 'a')
  230.   {
  231.     beg = 313;
  232.     end = 333;
  233.   }
  234.   else
  235.   {
  236.     beg = 334;
  237.     end = 354;
  238.   }
  239.  
  240.   /* find the control channel with the strongest signal */
  241.   hi_rss = 0;
  242.   for (chnl = beg; chnl <= end; chnl++)
  243.   {
  244.     ct_set_channel(chnl);
  245.     /* get rss a few times to let settle */
  246.     rss = ct_get_rss();
  247.     while ((tmp = ct_get_rss()) > rss)
  248.       rss = tmp;
  249.     if (rss > hi_rss)            /* if highest rss, remember it */
  250.     {
  251.       hi_rss = rss;
  252.       hi_chnl = chnl;
  253.     }
  254.   }
  255.   ps_cc = hi_chnl;            /* get channel with highest rss */
  256.   ps_cc_rss = hi_rss;
  257.   ct_set_channel(ps_cc);
  258.   cprintf("%d\r\n",ps_cc);
  259.   FULL_WINDOW;
  260.   cursor(2,1);
  261.   cprintf("Control Channel: %04d, ",ps_cc);
  262.   cprintf("Received Signal Strength: %02d\r\n",ps_cc_rss);
  263.   OUTPUT_WINDOW;
  264.   cursor(21,1);
  265. }
  266.  
  267. cmd_receivecall(dcc)
  268.   int dcc;
  269. {
  270.   unsigned long min1, min2;
  271.   
  272.   cprintf("answering page request\r\n");
  273.   ct_tel2min(ournum,&min1,&min2);
  274.   send_callrequest(dcc,min1,min2,ouresn,NULL);
  275. }
  276.  
  277. cmd_startcall(dcc)
  278.   int dcc;
  279. {
  280.   unsigned long min1, min2;
  281.   
  282.   cprintf("Call tel#: ");
  283.   buf[0] = 32;
  284.   cgets(buf);
  285.   cprintf("\r\nsending call request\r\n");
  286.   ct_tel2min(ournum,&min1,&min2);
  287.   send_callrequest(dcc,min1,min2,ouresn,buf+2);
  288. }
  289.  
  290. /* returns the decimal value of the next ascii digit from a string */
  291. char *nexttp;
  292. int nextdigit()
  293. {
  294.   char x;
  295.   
  296.   while (*nexttp != '\0')
  297.   {
  298.     if (isdigit(*nexttp)) break;
  299.     nexttp++;
  300.   }
  301.   if (*nexttp == '\0')
  302.     return(0);
  303.   x = *nexttp;
  304.   nexttp++;
  305.   if (x == '0')
  306.     return(10);
  307.   return(x - '0');
  308. }
  309.  
  310. /* if telnum is NULL, send page reply; otherwise send call request */
  311. send_callrequest(dcc,min1,min2,ouresn,telnum)
  312.   int dcc;
  313.   unsigned long min1, min2, ouresn;
  314.   char *telnum;
  315. {
  316.   int i;
  317.   byte msg[30];                /* 5 word message to send */
  318.  
  319.   for (i = 0; i < 30; i++)        /* clear message area */
  320.     msg[i] = '\0';
  321.  
  322.   /* Word A:  Abbreviated Address Word */ 
  323.   /* F=1, NAWC=4, T=1, S=1, E=1, RSVD=0 */
  324.   if (telnum != NULL)
  325.     msg[0] = 0xce;
  326.   else
  327.     msg[0] = 0xa6;
  328.   /* SCM = 1010, MIN1 */
  329.   msg[1] = 0xa0 | (min1 >> 20);
  330.   msg[2] = (min1 & 0x0ff000) >> 12;
  331.   msg[3] = (min1 & 0x000ff0) >> 4;
  332.   msg[4] = (min1 & 0x00000f) << 4;
  333.   ct_gen_bch(36,msg);
  334.  
  335.   /* Word B:  Extended Address Word */
  336.   /* F=0, NAWC=3, LOCAL=0 */
  337.   if (telnum != NULL)
  338.     msg[6]  = 0x30;
  339.   else
  340.     msg[6]  = 0x10;
  341.   /* LOCAL=0, ORDQ=0, ORDER=0 */
  342.   msg[7]  = 0x00;
  343.   msg[8]  = 0x00;
  344.   msg[9]  = (min2 >> 4);
  345.   msg[10] = (min2 & 0x000f) << 4;
  346.   ct_gen_bch(36,msg+6);
  347.  
  348.   /* Word C:  Serial Number Word */
  349.   if (telnum != NULL)
  350.     msg[12] = 0x20 | (ouresn >> 28);
  351.   else
  352.     msg[12] = 0x00 | (ouresn >> 28);
  353.   msg[13] = (ouresn & 0x0ff00000) >> 20;
  354.   msg[14] = (ouresn & 0x000ff000) >> 12;
  355.   msg[15] = (ouresn & 0x00000ff0) >> 4;
  356.   msg[16] = (ouresn & 0x0000000f) << 4;
  357.   ct_gen_bch(36,msg+12);
  358.  
  359.   /* Word D: telephone number to dial (digits 1-8) */
  360.   if (telnum != NULL)
  361.   {
  362.     nexttp = telnum;
  363.     msg[18] = 0x10 | nextdigit();
  364.     msg[19] = nextdigit() << 4;
  365.     msg[19] = msg[19] | nextdigit();
  366.     msg[20] = nextdigit() << 4;
  367.     msg[20] = msg[20] | nextdigit();
  368.     msg[21] = nextdigit() << 4;
  369.     msg[21] = msg[21] | nextdigit();
  370.     msg[22] = nextdigit() << 4;
  371.   }
  372.   ct_gen_bch(36,msg+18);
  373.  
  374.   /* Word E: telephone number to dial (digits 9-16) */
  375.   if (telnum != NULL)
  376.   {
  377.     msg[24] = nextdigit();
  378.     msg[25] = nextdigit() << 4;
  379.     msg[25] = msg[25] | nextdigit();
  380.     msg[26] = nextdigit() << 4;
  381.     msg[26] = msg[26] | nextdigit();
  382.     msg[27] = nextdigit() << 4;
  383.     msg[27] = msg[27] | nextdigit();
  384.     msg[28] = nextdigit() << 4;
  385.   }
  386.   ct_gen_bch(36,msg+24);
  387.  
  388.   cprintf("tx rcc msg: ");
  389.   for (i = 0; i < 30; i++)
  390.   {
  391.     cprintf("%02x",msg[i]);
  392.     if (((i+1) % 6) == 0)
  393.       cprintf(" ");
  394.   }
  395.   cprintf("\r\n");
  396.  
  397.   if (!ct_rcc_msg(dcc,msg))            /* send it */
  398.     cprintf("Failed to send message!\r\n");
  399. }
  400.  
  401. /* handle a call once the voice channel has been set up */
  402. conversation_mode()
  403. {
  404.   bool alerted;
  405.   byte word1[6];
  406.   int scc,pscc,order,ordq,local,chan,vmac;
  407.   
  408.   alerted = FALSE;
  409.   cprintf("hit any char to hangup\r\n");
  410.   ct_fvc_msg(MSG_SETUP,NULL);
  411.   for (;;)
  412.   {
  413.     if (kbhit())
  414.       break;
  415.     if (!ct_fvc_msg(MSG_GET,word1)) continue;
  416.  
  417.     /* got an order from base station, so decode it */
  418.     if (!ct_decode_fvc_msg(word1,&scc,&pscc,&order,&ordq,&local,&chan,&vmac))
  419.     {
  420.       ct_fvc_msg(MSG_SETUP,NULL);    /* get ready for next msg */
  421.       continue;                /* ignore bad messages */
  422.     }
  423.  
  424.     if (scc == 3)            /* if order received */
  425.     {
  426.       cprintf("  order: %s\r\n",ct_decode_order(order,ordq));
  427.       if (order == 1)            /* alert? */
  428.       {
  429.     if (!alerted)
  430.     {
  431.       cprintf("  signalling tone on\r\n");
  432.       ct_set_signalling_tone(TRUE);    /* ST on */
  433.     }
  434.     alerted = TRUE;
  435.     delay(1000);
  436.     cprintf("  signalling tone off\r\n");
  437.     ct_set_signalling_tone(FALSE);    /* ST off (answer call) */
  438.       }
  439.       if (order == 6)            /* stop alert? */
  440.       {
  441.     cprintf("  signalling tone off\r\n");
  442.     ct_set_signalling_tone(FALSE);    /* ST off */
  443.       }
  444.       ct_fvc_msg(MSG_SETUP,NULL);    /* get ready for next msg */
  445.       if (order == 3)            /* end of call if release order */
  446.     break;
  447.       continue;
  448.     }
  449.  
  450.     /* otherwise its a handoff request */
  451.     cprintf("  handoff msg: chan=%d, vmac=%d\r\n",chan,vmac);
  452.     cprintf("  signalling tone flash (50ms)\r\n");
  453.     ct_set_signalling_tone(TRUE);
  454.     delay(50);
  455.     ct_set_signalling_tone(FALSE);
  456.     cprintf("  carrier off\r\n");
  457.     ct_set_carrier(FALSE);
  458.     cprintf("  tune to channel %d\r\n",chan);
  459.     ct_set_channel(chan);
  460.     cprintf("  set power level to %d\r\n",vmac);
  461.     ct_set_tx_power(vmac);
  462.     cprintf("  set sat to %d\r\n",scc);
  463.     ct_set_sat(scc);
  464.     cprintf("  carrier on\r\n");
  465.     ct_set_carrier(TRUE);
  466.     ct_fvc_msg(MSG_SETUP,NULL);        /* rcv next msg */
  467.   }
  468.   ct_fvc_msg(MSG_ABORT,NULL);
  469.   /* hangup/release */
  470.   cprintf("hanging up\r\n");
  471.   cprintf("  signalling tone flash (2sec)\r\n");
  472.   ct_set_signalling_tone(TRUE);
  473.   sleep(2);
  474.   ct_set_signalling_tone(FALSE);
  475. }
  476.