Chinaunix首页 | 论坛 | 博客
  • 博客访问: 311266
  • 博文数量: 101
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 774
  • 用 户 组: 普通用户
  • 注册时间: 2018-10-15 14:13
个人简介

搭建一个和linux开发者知识共享和学习的平台

文章分类

全部博文(101)

文章存档

2024年(15)

2023年(24)

2022年(27)

2019年(8)

2018年(27)

分类: 嵌入式

2018-12-26 16:54:12

static void *OneNetServerThread(void *pArg)
{
int i,OldStatus,RetryCount,ReadyFdNum,MaxFd,UserIndex;
DWORD TimeOut,DelayCount=0,MaxDelayCount=10;
struct in_addr LocalAddr;
fd_set Rset,Wset;
char RecvBuf[2*1024];
int FdNum;
BOOL IsReReg=FALSE;
OnenetContext *pOnenetCtx=NULL;
int32_t CisError;
DWORD TimeOutMS,Len;
DWORD LastSysRunTime,CurrSysRunTime;
DWORD LastFlushTime=0,CurrFlushTime;
DWORD LastRegingTime=0,CurrRegingTime;
fd_set ReadFdSet;



TimeOut=50;
LocalAddr.s_addr=htonl(sFsuOneNetServer.IPAddr);
GET_CURRENT_SYS_TIME(LastSysRunTime);
for(RetryCount=0;;)
{
if(sFsuOneNetServer.IsReqServerQuit)
{
sFsuOneNetServer.IsReqServerQuit=FALSE;
break;
}
pthread_testcancel();
if(!sFsuOneNetServer.m_IsCreated)
{
pOnenetCtx = OnenetCreateInstance();
       if (pOnenetCtx == NULL)
{
           CisError = ONENET_ERRID_NOT_SUPPORT;
       }
else 
{
cissys_setIMEI(sFsuOneNetServer.m_IMEI);
cissys_setIMSI(sFsuOneNetServer.m_IMSI);
                pOnenetCtx->config_len = (uint16_t)OnenetHex2Bin(pOnenetCtx->config_bin,ONENET_CONFIG_BUFFER,strlen(ONENET_CONFIG_BUFFER));
                 
cis_ret_t cis_ret = cis_init(&pOnenetCtx->cis_context, NULL, 0);
                 
                if (cis_ret != CIS_RET_OK) 
{
                    CisError = ONENET_ERRID_SDK_ERROR;
                    OnenetDeleteInstance(pOnenetCtx);
                }
else 
{
sFsuOneNetServer.m_IsCreated=TRUE;
                }
       }
}
if(!sFsuOneNetServer.m_IsOpened && sFsuOneNetServer.m_ResourceTab.ResourceNum         {
        for(i=0;i         {
int count=1,j,k;
char bitmap[128]={0};
char *bita="1";

for(j=i+1;j {
if(sFsuOneNetServer.m_ResourceTab.ResourceList[j].ObjectID==sFsuOneNetServer.m_ResourceTab.ResourceList[i].ObjectID)
count++;
}
for(k=0;k strcat(bitmap,bita);

ON_FsuOneNetAddObject(sFsuOneNetServer.m_DevRef,sFsuOneNetServer.m_ResourceTab.ResourceList[i].ObjectID,count,bitmap,4,0);
i+=count;
        }
       CisError=OnenetServerOpen(sFsuOneNetServer.m_DevRef,sFsuOneNetServer.m_DevLifetime);
if (CisError==ONENET_ERRID_OK) 
{
           sFsuOneNetServer.m_IsOpened=TRUE;
GET_CURRENT_SYS_TIME(LastFlushTime);
GET_CURRENT_SYS_TIME(LastRegingTime);
       }
else 
{

           sFsuOneNetServer.m_IsOpened=FALSE;
sleep(5);
continue;
       }
}

for(RetryCount=0,DelayCount=0;;)
{
if(sFsuOneNetServer.IsReqServerQuit)
break;
pthread_testcancel();


GET_CURRENT_SYS_TIME(CurrFlushTime);
GET_CURRENT_SYS_TIME(CurrRegingTime);
if(CurrRegingTime-LastRegingTime >= 60 && !sFsuOneNetServer.m_IsRegisted)
{
GET_CURRENT_SYS_TIME(LastRegingTime);
IsReReg=TRUE;
}
if(CurrFlushTime-LastFlushTime >= sFsuOneNetServer.m_DevLifetime*9)
{
GET_CURRENT_SYS_TIME(LastFlushTime);
sFsuOneNetServer.m_IsNeedUpdate=TRUE;
}
for (i = 0; i < ONENET_INSTANCE_NUM; i++) 
{
           pOnenetCtx = &gOnenetContext[i];
           if (pOnenetCtx->is_used) 
{
if(IsReReg)
{
((st_context_t*)(pOnenetCtx->cis_context))->stateStep=0;
((st_context_t*)(pOnenetCtx->cis_context))->server->status=0;
IsReReg=FALSE;
}
               cis_pump(pOnenetCtx->cis_context);
               pOnenetCtx->state = ((st_context_t*)pOnenetCtx->cis_context)->stateStep;
           }
       }
sleep(1);
}
}


return NULL;
}

extern BOOL ON_FsuOneNetInit(DWORD DevRef, DWORD DevLifeTime, char *IMEI, char *IMSI, RESOURCE_DESC_TAB *pResourceTab)
{
int i,OldStatus;


if(IMEI==NULL || IMSI==NULL || pResourceTab==NULL)
{

return FALSE;
}
PTHREAD_MUTEX_SAFE_LOCK(sFsuOneNetMutex,OldStatus);
if(sFsuOneNetServer.IsOpenedServer)
{
PTHREAD_MUTEX_SAFE_UNLOCK(sFsuOneNetMutex,OldStatus);

return TRUE;
}
sFsuOneNetServer.m_DevRef=DevRef;
sFsuOneNetServer.m_DevLifetime=DevLifeTime;
if(sFsuOneNetServer.m_DevLifetime==0)
{
        sFsuOneNetServer.m_DevLifetime=3600;
    }
strcpy(sFsuOneNetServer.m_IMEI,IMEI);
strcpy(sFsuOneNetServer.m_IMSI,IMSI);
sFsuOneNetServer.m_ResourceTab=*pResourceTab;
sFsuOneNetServer.ListenFd=SOCKET_INVALID_FD;
if(pthread_create(&sFsuOneNetServer.ptServerThreadID,NULL,OneNetServerThread,NULL)!=STD_SUCCESS)
{
PTHREAD_MUTEX_SAFE_UNLOCK(sFsuOneNetMutex,OldStatus);

return FALSE;
}


sFsuOneNetServer.IsOpenedServer=TRUE;
PTHREAD_MUTEX_SAFE_UNLOCK(sFsuOneNetMutex,OldStatus);
return TRUE;
}

extern void ON_FsuOneNetNotify(DWORD onenet_id, DWORD objectid, DWORD instanceid, DWORD resourceid, DWORD valuetype, DWORD len, const char *value)
{
int i,OldStatus;
int32_t cis_error;
OnenetObserved *tmpObserved;


if(!sFsuOneNetServer.m_IsDisCover)
return;
PTHREAD_MUTEX_SAFE_LOCK(sFsuOneNetWorkMutex,OldStatus);
tmpObserved = (OnenetObserved *)CIS_LIST_FIND(sFsuOneNetServer.m_pOservedList,H_CONST_VAL(objectid*resourceid)+instanceid);
PTHREAD_MUTEX_SAFE_UNLOCK(sFsuOneNetWorkMutex,OldStatus);

if (NULL != tmpObserved) 
{
int32_t index=0;
uint32_t ackid=(uint32_t)-1;


if (index > 0) 
{
cis_error = OnenetNotify((int)onenet_id, (int)tmpObserved->lastMid, (int)tmpObserved->objectId, (int)tmpObserved->instanceId,tmpObserved->resourceId, valuetype, len, value, ackid, CIS_NOTIFY_CONTINUE);
}
else if (index == 0)
{
cis_error = OnenetNotify((int)onenet_id, (int)tmpObserved->lastMid, (int)tmpObserved->objectId, (int)tmpObserved->instanceId,tmpObserved->resourceId, valuetype, len, value, ackid, CIS_NOTIFY_CONTENT);
}
else
{

cis_error = ONENET_ERRID_PARAMETER_ERROR;
}
}
}


阅读(3306) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~