MCPcopy Create free account
hub / github.com/SmingHub/Sming / onConfiguration

Function onConfiguration

samples/Basic_WebSkeletonApp/app/webserver.cpp:90–143  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

88}
89
90int onConfiguration(HttpServerConnection& connection, HttpRequest& request, HttpResponse& response)
91{
92 if(request.method == HTTP_GET) {
93 sendFile("config.html", connection);
94 return 0;
95 }
96
97 if(request.method != HTTP_POST) {
98 response.code = HTTP_STATUS_BAD_REQUEST;
99 return 0;
100 }
101
102 debugf("Update config");
103 // Update config
104 if(request.getBodyStream() == nullptr) {
105 debugf("NULL bodyBuf");
106 return 0;
107 }
108
109 StaticJsonDocument<ConfigJsonBufferSize> root;
110
111 if(!Json::deserialize(root, request.getBodyStream())) {
112 debug_w("Invalid JSON to un-serialize");
113 return 0;
114 }
115
116 Json::serialize(root, Serial, Json::Pretty); // For debugging
117
118 if(root.containsKey("StaSSID")) // Settings
119 {
120 uint8_t prevStaEnable = activeConfig.StaEnable;
121
122 activeConfig.StaSSID = String((const char*)root["StaSSID"]);
123 activeConfig.StaPassword = String((const char*)root["StaPassword"]);
124 activeConfig.StaEnable = root["StaEnable"];
125
126 if(prevStaEnable && activeConfig.StaEnable) {
127 WifiStation.enable(true);
128 WifiAccessPoint.enable(false);
129 WifiStation.config(activeConfig.StaSSID, activeConfig.StaPassword);
130 } else if(activeConfig.StaEnable) {
131 WifiStation.enable(true, true);
132 WifiAccessPoint.enable(false, true);
133 WifiStation.config(activeConfig.StaSSID, activeConfig.StaPassword);
134 } else {
135 WifiStation.enable(false, true);
136 WifiAccessPoint.enable(true, true);
137 WifiAccessPoint.config("TyTherm", "ENTERYOURPASSWD", AUTH_WPA2_PSK);
138 }
139 }
140
141 saveConfig(activeConfig);
142 return 0;
143}
144
145void onConfigurationJson(HttpRequest& request, HttpResponse& response)
146{

Callers

nothing calls this directly

Calls 8

sendFileFunction · 0.85
deserializeFunction · 0.85
serializeFunction · 0.85
getBodyStreamMethod · 0.80
saveConfigFunction · 0.70
StringClass · 0.50
enableMethod · 0.45
configMethod · 0.45

Tested by

no test coverage detected