4 Copyright (c) 2016 Fred Sundvik
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #include <cgreen/cgreen.h>
26 #include <cgreen/mocks.h>
27 #include "serial_link/protocol/transport.c"
28 #include "serial_link/protocol/triple_buffered_object.c"
30 void signal_data_written(void) {
34 static uint8_t sent_data[2048];
35 static uint16_t sent_data_size;
37 void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) {
39 memcpy(sent_data + sent_data_size, data, size);
40 sent_data_size += size;
52 MASTER_TO_ALL_SLAVES_OBJECT(master_to_slave, test_object1_t);
53 MASTER_TO_SINGLE_SLAVE_OBJECT(master_to_single_slave, test_object1_t);
54 SLAVE_TO_MASTER_OBJECT(slave_to_master, test_object1_t);
56 static remote_object_t* test_remote_objects[] = {
57 REMOTE_OBJECT(master_to_slave),
58 REMOTE_OBJECT(master_to_single_slave),
59 REMOTE_OBJECT(slave_to_master),
63 BeforeEach(Transport) {
64 add_remote_objects(test_remote_objects, sizeof(test_remote_objects) / sizeof(remote_object_t*));
67 AfterEach(Transport) {}
69 Ensure(Transport, write_to_local_signals_an_event) {
70 begin_write_master_to_slave();
71 expect(signal_data_written);
72 end_write_master_to_slave();
73 begin_write_slave_to_master();
74 expect(signal_data_written);
75 end_write_slave_to_master();
76 begin_write_master_to_single_slave(1);
77 expect(signal_data_written);
78 end_write_master_to_single_slave(1);
81 Ensure(Transport, writes_from_master_to_all_slaves) {
83 test_object1_t* obj = begin_write_master_to_slave();
85 expect(signal_data_written);
86 end_write_master_to_slave();
87 expect(router_send_frame,
88 when(destination, is_equal_to(0xFF)));
90 transport_recv_frame(0, sent_data, sent_data_size);
91 test_object1_t* obj2 = read_master_to_slave();
92 assert_that(obj2, is_not_equal_to(NULL));
93 assert_that(obj2->test, is_equal_to(5));
96 Ensure(Transport, writes_from_slave_to_master) {
98 test_object1_t* obj = begin_write_slave_to_master();
100 expect(signal_data_written);
101 end_write_slave_to_master();
102 expect(router_send_frame,
103 when(destination, is_equal_to(0)));
105 transport_recv_frame(3, sent_data, sent_data_size);
106 test_object1_t* obj2 = read_slave_to_master(2);
107 assert_that(read_slave_to_master(0), is_equal_to(NULL));
108 assert_that(obj2, is_not_equal_to(NULL));
109 assert_that(obj2->test, is_equal_to(7));
112 Ensure(Transport, writes_from_master_to_single_slave) {
114 test_object1_t* obj = begin_write_master_to_single_slave(3);
116 expect(signal_data_written);
117 end_write_master_to_single_slave(3);
118 expect(router_send_frame,
119 when(destination, is_equal_to(4)));
121 transport_recv_frame(0, sent_data, sent_data_size);
122 test_object1_t* obj2 = read_master_to_single_slave();
123 assert_that(obj2, is_not_equal_to(NULL));
124 assert_that(obj2->test, is_equal_to(7));
127 Ensure(Transport, ignores_object_with_invalid_id) {
129 test_object1_t* obj = begin_write_master_to_single_slave(3);
131 expect(signal_data_written);
132 end_write_master_to_single_slave(3);
133 expect(router_send_frame,
134 when(destination, is_equal_to(4)));
136 sent_data[sent_data_size - 1] = 44;
137 transport_recv_frame(0, sent_data, sent_data_size);
138 test_object1_t* obj2 = read_master_to_single_slave();
139 assert_that(obj2, is_equal_to(NULL));
142 Ensure(Transport, ignores_object_with_size_too_small) {
144 test_object1_t* obj = begin_write_master_to_slave();
146 expect(signal_data_written);
147 end_write_master_to_slave();
148 expect(router_send_frame);
150 sent_data[sent_data_size - 2] = 0;
151 transport_recv_frame(0, sent_data, sent_data_size - 1);
152 test_object1_t* obj2 = read_master_to_slave();
153 assert_that(obj2, is_equal_to(NULL));
156 Ensure(Transport, ignores_object_with_size_too_big) {
158 test_object1_t* obj = begin_write_master_to_slave();
160 expect(signal_data_written);
161 end_write_master_to_slave();
162 expect(router_send_frame);
164 sent_data[sent_data_size + 21] = 0;
165 transport_recv_frame(0, sent_data, sent_data_size + 22);
166 test_object1_t* obj2 = read_master_to_slave();
167 assert_that(obj2, is_equal_to(NULL));