/*

 * Copyright (c) 2024 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.

 */



#ifndef HNP_API_H

#define HNP_API_H

#include "hilog/log.h"



#ifdef __cplusplus

extern "C" {

#endif



#ifndef APPSPAWN_TEST

#ifndef APPSPAWN_STATIC

#define APPSPAWN_STATIC static

#endif

#else

#ifndef APPSPAWN_STATIC

#define APPSPAWN_STATIC

#endif

#endif



typedef enum {

    OPTION_INDEX_FORCE = 0,     /* installed forcely */

    OPTION_INDEX_BUTT

} HnpInstallOptionIndex;



#define HNP_API_ERRNO_BASE 0x2000

#define FD_BUFFER_LEN (64)

#define HNP_INFO_RET_FD_ENV "HNP_INFO_RET_FD_ENV"

#define SESSIONID_BUFFER_LEN (32)





// 0x2001 参数非法

#define HNP_API_ERRNO_PARAM_INVALID             (HNP_API_ERRNO_BASE + 0x1)



// 0x2002 fork子进程失败

#define HNP_API_ERRNO_FORK_FAILED               (HNP_API_ERRNO_BASE + 0x2)



// 0x2003 等待子进程退出失败

#define HNP_API_WAIT_PID_FAILED                 (HNP_API_ERRNO_BASE + 0x3)



// 0x2004 非开发者模式

#define HNP_API_NOT_IN_DEVELOPER_MODE           (HNP_API_ERRNO_BASE + 0x4)



// 0x2005 创建管道失败

#define HNP_API_ERRNO_PIPE_CREATED_FAILED       (HNP_API_ERRNO_BASE + 0x5)



// 0x2006 读取管道失败

#define HNP_API_ERRNO_PIPE_READ_FAILED          (HNP_API_ERRNO_BASE + 0x6)



// 0x2007 获取返回码失败

#define HNP_API_ERRNO_RETURN_VALUE_GET_FAILED   (HNP_API_ERRNO_BASE + 0x7)



// 0x2008 移动内存失败

#define HNP_API_ERRNO_MEMMOVE_FAILED            (HNP_API_ERRNO_BASE + 0x8)



// 0x2009 产品不支持安装hnp软件包

#define HNP_API_ERRNO_HNP_INSTALL_DISABLED      (HNP_API_ERRNO_BASE + 0x9)



// 0x2010 参数超过上限

#define HNP_API_ERRNO_TOO_MANY_PARAM      (HNP_API_ERRNO_BASE + 0x10)



#define PACK_NAME_LENTH 256

#define HAP_PATH_LENTH 512

#define ABI_LENTH 128

#define APP_IDENTIFIER_LEN 64



typedef struct HapInfo {

    char packageName[PACK_NAME_LENTH];          // package name

    char hapPath[HAP_PATH_LENTH];               // hap file path

    char abi[ABI_LENTH];                        // system abi

    char appIdentifier[APP_IDENTIFIER_LEN];     // appIdentifier

    int count;                                  // independentSignHnpPaths counts

    char **independentSignHnpPaths;             // independentSign Hnp Paths

} HapInfo;



typedef struct HnpResult {

    int result;

} HnpResult;

/**

 * Install native software package.

 *

 * @param userId Indicates id of user.

 * @param hnpRootPath  Indicates the root path of hnp packages

 * @param hapInfo Indicates the information of HAP.

 * @param installOptions Indicates install options.

 *

 * @return 0:success;other means failure.

 */

int NativeInstallHnp(const char *userId, const char *hnpRootPath, const HapInfo *hapInfo,

    int installOptions, int32_t sessionId);



/**

 * Uninstall native software package.

 *

 * @param userId Indicates id of user.

 * @param packageName Indicates the packageName of HAP.

 *

 * @return 0:success;other means failure.

 */

int NativeUnInstallHnp(const char *userId, const char *packageName);



#define HNPAPI_LOG(fmt, ...) \

    HILOG_INFO(LOG_CORE, "[%{public}s:%{public}d]" fmt, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__)



#define HNPAPI_INFO_CHECK(ret, statement, format, ...) \

    do {                                                  \

        if (!(ret)) {                                    \

            HNPAPI_LOG(format, ##__VA_ARGS__);            \

            statement;                                   \

        }                                          \

    } while (0)



#define HNPAPI_LOG_ERR(fmt, ...) \

    HILOG_ERROR(LOG_CORE, "[%{public}s:%{public}d]" fmt, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__)



#define HNPAPI_ERROR_CHECK(ret, statement, format, ...) \

    do {                                                  \

        if (!(ret)) {                                     \

            HNPAPI_LOG_ERR(format, ##__VA_ARGS__);             \

            statement;                                    \

        }                                                 \

    } while (0)



#ifdef __cplusplus

}

#endif



#endif