⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ul_l_drv_eth.c

📁 一个linux下rs485驱动程序的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
  d=strpbrk(dev_name,":");  if (!d)    return UL_FD_INVALID;  memset(buff,0,sizeof(buff));  strncpy(buff,dev_name,d-dev_name);  eth_ipaddr=inet_addr(buff);  dev_name=d+1;  d=strpbrk(dev_name,":");  if (!d)    return UL_FD_INVALID;  memset(buff,0,sizeof(buff));  strncpy(buff,dev_name,d-dev_name);  eth_port=htons(strtol(buff,NULL,0));  dev_name=d+1;  sock_fd=ul_eth_sock_init(eth_ipaddr,eth_port);  r=ul_fd_direct2ul_fd(sock_fd);  if (r==UL_FD_INVALID)     return UL_FD_INVALID;  eth_fd=ul_fd2eth_context(r);  eth_fd->ipaddress=eth_ipaddr;  eth_fd->port=eth_port;  if (ul_eth_sock_init_file(eth_fd)) {    ul_eth_close(r);    return UL_FD_INVALID;  }  l=strlen(dev_name)+1;  if(ul_dbuff_set_len(&eth_fd->dev_name, l) != l) {    ul_eth_close(r);    return UL_FD_INVALID;  }  strncpy((char*)eth_fd->dev_name.data,dev_name,l);  /* send command to server request */  eth_stamp=ul_eth_gen_stamp(eth_fd);  fprintf(eth_fd->sock_wfile,"usc %d open %s\n",eth_stamp,eth_fd->dev_name.data);  fflush(eth_fd->sock_wfile);  /* wait for responce */  i=ul_eth_recv_responce_int(eth_fd,"open",eth_stamp,&v);  if ((i!=1) || (v==0)) {    ul_eth_close(r);    return UL_FD_INVALID;  }  return r;}int ul_eth_close(ul_fd_t ul_fd){  ul_fd_eth_context_t *eth_fd;  msgbuff_inproc_item_t *mitem;  filter_inproc_item_t *fitem;  unsigned eth_stamp;  int r;  if(ul_fd==UL_FD_INVALID)    return -1;  eth_fd=ul_fd2eth_context(ul_fd);  if (eth_fd->sock_fd!=UL_SOCKET_INVALID) {    eth_stamp=ul_eth_gen_stamp(eth_fd);    fprintf(eth_fd->sock_wfile,"usc %d close\n",eth_stamp);    fflush(eth_fd->sock_wfile);    /* wait for responce */    r=-1;    ul_eth_recv_responce_int(eth_fd,"close",eth_stamp,&r);  } else    r=0;  ul_dbuff_destroy(&eth_fd->rcvbuff);  ul_dbuff_destroy(&eth_fd->dev_name);  if (eth_fd->msgbuff_start) {    mitem=msgbuff_inproc_find(&eth_fd->msgbuff_inproc_root,&eth_fd->msgbuff_start->msginfo.stamp);    if (!mitem) {      ul_msg_buf_destroy(eth_fd->msgbuff_start);      free(eth_fd->msgbuff_start);    }  }  while((fitem=filter_inproc_cut_first(eth_fd))!=NULL) {    free(fitem);  }  while((mitem=msgbuff_inproc_cut_first(&eth_fd->msgbuff_inproc_root))!=NULL) {    ul_msg_buf_destroy(mitem->msgbuff);    free(mitem);  }   if (eth_fd->sock_wfile) {    fclose(eth_fd->sock_wfile);  }  closesocket(eth_fd->sock_fd);  free(eth_fd);  return r;}int ul_eth_drv_version(ul_fd_t ul_fd){  ul_fd_eth_context_t *eth_fd;  unsigned eth_stamp;  int v;  eth_fd=ul_fd2eth_context(ul_fd);  if (ul_eth_sock_check(eth_fd)<0)    return -1;  eth_stamp=ul_eth_gen_stamp(eth_fd);  fprintf(eth_fd->sock_wfile,"usc %d drv_version\n",eth_stamp);  fflush(eth_fd->sock_wfile);  /* wait for responce */  if (ul_eth_recv_responce_int(eth_fd,"drv_version",eth_stamp,&v)!=1)     return -1;  return v;}ssize_t ul_eth_read(ul_fd_t ul_fd, void *buffer, size_t size){  ul_fd_eth_context_t *eth_fd;  ssize_t s;  eth_fd=ul_fd2eth_context(ul_fd);  if (!eth_fd->msgbuff_proc)     return -1;    s=size;  if ((eth_fd->msgbuff_idx+size)>eth_fd->msgbuff_proc->msginfo.len)    s=eth_fd->msgbuff_proc->msginfo.len-eth_fd->msgbuff_idx;    memcpy(buffer,eth_fd->msgbuff_proc->data.data,s);  return s;}ssize_t ul_eth_write(ul_fd_t ul_fd, const void *buffer, size_t size){  ul_fd_eth_context_t *eth_fd;  ul_dbuff_t *msg_data;  int new_len;  eth_fd=ul_fd2eth_context(ul_fd);  if (!eth_fd->msgbuff_proc)     return -1;  msg_data=&eth_fd->msgbuff_proc->data;  new_len = msg_data->len + size;  if(ul_dbuff_set_len(msg_data, new_len) != new_len)     return -1;  memcpy(msg_data->data + eth_fd->msgbuff_idx,buffer,size);  eth_fd->msgbuff_idx+=size;  eth_fd->msgbuff_proc->msginfo.len+=size;  return size;}int ul_eth_newmsg(ul_fd_t ul_fd,const ul_msginfo *msginfo){  ul_fd_eth_context_t *eth_fd;  ul_msg_buf_t *msgbuff;  int flg;  eth_fd=ul_fd2eth_context(ul_fd);  if (ul_eth_sock_check(eth_fd)<0)    return -1;  if (eth_fd->msgbuff_proc) {    ul_msg_buf_destroy(eth_fd->msgbuff_proc);    free(eth_fd->msgbuff_proc);  }  msgbuff=malloc(sizeof(ul_msg_buf_t));  if (!msgbuff)    return -1;  ul_msg_buf_init(msgbuff);  eth_fd->msgbuff_start=msgbuff;  eth_fd->msgbuff_proc=eth_fd->msgbuff_start;  eth_fd->msgbuff_proc->msginfo=*msginfo;  flg=msginfo->flg;  flg &= ~(UL_BFL_LOCK | UL_BFL_FAIL | UL_BFL_TAIL | UL_BFL_REC);  flg |= UL_BFL_SND;  eth_fd->msgbuff_proc->msginfo.flg=flg;  eth_fd->msgbuff_proc->msginfo.len=0;  eth_fd->msgbuff_idx=0;  return 0;}int ul_eth_tailmsg(ul_fd_t ul_fd,const ul_msginfo *msginfo){  ul_fd_eth_context_t *eth_fd;  ul_msg_buf_t *tail;  eth_fd=ul_fd2eth_context(ul_fd);  if (!eth_fd->msgbuff_proc)     return -1;  if(!(tail=malloc(sizeof(ul_msg_buf_t))))    return -1;  ul_msg_buf_init(tail);  eth_fd->msgbuff_proc->tail=tail;  eth_fd->msgbuff_proc=tail;  eth_fd->msgbuff_proc->msginfo=*msginfo;  eth_fd->msgbuff_start->msginfo.flg &= ~(UL_BFL_LOCK | UL_BFL_FAIL | UL_BFL_REC);  if(!eth_fd->msgbuff_start->msginfo.flg&UL_BFL_REC)     eth_fd->msgbuff_start->msginfo.flg|=UL_BFL_SND;  eth_fd->msgbuff_start->msginfo.flg|=UL_BFL_TAIL;  eth_fd->msgbuff_idx=0;  return 0;}int ul_eth_freemsg(ul_fd_t ul_fd){  ul_fd_eth_context_t *eth_fd;  int r1,r2,r=0;  msgbuff_inproc_item_t *item;  eth_fd=ul_fd2eth_context(ul_fd);  if (!eth_fd->msgbuff_proc)     return -1;  if (!eth_fd->msg_received_flg) {    unsigned eth_stamp=ul_eth_gen_stamp(eth_fd);    /* send */    fprintf(eth_fd->sock_wfile,"usc %d send ",eth_stamp);    r1=ul_eth_send_msg(eth_fd->sock_wfile,eth_fd->msgbuff_start);    fprintf(eth_fd->sock_wfile,"\n");    fflush(eth_fd->sock_wfile);    /* wait for responce */    r2=ul_eth_recv_responce_int(eth_fd,"send",eth_stamp,&r);     if ((r1<0) || (r2!=1) || (r<0)) {      r=-1;    } else {      /* copy message to inproc queue in case flag UL_BFL_M2IN */      if (eth_fd->msgbuff_start->msginfo.flg&UL_BFL_M2IN) {        msgbuff_inproc_item_t *item;        item=malloc(sizeof(msgbuff_inproc_item_t));        if (item) {          item->msgbuff=eth_fd->msgbuff_start;         item->stamp=r;         msgbuff_inproc_insert(&eth_fd->msgbuff_inproc_root,item);         eth_fd->msgbuff_start=NULL;         eth_fd->msgbuff_proc=NULL;         eth_fd->msgbuff_idx=0;         eth_fd->msg_received_flg=0;         return r;        }      }    }  }   /* check if this message was in inproc queue */  if (eth_fd->msg_received_flg) {    item=msgbuff_inproc_find(&eth_fd->msgbuff_inproc_root,&eth_fd->msgbuff_start->msginfo.stamp);    if (item) {      msgbuff_inproc_delete(&eth_fd->msgbuff_inproc_root,item);      free(item);    }  }  /* destroy proc message */  ul_msg_buf_destroy(eth_fd->msgbuff_start);  free(eth_fd->msgbuff_start);  eth_fd->msgbuff_start=NULL;  eth_fd->msgbuff_proc=NULL;  eth_fd->msgbuff_idx=0;  eth_fd->msg_received_flg=0;  return r;}int ul_eth_acceptmsg(ul_fd_t ul_fd,ul_msginfo *msginfo){  ul_fd_eth_context_t *eth_fd;  msgbuff_inproc_item_t *item;  ul_msg_buf_t *msgbuff,*tail;  unsigned eth_stamp;  eth_fd=ul_fd2eth_context(ul_fd);  if (ul_eth_sock_check(eth_fd)<0) {    /* check if some message is inproc queue */    if (!msgbuff_inproc_is_empty(&eth_fd->msgbuff_inproc_root)) {       item=msgbuff_inproc_first(&eth_fd->msgbuff_inproc_root);       eth_fd->msgbuff_start=item->msgbuff;       eth_fd->msgbuff_proc=eth_fd->msgbuff_start;       eth_fd->msgbuff_proc->msginfo.flg|=UL_BFL_FAIL;       eth_fd->msgbuff_proc->msginfo.len=0;       eth_fd->msgbuff_proc->msginfo.stamp=item->stamp;       ul_dbuff_destroy(&eth_fd->msgbuff_proc->data);       tail=eth_fd->msgbuff_start->tail;       while(tail) {           tail->msginfo.len=0;         ul_dbuff_destroy(&tail->data);         tail=tail->tail;       }       eth_fd->msgbuff_idx=0;       eth_fd->msg_received_flg=1;       if (msginfo)         *msginfo=eth_fd->msgbuff_proc->msginfo;       return 0;    }    return -1;  }  if (eth_fd->msgbuff_proc) {    eth_fd->msgbuff_proc=eth_fd->msgbuff_start;    eth_fd->msgbuff_idx=0;    eth_fd->msg_received_flg=1;    if (msginfo)      *msginfo=eth_fd->msgbuff_proc->msginfo;    return 0;  }  msgbuff=malloc(sizeof(ul_msg_buf_t));  if (!msgbuff)    return -1;  ul_msg_buf_init(msgbuff);  eth_stamp=ul_eth_gen_stamp(eth_fd);  fprintf(eth_fd->sock_wfile,"usc %d recv\n",eth_stamp);  fflush(eth_fd->sock_wfile);  /* wait for responce */  if (ul_eth_recv_responce_msgbuf(eth_fd,"recv",eth_stamp,msgbuff)!=1)     return -1;  /* a new messsage is ready */  eth_fd->msgbuff_start=msgbuff;  eth_fd->msgbuff_proc=eth_fd->msgbuff_start;  eth_fd->msgbuff_idx=0;  eth_fd->msg_received_flg=1;  if (msginfo)    *msginfo=eth_fd->msgbuff_proc->msginfo;  return 0;}int ul_eth_actailmsg(ul_fd_t ul_fd,ul_msginfo *msginfo){  ul_fd_eth_context_t *eth_fd;  eth_fd=ul_fd2eth_context(ul_fd);  if (!eth_fd->msgbuff_proc)     return -1;  if (!eth_fd->msgbuff_proc->tail)    return -1;  eth_fd->msgbuff_proc=eth_fd->msgbuff_proc->tail;  eth_fd->msgbuff_idx=0;  *msginfo=eth_fd->msgbuff_proc->msginfo;  return 0;}int ul_eth_addfilt(ul_fd_t ul_fd,const ul_msginfo *msginfo){  ul_fd_eth_context_t *eth_fd;  filter_inproc_item_t *fitem;  unsigned eth_stamp;  int v;  eth_fd=ul_fd2eth_context(ul_fd);  if (ul_eth_sock_check(eth_fd)<0)    return -1;  fitem=malloc(sizeof(filter_inproc_item_t));  if (!fitem)    return -1;  eth_stamp=ul_eth_gen_stamp(eth_fd);  fprintf(eth_fd->sock_wfile,"usc %d addfilt %02x%02x%02x\n",eth_stamp,msginfo->sadr,msginfo->dadr,msginfo->cmd);  fflush(eth_fd->sock_wfile);  /* wait for responce */  if (ul_eth_recv_responce_int(eth_fd,"addfilt",eth_stamp,&v)!=1) {    free(fitem);    return -1;  }  /* insert in filter queue */  fitem->msginfo=*msginfo;  filter_inproc_insert(eth_fd,fitem);  return v;}int ul_eth_abortmsg(ul_fd_t ul_fd){  ul_fd_eth_context_t *eth_fd;  eth_fd=ul_fd2eth_context(ul_fd);  if (!eth_fd->msgbuff_proc)     return -1;  ul_msg_buf_destroy(eth_fd->msgbuff_start);  free(eth_fd->msgbuff_start);  eth_fd->msgbuff_start=NULL;  eth_fd->msgbuff_proc=NULL;  eth_fd->msgbuff_idx=0;  return 0;}int ul_eth_rewmsg(ul_fd_t ul_fd){  ul_fd_eth_context_t *eth_fd;  eth_fd=ul_fd2eth_context(ul_fd);  if (!eth_fd->msgbuff_proc)     return -1;  eth_fd->msgbuff_idx=0;  return 0;}int ul_eth_inepoll(ul_fd_t ul_fd){  return ul_eth_acceptmsg(ul_fd,NULL)==0?1:0;}int ul_eth_debflg(ul_fd_t ul_fd,int debug_msk){  return 0;}int ul_eth_setmyadr(ul_fd_t ul_fd, int newadr) {  return 0;}int ul_eth_setidstr(ul_fd_t ul_fd, const char *idstr) {  return 0;}int ul_eth_setbaudrate(ul_fd_t ul_fd, int baudrate) {  return 0;}int ul_eth_fd_wait(ul_fd_t ul_fd, int wait_sec){  ul_fd_eth_context_t *eth_fd;  ul_msg_buf_t *msgbuff;  unsigned eth_stamp;  eth_fd=ul_fd2eth_context(ul_fd);  if (ul_eth_sock_check(eth_fd)<0) {    /* check if some message is inproc queue */    if (!msgbuff_inproc_is_empty(&eth_fd->msgbuff_inproc_root))      return 1;    SLEEP(wait_sec);    return -1;  }  if (eth_fd->msg_received_flg)     return 1;  msgbuff=malloc(sizeof(ul_msg_buf_t));  if (!msgbuff)    return -1;  ul_msg_buf_init(msgbuff);  eth_stamp=ul_eth_gen_stamp(eth_fd);  fprintf(eth_fd->sock_wfile,"usc %d fd_wait_recv %d\n",eth_stamp,wait_sec);  fflush(eth_fd->sock_wfile);  /* wait for responce */  if (ul_eth_recv_responce_msgbuf(eth_fd,"fd_wait_recv",eth_stamp,msgbuff)!=1) {    free(msgbuff);    return -1;  }  /* a new messsage is ready */  eth_fd->msgbuff_start=msgbuff;  eth_fd->msgbuff_proc=eth_fd->msgbuff_start;  eth_fd->msgbuff_idx=0;  eth_fd->msg_received_flg=1;  return 1;}int ul_eth_namematch(const char *dev_name){  int ret=0;  if(!dev_name) ret=1;  else if(!strncmp(dev_name,"eth:",4))    ret=100; /* 100% name match for driver */  else if(!strchr(dev_name,':'))    ret=5;   /* no transport selected, direct driver is reasonable for this name */  return ret;}ul_fd_ops_t ul_fd_eth_ops={  "eth",  ul_eth_namematch,  ul_eth_open,  ul_eth_close,  ul_eth_drv_version,  ul_eth_read,  ul_eth_write,  ul_eth_newmsg,  ul_eth_tailmsg,  ul_eth_freemsg,  ul_eth_acceptmsg,  ul_eth_actailmsg,  ul_eth_addfilt,  ul_eth_abortmsg,  ul_eth_rewmsg,  ul_eth_inepoll,  ul_eth_debflg,  ul_eth_fd_wait,  ulop_eth_fd2sys_fd,  ul_eth_setmyadr,  ul_eth_setidstr,  ul_eth_setbaudrate};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -