114 lines
2.0 KiB
C++
114 lines
2.0 KiB
C++
#include <Ethernet.h>
|
|
#include "MD5.h"
|
|
|
|
EthernetServer server(80);
|
|
|
|
void setup(void) {
|
|
pinMode(4, OUTPUT);
|
|
digitalWrite(4, HIGH);
|
|
Ethernet.init(10);
|
|
|
|
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x6F, 0x4A };
|
|
while (Ethernet.begin(mac) == 0) {
|
|
delay(1000);
|
|
}
|
|
|
|
server.begin();
|
|
}
|
|
|
|
|
|
void loop() {
|
|
EthernetClient client = server.accept();
|
|
|
|
if (client) {
|
|
byte cmd = client.read();
|
|
switch (cmd) {
|
|
case 1:
|
|
{
|
|
sendSMS(client);
|
|
break;
|
|
}
|
|
}
|
|
client.stop();
|
|
}
|
|
delay(15);
|
|
}
|
|
|
|
char* readWithSize(EthernetClient client, byte size) {
|
|
while (client.available() == 0) {
|
|
delay(250);
|
|
}
|
|
|
|
char* result = (char*) malloc(sizeof(char) * size+1);
|
|
for (int i = 0; i < size; i++) {
|
|
result[i] = client.read();
|
|
}
|
|
result[size] = '\0';
|
|
return result;
|
|
}
|
|
|
|
char* readStringUntil(EthernetClient client, char c) {
|
|
while (client.available() == 0) {
|
|
delay(250);
|
|
}
|
|
|
|
char last = '\0';
|
|
const byte size = 140;
|
|
char* result = (char*) malloc(sizeof(char) * size);
|
|
for (byte i = 0; i < size; i++) {
|
|
result[i] = ' ';
|
|
}
|
|
result[size] = '\0';
|
|
byte i = 0;
|
|
while (last != c && i < size) {
|
|
last = client.read();
|
|
if (last != c) {
|
|
result[i] = last;
|
|
i++;
|
|
}
|
|
}
|
|
result[i] = '\0';
|
|
return result;
|
|
}
|
|
|
|
void sendSMS(EthernetClient client) {
|
|
// get all from client
|
|
char* phone = readWithSize(client, 10);
|
|
char* hash = readWithSize(client, 32);
|
|
char* msg = readStringUntil(client, 1);
|
|
|
|
// Check hash
|
|
unsigned char* h = MD5::make_hash(msg);
|
|
char* md5str = MD5::make_digest(h, 16);
|
|
|
|
if (strcmp(md5str, hash) != 0) {
|
|
// do something
|
|
write(client, "error: corrupted message: hash not matching");
|
|
free(md5str);
|
|
free(h);
|
|
free(msg);
|
|
free(hash);
|
|
free(phone);
|
|
return;
|
|
}
|
|
free(md5str);
|
|
free(h);
|
|
free(hash);
|
|
|
|
// do the sms thing
|
|
|
|
// Clean all
|
|
free(msg);
|
|
free(phone);
|
|
write(client, "done");
|
|
}
|
|
|
|
void write(EthernetClient client, char* payload) {
|
|
while (!client.availableForWrite()) {
|
|
delay(250);
|
|
}
|
|
|
|
client.print(payload);
|
|
client.flush();
|
|
}
|