GB28181协议--校时
1、簡(jiǎn)介
根據(jù)《GB/T 28181 —2016》7.10、9.10的要求,GB28181設(shè)備網(wǎng)絡(luò)校時(shí)功能描述如下:
聯(lián)網(wǎng)系統(tǒng)內(nèi)的IP 網(wǎng)絡(luò)服務(wù)器設(shè)備宜支持 NTP(見(jiàn)IETF RFC2030) 協(xié)議的網(wǎng)絡(luò)統(tǒng)一校時(shí)服務(wù)。 網(wǎng)絡(luò)校時(shí)設(shè)備分為時(shí)鐘源和客戶端, 支持客戶/服務(wù)器的工作模式; 時(shí)鐘源應(yīng)支持 TCP/IP、UDP 及 NTP協(xié)議, 能將輸入的或自身產(chǎn)生的時(shí)間信號(hào)以標(biāo)準(zhǔn)的 NTP 信息包格式輸出。聯(lián)網(wǎng)系統(tǒng)內(nèi)的IP 網(wǎng)絡(luò)接入設(shè)備應(yīng)支持SIP 信令的統(tǒng)一校時(shí), 接入設(shè)備應(yīng)在注冊(cè)時(shí)接受來(lái)自SIP 服務(wù)器通過(guò)消息頭 Date 域攜帶的授時(shí)。
2、基本流程
聯(lián)網(wǎng)內(nèi)設(shè)備支持基于SIP 方式或 NTP 方式的網(wǎng)絡(luò)校時(shí)功能, 標(biāo)準(zhǔn)時(shí)間為北京時(shí)間。流程如下所示:
在注冊(cè)成功情況下, 注冊(cè)流程的最后一個(gè) SIP 應(yīng)答消息200 OK 中的 Date 頭域中攜帶時(shí)間信息。采用的格式為 XML 標(biāo)準(zhǔn)格式:Date: yyyy-MM-dd’T’HH: mm:ss.SSS。
若SIP 代理通過(guò)注冊(cè)方式校時(shí), 其注冊(cè)過(guò)期時(shí)間宜設(shè)置為小于 SIP 代理與 SIP 服務(wù)器出現(xiàn)1 s 誤差所經(jīng)過(guò)的運(yùn)行時(shí)間。 例如:SIP 代理與SIP 服務(wù)器校時(shí)后,SIP 代理運(yùn)行10 h 后設(shè)備時(shí)間與SIP 服務(wù)器時(shí)間相差大于1 s, 則宜將注冊(cè)過(guò)期時(shí)間設(shè)置為10 h(36 000s) , 以保證SIP 代理與SIP 服務(wù)器之間時(shí)間誤差小于1 s。
3、基于SIP的校時(shí)
sip信令處理:
int SipReg(GB28181Param_t *pGB28181Param, int isReg) {int ret = 0;int len = 0;char *msg;int expires = 0 ;int regState = 0;int unAuthorized = 0;eXosip_event_t *je = NULL;osip_header_t *dest = NULL;osip_message_t *reg = NULL;long interval = GetSysSec();if (!pGB28181Param){return SIP_FAILED;}if (isReg){expires = strtoul(pGB28181Param->userParam.sipExpires, 0, 0);}while(GetSysSec() -interval <= MAX_SIP_REG_TIMEOUT_SEC){// 先進(jìn)行不認(rèn)證注冊(cè)if (0 == unAuthorized){ret = SipRegisterUnauthorized(pGB28181Param, expires);if (ret < 0){if(je){eXosip_event_free(je);}return SIP_FAILED;}else {unAuthorized = 1;}}je = eXosip_event_wait(0, 100); /* 等待新消息的到來(lái) */if(NULL == je){/* 以下語(yǔ)句會(huì)導(dǎo)致eXosip_register_send_register失敗 */eXosip_automatic_action();usleep(100*1000);continue;}/* 返回注冊(cè)失敗 */if(EXOSIP_REGISTRATION_FAILURE == je->type){/* 未認(rèn)證注冊(cè)失敗,那么使用認(rèn)證的方式進(jìn)行注冊(cè) */if((je->response!=NULL) && (401==je->response->status_code)){ret = SipRegisterAuthorized(pGB28181Param, je->rid, expires);eXosip_event_free(je);if(ret != OSIP_SUCCESS){return SIP_FAILED;}}else {eXosip_event_free(je);unAuthorized = 0; /* 注冊(cè)失敗, 重新走一遍注冊(cè)流程 */if (isReg){return SIP_REREG_AFTER_60S;}return SIP_FAILED;}}else if (EXOSIP_REGISTRATION_SUCCESS == je->type){regState = 1;/* 收到服務(wù)器返回的注冊(cè)成功 */g_SipState.registerID = je->rid; if (MSG_IS_REGISTER(je->request) && je->response){if (OSIP_SUCCESS == osip_message_to_str(je->response, &msg, &len)){if (osip_message_get_date(je->response, 0, &dest) > 0){SipSetSystemTime(dest->hvalue);}}}eXosip_execute();eXosip_automatic_action();eXosip_event_free(je);break;}else{eXosip_event_free(je);}}if( (GetSysSec() -interval > MAX_SIP_REG_TIMEOUT_SEC) && (regState == 0)){return SIP_FAILED;}pthread_mutex_lock(&g_SipState.mutex);// 更新sip的注冊(cè)狀態(tài)if (isReg){g_SipState.sipRegStatus = 1;g_SipState.keepliveAckTime = GetSysSec();}else {g_SipState.sipRegStatus = 0;}pthread_mutex_unlock(&g_SipState.mutex);return SIP_SUCCESS; }設(shè)置系統(tǒng)時(shí)間接口:
static int SipSetSystemTime(char *timeStr) {char *beginStr = timeStr;char *endStr = NULL;char tempStr[10] = {0, };struct tm st_time = {0, };struct timeval tv = {0, };uint32_t stime = 0;if (!timeStr){return SIP_FAILED;}if ((endStr = strstr(beginStr, "-"))){memset(tempStr, 0, sizeof(tempStr));strncpy(tempStr, beginStr, endStr-beginStr);beginStr = endStr + 1;stime = strtoul(tempStr, NULL, sizeof(tempStr));st_time.tm_year = stime - 1900;}if ((endStr = strstr(beginStr, "-"))){memset(tempStr, 0, sizeof(tempStr));strncpy(tempStr, beginStr, endStr-beginStr);beginStr = endStr + 1;stime = strtoul(tempStr, NULL, sizeof(tempStr));st_time.tm_mon = stime - 1;}if ((endStr = strstr(beginStr, "T"))){memset(tempStr, 0, sizeof(tempStr));strncpy(tempStr, beginStr, endStr-beginStr);beginStr = endStr + 1;stime = strtoul(tempStr, NULL, sizeof(tempStr));st_time.tm_mday = stime;}if ((endStr = strstr(beginStr, ":"))){memset(tempStr, 0, sizeof(tempStr));strncpy(tempStr, beginStr, endStr-beginStr);beginStr = endStr + 1;stime = strtoul(tempStr, NULL, sizeof(tempStr));st_time.tm_hour = stime;}if ((endStr = strstr(beginStr, ":"))){memset(tempStr, 0, sizeof(tempStr));strncpy(tempStr, beginStr, endStr-beginStr);beginStr = endStr + 1;stime = strtoul(tempStr, NULL, sizeof(tempStr));st_time.tm_min = stime;}if ((endStr = strstr(beginStr, "."))){memset(tempStr, 0, sizeof(tempStr));strncpy(tempStr, beginStr, endStr-beginStr);beginStr = endStr + 1;stime = strtoul(tempStr, NULL, sizeof(tempStr));st_time.tm_sec = stime;}// 設(shè)置系統(tǒng)時(shí)間if ((tv.tv_sec = mktime(&st_time)) < 0){GB_PrintError("mktime failed\n");return SIP_FAILED;}settimeofday(&tv, NULL);// 設(shè)置RTC時(shí)間struct tm *stTime = gmtime(&tv);if (SetRtcTime(stTime) < 0){return SIP_FAILED; }return SIP_SUCCESS; }參考資料:
《GBT 28181-2016 公共安全視頻監(jiān)控聯(lián)網(wǎng)系統(tǒng)信息傳輸、交換、控制技術(shù)要求》
推薦閱讀:GB28181協(xié)議–設(shè)備注冊(cè)和注銷
總結(jié)
以上是生活随笔為你收集整理的GB28181协议--校时的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【EXLIBRIS】纸版书目整理 --
- 下一篇: 使用jquery+css实现瀑布流布局