Open FFBoard
Open source force feedback firmware
ErrorHandler.cpp
Go to the documentation of this file.
1/*
2 * ErrorHandler.cpp
3 *
4 * Created on: 09.02.2021
5 * Author: Yannick
6 */
7
8#include "ErrorHandler.h"
9#include "global_callbacks.h"
10#include "FFBoardMain.h"
11#include "cppmain.h"
12#include "critical.hpp"
13#include <span>
14
15std::vector<ErrorHandler*> ErrorHandler::errorHandlers;
16//std::vector<Error> ErrorHandler::errors;
17std::array<Error,ERRORHANDLER_MAXERRORS> ErrorHandler::errors;
18
19
20std::string Error::toString(){
21 std::string r = std::to_string((uint32_t)code) + ":";
22 switch(type){
24 r += "warn";
25 break;
26
28 r += "crit";
29 break;
31 r += "info";
32 break;
33 default:
34 r += "err";
35 }
36 r += ":" + (info == "" ? "no info" : info);
37 return r;
38}
39
40
42// errors.reserve(10);
44}
45
48}
49
50/*
51 * Clears ALL error conditions
52 */
54 // Call all error handlers
56 for(Error& error : errors)
57 e->errorCallback(error, true);
58 }
59 errors.fill(Error());
60}
61
62/*
63 * Clears errors marked as temporary
64 */
66 for (uint8_t i = 0; i < errors.size(); i++){
67 if((errors)[i].type == ErrorType::temporary){
68 //errors.erase(errors.begin()+i);
69 errors[i] = Error(); // Empty
70 break;
71 }
72 }
73 sortErrors();
74}
75
76void ErrorHandler::addError(const Error &error){
77 for(Error e : errors){
78 if(error == e){
79 return;
80 }
81 }
82// errors.push_back(error);
83 auto errIt = std::find_if(errors.begin(), errors.end(), [](const Error& err){return !err.isError();});
84 if(errIt != errors.end()){
85 *errIt = error; // If buffer is full do not store error but still call callbacks.
86 }
87
88
89 // Call all error handler with this error
90 //cpp_freertos::CriticalSection::SuspendScheduler();
92 e->errorCallback(error, false);
93 }
94 //cpp_freertos::CriticalSection::ResumeScheduler();
95}
96
98 for (uint8_t i = 0; i < errors.size(); i++){
99 if(errors[i] == error){
100// errors.erase(errors.begin()+i);
101 errors[i] = Error(); // Empty
102 break;
103 }
104 }
105
106 // Call all error handler with this error and set clear flag
107 for(ErrorHandler* e : errorHandlers){
108 e->errorCallback(error, true);
109 }
110 sortErrors();
111}
112
113/*
114 * Clears all errors with a specific errorcode
115 */
117 for (uint8_t i = 0; i < errors.size(); i++){
118 if(errors[i].code == errorcode){
119// errors.erase(errors.begin()+i);
120 errors[i] = Error(); // Empty
121 for(ErrorHandler* e : errorHandlers){
122 e->errorCallback(errors[i], true);
123 }
124 }
125 }
126 sortErrors();
127}
128
129std::span<Error> ErrorHandler::getErrors(){
130 return std::span<Error>(errors.begin(), std::find_if(errors.begin(), errors.end(), [](const Error& err){return err.type == ErrorType::none;}));
131}
132
133void ErrorHandler::errorCallback(const Error &error, bool cleared){
134
135}
136
138 auto errSortFun = [](const Error& a, const Error& b){
139 return a.code < b.code;
140 };
141 std::sort(errors.begin(),errors.end(),errSortFun);
142}
143
144
145//ClassIdentifier ErrorPrinter::info = {
146// .name = "Errorprinter",
147// .id=CLSID_ERRORS
148//};
149
150//ClassIdentifier ErrorPrinter::getInfo(){
151// return info;
152//}
153
154ErrorPrinter::ErrorPrinter() : Thread("errprint",256,19){ // Higher than default task but low.
155 this->Start();
156}
157
159 while(1){
161 std::vector<CommandReply> replies;
164
165 ErrorHandler::clearTemp(); // Errors are sent. clear them
166 }
167 this->Suspend();
168 }
169}
170
171
172// TODO prints in thread when called from isr
173void ErrorPrinter::errorCallback(const Error &error, bool cleared){
174 if(!cleared){
175// this->errorsToPrint.push_back(error); // Errors are stored in errorhandler
176 if(inIsr()){
177 this->ResumeFromISR();
178 }else{
179 this->Resume();
180 }
181 }
182}
183
184void ErrorPrinter::setEnabled(bool enabled){
185 if(this->enabled == enabled){
186 return;
187 }
188
189 if(!enabled){
191 }else{
192 addCallbackHandler(errorHandlers,static_cast<ErrorHandler*>(this));
193 }
194 this->enabled = enabled;
195}
196
198 return this->enabled;
199}
ErrorCode
Definition: ErrorHandler.h:24
static void broadcastCommandReplyAsync(const std::vector< CommandReply > &reply, CommandHandler *handler, uint32_t cmdId, CMDtype type=CMDtype::get)
static std::span< Error > getErrors()
static void sortErrors()
static std::array< Error, ERRORHANDLER_MAXERRORS > errors
Definition: ErrorHandler.h:98
static void clearTemp()
virtual ~ErrorHandler()
virtual void errorCallback(const Error &error, bool cleared)
static std::vector< ErrorHandler * > errorHandlers
Definition: ErrorHandler.h:83
static void clearError(const Error &error)
static void addError(const Error &error)
static void clearAll()
std::string info
Definition: ErrorHandler.h:67
ErrorCode code
Definition: ErrorHandler.h:65
ErrorType type
Definition: ErrorHandler.h:66
std::string toString()
void errorCallback(const Error &error, bool cleared)
void setEnabled(bool enable)
static SystemCommands * systemCommandsInstance
static void replyErrors(std::vector< CommandReply > &replies)
static bool inIsr()
Definition: cppmain.h:41
void addCallbackHandler(std::vector< C > &vec, C instance)
void removeCallbackHandler(std::vector< C > &vec, C instance)