Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / i4b / isdnd / dial.c
1 /*
2  * Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      i4b daemon - dial handling routines
28  *      -----------------------------------
29  *
30  *      $Id: dial.c,v 1.8 1999/12/13 21:25:24 hm Exp $ 
31  *
32  * $FreeBSD: src/usr.sbin/i4b/isdnd/dial.c,v 1.6.2.2 2001/12/16 15:13:38 hm Exp $
33  * $DragonFly: src/usr.sbin/i4b/isdnd/dial.c,v 1.2 2003/06/17 04:29:54 dillon Exp $
34  *
35  *      last edit-date: [Mon Dec 13 21:45:51 1999]
36  *
37  *---------------------------------------------------------------------------*/
38
39 #include "isdnd.h"
40
41 /*---------------------------------------------------------------------------*
42  *      select the first remote number to dial according to the
43  *      dial strategy
44  *---------------------------------------------------------------------------*/
45 void
46 select_first_dialno(cfg_entry_t *cep)
47 {
48         int i, j;
49
50         if(cep->keypad[0] != '\0')
51                 return;
52
53         if(cep->remote_numbers_count < 1)
54         {
55                 log(LL_ERR, "select_first_dialno: remote_numbers_count < 1!");
56                 return;
57         }
58
59         if(cep->remote_numbers_count == 1)
60         {
61                 strcpy(cep->remote_phone_dialout, cep->remote_numbers[0].number);
62                 DBGL(DL_DIAL, (log(LL_DBG, "select_first_dialno: only one no, no = %s", cep->remote_phone_dialout)));
63                 cep->last_remote_number = 0;
64                 return;
65         }
66
67         if(cep->remote_numbers_handling == RNH_FIRST)
68         {
69                 strcpy(cep->remote_phone_dialout, cep->remote_numbers[0].number);
70                 DBGL(DL_DIAL, (log(LL_DBG, "select_first_dialno: use first, no = %s", cep->remote_phone_dialout)));
71                 cep->last_remote_number = 0;
72                 return;
73         }
74
75         i = cep->last_remote_number;
76            
77         for(j = cep->remote_numbers_count; j > 0; j--)
78         {
79                 if(cep->remote_numbers[i].flag == RNF_SUCC)
80                 {
81                         if(cep->remote_numbers_handling == RNH_LAST)
82                         {
83                                 strcpy(cep->remote_phone_dialout, cep->remote_numbers[i].number);
84                                 DBGL(DL_DIAL, (log(LL_DBG, "select_first_dialno: use last, no = %s", cep->remote_phone_dialout)));
85                                 cep->last_remote_number = i;
86                                 return;
87                         }
88                         else
89                         {
90                                 if(++i >= cep->remote_numbers_count)
91                                         i = 0;
92
93                                 strcpy(cep->remote_phone_dialout, cep->remote_numbers[i].number);
94                                 DBGL(DL_DIAL, (log(LL_DBG, "select_first_dialno: use next, no = %s", cep->remote_phone_dialout)));
95                                 cep->last_remote_number = i;
96                                 return;
97                         }
98                 }
99
100                 if(++i >= cep->remote_numbers_count)
101                         i = 0;
102         }
103         strcpy(cep->remote_phone_dialout, cep->remote_numbers[0].number);
104         DBGL(DL_DIAL, (log(LL_DBG, "select_first_dialno: no last found (use 0), no = %s", cep->remote_phone_dialout)));
105         cep->last_remote_number = 0;    
106 }                                                                       
107
108 /*---------------------------------------------------------------------------*
109  *      select next remote number to dial (last was unsuccesfull)
110  *---------------------------------------------------------------------------*/
111 void
112 select_next_dialno(cfg_entry_t *cep)
113 {
114         if(cep->remote_numbers_count < 1)
115         {
116                 log(LL_ERR, "select_next_dialno: remote_numbers_count < 1!");
117                 return;
118         }
119
120         if(cep->remote_numbers_count == 1)
121         {
122                 strcpy(cep->remote_phone_dialout, cep->remote_numbers[0].number);
123                 DBGL(DL_DIAL, (log(LL_DBG, "select_next_dialno: only one no, no = %s", cep->remote_phone_dialout)));
124                 cep->last_remote_number = 0;
125                 return;
126         }
127
128         /* mark last try as bad */
129
130         cep->remote_numbers[cep->last_remote_number].flag = RNF_IDLE;
131
132         /* next one to try */
133         
134         cep->last_remote_number++;
135
136         if(cep->last_remote_number >= cep->remote_numbers_count)
137                 cep->last_remote_number = 0;
138
139         strcpy(cep->remote_phone_dialout, cep->remote_numbers[cep->last_remote_number].number);
140         
141         DBGL(DL_DIAL, (log(LL_DBG, "select_next_dialno: index=%d, no=%s",
142                 cep->last_remote_number,
143                 cep->remote_numbers[cep->last_remote_number].number)));
144 }                                                                       
145
146 /*---------------------------------------------------------------------------*
147  *      dial succeded, store this number as the last successful
148  *---------------------------------------------------------------------------*/
149 void
150 select_this_dialno(cfg_entry_t *cep)
151 {
152         cep->remote_numbers[cep->last_remote_number].flag = RNF_SUCC;
153         
154         DBGL(DL_DIAL, (log(LL_DBG, "select_this_dialno: index = %d, no = %s",
155                 cep->last_remote_number,
156                 cep->remote_numbers[cep->last_remote_number].number)));
157 }
158
159 /* EOF */