ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
17#pragma GCC diagnostic ignored "-Wmissing-prototypes"
18#pragma GCC diagnostic ignored "-Wmissing-declarations"
19
20#include <libents/util/time.h>
21#include <libtock-sync/peripherals/rtc.h>
22#include <libtock-sync/services/alarm.h>
23#include <stdio.h>
24#include <string.h>
25#include <unity.h>
26
27static const int delay = 10;
28static const int delay_ms = delay * 1000;
29
33void setUp(void) {}
34
38void tearDown(void) {}
39
40void test_rtc_exists(void) {
41 bool exists = libtock_rtc_exists();
42 TEST_ASSERT(exists);
43}
44
45void test_set_date(void) {
46 int ret = 0;
47
48 // Initialises a date struct with a certain timestamp
49 libtock_rtc_date_t date = {.year = 2023,
50 .month = JANUARY,
51 .day = 1,
52
53 .day_of_week = MONDAY,
54 .hour = 12,
55 .minute = 30,
56 .seconds = 1};
57
58 ret = libtocksync_rtc_set_date(&date);
59
60 TEST_ASSERT_EQUAL(0, ret);
61}
62
63void test_get_date(void) {
64 int ret = 0;
65 libtock_rtc_date_t date = {};
66
67 ret = libtocksync_rtc_get_date(&date);
68
69 TEST_ASSERT_EQUAL(0, ret);
70
71 TEST_ASSERT_EQUAL(2023, date.year);
72 TEST_ASSERT_EQUAL(JANUARY, date.month);
73 TEST_ASSERT_EQUAL(1, date.day);
74 TEST_ASSERT_EQUAL(MONDAY, date.day_of_week);
75 TEST_ASSERT_EQUAL(12, date.hour);
76 TEST_ASSERT_EQUAL(30, date.minute);
77 TEST_ASSERT_INT_WITHIN(2, 1, date.seconds);
78}
79
81 int ret = 0;
82 libtock_rtc_date_t date = {};
83
84 libtocksync_alarm_delay_ms(delay_ms);
85
86 ret = libtocksync_rtc_get_date(&date);
87
88 TEST_ASSERT_EQUAL(0, ret);
89
90 TEST_ASSERT_EQUAL(2023, date.year);
91 TEST_ASSERT_EQUAL(JANUARY, date.month);
92 TEST_ASSERT_EQUAL(1, date.day);
93 TEST_ASSERT_EQUAL(MONDAY, date.day_of_week);
94 TEST_ASSERT_EQUAL(12, date.hour);
95 TEST_ASSERT_EQUAL(30 + (delay / 60), date.minute);
96 TEST_ASSERT_INT_WITHIN(2, (1 + delay) % 60, date.seconds);
97}
98
99void test_time_epoch(void) {
100 uint32_t epoch_set = 1785878164;
101 set_epoch(epoch_set);
102 uint32_t epoch_get = epoch();
103
104 TEST_ASSERT_INT_WITHIN(2, epoch_set, epoch_get);
105}
106
108 libtock_rtc_date_t date = {.year = 2026,
109 .month = AUGUST,
110 .day = 4,
111
112 .day_of_week = TUESDAY,
113 .hour = 21,
114 .minute = 7,
115 .seconds = 49};
116
117 uint32_t epoch = rtc_date_to_epoch(&date);
118
119 TEST_ASSERT_EQUAL(1785877669, epoch);
120}
121
123 libtock_rtc_date_t date = {};
124
125 uint32_t epoch = 1785877669;
126
127 epoch_to_rtc_date(epoch, &date);
128
129 TEST_ASSERT_EQUAL(2026, date.year);
130 TEST_ASSERT_EQUAL(AUGUST, date.month);
131 TEST_ASSERT_EQUAL(4, date.day);
132 // TEST_ASSERT_EQUAL(TUESDAY, date.day_of_week);
133 TEST_ASSERT_EQUAL(21, date.hour);
134 TEST_ASSERT_EQUAL(7, date.minute);
135 TEST_ASSERT_EQUAL(49, date.seconds);
136}
137
138int main(void) {
139 UNITY_BEGIN();
140
141 RUN_TEST(test_rtc_exists);
142 RUN_TEST(test_set_date);
143 RUN_TEST(test_get_date);
144 RUN_TEST(test_get_date_delay);
145 RUN_TEST(test_rtc_date_to_epoch);
146 RUN_TEST(test_epoch_to_rtc_date);
147 RUN_TEST(test_time_epoch);
148
149 return UNITY_END();
150}
int main()
Definition main.c:4
uint32_t epoch(void)
Gets the current unix epoch timestamp.
Definition time.c:19
void setUp(void)
Definition main.c:24
void tearDown(void)
Definition main.c:26
void test_rtc_date_to_epoch(void)
Definition main.c:107
void test_set_date(void)
Definition main.c:45
void test_rtc_exists(void)
Definition main.c:40
void test_get_date_delay(void)
Definition main.c:80
void test_epoch_to_rtc_date(void)
Definition main.c:122
void test_time_epoch(void)
Definition main.c:99
void test_get_date(void)
Definition main.c:63
void set_epoch(uint32_t epoch)
Sets the current unix epoch timestamp.
Definition time.c:25
uint32_t rtc_date_to_epoch(const libtock_rtc_date_t *date)
Converts tock time to unix epoch time.
Definition time.c:31
void epoch_to_rtc_date(uint32_t epoch, libtock_rtc_date_t *date)
Convert unix epoch to tock time.
Definition time.c:65