/*
 * Copyright (c) Likewise Software.  All rights Reserved.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the license, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.  You should have received a copy
 * of the GNU Lesser General Public License along with this program.  If
 * not, see <http://www.gnu.org/licenses/>.
 *
 * LIKEWISE SOFTWARE MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING
 * TERMS AS WELL.  IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT
 * WITH LIKEWISE SOFTWARE, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE
 * TERMS OF THAT SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE GNU
 * LESSER GENERAL PUBLIC LICENSE, NOTWITHSTANDING THE ABOVE NOTICE.  IF YOU
 * HAVE QUESTIONS, OR WISH TO REQUEST A COPY OF THE ALTERNATE LICENSING
 * TERMS OFFERED BY LIKEWISE SOFTWARE, PLEASE CONTACT LIKEWISE SOFTWARE AT
 * license@likewisesoftware.com
 */

/*
 * Module Name:
 *
 *        ipc-protocol.c
 *
 * Abstract:
 *
 *        IPC protocol definitions
 *
 * Authors: Brian Koropoff (bkoropoff@likewise.com)
 *
 */

#include "includes.h"

#define LWMSG_MEMBER_PWSTR(_type, _field)           \
    LWMSG_MEMBER_POINTER_BEGIN(_type, _field),      \
    LWMSG_UINT16(WCHAR),                            \
    LWMSG_POINTER_END,                              \
    LWMSG_ATTR_ZERO_TERMINATED,                     \
    LWMSG_ATTR_ENCODING("utf-16")

#define LWMSG_PWSTR                    \
    LWMSG_POINTER_BEGIN,               \
    LWMSG_UINT16(WCHAR),               \
    LWMSG_POINTER_END,                 \
    LWMSG_ATTR_ZERO_TERMINATED,        \
    LWMSG_ATTR_ENCODING("utf-16")


