home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ctcoll95.zip / BASTELST / PAPI020.ZIP / API.C next >
C/C++ Source or Header  |  1993-10-06  |  7KB  |  423 lines

  1. /* Common ISDN API C-Library Functions
  2.     Author: Dietmar Friede
  3. Copyright (c) 1992 D.Friede
  4.  #include "copy"
  5.  #include "copying.doc"
  6. */
  7.  
  8. #define VERSION        "0.18 21.09.1993"
  9.  
  10. #include "stdio.h"
  11. #include <dos.h>
  12. #include <ctype.h>
  13. #include <mem.h>
  14. #include "types.h"
  15. #include "api.h"
  16. #include "head.h"
  17.  
  18. word check_listen(void);
  19. extern unsigned application;
  20.  
  21. static word messagenr= 0;
  22. struct msg_head *getmsg(void);
  23. word dirps(void);
  24. void restore(word);
  25. extern dword InfoMask;
  26.  
  27. static struct msg_head *
  28. setmsghdnr(word msg_nr, word l, byte c, byte sc)
  29. {
  30.     struct msg_head *m;
  31.     m=getmsg();
  32.     if(m)
  33.     {
  34.         m->length=l;
  35.         m->appl_id= application;
  36.         m->command= c;
  37.         m->subcommand= sc;
  38.         m->msg_nr=msg_nr;
  39.     }
  40.     return m;
  41. }
  42.  
  43. static struct msg_head *
  44. setmsghd(word l, byte c, byte sc)
  45. {
  46.     messagenr++;
  47.     return(setmsghdnr(messagenr&0x7fff,l,c,sc));
  48. }
  49.  
  50. static int
  51. sendsimplenr(word msg_nr, byte c, byte sc, word w)
  52. {
  53.     word *s;
  54.     struct msg_head *m;
  55.     if((m=getmsg())==null) return 1;
  56.  
  57.     m->length= 10;
  58.     m->appl_id= application;
  59.     m->command= c;
  60.     m->subcommand= sc;
  61.     m->msg_nr=msg_nr;
  62.  
  63.     s= (word *) m->data;
  64.     *s= w;
  65.  
  66.     return api_put_message((byte far *)m);
  67. }
  68.  
  69. static int
  70. sendsimple(byte c, byte sc, word w)
  71. {
  72.     messagenr++;
  73.     return(sendsimplenr(messagenr&0x7fff,c,sc,w));
  74. }
  75.  
  76. int
  77. con_req(byte contr, byte b_channel, dword inf_mask,
  78.     byte out_serv, byte out_serv_add, byte src_eaz,
  79.     byte * dest_addr)
  80. {
  81.     dword *l;
  82.     struct msg_head *m;
  83.  
  84.     if((m=setmsghd(18+dest_addr[0],2,0)) == null) return 1;
  85.  
  86.     m->data[0]= contr;
  87.     m->data[1]= b_channel;
  88.     l= (dword *) &m->data[2];
  89.     *l= inf_mask;
  90.     m->data[6]= out_serv;
  91.     m->data[7]= out_serv_add;
  92.     m->data[8]= src_eaz;
  93.     memcpy(&m->data[9],dest_addr,dest_addr[0]+1);
  94.  
  95.     return api_put_message((byte far *)m);
  96. }
  97.  
  98. int
  99. con_resp(word msg_nr,word plci, byte reject)
  100. {
  101.     word *s;
  102.     struct msg_head *m;
  103.  
  104.     if((m=setmsghdnr(msg_nr,11,2,3)) == null ) return 1;
  105.  
  106.     s= (word *) m->data;
  107.     *s= plci;
  108.     m->data[2]= reject;
  109.  
  110.     return api_put_message((byte far *)m);
  111. }
  112.  
  113. int
  114. con_act_resp(word msg_nr,word plci)
  115. {
  116.     return(sendsimplenr(msg_nr,3,3,plci));
  117. }
  118.  
  119. int
  120. discon_req(word plci, byte cause)
  121. {
  122.     word *s;
  123.     struct msg_head *m;
  124.  
  125.     if((m=setmsghd(11,4,0)) == null) return 1;
  126.  
  127.     s= (word *) m->data;
  128.     *s= plci;
  129.     m->data[2]= cause;
  130.  
  131.     return api_put_message((byte far *)m);
  132. }
  133.  
  134. int
  135. discon_resp(word msg_nr,word plci)
  136. {
  137.     return(sendsimplenr(msg_nr,4,3,plci));
  138. }
  139.  
  140. word
  141. listen_req(byte contr, dword inf_mask, word eaz_mask, word si_mask)
  142. {
  143.     dword *l;
  144.     word *s;
  145.     struct msg_head *m;
  146.  
  147.     if((m=setmsghd(17,5,0)) == null) return 1;
  148.  
  149.     m->data[0]= contr;
  150.     l= (dword *) &m->data[1];
  151.     *l= inf_mask;
  152.     s= (word *) &m->data[5];
  153.     *s= eaz_mask;
  154.     s++;
  155.     *s= si_mask;
  156.  
  157.     return api_put_message((byte far *)m);
  158. }
  159.  
  160. int
  161. inf_req(word plci, dword inf_mask)
  162. {
  163.     word *s;
  164.     dword *l;
  165.     struct msg_head *m;
  166.  
  167.     if((m=setmsghd(14,7,0))==null) return 1;
  168.  
  169.     messagenr&=0x7fff;
  170.     s= (word *) m->data;
  171.     *s= plci;
  172.     l= (dword *) &m->data[2];
  173.     *l= inf_mask;
  174.  
  175.     return api_put_message((byte far *)m);
  176. }
  177.  
  178. int
  179. inf_resp(word msg_nr,word plci)
  180. {
  181.     return(sendsimplenr(msg_nr,7,3,plci));
  182. }
  183.  
  184. int
  185. con_inf_req(word plci,byte * dest_addr)
  186. {
  187.     word *s;
  188.     struct msg_head *m;
  189.  
  190.     if((m=setmsghd(11+dest_addr[0],9,0)) == null) return 1;
  191.  
  192.     s= (word *) m->data;
  193.     *s= plci;
  194.     memcpy(&m->data[2],dest_addr,dest_addr[0]+1);
  195.  
  196.     return api_put_message((byte far *)m);
  197. }
  198.  
  199. int
  200. sel_b2_prot_req(word plci, byte prot, byte * dlpd)
  201. {
  202.     word *s;
  203.     struct msg_head *m;
  204.  
  205.     if((m=setmsghd(12+dlpd[0],0x40,0)) == null) return 1;
  206.  
  207.     s= (word *) m->data;
  208.     *s= plci;
  209.     m->data[2]= prot;
  210.     memcpy(&m->data[3],dlpd,8);
  211.  
  212.     return api_put_message((byte far *)m);
  213. }
  214.  
  215. int
  216. sel_b3_prot_req(word plci, byte prot, byte * ncpd)
  217. {
  218.     word *s;
  219.     struct msg_head *m;
  220.  
  221.     if((m=setmsghd(12+ncpd[0],0x80,0)) == null) return 1;
  222.  
  223.     s= (word *) m->data;
  224.     *s= plci;
  225.     m->data[2]= prot;
  226.     memcpy(&m->data[3],ncpd,ncpd[0]+1);
  227.  
  228.     return api_put_message((byte far *)m);
  229. }
  230.  
  231. int
  232. listen_b3_req(word plci)
  233. {
  234.     return(sendsimple(0x81,0,plci));
  235. }
  236.  
  237. int
  238. con_b3_req(word plci, byte *ncpi)
  239. {
  240.     word *s;
  241.     struct msg_head *m;
  242.  
  243.     if((m=setmsghd(11+ncpi[0],0x82,0)) == null ) return 1;
  244.  
  245.     s= (word *) m->data;
  246.     *s= plci;
  247.     memcpy(&m->data[2],ncpi,ncpi[0]+1);
  248.  
  249.     return api_put_message((byte far *)m);
  250. }
  251.  
  252. int
  253. con_b3_resp(word msg_nr,word ncci, byte reject, byte *ncpi)
  254. {
  255.     word *s;
  256.     struct msg_head *m;
  257.  
  258.     if((m= setmsghdnr(msg_nr,12+ncpi[0],0x82,3)) == null) return 1;
  259.  
  260.     s= (word *) m->data;
  261.     *s= ncci;
  262.     m->data[2]= reject;
  263.     memcpy(&m->data[3],ncpi,ncpi[0]+1);
  264.  
  265.     return api_put_message((byte far *)m);
  266. }
  267.  
  268. int
  269. con_b3_act_resp(word msg_nr,word ncci)
  270. {
  271.     return(sendsimplenr(msg_nr,0x83,3,ncci));
  272. }
  273.  
  274. int
  275. discon_b3_req(word ncci, byte * ncpi)
  276. {
  277.     word *s;
  278.     struct msg_head *m;
  279.  
  280.     if((m=setmsghd(11+ncpi[0],0x84,0)) == null) return 1;
  281.  
  282.     s= (word *) m->data;
  283.     *s= ncci;
  284.     memcpy(&m->data[2],ncpi,ncpi[0]+1);
  285.  
  286.     return api_put_message((byte far *)m);
  287. }
  288.  
  289. int
  290. discon_b3_resp(word msg_nr,word ncci)
  291. {
  292.     return(sendsimplenr(msg_nr,0x84,3,ncci));
  293. }
  294.  
  295. int
  296. get_b3_par_req(word ncci)
  297. {
  298.     return(sendsimple(0x85,0,ncci));
  299. }
  300.  
  301. int
  302. dat_b3_req(word ncci, word dat_length, dword data, byte number, word flags)
  303. {
  304.     dword *l;
  305.     word *s;
  306.     struct msg_head *m;
  307.  
  308.     if((m=setmsghd(19,0x86,0)) == null) return 1;
  309.  
  310.     s= (word *) &m->data[0];
  311.     *s= ncci;
  312.     s++;
  313.     *s= dat_length;
  314.     l= (dword *) &m->data[4];
  315.     *l= data;
  316.     m->data[8]=number;
  317.     s= (word *) &m->data[9];
  318.     *s= flags;
  319.  
  320.     return api_put_message((byte far *)m);
  321. }
  322.  
  323. int
  324. dat_b3_resp(word msg_nr,word ncci, byte number)
  325. {
  326.     word *s;
  327.     struct msg_head *m;
  328.  
  329.     if((m=setmsghdnr(msg_nr,11,0x86,3)) == null) return 1;
  330.  
  331.     s= (word *) m->data;
  332.     *s= ncci;
  333.     m->data[2]= number;
  334.  
  335.     return api_put_message((byte far *)m);
  336. }
  337.  
  338. static byte far text[64];
  339. static byte far messages[0x4000];
  340. extern InterruptPtr SaveTimerInt;
  341. extern byte own_eaz;
  342. void interrupt timevec(void);
  343.  
  344. int
  345. api_init (word bsize, word verbose)
  346. {
  347.     word err;
  348.     word maxblock, nmesg;
  349.     dword msize;
  350.  
  351.     if(api_check()) return 1;
  352.  
  353.     if(verbose)
  354.     {
  355.         printf("ISDN Card:\n");
  356.         api_get_manufacturer(text);
  357.         printf("Manufacturer: %s\n",text);
  358.         api_get_version(text);
  359.         printf("%s\n",text);
  360.         api_get_serial(text);
  361.         if( text[0] )
  362.             printf("Serial: %s\n",text);
  363.     }
  364.  
  365.     msize= sizeof(messages)-(2*180);
  366.     maxblock= (word) (msize/bsize);
  367.     if(maxblock < 2)
  368.     {
  369.         printf("Message Memory too small to register\n");
  370.         return 1;
  371.     }
  372.     msize= maxblock*bsize;
  373.     nmesg= (word) ((sizeof(messages)-msize)/180);
  374.     if(verbose)
  375.     {
  376.         printf("register no mesg %d 1 conn. no buffs %d buffsize %d\n",
  377.             nmesg,maxblock,bsize);
  378.     }
  379.     if(api_register(messages,nmesg,1,maxblock,bsize) == 0)
  380.     {
  381.         printf("cannot register\n");
  382.         return 1;
  383.     }
  384.  
  385.     err= api_set_signal(apivec);
  386.     if(err)
  387.     {
  388.         printf("cannot set signal\n");
  389.         return 1;
  390.     }
  391.  
  392.     err= listen_req((byte)0,InfoMask,(word)(1 << own_eaz),(word)0x80);
  393.     if(err)
  394.     {
  395.         printf("Cannot listen %x\n",err);
  396.         api_set_signal((long)0);
  397.         return 1;
  398.     }
  399.  
  400.     err= dirps();
  401.     SaveTimerInt = getvect(8);
  402.     setvect(8,timevec);
  403.     restore(err);
  404.     if(check_listen())
  405.     {
  406.         printf("Cannot Listen\n");
  407.         api_set_signal((long)0);
  408.         setvect(8,SaveTimerInt);
  409.         return 1;
  410.     }
  411.     return 0;
  412. }
  413.  
  414. void
  415. api_stop(void)
  416. {
  417.     word err;
  418.     setvect(8,SaveTimerInt);
  419.     if(api_set_signal((long) 0)) printf("cannot reset signal\n");
  420.     err= api_release();
  421.     if(err) printf("release err %x\n",err);
  422. }
  423.