summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/glue/notif.cpp
blob: 5a03d34c12245c2e4ffe0601fa8b9c30bd5c9c53 (plain) (blame)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <algorithm>
#include <cstring>

#include "common/assert.h"
#include "common/logging/log.h"
#include "core/hle/service/cmif_serialization.h"
#include "core/hle/service/glue/notif.h"
#include "core/hle/service/ipc_helpers.h"
#include "core/hle/service/kernel_helpers.h"

namespace Service::Glue {

namespace {

constexpr inline std::size_t MaxAlarms = 8;

}

Result NotificationServiceImpl::RegisterAlarmSetting(AlarmSettingId* out_alarm_setting_id,
                                                     const AlarmSetting& alarm_setting,
                                                     std::span<const u8> application_parameter) {
    if (alarms.size() > MaxAlarms) {
        LOG_ERROR(Service_NOTIF, "Alarm limit reached");
        R_THROW(ResultUnknown);
    }

    ASSERT_MSG(application_parameter.size() <= sizeof(ApplicationParameter),
               "application_parameter_size is bigger than 0x400 bytes");

    AlarmSetting new_alarm = alarm_setting;
    new_alarm.alarm_setting_id = last_alarm_setting_id++;
    alarms.push_back(new_alarm);

    // TODO: Save application parameter data

    LOG_WARNING(Service_NOTIF,
                "(STUBBED) called, application_parameter_size={}, setting_id={}, kind={}, muted={}",
                application_parameter.size(), new_alarm.alarm_setting_id, new_alarm.kind,
                new_alarm.muted);

    *out_alarm_setting_id = new_alarm.alarm_setting_id;
    R_SUCCEED();
}

Result NotificationServiceImpl::UpdateAlarmSetting(const AlarmSetting& alarm_setting,
                                                   std::span<const u8> application_parameter) {
    ASSERT_MSG(application_parameter.size() <= sizeof(ApplicationParameter),
               "application_parameter_size is bigger than 0x400 bytes");

    const auto alarm_it = GetAlarmFromId(alarm_setting.alarm_setting_id);
    if (alarm_it != alarms.end()) {
        LOG_DEBUG(Service_NOTIF, "Alarm updated");
        *alarm_it = alarm_setting;
    }

    LOG_WARNING(Service_NOTIF,
                "(STUBBED) called, application_parameter_size={}, setting_id={}, kind={}, muted={}",
                application_parameter.size(), alarm_setting.alarm_setting_id, alarm_setting.kind,
                alarm_setting.muted);
    R_SUCCEED();
}

Result NotificationServiceImpl::ListAlarmSettings(s32* out_count,
                                                  std::span<AlarmSetting> out_alarms) {
    LOG_INFO(Service_NOTIF, "called, alarm_count={}", alarms.size());

    const auto count = std::min(out_alarms.size(), alarms.size());
    for (size_t i = 0; i < count; i++) {
        out_alarms[i] = alarms[i];
    }

    *out_count = static_cast<s32>(count);
    R_SUCCEED();
}

Result NotificationServiceImpl::LoadApplicationParameter(u32* out_size,
                                                         std::span<u8> out_application_parameter,
                                                         AlarmSettingId alarm_setting_id) {
    const auto alarm_it = GetAlarmFromId(alarm_setting_id);
    if (alarm_it == alarms.end()) {
        LOG_ERROR(Service_NOTIF, "Invalid alarm setting id={}", alarm_setting_id);
        R_THROW(ResultUnknown);
    }

    // TODO: Read application parameter related to this setting id
    ApplicationParameter application_parameter{};

    LOG_WARNING(Service_NOTIF, "(STUBBED) called, alarm_setting_id={}", alarm_setting_id);
    std::memcpy(out_application_parameter.data(), application_parameter.data(),
                std::min(sizeof(application_parameter), out_application_parameter.size()));

    *out_size = static_cast<u32>(application_parameter.size());
    R_SUCCEED();
}

Result NotificationServiceImpl::DeleteAlarmSetting(AlarmSettingId alarm_setting_id) {
    std::erase_if(alarms, [alarm_setting_id](const AlarmSetting& alarm) {
        return alarm.alarm_setting_id == alarm_setting_id;
    });

    LOG_INFO(Service_NOTIF, "called, alarm_setting_id={}", alarm_setting_id);

    R_SUCCEED();
}

Result NotificationServiceImpl::Initialize(u64 aruid) {
    // TODO: Load previous alarms from config

    LOG_WARNING(Service_NOTIF, "(STUBBED) called");
    R_SUCCEED();
}

std::vector<AlarmSetting>::iterator NotificationServiceImpl::GetAlarmFromId(
    AlarmSettingId alarm_setting_id) {
    return std::find_if(alarms.begin(), alarms.end(),
                        [alarm_setting_id](const AlarmSetting& alarm) {
                            return alarm.alarm_setting_id == alarm_setting_id;
                        });
}

INotificationServicesForApplication::INotificationServicesForApplication(Core::System& system_)
    : ServiceFramework{system_, "notif:a"} {
    // clang-format off
    static const FunctionInfo functions[] = {
        {500, D<&INotificationServicesForApplication::RegisterAlarmSetting>, "RegisterAlarmSetting"},
        {510, D<&INotificationServicesForApplication::UpdateAlarmSetting>, "UpdateAlarmSetting"},
        {520, D<&INotificationServicesForApplication::ListAlarmSettings>, "ListAlarmSettings"},
        {530, D<&INotificationServicesForApplication::LoadApplicationParameter>, "LoadApplicationParameter"},
        {540, D<&INotificationServicesForApplication::DeleteAlarmSetting>, "DeleteAlarmSetting"},
        {1000, D<&INotificationServicesForApplication::Initialize>, "Initialize"},
    };
    // clang-format on

    RegisterHandlers(functions);
}

INotificationServicesForApplication::~INotificationServicesForApplication() = default;

Result INotificationServicesForApplication::RegisterAlarmSetting(
    Out<AlarmSettingId> out_alarm_setting_id,
    InLargeData<AlarmSetting, BufferAttr_HipcMapAlias> alarm_setting,
    InBuffer<BufferAttr_HipcMapAlias> application_parameter) {
    R_RETURN(impl.RegisterAlarmSetting(out_alarm_setting_id.Get(), *alarm_setting,
                                       application_parameter));
}

Result INotificationServicesForApplication::UpdateAlarmSetting(
    InLargeData<AlarmSetting, BufferAttr_HipcMapAlias> alarm_setting,
    InBuffer<BufferAttr_HipcMapAlias> application_parameter) {
    R_RETURN(impl.UpdateAlarmSetting(*alarm_setting, application_parameter));
}

Result INotificationServicesForApplication::ListAlarmSettings(
    Out<s32> out_count, OutArray<AlarmSetting, BufferAttr_HipcMapAlias> out_alarms) {
    R_RETURN(impl.ListAlarmSettings(out_count.Get(), out_alarms));
}

Result INotificationServicesForApplication::LoadApplicationParameter(
    Out<u32> out_size, OutBuffer<BufferAttr_HipcMapAlias> out_application_parameter,
    AlarmSettingId alarm_setting_id) {
    R_RETURN(
        impl.LoadApplicationParameter(out_size.Get(), out_application_parameter, alarm_setting_id));
}

Result INotificationServicesForApplication::DeleteAlarmSetting(AlarmSettingId alarm_setting_id) {
    R_RETURN(impl.DeleteAlarmSetting(alarm_setting_id));
}

Result INotificationServicesForApplication::Initialize(ClientAppletResourceUserId aruid) {
    R_RETURN(impl.Initialize(*aruid));
}

class INotificationSystemEventAccessor final
    : public ServiceFramework<INotificationSystemEventAccessor> {
public:
    explicit INotificationSystemEventAccessor(Core::System& system_)
        : ServiceFramework{system_, "INotificationSystemEventAccessor"},
          service_context{system_, "INotificationSystemEventAccessor"} {
        // clang-format off
        static const FunctionInfo functions[] = {
            {0, D<&INotificationSystemEventAccessor::GetSystemEvent>, "GetSystemEvent"},
        };
        // clang-format on

        RegisterHandlers(functions);

        notification_event =
            service_context.CreateEvent("INotificationSystemEventAccessor:NotificationEvent");
    }

    ~INotificationSystemEventAccessor() {
        service_context.CloseEvent(notification_event);
    }

private:
    Result GetSystemEvent(OutCopyHandle<Kernel::KReadableEvent> out_readable_event) {
        LOG_WARNING(Service_NOTIF, "(STUBBED) called");

        *out_readable_event = &notification_event->GetReadableEvent();
        R_SUCCEED();
    }

    KernelHelpers::ServiceContext service_context;
    Kernel::KEvent* notification_event;
};

INotificationServices::INotificationServices(Core::System& system_)
    : ServiceFramework{system_, "notif:s"} {
    // clang-format off
    static const FunctionInfo functions[] = {
        {500, D<&INotificationServices::RegisterAlarmSetting>, "RegisterAlarmSetting"},
        {510, D<&INotificationServices::UpdateAlarmSetting>, "UpdateAlarmSetting"},
        {520, D<&INotificationServices::ListAlarmSettings>, "ListAlarmSettings"},
        {530, D<&INotificationServices::LoadApplicationParameter>, "LoadApplicationParameter"},
        {540, D<&INotificationServices::DeleteAlarmSetting>, "DeleteAlarmSetting"},
        {1000, D<&INotificationServices::Initialize>, "Initialize"},
        {1010, nullptr, "ListNotifications"},
        {1020, nullptr, "DeleteNotification"},
        {1030, nullptr, "ClearNotifications"},
        {1040, D<&INotificationServices::OpenNotificationSystemEventAccessor>, "OpenNotificationSystemEventAccessor"},
        {1500, nullptr, "SetNotificationPresentationSetting"},
        {1510, D<&INotificationServices::GetNotificationPresentationSetting>, "GetNotificationPresentationSetting"},
        {2000, nullptr, "GetAlarmSetting"},
        {2001, nullptr, "GetAlarmSettingWithApplicationParameter"},
        {2010, nullptr, "MuteAlarmSetting"},
        {2020, nullptr, "IsAlarmSettingReady"},
        {8000, nullptr, "RegisterAppletResourceUserId"},
        {8010, nullptr, "UnregisterAppletResourceUserId"},
        {8999, nullptr, "GetCurrentTime"},
        {9000, nullptr, "GetAlarmSettingNextNotificationTime"},
    };
    // clang-format on

    RegisterHandlers(functions);
}

INotificationServices::~INotificationServices() = default;

Result INotificationServices::RegisterAlarmSetting(
    Out<AlarmSettingId> out_alarm_setting_id,
    InLargeData<AlarmSetting, BufferAttr_HipcMapAlias> alarm_setting,
    InBuffer<BufferAttr_HipcMapAlias> application_parameter) {
    R_RETURN(impl.RegisterAlarmSetting(out_alarm_setting_id.Get(), *alarm_setting,
                                       application_parameter));
}

Result INotificationServices::UpdateAlarmSetting(
    InLargeData<AlarmSetting, BufferAttr_HipcMapAlias> alarm_setting,
    InBuffer<BufferAttr_HipcMapAlias> application_parameter) {
    R_RETURN(impl.UpdateAlarmSetting(*alarm_setting, application_parameter));
}

Result INotificationServices::ListAlarmSettings(
    Out<s32> out_count, OutArray<AlarmSetting, BufferAttr_HipcMapAlias> out_alarms) {
    R_RETURN(impl.ListAlarmSettings(out_count.Get(), out_alarms));
}

Result INotificationServices::LoadApplicationParameter(
    Out<u32> out_size, OutBuffer<BufferAttr_HipcMapAlias> out_application_parameter,
    AlarmSettingId alarm_setting_id) {
    R_RETURN(
        impl.LoadApplicationParameter(out_size.Get(), out_application_parameter, alarm_setting_id));
}

Result INotificationServices::DeleteAlarmSetting(AlarmSettingId alarm_setting_id) {
    R_RETURN(impl.DeleteAlarmSetting(alarm_setting_id));
}

Result INotificationServices::Initialize(ClientAppletResourceUserId aruid) {
    R_RETURN(impl.Initialize(*aruid));
}

Result INotificationServices::OpenNotificationSystemEventAccessor(
    Out<SharedPointer<INotificationSystemEventAccessor>> out_notification_system_event_accessor) {
    LOG_WARNING(Service_NOTIF, "(STUBBED) called");

    *out_notification_system_event_accessor =
        std::make_shared<INotificationSystemEventAccessor>(system);
    R_SUCCEED();
}

Result INotificationServices::GetNotificationPresentationSetting(
    Out<NotificationPresentationSetting> out_notification_presentation_setting,
    NotificationChannel notification_channel) {
    LOG_WARNING(Service_NOTIF, "(STUBBED) called");

    *out_notification_presentation_setting = {};
    R_SUCCEED();
}

} // namespace Service::Glue