~aleteoryx/muditaos

6ed406974a0de6ff28b0d1cf9f15cff4ddc527fd — Lukasz Mastalerz 2 years ago 85cd9dd
[CP-1797] Low battery connection with mc is possible

Add a reason field to USBSecurityEndpoint reson when device is locked.
M doc/os_api/endpoints/pure/security_endpoint.md => doc/os_api/endpoints/pure/security_endpoint.md +32 -4
@@ 32,16 32,44 @@ Security endpoint (13)

```json
{
  "endpoint":13,
  "status":204,
  "uuid":123
  "endpoint": 13,
  "status": 204,
  "uuid": 123
}
```

Parameters:
 - *status* - 423 when the EULA is not accepted or battery level is critical otherwise 204 when phone is unlocked or 403 when phone is locked

- *status* - 204 when phone is unlocked or 403 when phone is locked

**Response Payload Structure**

```json
{
  "endpoint": 13,
  "body": {
    "reason": "2"
  },
  "status": 423,
  "uuid": 123
}
```

Parameters:

- *status* - 423 when phone is unlocked or 403 when phone is locked
- *reason* - block reason, see code snippets below

```c++
    enum class BlockReason
    {
        EulaNotAccepted = 2,
        BatteryCriticalLevel
    };
```

Access to all edpoints is blocked when:

- device is locked - 403 status is returned
- EULA is not accepted - 423 status is returned
- Critical battery level - 423 status is returned

M module-services/service-desktop/endpoints/include/endpoints/security/SecurityEndpointHelper.hpp => module-services/service-desktop/endpoints/include/endpoints/security/SecurityEndpointHelper.hpp +2 -2
@@ 1,4 1,4 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once


@@ 23,7 23,7 @@ namespace sdesktop::endpoints
        auto processPut(Context &context) -> ProcessResult final;
        auto processGet(Context &context) -> ProcessResult final;

        auto processStatus(Context &context) -> http::Code;
        auto processStatus(Context &context) -> ResponseContext;
        auto getPhoneLockTime(Context &context) -> time_t;
        auto processConfiguration(Context &context) -> http::Code;


M module-services/service-desktop/endpoints/security/SecurityEndpointHelper.cpp => module-services/service-desktop/endpoints/security/SecurityEndpointHelper.cpp +9 -9
@@ 29,7 29,7 @@ namespace sdesktop::endpoints
    auto SecurityEndpointHelper::processGet(Context &context) -> ProcessResult
    {
        if (context.getBody()[json::messages::category].string_value() == json::usb::phoneLockStatus) {
            return {sent::no, ResponseContext{.status = processStatus(context)}};
            return {sent::no, processStatus(context)};
        }
        if (context.getBody()[json::messages::category].string_value() == json::usb::phoneLockTime) {
            if (auto phoneLockTime = getPhoneLockTime(context); phoneLockTime > std::time(nullptr)) {


@@ 48,31 48,31 @@ namespace sdesktop::endpoints
        return {sent::no, ResponseContext{.status = http::Code::BadRequest}};
    }

    auto SecurityEndpointHelper::processStatus(Context & /*context*/) -> http::Code
    auto SecurityEndpointHelper::processStatus(Context & /*context*/) -> ResponseContext
    {
        auto result         = http::Code::NoContent;
        auto desktopService = dynamic_cast<ServiceDesktop *>(owner);
        auto security       = desktopService->getSecurity()->getEndpointSecurity();
        ResponseContext responseContext{};

        if (security.access == EndpointSecurity::Allow) {
            preventBlockingDevice();
            result = http::Code::NoContent;
            responseContext.status = http::Code::NoContent;
        }
        else {
            switch (security.reason) {
            case BlockReason::NoReason:
            case BlockReason::DeviceLocked:
                result = http::Code::Forbidden;
                responseContext.status = http::Code::Forbidden;
                break;
            case BlockReason::EulaNotAccepted:
                result = http::Code::Locked;
                break;
            case BlockReason::BatteryCriticalLevel:
                result = http::Code::Locked;
                responseContext.status = http::Code::Locked;
                responseContext.body =
                    json11::Json::object({{json::reason, std::to_string(static_cast<int>(security.reason))}});
                break;
            }
        }
        return result;
        return responseContext;
    }

    auto SecurityEndpointHelper::getPhoneLockTime(Context & /*context*/) -> time_t