Bluetooth.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Baylor University
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include <config.h>
00019 #ifdef OASYS_BLUETOOTH_ENABLED
00020 
00021 #include <errno.h>
00022 #include <stdlib.h>
00023 #include <sys/types.h>
00024 #include <sys/fcntl.h>
00025 #include <sys/ioctl.h>
00026 #include <sys/socket.h>
00027 extern int errno;
00028 
00029 #include "Bluetooth.h"
00030 #include "debug/Log.h"
00031 
00032 namespace oasys {
00033 
00034 int
00035 Bluetooth::hci_devid(const char* hcidev, const char* log)
00036 {
00037     int dd = ::hci_devid(hcidev);
00038     if(log)
00039     {
00040         logf(log, LOG_DEBUG, "hci_devid %s: dd %d", hcidev, dd);
00041     }
00042     return dd;
00043 }
00044 
00045 int
00046 Bluetooth::hci_inquiry(int dev_id, int len, int nrsp, const uint8_t *lap, 
00047                        inquiry_info **ii, long flags, const char* log)
00048 {
00049     int err = ::hci_inquiry(dev_id,len,nrsp,lap,ii,flags);
00050     if(log)
00051     {
00052         logf(log, LOG_DEBUG, 
00053              "hci_inquiry(hci%d): len %d, nrsp %d, lap %p, info %p, flags 0x%lx",
00054              dev_id,len,nrsp,lap,ii,flags);
00055     }
00056     return err;
00057 }
00058 
00059 int
00060 Bluetooth::hci_open_dev(int dev_id, const char* log)
00061 {
00062     int fd = ::hci_open_dev(dev_id);
00063     if(log)
00064     {
00065         logf(log, LOG_DEBUG, "hci_open_dev(hci%d): fd %d",dev_id,fd);
00066     }
00067     return fd;
00068 }
00069 
00070 int
00071 Bluetooth::hci_close_dev(int dd, const char* log)
00072 {
00073     int err = ::hci_close_dev(dd);
00074     if(log)
00075     {
00076         logf(log, LOG_DEBUG, "hci_close_dev(%d): err %d",dd,err);
00077     }
00078     return err;
00079 }
00080 
00081 int
00082 Bluetooth::hci_read_remote_name(int dd, const bdaddr_t *bdaddr, int len, 
00083                                 char *name, int to, const char* log)
00084 {
00085     int err = ::hci_read_remote_name(dd,bdaddr,len,name,to);
00086     if(log)
00087     { 
00088         bdaddr_t ba;
00089         baswap(&ba,bdaddr);
00090         logf(log, LOG_DEBUG, 
00091              "hci_read_remote_name(%d): [%s] %s len %d to %d",
00092              dd,bd2str(ba),name,len,to);
00093     }
00094     return err;
00095 }
00096 
00097 void
00098 Bluetooth::hci_get_bdaddr(bdaddr_t* bdaddr,
00099                           const char * log)
00100 {
00101     struct hci_dev_info di;
00102     memset(&di,0,sizeof(struct hci_dev_info));
00103 
00104     // open socket to HCI control interface
00105     int fd=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
00106     if (fd<0)
00107     {
00108         if (log) logf(log, LOG_ERR, "can't open HCI socket");
00109         return;
00110     }
00111     int dev_id = hci_get_route(NULL);
00112     if (dev_id < 0)
00113     {
00114         if (log)
00115         {
00116             logf(log, LOG_DEBUG,
00117                  "bad device id");
00118             return;
00119         }
00120     }
00121     di.dev_id = dev_id;
00122     if (ioctl(fd, HCIGETDEVINFO, (void *) &di) < 0)
00123     {
00124         if(log) logf(log, LOG_ERR, "can't get device info");
00125         return;
00126     }
00127     bacpy(bdaddr,&di.bdaddr);
00128     close(fd);
00129 }
00130 
00131 int
00132 Bluetooth::hci_dev_up(int dd, const char * hcidev, const char *log)
00133 {
00134     int dev_id=-1;
00135     if (strncmp(hcidev,"hci",3) == 0 && strlen(hcidev) >= 4)
00136     {
00137         dev_id = atoi(hcidev+3);
00138     }
00139     if (dev_id<0)
00140     {
00141         if (log) logf(log, LOG_ERR, "badly formatted HCI device name: %s",hcidev);
00142         return -1;
00143     }
00144     if (ioctl(dd, HCIDEVUP, dev_id) < 0)
00145     {
00146         if (log) logf(log, LOG_ERR, "failed to init device hci%d: %s (%d)",
00147                       dev_id, strerror(errno), errno);
00148         return -1;
00149     }
00150     return 0;
00151 }
00152 
00153 /* Modified from BlueZ's bluetooth.c */
00154 char *
00155 Bluetooth::_batostr(const bdaddr_t* ba, char *str, size_t str_size)
00156 {
00157     if (!str)
00158         return NULL;
00159 
00160     memset(str,0,str_size);
00161 
00162     snprintf(str, str_size, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
00163              ba->b[5], ba->b[4], ba->b[3], 
00164              ba->b[2], ba->b[1], ba->b[0]);
00165 
00166     return str;
00167 }
00168 
00169 /* Modified from BlueZ's bluetooth.c */
00170 bdaddr_t *
00171 Bluetooth::strtoba(const char *str, bdaddr_t *addr)
00172 {
00173     const char *ptr = str;
00174     int i;
00175 
00176     if (!addr)
00177         return NULL;
00178     bdaddr_t bd;
00179     uint8_t *ba = (uint8_t*) &bd;
00180 
00181     for(i = 0; i < 6; i++) {
00182         ba[i] = (uint8_t) strtol(ptr, NULL, 16);
00183         if (i != 5 && !(ptr = strchr(ptr,':')))
00184             ptr = ":00:00:00:00:00";
00185         ptr++;
00186     }
00187 
00188     baswap(addr,(bdaddr_t *)ba);
00189     return addr;
00190 }
00191 
00192 void
00193 Bluetooth::baswap(bdaddr_t *dst, const bdaddr_t *src)
00194 {
00195     unsigned char *d = (unsigned char *) dst;
00196     const unsigned char *s = (const unsigned char *) src;
00197     int i;
00198     for (i = 0; i < 6; i++)
00199         d[i] = s[5-i];
00200 }
00201 
00202 
00203 } // namespace oasys
00204 #endif /* OASYS_BLUETOOTH_ENABLED */

Generated on Sat Sep 8 08:36:15 2007 for DTN Reference Implementation by  doxygen 1.5.3