当前位置:网站首页>Gb28181 protocol -- alarm

Gb28181 protocol -- alarm

2022-06-12 22:39:00 jimte_ pro

1、 brief introduction

according to 《GBT 28181-2016 Public security video monitoring network system information transmission 、 In exchange for 、 Control technical requirements 》9.4 Chaste 《 Basic requirements for alarm event notification and distribution 》 describe :

When an alarm event occurs , The source device shall send alarm information to SIP The server ;SIP After the server receives the alarm event , Distribute the alarm information to the target device . Alarm event notification and distribution IETF RFC 3428 The method defined in Message Transmit alarm information . Source devices include SIP equipment 、 gateway 、SIP client 、 Networking system or integrated alarm receiving and handling system, bayonet system, etc , The target equipment includes... With alarm receiving function SIP client 、 Networking system or integrated alarm receiving and handling system, bayonet system, etc .

2、 technological process

The alarm event notification and distribution process is shown in the figure below :
 Insert picture description here
The command flow is described below :

  • (1): After the alarm event occurs , Source device to SIP The server sends an alarm notification command , The alarm notification command adopts Message How to carry ;
  • (2): SIP After receiving the command, the server returns 200 OK;
  • (3):SIP After the server receives the alarm event , Send alarm event notification response command to the source device , The alarm notification response command adopts Message How to carry ;
  • (4): The source device returns... After receiving the command 200 OK;
  • (5):SIP After the server receives the alarm event , Identify the target device that needs to be forwarded ,SIP The server sends an alarm event notification command to the target device , The alarm notification command adopts Message How to carry ;
  • (6): After receiving the command, the target device returns 200 OK;
  • (7): After the target device receives the alarm event , towards SIP The server sends alarm event notification response command , The alarm notification response command adopts Message How to carry ;
  • (8):SIP After receiving the command, the server returns 200 OK

3、 Protocol interfaces

  • (1) Request command message body
    The message header Content-type Field is Content-type: Application/ MANSCDP+xml.
    The request command in the alarm event notification and distribution process adopts MANSCDP Protocol format definition , See for a detailed description A.2.5 Alarm notice .
    Source device to SIP The server notifies the alarm 、SIP The server sends alarm notification commands to the target device in the form of Message Method belt . newspaper police things Pieces of through know life Make Should be package enclosed life Make class type (CmdType) 、 life Make order Column Number (SN) 、 set up To prepare Ed code (DeviceID) 、 Alarm level (AlarmPriority) etc. . optional : Alarm time (AlarmTime) 、 Alarm mode (AlarmMethod) 、 longitude (Longitude) 、 latitude (Latitude) 、 Extended alarm type (AlarmType) 、 Alarm type parameter (AlarmTypeParam) . The relevant equipment received Message After the news , Return immediately 200 OK The reply ,200 OK There is no message body in the response .

  • (2) Reply command message body
    The message header Content-type Field is Content-type: Application/ MANSCDP+xml.
    The response command in the alarm event notification and distribution process adopts MANSCDP Protocol format definition , See for a detailed description A.2.6 Alarm notification response .SIP The server sends data to the source device 、 Target device to SIP The server sends alarm notification and response commands in Message The message body of the method carries . The alarm event notification response command shall include the command type (CmdType) 、 Command serial number (SN) 、 Equipment code (DeviceID) 、 Execution results (Result) .
    The relevant equipment received Message After the news , Return immediately 200 OK The reply ,200 OK There is no message body in the response .

4、 Software code

Alarm sending interface :
int SipSendAlarm(GB28181Param_t *pGB28181Param, AlarmHander_t *pHander, int sn);

