Add test framework using easyunit
[clinton/Smoothieware.git] / src / testframework / Test_kernel.cpp
1 /*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8 /**
9 This is aprt of the Smoothie test framework, it generates a Mockable Kernl so kernel calls can be tested for
10 */
11
12 #include "libs/Kernel.h"
13 #include "libs/Module.h"
14 #include "libs/Config.h"
15 #include "libs/nuts_bolts.h"
16 #include "libs/SlowTicker.h"
17 #include "libs/Adc.h"
18 #include "libs/StreamOutputPool.h"
19 #include <mri.h>
20 #include "checksumm.h"
21 #include "ConfigValue.h"
22
23 #include "libs/StepTicker.h"
24 #include "libs/PublicData.h"
25 #include "modules/communication/SerialConsole.h"
26 #include "modules/communication/GcodeDispatch.h"
27 #include "modules/robot/Planner.h"
28 #include "modules/robot/Robot.h"
29 #include "modules/robot/Stepper.h"
30 #include "modules/robot/Conveyor.h"
31 #include "modules/robot/Pauser.h"
32
33 #include "Config.h"
34 #include "FirmConfigSource.h"
35
36 #include <malloc.h>
37 #include <array>
38 #include <functional>
39 #include <map>
40
41 Kernel* Kernel::instance;
42
43 // The kernel is the central point in Smoothie : it stores modules, and handles event calls
44 Kernel::Kernel(){
45 instance= this; // setup the Singleton instance of the kernel
46
47 // serial first at fixed baud rate (DEFAULT_SERIAL_BAUD_RATE) so config can report errors to serial
48 // Set to UART0, this will be changed to use the same UART as MRI if it's enabled
49 this->serial = new SerialConsole(USBTX, USBRX, DEFAULT_SERIAL_BAUD_RATE);
50
51 // Config next, but does not load cache yet
52 // loads config from in memory source for test framework must be loaded by test
53 this->config = nullptr;
54
55 this->streams = new StreamOutputPool();
56 this->streams->append_stream(this->serial);
57
58 this->current_path = "/";
59
60 // Configure UART depending on MRI config
61 // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
62 NVIC_SetPriorityGrouping(0);
63 NVIC_SetPriority(UART0_IRQn, 5);
64 }
65
66 // Add a module to Kernel. We don't actually hold a list of modules we just call its on_module_loaded
67 void Kernel::add_module(Module* module){
68 module->on_module_loaded();
69 }
70
71 // Adds a hook for a given module and event
72 void Kernel::register_for_event(_EVENT_ENUM id_event, Module *mod){
73 this->hooks[id_event].push_back(mod);
74 }
75
76 static std::map<_EVENT_ENUM, std::function<void(void*)> > event_callbacks;
77
78 // Call a specific event with an argument
79 void Kernel::call_event(_EVENT_ENUM id_event, void * argument){
80 for (auto m : hooks[id_event]) {
81 (m->*kernel_callback_functions[id_event])(argument);
82 }
83 if(event_callbacks.find(id_event) != event_callbacks.end()){
84 event_callbacks[id_event](argument);
85 }else{
86 printf("call_event for event: %d not handled\n", id_event);
87 }
88 }
89
90 // These are used by tests to test for various things. basically mocks
91 bool Kernel::kernel_has_event(_EVENT_ENUM id_event, Module *mod)
92 {
93 for (auto m : hooks[id_event]) {
94 if(m == mod) return true;
95 }
96 return false;
97 }
98
99 void Kernel::unregister_for_event(_EVENT_ENUM id_event, Module *mod)
100 {
101 for (auto i = hooks[id_event].begin(); i != hooks[id_event].end(); ++i) {
102 if(*i == mod) {
103 hooks[id_event].erase(i);
104 return;
105 }
106 }
107 }
108
109 void test_kernel_setup_config(const char* start, const char* end)
110 {
111 THEKERNEL->config= new Config(new FirmConfigSource("rom", start, end) );
112 // Pre-load the config cache
113 THEKERNEL->config->config_cache_load();
114 }
115
116 void test_kernel_teardown()
117 {
118 delete THEKERNEL->config;
119 THEKERNEL->config= nullptr;
120 event_callbacks.clear();
121 }
122
123 void test_kernel_trap_event(_EVENT_ENUM id_event, std::function<void(void*)> fnc)
124 {
125 event_callbacks[id_event]= fnc;
126 }
127
128 void test_kernel_untrap_event(_EVENT_ENUM id_event)
129 {
130 event_callbacks.erase(id_event);
131 }