param_request.c 6.5 KB
Newer Older
Z
zhong_ning 已提交
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
/*
 * 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 "param_request.h"

#include <string.h>
#include <unistd.h>
#include <time.h>

#include "param_manager.h"
#include "uv.h"

#define LABEL "Client"
#define BUFFER_SIZE  200
#define ParamEntry(ptr, type, member)   (type *)((char *)(ptr) - offsetof(type, member))

static ParamWorkSpace g_paramWorkSpaceReadOnly = {ATOMIC_VAR_INIT(0), {}, {}, {}};

static void OnWrite(uv_write_t *req, int status)
{
Z
zhong_ning 已提交
33
    PARAM_LOGD("OnWrite status %d", status);
Z
zhong_ning 已提交
34 35 36 37 38 39
}

static void OnReceiveAlloc(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf)
{
    // 这里需要按实际回复大小申请内存,不需要大内存
    buf->base = (char *)malloc(sizeof(ResponseMsg));
S
sun_fan 已提交
40
    PARAM_CHECK(buf->base != NULL, return, "OnReceiveAlloc malloc failed");
Z
zhong_ning 已提交
41
    buf->len = sizeof(ResponseMsg);
Z
zhong_ning 已提交
42
    PARAM_LOGD("OnReceiveAlloc handle %p %zu", handle, suggestedSize);
Z
zhong_ning 已提交
43 44 45 46 47
}

static void OnReceiveResponse(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf)
{
    RequestNode *req = ParamEntry(handle, RequestNode, handle);
Z
zhong_ning 已提交
48
    PARAM_LOGD("OnReceiveResponse %p", handle);
Z
zhong_ning 已提交
49 50 51 52 53 54 55 56 57 58 59 60
    if (nread <= 0 || buf == NULL || handle == NULL || buf->base == NULL) {
        if (buf != NULL && buf->base != NULL) {
            free(buf->base);
        }
        if (handle != NULL) {
            uv_close((uv_handle_t*)handle, NULL);
            uv_stop(req->loop);
        }
        return;
    }
    ResponseMsg *response = (ResponseMsg *)(buf->base);
    PARAM_CHECK(response != NULL, return, "The response is null");
Z
zhong_ning 已提交
61
    PARAM_LOGD("OnReceiveResponse %p cmd %d result: %d", handle, response->type, response->result);
Z
zhong_ning 已提交
62 63 64 65 66 67 68 69
    switch (response->type) {
        case SET_PARAM:
            req->result = response->result;
            break;
        default:
            PARAM_LOGE("not supported the command: %d", response->type);
            break;
    }
Z
zhong_ning 已提交
70
    PARAM_LOGD("Close handle %p", handle);
Z
zhong_ning 已提交
71 72 73 74 75 76 77 78 79
    free(buf->base);
    uv_close((uv_handle_t*)handle, NULL);
    uv_stop(req->loop);
}

static void OnConnection(uv_connect_t *connect, int status)
{
    PARAM_CHECK(status >= 0, return, "Failed to conntect status %s", uv_strerror(status));
    RequestNode *request = ParamEntry(connect, RequestNode, connect);
Z
zhong_ning 已提交
80
    PARAM_LOGD("Connect to server handle %p", &(request->handle));
Z
zhong_ning 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    uv_buf_t buf = uv_buf_init((char*)&request->msg, request->msg.contentSize + sizeof(request->msg));
    int ret = uv_write2(&request->wr, (uv_stream_t*)&(request->handle), &buf, 1, (uv_stream_t*)&(request->handle), OnWrite);
    PARAM_CHECK(ret >= 0, return, "Failed to uv_write2 porperty");

    // read result
    ret = uv_read_start((uv_stream_t*)&(request->handle), OnReceiveAlloc, OnReceiveResponse);
    PARAM_CHECK(ret >= 0, return, "Failed to uv_read_start response");
}

static int StartRequest(int cmd, RequestNode *request)
{
    PARAM_CHECK(request != NULL, return -1, "Invalid request");
    request->result = -1;
    request->msg.type = cmd;
    request->loop = uv_loop_new();
S
sun_fan 已提交
96
    PARAM_CHECK(request->loop != NULL, return -1, "StartRequest uv_loop_new failed");
Z
zhong_ning 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    uv_pipe_init(request->loop, &request->handle, 1);
    uv_pipe_connect(&request->connect, &request->handle, PIPE_NAME, OnConnection);
    uv_run(request->loop, UV_RUN_DEFAULT);
    uv_loop_delete(request->loop);
    int result = request->result;
    free(request);
    return result;
}

int SystemSetParameter(const char *name, const char *value)
{
    PARAM_CHECK(name != NULL && value != NULL, return -1, "Invalid param");
    int ret = CheckParamName(name, 0);
    PARAM_CHECK(ret == 0, return ret, "Illegal param name");

Z
zhong_ning 已提交
112
    PARAM_LOGD("StartRequest %s", name);
Z
zhong_ning 已提交
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
    u_int32_t msgSize = sizeof(RequestMsg) + strlen(name) + strlen(value) + 2;
    RequestNode *request = (RequestNode *)malloc(sizeof(RequestNode) + msgSize);
    PARAM_CHECK(request != NULL, return -1, "Failed to malloc for connect");

    memset_s(request, sizeof(RequestNode), 0, sizeof(RequestNode));
    // 带字符串结束符
    int contentSize = BuildParamContent(request->msg.content, msgSize - sizeof(RequestMsg), name, value);
    PARAM_CHECK(contentSize > 0, free(request); return -1, "Failed to copy porperty");
    request->msg.contentSize = contentSize;
    return StartRequest(SET_PARAM, request);
}

int SystemGetParameter(const char *name, char *value, unsigned int *len)
{
    PARAM_CHECK(name != NULL && len != NULL, return -1, "The name or value is null");
    InitParamWorkSpace(&g_paramWorkSpaceReadOnly, 1, NULL);

    ParamHandle handle = 0;
    int ret = ReadParamWithCheck(&g_paramWorkSpaceReadOnly, name, &handle);
    PARAM_CHECK(ret == 0, return ret, "Can not get param for %s", name);
    return ReadParamValue(&g_paramWorkSpaceReadOnly, handle, value, len);
}

int SystemGetParameterName(ParamHandle handle, char *name, unsigned int len)
{
    PARAM_CHECK(name != NULL && handle != 0, return -1, "The name is null");
    InitParamWorkSpace(&g_paramWorkSpaceReadOnly, 1, NULL);
    return ReadParamName(&g_paramWorkSpaceReadOnly, handle, name, len);
}

int SystemGetParameterValue(ParamHandle handle, char *value, unsigned int *len)
{
    PARAM_CHECK(len != NULL && handle != 0, return -1, "The value is null");
    InitParamWorkSpace(&g_paramWorkSpaceReadOnly, 1, NULL);
    return ReadParamValue(&g_paramWorkSpaceReadOnly, handle, value, len);
}

int SystemTraversalParameter(void (*traversalParameter)(ParamHandle handle, void* cookie), void* cookie)
{
    PARAM_CHECK(traversalParameter != NULL, return -1, "The param is null");
    InitParamWorkSpace(&g_paramWorkSpaceReadOnly, 1, NULL);
    return TraversalParam(&g_paramWorkSpaceReadOnly, traversalParameter, cookie);
}

const char *SystemDetectParamChange(ParamCache *cache,
    ParamEvaluatePtr evaluate, u_int32_t count, const char *parameters[][2])
{
    PARAM_CHECK(cache != NULL && evaluate != NULL && parameters != NULL, return NULL, "The param is null");
    return DetectParamChange(&g_paramWorkSpaceReadOnly, cache, evaluate, count, parameters);
}