int SipSendAlarm(GB28181Param_t *pGB28181Param, AlarmHander_t *pHander, int sn)
{
    
	int ret = 0;
	char from[128] = {
    0,};  
	char proxy[128] = {
    0,}; 
	char xmlBody[1024] = {
    0,};
	osip_message_t *rqt_msg = NULL;


	// sip Not registered yet , Then don't send data 
	if (!SipGetRegStatus() || !pGB28181Param || !pHander)
	{
    
		return -1;
	}

	if (!IS_ALARM_CHN_VALID(pHander->alarmChn))
	{
    
		return -1;
	}

	snprintf(from, sizeof(from), "sip:%[email protected]%s:%s", 
			pGB28181Param->userParam.devSipID, 
			pGB28181Param->userParam.devSipIP,
			pGB28181Param->userParam.devSipPort);

	snprintf(proxy, sizeof(proxy), "sip:%[email protected]%s:%s", 
			pGB28181Param->userParam.sipServerID, 
			pGB28181Param->userParam.sipServerIP,
			pGB28181Param->userParam.sipServerPort);

	/*  structure "MESSAGE" request  */
	if (eXosip_message_build_request(&rqt_msg, "MESSAGE", proxy, from, NULL)!=OSIP_SUCCESS)
	{
    
		return -1;
	}

	if (MakeAlarmBody(xmlBody, sizeof(xmlBody), sn, pGB28181Param, pHander))
	{
    
		return -1;
	}
	
	if (osip_message_set_content_type(rqt_msg, "Application/MANSCDP+xml")!=OSIP_SUCCESS)
	{
    
		osip_message_free(rqt_msg);
		return -1;
	}

	if (osip_message_set_body(rqt_msg, xmlBody, strlen(xmlBody))!=OSIP_SUCCESS)
	{
    
		osip_message_free(rqt_msg);
		return -1;
	}

	/*  Send a message  */
	eXosip_lock();
	ret = eXosip_message_send_request(rqt_msg);
	eXosip_unlock();
	
	return (ret==OSIP_SUCCESS)?0:-1;
}

Alarm message construction interface :
static int MakeAlarmBody(char *xmlBody, int xmlBodyLen, int sn, GB28181Param_t *pGB28181Param, AlarmHander_t *pHander);

static int MakeAlarmBody(char *xmlBody, int xmlBodyLen, int sn, GB28181Param_t *pGB28181Param, AlarmHander_t *pHander)
{
    
	if (!xmlBody ||!pGB28181Param || !pHander)
	{
    
		return -1;
	}
	
	snprintf(xmlBody, xmlBodyLen, "<?xml version=\"1.0\"?>\r\n"
		"<Notify>\r\n"
		"<CmdType>Alarm</CmdType>\r\n"					/* Command type */
		"<SN>%d</SN>\r\n"								/* Command serial number */
		"<DeviceID>%s</DeviceID>\r\n"					/* Equipment code */
		"<AlarmPriority>%d</AlarmPriority>\r\n"  		/* Alarm level */
		"<AlarmTime>%s</AlarmTime>\r\n"  				/* Alarm time */
		"<AlarmMethod>%d</AlarmMethod>\r\n"  			/* Alarm mode */
		"<AlarmDescription>%s</AlarmDescription>\r\n" 	/* Alarm description */
		"<Longitude>0.000</Longitude>\r\n"
		"<Latitude>0.000</Latitude>\r\n"
		"<Info>\r\n"
		"<AlarmType>%d</AlarmType>\r\n"  				/* Alarm type */
		"<AlarmTypeParam>\r\n"
		"</AlarmTypeParam>\r\n"
		"</Info>\r\n"
		"</Notify>\r\n",
		sn, 
		pGB28181Param->userParam.iAlarmChn[pHander->alarmChn],
		pGB28181Param->userParam.iAlarmPriority[pHander->alarmChn],
		pHander->alarmTime,
		pHander->alarmMethod,
		pHander->alarmDescri,
		pHander->alarmType
	);


	return 0;
}

Alarm response analysis :
receive sip Data interface :
int SipEventProcess(GB28181Param_t *pGB28181Param)

int SipEventProcess(GB28181Param_t *pGB28181Param)
{
    
	int ret = 0;
	int len = 0;
	char *msg = NULL;
	osip_header_t *dest = NULL;
	eXosip_event_t *sipEvent = NULL;


	sipEvent = eXosip_event_wait( 0, 100);
	if (!sipEvent)
	{
    
		eXosip_lock();
		eXosip_execute();
		eXosip_automatic_action();
		eXosip_unlock();
		return -1;
	}
	eXosip_lock();
	eXosip_execute();
	eXosip_automatic_action();
	eXosip_unlock();

	switch(sipEvent->type)
	{
    
		case EXOSIP_MESSAGE_ANSWERED:
		{
    
			//  Update the retention time 
			pthread_mutex_lock(&g_SipState.mutex);
			g_SipState.keepliveAckTime = GetSysSec();
			pthread_mutex_unlock(&g_SipState.mutex);
			if(MSG_IS_MESSAGE(sipEvent->response))
			{
    
				ret = SipResponsMsgProcess(pGB28181Param, sipEvent);
			}
		}
			break;
			
		default:
			GB_PrintWarn("error Event, Event type: %d\n", sipEvent->type);
			break;
	}

	eXosip_event_free(sipEvent);

	return ret;
}

