00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 # include <dtn-config.h>
00019 #endif
00020
00021 #include <climits>
00022
00023 #ifdef OASYS_BLUETOOTH_ENABLED
00024
00025 #include <oasys/util/Random.h>
00026 #include <oasys/bluez/Bluetooth.h>
00027 #include <oasys/bluez/BluetoothSDP.h>
00028 #include <oasys/bluez/BluetoothInquiry.h>
00029 #include <oasys/util/OptParser.h>
00030 #include "bundling/BundleDaemon.h"
00031
00032 #include "BluetoothAnnounce.h"
00033 #include "BluetoothDiscovery.h"
00034
00035 namespace dtn {
00036
00037 void
00038 BluetoothDiscovery::shutdown()
00039 {
00040 shutdown_ = true;
00041 notifier_.notify();
00042 }
00043
00044 BluetoothDiscovery::BluetoothDiscovery(const std::string& name)
00045 : Discovery(name,"bt"),
00046 oasys::Thread("BluetoothDiscovery"),
00047 notifier_("/dtn/discovery/bt")
00048 {
00049 oasys::Bluetooth::hci_get_bdaddr(&local_addr_);
00050 shutdown_ = false;
00051 }
00052
00053 bool
00054 BluetoothDiscovery::configure(int argc, const char* argv[])
00055 {
00056 if (oasys::Thread::started())
00057 {
00058 log_warn("reconfiguration of BluetoothDiscovery not supported");
00059 return false;
00060 }
00061
00062 oasys::OptParser p;
00063 const char* invalid;
00064 p.addopt(new oasys::BdAddrOpt("local_addr",&local_addr_));
00065 if (! p.parse(argc,argv,&invalid))
00066 {
00067 log_err("bad option: %s",invalid);
00068 return false;
00069 }
00070
00071 local_.assign(bd2str(local_addr_));
00072
00073 start();
00074 return true;
00075 }
00076
00077 void
00078 BluetoothDiscovery::run()
00079 {
00080 oasys::BluetoothInquiry inquiry;
00081 while (! shutdown_)
00082 {
00083 u_int interval = INT_MAX;
00084
00085 for (iterator i = list_.begin(); i != list_.end(); i++)
00086 {
00087 BluetoothAnnounce* announce = dynamic_cast<BluetoothAnnounce*>(*i);
00088 if (announce->interval_remaining() < interval)
00089 {
00090 interval = announce->interval_remaining();
00091 }
00092 }
00093
00094
00095
00096
00097
00098
00099
00100 if (interval > 0)
00101 {
00102 u_int sleep_time = oasys::Random::rand(interval);
00103 log_debug("sleep time %d",sleep_time);
00104
00105 notifier_.wait(NULL,sleep_time);
00106 }
00107
00108 if (shutdown_) break;
00109
00110
00111
00112 int nr = inquiry.inquire();
00113
00114 if (shutdown_) break;
00115
00116
00117
00118 for (iterator i = list_.begin(); i != list_.end(); i++)
00119 {
00120 BluetoothAnnounce* announce = dynamic_cast<BluetoothAnnounce*>(*i);
00121 announce->reset_interval();
00122 }
00123
00124 if (nr < 0) continue;
00125
00126
00127 bdaddr_t remote;
00128 while (inquiry.next(remote) != -1)
00129 {
00130
00131
00132 oasys::BluetoothServiceDiscoveryClient sdpclient;
00133 if (sdpclient.is_dtn_router(remote))
00134 {
00135 std::string nexthop(bd2str(remote));
00136 EndpointID eid = sdpclient.remote_eid();
00137 log_info("discovered DTN peer %s at %s channel %d",eid.c_str(),
00138 nexthop.c_str(),sdpclient.channel());
00139 handle_neighbor_discovered("bt",nexthop,eid);
00140 }
00141
00142 if (shutdown_) break;
00143 }
00144 }
00145 }
00146
00147 }
00148
00149 #endif // OASYS_BLUETOOTH_ENABLED