Format function names
This commit is contained in:
parent
b23dac7531
commit
bd97eb46ff
|
|
@ -22,7 +22,7 @@ void Span::processImprovCommand(const char *c, Span* span){
|
||||||
strcpy(buffer, c);
|
strcpy(buffer, c);
|
||||||
buffer[len-1] = c[len-1]; // Copy the checksum bit since it falls after the null terminator
|
buffer[len-1] = c[len-1]; // Copy the checksum bit since it falls after the null terminator
|
||||||
|
|
||||||
improv::parse_improv_serial_byte(len - 1, buffer[len - 1], (uint8_t *)c, [&](ImprovCommand command) {
|
improv::parseImprovSerialByte(len - 1, buffer[len - 1], (uint8_t *)c, [&](ImprovCommand command) {
|
||||||
improv::handleImprovCommand(command, span);
|
improv::handleImprovCommand(command, span);
|
||||||
return true;
|
return true;
|
||||||
}, [&](Error error) {
|
}, [&](Error error) {
|
||||||
|
|
@ -49,7 +49,7 @@ void handleImprovCommand(improv::ImprovCommand cmd, Span* span) {
|
||||||
if (connectWifi(cmd.ssid.c_str(), cmd.password.c_str())) {
|
if (connectWifi(cmd.ssid.c_str(), cmd.password.c_str())) {
|
||||||
sendImprovState(improv::State::STATE_PROVISIONED);
|
sendImprovState(improv::State::STATE_PROVISIONED);
|
||||||
std::vector<std::string> infos; // Empty vector, we could put an HTTP URL here as the next step for the user if we had one
|
std::vector<std::string> infos; // Empty vector, we could put an HTTP URL here as the next step for the user if we had one
|
||||||
std::vector<uint8_t> data = improv::build_rpc_response(improv::WIFI_SETTINGS, infos, false);
|
std::vector<uint8_t> data = improv::buildRPCResponse(improv::WIFI_SETTINGS, infos, false);
|
||||||
improv::sendImprovResponse(data);
|
improv::sendImprovResponse(data);
|
||||||
|
|
||||||
span->setWifiCredentials(cmd.ssid.c_str(), cmd.password.c_str()); // Save credentials and reboot
|
span->setWifiCredentials(cmd.ssid.c_str(), cmd.password.c_str()); // Save credentials and reboot
|
||||||
|
|
@ -66,7 +66,7 @@ void handleImprovCommand(improv::ImprovCommand cmd, Span* span) {
|
||||||
if((WiFi.status() == WL_CONNECTED)) {
|
if((WiFi.status() == WL_CONNECTED)) {
|
||||||
sendImprovState(improv::State::STATE_PROVISIONED);
|
sendImprovState(improv::State::STATE_PROVISIONED);
|
||||||
std::vector<std::string> infos; // Empty vector, we could put an HTTP URL here as the next step for the user if we had one
|
std::vector<std::string> infos; // Empty vector, we could put an HTTP URL here as the next step for the user if we had one
|
||||||
std::vector<uint8_t> data = improv::build_rpc_response(improv::GET_CURRENT_STATE, infos, false);
|
std::vector<uint8_t> data = improv::buildRPCResponse(improv::GET_CURRENT_STATE, infos, false);
|
||||||
improv::sendImprovResponse(data);
|
improv::sendImprovResponse(data);
|
||||||
} else {
|
} else {
|
||||||
sendImprovState(improv::State::STATE_AUTHORIZED);
|
sendImprovState(improv::State::STATE_AUTHORIZED);
|
||||||
|
|
@ -87,7 +87,7 @@ void handleImprovCommand(improv::ImprovCommand cmd, Span* span) {
|
||||||
// Device name
|
// Device name
|
||||||
span->getDisplayName(),
|
span->getDisplayName(),
|
||||||
};
|
};
|
||||||
std::vector<uint8_t> data = improv::build_rpc_response(improv::GET_DEVICE_INFO, infos, false);
|
std::vector<uint8_t> data = improv::buildRPCResponse(improv::GET_DEVICE_INFO, infos, false);
|
||||||
improv::sendImprovResponse(data);
|
improv::sendImprovResponse(data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -98,7 +98,7 @@ void handleImprovCommand(improv::ImprovCommand cmd, Span* span) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Command::BAD_CHECKSUM:
|
case Command::BAD_CHECKSUM:
|
||||||
LOG2("Improv-Serial Error: Bad Checksum");
|
//LOG2("Improv-Serial Error: Bad Checksum");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Command::UNKNOWN:
|
case Command::UNKNOWN:
|
||||||
|
|
@ -112,12 +112,12 @@ void handleImprovCommand(improv::ImprovCommand cmd, Span* span) {
|
||||||
bool connectWifi(const char *ssid, const char *pwd) {
|
bool connectWifi(const char *ssid, const char *pwd) {
|
||||||
uint8_t attempts = 0;
|
uint8_t attempts = 0;
|
||||||
|
|
||||||
LOG2("Attempting to connect to WiFi SSID %s\n", ssid);
|
//LOG2("Attempting to connect to WiFi SSID %s\n", ssid);
|
||||||
WiFi.begin(ssid, pwd); // Connect to the network
|
WiFi.begin(ssid, pwd); // Connect to the network
|
||||||
|
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
delay(1000); // Wait for the connection to be established
|
delay(1000); // Wait for the connection to be established
|
||||||
LOG2("Attempting to connect to WiFi SSID %s, attempt #%s\n", ssid, String(attempts));
|
//LOG2("Attempting to connect to WiFi SSID %s, attempt #%s\n", ssid, String(attempts));
|
||||||
|
|
||||||
if (attempts > MAX_ATTEMPTS_WIFI_CONNECTION) { // Give up after a certain number of attempts
|
if (attempts > MAX_ATTEMPTS_WIFI_CONNECTION) { // Give up after a certain number of attempts
|
||||||
LOG0("Failed to connect to WiFi");
|
LOG0("Failed to connect to WiFi");
|
||||||
|
|
@ -138,14 +138,14 @@ void getAvailableWifiNetworks() {
|
||||||
|
|
||||||
// Send one RPC response for each network we found
|
// Send one RPC response for each network we found
|
||||||
for(int id = 0; id < networkNum; ++id) {
|
for(int id = 0; id < networkNum; ++id) {
|
||||||
std::vector<uint8_t> data = improv::build_rpc_response(
|
std::vector<uint8_t> data = improv::buildRPCResponse(
|
||||||
improv::GET_WIFI_NETWORKS, {WiFi.SSID(id), String(WiFi.RSSI(id)), (WiFi.encryptionType(id) == WIFI_AUTH_OPEN ? "NO" : "YES")}, false);
|
improv::GET_WIFI_NETWORKS, {WiFi.SSID(id), String(WiFi.RSSI(id)), (WiFi.encryptionType(id) == WIFI_AUTH_OPEN ? "NO" : "YES")}, false);
|
||||||
improv::sendImprovResponse(data);
|
improv::sendImprovResponse(data);
|
||||||
delay(1);
|
delay(1);
|
||||||
}
|
}
|
||||||
// Send a blank response to signal the end of the list
|
// Send a blank response to signal the end of the list
|
||||||
std::vector<uint8_t> data =
|
std::vector<uint8_t> data =
|
||||||
improv::build_rpc_response(improv::GET_WIFI_NETWORKS, std::vector<std::string>{}, false);
|
improv::buildRPCResponse(improv::GET_WIFI_NETWORKS, std::vector<std::string>{}, false);
|
||||||
improv::sendImprovResponse(data);
|
improv::sendImprovResponse(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -172,7 +172,7 @@ void sendImprovState(improv::State state) {
|
||||||
sprintf(hex, "%02X", data[i]);
|
sprintf(hex, "%02X", data[i]);
|
||||||
hexData += hex;
|
hexData += hex;
|
||||||
}
|
}
|
||||||
LOG2("Wrote Improv Serial state: %s\n", hexData);
|
//LOG2("Wrote Improv Serial state: %s\n", hexData);
|
||||||
} // sendImprovState
|
} // sendImprovState
|
||||||
|
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
|
|
@ -198,7 +198,7 @@ void sendImprovError(improv::Error error) {
|
||||||
sprintf(hex, "%02X", data[i]);
|
sprintf(hex, "%02X", data[i]);
|
||||||
hexData += hex;
|
hexData += hex;
|
||||||
}
|
}
|
||||||
LOG2("Wrote Improv Serial error: %s\n", hexData);
|
//LOG2("Wrote Improv Serial error: %s\n", hexData);
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
|
|
@ -224,19 +224,19 @@ void sendImprovResponse(std::vector<uint8_t> &response) {
|
||||||
sprintf(hex, "%02X", data[i]);
|
sprintf(hex, "%02X", data[i]);
|
||||||
hexData += hex;
|
hexData += hex;
|
||||||
}
|
}
|
||||||
LOG2("Wrote Improv Serial response: %s\n", hexData);
|
//LOG2("Wrote Improv Serial response: %s\n", hexData);
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
|
|
||||||
// From https://github.com/improv-wifi/sdk-cpp/blob/main/src/improv.cpp
|
// From https://github.com/improv-wifi/sdk-cpp/blob/main/src/improv.cpp
|
||||||
ImprovCommand parse_improv_data(const std::vector<uint8_t> &data, bool check_checksum) {
|
ImprovCommand parseImprovData(const std::vector<uint8_t> &data, bool check_checksum) {
|
||||||
return parse_improv_data(data.data(), data.size(), check_checksum);
|
return parseImprovData(data.data(), data.size(), check_checksum);
|
||||||
} // parse_improv_data
|
} // parseImprovData
|
||||||
|
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
|
|
||||||
ImprovCommand parse_improv_data(const uint8_t *data, size_t length, bool check_checksum) {
|
ImprovCommand parseImprovData(const uint8_t *data, size_t length, bool check_checksum) {
|
||||||
ImprovCommand improv_command;
|
ImprovCommand improv_command;
|
||||||
Command command = (Command) data[0];
|
Command command = (Command) data[0];
|
||||||
uint8_t data_length = data[1];
|
uint8_t data_length = data[1];
|
||||||
|
|
@ -276,11 +276,11 @@ ImprovCommand parse_improv_data(const uint8_t *data, size_t length, bool check_c
|
||||||
|
|
||||||
improv_command.command = command;
|
improv_command.command = command;
|
||||||
return improv_command;
|
return improv_command;
|
||||||
} // parse_improv_data
|
} // parseImprovData
|
||||||
|
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
|
|
||||||
bool parse_improv_serial_byte(size_t position, uint8_t byte, const uint8_t *buffer,
|
bool parseImprovSerialByte(size_t position, uint8_t byte, const uint8_t *buffer,
|
||||||
std::function<bool(ImprovCommand)> &&callback, std::function<void(Error)> &&on_error) {
|
std::function<bool(ImprovCommand)> &&callback, std::function<void(Error)> &&on_error) {
|
||||||
if(position == 0)
|
if(position == 0)
|
||||||
return byte == 'I';
|
return byte == 'I';
|
||||||
|
|
@ -312,24 +312,24 @@ bool parse_improv_serial_byte(size_t position, uint8_t byte, const uint8_t *buff
|
||||||
for(size_t i = 0; i < position; i++)
|
for(size_t i = 0; i < position; i++)
|
||||||
checksum += buffer[i];
|
checksum += buffer[i];
|
||||||
|
|
||||||
Serial.println("Checksum: " + String(checksum) + " Byte: " + String(byte));
|
|
||||||
if(checksum != byte) {
|
if(checksum != byte) {
|
||||||
|
//LOG2.println("Improv-Serial packet checksum, calculated: " + String(checksum) + ", expecting: " + String(byte));
|
||||||
on_error(ERROR_INVALID_RPC);
|
on_error(ERROR_INVALID_RPC);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(type == TYPE_RPC) {
|
if(type == TYPE_RPC) {
|
||||||
auto command = parse_improv_data(&buffer[9], data_len, false);
|
auto command = parseImprovData(&buffer[9], data_len, false);
|
||||||
return callback(command);
|
return callback(command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
} // parse_improv_serial_byte
|
} // parseImprovSerialByte
|
||||||
|
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
|
|
||||||
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<std::string> &datum, bool add_checksum) {
|
std::vector<uint8_t> buildRPCResponse(Command command, const std::vector<std::string> &datum, bool add_checksum) {
|
||||||
std::vector<uint8_t> out;
|
std::vector<uint8_t> out;
|
||||||
uint32_t length = 0;
|
uint32_t length = 0;
|
||||||
out.push_back(command);
|
out.push_back(command);
|
||||||
|
|
@ -350,12 +350,12 @@ std::vector<uint8_t> build_rpc_response(Command command, const std::vector<std::
|
||||||
out.push_back(calculated_checksum);
|
out.push_back(calculated_checksum);
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
} // build_rpc_response
|
} // buildRPCResponse
|
||||||
|
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
|
|
||||||
#ifdef ARDUINO
|
#ifdef ARDUINO
|
||||||
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<String> &datum, bool add_checksum) {
|
std::vector<uint8_t> buildRPCResponse(Command command, const std::vector<String> &datum, bool add_checksum) {
|
||||||
std::vector<uint8_t> out;
|
std::vector<uint8_t> out;
|
||||||
uint32_t length = 0;
|
uint32_t length = 0;
|
||||||
out.push_back(command);
|
out.push_back(command);
|
||||||
|
|
@ -376,7 +376,7 @@ std::vector<uint8_t> build_rpc_response(Command command, const std::vector<Strin
|
||||||
out.push_back(calculated_checksum);
|
out.push_back(calculated_checksum);
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
} // build_rpc_response
|
} // buildRPCResponse
|
||||||
#endif // ARDUINO
|
#endif // ARDUINO
|
||||||
|
|
||||||
} // namespace improv
|
} // namespace improv
|
||||||
10
src/Improv.h
10
src/Improv.h
|
|
@ -62,16 +62,16 @@ struct ImprovCommand {
|
||||||
std::string password;
|
std::string password;
|
||||||
};
|
};
|
||||||
|
|
||||||
ImprovCommand parse_improv_data(const std::vector<uint8_t> &data, bool check_checksum = true);
|
ImprovCommand parseImprovData(const std::vector<uint8_t> &data, bool check_checksum = true);
|
||||||
ImprovCommand parse_improv_data(const uint8_t *data, size_t length, bool check_checksum = true);
|
ImprovCommand parseImprovData(const uint8_t *data, size_t length, bool check_checksum = true);
|
||||||
|
|
||||||
bool parse_improv_serial_byte(size_t position, uint8_t byte, const uint8_t *buffer,
|
bool parseImprovSerialByte(size_t position, uint8_t byte, const uint8_t *buffer,
|
||||||
std::function<bool(ImprovCommand)> &&callback, std::function<void(Error)> &&on_error);
|
std::function<bool(ImprovCommand)> &&callback, std::function<void(Error)> &&on_error);
|
||||||
|
|
||||||
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<std::string> &datum,
|
std::vector<uint8_t> buildRPCResponse(Command command, const std::vector<std::string> &datum,
|
||||||
bool add_checksum = true);
|
bool add_checksum = true);
|
||||||
#ifdef ARDUINO
|
#ifdef ARDUINO
|
||||||
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<String> &datum, bool add_checksum = true);
|
std::vector<uint8_t> buildRPCResponse(Command command, const std::vector<String> &datum, bool add_checksum = true);
|
||||||
#endif // ARDUINO
|
#endif // ARDUINO
|
||||||
|
|
||||||
void handleImprovCommand(improv::ImprovCommand cmd, Span* span);
|
void handleImprovCommand(improv::ImprovCommand cmd, Span* span);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue