/*
* Copyright (c) 2007,中星微电子有限公司或Vimicro
* All rights reserved.
*
* 文件名称: IpCamXml_Codec.cpp
* 文件标识:
* 摘 要: IpCamXml配置信息解码文件
*
* 版本:1.1
* 作 者:输入作者(或修改者)名字
* 完成日期:200*年*月*日
*
* 当前版本: 1.0
* 原作者 : lxt
* 完成日期: 2007年5月11日
*/
#include "stdafx.h"
#include "osplatform.h"
#include "IpCamXml_Codec.h"
#include <string.h>
#include "xmlpub.h"
#include "mysocket.h"
#include <stdlib.h>
//#include "HTTPDownload.h"
int EncodeIpCamXMLConfig(char *, unsigned short , Ipcam_Config_Req_T *);
int DecodeICCSipService(char **, unsigned short, Sip_Service *);
int DecodeICCCamSetting(char **, unsigned short, Cam_Setting *);
int DecodeICCSystemSet(char **, unsigned short, System_Set *);
//int DecodeICCAccountSettings(char **, unsigned short, Account_Settings *);
int DecodeICCFtpUpLoad(char **, unsigned short, Ftp_Upload *);
int DecodeICCFtpConfigFile(char **, unsigned short, Ftp_Configfile *);
int DecodeICCDefaultVideoParams(char **, unsigned short, DefaultVideoParams *);
int DecodeICCCryptogram(char **, unsigned short, Cryptogram *);
int DecodeICCTimer(char **, unsigned short, Timer *);
//int GetIpCamCfg(char *, unsigned short, char *, IpCam_Config_T *);
// pchUrl: include port number
/*
int GetIpCamCfg(char *pchUrl, char *chIpCamID, IpCam_Config_T *ptDe)
{
return 0;
}
*/
int DecodeIpCamXMLConfig(char *p, unsigned short wLen, IpCam_Config_T *ptDe)
{
int c=0;
int rtn = 0;
char *str;
memset(ptDe, 0, sizeof(*ptDe));
SKIPSPACE(p,wLen,c);
if (strncmp(p, "<?xml", 5))
{
return -1;
}
p+=5;
//skip declare: <?xml version="1.0" encoding="UTF-8"?>
str = strstr(p,"?>");
if((str==NULL)||((p-str)>=LOGN_NAME_LTH))
return -1;
p=str+2; //skip "?>"
SKIPSPACE(p,wLen,c);
SkipComment((const char **)&p,wLen-c);
SKIPSPACE(p,wLen,c);
// decode root node
if (strncmp(p, "<IpCam_CfgResponse>", 19))
{
return -1;
}
p+=19;
/////////// decode sub-nodes /////////////////////
while (*p!='\0')
{
SKIPSPACE(p,wLen,c);
SkipComment((const char **)&p,wLen-c);
SKIPSPACE(p,wLen,c);
if (!strncmp(p, "<SipService>", 12))
{
p+=12; //remove header
rtn = DecodeICCSipService(&p, 1000, &ptDe->tSipService);
if (rtn!=0)
{
return rtn;
}
}
else if (!strncmp(p, "<CamSetting>", 12))
{
p+=12; //remove header
rtn = DecodeICCCamSetting(&p, 1000, &ptDe->tCamSetting);
if (rtn!=0)
{
return rtn;
}
}
else if (!strncmp(p, "<SystemSet>", 11))
{
p+=11; //remove header
rtn = DecodeICCSystemSet(&p, 1000, &ptDe->tSystemSet);
if (rtn!=0)
{
return rtn;
}
}
/* else if (!strncmp(p, "<AccountSettings>", 17))
{
p+=17; //remove header
rtn = DecodeICCAccountSettings(&p, 1000, &ptDe->tAcntSettings);
if (rtn!=0)
{
return rtn;
}
}*/
else if (!strncmp(p, "<FTPUpLoad>", 11))
{
p+=11; //remove header
rtn = DecodeICCFtpUpLoad(&p, 1000, &ptDe->tFtpUpload);
if (rtn!=0)
{
return rtn;
}
}
else if (!strncmp(p, "<FTPConfigFile>", 15))
{
p+=15; //remove header
rtn = DecodeICCFtpConfigFile(&p, 1000, &ptDe->tFtpConfigFile);
if (rtn!=0)
{
return rtn;
}
}
else if (!strncmp(p, "<DefaultVideoParams>", 20))
{
p+=20; //remove header
rtn = DecodeICCDefaultVideoParams(&p, 1000, &ptDe->tDfltVideoParams);
if (rtn!=0)
{
return rtn;
}
}
else if (!strncmp(p, "<Cryptogram>", 12))
{
p+=12; //remove header
rtn = DecodeICCCryptogram(&p, 1000, &ptDe->tCryptogram);
if (rtn!=0)
{
return rtn;
}
}
else if (!strncmp(p, "<Timer>", 7))
{
p+=7; //remove header
rtn = DecodeICCTimer(&p, 1000, &ptDe->tTimer);
if (rtn!=0)
{
return rtn;
}
}
else if (!strncmp(p, "</IpCam_CfgResponse>", 20))
{
// remove root node pare
p+=20;
break; // ok, jump out the while(){}
}
else
{
return -1; //error
}
}
return 0;
}
int DecodeICCSipService(char **pSrc, unsigned short len, Sip_Service *pt)
{
int c=0;
char *p = *pSrc;
char *str;
char bString[128]={0};;
// decode SipService
while (*p!='\0')
{
SKIPSPACE(p,len,c);
SkipComment((const char**)&p,len-c);
SKIPSPACE(p,len,c);
if (!strncmp(p, "<EnableSIPService>", 18))
{
p+=18; //remove header
SKIPSPACE(p,len,c);
if (*p=='1')
{
pt->blEnable = true;
}
else if (*p=='0')
{
pt->blEnable = false;
}
else
{
return -1;
}
p++;
SKIPSPACE(p,len,c);
if (strncmp(p, "</EnableSIPService>", 19))
return -1; //error
p+=19;
}
else if (!strncmp(p, "<SIPUsername>", 13))
{
p+=13; //remove header
SKIPSPACE(p,len,c);
str = strstr(p,"</SIPUsername>");
if((str==NULL)||((str-p)>=USER_NAME_LTH))
return -1;
memcpy(pt->abUserName, p, str-p);
p=str;
p+=14;
}
else if (!strncmp(p, "<SIPPassword>", 13))
{
p+=13; //remove header
SKIPSPACE(p,len,c);
str = strstr(p,"</SIPPassword>");
if((str==NULL)||((str-p)>=PASSWORD_LTH))
return -1;
memcpy(pt->abPassword, p, str-p);
p=str;
p+=14;
}
else if (!strncmp(p, "<SIPServerIP>", 13))
{
p+=13; //remove header
SKIPSPACE(p,len,c);
str = strstr(p,"</SIPServerIP>");
if((str==NULL)||((str-p)>=LOGN_NAME_LTH)) //IpV6 or domain name
return -1;
memcpy(pt->abHostAddr, p, str-p);
pt->abHostAddr[str-p]='\0';
p=str;
p+=14;
}
else if (!strncmp(p, "<SIPServerPort>", 15))
{
p+=15; //remove header
SKIPSPACE(p,len,c);
str = strstr(p,"</SIPServerPort>");
if((str==NULL)||((str-p)>=20)) //255.255.255.255
return -1;
memcpy(bString, p, str-p);
bString[str-p]='\0';
pt->wPort = atoi( bString );
if (pt->wPort==INADDR_NONE)
return -1;
p=str;
SKIPSPACE(p,len,c);
p+=16;
}
else if (!strncmp(p, "</SipService>", 13))
{
p+=13; //remove header
break; // OK, out
}
else
{
return -1; // error out
}
}
*pSrc = p;
return 0;
}
int DecodeICCCamSetting(char **pSrc, unsigned short len, Cam_Setting *pt)
{
int c=0;
char *p = *pSrc;
char bString[128]={0};;
// decode CamSetting
while (*p!='\0')
{
SKIPSPACE(p,len,c);
SkipComment((const char**)&p,len-c);
SKIPSPACE(p,len,c);
if (!strncmp(p, "<Location>", 10))
{
p+=10; //remove header
SKIPSPACE(p,len,c);
if (*p=='0')
{
pt->chLocation = 0;
}
else if (*p=='1')
{
pt->chLocation = 1;
}
else
{
return -1;
}
p++;
SKIPSPACE(p,len,c);
if (strncmp(p, "</Location>", 11))
return -1; //error
p+=11;
}
else if (!strncmp(p, "<Rotation>", 10))
{
p+=10; //remove header
SKIPSPACE(p,len,c);
switch (*p)
{
case '0': pt->chRotation = 0; break;
case '1': pt->chRotation = 1; break;
case '2': pt->chRotation = 2; break;
case '3': pt->chRotation = 3; break;
default: return -1;
}
p++;
SKIPSPACE(p,len,c);
if (strncmp(p, "</Rotation>", 11))
return -1; //error
p+=11;
}
else if (!strncmp(p, "</CamSetting>", 13))
{
p+=13; //remove header
break; // OK, out
}
else
{
return -1; // error out
}
}
*pSrc = p;
return 0;
}
int DecodeICCSystemSet(char **pSrc, unsigned short len, System_Set *pt)
{
int c=0;
char *p = *pSrc;
char bString[128]={0};;
// decode SipService
while (*p!='\0')
{
SKIPSPACE(p,len,c);
SkipComment((const char**)&p,len-c);
SKIPSPACE(p,len,c);
if (!strncmp(p, "<SystemAutoRebootEnable>", 24))
{
p+=24; //remove header
SKIPSPACE(p,len,c);
if (*p=='1')
{
pt->blAutoReboo