Make install now works again. (Missing and required macro defintion)
[lcr.git] / endpoint.cpp
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** The Endpoint is the link between the join and the port.                   **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13
14 unsigned int epoint_serial = 1; /* initial value must be 1, because 0== no epoint */
15
16 class Endpoint *epoint_first = NULL;
17
18
19 /*
20  * find the epoint with epoint_id
21  */ 
22 class Endpoint *find_epoint_id(unsigned int epoint_id)
23 {
24         class Endpoint *epoint = epoint_first;
25
26         while(epoint) {
27 //printf("comparing: '%s' with '%s'\n", name, epoint->name);
28                 if (epoint->ep_serial == epoint_id)
29                         return(epoint);
30                 epoint = epoint->next;
31         }
32
33         return(NULL);
34 }
35
36 int delete_endpoint(struct lcr_work *work, void *instance, int index);
37
38 /*
39  * endpoint constructor (link with either port or join id)
40  */
41 Endpoint::Endpoint(unsigned int port_id, unsigned int join_id)
42 {
43         class Port *port;
44         class Endpoint **epointpointer;
45         int earlyb = 0;
46
47         /* epoint structure */
48         PDEBUG(DEBUG_EPOINT, "EPOINT(%d): Allocating enpoint %d and connecting it with:%s%s\n", epoint_serial, epoint_serial, (port_id)?" ioport":"", (join_id)?" join":"");
49
50         ep_portlist = NULL;
51         ep_app = NULL;
52         memset(&ep_delete, 0, sizeof(ep_delete));
53         add_work(&ep_delete, delete_endpoint, this, 0);
54         ep_use = 1;
55
56         /* add endpoint to chain */
57         next = NULL;
58         epointpointer = &epoint_first;
59         while(*epointpointer)
60                 epointpointer = &((*epointpointer)->next);
61         *epointpointer = this;
62
63         /* serial */
64         ep_serial = epoint_serial++;
65
66         /* link to join or port */
67         if (port_id) {
68                 port = find_port_id(port_id);
69                 if (port) {
70                         if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_mISDN)
71                                 earlyb = ((class PmISDN *)port)->p_m_mISDNport->earlyb;
72                         if (!portlist_new(port_id, port->p_type, earlyb))
73                                 FATAL("No memory for portlist.\n");
74                 }
75         }
76         ep_join_id = join_id;
77
78         ep_park = 0;
79         ep_park_len = 0;
80
81         classuse++;
82 }
83
84
85 /*
86  * endpoint destructor
87  */
88 Endpoint::~Endpoint(void)
89 {
90         class Endpoint *temp, **tempp;
91         struct port_list *portlist, *mtemp;
92
93         classuse--;
94
95         /* remote application */
96         if (ep_app)
97                 delete ep_app;
98         
99         /* free relations */
100         if (ep_join_id) {
101                 PERROR("warning: still relation to join.\n");
102         }
103
104         /* free portlist */
105         portlist = ep_portlist;
106         while(portlist) {
107                 if (portlist->port_id) {
108                         PERROR("warning: still relation to port (portlist list)\n");
109                 }
110                 mtemp = portlist;
111                 portlist = portlist->next;
112                 memset(mtemp, 0, sizeof(struct port_list));
113                 FREE(mtemp, sizeof(struct port_list));
114                 ememuse--;
115         }
116
117         /* detach */
118         temp =epoint_first;
119         tempp = &epoint_first;
120         while(temp) {
121                 if (temp == this)
122                         break;
123
124                 tempp = &temp->next;
125                 temp = temp->next;
126         }
127         if (temp == 0)
128                 FATAL("Endpoint not in Endpoint's list.\n");
129         *tempp = next;
130
131         del_work(&ep_delete);
132
133         /* free */
134         PDEBUG(DEBUG_EPOINT, "removed endpoint %d.\n", ep_serial);
135 }
136
137 /* create new portlist relation
138  */
139 struct port_list *Endpoint::portlist_new(unsigned int port_id, int port_type, int earlyb)
140 {
141         struct port_list *portlist, **portlistpointer;
142
143         /* portlist structure */
144         portlist = (struct port_list *)MALLOC(sizeof(struct port_list));
145         ememuse++;
146         PDEBUG(DEBUG_EPOINT, "EPOINT(%d) allocating port_list.\n", ep_serial);
147
148         /* add port_list to chain */
149         portlist->next = NULL;
150         portlistpointer = &ep_portlist;
151         while(*portlistpointer)
152                 portlistpointer = &((*portlistpointer)->next);
153         *portlistpointer = portlist;
154
155         /* link to join or port */
156         portlist->port_id = port_id;
157         portlist->port_type = port_type;
158         portlist->early_b = earlyb;
159
160         return(portlist);
161 }
162
163
164 /* free portlist relation
165  */
166 void Endpoint::free_portlist(struct port_list *portlist)
167 {
168         struct port_list *temp, **tempp;
169
170         temp = ep_portlist;
171         tempp = &ep_portlist;
172         while(temp) {
173                 if (temp == portlist)
174                         break;
175
176                 tempp = &temp->next;
177                 temp = temp->next;
178         }
179         if (!temp)
180                 FATAL("port_list not in Endpoint's list.\n");
181         /* detach */
182         *tempp=portlist->next;
183
184         /* free */
185         PDEBUG(DEBUG_EPOINT, "EPOINT(%d) removed port_list from endpoint\n", ep_serial);
186         FREE(portlist, sizeof(struct port_list));
187         ememuse--;
188 }
189
190
191 int delete_endpoint(struct lcr_work *work, void *instance, int index)
192 {
193         class Endpoint *ep = (class Endpoint *)instance;
194
195         if (ep->ep_use <= 0)
196                 delete ep;
197
198         return 0;
199 }
200