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

📄 hci_stub.c

📁 牛人写的一个蓝牙驱动教学程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  Wipro Bluetooth HCI USB Transport Layer.  *  This file is part of the "Wipro Bluetooth USB Driver for Linux". *  Copyright (C) 2001  Wipro Technologies. <www.wipro.com>  *   *  This library is free software; you can redistribute it and/or *  modify it under the terms of the GNU Library General Public *  License as published by the Free Software Foundation; either *  version 2 of the License, or (at your option) any later version. *   *  This library is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *  Library General Public License for more details. * *  You should have received a copy of the GNU Library General Public *  License along with this library; if not, write to the Free *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * *  Please forward any queries and feedback regarding the driver to  *  Sadashivan Manickam  - Sadashivan.Manickam@wipro.com * */													/* * NAME        : HCI_Stub.c * * DESCRIPTION : This files can be used read the BD Address of the device. * */#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <stdio.h>/* Maximum HCI Packet size of bluetooth */#define HCI_MAX_PACKET_SIZE  360/* Bluetooth controller symbolic name */#define BT_CONTROLLER  "/dev/btusb1"/* Command to read the reset the bluetooth controller */char RESET[] = {0x01, 0x03, 0x0c, 0x00};/* To hold the reset command length */#define RESET_CMD_LENGTH  4/* To hold the reset command response length */#define RESET_CMD_RSP_LENGTH  7/* Command to read the BD address of the device */char RBDADDR[]  = {0x01, 0x09, 0x10, 0x00};/* To hold the Read BD Address command length */#define RBDADDR_CMD_LENGTH  4/* To hold the read BD Address command response length */#define RBDADDR_CMD_RSP_LENGTH  7/* Command to read the Buffer Status of the device */char RBS[] = {0x01, 0x05, 0x10, 0x00};/* To hold the Read Buffer Status command length */#define RBS_CMD_LENGTH  4/* To hold the read Buffer status command response length */#define RBS_CMD_RSP_LENGTH  7/* Command to change the local device name */char CLN[] = {0x01, 0x13, 0x0C, 0xF8};/* To hold the change local name command length */#define CLN_CMD_LENGTH  4/* To hold the change local name command response length */#define CLN_CMD_RSP_LENGTH  7/* Command to read the local device name */char RLN[] = {0x01, 0x14, 0x0C, 0x00};/* To hold the read local name command length */#define RLN_CMD_LENGTH  4/* To hold the read local name command response length */#define RLN_CMD_RSP_LENGTH  7/* Buffer to hold the response from the device */unsigned char RESPONSE[HCI_MAX_PACKET_SIZE];/* Pointer to the device (bluetooth controller */int fd;/* Function to reset the Bluetooth Controller */static void ResetBTController();/* Function to read the BD Address */static void ReadBDAddress();/* Function to read the buffer status */static void ReadBufferStatus();/* Function to change the local device name */static void ChangeLocalName();/* Function to read the local device name */static void ReadLocalName();/* Main function */int main(){  /* Open the BT USB Device */  fd = open(BT_CONTROLLER, O_RDWR);  if (fd > 0)  {    unsigned int choice;    while (1)    {      /* Display the menu */      printf("HCI Stub to test the BT HCI-USB Transport Layer.....\n\n");      /* To reset the Bluetooth Controller */      printf("1: Reset the Bluetooth Controller\n");      /* To read the BD address of the device */      printf("2: Read the BD Address of the device\n");      /* To read the buffer status of the device */      printf("3: Read the Buffer Status of the device\n");      /* To Change the Local Device Name */      printf("4: Change the Local Device Name\n");      /* To Read the Local Device Name */      printf("5: Read the Local Device Name\n");      /* To Exit */      printf("6: Exit\n");      /* Choice */      printf("Enter your choice: ");      /* Read the choice */      fflush(stdin);      scanf("%d",&choice);      /* Check the choice and execute the corresponding command */      switch (choice)      {        case 1:          /* Reset the Bluetooth Controller */          ResetBTController();          break;        case 2:          /* Read the BD Address of the device */          ReadBDAddress();          break;        case 3:          /* Read the Buffer Status of the device */          ReadBufferStatus();          break;        case 4:          /* Change the Local Device Name */          ChangeLocalName();          break;        case 5:          /* Read the Local Device Name */          ReadLocalName();          break;        case 6:          /* Close the device */          close(fd);          return 0;        default:          printf("Invalid option.\n");          break;      }; /* End of switch (choice) */    } /* End of while (1) */  } /* End of if (fd > 0) */  else  {    printf (" Could not open BTUSB device.\n");  }  return 0;}/* Function to reset the Bluetooth Controller */static void ResetBTController(){  /* 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 RESET the Bluetooth Controller\n");  printf("\t# COMMAND 01 03 0C 00\n");  printf("\t# HCI_PacketOpcode [ 2 byte/s] = 0x0C03\n");  printf("\t# Parameter_Length [ 1 byte/s] = 0x00\n");  /* Issue the command to reset the BT controller */  do  {    nNumberOfBytesWritten = write(fd, RESET, RESET_CMD_LENGTH);  }  while (nNumberOfBytesWritten != RESET_CMD_LENGTH);  /* Read the status of the reset command */  memset(RESPONSE, 0, HCI_MAX_PACKET_SIZE);  do  {    nNumberOfBytesRead = read(fd, RESPONSE, HCI_MAX_PACKET_SIZE);  }  while (nNumberOfBytesRead == 0);  /* Check the status */  if (nNumberOfBytesRead == RESET_CMD_RSP_LENGTH)  {    /* Display the event (response) details */    printf("Response to the RESET command\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");    }    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 BD Address */static void ReadBDAddress(){  /* 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 BD Address\n");  printf("\t# COMMAND 01 09 10 00\n");  printf("\t# HCI_PacketOpcode [ 2 byte/s] = 0x1009\n");  printf("\t# Parameter_Length [ 1 byte/s] = 0x00\n");  /* Issue the command to read the BD Address of the device */  do  {    nNumberOfBytesWritten = write(fd, RBDADDR, RBDADDR_CMD_LENGTH);  }  while (nNumberOfBytesWritten != RBDADDR_CMD_LENGTH);  /* Read the status of the command to read the BD address 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 >= RBDADDR_CMD_RSP_LENGTH)  {    /* Display the event (response) details */    printf("Response to the command to read the BD Address\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]);

⌨️ 快捷键说明

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