In line 3, we read the next separated string, which contains the actual path.
If you were to pass the request
GET http://192.168.2.42/SAMSUNG/32/E0E040BF HTTP/1.1
to
handle_command
,
path
would contain
/SAMSUNG/32/E0E040BF
now.
At this stage, we have a string consisting of three strings separated by a slash
character (/). It’s time to use
strsep
again, and if you understand what happens
in lines 6 to 9, then you can call yourself familiar with both C and the
strsep
function. In the end, the array
args
contains all three path elements. We can
pass the protocol name directly to
send_ir_data
, but we have to turn the bit
length and the value of the code into
int
and
long
values before. For the conver-
sion, we use the
atoi
and
strtoul
functions. We use the latter one to convert a
hexadecimal value to a decimal value.
Now we have defined all helper methods we need, and we only have to
implement the only public method of the
InfraredProxy
class:
RemoteControl/InfraredProxy/infrared_proxy.cpp
void InfraredProxy::receive_from_server(EthernetServer server) {
Line 1
const int MAX_LINE = 256;
-
char line[MAX_LINE];
-
EthernetClient client = server.available();
-
if (client) {
5
while (client.connected()) {
-
if (client.available()) {
-
read_line(client, line, MAX_LINE);
-
Serial.println(line);
-
if (line[0] == 'G' && line[1] == 'E' && line[2] == 'T')
10
handle_command(line);
-
if (!strcmp(line, "")) {
-
client.println("HTTP/1.1 200 OK\n");
-
break;
-
}
15
}
-
}
-
delay(1);
-
client.stop();
-
}
20
}
-
The
receive_from_server
method finally implements the core logic of our
InfraredProxy
class. It expects an instance of the
EthernetServer
class that is defined in the
Ethernet library. It waits for a client to connect using
EthernetServer
’s
available
method in line 4. Whenever the server is connected to a client, it checks
whether the client has new data using
EthernetClient
’s
available
method in line 7.
receive_from_server
reads the data sent by the client line by line, calling
read_line
.
It prints each line to the serial port for debugging purposes, and for every line
Chapter 12. Creating Your Own Universal Remote Control • 220
report erratum • discuss
www.it-ebooks.info