CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Bluetooth Module Driver

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
endSly



Joined: 27 Jun 2007
Posts: 6

View user's profile Send private message

Bluetooth Module Driver
PostPosted: Wed Jun 27, 2007 5:46 am     Reply with quote

This driver suports all a7eng serial bluetooth modules and parallax Bluetooth Module.
This is a first beta.

Code:
/***************************************************\
 * bt.c     Bluetooth Controler Library
 *
 *    Distribute freely with copyright notice intact.
 *    Library to control a7eng's Bluetooth Modules eb100
 *    and eb500 by serial port using the CCS compilers.
 *       www.a7eng.com
 *
 * (c) 2007 endSly
 *
 ** Functions Reference **
 *-------------------------
 * bt_command_mode(EscapeChar)
 *    The bt_command_mode allow  to use commands.
 *    When Bluetooth device started goes automaticaly to
 *    command Mode
 *    Parameters:
 *     EscapeChar Control Char to escape from EasyConnect
 *                Mode. Default '+'
 *
 * bt_connect(Address, Profile, Timeout)
 *    The bt_connect command establishes a connection
 *    to another Bluetooth device. Returns 1 if error
 *    Parameters:
 *     Address    The Bluetooth address of the remote
 *                device.
 *     Profile    The Bluetooth profile to connect with.
 *     Timeout    An optional parameter used to abort
 *                the connection request after the
 *                specified number of seconds.
 *                0 -> Not Used
 *
 * bt_delete_trusted(All, Address)
 *    The delete trusted device command removes the
 *    remote device from the trusted list and prevents
 *    it from being able to connect with the local Bluetooth
 *    device when security is turned on.
 *    Parameters:
 *     All        This parameter is used to remove   
 *                all devices from trusted status if 1.
 *     Address    The Bluetooth address of the device that
 *                should be removed from trusted status if all 0.
 *
 * bt_diconnect()
 *    The bt_disconnect command closes the connection 
 *    with the remote Bluetooth device.
 *
 * bt_EasyConnect_mode()
 *    The bt_EasyConnect_mode commandallow to send messages
 *    via Bluetooth;
 *
 * bt_get_address(Device)
 *    The bt_get_address command returns a globally unique
 *    address for either the local Bluetooth device
 *    or the remote Bluetooth device.
 *    Parameters:
 *     Device     LOCAL or REMOTE
 *
 * bt_get_connectable()
 *    The bt_get_connectable command returns the current
 *    connectable setting of the local Bluetooth device.
 *    Returns 1 if on, 0 if off or error
 *
 * bt_get_encrypt()
 *    The bt_get_encryp command returns the current
 *    encryption setting of the local Bluetooth device.
 *    Returns 1 if on, 0 if off or error
 *   
 * bt_get_escape_char()
 *    The get escape character command returns the current
 *    character used in the soft break command to instruct
 *    the local EmbeddedBlue device to break the flow of
 *    data and begin accepting commands.
 *    Returns char or 0 if error
 *
 * bt_get_flow_control()
 *    The bt_get_flow_control command returns the flow control
 *    setting of the local EmbeddedBlue device.
 *    Returns 0 if none or 1 if hardware
 *
 * bt_get_link_timeout()
 *    The bt_get_link_timeout command returns the amount of time,
 *    in seconds, it takes for the local Bluetooth device to
 *    notice that the connection has been broken if the remote
 *    device disappears.
 *
 * bt_get_name(*Name)
 *    The bt_get_name command returns the name of the local
 *    Bluetooth device in *Name.
 *    Name's lenght 16 chars
 *
 * bt_get_security()
 *    The get security command returns the current security
 *    setting of the local EmbeddedBlue device.
 *    Returns 1 if on, 0 if off or error
 *
 * bt_get_tx_power()
 *    The bt_get_tx_power command returns the transmit
 *    power setting of the Bluetooth radio.
 *    The transmit power is set to 10 (maximum) by default.
 *
 * bt_get_visible()
 *    The bt_get_visible command returns the current visibility
 *    setting of the local EmbeddedBlue device.
 *    Returns 1 if on, 0 if off or error
 *
 * bt_list_trusted_devices(*ListAddress)
 *    The bt_list_trusted_devices command returns a list of
 *    all the devices that are allowed to connect to the
 *    local Bluetooth device when security is turned on.
 *    Parameters:
 *     ListAddress   Pointer to array of address
 *
 * bt_list_visible_devices(*ListAddress, *Name, Timeout)
 *    The bt_list_visible_devices command returns a
 *    listing of all the Bluetooth devices that are
 *    currently in range and visible.
 *    Parameters:
 *     ListAddress   Pointer to array of address
 *     Name          Pointer to array of chars (16 chars for each Name)
 *                   0xFF Not names listed
 *     Timeout       An optional parameter used to abort the
 *                   list visible devices command after the
 *                   specified number of seconds.
 *                   0 Not used
 *
 * bt_reset(Factory)
 *    The bt_reset command will cause the local Bluetooth
 *    device to reboot.
 *    Parameters:
 *     Factory       Restores all device settings to factory
 *                   defaults. This includes the baud rate parameter
 *                   which may cause serial communications to be lost after
 *                   the command is issued. To reestablish communications
 *                   with the device after resetting to factory defaults,
 *                   simply adjust the baud rate on the microprocessor
 *                   serial port to match the device default rate of 9600bps.
 *
 * bt_set_baud_rate(Rate)
 *    The bt_set_baud_rate command sets the baud rate for
 *    communications with the local Bluetooth device.
 *    Parameters:
 *     Rate          The baud rate value. Valid baud rates are
 *                   1200, 2400, 4800, 9600 (default), 19200,
 *                   38400, 57600, 115200, 230400, and 460800.
 *
 * bt_set_connectable(State)
 *    The bt_set_connectable command provides control over whether the
 *    local Bluetooth device will accept connections from other
 *    Bluetooth devices.
 *    Parameters:
 *     State         1- On / 0- Off
 *
 * bt_set_encrypt(State)
 *    The set encryption command provides control over whether
 *    transmitted data is encrypted or sent in the clear.
 *    Parameters:
 *     State         1- On / 0- Off
 *
 * bt_set_escape_char(Char)
 *    The bt_set_escape_char command provides control over the
 *    character used in the soft break command to instruct the
 *    local Bluetooth device to break the flow of data and begin
 *    accepting commands.
 *    Parameters:
 *     Char          Escape Character
 *   
 * bt_set_flow_control(State)
 *    The bt_set_flow _control command provides control over the
 *    flow control setting of the local Bluetooth device.
 *    Parameters:
 *     State         1- Hardware / 0- None
 *
 * bt_set_link_timeout(Timeout)
 *    The bt_set_link_timeout command sets the amount of time it
 *    takes for the local Bluetooth device to notice if the remote
 *    device disappears. In Bluetooth terminology, this command
 *    controls the setting for link supervisor timeout.
 *    Parameters:
 *     Timeout       The time, in seconds, it takes for the device
 *                   to notice that a connection has been broken.
 *
 * bt_set_name(*Name, Lenght)
 *    The bt_set_name command sets the name of the local Buetooth device.
 *    Parameters:
 *     Name          Pointer to array of chars with name
 *     Lenght        Lenght of the Name (Max = 32)
 *
 * bt_set_passkey(*Passkey, Lenght)
 *    The bt_set_passkey command sets the passkey that is used when
 *    establishing a connection with a new device.
 *    The passkey is set to 0000 by default.
 *    Parameters:
 *     Name          Pointer to array of chars with Passkey
 *     Lenght        Lenght of the Name (Max = 16)
 *
 * bt_set_security(State)
 *    The bt_set_security command sets the current security
 *    setting of the local Bluetooth device.
 *    Parameters:
 *     State         1- On / 0- Off
 *
 * bt_set_tx_power(Power)
 *    The bt_set_tx_power command sets the transmit power
 *    setting of the Bluetooth radio.
 *    Parameters:
 *     Power         The power setting which is an integer
 *                   between 1-10.
 *
 * bt_set_visible(State)
 *    The bt_set_visible command provides control over
 *    whether the local Bluetooth device can be seen by
 *    other Bluetooth devices.
 *    Parameters:
 *     State         1- On / 0- Off
 *
\***************************************************/
 
