init_reboot_innerkits.c 2.6 KB
Newer Older
X
add ut  
xionglei6 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "init_reboot.h"

#include <string.h>
#include <sys/types.h>
#include <unistd.h>

X
xionglei6 已提交
21
#include "beget_ext.h"
X
add ut  
xionglei6 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include "param.h"
#include "securec.h"
#include "sys_param.h"

// Refer to parameter limit, value size should not bigger than 96
#define MAX_REBOOT_OPTION_SIZE PARAM_VALUE_LEN_MAX

#ifndef STARTUP_INIT_TEST
#define DOREBOOT_PARAM "ohos.startup.powerctrl"
#else
#define DOREBOOT_PARAM "reboot.ut"
#endif

int DoReboot(const char *option)
{
    char value[MAX_REBOOT_OPTION_SIZE];
X
xionglei6 已提交
38
    int ret = 0;
X
add ut  
xionglei6 已提交
39
    if (option == NULL || strlen(option) == 0) {
X
xionglei6 已提交
40 41 42 43
        ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP);
        BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param");
        ret = SystemSetParameter(DOREBOOT_PARAM, "reboot");
        BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set reboot param");
X
add ut  
xionglei6 已提交
44 45 46
        return 0;
    }
    int length = strlen(option);
X
xionglei6 已提交
47
    BEGET_ERROR_CHECK(length <= MAX_REBOOT_OPTION_SIZE, return -1,
X
add ut  
xionglei6 已提交
48
        "Reboot option \" %s \" is too large, overflow", option);
X
xionglei6 已提交
49 50 51 52
    ret = snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "reboot,%s", option);
    BEGET_ERROR_CHECK(ret >= 0, return -1, "Failed to copy boot option \" %s \"", option);

    if (strcmp(option, DEVICE_CMD_SUSPEND) == 0) {
X
xionglei6 已提交
53
        ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP);
X
xionglei6 已提交
54 55
        BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param");
    } else if (strcmp(option, DEVICE_CMD_FREEZE) == 0) {
X
xionglei6 已提交
56
        ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP);
X
xionglei6 已提交
57 58 59 60 61 62 63
        BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param");
    } else {
        ret = SystemSetParameter(STARTUP_DEVICE_CTL, DEVICE_CMD_STOP);
        BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to set stop param");
    }
    ret = SystemSetParameter(DOREBOOT_PARAM, value);
    BEGET_ERROR_CHECK(ret == 0, return -1, "Set parameter to trigger reboot command \" %s \" failed", value);
X
add ut  
xionglei6 已提交
64 65
    return 0;
}