Function bodies 41 total
TCPMessageTimerCallback function · cpp · L4-L8 (5 LOC)src/globals.cpp
void TCPMessageTimerCallback(TimerHandle_t xTimer)
{
TCPServer.SetTCPMessageTimeout(true);
}setup function · cpp · L9-L15 (7 LOC)src/main.cpp
void setup()
{
MACHINE_SERIAL.begin(9600);
SerialDebugPrintInit();
SerialDebugPrint("Setup Complete!");
}loop function · cpp · L16-L31 (16 LOC)src/main.cpp
void loop()
{
if (MaintenanceHandle.IsMaintenanceModeActive() == true)
{
TCPServer.Deinit();
MaintenanceHandle.Init();
MaintenanceHandle.Run();
}
else
{
MaintenanceHandle.Deinit();
TCPServer.Init();
TCPServer.Run();
}
}Init method · cpp · L23-L35 (13 LOC)src/Maintenance.cpp
void Maintenance::Init(void)
{
if (initDone == true)
{
return;
}
SerialDebugPrint("Maintenance Mode");
WiFi.mode(WIFI_STA);
WiFi.begin(MAINTENANCE_SSID, MAINTENANCE_PASSWORD);
initDone = true;
}Deinit method · cpp · L36-L46 (11 LOC)src/Maintenance.cpp
void Maintenance::Deinit(void)
{
if (initDone == false)
{
return;
}
WiFi.disconnect();
initDone = false;
}IsMaintenanceModeActive method · cpp · L47-L109 (63 LOC)src/Maintenance.cpp
bool Maintenance::IsMaintenanceModeActive(void)
{
if (MACHINE_SERIAL.peek() == 'M' && MACHINE_SERIAL.available() >= 4)
{
uint8_t buffer[4];
MACHINE_SERIAL.readBytes((uint8_t *)buffer, 4);
if (strncmp((const char *)buffer, MAINTENANCE_MODE_ACTIVE_MESSAGE, 4) == 0)
{
SerialDebugPrint("Maintenance Mode Active");
TCPServer.Deinit();
Init();
maintenanceModeActive = true;
MACHINE_SERIAL.write(ACK_MESSAGE, 3);
}
else if (strncmp((const char *)buffer, MAINTENANCE_MODE_DEACTIVE_MESSAGE, 4) == 0)
{
SerialDebugPrint("Maintenance Mode deactive");
Deinit();
TCPServer.Init();
maintenanceModeActive = false;
MACHINE_SERIAL.write(ACK_MESSAGE, 3);
}
}
else if(MACHINE_SERIAL.peek() == 'M' && MACHINE_SERIAL.available() < 4)
{
for (uint8_t i = 0; i < 10; i++)
{
if (MACHINE_SERIRun method · cpp · L110-L226 (117 LOC)src/Maintenance.cpp
void Maintenance::Run(void)
{
static Parameters_t machineParameters;
switch (GetMaintenanceState())
{
case WIFI_CONNECTION_STATE:
{
if (WiFi.status() == WL_CONNECTED)
{
SerialDebugPrint("Connected to WiFi.");
SetMaintenanceState(CONNECTION_ESTABLISHED);
}
else
{
SerialDebugPrint("Connecting to WiFi...");
vTaskDelay(3000 / portTICK_PERIOD_MS);
}
break;
}
case CONNECTION_ESTABLISHED:
{
RequestApiState_t machineRequest = GetParamFromMachine(machineParameters);
if (machineRequest == FAIL_REQ)
{
SerialDebugPrint("Machine request error.");
SetMaintenanceState(WIFI_CONNECTION_STATE);
}
else
{
vTaskDelay(1000 / portTICK_PERIOD_MS);
RequestApiState_t isMachineInDRepobility · severity-and-effort ranking · https://repobility.com
ReceiveAck method · cpp · L227-L254 (28 LOC)src/Maintenance.cpp
bool Maintenance::ReceiveAck(const char *expectedAck)
{
unsigned long startTime = millis();
int ackLength = strlen(expectedAck);
int matchIndex = 0;
while (millis() - startTime < TIMEOUT_MS)
{
if (MACHINE_SERIAL.available() > 0)
{
char inChar = (char)MACHINE_SERIAL.read();
if (inChar == expectedAck[matchIndex])
{
matchIndex++;
if (matchIndex == ackLength)
{
return true;
}
}
else
{
matchIndex = 0;
}
}
}
return false;
}SendParamToMachine method · cpp · L255-L271 (17 LOC)src/Maintenance.cpp
RequestApiState_t Maintenance::SendParamToMachine(const Parameters_t machine)
{
RequestApiState_t retVal = FAIL_REQ;
MACHINE_SERIAL.write(SET_PARAMETERS_MESSAGE, 3);
MACHINE_SERIAL.write((uint8_t *)&machine, sizeof(machine));
if (ReceiveAck(SET_PARAMETERS_ACK) == false)
{
Serial.println("SET ACK Failed.");
return retVal;
}
Serial.println("SET ACK OK.");
retVal = SUCCESS_REQ;
return retVal;
}GetParamFromMachine method · cpp · L272-L315 (44 LOC)src/Maintenance.cpp
RequestApiState_t Maintenance::GetParamFromMachine(Parameters_t &machine)
{
RequestApiState_t retVal = FAIL_REQ;
unsigned long startTime = millis();
MACHINE_SERIAL.write(GET_PARAMETERS_MESSAGE, 3);
if (ReceiveAck(GET_PARAMETERS_ACK) == false)
{
Serial.println("GET ACK Failed.");
return retVal;
}
Serial.println("GET ACK OK.");
size_t bytesReceived = 0;
while ((millis() - startTime < TIMEOUT_MS) && (bytesReceived < sizeof(machine)))
{
if (MACHINE_SERIAL.available() > 0)
{
bytesReceived += MACHINE_SERIAL.readBytes((uint8_t *)&machine + bytesReceived, sizeof(machine) - bytesReceived);
}
}
if (millis() - startTime > TIMEOUT_MS)
{
Serial.println("GET timeout.");
return retVal;
}
if (bytesReceived != sizeof(machine))
{
Serial.println("GET incorrect number of bytes received.");
return retVal;
}
machine.machineIP[0] = STATIC_IP[0];
GetMaintenanceState method · cpp · L316-L320 (5 LOC)src/Maintenance.cpp
MaintenanceState_t Maintenance::GetMaintenanceState(void)
{
return maintenanceState;
}SetMaintenanceState method · cpp · L321-L325 (5 LOC)src/Maintenance.cpp
void Maintenance::SetMaintenanceState(MaintenanceState_t state)
{
maintenanceState = state;
}Maintenance class · c · L36-L58 (23 LOC)src/Maintenance.h
class Maintenance
{
public:
Maintenance();
~Maintenance();
void Init(void);
void Deinit(void);
bool IsMaintenanceModeActive(void);
void Run(void);
MaintenanceState_t GetMaintenanceState(void);
void SetMaintenanceState(MaintenanceState_t);
private:
bool ReceiveAck(const char *expectedAck);
RequestApiState_t GetParamFromMachine(Parameters_t &machine);
RequestApiState_t SendParamToMachine(const Parameters_t machine);
MaintenanceState_t maintenanceState;
bool maintenanceModeActive = false;
bool initDone = false;
};SerialDebugPrintInit function · cpp · L4-L9 (6 LOC)src/serialDebugPrint.cpp
void SerialDebugPrintInit(void)
{
Serial.begin(115200);
xSerialPrintSemaphore = xSemaphoreCreateMutex();
}SerialDebugPrint function · cpp · L10-L18 (9 LOC)src/serialDebugPrint.cpp
void SerialDebugPrint(const char *text)
{
#if SERIAL_DEBUG_PRINT
xSemaphoreTake(xSerialPrintSemaphore, portMAX_DELAY);
Serial.println(text);
xSemaphoreGive(xSerialPrintSemaphore);
#endif
}Want fix-PRs on findings? Install Repobility's GitHub App · github.com/apps/repobility-bot
SerialDebugPrint function · cpp · L19-L27 (9 LOC)src/serialDebugPrint.cpp
void SerialDebugPrint(int number)
{
#if SERIAL_DEBUG_PRINT
xSemaphoreTake(xSerialPrintSemaphore, portMAX_DELAY);
Serial.println(number);
xSemaphoreGive(xSerialPrintSemaphore);
#endif
}Init method · cpp · L15-L65 (51 LOC)src/TCPServerRouter.cpp
void TCPServerRouter::Init(void)
{
if (initDone == true)
{
return;
}
SerialDebugPrint("Router Mode");
server = WiFiServer(TCP_SERVER_LISTEN_PORT); /* Port */
IPAddress staticIP(STATIC_IP[0], STATIC_IP[1], STATIC_IP[2], STATIC_IP[3]);
IPAddress gateway(GATEWAY[0], GATEWAY[1], GATEWAY[2], GATEWAY[3]);
IPAddress subnet(SUBNET[0], SUBNET[1], SUBNET[2], SUBNET[3]);
WiFi.mode(WIFI_AP);
bool result = WiFi.softAPConfig(staticIP, gateway, subnet);
if (result == true)
{
result = WiFi.softAP(AP_SSID, AP_PASSWORD, 1, 0, 4);
if (result == true)
{
server.begin();
TCPMessageTimer = xTimerCreate("TCPMessageTimer", pdMS_TO_TICKS(TCP_MESSAGE_TIMEOUT_MS), pdTRUE, (void *)0, TCPMessageTimerCallback);
if (TCPMessageTimer == NULL)
{
SerialDebugPrint("Timer Creation Error");
while (1)
{
/* Error! */
Deinit method · cpp · L66-L79 (14 LOC)src/TCPServerRouter.cpp
void TCPServerRouter::Deinit(void)
{
if (initDone == false)
{
return;
}
StopTCPMessageTimeoutTimer();
server.close();
WiFi.disconnect();
SetTCPConnectionState(WAITING_FOR_CLIENT);
initDone = false;
}Run method · cpp · L80-L141 (62 LOC)src/TCPServerRouter.cpp
void TCPServerRouter::Run(void)
{
switch (GetTCPConnectionState())
{
case WAITING_FOR_CLIENT:
{
if(WaitingForClient() == true)
{
SerialDebugPrint("New client connected");
SetTCPConnectionState(CLIENT_CONNECTED);
ResetTCPMessageTimeoutTimer();
}
break;
}
case CLIENT_CONNECTED:
{
if (GetTCPMessageTimeout() == true)
{
SetTCPMessageTimeout(false);
SerialDebugPrint("TCP message timeout!");
client.stop();
ResetTCPMessageTimeoutTimer();
SetTCPConnectionState(WAITING_FOR_CLIENT);
}
else if (IsClientConnected() != true)
{
SerialDebugPrint("Client disconnected!");
client.stop();
ResetTCPMessageTimeoutTimer();
SetTCPConnectionState(WAITING_FOR_CLIENT);
GetTCPConnectionState method · cpp · L142-L146 (5 LOC)src/TCPServerRouter.cpp
TCPConnectionState_t TCPServerRouter::GetTCPConnectionState(void)
{
return TCPConnectionState;
}SetTCPConnectionState method · cpp · L147-L151 (5 LOC)src/TCPServerRouter.cpp
void TCPServerRouter::SetTCPConnectionState(TCPConnectionState_t state)
{
TCPConnectionState = state;
}WaitingForClient method · cpp · L152-L161 (10 LOC)src/TCPServerRouter.cpp
bool TCPServerRouter::WaitingForClient(void)
{
client = server.available();
if (client)
{
return true;
}
return false;
}IsClientConnected method · cpp · L162-L166 (5 LOC)src/TCPServerRouter.cpp
bool TCPServerRouter::IsClientConnected(void)
{
return client.connected();
}Want this analysis on your repo? https://repobility.com/scan/
TCPMessageBufferSize method · cpp · L167-L171 (5 LOC)src/TCPServerRouter.cpp
int TCPServerRouter::TCPMessageBufferSize(void)
{
return client.available();
}CheckForProperTCPMessage method · cpp · L172-L225 (54 LOC)src/TCPServerRouter.cpp
bool TCPServerRouter::CheckForProperTCPMessage(TCPMessage_t* messagePointer)
{
int readData = client.peek();
int bufferSize = TCPMessageBufferSize();
bool firstControlResult = false;
if (readData == 'T' && bufferSize >= sizeof(TCPMessage_t))
{
firstControlResult = true;
}
else if (readData == 'T' && bufferSize < sizeof(TCPMessage_t))
{
for (uint8_t i = 0; i < 5; i++)
{
vTaskDelay(10 / portTICK_PERIOD_MS);
bufferSize = TCPMessageBufferSize();
if (bufferSize >= sizeof(TCPMessage_t))
{
firstControlResult = true;
}
}
int discardData = client.read();
SerialDebugPrint("Half Message! Discarded!");
firstControlResult = false;
}
else
{
firstControlResult = false;
int discardData = client.read();
SerialDebugPrint("Wrong Header Byte! Discarded!");
}
if (firstControlResult == false)
{
SendACKToRemote method · cpp · L226-L230 (5 LOC)src/TCPServerRouter.cpp
void TCPServerRouter::SendACKToRemote(void)
{
client.write(ACK_MESSAGE, 3);
}SendMessageToMachine method · cpp · L231-L235 (5 LOC)src/TCPServerRouter.cpp
void TCPServerRouter::SendMessageToMachine(TCPMessage_t message)
{
MACHINE_SERIAL.write((uint8_t *)&message, sizeof(TCPMessage_t));
}WaitACKFromMachine method · cpp · L236-L291 (56 LOC)src/TCPServerRouter.cpp
MachineResponseState_t TCPServerRouter::WaitACKFromMachine(void)
{
bool ackCameFromMachine = false;
static uint8_t ackWaitCounter = 0;
MachineResponseState_t response = RESPONSE_TIMEOUT;
for (uint8_t i = 0; i < 10; i++)
{
if (MACHINE_SERIAL.available() > 0)
{
uint8_t firstByte = MACHINE_SERIAL.peek();
if (firstByte == 'A' && MACHINE_SERIAL.available() >= 3)
{
ackWaitCounter = 0;
uint8_t message[3] = {0};
MACHINE_SERIAL.readBytes(message, 3);
if (strncmp((const char *)message, ACK_MESSAGE, 3) == 0)
{
/* ACK Came from machine */
ackCameFromMachine = true;
return ACK_CAME;
}
else
{
/* Unknown 3 byte message came */
SerialDebugPrint("Unknown 3 byte message came from machine");
GetTCPMessageTimeout method · cpp · L292-L296 (5 LOC)src/TCPServerRouter.cpp
bool TCPServerRouter::GetTCPMessageTimeout(void)
{
return TCPMessageTimeout;
}SetTCPMessageTimeout method · cpp · L297-L301 (5 LOC)src/TCPServerRouter.cpp
void TCPServerRouter::SetTCPMessageTimeout(bool timeout)
{
TCPMessageTimeout = timeout;
}ResetTCPMessageTimeoutTimer method · cpp · L302-L309 (8 LOC)src/TCPServerRouter.cpp
void TCPServerRouter::ResetTCPMessageTimeoutTimer(void)
{
if (xTimerReset(TCPMessageTimer, 10) != pdPASS)
{
SerialDebugPrint("Failed to start timer!");
}
}Same scanner, your repo: https://repobility.com — Repobility
StopTCPMessageTimeoutTimer method · cpp · L310-L317 (8 LOC)src/TCPServerRouter.cpp
void TCPServerRouter::StopTCPMessageTimeoutTimer(void)
{
if (xTimerStop(TCPMessageTimer, 10) != pdPASS)
{
SerialDebugPrint("Failed to stop timer!");
}
}ControlChecksum method · cpp · L318-L328 (11 LOC)src/TCPServerRouter.cpp
bool TCPServerRouter::ControlChecksum(uint8_t *dataPointer, uint8_t size)
{
uint8_t checksum = 0;
for (uint8_t i = 0; i < size - 1; i++)
{
checksum ^= dataPointer[i];
}
checksum ^= 255;
return (dataPointer[size - 1] == checksum);
}TCPServerRouter class · c · L60-L99 (40 LOC)src/TCPServerRouter.h
class TCPServerRouter
{
public:
TCPServerRouter();
~TCPServerRouter();
void Init(void);
void Deinit(void);
void Run(void);
TCPConnectionState_t GetTCPConnectionState(void);
void SetTCPConnectionState(TCPConnectionState_t);
bool WaitingForClient(void);
bool IsClientConnected(void);
int TCPMessageBufferSize(void);
bool CheckForProperTCPMessage(TCPMessage_t*);
void SendACKToRemote(void);
void SendMessageToMachine(TCPMessage_t);
MachineResponseState_t WaitACKFromMachine(void);
bool GetTCPMessageTimeout(void);
void SetTCPMessageTimeout(bool);
void ResetTCPMessageTimeoutTimer(void);
void StopTCPMessageTimeoutTimer(void);
private:
WiFiServer server;
WiFiClient client;
TCPConnectionState_t TCPConnectionState = WAITING_FOR_CLIENT;
bool initDone = false;
bool TCPMessageTimeout = false;
TimerHandle_t TCPMessageTimer;
bool ControlChecksum(uint8_t *dataPointer, uint8_t size);
};SetMachineData function · cpp · L13-L72 (60 LOC)src/webApi.cpp
RequestApiState_t SetMachineData(const char *apiUrl, const Parameters_t parameters)
{
RequestApiState_t retVal = FAIL_REQ;
HTTPClient http;
String fullApiUrl = String(apiUrl) + "/robot/api/setMachine.php";
String columns = "machineIp,errLeftCnt,errRightCnt,errBrushCnt,errControllerCnt,prmLeftRampUp,prmLeftRampDown,prmLeftMinSpeed,prmLeftMaxSpeed,prmRightRampUp,prmRightRampDown,prmRightMinSpeed,prmRightMaxSpeed,prmBrushRampUp,prmBrushRampDown,prmBrushMinSpeed,prmBrushMaxSpeed,prmJyMiddleVal,prmJyDeadZone,prmJoyMinVal,prmJoyMaxVal,prmPotMinVal,prmPotMaxVal";
String values = String(parameters.machineIP[0]) + "." + String(parameters.machineIP[1]) + "." + String(parameters.machineIP[2]) + "." + String(parameters.machineIP[3]) + "," +
String(parameters.leftErrorCount) + "," +
String(parameters.rightErrorCount) + "," +
String(parameters.brushErrorCount) + "," +
String(parameters.controllerErCreateMachine function · cpp · L73-L129 (57 LOC)src/webApi.cpp
RequestApiState_t CreateMachine(const char *apiUrl, const Parameters_t parameters)
{
RequestApiState_t retVal = FAIL_REQ;
HTTPClient http;
String fullApiUrl = String(apiUrl) + "/robot/api/createMachine.php";
String postData = "machineName=" + String((char *)parameters.companyName) +
"&machineIp=" + String(parameters.machineIP[0]) + "." + String(parameters.machineIP[1]) +
"." + String(parameters.machineIP[2]) + "." + String(parameters.machineIP[3]) +
"&errLeftCnt=" + String(parameters.leftErrorCount) +
"&errRightCnt=" + String(parameters.rightErrorCount) +
"&errBrushCnt=" + String(parameters.brushErrorCount) +
"&errControllerCnt=" + String(parameters.controllerErrorCount) +
"&prmLeftRampUp=" + String(parameters.leftRampUp) +
"&prmLeftRampDown=" + String(parameters.leftRampDown) +
IsMachinePresentInDB function · cpp · L130-L166 (37 LOC)src/webApi.cpp
RequestApiState_t IsMachinePresentInDB(const char *apiUrl, const Parameters_t parameters)
{
RequestApiState_t retVal = FALSE_REQ;
HTTPClient http;
String fullApiUrl = String(apiUrl) + "/robot/api/getMachine.php";
String postData = "";
http.begin(fullApiUrl);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST(postData);
if (httpResponseCode == HTTP_CODE_OK)
{
String response = http.getString();
if (IsResponseFalse(response) == false)
{
String targetString = "\"machineName\":\"" + String((char *)parameters.companyName) + "\"";
if (response.indexOf(targetString) != -1)
{
retVal = TRUE_REQ;
}
}
}
else
{
Serial.println("HTTP Request failed. Error code: " + String(httpResponseCode));
retVal = FAIL_REQ;
}
http.end();
return retVal;
}FetchMachineData function · cpp · L167-L230 (64 LOC)src/webApi.cpp
RequestApiState_t FetchMachineData(const char *apiUrl, Parameters_t ¶meters)
{
RequestApiState_t retVal = FAIL_REQ;
HTTPClient http;
String fullApiUrl = String(apiUrl) + "/robot/api/getMachine.php";
String postData = "machineName=" + String((char *)parameters.companyName);
http.begin(fullApiUrl);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST(postData);
if (httpResponseCode == HTTP_CODE_OK)
{
String response = http.getString();
if (IsResponseFalse(response) == false)
{
String companyName = GetJsonValue(response, "machineName");
strncpy((char *)parameters.companyName, companyName.c_str(), sizeof(parameters.companyName));
std::vector<int> ipVec = SplitIpString(GetJsonValue(response, "machineIp"));
parameters.machineIP[0] = ipVec[0];
parameters.machineIP[1] = ipVec[1];
parameters.machineIP[2] =SplitIpString function · cpp · L231-L252 (22 LOC)src/webApi.cpp
std::vector<int> SplitIpString(const String &ipString)
{
std::vector<int> ipParts;
int startPos = 0;
for (int i = 0; i < 4; ++i)
{
int dotPos = ipString.indexOf('.', startPos);
if (dotPos == -1)
{
dotPos = ipString.length();
}
String part = ipString.substring(startPos, dotPos);
ipParts.push_back(part.toInt());
startPos = dotPos + 1;
}
return ipParts;
}Repobility · severity-and-effort ranking · https://repobility.com
GetJsonValue function · cpp · L253-L285 (33 LOC)src/webApi.cpp
String GetJsonValue(const String &json, const String &key)
{
int keyIndex = json.indexOf('\"' + key + '\"');
if (keyIndex == -1)
{
Serial.println("Key not found");
return "";
}
int valueIndex = json.indexOf(':', keyIndex);
if (valueIndex == -1)
{
Serial.println("Value not found");
return "";
}
int startIndex = json.indexOf('\"', valueIndex);
if (startIndex == -1)
{
Serial.println("Start double quote for value not found");
return "";
}
int endIndex = json.indexOf('\"', startIndex + 1);
if (endIndex == -1)
{
Serial.println("End double quote for value not found");
return "";
}
return json.substring(startIndex + 1, endIndex);
}IsResponseFalse function · cpp · L286-L302 (17 LOC)src/webApi.cpp
bool IsResponseFalse(const String &response)
{
if (!response.isEmpty() && (response[0] == ' ' || response[0] == '\0'))
{
return true;
}
if (response.length() <= 20)
{
return true;
}
String lowercaseResponse = response;
lowercaseResponse.toLowerCase();
return lowercaseResponse.indexOf("false") != -1;
}