Version 0.1.12 from FTP
[asterisk/asterisk.git] / apps / app_getcpeid.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Execute arbitrary system commands
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/module.h>
20 #include <asterisk/adsi.h>
21 #include <asterisk/options.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <pthread.h>
28
29
30 static char *tdesc = "Get ADSI CPE ID";
31
32 static char *app = "GetCPEID";
33
34 static char *synopsis = "Get ADSI CPE ID";
35
36 static char *descrip =
37 "  GetCPEID: Obtains and displays CPE ID and other information in order to\n"
38 "properly setup zapata.conf for on-hook operations.  Returns -1 on hanup\n"
39 "only.";
40
41 STANDARD_LOCAL_USER;
42
43 LOCAL_USER_DECL;
44
45 static int cpeid_setstatus(struct ast_channel *chan, char *stuff[], int voice)
46 {
47         int justify[5] = { ADSI_JUST_CENT, ADSI_JUST_LEFT, ADSI_JUST_LEFT, ADSI_JUST_LEFT };
48         char *tmp[5];
49         int x;
50         for (x=0;x<4;x++)
51                 tmp[x] = stuff[x];
52         tmp[4] = NULL;
53         return adsi_print(chan, tmp, justify, voice);
54 }
55
56 static int cpeid_exec(struct ast_channel *chan, void *idata)
57 {
58         int res=0;
59         struct localuser *u;
60         unsigned char cpeid[4];
61         int gotgeometry = 0;
62         int gotcpeid = 0;
63         int width, height, buttons;
64         char data[4][80];
65         char *stuff[4];
66
67         LOCAL_USER_ADD(u);
68         stuff[0] = data[0];
69         stuff[1] = data[1];
70         stuff[2] = data[2];
71         stuff[3] = data[3];
72         memset(data, 0, sizeof(data));
73         strcpy(stuff[0], "** CPE Info **");
74         strcpy(stuff[1], "Identifying CPE...");
75         strcpy(stuff[2], "Please wait...");
76         res = adsi_load_session(chan, NULL, 0, 1);
77         if (res > 0) {
78                 cpeid_setstatus(chan, stuff, 0);
79                 res = adsi_get_cpeid(chan, cpeid, 0);
80                 if (res > 0) {
81                         gotcpeid = 1;
82                         if (option_verbose > 2)
83                                 ast_verbose(VERBOSE_PREFIX_3 "Got CPEID of '%02x:%02x:%02x:%02x' on '%s'\n", cpeid[0], cpeid[1], cpeid[2], cpeid[3], chan->name);
84                 }
85                 if (res > -1) {
86                         strcpy(stuff[1], "Measuring CPE...");
87                         strcpy(stuff[2], "Please wait...");
88                         cpeid_setstatus(chan, stuff, 0);
89                         res = adsi_get_cpeinfo(chan, &width, &height, &buttons, 0);
90                         if (res > -1) {
91                                 if (option_verbose > 2)
92                                         ast_verbose(VERBOSE_PREFIX_3 "CPE has %d lines, %d columns, and %d buttons on '%s'\n", height, width, buttons, chan->name);
93                                 gotgeometry = 1;
94                         }
95                 }
96                 if (res > -1) {
97                         if (gotcpeid)
98                                 sprintf(stuff[1], "CPEID: %02x:%02x:%02x:%02x", cpeid[0], cpeid[1], cpeid[2], cpeid[3]);
99                         else
100                                 strcpy(stuff[1], "CPEID Unknown");
101                         if (gotgeometry) 
102                                 sprintf(stuff[2], "Geom: %dx%d, %d buttons", width, height, buttons);
103                         else
104                                 strcpy(stuff[2], "Geometry unknown");
105                         strcpy(stuff[3], "Press # to exit");
106                         cpeid_setstatus(chan, stuff, 1);
107                         for(;;) {
108                                 res = ast_waitfordigit(chan, 1000);
109                                 if (res < 0)
110                                         break;
111                                 if (res == '#') {
112                                         res = 0;
113                                         break;
114                                 }
115                         }
116                         adsi_unload_session(chan);
117                 }
118         }
119         LOCAL_USER_REMOVE(u);
120         return res;
121 }
122
123 int unload_module(void)
124 {
125         STANDARD_HANGUP_LOCALUSERS;
126         return ast_unregister_application(app);
127 }
128
129 int load_module(void)
130 {
131         return ast_register_application(app, cpeid_exec, synopsis, descrip);
132 }
133
134 char *description(void)
135 {
136         return tdesc;
137 }
138
139 int usecount(void)
140 {
141         int res;
142         STANDARD_USECOUNT(res);
143         return res;
144 }
145
146 char *key()
147 {
148         return ASTERISK_GPL_KEY;
149 }