00001
00002 #include <config.h>
00003 #ifdef OASYS_BLUETOOTH_ENABLED
00004
00005 #include <errno.h>
00006 #include <stdlib.h>
00007 #include <sys/types.h>
00008 #include <sys/fcntl.h>
00009 #include <sys/ioctl.h>
00010 #include <sys/socket.h>
00011 extern int errno;
00012
00013 #include <bluetooth/bluetooth.h>
00014 #include <bluetooth/hci.h>
00015 #include <bluetooth/hci_lib.h>
00016 #include <bluetooth/rfcomm.h>
00017
00018 #include "Bluetooth.h"
00019 #include "debug/Log.h"
00020
00021 namespace oasys {
00022
00023 int
00024 Bluetooth::hci_devid(const char* hcidev, const char* log)
00025 {
00026 int dd = ::hci_devid(hcidev);
00027 if(log)
00028 {
00029 logf(log, LOG_DEBUG, "hci_devid %s: dd %d", hcidev, dd);
00030 }
00031 return dd;
00032 }
00033
00034 int
00035 Bluetooth::hci_inquiry(int dev_id, int len, int nrsp, const uint8_t *lap,
00036 inquiry_info **ii, long flags, const char* log)
00037 {
00038 int err = ::hci_inquiry(dev_id,len,nrsp,lap,ii,flags);
00039 if(log)
00040 {
00041 logf(log, LOG_DEBUG,
00042 "hci_inquiry(hci%d): len %d, nrsp %d, lap %p, info %p, flags 0x%lx",
00043 dev_id,len,nrsp,lap,ii,flags);
00044 }
00045 return err;
00046 }
00047
00048 int
00049 Bluetooth::hci_open_dev(int dev_id, const char* log)
00050 {
00051 int fd = ::hci_open_dev(dev_id);
00052 if(log)
00053 {
00054 logf(log, LOG_DEBUG, "hci_open_dev(hci%d): fd %d",dev_id,fd);
00055 }
00056 return fd;
00057 }
00058
00059 int
00060 Bluetooth::hci_close_dev(int dd, const char* log)
00061 {
00062 int err = ::hci_close_dev(dd);
00063 if(log)
00064 {
00065 logf(log, LOG_DEBUG, "hci_close_dev(%d): err %d",dd,err);
00066 }
00067 return err;
00068 }
00069
00070 int
00071 Bluetooth::hci_read_remote_name(int dd, const bdaddr_t *bdaddr, int len,
00072 char *name, int to, const char* log)
00073 {
00074 int err = ::hci_read_remote_name(dd,bdaddr,len,name,to);
00075 if(log)
00076 {
00077 bdaddr_t ba;
00078 baswap(&ba,bdaddr);
00079 char buff[18];
00080 logf(log, LOG_DEBUG,
00081 "hci_read_remote_name(%d): [%s] %s len %d to %d",
00082 dd,batostr(&ba,buff),name,len,to);
00083 }
00084 return err;
00085 }
00086
00087 void
00088 Bluetooth::hci_get_bdaddr(const char * hcidev, bdaddr_t* bdaddr,
00089 const char * log)
00090 {
00091 struct hci_dev_info di;
00092
00093
00094 int fd=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
00095 if (fd<0)
00096 {
00097 if (log) logf(log, LOG_ERR, "can't open HCI socket");
00098 return;
00099 }
00100 int dev_id = hci_devid(hcidev);
00101 if (dev_id < 0)
00102 {
00103 if (log)
00104 logf(log, LOG_DEBUG,
00105 "bad device id -- attempting to up the interface");
00106 if (hci_dev_up(fd,hcidev,log) < 0)
00107 {
00108 if (log)
00109 logf(log, LOG_ERR, "unable to change device status: %s",
00110 hcidev);
00111 return;
00112 }
00113 if ((dev_id = hci_devid(hcidev)) < 0) {
00114 return;
00115 }
00116 }
00117 di.dev_id = dev_id;
00118 if (ioctl(fd, HCIGETDEVINFO, (void *) &di) < 0)
00119 {
00120 if(log) logf(log, LOG_ERR, "can't get device info");
00121 return;
00122 }
00123 bacpy(bdaddr,&di.bdaddr);
00124 close(fd);
00125 }
00126
00127 int
00128 Bluetooth::hci_dev_up(int dd, const char * hcidev, const char *log)
00129 {
00130 int dev_id=-1;
00131 if (strncmp(hcidev,"hci",3) == 0 && strlen(hcidev) >= 4)
00132 {
00133 dev_id = atoi(hcidev+3);
00134 }
00135 if (dev_id<0)
00136 {
00137 if (log) logf(log, LOG_ERR, "badly formatted HCI device name: %s",hcidev);
00138 return -1;
00139 }
00140 if (ioctl(dd, HCIDEVUP, dev_id) < 0)
00141 {
00142 if (log) logf(log, LOG_ERR, "failed to init device hci%d: %s (%d)",
00143 dev_id, strerror(errno), errno);
00144 return -1;
00145 }
00146 return 0;
00147 }
00148
00149
00150 char *
00151 Bluetooth::batostr(const bdaddr_t* ba, char *str, size_t str_size)
00152 {
00153 if (!str)
00154 return NULL;
00155
00156 memset(str,0,str_size);
00157
00158 snprintf(str, str_size, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
00159 ba->b[5], ba->b[4], ba->b[3],
00160 ba->b[2], ba->b[1], ba->b[0]);
00161
00162 return str;
00163 }
00164
00165
00166 bdaddr_t *
00167 Bluetooth::strtoba(const char *str, bdaddr_t *addr)
00168 {
00169 const char *ptr = str;
00170 int i;
00171
00172 if (!addr)
00173 return NULL;
00174 bdaddr_t bd;
00175 uint8_t *ba = (uint8_t*) &bd;
00176
00177 for(i = 0; i < 6; i++) {
00178 ba[i] = (uint8_t) strtol(ptr, NULL, 16);
00179 if (i != 5 && !(ptr = strchr(ptr,':')))
00180 ptr = ":00:00:00:00:00";
00181 ptr++;
00182 }
00183
00184 baswap(addr,(bdaddr_t *)ba);
00185 return addr;
00186 }
00187
00188 void
00189 Bluetooth::baswap(bdaddr_t *dst, const bdaddr_t *src)
00190 {
00191 unsigned char *d = (unsigned char *) dst;
00192 const unsigned char *s = (const unsigned char *) src;
00193 int i;
00194 for (i = 0; i < 6; i++)
00195 d[i] = s[5-i];
00196 }
00197
00198
00199 }
00200 #endif