视频监控安防平台-GB28181-20位地址编码解析
国标28181地址编码遵循国标文档,下面是简单对28181地址编码进行解析,主要解析成不同的类型来使用。
下面粘贴一下地址编码的解析代码:
#ifndef __PUBLICID_H__
#define __PUBLICID_H__
#include
#include
#include
#include
#include "../Common/MyThread.h"
#define PUBID_PROVINCE_POS 0
#define PUBID_PROVINCE_LEN 2
#define PUBID_CIRY_POS PUBID_PROVINCE_LEN
#define PUBID_CITY_LEN 2
#define PUBID_AREA_POS (PUBID_CIRY_POS + PUBID_CITY_LEN)
#define PUBID_AREA_LEN 2
#define PUBID_UNIT_POS (PUBID_AREA_POS + PUBID_AREA_LEN)
#define PUBID_UNIT_LEN 2
#define PUBID_VOCATION_POS (PUBID_UNIT_POS + PUBID_UNIT_LEN)
#define PUBID_VOCATION_LEN 2
#define PUBID_TYPE_POS (PUBID_VOCATION_POS + PUBID_VOCATION_LEN)
#define PUBID_TYPE_LEN 3
#define PUBID_SN_POS (PUBID_TYPE_POS + PUBID_TYPE_LEN)
#define PUBID_SN_LEN 7
#define PUBID_TOTAL_LEN (PUBID_SN_POS + PUBID_SN_LEN)
typedef enum{
PUBIDTYPE_ERROR = 0,
PUBIDTYPE_DEVICE = 100,
PUBIDTYPE_DEVICE_DVR = 111, //DVR编码
PUBIDTYPE_DEVICE_VIDEO_SVR = 112, //视频服务器编码
PUBIDTYPE_DEVICE_ENCODER = 113, //编码器编码
PUBIDTYPE_DEVICE_DECODER = 114, //解码器编码
PUBIDTYPE_DEVICE_VIDEO_SWITCH_MATRIX = 115, //视频切换矩阵编码
PUBIDTYPE_DEVICE_AUDIO_SWITCH_MATRIX = 116,//音频切换矩阵编码
PUBIDTYPE_DEVICE_ALARM_CTRL = 117, //报警控制器编码
PUBIDTYPE_DEVICE_NVR = 118, //网络视频录像机(NVR)编码
PUBIDTYPE_DEVICE_HVR = 119, //混合硬盘录像机(HVR)编码
PUBIDTYPE_DEVICE_VIDEOSTITCH = 121, //画面分割器设备
PUBIDTYPE_DEVICE_CAM = 130, //混合硬盘录像机(HVR)编码
PUBIDTYPE_DEVICE_2 = 131, //摄像机编码
PUBIDTYPE_DEVICE_IPC = 132, //网络摄像机(IPC)编码
PUBIDTYPE_DEVICE_MONITOR = 133, //显示器编码
PUBIDTYPE_DEVICE_ALARMINPUT= 134, //报警输入设备编码(如红外、烟感、门禁等报警设备)
PUBIDTYPE_DEVICE_ALARMOUTPUT= 135, //报警输出设备编码(如警灯、警铃等设备)
PUBIDTYPE_DEVICE_AUDIOINPUT= 136, //语音输入设备编码
PUBIDTYPE_DEVICE_AUDIOOUTPUT= 137, //语音输出设备
PUBIDTYPE_DEVICE_MOBILE= 138, //移动传输设备编码
PUBIDTYPE_SIPSVR = 200,
PUBIDTYPE_WEBSVR = 201,
PUBIDTYPE_MEDIASVR = 202,
PUBIDTYPE_PROXYSVR = 203,
PUBIDTYPE_SAFESVR = 204,
PUBIDTYPE_ALARMSVR = 205,
PUBIDTYPE_DBSVR = 206,
PUBIDTYPE_GISSVR,
PUBIDTYPE_ADMINSVR,
PUBIDTYPE_GATEWAY = 209,
PUBIDTYPE_MSS = 210,
PUBIDTYPE_VOD = 212,
//UpdateServer
PUBIDTYPE_UPDATECENTER = 213, //中心升级服务器编码
PUBIDTYPE_UPDATESVR = 214,
PUBIDTYPE_BUSINESS_GROUP = 215, //业务分组 角色
PUBIDTYPE_VIRTUAL_GROUP = 216, //虚拟分组 资源组
PUBIDTYPE_EXTENDPLATFORM = 220,
PUBIDTYPE_CENTERUSER = 300,
PUBIDTYPE_CLIENT = 400,
PUBIDTYPE_EXTEND = 500,
PUBIDTYPE_CAM_GROUP = 501,
PUBIDTYPE_MON_GROUP = 503,
PUBIDTYPE_VIDEOSTITCH_GROUP = 521,
PUBIDTYPE_OTHER_GROUP = 522, //其他资源组,包含报警输入输出,音频输入输出等
}PUBIDTYPE_E;
class CPublicID
{
public:
//sip:[email protected]:2039
CPublicID(const char *idstr = NULL);
~CPublicID();
char* GetPubID(const char *idstr, char XBuf[PUBID_TOTAL_LEN+1]);
// char* GetPubID(const char *idstr);
void PrasePubID(const char *idstr);
PUBIDTYPE_E GetSvrType();
int GetProvice();
int GetCity();
int GetArea();
int GetUnit();
int GetVocation();
int GetType();
int GetSN();
bool IsSvr();
bool IsDev();
bool IsClient();
bool IsExtReg();
bool IsOurPlatFrorm();
bool IsOurPlatFrorm(const char aor[]);
char* BuildPubID(int type,int sn);
char *GetAdministrativeAreaID();
static int GetSN_s(const char *idstr);
static int GetType_s(const char *idstr);
static char* BuildPubID_s(int type,int sn);
static bool IsOurPlatForm_s(const char *idstr);
static bool ClonePubID_s(char* destid,const char*idstr);
static char* GetPubID_s(const char *idstr);
static bool IsSamePubID(const char *idstr1,const char *idstr2);
static bool IsSvr_s(const char *idstr);
static bool IsDev_S(const char *idstr);
static bool IsClient_S(const char *idstr);
static char* ToDevAorForRegister(char Aor[], char XBuf[128+1])
{
strcpy(XBuf, Aor);
memset(XBuf + PUBID_TYPE_POS, '0', PUBID_TOTAL_LEN - PUBID_TYPE_POS);
return XBuf;
}
static bool ISAtOuerPlatForm_s(const char* aor,const char *idstr);
static bool IsExtReg_s(const char *idstr);
static char *GetAdministrativeAreaID_s(const char *idstr);
char* GetAreaUnitID(void);
private:
PUBIDTYPE_E svrtype;
int provice;
int city;
int area;
int unit;
int vocation;
int type;
int sn;
static CCritSec CritSec_Publicid;
};
#define PUBIDMAXTYPEVALUE 999
#define PUBIDMAXSNVALUE 9999999
#define PUBIDGetSN(idstr) CPublicID::GetSN_s(idstr)
#define PUBIDGetType(idstr) CPublicID::GetType_s(idstr)
#define PUBIDBuild(type,sn) CPublicID::BuildPubID_s(type,sn)
#define PUBIDIsOurPlatForm(idstr) CPublicID::IsOurPlatForm_s(idstr)
#define PUBIDClonePubid(destid,srcstr) CPublicID::ClonePubID_s(destid,srcstr)
#define PUBIDGetPubID(idstr) CPublicID::GetPubID_s(idstr)
#define PUBIDIsSamePubID(idstr1,idstr2) CPublicID::IsSamePubID(idstr1,idstr2)
#define PUBIDISTHESAMEPlatForm(aor,idstr) CPublicID::ISAtOuerPlatForm_s(aor,idstr)
#define PUBIDISSVR(idstr) CPublicID::IsSvr_s(idstr)
#define PUBIDISDEV(idstr) CPublicID::IsDev_S(idstr)
#define PUBIDISCLIENT(idstr) CPublicID::IsClient_S(idstr)
#define PUBIDISEXTREG(idstr) CPublicID::IsExtReg_s(idstr)
void PUBIDSETPLATFORMID(char* myid);
#endif
#include "publicid.h"
#include "Log.h"
static char * myplatformid = NULL;
void PUBIDSETPLATFORMID(char* myid)
{
myplatformid = myid;
}
#define PUBLICIDOURPLATFORMID myplatformid
CCritSec CPublicID::CritSec_Publicid;
CPublicID::CPublicID(const char *idstr):svrtype(PUBIDTYPE_ERROR)
{
if(idstr != NULL)
{
PrasePubID(idstr);
}
}
CPublicID::~CPublicID()
{
}
char* CPublicID::GetPubID(const char *idstr, char XBuf[PUBID_TOTAL_LEN+1])
{
if(idstr == NULL)
{
DBGPrint(ERROR_LEVEL, ERROR_LEVEL,"%s:idstr is null!!", __FUNCTION__);
return NULL;
}
char* string = (char*)idstr;
char *ptmp;
if(memcmp(string,"sip:",4) == 0)
{
string += 4;
}
ptmp = strchr(string,'@');
if(ptmp != NULL)
{
if(ptmp - string != PUBID_TOTAL_LEN)
{
DBGPrint(ERROR_LEVEL, ERROR_LEVEL,"%s:<%s>ptmp - string != %d!", __FUNCTION__, idstr, PUBID_TOTAL_LEN);
return NULL;
}
memcpy(XBuf,string,ptmp-string);
XBuf[ptmp-string] = '\0';
}
else
{
if(strlen(string) != PUBID_TOTAL_LEN)
{
//DBGPrint(ERROR_LEVEL, ERROR_LEVEL,"%s:<%s>len<%d> is not <%d>!", __FUNCTION__, idstr, strlen(string), PUBID_TOTAL_LEN);
return NULL;
}
memcpy(XBuf,string,PUBID_TOTAL_LEN);
XBuf[PUBID_TOTAL_LEN] = '\0';
}
return XBuf;
}
/*
char* CPublicID::GetPubID(const char *idstr)
{
if(idstr == NULL)
{
DBGPrint(ERROR_LEVEL, ERROR_LEVEL,"%s:idstr is null!!", __FUNCTION__);
return NULL;
}
static char buffer[PUBID_TOTAL_LEN + 0];
char* string = (char*)idstr;
char *ptmp;
if(memcmp(string,"sip:",4) == 0)
{
string += 4;
}
ptmp = strchr(string,'@');
if(ptmp != NULL)
{
if(ptmp - string != PUBID_TOTAL_LEN)
{
DBGPrint(ERROR_LEVEL, ERROR_LEVEL,"%s:<%s>ptmp - string != %d!", __FUNCTION__, idstr, PUBID_TOTAL_LEN);
return NULL;
}
memcpy(buffer,string,ptmp-string);
buffer[ptmp-string] = '\0';
}
else
{
if(strlen(string) != PUBID_TOTAL_LEN)
{
DBGPrint(ERROR_LEVEL, ERROR_LEVEL,"%s:<%s>len<%d> is not <%d>!", __FUNCTION__, idstr, strlen(string), PUBID_TOTAL_LEN);
return NULL;
}
memcpy(buffer,string,PUBID_TOTAL_LEN);
buffer[PUBID_TOTAL_LEN] = '\0';
}
return buffer;
}
*/
void CPublicID::PrasePubID(const char *idstr)
{
if(idstr == NULL)
{
DBGPrint(ERROR_LEVEL, ERROR_LEVEL,"%s:%idstr is null!!", __FUNCTION__);
return;
}
svrtype = PUBIDTYPE_ERROR;
char buffer[PUBID_TOTAL_LEN + 1] = {0};
GetPubID(idstr, buffer);
if(buffer == NULL)
{
DBGPrint(ERROR_LEVEL, ERROR_LEVEL,"%s:GetPubID<%s> is failed!buffer is null!", __FUNCTION__, idstr);
return;
}
if(sscanf(buffer,"%02d%02d%02d%02d%02d%03d%07d",&provice,&city,&area,&unit,&vocation,&type,&sn) != 7)
{
//DBGPrint(ERROR_LEVEL, ERROR_LEVEL,"%s: <%s>sscanf failed!!!!", __FUNCTION__, idstr);
return;
}
if(type>= 111 && type <= 199)
{
svrtype = PUBIDTYPE_DEVICE;
}
else if(type >= 200 && type <= 212)
{
svrtype = (PUBIDTYPE_E)type;
}
else if(type >= 213 && type <= 299)
{
svrtype = PUBIDTYPE_EXTENDPLATFORM;
}
else if(type >= 300 && type <= 399)
{
svrtype = PUBIDTYPE_CENTERUSER;
}
else if(type >= 400 && type <= 499)
{
svrtype = PUBIDTYPE_CLIENT;
}
else if(type >= 500 && type <= 999)
{
svrtype = PUBIDTYPE_EXTEND;
}
else
{
DBGPrint(ERROR_LEVEL, ERROR_LEVEL,"%s: get svrtype ID<%s> GetPubID <%s>Type<%d> Failed!!!!", __FUNCTION__, idstr, buffer, type);
return;
}
// DBGPrint(BREAK_LEVEL, BREAK_LEVEL,"%s: Prase Pubid<%s> GetPubID<%s> provice<%02d> city<%02d> area<%02d> unit<%02d> vocation<%02d> type<%03d> sn<%07d> svrtype<%03d>!!!!", __FUNCTION__, idstr, buffer, provice, city, area, unit, vocation, type, sn, (int)svrtype);
}
PUBIDTYPE_E CPublicID::GetSvrType()
{
return svrtype;
}
int CPublicID::GetProvice()
{
return (svrtype == PUBIDTYPE_ERROR)?-1:provice;
}
int CPublicID::GetCity()
{
return (svrtype == PUBIDTYPE_ERROR)?-1:city;
}
int CPublicID::GetArea()
{
return (svrtype == PUBIDTYPE_ERROR)?-1:area;
}
int CPublicID::GetUnit()
{
return (svrtype == PUBIDTYPE_ERROR)?-1:unit;
}
int CPublicID::GetVocation()
{
return (svrtype == PUBIDTYPE_ERROR)?-1:vocation;
}
int CPublicID::GetType()
{
return (svrtype == PUBIDTYPE_ERROR)?-1:type;
}
int CPublicID::GetSN()
{
return (svrtype == PUBIDTYPE_ERROR)?-1:sn;
}
bool CPublicID::IsSvr()
{
return (svrtype >= PUBIDTYPE_SIPSVR && svrtype < PUBIDTYPE_CENTERUSER);
}
bool CPublicID::IsDev()
{
return svrtype == PUBIDTYPE_DEVICE;
}
bool CPublicID::IsClient()
{
return svrtype == PUBIDTYPE_CLIENT;
}
bool CPublicID::IsExtReg()
{
return ( (svrtype == PUBIDTYPE_DEVICE) || (svrtype == PUBIDTYPE_SIPSVR) || (svrtype == PUBIDTYPE_GATEWAY));
}
bool CPublicID::IsOurPlatFrorm()
{
return IsOurPlatFrorm(PUBLICIDOURPLATFORMID);
}
bool CPublicID::IsOurPlatFrorm(const char aor[])
{
CPublicID pubid(aor);
if(svrtype == PUBIDTYPE_ERROR || pubid.GetSvrType() == PUBIDTYPE_ERROR)
{
return false;
}
return (GetProvice() == pubid.GetProvice() && GetCity() == pubid.GetCity() && GetArea() == pubid.GetArea()
&& GetUnit() == pubid.GetUnit() && GetVocation() == pubid.GetVocation());
}
char* CPublicID::BuildPubID(int _type,int _sn)
{
static char pubid[32];
if(svrtype == PUBIDTYPE_ERROR)
{
return NULL;
}
_type = (_type > PUBIDMAXTYPEVALUE)?(_type%PUBIDMAXTYPEVALUE):_type;
_sn = (_sn > PUBIDMAXSNVALUE)?(_sn%PUBIDMAXSNVALUE):_sn;
sprintf(pubid,"%02d%02d%02d%02d%02d%03d%07d",provice,city,area,unit,vocation,_type,_sn);
return pubid;
}
char* CPublicID::GetAdministrativeAreaID()
{
static char AreaID[32] = {0};
AreaID[0] = '\0';
if(provice > 0)
sprintf(AreaID + strlen(AreaID), "%02d", provice);
if(city > 0)
sprintf(AreaID + strlen(AreaID), "%02d", city);
if(area > 0)
sprintf(AreaID + strlen(AreaID), "%02d", area);
if(unit > 0)
sprintf(AreaID + strlen(AreaID), "%02d", unit);
if(vocation > 0)
sprintf(AreaID + strlen(AreaID), "%02d", vocation);
AreaID[strlen(AreaID)] = '\0';
return AreaID;
}
char* CPublicID::GetAreaUnitID(void)
{
static char AreaUnitID[5] = {0};
AreaUnitID[0] = '\0';
if(area > 0)
sprintf(AreaUnitID + strlen(AreaUnitID), "%02d", area);
if(unit > 0)
sprintf(AreaUnitID + strlen(AreaUnitID), "%02d", unit);
AreaUnitID[strlen(AreaUnitID)] = '\0';
return AreaUnitID;
}
int CPublicID::GetSN_s(const char *idstr)
{
CAutoLock AutoLock(&CritSec_Publicid);
CPublicID pubid(idstr);
return pubid.GetSN();
}
int CPublicID::GetType_s(const char *idstr)
{
CAutoLock AutoLock(&CritSec_Publicid);
CPublicID pubid(idstr);
return pubid.GetType();
}
char* CPublicID::BuildPubID_s(int _type,int _sn)
{
CAutoLock AutoLock(&CritSec_Publicid);
CPublicID pubid(PUBLICIDOURPLATFORMID);
return pubid.BuildPubID(_type,_sn);
}
bool CPublicID::IsOurPlatForm_s(const char *idstr)
{
CAutoLock AutoLock(&CritSec_Publicid);
CPublicID pubid(idstr);
return pubid.IsOurPlatFrorm();
}
bool CPublicID::ClonePubID_s(char* destid,const char*idstr)
{
char*srcstr = GetPubID_s(idstr);
if(destid == NULL || srcstr == NULL)
{
return false;
}
memcpy(destid,srcstr,PUBID_TOTAL_LEN);
destid[PUBID_TOTAL_LEN] = 0;
return true;
}
char* CPublicID::GetPubID_s(const char *idstr)
{
CAutoLock AutoLock(&CritSec_Publicid);
CPublicID pubid;
char buffer[PUBID_TOTAL_LEN + 1] = {0};
return pubid.GetPubID(idstr, buffer);
}
bool CPublicID::IsSamePubID(const char *idstr1,const char *idstr2)
{
char pubid1[PUBID_TOTAL_LEN + 1];
char pubid2[PUBID_TOTAL_LEN + 1];
char* pid1;
char* pid2;
if(idstr1 == NULL || idstr2 == NULL)
{
return false;
}
pid1 = PUBIDGetPubID(idstr1);
if(pid1 == NULL)
{
return false;
}
strcpy(pubid1,pid1);
pid2 = PUBIDGetPubID(idstr2);
if(pid2 == NULL)
{
return false;
}
strcpy(pubid2,pid2);
return strcmp(pubid1,pubid2) == 0;
}
bool CPublicID::ISAtOuerPlatForm_s(const char* aor,const char *idstr)
{
CAutoLock AutoLock(&CritSec_Publicid);
if(aor == NULL || idstr == NULL)
{
return false;
}
CPublicID pubid1(aor);
CPublicID pubid2(idstr);
if(pubid1.GetProvice() == pubid2.GetProvice() && pubid1.GetCity() == pubid2.GetCity() &&
pubid1.GetArea() == pubid2.GetArea() && pubid1.GetUnit() == pubid2.GetUnit() && pubid1.GetVocation() == pubid2.GetVocation())
{
return true;
}
return false;
}
bool CPublicID::IsSvr_s(const char *idstr)
{
CAutoLock AutoLock(&CritSec_Publicid);
CPublicID pubid(idstr);
return pubid.IsSvr();
}
bool CPublicID::IsDev_S(const char *idstr)
{
CAutoLock AutoLock(&CritSec_Publicid);
CPublicID pubid(idstr);
return pubid.IsDev();
}
bool CPublicID::IsClient_S(const char *idstr)
{
CAutoLock AutoLock(&CritSec_Publicid);
CPublicID pubid(idstr);
return pubid.IsClient();
}
bool CPublicID::IsExtReg_s(const char *idstr)
{
CAutoLock AutoLock(&CritSec_Publicid);
CPublicID pubid(idstr);
if(pubid.IsExtReg())
{
return true;
}
return false;
}
char *CPublicID::GetAdministrativeAreaID_s(const char *idstr)
{
CAutoLock AutoLock(&CritSec_Publicid);
CPublicID pubid(idstr);
return pubid.GetAdministrativeAreaID();
}