-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathNetworkDiscovery.hh
95 lines (76 loc) · 2.64 KB
/
NetworkDiscovery.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef _NETWORK_DISCOVERY_HH_
#define _NETWORK_DISCOVERY_HH_
/*
Copyright (C) 2016-2017 Omron Adept Technologies
This is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "Aria.h"
#include "ariaUtil.h"
#include "ArSocket.h"
#include "ArASyncTask.h"
/** This object listens for UDP messages on port 30718, and responds
with some identifiying information to certain requests. This allows
clients to search the local network for instances of MobileSim by
sending broadcast messages to the network. For client-side implementation
see DiscoverWiBox.py in ARIA Python code.
Discovery requests must consist of four bytes with value 0xf6 (0, 0, 0, 0xf6).
The response packet will be 30 null bytes except byte 3 will be 0xF7, byte
8 will be 0xFA and byte 9 will be OxFB. Byte 3 being 0xF7 identifies this
as a valid response to a request of 0xF6, and 0xFA,0xFB identifies it as
MobileSim responding.
See <http://wiki.lantronix.com/developer/Lantronix_Discovery_Protocol>.
*/
class NetworkDiscoveryResponder : public virtual ArASyncTask
{
public:
NetworkDiscoveryResponder(int _port = 30718) : port (_port)
{
}
int getPort() const { return port; }
private:
int port;
ArSocket socket;
void check()
{
char request[4];
sockaddr_in sin;
while(socket.recvFrom(request, 4, &sin) >= 4)
{
//printf("got data %x %x %x %x, want 3 to be %x\n", request[0], request[1], request[2], request[3], (int)0xf6);
if(request[3] == (char)0xf6)
{
//puts("got 0xf6, sending response");
char response[30];
memset(response, 0, 30);
response[3] = 0xF7;
response[8] = 0xFA;
response[9] = 0xFB;
socket.sendTo(response, 30, &sin);
}
ArUtil::sleep(300);
}
}
virtual void *runThread(void *)
{
socket.open(port, ArSocket::UDP);
socket.setNonBlock();
while(getRunning())
{
check();
ArUtil::sleep(2000);
}
socket.close();
return 0;
}
};
#endif