Magic Packet(wake on lan)
Topic last updated
05 Jan 2013, by
Digonto Rahman.
6 replies
wakeup
Does anyone has a sample Wake-on-lan code to wake up a PC by mbed. or any help on how can I send the packet(magic packet through ethernet).
Replies
Well, the discussions give only some hints, but not a complete solution. So, I try to reopen this thread:
Here, some of code we are using, but something is not goog enough... PC does not wake up.
If someone can see where it is not working, thank by advance.
<p$1$2$3$4$5$6>
#include "mbed.h"
#include <string.h>
Ethernet eth;
int main() {
char buf[102];
char MAC[6];
// MAC Adress 00:30:64:08:82:30
MAC[0]=0x00;
MAC[1]=0x30;
MAC[2]=0x64;
MAC[3]=0x08;
MAC[4]=0x82;
MAC[5]=0x30;
for (int i=0;i<6;i++) {
buf[i]=0xff;
}
// Add 16 times the MAC Adress
for (int j=1;j<17;j++) {
buf[0+j*6]=MAC[0];
buf[1+j*6]=MAC[1];
buf[2+j*6]=MAC[2];
buf[3+j*6]=MAC[3];
buf[4+j*6]=MAC[4];
buf[5+j*6]=MAC[5];
}
// 102 Bytes to send
int ok_eth = 0;
while(1) {
eth.write(buf, sizeof(buf)); // Generate an outgoing ethernet packet
wait(1);
ok_eth = eth.send(); // Send the magic packet
wait(1);
}
}
I think the first six bytes should be 0xFF, not the MAC address?
Indeed, the first six bytes are 0xFF.
It's just because, I have defined the MAC address first.
But, to construct the packet, I use the 6 times 0xFF first (buf[0] to buf[5]
and then I add 16 times the MAC Address. buf[6] to buf [102]
<p$1$2$3$4$5$6>
I have got some questionning during my reading:
- Most program, I have found are using unsigned char instead of char. But, ethernet.send() only use char...
- Some code are using a specific port (7 or 9 or 40000), But, ethernet.send() does not define a port...
- Some code specify that UDP packet are needed, But, ethernet.send() does not define UDP or TCP..
SO, I am stuck
Wikipedia says:
Quote:
Since the magic packet is only scanned for the string above, and not actually parsed by a full protocol stack, it may be sent as any network- and transport-layer protocol. It is typically sent as a UDP datagram to port 7 or 9, but actually it can be sent on any port.
So you can send it with an UDP socket, or directly as Ethernet packet. The latter one is what ethernet.send does, thats why you cannot specify UDP or TCP, ethernet packages are 2 levels below TCP / UDP...
#include "mbed.h"
#include "EthernetNetIf.h"
#include "UDPSocket.h"
EthernetNetIf eth;
int main() {
int j=0;
UDPSocket udp;
char msg[102];
EthernetErr ethErr = eth.setup();
if(ethErr) {
printf("Error %d in setup.\n", ethErr);
return -1;
}
Host host(IpAddr(192,168,0,255), 2304, NULL);
udp.bind(host);
for ( j=0; j<6 ; j++ ) msg[j] = 0xff;
for ( j=1; j<17 ; j++ ) { //MAC address
msg[j*6+0] = 0x00;msg[j*6+1] = 0x11;msg[j*6+2] = 0x22;
msg[j*6+3] = 0x33;msg[j*6+4] = 0x44;msg[j*6+5] = 0x55;
}
udp.sendto( msg, 102, &host );
return 0;
}
Please log in to post a reply.
Does anyone has a sample Wake-on-lan code to wake up a PC by mbed. or any help on how can I send the packet(magic packet through ethernet).