Merge remote-tracking branch 'upstream/development/test-framework' into development...
[clinton/Smoothieware.git] / src / testframework / Test_kernel.cpp
CommitLineData
93ea6adb
JM
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/**
9This 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
41Kernel* Kernel::instance;
42
43// The kernel is the central point in Smoothie : it stores modules, and handles event calls
44Kernel::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
2097978f
JM
60 this->slow_ticker = new SlowTicker();
61
a3cb0c4c 62 // dummies (would be nice to refactor to not have to create a conveyor)
2097978f
JM
63 this->conveyor= new Conveyor();
64
93ea6adb
JM
65 // Configure UART depending on MRI config
66 // Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
67 NVIC_SetPriorityGrouping(0);
68 NVIC_SetPriority(UART0_IRQn, 5);
69}
70
71// Add a module to Kernel. We don't actually hold a list of modules we just call its on_module_loaded
72void Kernel::add_module(Module* module){
73 module->on_module_loaded();
74}
75
76// Adds a hook for a given module and event
77void Kernel::register_for_event(_EVENT_ENUM id_event, Module *mod){
78 this->hooks[id_event].push_back(mod);
79}
80
81static std::map<_EVENT_ENUM, std::function<void(void*)> > event_callbacks;
82
83// Call a specific event with an argument
84void Kernel::call_event(_EVENT_ENUM id_event, void * argument){
85 for (auto m : hooks[id_event]) {
86 (m->*kernel_callback_functions[id_event])(argument);
87 }
88 if(event_callbacks.find(id_event) != event_callbacks.end()){
89 event_callbacks[id_event](argument);
90 }else{
91 printf("call_event for event: %d not handled\n", id_event);
92 }
93}
94
95// These are used by tests to test for various things. basically mocks
96bool Kernel::kernel_has_event(_EVENT_ENUM id_event, Module *mod)
97{
98 for (auto m : hooks[id_event]) {
99 if(m == mod) return true;
100 }
101 return false;
102}
103
104void Kernel::unregister_for_event(_EVENT_ENUM id_event, Module *mod)
105{
106 for (auto i = hooks[id_event].begin(); i != hooks[id_event].end(); ++i) {
107 if(*i == mod) {
108 hooks[id_event].erase(i);
109 return;
110 }
111 }
112}
113
114void test_kernel_setup_config(const char* start, const char* end)
115{
116 THEKERNEL->config= new Config(new FirmConfigSource("rom", start, end) );
117 // Pre-load the config cache
118 THEKERNEL->config->config_cache_load();
119}
120
121void test_kernel_teardown()
122{
123 delete THEKERNEL->config;
124 THEKERNEL->config= nullptr;
125 event_callbacks.clear();
126}
127
128void test_kernel_trap_event(_EVENT_ENUM id_event, std::function<void(void*)> fnc)
129{
130 event_callbacks[id_event]= fnc;
131}
132
133void test_kernel_untrap_event(_EVENT_ENUM id_event)
134{
135 event_callbacks.erase(id_event);
136}