File:  [ELWIX - Embedded LightWeight unIX -] / libaitpelco / src / get.c
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Thu May 30 09:20:16 2013 UTC (11 years ago) by misho
Branches: MAIN
CVS tags: pelco2_3, pelco2_2, pelco2_1, PELCO2_2, PELCO2_1, PELCO2_0, HEAD
version 2.0

    1: /*************************************************************************
    2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: get.c,v 1.4 2013/05/30 09:20:16 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: 
   48: 
   49: /*
   50:  * pelco_GetVersion() Return Pelco protocol version
   51:  * @p = Packet structure
   52:  * return: 'd' - PelcoD, 'p' - PelcoP, 0 - unknown or bad packet
   53:  */
   54: u_char
   55: pelco_GetVersion(void * __restrict p)
   56: {
   57: 	u_char *ptr = (u_char *) p;
   58: 
   59: 	if (!p || !*ptr) {
   60: 		pelcoSetErr(EINVAL, "invalid argument!\n");
   61: 		return 0;
   62: 	}
   63: 
   64: 	switch (*ptr) {
   65: 		case VER_D_SYNC:
   66: 			return 'd';
   67: 		case VER_P_STX:
   68: 			if (VER_P_ETX == ptr[6])
   69: 				return 'p';
   70: 			pelcoSetErr(ENOEXEC, "broken Pelco P packet ...\n");
   71: 			break;
   72: 		default:
   73: 			pelcoSetErr(ENOEXEC, "unsupported Pelco protocol version!\n");
   74: 	}
   75: 
   76: 	return 0;
   77: }
   78: 
   79: /*
   80:  * pelco_GetCamNo() Get Camera number and check for valid packet
   81:  * @p = Packet structure
   82:  * return: 0xFF and pelco_GetErrno() == ENOEXEC - error, 
   83:  	any number is camera address
   84:  */
   85: u_char
   86: pelco_GetCamNo(void * __restrict p)
   87: {
   88: 	u_char ver;
   89: 	u_char *ptr = (u_char *) p;
   90: 
   91: 	if (!p || !*ptr) {
   92: 		pelcoSetErr(EINVAL, "invalid argument!\n");
   93: 		return 0;
   94: 	}
   95: 
   96: 	ver = pelco_GetVersion(p);
   97: 	switch (ver) {
   98: 		case 'd':
   99: 			if (crcPelco(ver, p) == ptr[6])
  100: 				return ptr[1];
  101: 
  102: 			pelcoSetErr(ENOEXEC, "broken Pelco D packet!!!\n");
  103: 			break;
  104: 		case 'p':
  105: 			if (crcPelco(ver, p) == ptr[7])
  106: 				return ptr[1] + 1;
  107: 
  108: 			pelcoSetErr(ENOEXEC, "broken Pelco P packet!!!\n");
  109: 			break;
  110: 	}
  111: 
  112: 	return 0xFF;
  113: }
  114: 
  115: /*
  116:  * pelco_GetCamCmdData() Get from Camera commands and datas with verify packet
  117:  * @p = Input Packet structure
  118:  * @cam = Output camera number
  119:  * @cmd[2] = Output Commands 1 & 2
  120:  * @data[2] = Output Data for commands 1 & 2
  121:  * return: 'd' - PelcoD, 'p' - PelcoP, 0 - unknown or bad packet
  122:  */
  123: u_char
  124: pelco_GetCamCmdData(void * __restrict p, u_char * __restrict cam, 
  125: 		u_char * __restrict cmd, u_char * __restrict data)
  126: {
  127: 	u_char ret;
  128: 	u_char *ptr = (u_char *) p;
  129: 
  130: 	if (!p || !*ptr) {
  131: 		pelcoSetErr(EINVAL, "invalid argument!\n");
  132: 		return 0;
  133: 	}
  134: 
  135: 	ret = pelco_GetVersion(p);
  136: 	switch (ret) {
  137: 		case 'd':
  138: 			if (crcPelco(ret, p) != ptr[6]) {
  139: 				pelcoSetErr(ENOEXEC, "broken Pelco D packet!!!\n");
  140: 				return 0;
  141: 			}
  142: 
  143: 			if (cam)
  144: 				*cam = ptr[1];
  145: 			break;
  146: 		case 'p':
  147: 			if (crcPelco(ret, p) != ptr[7]) {
  148: 				pelcoSetErr(ENOEXEC, "broken Pelco P packet!!!\n");
  149: 				return 0;
  150: 			}
  151: 
  152: 			if (cam)
  153: 				*cam = ptr[1] + 1;
  154: 			break;
  155: 	}
  156: 
  157: 	if (cmd)
  158: 		memcpy(cmd, ptr + 2, 2);
  159: 	if (data)
  160: 		memcpy(data, ptr + 4, 2);
  161: 	return ret;
  162: }
  163: 
  164: /*
  165:  * pelco_D_toCmd() Convert to Pelco D commands
  166:  * @cmd[2] = Input Commands 1 & 2
  167:  * @cmd1 = Output typecasted commands 1
  168:  * @cmd2 = Output typecasted commands 2
  169:  * return: 0xFF - error, !=0 return number arguments
  170:  */
  171: u_char
  172: pelco_D_toCmd(u_char * __restrict cmd, struct bitD_Cmd1 * __restrict cmd1, 
  173: 		struct bitD_Cmd2 * __restrict cmd2)
  174: {
  175: 	u_char ret = 0;
  176: 
  177: 	if (!cmd) {
  178: 		pelcoSetErr(EINVAL, "invalid argument!\n");
  179: 		return 0xFF;
  180: 	}
  181: 
  182: 	if (cmd1) {
  183: 		cmd1 = (struct bitD_Cmd1*) &cmd[0];
  184: 		ret++;
  185: 	}
  186: 	if (cmd2) {
  187: 		cmd2 = (struct bitD_Cmd2*) &cmd[1];
  188: 		ret++;
  189: 	}
  190: 
  191: 	return ret;
  192: }
  193: 
  194: /*
  195:  * pelco_P_toCmd() Convert to Pelco P commands
  196:  * @cmd[2] = Input Commands 1 & 2
  197:  * @cmd1 = Output typecasted commands 1
  198:  * @cmd2 = Output typecasted commands 2
  199:  * return: 0xFF - error, !=0 return number arguments
  200:  */
  201: u_char
  202: pelco_P_toCmd(u_char * __restrict cmd, struct bitP_Cmd1 * __restrict cmd1, 
  203: 		struct bitP_Cmd2 * __restrict cmd2)
  204: {
  205: 	u_char ret = 0;
  206: 
  207: 	if (!cmd) {
  208: 		pelcoSetErr(EINVAL, "invalid argument!\n");
  209: 		return 0xFF;
  210: 	}
  211: 
  212: 	if (cmd1) {
  213: 		cmd1 = (struct bitP_Cmd1*) &cmd[0];
  214: 		ret++;
  215: 	}
  216: 	if (cmd2) {
  217: 		cmd2 = (struct bitP_Cmd2*) &cmd[1];
  218: 		ret++;
  219: 	}
  220: 
  221: 	return ret;
  222: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>