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