Skip to content

Commit

Permalink
coded use existing bridge option, more detailed readme, added install…
Browse files Browse the repository at this point in the history
…_autostart script
  • Loading branch information
Kasidit Yusuf committed Jan 13, 2014
1 parent e706d01 commit cbbbef0
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 74 deletions.
39 changes: 39 additions & 0 deletions README.markdown
Expand Up @@ -106,3 +106,42 @@ Simply run:
If it shows "Successfully setup auto-start-on-boot for edl_main" then you're done - reboot your pi and test it! For example run:
<pre>sudo shutdown -r now</pre>

NOTE: The default/generated autostart script would not contain any custom parameters. Therefore, if you want to specify --interface or the recommended --use_existing_bridge part as in next section, so please edit and add your desired parameters in the file /etc/init.d/ecodroidlink (sudo nano /etc/init.d/ecodroidlink) - the line which calls edl_main in the line under "start)" - make sure the trailing & is still there.

Recommended: Manually create your own bridge for best connection reliability
--------------------------------------------

By default, the edl_main would automatically create a bridge over eth0 as already descibed above. However, this is done only once on startup, problems could arise if the ethernet cable gets temporarily disconnected and so forth - the edl_main auto-bridging doesn't check and re-bridge in such cases yet - the auto-bridging is intended for easy testing purposes but not permanet use.

It is advisable to create and use your own 'bridge' (in /etc/network/interfaces) because you can fully customize the bridge as you like (static ip for your Computer/Pi, etc). So, just create a bridge (like 'br0') containing your desired interface (like 'eth0') in this file: sudo nano /etc/network/interfaces - let's first remove the 'auto eth0' line

For example, make it:
auto lo
iface lo inet loopback

auto br0
iface br0 inet dhcp
bridge_ports eth0

For more detailed info, please see <http://www.hkepc.com/forum/viewthread.php?tid=1710030> - the part about "The modified /etc/network/interfaces file".

- For static ip examples please see <https://wiki.debian.org/NetworkConfiguration#Bridging> - for most computers which have eth0 only - make sure you remove the eth1 from the bridge_ports.

- Make sure you removed do not have a line to setup dhcp for your interface (eth0) otherwise it could cause strange behavior - only set ip/dhcp for the bridge as explained in <http://www.linuxfoundation.org/collaborate/workgroups/networking/bridge#Creating_a_bridge_device>.

- Restart your computer (sudo shutdown -r now), make sure internet works, then specify the bridge's name (like 'br0') - for example:
sudo ./edl_main --use_existing_bridge br0

License
-------

EcoDroidLink is free-software, licensed under GNU GPL, same as BlueZ. Please see the COPYING file in this same folder for full info.

Special Thanks
-------------

- Thanks to the user 'howdy' in <http://www.hkepc.com/forum/viewthread.php?tid=1710030> for posting a detailed tutorial for setting nap on bluez.

- Thanks to [BlueZ](http://www.bluez.org)

- Thanks to Raspberry Pi forum users: Basil_R, Douglas6, maxwed, and all others providing great input/suggestions in <http://www.raspberrypi.org/phpBB3/viewtopic.php?f=36&t=57529>.
47 changes: 46 additions & 1 deletion edl_main
Expand Up @@ -14,9 +14,54 @@ __status__ = "Production"
#header format from http://stackoverflow.com/questions/1523427/python-what-is-the-common-header-format

import edl_util
from edl_util import printlog
import singleton
from optparse import OptionParser, make_option

#http://stackoverflow.com/questions/380870/python-single-instance-of-program
me = singleton.SingleInstance() # will sys.exit(-1) if other instance is running

edl_util.main_loop()
usage = """EcoDroidLink v1.20 - BlueZ Bluetooth Network Access Point Automation
Please see README.markdown (at https://github.com/ykasidit/ecodroidlink) for full purpose, setup and usage.
Usage:
- [Default]: By default, edl_main would try to create a DHCP bridge over eth0, then use that bridge to share the internet to bluetooth clients. So just run:
sudo ./edl_main
However, if your main dhcp internet source is not eth0 (For example 'usb0' for usb-tethering internet sources) - you can specify it like below example for usb0 :
sudo ./edl_main --interface usb0
- [Recommended]: Please manually create a 'bridge' (in /etc/network/interfaces - you can fully customize the bridge as you like - static ip, etc) containing your desired interface (like 'eth0'). Make sure the same interface is not set out of the bridge. Restart your computer, make sure internet works, then specify the bridge's name (like 'br0') - for example:
sudo ./edl_main --use_existing_bridge br0
"""

use_existing_bridge = None
#if use_existing_bridge was not specified in option then create our own dhcp bridge, over eth0 by default
src_interface = "eth0"

##### override this like 'sudo ./edl_main --interface eth1' (for usb tethering sources it would be usb0)

option_list = [
make_option("--interface", action="store",
type="string", dest="interface"),
make_option("--use_existing_bridge", action="store",
type="string", dest="existing_bridge")
]

parser = OptionParser(usage=usage,option_list=option_list)

(options, args) = parser.parse_args()

if (options.existing_bridge is not None):
use_existing_bridge = options.existing_bridge
printlog("edl: using existing bridge: "+use_existing_bridge)
else:
use_existing_bridge = None
if (options.interface is not None):
printlog("edl: auto-create bridge (dhcp) over network interface "+options.interface)
src_interface = options.interface
else:
printlog("edl: auto-create bridge (dhcp) over default interface eth0 - you can customize like 'sudo ./edl_main --interface eth1' or usb0 or whatever is your internet souce. NOTE: It is recommended to create your own bridge in /etc/network/interfaces and specify like 'sudo ./edl_main --use_existing_bridge br0' for real deployment in auto-start-on-boot mode. Please see README.markdown for full info.")

edl_util.main_loop(use_existing_bridge,src_interface)
104 changes: 31 additions & 73 deletions edl_util.py
Expand Up @@ -16,7 +16,6 @@
from subprocess import call
import subprocess
import os
from optparse import OptionParser, make_option

import logging
import logging.handlers
Expand All @@ -30,48 +29,7 @@ def printlog(s):
logger.info(s)
print(s)

####################3
src_interface = "eth0"
##### override this like 'sudo ./edl_main --interface eth1' (for usb tethering sources it would be usb0)

#TODO - extact the current setting from the current interface and use that automatically - if it's static then use its current ip
############### default is dhcp - to use static provide command-line options like 'sudo ./edl_main --ip 192.168.1.33 --mask 255.255.255.0'
src_interface_ip_mode_dhcp = True
src_interface_ifconfig_ip = ""
src_interface_ifconfig_subnet_mask = ""

option_list = [
make_option("--interface", action="store",
type="string", dest="interface"),
make_option("--ip", action="store",
type="string", dest="ip"),
make_option("--mask", action="store",
type="string", dest="mask"),
]

parser = OptionParser(option_list=option_list)

(options, args) = parser.parse_args()

if (options.interface is not None):
printlog("edl: using network interface "+options.interface)
src_interface = options.interface
else:
printlog("edl: using default interface eth0 - you can customize like 'sudo ./edl_main --interface eth1' or usb0 or whatever is your internet souce")

if (options.ip is not None):
src_interface_ifconfig_ip = options.ip
src_interface_ifconfig_subnet_mask = options.mask
if (options.mask is None):
printlog("edl: ip supplied but mask not support - please supply --mask option")
exit(1)
printlog("edl: using static ip for bridge: "+options.ip+" subnet_mask: "+options.mask)
src_interface_ip_mode_dhcp = False
#TODO - try implementing static ip in the future - for now support only dhcp...
printlog("edl: static ip is NOT supported yet... you can try directly edit this python source code do to ifconfig instead of dhclient... and dont supply these ip params")
exit(2)
else:
printlog("edl: using dhcp")
####################

def edl_call(cmd,dbg_header):
printlog(dbg_header+": Attempt call: "+cmd);
Expand Down Expand Up @@ -140,7 +98,7 @@ def watch_agent_and_nap_process(agent_process,nap_process):
return -3
#end of watcher def

def main_loop():
def main_loop(use_existing_bridge,src_interface):
while (1):
printlog ("edl: EcoDroidLink initialzing/cleaning processes and adapter state...")
edl_deinit()
Expand All @@ -160,42 +118,42 @@ def main_loop():

printlog ("edl: bluetooth adapter ready")

########### prepare new bridge between eth0 (or other interface as specified in option like 'sudo ./edl_main --interface eth1'

ret = edl_call("sudo ifconfig "+src_interface,"edl_bridge_init");
if (ret != 0):
printlog("edl: CRITICAL source interface probably doesn't exist: "+src_interface+" - failed to get initial info of interface...")
break;

edl_call("sudo ifconfig "+src_interface+" 0.0.0.0 0.0.0.0","edl_bridge_init");
edl_call("sudo ifconfig edl_br0 down","edl_bridge_init")
edl_call("sudo brctl delbr edl_br0","edl_bridge_init")

ret = edl_call("sudo brctl addbr edl_br0","edl_bridge_init")
if (ret != 0):
printlog("edl: CRITICAL create bridge edl_br0 failed!")
break;

ret = edl_call("sudo brctl addif edl_br0 "+src_interface,"edl_bridge_init")
if (ret != 0):
printlog("edl: CRITICAL create bridge edl_br0 failed!")
break;

edl_call("sudo ifconfig edl_br0 0.0.0.0 0.0.0.0","edl_bridge_init")

ret = edl_call("sudo dhclient edl_br0","edl_bridge_init")
if (ret != 0):
printlog("edl: CRITICAL set DHCP for newly created bridge failed!")
break;
bridge_to_use = 'edl_br0'

# if use_existing_bridge was not specified then make our own bridge... ########### prepare new bridge between eth0 (or other interface as specified in option)
if use_existing_bridge is None:
printlog("edl: creating a bridge with DHCP over "+src_interface)
ret = edl_call("sudo ifconfig "+src_interface,"edl_bridge_init");
if (ret != 0):
printlog("edl: CRITICAL source interface probably doesn't exist: "+src_interface+" - failed to get initial info of interface...")
break;
edl_call("sudo ifconfig "+src_interface+" 0.0.0.0 0.0.0.0","edl_bridge_init");
edl_call("sudo ifconfig edl_br0 down","edl_bridge_init")
edl_call("sudo brctl delbr edl_br0","edl_bridge_init")
ret = edl_call("sudo brctl addbr edl_br0","edl_bridge_init")
if (ret != 0):
printlog("edl: CRITICAL create bridge edl_br0 failed!")
break;
ret = edl_call("sudo brctl addif edl_br0 "+src_interface,"edl_bridge_init")
if (ret != 0):
printlog("edl: CRITICAL create bridge edl_br0 failed!")
break;
edl_call("sudo ifconfig edl_br0 0.0.0.0 0.0.0.0","edl_bridge_init")
ret = edl_call("sudo dhclient edl_br0 ","edl_bridge_init")
if (ret != 0):
printlog("edl: CRITICAL set DHCP for newly created bridge failed!")
break;
else:
bridge_to_use = use_existing_bridge

#get path to local module - since edl_nap and edl_agent are in the same folder
encoding = sys.getfilesystemencoding()
this_path = os.path.dirname(unicode(__file__, encoding))

#start new NAP process - start this before the agent so the sdp profile would be there before users come to pair and discover services...

printlog('edl: path_to_execute agent and nap: '+this_path)
nap_process = subprocess.Popen(this_path+'/edl_nap edl_br0', shell=True)
printlog('edl: path_to_execute agent and nap on bridge: '+this_path)
nap_process = subprocess.Popen(this_path+'/edl_nap '+bridge_to_use, shell=True)

time.sleep(5)

Expand Down

0 comments on commit cbbbef0

Please sign in to comment.