Merge "res_musiconhold: Start playlist after initial announcement"
[asterisk/asterisk.git] / apps / app_getcpeid.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Get ADSI CPE ID
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27
28 /*** MODULEINFO
29         <support_level>extended</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 #include "asterisk/lock.h"
35 #include "asterisk/file.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/module.h"
39 #include "asterisk/adsi.h"
40
41 /*** DOCUMENTATION
42         <application name="GetCPEID" language="en_US">
43                 <synopsis>
44                         Get ADSI CPE ID.
45                 </synopsis>
46                 <syntax />
47                 <description>
48                         <para>Obtains and displays ADSI CPE ID and other information in order
49                         to properly setup <filename>dahdi.conf</filename> for on-hook operations.</para>
50                 </description>
51         </application>
52  ***/
53 static char *app = "GetCPEID";
54
55 static int cpeid_setstatus(struct ast_channel *chan, char *stuff[], int voice)
56 {
57         int justify[5] = { ADSI_JUST_CENT, ADSI_JUST_LEFT, ADSI_JUST_LEFT, ADSI_JUST_LEFT };
58         char *tmp[5];
59         int x;
60         for (x=0;x<4;x++)
61                 tmp[x] = stuff[x];
62         tmp[4] = NULL;
63         return ast_adsi_print(chan, tmp, justify, voice);
64 }
65
66 static int cpeid_exec(struct ast_channel *chan, const char *idata)
67 {
68         int res=0;
69         unsigned char cpeid[4];
70         int gotgeometry = 0;
71         int gotcpeid = 0;
72         int width, height, buttons;
73         char *data[4];
74         unsigned int x;
75
76         for (x = 0; x < 4; x++)
77                 data[x] = ast_alloca(80);
78
79         strcpy(data[0], "** CPE Info **");
80         strcpy(data[1], "Identifying CPE...");
81         strcpy(data[2], "Please wait...");
82         res = ast_adsi_load_session(chan, NULL, 0, 1);
83         if (res > 0) {
84                 cpeid_setstatus(chan, data, 0);
85                 res = ast_adsi_get_cpeid(chan, cpeid, 0);
86                 if (res > 0) {
87                         gotcpeid = 1;
88                         ast_verb(3, "Got CPEID of '%02hhx:%02hhx:%02hhx:%02hhx' on '%s'\n",
89                                 cpeid[0], cpeid[1], cpeid[2], cpeid[3], ast_channel_name(chan));
90                 }
91                 if (res > -1) {
92                         strcpy(data[1], "Measuring CPE...");
93                         strcpy(data[2], "Please wait...");
94                         cpeid_setstatus(chan, data, 0);
95                         res = ast_adsi_get_cpeinfo(chan, &width, &height, &buttons, 0);
96                         if (res > -1) {
97                                 ast_verb(3, "CPE has %d lines, %d columns, and %d buttons on '%s'\n", height, width, buttons, ast_channel_name(chan));
98                                 gotgeometry = 1;
99                         }
100                 }
101                 if (res > -1) {
102                         if (gotcpeid)
103                                 snprintf(data[1], 80, "CPEID: %02hhx:%02hhx:%02hhx:%02hhx",
104                                         cpeid[0], cpeid[1], cpeid[2], cpeid[3]);
105                         else
106                                 strcpy(data[1], "CPEID Unknown");
107                         if (gotgeometry) 
108                                 snprintf(data[2], 80, "Geom: %dx%d, %d buttons", width, height, buttons);
109                         else
110                                 strcpy(data[2], "Geometry unknown");
111                         strcpy(data[3], "Press # to exit");
112                         cpeid_setstatus(chan, data, 1);
113                         for(;;) {
114                                 res = ast_waitfordigit(chan, 1000);
115                                 if (res < 0)
116                                         break;
117                                 if (res == '#') {
118                                         res = 0;
119                                         break;
120                                 }
121                         }
122                         ast_adsi_unload_session(chan);
123                 }
124         }
125
126         return res;
127 }
128
129 static int unload_module(void)
130 {
131         return ast_unregister_application(app);
132 }
133
134 static int load_module(void)
135 {
136         return ast_register_application_xml(app, cpeid_exec);
137 }
138
139 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Get ADSI CPE ID",
140         .support_level = AST_MODULE_SUPPORT_EXTENDED,
141         .load = load_module,
142         .unload = unload_module,
143         .nonoptreq = "res_adsi",
144 );