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

📄 hci_stub.c

📁 牛人写的一个蓝牙驱动教学程序
💻 C
📖 第 1 页 / 共 2 页
字号:
    printf("\t# Num_HCI_Command_Packets [ 1 byte/s] = 0x%02X\n", RESPONSE[3]);    printf("\t# Command_Opcode [ 2 byte/s] = 0x%02X%02X\n",           RESPONSE[5],           RESPONSE[4]);    /* Check the status of the command */    if (RESPONSE[6] == 0x00)    {      printf("\t# Status [ 1 byte/s] = SUCCESS (0)\n");      /* Display the BD Address */      printf("\n# BD_ADDR [ 6 byte/s] = 0x");      for (nCount = 7; nCount < nNumberOfBytesRead; nCount++)      {        printf("%02X", RESPONSE[nCount]);      }      printf("\n");    }    else    {      printf("\t# Status [ 1 byte/s] = FAILURE (0x%02X)\n", RESPONSE[6]);    }  }  else  {    printf("\nFailed to read the response from the device.\n");  }  /* Wait for a key press */  printf("Press any key continue.....\n");  fflush(stdin);  getchar();}/* Function to read the buffer status */static void ReadBufferStatus(){  /* To hold the number of bytes written */  unsigned int nNumberOfBytesWritten = 0;  /* To hold the number of bytes read */  unsigned int nNumberOfBytesRead = 0;  /* To hold the number of retries */  unsigned int nCount = 0;  /* Display the command details */  printf("Command to read the Buffer Status\n");  printf("\t# COMMAND 01 05 10 00\n");  printf("\t# HCI_PacketOpcode [ 2 byte/s] = 0x1005\n");  printf("\t# Parameter_Length [ 1 byte/s] = 0x00\n");  /* Issue the command to read the buffer status of the device */  do  {    nNumberOfBytesWritten = write(fd, RBS, RBS_CMD_LENGTH);  }  while (nNumberOfBytesWritten != RBS_CMD_LENGTH);  /* Read the status of the command to read the Buffer Status of the device */  memset(RESPONSE, 0, HCI_MAX_PACKET_SIZE);  do  {    nNumberOfBytesRead = read(fd, RESPONSE, HCI_MAX_PACKET_SIZE);  }  while (nNumberOfBytesRead == 0);  /* Check the status */  if (nNumberOfBytesRead >= RBS_CMD_RSP_LENGTH)  {    /* Display the event (response) details */    printf("Response to the command to read the Buffer Status\n");    printf("\t# EVENT ");    for (nCount = 0; nCount < nNumberOfBytesRead; nCount++)    {      printf("%02X ", RESPONSE[nCount]);    }    printf("\n");    printf("\t# HCI_PacketOpcode [ 1 byte/s] = 0x%02X\n", RESPONSE[1]);    printf("\t# Parameter_Length [ 1 byte/s] = 0x%02X\n", RESPONSE[2]);    printf("\t# Num_HCI_Command_Packets [ 1 byte/s] = 0x%02X\n", RESPONSE[3]);    printf("\t# Command_Opcode [ 2 byte/s] = 0x%02X%02X\n",           RESPONSE[5],           RESPONSE[4]);    /* Check the status of the command */    if (RESPONSE[6] == 0x00)    {      printf("\t# Status [ 1 byte/s] = SUCCESS (0)\n");      printf("\t# HC_ACL_Data_Packet_Length [ 2 byte/s] = 0x%02X%02X\n",             RESPONSE[8],             RESPONSE[7]);      printf("\t# HC_SCO_Data_Packet_Length [ 1 byte/s] = 0x%02X\n",             RESPONSE[9]);      printf("\t# HC_Total_Num_ACL_Data_Packets [ 2 byte/s] = 0x%02X%02X\n",             RESPONSE[11],             RESPONSE[10]);      printf("\t# HC_Total_Num_SCO_Data_Packets [ 2 byte/s] = 0x%02X%02X\n",             RESPONSE[13],             RESPONSE[12]);    }    else    {      printf("\t# Status [ 1 byte/s] = FAILURE (0x%02X)\n", RESPONSE[6]);    }  }  else  {    printf("\nFailed to read the response from the device.\n");  }  /* Wait for a key press */  printf("Press any key continue.....\n");  fflush(stdin);  getchar();}/* Function to change the local device name */static void ChangeLocalName(){  /* To hold the number of bytes written */  unsigned int nNumberOfBytesWritten = 0;  /* To hold the number of bytes read */  unsigned int nNumberOfBytesRead = 0;  /* To hold the number of retries */  unsigned int nCount = 0;  /* To hold the device name */  unsigned char strDeviceName[256];  /* To hold the command */  unsigned char strCommand[HCI_MAX_PACKET_SIZE];  /* Clear the device name string */  memset(strDeviceName, 0, 256);  /* Read the name to be assigned to the device */  printf("Enter the device name: ");  scanf("%s", strDeviceName);  /* Format the command packet */  memset(strCommand, 0, HCI_MAX_PACKET_SIZE);  memcpy(strCommand, CLN, CLN_CMD_LENGTH);  memcpy(&strCommand[CLN_CMD_LENGTH], strDeviceName, 256);  /* Display the command details */  printf("Command to Change the Local Name\n");  printf("\t# COMMAND ");  for (nCount = 0; nCount < HCI_MAX_PACKET_SIZE; nCount++)  {    printf("%02X ", strDeviceName[nCount]);    if (((nCount + 1) % 15) == 0)    {      printf("\n\t");    }  }  printf("\n");  printf("\t# HCI_PacketOpcode [ 2 byte/s] = 0x0C13\n");  printf("\t# Parameter_Length [ 1 byte/s] = 0xF8\n");  printf("\t# Name [248 byte/s] = 0x");  for (nCount = 0; nCount < 248; nCount++)  {    printf("%02X", strCommand[nCount + 4]);    if (((nCount + 4) % 15) == 0)    {      printf("\n\t");    }  }  printf("\n");  /* Issue the command to change the local device name */  do  {    nNumberOfBytesWritten = write(fd, strCommand, 252);  }  while (nNumberOfBytesWritten != HCI_MAX_PACKET_SIZE);  /* Read the status of the command to change the local device name */  memset(RESPONSE, 0, HCI_MAX_PACKET_SIZE);  do  {    nNumberOfBytesRead = read(fd, RESPONSE, HCI_MAX_PACKET_SIZE);  }  while (nNumberOfBytesRead == 0);  /* Check the status */  if (nNumberOfBytesRead >= CLN_CMD_RSP_LENGTH)  {    /* Display the event (response) details */    printf("Response to the command to read the Buffer Status\n");    printf("\t# EVENT ");    for (nCount = 0; nCount < nNumberOfBytesRead; nCount++)    {      printf("%02X ", RESPONSE[nCount]);      if ((nCount % 15) == 0)      {        printf("\n\t");      }    }    printf("\n");    printf("\t# HCI_PacketOpcode [ 1 byte/s] = 0x%02X\n", RESPONSE[1]);    printf("\t# Parameter_Length [ 1 byte/s] = 0x%02X\n", RESPONSE[2]);    printf("\t# Num_HCI_Command_Packets [ 1 byte/s] = 0x%02X\n", RESPONSE[3]);    printf("\t# Command_Opcode [ 2 byte/s] = 0x%02X%02X\n",           RESPONSE[5],           RESPONSE[4]);    /* Check the status of the command */    if (RESPONSE[6] == 0x00)    {      printf("\t# Status [ 1 byte/s] = SUCCESS (0)\n");    }    else    {      printf("\t# Status [ 1 byte/s] = FAILURE (0x%02X)\n", RESPONSE[6]);    }  }  else  {    printf("\nFailed to read the response from the device.\n");  }  /* Wait for a key press */  printf("Press any key continue.....\n");  fflush(stdin);  getchar();}/* Function to read the local device name */static void ReadLocalName(){  /* To hold the number of bytes written */  unsigned int nNumberOfBytesWritten = 0;  /* To hold the number of bytes read */  unsigned int nNumberOfBytesRead = 0;  /* To hold the number of retries */  unsigned int nCount = 0;  /* To hold the device name */  char strDeviceName[256];  /* Display the command details */  printf("Command to Read the Local Name\n");  printf("\t# HCI_PacketOpcode [ 2 byte/s] = 0x0C14\n");  printf("\t# Parameter_Length [ 1 byte/s] = 0x00\n");  /* Issue the command to read the buffer status of the device */  do  {    nNumberOfBytesWritten = write(fd, RLN, RLN_CMD_LENGTH);  }  while (nNumberOfBytesWritten != RLN_CMD_LENGTH);  /* Read the status of the command to read the Buffer Status of the device */  memset(RESPONSE, 0, HCI_MAX_PACKET_SIZE);  do  {    nNumberOfBytesRead = read(fd, RESPONSE, HCI_MAX_PACKET_SIZE);  }  while (nNumberOfBytesRead == 0);  /* Check the status */  if (nNumberOfBytesRead >= RLN_CMD_RSP_LENGTH)  {    /* Display the event (response) details */    printf("Response to the command to read the Buffer Status\n");    printf("\t# EVENT ");    for (nCount = 0; nCount < nNumberOfBytesRead; nCount++)    {      printf("%02X ", RESPONSE[nCount]);    }    printf("\n");    printf("\t# HCI_PacketOpcode [ 1 byte/s] = 0x%02X\n", RESPONSE[1]);    printf("\t# Parameter_Length [ 1 byte/s] = 0x%02X\n", RESPONSE[2]);    printf("\t# Num_HCI_Command_Packets [ 1 byte/s] = 0x%02X\n", RESPONSE[3]);    printf("\t# Command_Opcode [ 2 byte/s] = 0x%02X%02X\n",           RESPONSE[5],           RESPONSE[4]);    /* Check the status of the command */    if (RESPONSE[6] == 0x00)    {      printf("\t# Status [ 1 byte/s] = SUCCESS (0)\n");      printf("\t# Name [248 byte/s] = 0x");      for (nCount = RLN_CMD_RSP_LENGTH; nCount < nNumberOfBytesRead; nCount++)      {        printf("%02X", RESPONSE[nCount]);      }      printf("\n");      /* Extract the device name */      memcpy(strDeviceName,             &(RESPONSE[RLN_CMD_RSP_LENGTH]),             nNumberOfBytesRead - RLN_CMD_RSP_LENGTH);      /* Display the device name */      printf("Device Name: %s\n", strDeviceName);    }    else    {      printf("\t# Status [ 1 byte/s] = FAILURE (0x%02X)\n", RESPONSE[6]);    }  }  else  {    printf("\nFailed to read the response from the device.\n");  }  /* Wait for a key press */  printf("Press any key continue.....\n");  fflush(stdin);  getchar();}

⌨️ 快捷键说明

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