ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
test_main.cpp
Go to the documentation of this file.
1
12#include <Arduino.h>
13#include <unity.h>
14
15#include "module_handler.hpp"
16#include "module_test.hpp"
17
18// create handler
19static ModuleHandler::ModuleHandler module_handler;
20// create test module
21static ModuleTest module_test;
22
23void setUp(void) {
24 // reset test module
25 module_test.Reset();
26
27 // zero out buffers
28 memset(module_handler_rx_buffer, 0, module_handler_buffer_size);
29 memset(module_handler_tx_buffer, 0, module_handler_buffer_size);
30}
31
32void tearDown(void) {}
33
34void TestRegister(void) {
35 // register module with message
36 module_handler.RegisterModule(2, &module_test);
37
38 bool mod_exist = true;
39
40 // check if module exists in map
41 try {
42 ModuleHandler::Module *mod = module_handler.GetModule(2);
43 } catch (const std::out_of_range &oor) {
44 mod_exist = false;
45 }
46
47 TEST_ASSERT_TRUE_MESSAGE(mod_exist, "Module does not exist after register");
48}
49
50void TestDeregister(void) {
51 module_handler.DeregisterModule(2);
52
53 bool mod_exist = true;
54
55 // check that module doesn't exist anymore
56 try {
57 // get the module of that message type
58 ModuleHandler::Module *mod = module_handler.GetModule(2);
59 } catch (const std::out_of_range &oor) {
60 mod_exist = false;
61 }
62
63 TEST_ASSERT_TRUE_MESSAGE(mod_exist, "Module exists after deregister");
64}
65
66void TestReset(void) {
67 // reset modules
68 module_handler.ResetModules();
69
70 // get reference to module
71 ModuleHandler::Module *mod = module_handler.GetModule(2);
72
73 // check for the 0th state
74 TEST_ASSERT_EQUAL(0, mod->State());
75}
76
77void TestOnReceive(void) {
78 // definition for test command
79 const uint8_t cmd[] = {0x12, 0x2, 0x10, 0x7b};
80 const size_t cmd_len = 4;
81
82 // copy command to rx buffer
83 memcpy(module_handler_rx_buffer, cmd, cmd_len);
84
85 // simulate i2c receive
86 module_handler.OnReceive(cmd_len);
87
88 // check state of module
89 ModuleHandler::Module *mod = module_handler.GetModule(2);
90 TEST_ASSERT_EQUAL(ModuleTest::RECEIVE, mod->State());
91
92 // check tx buffer has not changed
93 TEST_ASSERT_EACH_EQUAL_UINT8(0, module_handler_tx_buffer,
94 module_handler_buffer_size);
95}
96
97void TestOnRequest(void) {
98 // test command with "request" and data 456
99 const uint8_t data[] = {0x12, 0x5, 0x8, 0x2, 0x10, 0xc8, 0x3};
100 const size_t data_len = 7;
101
102 // copy data to rx buffer
103 memcpy(module_handler_rx_buffer, data, data_len);
104
105 // simulate i2c receive
106 module_handler.OnReceive(data_len);
107
108 // check state of module
109 ModuleHandler::Module *mod = module_handler.GetModule(2);
110 TEST_ASSERT_EQUAL(ModuleTest::RECEIVE, mod->State());
111
112 // check tx buffer has not changed
113 TEST_ASSERT_EACH_EQUAL_UINT8(0, module_handler_tx_buffer,
114 module_handler_buffer_size);
115
116 // simulate i2c request
117 module_handler.OnRequest();
118
119 // 123 in byte array
120 const uint8_t out[] = {0x00, 0x00, 0x00, 0x7B};
121 const size_t out_len = 4;
122
123 // compare state
124 TEST_ASSERT_EQUAL(ModuleTest::INIT, mod->State());
125
126 // compare to tx buffer
127 TEST_ASSERT_EQUAL_HEX8_ARRAY(out, module_handler_tx_buffer, out_len);
128}
129
130void setup() {
131 // NOTE!!! Wait for >2 secs
132 // if board doesn't support software reset via Serial.DTR/RTS
133 delay(2000);
134
135 UNITY_BEGIN();
136
137 RUN_TEST(TestRegister);
138 RUN_TEST(TestDeregister);
139 RUN_TEST(TestReset);
140
141 UNITY_END();
142}
143
144void loop() {}
Interface for the module handler.
Module * GetModule(int type)
Get the module associated with a specific message type.
void OnReceive(size_t num_bytes)
Arduino I2C onRecieve.
void RegisterModule(Module *module)
Register a module with a message type.
void ResetModules(void)
Resets all modules.
void DeregisterModule(int type)
Deregister a module.
void OnRequest(void)
Arduino I2C onRequest.
Class prototype for modules.
void Reset(void)
Resets the module to default state.
int State(void)
Get the current state of the module.
const size_t data_len
const char data[]
void TestOnReceive(void)
Definition test_main.cpp:77
void setup()
void TestOnRequest(void)
Definition test_main.cpp:97
void TestReset(void)
Definition test_main.cpp:66
void setUp(void)
Definition test_main.cpp:23
void tearDown(void)
Definition test_main.cpp:32
void TestRegister(void)
Definition test_main.cpp:34
void TestDeregister(void)
Definition test_main.cpp:50
void loop()
Interface for controlling the esp32.