Analyze the response interface :

static int SipResponsMsgProcess(GB28181Param_t *pGB28181Param, eXosip_event_t *sipEvent)
{
    
	char xmlSN[32] = {
    0,};
	char deviceID[32] = {
    0,};
	char cmdType[32] = {
    0,};
	char rspXmlBody[2048] = {
    0,};
	osip_body_t *rspBody = NULL;
	mxml_node_t *xml = NULL;
	mxml_node_t *node = NULL;
	osip_message_t *rsqMsg = NULL;
	

	if (!pGB28181Param || !sipEvent)
	{
    
		return -1;
	}
	eXosip_lock();
	/* Gets the of the received request XML Message body */
	int ret = osip_message_get_body(sipEvent->response, 0, &rspBody);
	if((NULL == rspBody) || (NULL == rspBody->body))
	{
    
		eXosip_unlock();
		return -1;
	}
	eXosip_unlock();
	xml = mxmlLoadString(NULL,rspBody->body, MXML_TEXT_CALLBACK);
	if (!xml)
	{
    
		return -1;
	}

	//  lookup CmdType
	node = mxmlFindElement(xml, xml, "CmdType", NULL, NULL, MXML_DESCEND);
	if (!node)
	{
    
		return -1;
	}
	strncpy(cmdType, mxmlGetText(node, NULL), sizeof(cmdType));
	if (strlen(cmdType) <=0 )
	{
    
		return -1;
	}
	
	node = mxmlFindElement(xml, xml, "DeviceID", NULL, NULL, MXML_DESCEND);
	if (!node)
	{
    
		return -1;
	}
	strncpy(deviceID, mxmlGetText(node, NULL), sizeof(deviceID));
	if (strlen(deviceID) <=0)
	{
    
		return -1;
	}

	//  For this SN You need to do some anti duplication treatment , Consider this function later 
	node = mxmlFindElement(xml, xml, "SN", NULL, NULL, MXML_DESCEND);
	if (!node)
	{
    
		return -1;
	}
	//  Response time , The equipment needs to be sent SN
	strcpy(xmlSN, mxmlGetText(node, NULL));
	if (strlen(xmlSN) <=0 )
	{
    
		return -1;
	}
	
	eXosip_lock();
	if ((ret = eXosip_message_build_answer( sipEvent->tid, 200, &rsqMsg)) != OSIP_SUCCESS)
	{
    
		eXosip_unlock();
		return -1;
	}
	
	if ((ret = eXosip_message_send_answer( sipEvent->tid, 200, rsqMsg)) != OSIP_SUCCESS)
	{
    
		eXosip_unlock();
		return -1;
	}
	eXosip_unlock();


	 if (!strcmp(cmdType, "Alarm"))
	{
    
		AlarmMsgParase(xml, rspXmlBody, sizeof(rspXmlBody), xmlSN, deviceID, pGB28181Param);
	}
	else
	{
    
		mxmlDelete(xml);
		return -1;
	}

	mxmlDelete(xml);
	

	return 0;
}

Parse answer command :

static int AlarmMsgParase(mxml_node_t *xml, char *msg, int msgLen, char *sn, char *devID, GB28181Param_t *pGB28181Param)
{
    
	char result[32] = {
    0,};
	
	if ( !pGB28181Param || !msg || msgLen<= 0 || !xml )
	{
    
		return -1;
	}

	memset(msg, 0, msgLen);
	uint8_t iChn = GetChannelID(devID, pGB28181Param, GET_ALARM_TYPE);
	if (!IS_ALARM_CHN_VALID(iChn))
	{
    
		return -1;
	}

	//  lookup CmdType
	mxml_node_t *node = mxmlFindElement(xml, xml, "Result", NULL, NULL, MXML_DESCEND);
	if (!node)
	{
    
		return -1;
	}
	strncpy(result, mxmlGetText(node, NULL), sizeof(result));

	if (!strcmp(result, "OK"))
	{
    
	}

	return 0;
}

Recommended reading :
GB28181 agreement – Device registration and logout

Reference material :
《GBT 28181-2016 Public security video monitoring network system information transmission 、 In exchange for 、 Control technical requirements 》

原网站

版权声明
本文为[jimte_ pro]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281134481256.html

随机推荐