#define ser_out   printf
//Profiles
#define SPP       0        //Serial Port Profile         "spp"
#define DUN       1        //Dial-Up Networking Profile  "dun"
#define LOCAL     0        //Local Bluetooth
#define REMOTE    1        //Remote Bluetooth
 
typedef struct address{
  unsigned int8 add_byte[6];
}address;

//ascii_to_u8bit -> Copy and Paste from Gary Smithson work - theByteFactory.com
unsigned int8 ascii_to_u8bit (char *string)      // Converts string "A1" to unsigned 8 bit A1 (basically atoi but more compact)
{                              // NOTE: This function processes both uppercase and lowercase strings
   unsigned int8   MSN;                  // Most Significant Nibble of the result
   unsigned int8   LSN;                  // Least Significant Nibble of the result

   MSN = (string[0] - '0');         // Assume numeric and subtract text 0 which is ASCII 30
   if (string[0] >= 'A') {            // The character is actually alphabetic,...
      MSN = MSN - 7;               // ...adjust for the 7 character gap in the ASCII table
   }

   LSN = (string[1] - '0');         // Assume numeric and subtract text 0 which is ASCII 30
   if (string[1] >= 'A') {            // The character is actually alphabetic,...
      LSN = LSN - 7;               // ...adjust for the 7 character gap in the ASCII table
   }
                              // Shift the MSN into position (zero fill), strip lowercase bit and...
   return ((MSN << 4) | (LSN & 0x0F));   // ...merge it with the LSN after the lowercase bit is stripped
}

