Skip to content
Snippets Groups Projects
app_getcpeid.c 3.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mark Spencer's avatar
    Mark Spencer committed
    /*
    
     * Asterisk -- An open source telephony toolkit.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Copyright (C) 1999 - 2005, Digium, Inc.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
    Mark Spencer's avatar
    Mark Spencer committed
     * Mark Spencer <markster@digium.com>
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
    
    Mark Spencer's avatar
    Mark Spencer committed
     * This program is free software, distributed under the terms of
    
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    
    Russell Bryant's avatar
    Russell Bryant committed
     * \brief Get ADSI CPE ID
    
     *
     * \author Mark Spencer <markster@digium.com>
    
    Russell Bryant's avatar
    Russell Bryant committed
     * \ingroup applications
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    #include <stdlib.h>
    
    #include <stdio.h>
    
    #include <unistd.h>
    #include <string.h>
    
    
    #include "asterisk/lock.h"
    #include "asterisk/file.h"
    #include "asterisk/logger.h"
    #include "asterisk/channel.h"
    #include "asterisk/pbx.h"
    #include "asterisk/module.h"
    #include "asterisk/adsi.h"
    #include "asterisk/options.h"
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    static char *app = "GetCPEID";
    
    static char *synopsis = "Get ADSI CPE ID";
    
    static char *descrip =
    
    Mark Spencer's avatar
    Mark Spencer committed
    "  GetCPEID: Obtains and displays ADSI CPE ID and other information in order\n"
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    "to properly setup zapata.conf for on-hook operations.\n";
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    
    static int cpeid_setstatus(struct ast_channel *chan, char *stuff[], int voice)
    {
    	int justify[5] = { ADSI_JUST_CENT, ADSI_JUST_LEFT, ADSI_JUST_LEFT, ADSI_JUST_LEFT };
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int x;
    	for (x=0;x<4;x++)
    		tmp[x] = stuff[x];
    	tmp[4] = NULL;
    
    	return ast_adsi_print(chan, tmp, justify, voice);
    
    Mark Spencer's avatar
    Mark Spencer committed
    }
    
    static int cpeid_exec(struct ast_channel *chan, void *idata)
    {
    	int res=0;
    
    	struct ast_module_user *u;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	unsigned char cpeid[4];
    	int gotgeometry = 0;
    	int gotcpeid = 0;
    	int width, height, buttons;
    	char data[4][80];
    	char *stuff[4];
    
    
    	u = ast_module_user_add(chan);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	stuff[0] = data[0];
    	stuff[1] = data[1];
    	stuff[2] = data[2];
    	stuff[3] = data[3];
    	memset(data, 0, sizeof(data));
    
    	strncpy(stuff[0], "** CPE Info **", sizeof(data[0]) - 1);
    	strncpy(stuff[1], "Identifying CPE...", sizeof(data[1]) - 1);
    	strncpy(stuff[2], "Please wait...", sizeof(data[2]) - 1);
    
    	res = ast_adsi_load_session(chan, NULL, 0, 1);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (res > 0) {
    		cpeid_setstatus(chan, stuff, 0);
    
    		res = ast_adsi_get_cpeid(chan, cpeid, 0);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (res > 0) {
    			gotcpeid = 1;
    			if (option_verbose > 2)
    				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);
    		}
    		if (res > -1) {
    
    			strncpy(stuff[1], "Measuring CPE...", sizeof(data[1]) - 1);
    			strncpy(stuff[2], "Please wait...", sizeof(data[2]) - 1);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			cpeid_setstatus(chan, stuff, 0);
    
    			res = ast_adsi_get_cpeinfo(chan, &width, &height, &buttons, 0);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			if (res > -1) {
    				if (option_verbose > 2)
    					ast_verbose(VERBOSE_PREFIX_3 "CPE has %d lines, %d columns, and %d buttons on '%s'\n", height, width, buttons, chan->name);
    				gotgeometry = 1;
    			}
    		}
    		if (res > -1) {
    			if (gotcpeid)
    
    				snprintf(stuff[1], sizeof(data[1]), "CPEID: %02x:%02x:%02x:%02x", cpeid[0], cpeid[1], cpeid[2], cpeid[3]);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			else
    
    				strncpy(stuff[1], "CPEID Unknown", sizeof(data[1]) - 1);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			if (gotgeometry) 
    
    				snprintf(stuff[2], sizeof(data[2]), "Geom: %dx%d, %d buttons", width, height, buttons);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			else
    
    				strncpy(stuff[2], "Geometry unknown", sizeof(data[2]) - 1);
    			strncpy(stuff[3], "Press # to exit", sizeof(data[3]) - 1);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			cpeid_setstatus(chan, stuff, 1);
    			for(;;) {
    				res = ast_waitfordigit(chan, 1000);
    				if (res < 0)
    					break;
    				if (res == '#') {
    					res = 0;
    					break;
    				}
    			}
    
    			ast_adsi_unload_session(chan);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return res;
    }
    
    
    static int unload_module(void)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	ast_module_user_hangup_all();
    
    static int load_module(void)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	return ast_register_application(app, cpeid_exec, synopsis, descrip);
    }
    
    
    AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get ADSI CPE ID");