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

10373阅读 0评论2012-11-21 qiushui_007
分类:LINUX

--- 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 保存。
上一篇:dd if=/dev/mtd5 of=firmware.bin, dd命令详解
下一篇:Luci - 网页用户配置界面开发