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

📄 btd.c

📁 bluetooth 驱动
💻 C
📖 第 1 页 / 共 5 页
字号:
    pppd_options[i++] = ip_addresses;  }  pppd_options[i] = NULL;#if 0  /* print pppd_options */  i = 0;  while (pppd_options[i])    syslog(LOG_INFO, "%s\n", pppd_options[i++]);#endif}int translate_speed(int spd){  int speed_c = 0;  switch(spd)  {   case 9600:    speed_c = B9600;    break;   case 19200:    speed_c = B19200;    break;   case 38400:    speed_c = B38400;    break;   case 57600:    speed_c = B57600;    break;   case 115200:    speed_c = B115200;    break;   case 230400:    speed_c = B230400;    break;   case 460800:    speed_c = B460800;    break;   default:    printf("Bad baudrate %d.\n", spd);    break;  }  return speed_c;}voidfd_setup(int fd, int speed, int flow){  struct termios t;#ifdef BTD_USERSTACK  if (use_local_socket || use_tcp_socket)    return ;#endif  if (fd < 0)  {    perror("fd_setup");    exit(1);  }	  if (tcgetattr(fd, &t) < 0)  {      perror("tcgetattr");    exit(1);  }  cfmakeraw(&t);	  t.c_cflag &= ~CBAUD;  t.c_cflag |= translate_speed(speed) | CS8 | CLOCAL;  t.c_oflag = 0; /* turn off output processing */  t.c_lflag = 0; /* no local modes */    if (flow)    t.c_cflag |= CRTSCTS;  else    t.c_cflag &= ~CRTSCTS;    if (tcsetattr(fd, TCSANOW, &t) < 0)  {    perror("fd_setup : tcsetattr");    exit(1);  }  return;}voidshow_menu(void){  char** option;  option = menu;  while (*option)  {    printf("%s\n", *option);    option++;  }         }intprocess_cmd(char *buf, int bt_fd){  int bd[6];  unsigned char my_bd_addr[6];  unsigned int tmp[11];  int repeat;  int i, line, nbr_rsp, t;#ifdef BTD_USERSTACK  unsigned char tmp_bd[6]; /* used for byte swapping */  int server_channel, profile;#endif   if (!strncmp(buf, "quit", 4))  {    return QUIT_BTD;  }  else if (!strncmp(buf, "ppp", 3))  {    return START_PPP;  }  if (sscanf(buf, "rf_conn %x:%x:%x:%x:%x:%x %d %d",	     &tmp[0], &tmp[1], &tmp[2],	     &tmp[3], &tmp[4], &tmp[5],	     &tmp[6], &tmp[7]) == 8)  {    int i;    unsigned int con_id;    unsigned short srv_ch, line;    unsigned char tmp_bd[6];    for (i = 0; i < 6; i++)    {      tmp_bd[i] = (unsigned char)tmp[i];    }        /* layer specific for rfcomm is 16 bits => | line | dlci | */    line = (unsigned short)(tmp[7]);    srv_ch = (unsigned short)(tmp[6]);    con_id = CREATE_RFCOMM_ID(line, srv_ch<<1);    bt_connect(bt_fd, tmp_bd, con_id);  }  else if (sscanf(buf, "rf_disc %d", &line) == 1)  {    unsigned int con_id;    con_id = CREATE_RFCOMM_ID(line, 0 /* fixme -- don't care */);    bt_disconnect(bt_fd, con_id);  }  else if (sscanf(buf, "rf_wait %d", &line) == 1)  {    bt_waitline(bt_fd, line);    printf("Connect on line %d\n", line);  }  else if (sscanf(buf, "inq %d %d", &nbr_rsp, &t) == 2)  {    bt_inquiry(bt_cfd, nbr_rsp, t);  }  else if (sscanf(buf, "rf_send %d %d %d", &i, &repeat, &line) == 3)  {#define  MAXSIZE (4096*2)    struct timeval start_t, stop_t;    int bytes_tot;    int avg_speed = 0; /* bps */    unsigned int ms;    char tmp[MAXSIZE];    char dev[20];    int fd;    int j;    sprintf(dev, "/dev/ttyBT%d",line);#ifndef BTD_USERSTACK        printf("Opening %s\n", dev);    if ((fd = open_device(dev, O_RDWR, CLIENT)) < 0)    {      perror("open_device");      return 0;    }        printf("Done.\n");    bt_waitline(fd, line);#else    /* FIXME -- currently only uses line 0 */    if (line != 0)       {      printf("No support for sending data on line %d, use line 0 instead ! (userstack only)\n", line);      fd=-1;      return 0;    }#endif    if (i > MAXSIZE)    {      printf("Max 8192 bytes per write!\n");      i = MAXSIZE;    }    /* fill them with letters... */    for(j = 0; j < i; j++)    {      tmp[j] = (j % 25) + 65;    }    bytes_tot = i*repeat;        printf("\nNow sending %d %d-bytes packet (%d kB) on line %d\n",            repeat, i, bytes_tot/1000, line);    gettimeofday(&start_t, NULL);     while (repeat)    {      int n = 0;#ifndef BTD_USERSTACK      n = write(fd, tmp, i);   #else      n = bt_write_top(tmp, i, line);#endif            if (n <=0)      {        printf("couldn't write any more data...\n\n");        bytes_tot = bytes_tot-repeat*i;        break;      }      repeat--;      printf("%6d kB left to send... \r",             ((repeat*i)/1000));      fflush(stdout);    }    gettimeofday(&stop_t, NULL);    printf("\ndone.\n");    ms = (stop_t.tv_sec-start_t.tv_sec)*1000 +       (stop_t.tv_usec-start_t.tv_usec)/1000;    if (ms)      avg_speed = ((8*bytes_tot)/ms);    printf("Average TX rate : %d kbps (%d kB/s)\n", avg_speed, avg_speed/8);#ifndef BTD_USERSTACK    printf("Closing %s\n", dev);    close_device(fd);#endif  }  else if (sscanf(buf, "me %d", &i) == 1)  {    /* switches on/off the modem emulation */    modem_emulation = i;  }  else if (sscanf(buf, "setbd %x:%x:%x:%x:%x:%x",                  &bd[0], &bd[1], &bd[2], &bd[3], &bd[4], &bd[5]) == 6)  {    bt_set_bd_addr(bt_cfd, &bd[0]);  }  else if (strncmp(buf, "readbd", 6) == 0)  {    read_local_bd(bt_cfd, my_bd_addr);  }  else if (strncmp(buf, "reset", 5) == 0)  {    close_device(bt_cfd);    close_device(phys_fd);    bt_cfd = -1;    phys_fd = -1;    reset_hw();#ifdef RESTART_ENABLED    /* must set baud rate to default for hw again... */    siglongjmp(jmpbuffer, 1);#endif  }  else if(sscanf(buf, "role_switch %x:%x:%x:%x:%x:%x %d ",		 &tmp[0], &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5], &tmp[6]) == 7)  {	  unsigned char tmp_bd[6];	  for (i = 0; i < 6; i++)	  {		  tmp_bd[i] = (unsigned char)tmp[i];	  }  	  role_switch(bt_cfd, &tmp_bd[0], tmp[6]);  }#ifdef ECS_TEST_FUNCTIONS  else if (sscanf(buf, "ecs_testctrl %x,%x,%x,%x,%x,%x,%x,%x,%x",		  &tmp[0], &tmp[1], &tmp[2],		  &tmp[3], &tmp[4], &tmp[5],		  &tmp[6], &tmp[7], &tmp[8]) == 9)  {    ericsson_test_control(&tmp[0]);  }  else if (sscanf(buf, "ecs_entertest %x", &tmp[0]) == 1)  {    ericsson_enter_test_mode(&tmp[0]);      }  else if (sscanf(buf, "ecs_testcon %x:%x:%x:%x:%x:%x",             &bd[5], &bd[4], &bd[3], &bd[2], &bd[1], &bd[0]) == 6)  {    test_connection_req(&bd[0]);  }  else if (strncmp(buf, "enable_dut", 10) == 0)  {    printf("Enable device under test mode\n");    enable_dut(bt_cfd);    printf("done.\n");  }  else if (sscanf(buf, "ecs_txtest %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x",		  &tmp[0], &tmp[1], &tmp[2],		  &tmp[3], &tmp[4], &tmp[5],		  &tmp[6], &tmp[7], &tmp[8],		  &tmp[9], &tmp[10]) == 11)  {    ericsson_tx_test(&tmp[0]);  }#endif/* * Non usermode stack functions for the stuff  * below will be added later on */#ifdef BTD_USERSTACK  else if (strncmp(buf, "stat", 4) == 0)  {    char tmp[4096];    int len;    len = bt_read_internal(tmp);    tmp[len] = 0;    printf("%s", tmp);  }  else if (sscanf(buf, "ping %x:%x:%x:%x:%x:%x",		  &bd[0], &bd[1], &bd[2], &bd[3], &bd[4], &bd[5]) == 6)  {    for (i = 0; i < 6; i++)    {      tmp_bd[i] = (unsigned char)bd[i];    }     printf("Pinging bd : %02X:%02X:%02X:%02X:%02X:%02X\n",           tmp_bd[0], tmp_bd[1], tmp_bd[2],           tmp_bd[3], tmp_bd[4], tmp_bd[5]);    l2ca_ping(tmp_bd, NULL, 0);  }  else if (sscanf(buf, "getinfo %x:%x:%x:%x:%x:%x %x",		  &bd[0], &bd[1], &bd[2], &bd[3], &bd[4], &bd[5], 		  &tmp[0]) == 7)  {    for (i = 0; i < 6; i++)    {      tmp_bd[i] = (unsigned char)bd[i];    }     printf("Getinfo <%02X:%02X:%02X:%02X:%02X:%02X> type : %d\n",           tmp_bd[0], tmp_bd[1], tmp_bd[2],           tmp_bd[3], tmp_bd[4], tmp_bd[5], tmp[0]);    l2ca_getinfo(tmp_bd, tmp[0]);  }#ifdef CONFIG_BLUETOOTH_UNPLUG_TEST  else if (sscanf(buf, "t %d", &i) == 1)  {    /* unplug test cases, if you don't know what it is, don't use it !!! :) */    process_test_cmd(i);  }#endif  else if(sscanf(buf, "tcs_conn %x:%x:%x:%x:%x:%x",		 &bd[0], &bd[1], &bd[2], &bd[3], &bd[4], &bd[5]) == 6)  {        for (i = 0; i < 6; i++)    {      tmp_bd[i] = (unsigned char)bd[i];    }    printf("Connecting TCS to bd : %02X:%02X:%02X:%02X:%02X:%02X\n",           tmp_bd[0], tmp_bd[1], tmp_bd[2],           tmp_bd[3], tmp_bd[4], tmp_bd[5]);    tcs_connect_req(tmp_bd);  }  else if(sscanf(buf, "rf_conn %x:%x:%x:%x:%x:%x %d",		 &bd[0], &bd[1], &bd[2], &bd[3], &bd[4], &bd[5],		 &server_channel) == 7)  {    for (i = 0; i < 6; i++)    {      tmp_bd[i] = (unsigned char)bd[i];    }        printf("Connecting RFCOMM to bd : %02X:%02X:%02X:%02X:%02X:%02X\n",	   tmp_bd[0], tmp_bd[1], tmp_bd[2],	   tmp_bd[3], tmp_bd[4], tmp_bd[5]);        printf("RFCOMM server channel is %d\n", server_channel);        rfcomm_connect_req(tmp_bd, server_channel, 0);  }  else if(sscanf(buf, "sdp_conn %x:%x:%x:%x:%x:%x %d",		 &bd[0], &bd[1], &bd[2], &bd[3], &bd[4], &bd[5], &profile) ==7)  {    for (i = 0; i < 6; i++)    {      tmp_bd[i] = (unsigned char)bd[i];    }          printf("Connecting SDP to bd : %02X:%02X:%02X:%02X:%02X:%02X\n",           tmp_bd[0], tmp_bd[1], tmp_bd[2],           tmp_bd[3], tmp_bd[4], tmp_bd[5]);    sdp_connect_req(tmp_bd, 0);  }  else if(sscanf(buf, "test_conn %x:%x:%x:%x:%x:%x %x",		 &bd[0], &bd[1], &bd[2], &bd[3], &bd[4], &bd[5], &tmp[0]) == 7)  {    for (i = 0; i < 6; i++)    {      tmp_bd[i] = (unsigned char)bd[i];    }    printf("Connecting test layer (psm 0x%x) to bd : %02X:%02X:%02X:%02X:%02X:%02X\n", tmp[0], tmp_bd[0], tmp_bd[1], tmp_bd[2], tmp_bd[3], tmp_bd[4], tmp_bd[5]);    test_connect_psmreq(tmp_bd, tmp[0]);  }  else if (sscanf(buf, "test_case_reject %x:%x:%x:%x:%x:%x",                  &bd[0], &bd[1], &bd[2], &bd[3], &bd[4], &bd[5]) == 6)  {    unsigned short fake_psm = 0x4561;    for (i = 0; i < 6; i++)    {      tmp_bd[i] = (unsigned char)bd[i];    }    printf("test_case_reject, client tries to connect psm %d\n", fake_psm);      l2ca_connect_req(tmp_bd, fake_psm);  }  else if (strncmp(buf, "test_disc", 9) == 0)  {    l2cap_con *con = get_first_con();    printf("Disconnecting all l2cap cons\n");#if 1    con = get_first_con();    while (con!= NULL) {      l2ca_disconnect_req(con);      con = get_first_con();      sleep(1);    }#else    test_disconnect_req(testcon); /* extern l2cap_con set in test.c */    sleep(2);    test_disconnect_req(testcon2);    sleep(2);    test_disconnect_req(testcon3);#endif        /* shutdown baseband ? */  }  else if(sscanf(buf, "bb_conn %x:%x:%x:%x:%x:%x",		 &bd[5], &bd[4], &bd[3], &bd[2], &bd[1], &bd[0]) == 6)  {    for (i = 0; i < 6; i++)    {      tmp_bd[i] = (unsigned char)bd[i];    }    printf("Connecting baseband to bd : %x:%x:%x:%x:%x:%x\n",           tmp_bd[5], tmp_bd[4], tmp_bd[3], tmp_bd[2], tmp_bd[1], tmp_bd[0]);    /* we must store bd address in l2cap con to keep track of peer */    l2cap_create_con(tmp_bd);    lp_connect_req(tmp_bd);  }  else if(sscanf(buf, "bb_disc %d", &i) == 1)  {    printf("Disconnecting hci handle %d\n", i);    lp_disconnect((unsigned short)i);  }  #endif /* BTD_USERSTACK */  else  {    printf("> error: command not recognized or wrong syntax\n");    show_menu();  }  return 0;}/*  * Set server_name to the numerical IP of eth0 instead of using DNS or * relying on /etc/hosts being correct. for now. */char* get_local_addr(void){  char *local_address = NULL;  int fd = socket(AF_INET, SOCK_DGRAM, 0);  struct ifreq ifr;  ifr.ifr_addr.sa_family = AF_INET;  strcpy(ifr.ifr_name, "eth0");  ioctl(fd, SIOCGIFADDR, (int)&ifr);  close_device(fd);  local_address = strdup(inet_ntoa(((struct sockaddr_in *)				    &ifr.ifr_addr)->sin_addr));    if (!local_address)  {    printf("could not determine local IP address!\n");  }  return local_address;}void set_local_name(const char *local_name){  unsigned char domainname[DOMAIN_NAME_LENGTH+1];  unsigned char buf[LOCAL_NAME_LENGTH+HOST_NAME_LENGTH+DOMAIN_NAME_LENGTH+5];  int len = 0;  *buf = '\0';  *domainname = '\0';  if (*local_name)  {    while (*local_name && len < LOCAL_NAME_LENGTH)    {      if (*local_name >= ' ' && *local_name <= 'z')      {        buf[len++] = *local_name++;      }    }     if (len)    {      buf[len++] = ' ';      buf[len++] = '(';      buf[len] = '\0';    }  }  gethostname(&buf[len], HOST_NAME_LENGTH);  if (!strchr(&buf[len], '.'))  {    getdomainname(domainname, DOMAIN_NAME_LENGTH);    if (*domainname)    {      strcat(buf, ".");      strcat(buf, domainname);    }  }  if (len)  {    strcat(buf, ")");  }#ifndef BTD_USERSTACK    if (ioctl(bt_cfd, HCISETLOCALNAME, buf) < 0)  {    perror("HCI set local name");     exit(1);  }#else  hci_change_local_name(buf);#endif}

⌨️ 快捷键说明

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