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

📄 btd.c

📁 bluetooth 驱动
💻 C
📖 第 1 页 / 共 5 页
字号:
  "  sdp_conn  <xx:xx:xx:xx:xx:xx> <profile>",  "  tcs_conn  <xx:xx:xx:xx:xx:xx>",  "                               ",  "  test_conn  <xx:xx:xx:xx:xx:xx> <psm>", /* l2cap test using PSM psm */  "  test_disc", /* disconnect all (both) test connections */  "  test_case_reject <xx:xx:xx:xx:xx:xx>",   "  ping <xx:xx:xx:xx:xx:xx>",  "  getinfo <xx:xx:xx:xx:xx:xx> <type>",  "                               ",  "  bb_conn  <xx:xx:xx:xx:xx:xx>", /* connect only baseband */  "  bb_disc  <hci handle>", /* disconnect baseband */  "                               ",#ifdef CONFIG_BLUETOOTH_UNPLUG_TEST  "  t <testcase>", /* e.g testcase 4.3 't 43' */#endif  "",#endif  NULL};intmain(int argc, char **argv){   int opt;  int spd;  char pid_buf[80];  printf("Bluetooth Control Application\n");  printf("-----------------------------\n");  if (atexit(btd_cleanup) < 0)  {    printf("btd failed to register cleanup function.\n");    exit(1);  }#ifndef BTD_USERSTACK    if ((pid_fd = open(PID_FILE, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666)) < 0)  {    if (errno == EEXIST)    {      printf("There is already another version of btd running.\n");    }    else    {      printf("Could not create pid file for btd.\n");    }    exit(0);  }#endif    sprintf(pid_buf, "%d\n", getpid());  write(pid_fd, pid_buf, strlen(pid_buf));  close(pid_fd);  /* do not set pid_fd to -1, we use it to mark that we created the pid file */  /* now parse options */#define OPTIONS_STRING "a:b:d:D:e:i:mn:r:Rs:ST:u:X"  while ((opt = getopt_long(argc, argv, OPTIONS_STRING,                            long_options, &option_index)) != -1)  {    switch(opt)    {     case 'u':      /* uart device */      physdev = optarg;      break;     case 'b':      /* bluetooth device */      btdev = optarg;      break;           case 'd':      /* Sets local ipaddress in pppd options */      local_address = strdup(optarg);      break;           case 'D':      /* Sets remote ipaddress in pppd options */      remote_address = strdup(optarg);      break;           case 'e':      /* switches on/off the modem emulation */      modem_emulation = atoi(optarg);      break;     case 'm':      /* enter command line mode */      enter_cmd_mode = 1;      break;     case 'n':      strncpy(local_name, optarg, LOCAL_NAME_LENGTH);      local_name[LOCAL_NAME_LENGTH] = '\0';      break;     case 'r':      /* role */      if (strcmp(optarg, "client") == 0)        role = CLIENT;      else if (strncmp(optarg, "server", 6) == 0)        role = SERVER;      break;     case 'R':      /* try to reset the hardware */      do_reset = 1;      break;     case 's':      /* speed */      speedstring = optarg;      break;#ifdef BTD_USERSTACK     case 'S':      /* Use local unix socket together with hci emulation */      use_local_socket = 1;      /* adjust invalid settings */      do_reset = 0;      modem_emulation = 0;      hw_init = 0;      enter_cmd_mode = 1; /* temp fix until bt_waitline works for usermode */      break;     case 'T':       /* Use TCP socket together with hci emulation */       use_tcp_socket = 1;       tcp_address = optarg; /* ipaddress:port */         /* adjust invalid settings */       do_reset = 0;       hw_init = 0;       modem_emulation = 0;       enter_cmd_mode = 1; /* force */       break;#endif     case '?':/*      printf("unknown option: %c\n", optopt); */      break;    }  }    init_sighandler();  /* Hardreset of BT hardware */  if (do_reset)      reset_hw();   #ifdef RESTART_ENABLED  /* restart point after setting bd address in ericsson hw */  if (sigsetjmp(jmpbuffer, 1) != 0)  {    printf("restart...\n\n\n\n");  }#endif  if (role == SERVER)    printf("Running as server\n");  else {    printf("Running as client\n");    enter_cmd_mode = 1; /* force */  }  #ifdef BTD_USERSTACK  printf("Running stack in user mode\n");  if (use_local_socket)  {     physdev = SOCKET_NAME;    printf("Using local UNIX socket %s\n", physdev);      }  else if (use_tcp_socket)  {    physdev = tcp_address;    printf("Using TCP socket to %s\n", physdev);   }  else  {    printf("Physdev %s, btdev (not used), speed %s baud\n",            physdev, speedstring);  }  #else  printf("Physdev %s, btdev %s, speed %s baud\n",          physdev, btdev, speedstring);#endif  if (role == SERVER)	  sdpsrv_pid = start_sdp_server();    spd = atoi(speedstring);  if ((phys_fd = open_device(physdev, O_RDWR, role)) < 0)  {    perror("could not open phys dev\n");    exit(1);  }    init_phys(phys_fd);  #ifndef BTD_USERSTACK  /* Set the current tty to the bluetooth discpline */  set_bt_line_disc(phys_fd, bt_disc, physdev);#endif    if ((bt_cfd = open_device(BT_CTRL_TTY, O_RDWR, 0)) < 0)  {    perror("could not open control\n");    exit(1);  }  init_stack(spd);  /* Now set phys device speed to whatever HW was set to */  fd_setup(phys_fd, spd, USE_FLOW_CTRL);  tcflush(phys_fd, TCIOFLUSH);#ifdef BTD_USERSTACK  open_pty();  init_pty_thread();#endif  if (enter_cmd_mode)  {    read_history(BTD_HISTORY_FILE);    printf("Now entering cmd line mode\n");    show_menu();        while (1)    {      char *line = readline("> ");      int tmp;      if (!line)      {        start_ppp = 0;        break;      }      add_history(line);      tmp = process_cmd(line, bt_cfd);      if (tmp  == START_PPP)      {        start_ppp = 1;        break;      }      else if (tmp == QUIT_BTD)      {        start_ppp = 0;        break;      }            free(line);    }    write_history(BTD_HISTORY_FILE);  }    if (!start_ppp)  {    exit(0);  }  while (1)  {    if (modem_emulation)    {      bt_fd = open_device(btdev, O_RDWR, 0);      syslog(LOG_INFO, "Starting modem_emulator");      modem_emulator(bt_fd, NULL, 0);#ifdef BTD_USERSTACK      while (!modem_connected)        sleep(1);#endif      if (btd_shuttingdown)        exit(0);      syslog(LOG_INFO, "Modem done.");      close_device(bt_fd); /* for pppd... */      bt_fd = -1;    }     #ifdef USE_IPASSIGN    /* wait until line is connected */    /* fixme -- for now only use line 0 */    bt_waitline(bt_cfd, 0);    /* check if there is a server available */    if (! use_ipassign)    {      if ((ipa_sock = open_socket(SRVSOCKET, CLIENT)) >= 0)      {        syslog(LOG_INFO, "IP Assign server OK.\n");        close_device(ipa_sock);        use_ipassign = 1;          }      else      {        syslog(LOG_INFO, "Could not find IP Assign server\n");      }    }#endif    syslog(LOG_INFO, "build_pppdopt");    build_pppdopt(role);    syslog(LOG_INFO, "start pppd...");    /* Now start pppd */    start_pppd();     if (modem_emulation)      modem_connected = 0;    /* pppd done, restart */    syslog(LOG_INFO, "ppp child died, now restart!\n\n\n");  }}/* readline replacement  - mfuchs */#if !defined(HAVE_READLINE) && !defined(HAVE_READLINE_READLINE)#define MAXLINE 100void read_history(char *hist_file_name){}void write_history(char *hist_file_name){}void add_history(char *command){}char *readline(char *promt){  int len;  char *line;  if ((line = malloc(MAXLINE)))  {    printf("%s", promt);    fflush(stdout);    *line = 0;    if ((len = read(STDIN_FILENO, line, MAXLINE-1)) >= 0)    {      line[len] = 0;    }  }  return line;}#endif /* !HAVE_READLINE */#define SDPSRV_CONF "/etc/sdp.xml"#ifdef BTD_USERSTACK#define SDPSRV_PROC "/tmp/sdp_sock"#else#define SDPSRV_PROC "/proc/sdp_srv"#endifint start_sdp_server(void){  int sdpsrv_pid;  char *args[] = { SDPSRV_CMD, SDPSRV_CONF, SDPSRV_PROC, NULL };  syslog(LOG_INFO, "Starting SDP server [%s]\n", SDPSRV_CMD);    if (!(sdpsrv_pid = vfork()))  {    execvp(SDPSRV_CMD, args);    fprintf(stderr, "%s: no such file or directory\n", SDPSRV_CMD);    syslog(LOG_INFO, "%s not found", SDPSRV_CMD);        _exit(0);  }  return sdpsrv_pid;}static void start_pppd(void){    /* run pppd in a child */  if (!(pppd_pid = vfork()))  {    /* modem emulation resides in stack for now */        if (role == SERVER)      syslog(LOG_INFO, "Starting ppp server on %s", btdev);    else      syslog(LOG_INFO, "Starting ppp client on %s", btdev);         execvp(PPPDCMD, pppd_options);    fprintf(stderr, "%s: no such file or directory\n", PPPDCMD);        _exit(0);  }  /* in parent, we let pppd run in the background */  syslog(LOG_INFO, "Spawned pppd[%d] in the background", pppd_pid);    /* Parent, wait for ppp child to die */  wait4(pppd_pid, NULL, 0, NULL);  pppd_pid = 0;#ifdef USE_IPASSIGN  if (use_ipassign)  {    syslog(LOG_INFO, "Returning IP address to IPA");        /* Return IP address! */    ipa_req.type = RETURNCLIENTIP;    //ipa_req.remote_bd    memcpy(&ipa_req.client, &a_client, sizeof(struct ipa_client));        if (!ipa_send(&ipa_req))      syslog(LOG_ERR, "Failed to return IP address to IPA\n");  }#endif} /* FIXME -- use separate options file 'pppd file <optionfile>' for    most common options */void build_pppdopt(int role){  int i = 0;  #ifdef USE_IPASSIGN  if (use_ipassign)    ipa_getpars();#endif  /* we have no local ip address, get it from eth0 */  if (!local_address)    local_address = get_local_addr();    /* we have no remote ip address, skip it ! */  if (!remote_address){    syslog(LOG_INFO, "Didn't get any remote IP\n");    remote_address = strdup("");  }    /* create pppd option */  sprintf(ip_addresses,"%s:%s",local_address, remote_address);    /* deallocate strings */  if (local_address)    free(local_address);    if (remote_address)    free(remote_address);    local_address = NULL;  remote_address = NULL;    syslog(LOG_INFO, "IP addresses used : %s",ip_addresses);    /* general options */   pppd_options[i++] = (char*)PPPDCMD;#ifndef USE_PSEUDOTERMINALS  pppd_options[i++] = btdev;#else  pppd_options[i++] = ptydev;#endif  pppd_options[i++] = speedstring;  pppd_options[i++] = "crtscts";/*  pppd_options[i++] = "debug";    --- moved to options file *//*  pppd_options[i++] = "local";    --- moved to options file *//*  pppd_options[i++] = "nodetach"; --- moved to options file (-detach) */   if (role==SERVER)  {    pppd_options[i++] = "nopersist";    pppd_options[i++] = "silent";    pppd_options[i++] = "passive";        if (use_radius)    {      pppd_options[i++] = "useradius";      pppd_options[i++] = "auth";      pppd_options[i++] = "login";      if (use_radius_ip_assign)      {        pppd_options[i++] = "useautoip";      }#ifdef USE_IPASSIGN      pppd_options[i++] = "localbdaddr";      pppd_options[i++] = local_bd_string;      pppd_options[i++] = "remotebdaddr";      pppd_options[i++] = remote_bd_string;#endif /* USE_IPASSIGN */          }    else    {      pppd_options[i++] = "noauth";    }        if (use_proxyarp)    {	    pppd_options[i++] = "proxyarp";	    /* ktune only works on pppd version > 2.3.10 */	    /* similar as doing ' echo 1 > /proc/sys/net/ipv4/ip_forward */	    pppd_options[i++] = "ktune"; /* enables ip_forwarding */    }    if (using_masq)    {      pppd_options[i++] = "usingmasq";    }    if (ip_addresses)    {      pppd_options[i++] = ip_addresses;    }        if (ppp_netmask)    {      pppd_options[i++] = "netmask";      pppd_options[i++] = ppp_netmask;     }        if (prim_dns)    {      pppd_options[i++] = "ms-dns";       pppd_options[i++] = prim_dns;     }        if (sec_dns)    {      pppd_options[i++] = "ms-dns";       pppd_options[i++] = sec_dns;    }    if (prim_wins)    {      pppd_options[i++] = "ms-wins";       pppd_options[i++] = prim_wins;     }        if (sec_wins)    {      pppd_options[i++] = "ms-wins";       pppd_options[i++] = sec_wins;    }  }  else  {    pppd_options[i++] = strdup("defaultroute");    

⌨️ 快捷键说明

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