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;
}
}
}
阅读(3213) | 评论(1) | 转发(0) |