-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetworkIO.cpp
More file actions
140 lines (130 loc) · 3.72 KB
/
Copy pathNetworkIO.cpp
File metadata and controls
140 lines (130 loc) · 3.72 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "NetworkIO.hpp"
#include <string>
#include <cstring>
#include <thread>
#define CONCLOSE_MESSAGE "CLOSED\n"
#define PING "PING\n"
#define SERIAL_PORT "/dev/ttyACM0"
void Connection::StartIO(Server &tcpserver, HOMEKIT &device)
{
//std::thread IO(&Connection::IOHandler, this, tcpserver);
std::thread IO([this, tcpserver, device] {
this->IOHandler(tcpserver, device);
});
while (Status)
{
//std::cout << "ELAPSED SECONDS:" << Con_Check.elapsedSeconds() << std::endl;
if (Con_Check.elapsedSeconds() > 5)
{
Status = false;
tcpserver.Close();
std ::cout << "NO RESPONSE" << std ::endl;
break;
}
}
IO.join();
}
bool Auth(Server tcpserver, HOMEKIT device)
{
//char command[100];
//read(tcpserver.GetConID(), command, 100);
//command[8]='\0';
//if (!strcmp(command, "HOMEKIT\n"))
//{
write(tcpserver.GetConID(), "HOMEKIT\n", 8);
std::string Auth_key;
std::cout << "Authorized" << std::endl;
if (device.is_configured())
{
Auth_key = "A";
if (device.is_secured())
{
Auth_key+="S";
}
Auth_key+="N"+device.get_name()+"\t"+"I"+device.get_client_id()+"\t\n";
std::cout << Auth_key;
}
else
{
// TODO: Implement
}
write(tcpserver.GetConID(), Auth_key.c_str(), sizeof(Auth_key)+1);
return true;
//else
//{
// return false;
//}
}
void Connection::IOHandler(Server tcpserver, HOMEKIT device)
{
char command[100];
Con_Check.start();
if(Auth(tcpserver, device))
{
// TODO
if(device.is_secured())
{
}
}
else
{
Status = false;
return;
}
Con_Check.start();
while (Status)
{
int length = read(tcpserver.GetConID(), command, 100);
if (!Status)
break;
if (length > 0)
{
Con_Check.start();
if (!strcmp(command, PING))
{
std::cout << "Pinged" << std::endl;
bzero(&command, 100);
continue;
}
if (!strcmp(command, CONCLOSE_MESSAGE))
{
tcpserver.Close();
Status = false;
break;
}
if (command[0] == 'D')
{
std::string settings = command;
std::cout << "Settings:" << settings << std::endl;
settings.erase(0, 1);
std :: string name;
std :: string password;
name = settings.substr(0, settings.find("\t"));
std::cout << "Name:" << name << std::endl;
device.set_name(name);
int last_delimiter_pos = settings.find("\t");
settings.erase(0, last_delimiter_pos+1);
if(settings.length()>2)
{
password = settings.substr(last_delimiter_pos, settings.find("\t"));
std::cout << "Password:" << settings << std::endl;
device.set_pin(password);
}
write(tcpserver.GetConID(), "OK\n", 9);
std::cout << "CHANGED NAME:" << device.get_name() << std::endl;
continue;
}
std::cout << "Message from client:" << command << std::endl;
if (strlen(command) > 7)
{
//serialport_write(global_serial, command);
//SerialPortWrite(command, "");
}
bzero(&command, 100);
}
}
}
const bool Connection::GetStatus()
{
return Status;
}