static LWMsgTypeSpec gBooleanSpec[] =
{
    LWMSG_ENUM_BEGIN(BOOLEAN, 1, LWMSG_UNSIGNED),
    LWMSG_ENUM_NAMED_VALUE("FALSE", FALSE),
    LWMSG_ENUM_NAMED_VALUE("TRUE", TRUE),
    LWMSG_ENUM_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gStringSpec[] =
{
    LWMSG_PWSTR,
    LWMSG_ATTR_NOT_NULL,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gStringListSpec[] =
{
    LWMSG_POINTER_BEGIN,
    LWMSG_PWSTR,
    LWMSG_ATTR_NOT_NULL,
    LWMSG_POINTER_END,
    LWMSG_ATTR_ZERO_TERMINATED,
    LWMSG_ATTR_NOT_NULL,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gExistingHandleSpec[] =
{
    LWMSG_HANDLE(LW_SERVICE_HANDLE),
    LWMSG_ATTR_HANDLE_LOCAL_FOR_RECEIVER,
    LWMSG_ATTR_NOT_NULL,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gOptionalHandleSpec[] =
{
    LWMSG_HANDLE(LW_SERVICE_HANDLE),
    LWMSG_ATTR_HANDLE_LOCAL_FOR_RECEIVER,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gNewHandleSpec[] =
{
    LWMSG_HANDLE(LW_SERVICE_HANDLE),
    LWMSG_ATTR_HANDLE_LOCAL_FOR_SENDER,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gErrorSpec[] =
{
    LWMSG_UINT32(DWORD),
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gServiceStateSpec[] =
{
    LWMSG_ENUM_BEGIN(LW_SERVICE_STATE, 1, LWMSG_UNSIGNED),
    LWMSG_ENUM_VALUE(LW_SERVICE_STATE_STOPPED),
    LWMSG_ENUM_VALUE(LW_SERVICE_STATE_STARTING),
    LWMSG_ENUM_VALUE(LW_SERVICE_STATE_RUNNING),
    LWMSG_ENUM_VALUE(LW_SERVICE_STATE_STOPPING),
    LWMSG_ENUM_VALUE(LW_SERVICE_STATE_DEAD),
    LWMSG_ENUM_VALUE(LW_SERVICE_STATE_FAILED),
    LWMSG_ENUM_VALUE(LW_SERVICE_STATE_STUCK),
    LWMSG_ENUM_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gLogLevelSpec[] =
{
    LWMSG_ENUM_BEGIN(LW_SM_LOG_LEVEL, 1, LWMSG_UNSIGNED),
    LWMSG_ENUM_VALUE(LW_SM_LOG_LEVEL_DEFAULT),
    LWMSG_ENUM_VALUE(LW_SM_LOG_LEVEL_ALWAYS),
    LWMSG_ENUM_VALUE(LW_SM_LOG_LEVEL_ERROR),
    LWMSG_ENUM_VALUE(LW_SM_LOG_LEVEL_WARNING),
    LWMSG_ENUM_VALUE(LW_SM_LOG_LEVEL_INFO),
    LWMSG_ENUM_VALUE(LW_SM_LOG_LEVEL_VERBOSE),
    LWMSG_ENUM_VALUE(LW_SM_LOG_LEVEL_DEBUG),
    LWMSG_ENUM_VALUE(LW_SM_LOG_LEVEL_TRACE),
    LWMSG_ENUM_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gLogTypeSpec[] =
{
    LWMSG_ENUM_BEGIN(LW_SM_LOGGER_TYPE, 1, LWMSG_UNSIGNED),
    LWMSG_ENUM_VALUE(LW_SM_LOGGER_DEFAULT),
    LWMSG_ENUM_VALUE(LW_SM_LOGGER_NONE),
    LWMSG_ENUM_VALUE(LW_SM_LOGGER_SYSLOG),
    LWMSG_ENUM_VALUE(LW_SM_LOGGER_FILE),
    LWMSG_ENUM_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gServiceInfoSpec[] =
{
    LWMSG_STRUCT_BEGIN(LW_SERVICE_INFO),
    LWMSG_MEMBER_PWSTR(LW_SERVICE_INFO, pwszName),
    LWMSG_MEMBER_PWSTR(LW_SERVICE_INFO, pwszDescription),
    LWMSG_MEMBER_PWSTR(LW_SERVICE_INFO, pwszPath),
    LWMSG_ATTR_NOT_NULL,
    LWMSG_MEMBER_TYPESPEC(LW_SERVICE_INFO, ppwszArgs, gStringListSpec),
    LWMSG_MEMBER_TYPESPEC(LW_SERVICE_INFO, ppwszDependencies, gStringListSpec),
    LWMSG_MEMBER_PWSTR(LW_SERVICE_INFO, pwszGroup),
    LWMSG_MEMBER_TYPESPEC(LW_SERVICE_INFO, ppCategories, gStringListSpec),
    LWMSG_MEMBER_UINT32(LW_SERVICE_INFO, dwFdLimit),
    LWMSG_MEMBER_TYPESPEC(LW_SERVICE_INFO, ppCheckCommand, gStringListSpec),
    LWMSG_STRUCT_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gWaitStateChangeSpec[] =
{
    LWMSG_STRUCT_BEGIN(SM_IPC_WAIT_STATE_CHANGE_REQ),
    LWMSG_MEMBER_TYPESPEC(SM_IPC_WAIT_STATE_CHANGE_REQ, hHandle, gExistingHandleSpec),
    LWMSG_MEMBER_TYPESPEC(SM_IPC_WAIT_STATE_CHANGE_REQ, state, gServiceStateSpec),
    LWMSG_STRUCT_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gServiceStatusSpec[] =
{
    LWMSG_STRUCT_BEGIN(LW_SERVICE_STATUS),
    LWMSG_MEMBER_TYPESPEC(LW_SERVICE_STATUS, state, gServiceStateSpec),
    LWMSG_MEMBER_UINT32(LW_SERVICE_STATUS, Error),
    LWMSG_MEMBER_INT32(LW_SERVICE_STATUS, pid),
    LWMSG_STRUCT_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gSetLogInfoSpec[] =
{
    LWMSG_STRUCT_BEGIN(SM_SET_LOG_INFO_REQ),
    LWMSG_MEMBER_TYPESPEC(SM_SET_LOG_INFO_REQ, hHandle, gOptionalHandleSpec),
    LWMSG_MEMBER_PSTR(SM_SET_LOG_INFO_REQ, pFacility),
    LWMSG_MEMBER_TYPESPEC(SM_SET_LOG_INFO_REQ, type, gLogTypeSpec),
    LWMSG_MEMBER_PSTR(SM_SET_LOG_INFO_REQ, pszTarget),
    LWMSG_STRUCT_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gSetLogLevelSpec[] =
{
    LWMSG_STRUCT_BEGIN(SM_SET_LOG_LEVEL_REQ),
    LWMSG_MEMBER_TYPESPEC(SM_SET_LOG_LEVEL_REQ, hHandle, gOptionalHandleSpec),
    LWMSG_MEMBER_PSTR(SM_SET_LOG_LEVEL_REQ, pFacility),
    LWMSG_MEMBER_TYPESPEC(SM_SET_LOG_LEVEL_REQ, Level, gLogLevelSpec),
    LWMSG_STRUCT_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gGetLogStateReqSpec[] =
{
    LWMSG_STRUCT_BEGIN(SM_GET_LOG_STATE_REQ),
    LWMSG_MEMBER_TYPESPEC(SM_GET_LOG_STATE_REQ, hHandle, gOptionalHandleSpec),
    LWMSG_MEMBER_PSTR(SM_GET_LOG_STATE_REQ, pFacility),
    LWMSG_STRUCT_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gGetLogStateResSpec[] =
{
    LWMSG_STRUCT_BEGIN(SM_GET_LOG_STATE_RES),
    LWMSG_MEMBER_TYPESPEC(SM_GET_LOG_STATE_RES, type, gLogTypeSpec),
    LWMSG_MEMBER_PSTR(SM_GET_LOG_STATE_RES, pszTarget),
    LWMSG_MEMBER_TYPESPEC(SM_GET_LOG_STATE_RES, Level, gLogLevelSpec),
    LWMSG_STRUCT_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gGlobalSettingSpec[] =
{
    LWMSG_ENUM_BEGIN(LW_SM_GLOBAL_SETTING, 1, LWMSG_UNSIGNED),
    LWMSG_ENUM_VALUE(LW_SM_GLOBAL_SETTING_WATCHDOG),
    LWMSG_ENUM_VALUE(LW_SM_GLOBAL_SETTING_STUCK_TIMEOUT),
    LWMSG_ENUM_VALUE(LW_SM_GLOBAL_SETTING_RESTART_LIMIT),
    LWMSG_ENUM_VALUE(LW_SM_GLOBAL_SETTING_RESTART_PERIOD),
    LWMSG_ENUM_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gGlobalTypeSpec[] =
{
    LWMSG_ENUM_BEGIN(SM_GLOBAL_TYPE, 1, LWMSG_UNSIGNED),
    LWMSG_ENUM_VALUE(SM_GLOBAL_TYPE_BOOLEAN),
    LWMSG_ENUM_VALUE(SM_GLOBAL_TYPE_DWORD),
    LWMSG_ENUM_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gGlobalUnionSpec[] =
{
    LWMSG_UNION_BEGIN(SM_GLOBAL_UNION),
    LWMSG_MEMBER_TYPESPEC(SM_GLOBAL_UNION, Boolean, gBooleanSpec),
    LWMSG_ATTR_TAG(SM_GLOBAL_TYPE_BOOLEAN),
    LWMSG_MEMBER_UINT32(SM_GLOBAL_UNION, Dword),
    LWMSG_ATTR_TAG(SM_GLOBAL_TYPE_DWORD),
    LWMSG_UNION_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gGlobalValueSpec[] =
{
    LWMSG_STRUCT_BEGIN(SM_GLOBAL_VALUE),
    LWMSG_MEMBER_TYPESPEC(SM_GLOBAL_VALUE, Type, gGlobalTypeSpec),
    LWMSG_MEMBER_TYPESPEC(SM_GLOBAL_VALUE, Value, gGlobalUnionSpec),
    LWMSG_ATTR_DISCRIM(SM_GLOBAL_VALUE, Type),
    LWMSG_STRUCT_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gSetGlobalReqSpec[] =
{
    LWMSG_STRUCT_BEGIN(SM_SET_GLOBAL_REQ),
    LWMSG_MEMBER_TYPESPEC(SM_SET_GLOBAL_REQ, Setting, gGlobalSettingSpec),
    LWMSG_MEMBER_TYPESPEC(SM_SET_GLOBAL_REQ, Value, gGlobalValueSpec),
    LWMSG_STRUCT_END,
    LWMSG_TYPE_END
};

static LWMsgTypeSpec gGetGlobalReqSpec[] =
{
    LWMSG_STRUCT_BEGIN(SM_GET_GLOBAL_REQ),
    LWMSG_MEMBER_TYPESPEC(SM_GET_GLOBAL_REQ, Setting, gGlobalSettingSpec),
    LWMSG_STRUCT_END,
    LWMSG_TYPE_END
};

static LWMsgProtocolSpec gIpcSpec[] =
{
    LWMSG_MESSAGE(SM_IPC_ERROR, gErrorSpec),
    LWMSG_MESSAGE(SM_IPC_ACQUIRE_SERVICE_HANDLE_REQ, gStringSpec),
    LWMSG_MESSAGE(SM_IPC_ACQUIRE_SERVICE_HANDLE_RES, gNewHandleSpec),
    LWMSG_MESSAGE(SM_IPC_RELEASE_SERVICE_HANDLE_REQ, gExistingHandleSpec),
    LWMSG_MESSAGE(SM_IPC_RELEASE_SERVICE_HANDLE_RES, NULL),
    LWMSG_MESSAGE(SM_IPC_ENUMERATE_SERVICES_REQ, NULL),
    LWMSG_MESSAGE(SM_IPC_ENUMERATE_SERVICES_RES, gStringListSpec),
    LWMSG_MESSAGE(SM_IPC_ADD_SERVICE_REQ, gServiceInfoSpec),
    LWMSG_MESSAGE(SM_IPC_ADD_SERVICE_RES, gNewHandleSpec),
    LWMSG_MESSAGE(SM_IPC_REMOVE_SERVICE_REQ, gExistingHandleSpec),
    LWMSG_MESSAGE(SM_IPC_REMOVE_SERVICE_RES, NULL),
    LWMSG_MESSAGE(SM_IPC_START_SERVICE_REQ, gExistingHandleSpec),
    LWMSG_MESSAGE(SM_IPC_START_SERVICE_RES, NULL),
    LWMSG_MESSAGE(SM_IPC_STOP_SERVICE_REQ, gExistingHandleSpec),
    LWMSG_MESSAGE(SM_IPC_STOP_SERVICE_RES, NULL),
    LWMSG_MESSAGE(SM_IPC_REFRESH_SERVICE_REQ, gExistingHandleSpec),
    LWMSG_MESSAGE(SM_IPC_REFRESH_SERVICE_RES, NULL),
    LWMSG_MESSAGE(SM_IPC_KILL_SERVICE_REQ, gExistingHandleSpec),
    LWMSG_MESSAGE(SM_IPC_KILL_SERVICE_RES, NULL),
    LWMSG_MESSAGE(SM_IPC_QUERY_SERVICE_STATUS_REQ, gExistingHandleSpec),
    LWMSG_MESSAGE(SM_IPC_QUERY_SERVICE_STATUS_RES, gServiceStatusSpec),
    LWMSG_MESSAGE(SM_IPC_QUERY_SERVICE_INFO_REQ, gExistingHandleSpec),
    LWMSG_MESSAGE(SM_IPC_QUERY_SERVICE_INFO_RES, gServiceInfoSpec),
    LWMSG_MESSAGE(SM_IPC_WAIT_SERVICE_REQ, gWaitStateChangeSpec),
    LWMSG_MESSAGE(SM_IPC_WAIT_SERVICE_RES, gServiceStateSpec),
    LWMSG_MESSAGE(SM_IPC_SET_LOG_INFO_REQ, gSetLogInfoSpec),
    LWMSG_MESSAGE(SM_IPC_SET_LOG_INFO_RES, NULL),
    LWMSG_MESSAGE(SM_IPC_SET_LOG_LEVEL_REQ, gSetLogLevelSpec),
    LWMSG_MESSAGE(SM_IPC_SET_LOG_LEVEL_RES, NULL),
    LWMSG_MESSAGE(SM_IPC_GET_LOG_STATE_REQ, gGetLogStateReqSpec),
    LWMSG_MESSAGE(SM_IPC_GET_LOG_STATE_RES, gGetLogStateResSpec),
    LWMSG_MESSAGE(SM_IPC_ENUM_FACILITIES_REQ, gExistingHandleSpec),
    LWMSG_MESSAGE(SM_IPC_ENUM_FACILITIES_RES, gStringListSpec),
    LWMSG_MESSAGE(SM_IPC_REFRESH_REQ, NULL),
    LWMSG_MESSAGE(SM_IPC_REFRESH_RES, NULL),
    LWMSG_MESSAGE(SM_IPC_SHUTDOWN_REQ, NULL),
    LWMSG_MESSAGE(SM_IPC_SHUTDOWN_RES, NULL),
    LWMSG_MESSAGE(SM_IPC_SET_GLOBAL_REQ, gSetGlobalReqSpec),
    LWMSG_MESSAGE(SM_IPC_SET_GLOBAL_RES, NULL),
    LWMSG_MESSAGE(SM_IPC_GET_GLOBAL_REQ, gGetGlobalReqSpec),
    LWMSG_MESSAGE(SM_IPC_GET_GLOBAL_RES, gGlobalValueSpec),
    LWMSG_PROTOCOL_END,
};

LWMsgProtocolSpec*
LwSmIpcGetProtocolSpec(
    VOID
    )
{
    return gIpcSpec;
}