unsigned int8 ascii_to_dec (char *string)      // Converts string "11" to unsigned 8 bit 11 (basically atoi but more compact)
{                             
   unsigned int8   MSN;
   unsigned int8   LSN;           
   unsigned int8   CSN;

   MSN = (string[0] - '0');         // Assume numeric and subtract text 0 which is ASCII 30
   if(string[1]==0x00)
      return MSN;
     
   LSN = (string[1] - '0');         // Assume numeric and subtract text 0 which is ASCII 30
   if(string[2]==0x00)
      return LSN + MSN*10;
     
   CSN = (string[2] - '0');         // Assume numeric and subtract text 0 which is ASCII 30
   return CSN + LSN*10 + MSN*100;
}

void bt_command_mode(char esc_char){
   int i;
   delay_ms(1000);
   for(i=0;i<3;i++)
      ser_out(esc_char);
   delay_ms(1000);
   ser_out("\r");
}

int bt_connect(address address, int8 profile, unsigned int8 timeout){
   char r[3];
   // Example of connect command:
   //>con 00:0C:84:00:05:29 dun 10<CR>
   ser_out("con %2x:%2x:%2x:%2x:%2x:%2x ", address.add_byte[0],
                                           address.add_byte[1],
                                           address.add_byte[2],
                                           address.add_byte[3],
                                           address.add_byte[4],
                                           address.add_byte[5]);
   switch(profile) {
      case SPP :  ser_out("spp"); break;
      case DUN :  ser_out("dun"); break;           
   }                                       
   if(timeout){
      ser_out(" %u", timeout);   
   }
   ser_out("\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

int bt_delete_trusted(int1 all, address address){
   char r[3];
   ser_out("del trusted ");
   if(all)
      ser_out("all");
   else
      ser_out("%2x:%2x:%2x:%2x:%2x:%2x", address.add_byte[0],
                                         address.add_byte[1],
                                         address.add_byte[2],
                                         address.add_byte[3],
                                         address.add_byte[4],
                                         address.add_byte[5]);
   ser_out("\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

int bt_disconnect(){
   char r[3];
   ser_out("dis\r");    //Send disconnect command 
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

int bt_EasyConnect_mode(){
   char r[3];
   ser_out("ret\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

address bt_get_address(int8 device){
   int i;
   char r[3];
   char addr_s[18];
   char byte_s[2];
   address addr;
   ser_out("get address ");
   switch(device){
      case  LOCAL    :  ser_out("local");    break;
      case  REMOTE   :  ser_out("remote");   break;
   }
   ser_out("\r");
   gets(r);

   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      gets(addr_s);
      for(i=0; i<6; i++){
         byte_s[0]=addr_s[3*i];
         byte_s[1]=addr_s[3*i+1];
         addr.add_byte[i] = ascii_to_u8bit(byte_s);
      }
   }
   return addr;
}

int1 bt_get_connectable(){
   char r[3];
   char s[3];
   ser_out("get connectable\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      gets(s);
      if(s[0]=='o'&&s[1]=='n'){
         return 1;
      } else {
         return 0;
      }
   } else {
      return 0;
   }
}

int1 bt_get_encrypt(){
   char r[3];
   char s[3];
   ser_out("get encrypt\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      gets(s);
      if(s[0]=='o'&&s[1]=='n'){
         return 1;
      } else {
         return 0;
      }
   } else {
      return 0;
   }
}

char bt_get_escape_char(){
   char r[3];
   char s;
   ser_out("get escchar\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      s=getc();
      return s;
   } else {
      return 0;
   }
}

int1 bt_get_flow_control(){
   char r[3];
   char s[8];
   ser_out("get flow\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      gets(s);
      if(s[0]=='n'&&s[1]=='o'&&s[2]=='n'&&s[3]=='e'){
         return 0;
      } else {
         return 1;
      }
   } else {
      return 0;
   }
}

unsigned int8 bt_get_link_timeout(){
   char r[3];
   char s[3];
   ser_out("get linktimeout\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      gets(s);
      return ascii_to_dec(s);
   } else {
      return 0;
   }
}

void bt_get_name(char *name){
   char r[3];
   char s[16];
   int i;
   ser_out("get name\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      gets(s);
      for(i=0;i<16;i++)
         *(name + i) = s[i];
   }
}

int1 bt_get_security(){
   char r[3];
   char s[3];
   ser_out("get security\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      gets(s);
      if(s[0]=='o'&&s[1]=='n'){
         return 1;
      } else {
         return 0;
      }
   } else {
      return 0;
   }
}

unsigned int8 bt_get_tx_power(){
   char r[3];
   char s[3];
   ser_out("get txpower\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      gets(s);
      return ascii_to_dec(s);
   } else {
      return 0;
   }
}

int1 bt_get_visible(){
   char r[3];
   char s[3];
   ser_out("get visible\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      gets(s);
      if(s[0]=='o'&&s[1]=='n'){
         return 1;
      } else {
         return 0;
      }
   } else {
      return 0;
   }
}

int bt_list_trusted_devices(address *list_address){
   char r[3];
   char s[18];
   int1 cntrl=0;
   unsigned int8 byte_s[2];
   int i;
   unsigned int8 count = 0;
   ser_out("lst trusted\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      while(!cntrl){
         for(i=0;i<17;i++){
            s[i]=getchar();
            if(s[i]=='>'){
               cntrl=1;
               return count;
               break;
            }
         }
         for(i=0; i<6; i++){
            byte_s[0]=s[3*i];
            byte_s[1]=s[3*i+1];
            list_address[count].add_byte[i] = ascii_to_u8bit(byte_s);
         }
         gets(r);
         count += 1;
      }
   }
   return count;
}

int bt_list_visible_devices(address *list_address, char *name, unsigned int8 timeout){
   char r[3];
   char s[18];
   int1 cntrl=0;
   unsigned int8 byte_s[2];
   int i;
   unsigned int8 count = 0;
   ser_out("lst visible");
   if(name != 0xFF)
      ser_out(" name");
   if(timeout)
      ser_out(" %u", timeout);
   ser_out("\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      while(!cntrl){
         for(i=0;i<18;i++){
            s[i]=getchar();
            if(s[i]=='>'){
               cntrl=1;
               return count;
               break;
            }
         }
         if(name != 0xFF){
            for(i=0;i<16;i++){
               *name[i]=getchar();
               printf(*name[i]);
               if(*name[i] == 0x0D){
                  break;
               }
            }
         } else {
            gets(r);
         }
         for(i=0; i<6; i++){
            byte_s[0]=s[3*i];
            byte_s[1]=s[3*i+1];
            list_address[count].add_byte[i] = ascii_to_u8bit(byte_s);
         }
         count += 1;
         printf("OK");
      }
   }
   return count;
}

int1 bt_reset(int1 factory){
   char r[3];
   ser_out("rst");
   if(factory)
      ser_out(" factory");
   ser_out("\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

int1 bt_set_baud_rate(unsigned int32 rate){
   char r[3];
   ser_out("set baud %Lu\r", rate);
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

int1 bt_set_connectable(int1 state){
   char r[3];
   ser_out("set connectable ");
   if(state){
      ser_out("on\r");
   } else {
      ser_out("off\r");
   }
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

int1 bt_set_encrypt(int1 state){
   char r[3];
   ser_out("set encrypt ");
   if(state){
      ser_out("on\r");
   } else {
      ser_out("off\r");
   }
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

int1 bt_set_escape_char(char esc_char){
   char r[3];
   ser_out("set escchar ");
   ser_out(esc_char);
   ser_out("\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

int1 bt_set_flow_control(int1 state){
   char r[3];
   ser_out("set flow ");
   if(state){
      ser_out("none\r");
   } else {
      ser_out("hardware\r");
   }
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

int1 bt_set_link_timeout(unsigned int8 timeout){
   char r[3];
   ser_out("set linktimeout %u\r", timeout);
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

int1 bt_set_name(char *name, unsigned int8 lenght){
   char r[3];
   unsigned int8 i;
   ser_out("set name ");
   for(i=0;i<lenght;i++)
      ser_out(*(name+i));
   ser_out("\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

int bt_set_passkey(char *passkey, unsigned int8 lenght){
   char r[3];
   unsigned int8 i;
   ser_out("set passkey ");
   for(i=0;i<lenght;i++)
      ser_out(*(passkey+i));
   ser_out("\r");
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

int1 bt_set_security(int1 state){
   char r[3];
   ser_out("set security ");
   if(state){
      ser_out("on\r");
   } else {
      ser_out("off\r");
   }
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

int1 bt_set_tx_power(unsigned int8 power){
   char r[3];
   ser_out("set txpower %u\r", power);
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}

int1 bt_set_visible(int1 state){
   char r[3];
   ser_out("set visible ");
   if(state){
      ser_out("on\r");
   } else {
      ser_out("off\r");
   }
   gets(r);
   if(r[0]=='A'&&r[1]=='C'&&r[2]=='K'){
      return 0;
   } else {
      return 1;
   }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group