~aleteoryx/muditaos

ref: 2f74db29a6442a9f1b6e78334ea9da72cd0dff2d muditaos/module-bsp/board/linux/rtc/rtc.cpp -rw-r--r-- 3.2 KiB
2f74db29 — Lefucjusz [MOS-608] Fix crash on phone turn off 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <bsp/rtc/rtc.hpp>
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include <time.h>

namespace
{
    time_t timestampOffset;
    time_t timestampAlarm;
    time_t localOffset;
    xQueueHandle qHandleRtcIrq   = NULL;
    TaskHandle_t rtcWorkerHandle = NULL;

    void rtcWorker(void *pvp)
    {
        while (true) {
            time_t current;
            bsp::rtc::getCurrentTimestamp(&current);

            std::uint8_t notification;
            if (current == timestampAlarm) {
                notification = static_cast<std::uint8_t>(bsp::rtc::IrqNotification::AlarmOccurred);
                xQueueSend(qHandleRtcIrq, &notification, 100);
            }
            vTaskDelay(1000);
        }
    }
} // namespace

namespace bsp::rtc
{
    ErrorCode init(xQueueHandle qHandle)
    {
        qHandleRtcIrq   = qHandle;
        timestampOffset = 0;
        timestampAlarm  = 0;

        xTaskCreate(rtcWorker, "rtc_worker", 512, qHandle, 0, &rtcWorkerHandle);
        return ErrorCode::OK;
    }

    ErrorCode setDateTimeFromTimestamp(time_t timestamp)
    {
        time_t current   = time(NULL);
        struct tm *local = localtime(&current);

        localOffset = local->tm_gmtoff;

        timestampOffset = timestamp - current;
        return ErrorCode::OK;
    }

    ErrorCode setDateTime(struct tm *tim)
    {

        time_t current   = time(NULL);
        time_t timestamp = mktime(tim);

        timestampOffset = timestamp - current;
        return ErrorCode::OK;
    }

    ErrorCode getCurrentDateTime(struct tm *datetime)
    {
        time_t t = time(NULL);
        t += timestampOffset;
        *datetime = *gmtime(&t);

        return ErrorCode::OK;
    }

    ErrorCode getCurrentTimestamp(time_t *timestamp)
    {
        *timestamp = time(NULL) + timestampOffset;

        return ErrorCode::OK;
    }

    ErrorCode setAlarmOnDate(struct tm *datetime)
    {
        if (datetime == NULL) {
            return ErrorCode::Error;
        }

        return ErrorCode::OK;
    }

    ErrorCode setAlarmOnTimestamp(std::uint32_t secs)
    {
        timestampAlarm = secs;
        return ErrorCode::OK;
    }

    ErrorCode setAlarmInSecondsFromNow(std::uint32_t secs)
    {
        time_t current;
        if (getCurrentTimestamp(&current) != ErrorCode::OK)
            return ErrorCode::Error;

        current += secs;

        if (setAlarmOnTimestamp(current) != ErrorCode::OK)
            return ErrorCode::Error;

        return ErrorCode::OK;
    }

    ErrorCode getAlarmTimestamp(std::uint32_t *secs)
    {
        if (secs == NULL) {
            return ErrorCode::Error;
        }

        return ErrorCode::OK;
    }

    ErrorCode enableAlarmIrq()
    {
        std::uint32_t cnt = 100000;

        if (cnt == 0) {
            return ErrorCode::Error;
        }

        return ErrorCode::OK;
    }

    ErrorCode setMinuteAlarm(time_t timestamp)
    {
        std::uint32_t secondsToMinute = 60 - (timestamp % 60);

        struct tm date;
        getCurrentDateTime(&date);

        return setAlarmInSecondsFromNow(secondsToMinute);
    }
} // namespace bsp::rtc