~aleteoryx/muditaos

ref: f585c33b7afacce8fa10c33c44bab37e935b68d4 muditaos/module-services/service-desktop/include/service-desktop/ServiceDesktop.hpp -rw-r--r-- 6.5 KiB
f585c33b — Lefucjusz [BH-2002] Fix crash when connected with broken USB cable 1 year, 6 months 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <bsp/usb/usb.hpp>
#include "Constants.hpp"
#include "ServiceDesktopName.hpp"
#include "ServiceDesktopDependencies.hpp"
#include "USBSecurityModel.hpp"
#include "DeveloperModeMessage.hpp"
#include "DesktopMessages.hpp"
#include <locks/data/PhoneLockMessages.hpp>
#include <service-bluetooth/messages/Status.hpp>
#include <service-bluetooth/messages/BondedDevices.hpp>
#include <service-bluetooth/messages/ResponseVisibleDevices.hpp>
#include <service-desktop/Sync.hpp>
#include <service-desktop/OutboxNotifications.hpp>
#include <service-evtmgr/BatteryMessages.hpp>
#include <service-desktop/WorkerDesktop.hpp>

namespace settings
{
    class Settings;
}

namespace sdesktop
{
    using namespace std::chrono_literals;

    inline constexpr auto serviceStackSize = 1024 * 8;
    inline constexpr auto workerStackSize  = 1024 * 9;

    inline constexpr auto cdcReceiveQueueName          = "cdcReceiveQueueBuffer";
    inline constexpr auto cdcReceiveQueueItemSize      = sizeof(std::string *);
    inline constexpr auto cdcReceiveQueueLength        = 1024;

    inline constexpr auto cdcSendQueueName             = "cdcSendQueueBuffer";
    inline constexpr auto cdcSendQueueItemSize         = sizeof(std::string *);
    inline constexpr auto cdcSendQueueLength           = 1024;

    inline constexpr auto irqQueueName     = "irqQueueBuffer";
    inline constexpr auto irqQueueItemSize = sizeof(bsp::USBDeviceStatus);
    inline constexpr auto irqQueueLength   = 1;

    inline constexpr auto signallingQueueName     = "signallingQueueBuffer";
    inline constexpr auto signallingQueueItemSize = sizeof(WorkerDesktop::Signal);
    inline constexpr auto signallingQueueLength   = 1;

    inline constexpr auto connectionActiveTimerName    = "connectionActiveTimer";
    inline constexpr auto connectionActiveTimerDelayMs = 20s;

    inline constexpr auto deviceUniqueIdName   = "sd_device_unique_id";
    inline constexpr auto deviceUniqueIdLength = 32;

    inline constexpr auto pathFactoryDataSerial     = "factory_data/serial";
    inline constexpr auto pathFactoryDataCaseColour = "factory_data/case_colour";
}; // namespace sdesktop

namespace sdesktop::bluetooth
{
    class BluetoothMessagesHandler;
}

class ServiceDesktop : public sys::Service
{
  public:
    explicit ServiceDesktop(const std::filesystem::path &mtpRootPath);
    ~ServiceDesktop() override;

    std::unique_ptr<WorkerDesktop> desktopWorker;
    Sync::OperationStatus syncStatus;

    auto InitHandler() -> sys::ReturnCodes override;
    auto DeinitHandler() -> sys::ReturnCodes override;
    auto SwitchPowerModeHandler(sys::ServicePowerMode mode) -> sys::ReturnCodes override;
    auto DataReceivedHandler(sys::DataMessage *msg, sys::ResponseMessage *resp) -> sys::MessagePointer override;

    auto prepareSyncData() -> void;
    auto getSyncStatus() const -> Sync::OperationStatus
    {
        return syncStatus;
    }
    auto getSecurity() const -> const sdesktop::USBSecurityModel *
    {
        return usbSecurityModel.get();
    }

    auto requestLogsFlush() -> void;

    auto getSerialNumber() const -> std::string;
    auto getCaseColour() const -> std::string;
    auto getDeviceToken() -> std::string;

    auto getNotificationEntries() const -> std::vector<Outbox::NotificationEntry>;
    auto removeNotificationEntries(const std::vector<std::uint32_t> &uidsOfNotificationsToBeRemoved) -> void;
    auto getMtpPath() const noexcept -> std::filesystem::path;
    auto getOnboardingState() const -> sdesktop::endpoints::OnboardingState;

  private:
    std::unique_ptr<sdesktop::USBSecurityModel> usbSecurityModel;
    std::unique_ptr<settings::Settings> settings;
    std::unique_ptr<sdesktop::bluetooth::BluetoothMessagesHandler> btMsgHandler;
    OutboxNotifications outboxNotifications;
    sys::TimerHandle connectionActiveTimer;
    static constexpr unsigned int defaultLogFlushTimeoutMs   = 1000U;
    bool initialized                                         = false;
    bool isPlugEventUnhandled                                = false;
    bool isUsbConfigured                                     = false;
    std::filesystem::path mtpRootPath;

    auto generateDeviceUniqueId() -> void;
    auto getDeviceUniqueId() const -> std::string;
    auto setDeviceUniqueId(const std::string &token) -> void;

    auto usbWorkerInit() -> sys::ReturnCodes;
    auto usbWorkerDeinit() -> sys::ReturnCodes;

    auto restartConnectionActiveTimer() -> void;

    auto checkChargingCondition() -> void;

    auto cleanFileSystemEndpointUndeliveredTransfers() -> void;

    template <typename T>
    auto connectHandler() -> bool
    {
        return connect(typeid(T), [&](sys::Message *msg) { return handle(static_cast<T *>(msg)); });
    }

    [[nodiscard]] auto handle(db::NotificationMessage *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(locks::UnlockedPhone *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(locks::LockedPhone *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(locks::NextPhoneUnlockAttemptLockTime *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(message::bluetooth::ResponseStatus *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(message::bluetooth::ResponseBondedDevices *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(message::bluetooth::ResponseVisibleDevices *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(sdesktop::developerMode::DeveloperModeRequest *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(sdesktop::SyncMessage *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(sdesktop::FactoryMessage *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(sdesktop::usb::USBConfigured *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(sdesktop::usb::USBDisconnected *msg) -> std::shared_ptr<sys::Message>;
    [[nodiscard]] auto handle(sevm::USBPlugEvent *msg) -> std::shared_ptr<sys::Message>;
};

namespace sys
{
    template <>
    struct ManifestTraits<ServiceDesktop>
    {
        static auto GetManifest() -> ServiceManifest
        {
            ServiceManifest manifest;
            manifest.name         = service::name::service_desktop;
            manifest.dependencies = sys::dependencies::getDependenciesFor<ServiceDesktop>();
            return manifest;
        }
    };
} // namespace sys