-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigTask.h
More file actions
164 lines (138 loc) · 3.87 KB
/
ConfigTask.h
File metadata and controls
164 lines (138 loc) · 3.87 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ifndef __discoverytask_h__
#define __discoverytask_h__
#include <functional>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>
#include "common.h"
#include "task.h"
namespace configtask_detail
{
static constexpr uint16_t config_udp_port = 7866;
typedef StaticJsonDocument<256> JsonDocumentType;
enum
{
CONFERR_DONOT_REPLY = -10000,
CONFERR_OK = 0,
CONFERR_FAIL,
CONFERR_INVALID,
CONFERR_NO_COMMAND,
};
class ConfigImpl : public TaskImpl {
public:
void on_config_wifi(const std::function<void(const char *ssid, const char *key)> &cb)
{
m_config_wifi_cb = cb;
}
protected:
void on_start() override
{
m_udp.begin(config_udp_port);
}
void on_loop() override
{
char buffer[256];
JsonDocumentType json;
if (m_udp.parsePacket() == 0)
return;
size_t total = m_udp.readBytes((uint8_t*)buffer, sizeof(buffer) - 1);
buffer[total] = 0;
logv() << "received config packet(len=" << total << ") from " << m_udp.remoteIP() << ": " << buffer;
if (deserializeJson(json, buffer))
{
logd() << "parse json fail";
return;
}
int id = 0;
bool id_exist = true;
if (!json["method"].is<String>())
return;
if (json["id"].is<int>())
id = json["id"];
else
id_exist = false;
String str = json["method"].as<String>();
int code = CONFERR_NO_COMMAND;
if (str == "discovery")
code = do_discovery(json);
else if (str == "config_wifi")
code = do_config_wifi(json);
if (code != CONFERR_DONOT_REPLY)
{
if (code != CONFERR_OK)
json.clear();
if (id_exist)
json["id"] = id;
else
json["id"] = nullptr;
json["result"] = code;
serializeJson(json, str);
logv() << "reply: " << str;
m_udp.beginPacket(m_udp.remoteIP(), m_udp.remotePort());
m_udp.write(str.c_str(), str.length());
m_udp.endPacket();
}
}
void on_stop() override
{
m_udp.stop();
}
const char* get_name() const override
{
return "Config";
}
private:
int do_discovery(JsonDocumentType& json)
{
if (json["device_type"].is<String>() && json["device_type"].as<String>() != get_device_type())
return CONFERR_DONOT_REPLY;
if (json["device_id"].is<String>() && json["device_id"].as<String>() != get_device_id())
return CONFERR_DONOT_REPLY;
json.clear();
json["device_type"] = get_device_type();
json["device_id"] = get_device_id();
return CONFERR_OK;
}
int do_config_wifi(JsonDocumentType& json)
{
String ssid, key;
bool key_exist = false;
if (!json["ssid"].is<String>())
{
logw() << "require ssid";
return CONFERR_INVALID;
}
if (json["key"].is<String>())
key_exist = true;
ssid = json["ssid"].as<String>();
if (key_exist)
{
key = json["key"].as<String>();
if (key.length() < 8)
{
logw() << "key is too short";
return CONFERR_INVALID;
}
}
if (m_config_wifi_cb)
m_config_wifi_cb(ssid.c_str(), key_exist ? key.c_str() : nullptr);
json.clear();
return CONFERR_OK;
}
WiFiUDP m_udp;
std::function<void(const char *name, const char *key)> m_config_wifi_cb;
};
}
class ConfigTask : public Task
{
public:
void on_config_wifi(const std::function<void(const char *ssid, const char *key)> &cb)
{
m_impl.on_config_wifi(cb);
}
ConfigTask() : Task(&m_impl)
{}
private:
configtask_detail::ConfigImpl m_impl;
};
#endif