⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 isdn_divert.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Id: isdn_divert.c,v 1.6.6.3 2001/09/23 22:24:36 kai Exp $ * * DSS1 main diversion supplementary handling for i4l. * * Copyright 1999       by Werner Cornelius (werner@isdn4linux.de) *  * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. * */#include <linux/proc_fs.h>#include "isdn_divert.h"/**********************************//* structure keeping calling info *//**********************************/struct call_struc  { isdn_ctrl ics; /* delivered setup + driver parameters */    ulong divert_id; /* Id delivered to user */    unsigned char akt_state; /* actual state */    char deflect_dest[35]; /* deflection destination */       struct timer_list timer; /* timer control structure */    char info[90]; /* device info output */     struct call_struc *next; /* pointer to next entry */    struct call_struc *prev;  };/********************************************//* structure keeping deflection table entry *//********************************************/struct deflect_struc  { struct deflect_struc *next,*prev;     divert_rule rule; /* used rule */  };/*****************************************//* variables for main diversion services *//*****************************************//* diversion/deflection processes */static struct call_struc *divert_head = NULL; /* head of remembered entrys */static ulong next_id = 1; /* next info id */   static struct deflect_struc *table_head = NULL;static struct deflect_struc *table_tail = NULL; static unsigned char extern_wait_max = 4; /* maximum wait in s for external process */ DEFINE_SPINLOCK(divert_lock);/***************************//* timer callback function *//***************************/static void deflect_timer_expire(ulong arg){  unsigned long flags;  struct call_struc *cs = (struct call_struc *) arg;  spin_lock_irqsave(&divert_lock, flags);  del_timer(&cs->timer); /* delete active timer */  spin_unlock_irqrestore(&divert_lock, flags);  switch(cs->akt_state)   { case DEFLECT_PROCEED:       cs->ics.command = ISDN_CMD_HANGUP; /* cancel action */       divert_if.ll_cmd(&cs->ics);                   	         spin_lock_irqsave(&divert_lock, flags);       cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */       cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);       add_timer(&cs->timer);       spin_unlock_irqrestore(&divert_lock, flags);       break;     case DEFLECT_ALERT:       cs->ics.command = ISDN_CMD_REDIR; /* protocol */       strcpy(cs->ics.parm.setup.phone,cs->deflect_dest);       strcpy(cs->ics.parm.setup.eazmsn,"Testtext delayed");       divert_if.ll_cmd(&cs->ics);       spin_lock_irqsave(&divert_lock, flags);       cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */       cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);       add_timer(&cs->timer);       spin_unlock_irqrestore(&divert_lock, flags);       break;     case DEFLECT_AUTODEL:     default:       spin_lock_irqsave(&divert_lock, flags);       if (cs->prev)          cs->prev->next = cs->next; /* forward link */        else         divert_head = cs->next;       if (cs->next)         cs->next->prev = cs->prev; /* back link */                  spin_unlock_irqrestore(&divert_lock, flags);       kfree(cs);       return;   } /* switch */} /* deflect_timer_func *//*****************************************//* handle call forwarding de/activations *//* 0 = deact, 1 = act, 2 = interrogate   *//*****************************************/int cf_command(int drvid, int mode,                u_char proc, char *msn,                u_char service, char *fwd_nr, ulong *procid){ unsigned long flags;  int retval,msnlen;  int fwd_len;  char *p,*ielenp,tmp[60];  struct call_struc *cs;  if (strchr(msn,'.')) return(-EINVAL); /* subaddress not allowed in msn */  if ((proc & 0x7F) > 2) return(-EINVAL);  proc &= 3;  p = tmp;  *p++ = 0x30; /* enumeration */  ielenp = p++; /* remember total length position */  *p++ = 0xa; /* proc tag */  *p++ = 1;   /* length */  *p++ = proc & 0x7F; /* procedure to de/activate/interrogate */  *p++ = 0xa; /* service tag */  *p++ = 1;   /* length */  *p++ = service; /* service to handle */  if (mode == 1)    { if (!*fwd_nr) return(-EINVAL); /* destination missing */     if (strchr(fwd_nr,'.')) return(-EINVAL); /* subaddress not allowed */     fwd_len = strlen(fwd_nr);     *p++ = 0x30; /* number enumeration */     *p++ = fwd_len + 2; /* complete forward to len */      *p++ = 0x80; /* fwd to nr */     *p++ = fwd_len; /* length of number */     strcpy(p,fwd_nr); /* copy number */     p += fwd_len; /* pointer beyond fwd */   } /* activate */  msnlen = strlen(msn);  *p++ = 0x80; /* msn number */  if (msnlen > 1)   { *p++ = msnlen; /* length */     strcpy(p,msn);     p += msnlen;   }  else *p++ = 0;  *ielenp = p - ielenp - 1; /* set total IE length */   /* allocate mem for information struct */    if (!(cs = (struct call_struc *) kmalloc(sizeof(struct call_struc), GFP_ATOMIC)))              return(-ENOMEM); /* no memory */  init_timer(&cs->timer);  cs->info[0] = '\0';  cs->timer.function = deflect_timer_expire;  cs->timer.data = (ulong) cs; /* pointer to own structure */  cs->ics.driver = drvid;  cs->ics.command = ISDN_CMD_PROT_IO; /* protocol specific io */  cs->ics.arg = DSS1_CMD_INVOKE; /* invoke supplementary service */  cs->ics.parm.dss1_io.proc = (mode == 1) ? 7: (mode == 2) ? 11:8; /* operation */   cs->ics.parm.dss1_io.timeout = 4000; /* from ETS 300 207-1 */  cs->ics.parm.dss1_io.datalen = p - tmp; /* total len */  cs->ics.parm.dss1_io.data = tmp; /* start of buffer */    spin_lock_irqsave(&divert_lock, flags);  cs->ics.parm.dss1_io.ll_id = next_id++; /* id for callback */  spin_unlock_irqrestore(&divert_lock, flags);  *procid = cs->ics.parm.dss1_io.ll_id;    sprintf(cs->info,"%d 0x%lx %s%s 0 %s %02x %d%s%s\n",	  (!mode ) ? DIVERT_DEACTIVATE : (mode == 1) ? DIVERT_ACTIVATE : DIVERT_REPORT,          cs->ics.parm.dss1_io.ll_id,          (mode != 2) ? "" : "0 ",          divert_if.drv_to_name(cs->ics.driver),          msn,          service & 0xFF,          proc,          (mode != 1) ? "" : " 0 ",          (mode != 1) ? "" : fwd_nr);   retval = divert_if.ll_cmd(&cs->ics); /* excute command */  if (!retval)   { cs->prev = NULL;     spin_lock_irqsave(&divert_lock, flags);     cs->next = divert_head;     divert_head = cs;      spin_unlock_irqrestore(&divert_lock, flags);   }  else   kfree(cs);  return(retval); } /* cf_command *//****************************************//* handle a external deflection command *//****************************************/int deflect_extern_action(u_char cmd, ulong callid, char *to_nr){ struct call_struc *cs;  isdn_ctrl ic;  unsigned long flags;  int i;  if ((cmd & 0x7F) > 2) return(-EINVAL); /* invalid command */  cs = divert_head; /* start of parameter list */  while (cs)   { if (cs->divert_id == callid) break; /* found */     cs = cs->next;     } /* search entry */  if (!cs) return(-EINVAL); /* invalid callid */  ic.driver = cs->ics.driver;  ic.arg = cs->ics.arg;  i = -EINVAL;  if (cs->akt_state == DEFLECT_AUTODEL) return(i); /* no valid call */  switch (cmd & 0x7F)   { case 0: /* hangup */       del_timer(&cs->timer);        ic.command = ISDN_CMD_HANGUP;       i = divert_if.ll_cmd(&ic);       spin_lock_irqsave(&divert_lock, flags);       cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */       cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);       add_timer(&cs->timer);       spin_unlock_irqrestore(&divert_lock, flags);     break;           case 1: /* alert */       if (cs->akt_state == DEFLECT_ALERT) return(0);       cmd &= 0x7F; /* never wait */       del_timer(&cs->timer);        ic.command = ISDN_CMD_ALERT;       if ((i = divert_if.ll_cmd(&ic)))	{          spin_lock_irqsave(&divert_lock, flags);          cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */          cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);          add_timer(&cs->timer);          spin_unlock_irqrestore(&divert_lock, flags);        }       else          cs->akt_state = DEFLECT_ALERT;      break;           case 2: /* redir */       del_timer(&cs->timer);        strcpy(cs->ics.parm.setup.phone, to_nr);       strcpy(cs->ics.parm.setup.eazmsn, "Testtext manual");       ic.command = ISDN_CMD_REDIR;       if ((i = divert_if.ll_cmd(&ic)))	{          spin_lock_irqsave(&divert_lock, flags);          cs->akt_state = DEFLECT_AUTODEL; /* delete after timeout */          cs->timer.expires = jiffies + (HZ * AUTODEL_TIME);          add_timer(&cs->timer);          spin_unlock_irqrestore(&divert_lock, flags);        }       else          cs->akt_state = DEFLECT_ALERT;      break;         } /* switch */  return(i);} /* deflect_extern_action *//********************************//* insert a new rule before idx *//********************************/int insertrule(int idx, divert_rule *newrule){ struct deflect_struc *ds,*ds1=NULL;  unsigned long flags;  if (!(ds = (struct deflect_struc *) kmalloc(sizeof(struct deflect_struc),                                               GFP_KERNEL)))     return(-ENOMEM); /* no memory */  ds->rule = *newrule; /* set rule */  spin_lock_irqsave(&divert_lock, flags);  if (idx >= 0)   { ds1 = table_head;     while ((ds1) && (idx > 0))      { idx--;        ds1 = ds1->next;      }      if (!ds1) idx = -1;    }  if (idx < 0)   { ds->prev = table_tail; /* previous entry */     ds->next = NULL; /* end of chain */     if (ds->prev)        ds->prev->next = ds; /* last forward */      else        table_head = ds; /* is first entry */     table_tail = ds; /* end of queue */   }  else    { ds->next = ds1; /* next entry */      ds->prev = ds1->prev; /* prev entry */      ds1->prev = ds; /* backward chain old element */      if (!ds->prev)        table_head = ds; /* first element */   }  spin_unlock_irqrestore(&divert_lock, flags);  return(0);} /* insertrule *//***********************************//* delete the rule at position idx *//***********************************/int deleterule(int idx){ struct deflect_struc *ds,*ds1;  unsigned long flags;    if (idx < 0)    { spin_lock_irqsave(&divert_lock, flags);     ds = table_head;     table_head = NULL;     table_tail = NULL;     spin_unlock_irqrestore(&divert_lock, flags);     while (ds)      { ds1 = ds;         ds = ds->next;        kfree(ds1);      }      return(0);    }  spin_lock_irqsave(&divert_lock, flags);  ds = table_head;  while ((ds) && (idx > 0))   { idx--;      ds = ds->next;     }  if (!ds)    {     spin_unlock_irqrestore(&divert_lock, flags);     return(-EINVAL);   }    if (ds->next)     ds->next->prev = ds->prev; /* backward chain */   else     table_tail = ds->prev; /* end of chain */  if (ds->prev)    ds->prev->next = ds->next; /* forward chain */   else     table_head = ds->next; /* start of chain */          spin_unlock_irqrestore(&divert_lock, flags);  kfree(ds);  return(0);} /* deleterule *//*******************************************//* get a pointer to a specific rule number *//*******************************************/divert_rule *getruleptr(int idx){ struct deflect_struc *ds = table_head;    if (idx < 0) return(NULL);  while ((ds) && (idx >= 0))   { if (!(idx--))       { return(&ds->rule);        break;      }     ds = ds->next;     }  return(NULL);} /* getruleptr *//*************************************************//* called from common module on an incoming call *//*************************************************/static int isdn_divert_icall(isdn_ctrl *ic){ int retval = 0;  unsigned long flags;  struct call_struc *cs = NULL;   struct deflect_struc *dv;  char *p,*p1;  u_char accept;  /* first check the internal deflection table */  for (dv = table_head; dv ; dv = dv->next )   { /* scan table */     if (((dv->rule.callopt == 1) && (ic->command == ISDN_STAT_ICALLW)) ||         ((dv->rule.callopt == 2) && (ic->command == ISDN_STAT_ICALL)))       continue; /* call option check */       if (!(dv->rule.drvid & (1L << ic->driver)))        continue; /* driver not matching */      if ((dv->rule.si1) && (dv->rule.si1 != ic->parm.setup.si1))        continue; /* si1 not matching */     if ((dv->rule.si2) && (dv->rule.si2 != ic->parm.setup.si2))        continue; /* si2 not matching */     p = dv->rule.my_msn;     p1 = ic->parm.setup.eazmsn;     accept = 0;     while (*p)      { /* complete compare */        if (*p == '-')	  { accept = 1; /* call accepted */            break;          }        if (*p++ != *p1++)           break; /* not accepted */        if ((!*p) && (!*p1))          accept = 1;      } /* complete compare */     if (!accept) continue; /* not accepted */      if ((strcmp(dv->rule.caller,"0")) || (ic->parm.setup.phone[0]))      { p = dv->rule.caller;        p1 = ic->parm.setup.phone;        accept = 0;        while (*p)	 { /* complete compare */           if (*p == '-')	    { accept = 1; /* call accepted */              break;            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -