Would you like to start XBMC remotely?

  Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
dieharder Offline
Junior Member
Posts: 6
Joined: May 2011
Reputation: 0
Location: UK
Question  Would you like to start XBMC remotely? Post: #1
Hi,

This is my first post, so please go easy on me ;-)

I've been using XBMC for a while now but recently I've been remotely controlling it using the superb XBMC Commander iPad app (made by grywnn - see this forum thread for details).

I'm posting this thread to see if there's appetite for anyone wanting to start XBMC either using a small local webserver or using Wake On LAN (with the PC already switched on).

Background
I don't have a dedicated HTPC, but have my XBMC shortcut set to switch monitors to my TV (using UltraMon as it happens)... now, in my pursuit of perfect laziness I decided to see if I could use something on my iPhone/iPad to startup XBMC, switch to my TV - even better if the solution could be made to work from within my new best friend, XBMC Commander.

I started by getting getting a solution together which would fire up XBMC when you hit a simple URL directed at the PC on which XBMC runs (e.g. http://192.168.0.2/startxbmc) ... this worked fine and had the added advantage of being usable by any device with a browser on your LAN.

I was delighted to discover that XBMC Commander has the facility on it's Remote screen to add a custom menu item which will fire a HTTP call wherever you want. The solution worked, but it turns out that XBMC Commander doesn't show that menu when it can't connect to the XBMC server, which is exactly when you need it - D'oh!

I contacted grywnn to see if that could be changed in a future release and to his credit, he replied back straight away and kindly agreed to add it in (possibly the release after next). Thanks grywnn!

Anyway, not only am I lazy, I'm also impatient ... so I moved onto a second solution... I noticed that XBMC Commander has the option to use WOL (Wake on LAN)... I figured that I could write something to sniff packets and look out for the WOL Magic Packet, and when it got one, fire up XBMC.

I'm pleased to say it works great :-) I no longer have to lift my backside off the sofa when I want to watch something, and my wife is almost impressed with something technical I've done Big Grin

Technical
I run XBMC on Vista and the script which fires up XBMC and changes to the TV is written in Perl (the monitor changing bit is actually done through an UltraMon shortcut).

I originally made it work using the TinyWeb webserver - any webserver which allows CGI execution will be fine (Apache on Linux for e.g.) and I could happily get it working on Linux if there was enough interest.

The packet-sniffing solution is also written in Perl (both scripts are very small - 20 lines-ish). Both Perl scripts could be made into executables for those who don't want to install ActiveSync and set up the required modules.

I could also get the scripts take parameters (MAC address, NIC device ID, path to start up XBMC etc.)

Interested?
So, if there's appetite in the community, I'd be happy to parameterise and share the scripts/solutions and even port them over to Linux (my preferred platform, but the XBMC box runs Windows so I wrote them for that) - depends on how much interest there is.

Simply reply to this thread if this is something you'd like me to formalise and share ... thanks!
find quote
dibsta Offline
Senior Member
Posts: 154
Joined: Apr 2009
Reputation: 0
Post: #2
Hi dieharder

I would be very interested in your WOL feature
could you please let me know how this is done

Cheers dibsta
find quote
x23piracy Offline
Senior Member
Posts: 159
Joined: Feb 2011
Reputation: 0
Location: Germany
Post: #3
Hi,

dibsta Wrote:I would be very interested in your WOL feature

same interest here Wink


Greetz X23
find quote
dibsta Offline
Senior Member
Posts: 154
Joined: Apr 2009
Reputation: 0
Post: #4
Bump
find quote
dieharder Offline
Junior Member
Posts: 6
Joined: May 2011
Reputation: 0
Location: UK
Post: #5
OK, so not the deluge of interest I thought ;-) So, I won't go to the effort of paramaterising, porting to Linux, compiling binaries etc. but I'm more than happy to share the code and help you guys implement it as best I can...

To do this you'll need to have Perl installed (see ActivePerl for the standard, and free, install on Windows).

There are two bits to the code ... the packet sniffer and the thing which kicks the XBMC process off. For the packet sniffer, you need to have the Net::Pcap perl module installed (using cpan or ppm for e.g.) and also install winpcap. For checking/kicking off the XBMC process, the Win32::Process and Win32::Process::List perl modues are required.

Once you've got all that going, open your text editor and pop the following code into it (save it as something like XBMC_WOL_Sniffer.pl)...

Code:
#!/usr/bin/perl

use strict;
use Net::Pcap;
use Win32::Process::List;
use Win32::Process;

$| = 1;
sub f_probe_pcapinit;
sub f_probe_read80211b_func;
sub f_probe_ctrl_c;

my $g_pcap_err     = '';
my $g_sniff_device = '\Device\NPF_{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}';
my $g_cap_descrip;

$SIG{INT} = \&f_probe_ctrl_c;

f_probe_pcapinit;

sub f_probe_pcapinit {
    if ( $g_cap_descrip = Net::Pcap::open_live( $g_sniff_device, 2000, 0, 1000, \$g_pcap_err ) ) {
        # Initiate endless packet gathering.
        Net::Pcap::loop( $g_cap_descrip, -1, \&f_probe_read80211b_func, '' );
      }
    else {
        print "\nCould not initiate the open_live command on $g_sniff_device from pcap.\nThe following error was reported: $g_pcap_err\n";
        exit;
      }
  }

sub f_probe_read80211b_func {
    my ( $data, $header, $packet ) = @_;
    my $packet_content = unpack( 'H*', $packet );
    #print "'$packet_content'\n";
    if ( $packet_content =~ /ffffffffffff00112233445500112233445500112233445500112233445500112233445500112233​44550011223344550011223344550011223344550011223344550011223344550011223344550011​22334455001122334455001122334455001122334455/g ) {
        #print "Found the Magic Packet :-)\n";
        start_xbmc();
      }
  }

sub f_probe_ctrl_c {
    # Checks if there is a open pcap handle and closes it first.
    if ($g_cap_descrip) {
        Net::Pcap::close($g_cap_descrip);
        print "\nClosed the pcap handle, program will now exit.\n";
      }
  }

sub start_xbmc() {
    my $xbmc;
    my $P    = Win32::Process::List->new();
    my %list = $P->GetProcesses();
    foreach my $key ( keys %list ) {
        next unless $list{$key} eq "XBMC.exe";
        # $list{$key} is now the process name and $key is the PID
        #print sprintf( "%30s has PID %15s", $list{$key}, $key ) . "\n";
        $xbmc = $key;
      }

    if ($xbmc) {
        print "XBMC is already open. No action taken.\n";
        #Win32::Process::KillProcess( $xbmc, 0 );
      }
    else {
        print "XBMC is not yet open. Starting...\n";

        my $app       = "C:\\Program Files\\UltraMon\\UltraMonShortcuts.exe";
        my $cmd       = "UltraMonShortcuts.exe /l C:\\ProgramData\\Realtime Soft\\UltraMon\\ShellShortcuts\\XBMC (3).umshortcut";
        my $b_inherit = 0;
        my $flags     = "CREATE_NEW_PROCESS || DETACHED_PROCESS";
        my $dir       = '.';

        my $result = Win32::Process::Create( my $process, $app, $cmd, $b_inherit, $flags, $dir );
        if ($result) {
            print "$cmd has been created with process id of $result\n";
          }
        else {
            print "unable to start $cmd\n";
            print "error : " . Win32::FormatMessage( Win32::GetLastError() );
          }
      }

  }

OK, so the things you'll now need to change in the code are:

Code:
my $g_sniff_device = '\Device\NPF_{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}';
Here you'll need to find the DeviceID of your NIC (which, of course, must support WOL and have it turned on etc.) - you can get this from the Windows registry (hard to find but here's a blog post on how do find it).

Code:
if ( $packet_content =~ /ffffffffffff00112233445500112233445500112233445500112233445500112233445500112233​44550011223344550011223344550011223344550011223344550011223344550011223344550011​22334455001122334455001122334455001122334455/g ) {
This looks weird ... but it's the format of the WOL magic packet ... find the MAC address of your NIC (e.g. using ipconfig) and replace 001122334455 with it (16 times!)

Code:
my $app       = "C:\\Program Files\\UltraMon\\UltraMonShortcuts.exe";
        my $cmd       = "UltraMonShortcuts.exe /l C:\\ProgramData\\Realtime Soft\\UltraMon\\ShellShortcuts\\XBMC (3).umshortcut";
These are the paths to your XBMC executable - in my case I start it with an UltraMon shortcut (to switch monitors to my TV). Replace it with the path to your executable ($app is the path, including exe, and $cmd is the exe plus any parameters to throw at it).

So, how test it?

First, uncomment a couple of print statements (remove the #):
Code:
#print "'$packet_content'\n";
and
Code:
#print "Found the Magic Packet :-)\n";

Fire up a cmd.exe window and change to the dir you saved the code in then
Code:
perl XBMC_WOL_Sniffer.pl

You should see some activity as packets hitting your LAN are displayed.

Now, open XBMC Commander (or anything else that will broadcast a WOL packet to your LAN) and hopefully when you hit the wake button, you'll see the output indicate it's worked, and XBMC will open on up...

If it all works, then put the #'s back to comment out the debug statements and to make it run nicely in the background use "wperl path_to_XBMC_WOL_Sniffer.pl" (you can of course make a shortcut and put that in Startup).

Hopefully that'll get you going :-)

Let me know if you run into any issues and I'll do my best to help...
find quote
dibsta Offline
Senior Member
Posts: 154
Joined: Apr 2009
Reputation: 0
Post: #6
Hi dieharder

just to let you know i have not got a clue ithought it would be a simple script

also i dont use ultramon

Cheers dibsta
find quote
dieharder Offline
Junior Member
Posts: 6
Joined: May 2011
Reputation: 0
Location: UK
Post: #7
dibsta Wrote:ithought it would be a simple script
It is a simple script ;-) But everything is relative I guess Laugh

dibsta Wrote:also i dont use ultramon
That doesn't matter - you would just replace the ultramon.exe path with the path to your XBMC.exe

Sorry it isn't going to help you ... hopefully someone else will get some joy out if it.
find quote
Scidd0w Offline
Junior Member
Posts: 25
Joined: Dec 2010
Reputation: 0
Post: #8
dieharder;

I would also be interested in a windows executable of the WOL solution.
I know the interest is maybe not enough for the effort you have to put into it, but I can always hope this will change in time.. Tongue ;o)
find quote
castortray Offline
Fan
Posts: 503
Joined: May 2009
Reputation: 0
Post: #9
Scidd0w Wrote:dieharder;

I would also be interested in a windows executable of the WOL solution.
I know the interest is maybe not enough for the effort you have to put into it, but I can always hope this will change in time.. Tongue ;o)

maybe this thread help you:

http://forum.xbmc.org/showthread.php?tid...hlight=wol
find quote
dieharder Offline
Junior Member
Posts: 6
Joined: May 2011
Reputation: 0
Location: UK
Post: #10
castortray Wrote:maybe this thread help you:

http://forum.xbmc.org/showthread.php?tid...hlight=wol

Except that the solution we're talking about here in this thread isn't about sending WOL packets from XBMC, it's about receiving WOL packets to a machine which is already awake and then starting XBMC up...
find quote
Post Reply