Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1490656
  • 博文数量: 129
  • 博客积分: 1449
  • 博客等级: 上尉
  • 技术积分: 3048
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 18:36
文章分类

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: LINUX

2012-11-21 10:56:13

--- ser2net 源码分析: 将TCP数据转发到serial


  1. static char *config_file = "/etc/ser2net.conf";
  2. --- readconfig.c: 用fgets读取配置文件的内容并进行解析(handle_config_line)
  3. --- selector.c: 利用回调函数
  4. typedef void (*t_sighup_handler)(void);
  5. static t_sighup_handler user_sighup_handler = NULL;
  6. /* The main loop for the program. This will select on the various sets, then scan for any available I/O to process.
  7. It also monitors the time and call the timeout handlers periodically. 被调用端传递数据, 循环判断有无数据*/
  8. void sel_select_loop(selector_t *sel)
  9. {
  10.     for (;;) {
  11.         memcpy(&tmp_read_set, &sel->read_set, sizeof(tmp_read_set));
  12.         memcpy(&tmp_write_set, &sel->write_set, sizeof(tmp_write_set));
  13.         memcpy(&tmp_except_set, &sel->except_set, sizeof(tmp_except_set));
  14.         err = select(sel->maxfd+1, &tmp_read_set, &tmp_write_set, &tmp_except_set, to_time);
  15.         if (err == 0) {
  16.          /* A timeout occurred. */
  17.         } else if (err < 0) {
  18.          /* An error occurred. */
  19.         } else {
  20.             /* We got some I/O. */
  21.      for (i=0; i<=sel->maxfd; i++) {
  22.                 if (FD_ISSET(i, &tmp_read_set)) {
  23.                  if (sel->fds[i].handle_read == NULL) {
  24.                             /* Somehow we don't have a handler for this. Just shut it down. */
  25.                             sel_set_fd_read_handler(sel, i, SEL_FD_HANDLER_DISABLED);
  26.                  } else {
  27.                             sel->fds[i].handle_read(i, sel->fds[i].data);
  28.                  }
  29.                 }
  30.                 if (FD_ISSET(i, &tmp_write_set)) {
  31.                  if (sel->fds[i].handle_write == NULL) {
  32.                             /* Somehow we don't have a handler for this. Just shut it down. */
  33.                             sel_set_fd_write_handler(sel, i, SEL_FD_HANDLER_DISABLED);
  34.                  } else {
  35.                             sel->fds[i].handle_write(i, sel->fds[i].data);
  36.                  }
  37.                 }
  38.                 if (FD_ISSET(i, &tmp_except_set)) {
  39.                  if (sel->fds[i].handle_except == NULL) {
  40.                             /* Somehow we don't have a handler for this. Just shut it down. */
  41.                             sel_set_fd_except_handler(sel, i, SEL_FD_HANDLER_DISABLED);
  42.                  } else {
  43.                             sel->fds[i].handle_except(i, sel->fds[i].data);
  44.                  }
  45.                 }
  46.      }
  47.         }

  48.         if (got_sighup) {
  49.          got_sighup = 0;
  50.          if (user_sighup_handler != NULL) {
  51.                     user_sighup_handler();
  52.          }
  53.         }
  54.     }    //for (;;)
  55. }

  56. //设置原型
  57. void set_sighup_handler(t_sighup_handler handler)
  58. {
  59.     user_sighup_handler = handler;
  60. }

  61. //调用端处理数据
  62. void reread_config(void)    {}
  63. set_sighup_handler(reread_config);
  64. sel_select_loop(ser2net_sel);

--- dataxfer.c == 串口部分的操作

//--- openwrt中的ipk应用
opkg install /tmp/ser2net_2.7-2_brcm63xx.ipk
看到安装成功的信息后启动ser2net
Ser2net –c /etc/ser2net.conf

设置随机启动
命令vi /etc/rc.local 打开它,i进入编辑,在exit0的上一行添加一条ser2net,esc退出编辑,wq保存
命令vi /etc/init.d/ser2net,,i进入编辑
#!/bin/sh /etc/rc.common
# /init.d/my-ser2net

START=99
start() {
Ser2net –c /etc/ser2net.conf
}
Esc退出编辑,wq 保存。
命令reboot重启路由器,ok

vi /etc/init.d/ser2net,,a进入编辑
#!/bin/sh /etc/rc.common
# /init.d/my-plugin
START=99
start() {
  ser2net
  }
Esc退出编辑,ZZ 保存。
阅读(10309) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~