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