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/socket.h>
00010
00011 #include <bluetooth/bluetooth.h>
00012 #include <bluetooth/hci.h>
00013 #include <bluetooth/rfcomm.h>
00014
00015 #include "../debug/Log.h"
00016 #include "Bluetooth.h"
00017 #include "BluetoothInquiry.h"
00018
00019 namespace oasys {
00020
00021 BluetoothInquiry::BluetoothInquiry(const char * logbase)
00022 : Logger("BluetoothInquiry", logbase),
00023 hci_dev_(0),
00024 num_responses_(10),
00025 num_responses_i_(-1),
00026 length_(8),
00027 lap_(NULL),
00028 info_(NULL),
00029 flags_(0L),
00030 timeout_(100000),
00031 fd_(-1),
00032 pos_(0)
00033 {
00034 hci_device_name_ = NULL;
00035 set_hci_device_name("hci0");
00036 }
00037
00038 BluetoothInquiry::~BluetoothInquiry()
00039 {
00040 if(fd_ != -1)
00041 {
00042 close(fd_);
00043 }
00044
00045 if(info_)
00046 {
00047 free(info_);
00048 }
00049 delete [] hci_device_name_;
00050 }
00051
00052 char *
00053 BluetoothInquiry::hci_device_name()
00054 {
00055 return hci_device_name_;
00056 }
00057
00058 void
00059 BluetoothInquiry::set_hci_device_name(char *hci_device_name )
00060 {
00061 int sz = strlen(hci_device_name) + 1;
00062 if (hci_device_name_) {
00063 delete [] hci_device_name_;
00064 }
00065 hci_device_name_ = new char[sz];
00066 strncpy(hci_device_name_, hci_device_name, sz);
00067 hci_dev_ = Bluetooth::hci_devid(hci_device_name_,logpath_);
00068 }
00069
00070 int
00071 BluetoothInquiry::num_responses()
00072 {
00073 return num_responses_;
00074 }
00075
00076 void
00077 BluetoothInquiry::set_numresponses(int nr)
00078 {
00079 ASSERT( nr > 0 && nr < 250 );
00080 num_responses_ = nr;
00081 }
00082
00083 int
00084 BluetoothInquiry::length()
00085 {
00086 return length_;
00087 }
00088
00089 void
00090 BluetoothInquiry::set_length(int length)
00091 {
00092 ASSERT(length > 0 && length < 20);
00093 length_ = length;
00094 }
00095
00096 uint8_t *
00097 BluetoothInquiry::lap()
00098 {
00099 return lap_;
00100 }
00101
00102 void
00103 BluetoothInquiry::set_lap(uint8_t *lap )
00104 {
00105 lap_ = lap;
00106 }
00107
00108 inquiry_info *
00109 BluetoothInquiry::info()
00110 {
00111 return info_;
00112 }
00113
00114 void
00115 BluetoothInquiry::set_info(inquiry_info *info)
00116 {
00117 info_ = info;
00118 }
00119
00120 long
00121 BluetoothInquiry::flags()
00122 {
00123 return flags_;
00124 }
00125
00126 void
00127 BluetoothInquiry::set_flags(long flags)
00128 {
00129 flags_ = flags;
00130 }
00131
00132 int
00133 BluetoothInquiry::inquire()
00134 {
00135 if(info_)
00136 {
00137 free(info_);
00138 info_ = NULL;
00139 }
00140 num_responses_i_ = Bluetooth::hci_inquiry(
00141 hci_dev_,length_,num_responses_,
00142 lap_,&info_,flags_,logpath_);
00143 if( num_responses_i_ < 0 )
00144 {
00145 log_info("hci_inquiry found no devices in range");
00146 }
00147 return num_responses_i_;
00148 }
00149
00150 int
00151 BluetoothInquiry::timeout()
00152 {
00153 return timeout_;
00154 }
00155
00156 void
00157 BluetoothInquiry::set_timeout(int to)
00158 {
00159 timeout_ = to;
00160 }
00161
00162 void
00163 BluetoothInquiry::reset()
00164 {
00165 pos_ = 0;
00166 if( fd_ != -1 ) {
00167 close(fd_);
00168 fd_ = -1;
00169 }
00170 flags_ |= IREQ_CACHE_FLUSH;
00171 }
00172
00173 int
00174 BluetoothInquiry::first(BluetoothInquiryInfo& bii)
00175 {
00176 reset();
00177 return next(bii);
00178 }
00179
00180 int
00181 BluetoothInquiry::next(BluetoothInquiryInfo& bii)
00182 {
00183 char buff[18];
00184 (void)buff;
00185
00186 ASSERT( info_ != NULL );
00187 if(pos_ >= num_responses_i_)
00188 {
00189 return -1;
00190 }
00191 log_debug("BluetoothInquiry::next(%d)",pos_);
00192 if(fd_ == -1)
00193 {
00194 fd_ = Bluetooth::hci_open_dev(hci_dev_,logpath_);
00195 if(fd_ == -1)
00196 {
00197 log_err("hci_open_dev failed");
00198 return -1;
00199 }
00200 }
00201 memset(bii.name_,0,bii.name_len_);
00202 if(Bluetooth::hci_read_remote_name(fd_,&(info_ + pos_)->bdaddr,
00203 bii.name_len_,bii.name_,
00204 timeout_) < 0 )
00205 {
00206 strcpy(bii.name_,"[none]");
00207 }
00208 bacpy(&bii.addr_,&(info_ + pos_)->bdaddr);
00209 log_debug("read remote name %s(%s)",bii.name_,
00210 Bluetooth::batostr(&bii.addr_,buff));
00211 pos_++;
00212 return 0;
00213 }
00214
00215 }
00216 #endif