1 /* 2 * Copyright (c) 2021 Rockchip Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 #ifndef __RK_AIQ_USER_API2_HELPER_H__ 19 #define __RK_AIQ_USER_API2_HELPER_H__ 20 21 #include "rk_aiq_user_api_sysctl.h" 22 23 typedef enum __RkAiqUapiOpMode { 24 RKAIQUAPI_OPMODE_SET = 0, 25 RKAIQUAPI_OPMODE_GET, 26 } RkAiqUapiOpMode_t; 27 28 #define RKAIQ_UAPI_NAME_MAX_LEN 64 29 #define JSON_PATCH_PATH "path" 30 #define JSON_PATCH_VALUE "value" 31 32 #define UAPIRPC_DEBUG 33 34 #define __RKAIQUAPI_SET(api_name, type_name) \ 35 inline int __rkaiq_uapi_##api_name##_set(void *ctx, void *data) { \ 36 rk_aiq_sys_ctx_t *__ctx = (rk_aiq_sys_ctx_t *)ctx; \ 37 type_name *__data = (type_name *)data; \ 38 return rkaiq_uapi_##api_name##_set(__ctx, __data); \ 39 } 40 41 #define __RKAIQUAPI_GET(api_name, type_name) \ 42 inline int __rkaiq_uapi_##api_name##_get(void *ctx, void *data) { \ 43 rk_aiq_sys_ctx_t *__ctx = (rk_aiq_sys_ctx_t *)ctx; \ 44 type_name *__data = (type_name *)data; \ 45 return rkaiq_uapi_##api_name##_get(__ctx, __data); \ 46 } 47 48 #define __RKAIQUAPI_CALLER(type_name) \ 49 inline int __rkaiq_uapi_##type_name##_call( \ 50 void *desc, void *sys_ctx, cJSON *cmd_js, cJSON **ret_js, int mode) { \ 51 RkAiqUapiDesc_t *uapi_desc = (RkAiqUapiDesc_t *)desc; \ 52 rk_aiq_sys_ctx_t *aiq_ctx = (rk_aiq_sys_ctx_t *)sys_ctx; \ 53 char* js_str = NULL; \ 54 j2s_ctx ctx; \ 55 int ret = -1; \ 56 j2s_init(&ctx); \ 57 ctx.format_json = false; \ 58 ctx.manage_data = false; \ 59 type_name real_obj; \ 60 \ 61 if (mode == RKAIQUAPI_OPMODE_SET) { \ 62 /* Get old json then apply change */ \ 63 cJSON *old_json = NULL; \ 64 ret = __rkaiq_uapi_##type_name##_call(desc, sys_ctx, cmd_js, &old_json, \ 65 RKAIQUAPI_OPMODE_GET); \ 66 if (ret || !old_json) { \ 67 XCAM_LOG_ERROR("sysctl for %s readback failed.", #type_name); \ 68 return -1; \ 69 } \ 70 ret = cJSONUtils_ApplyPatches(old_json, cmd_js); \ 71 if (0 != ret) { \ 72 XCAM_LOG_ERROR("%s apply patch failed %d!", __func__, ret); \ 73 return -1; \ 74 } \ 75 memset(&real_obj, 0, sizeof(type_name)); \ 76 ret = j2s_json_to_struct(&ctx, old_json, #type_name, &real_obj); \ 77 j2s_deinit(&ctx); \ 78 if (ret) { \ 79 return -1; \ 80 } \ 81 if (!uapi_desc->arg_set) { \ 82 return -1; \ 83 } \ 84 return uapi_desc->arg_set(aiq_ctx, &real_obj); \ 85 } else if (mode == RKAIQUAPI_OPMODE_GET) { \ 86 if (!uapi_desc->arg_get) { \ 87 return -1; \ 88 } \ 89 uapi_desc->arg_get(aiq_ctx, &real_obj); \ 90 *ret_js = j2s_struct_to_json(&ctx, #type_name, &real_obj); \ 91 j2s_deinit(&ctx); \ 92 if (!*ret_js) { \ 93 XCAM_LOG_ERROR("create %s failed.", #type_name); \ 94 return -1; \ 95 } \ 96 } \ 97 return 0; \ 98 } 99 100 #define __RKAIQUAPI_SET_FUNC(api_name, type_name) __rkaiq_uapi_##api_name##_set 101 #define __RKAIQUAPI_GET_FUNC(api_name, type_name) __rkaiq_uapi_##api_name##_get 102 #define __RKAIQUAPI_FUNC_CAST(__api) (RkAiqUapi)(__api) 103 104 typedef int (*RkAiqUapi)(void *, void *); 105 typedef int (*RkAiqUapiCaller)(void *, void *, void *, void **, int); 106 107 typedef struct __RkAiqUapiDesc { 108 char arg_path[RKAIQ_UAPI_NAME_MAX_LEN]; 109 char arg_type[RKAIQ_UAPI_NAME_MAX_LEN]; 110 RkAiqUapi arg_set; 111 RkAiqUapi arg_get; 112 RkAiqUapiCaller uapi_caller; 113 } RkAiqUapiDesc_t; 114 115 #ifdef __cplusplus 116 117 #define __RKAIQUAPI_DESC_DEF(__arg_path, __arg_type, __arg_set, __arg_get) \ 118 { \ 119 __arg_path, #__arg_type, (RkAiqUapi)__arg_set, (RkAiqUapi)__arg_get, \ 120 (RkAiqUapiCaller)__rkaiq_uapi_##__arg_type##_call, \ 121 } 122 123 #else 124 125 #define __RKAIQUAPI_DESC_DEF(__arg_path, __arg_type, __arg_set, __arg_get) \ 126 { \ 127 .arg_path = {__arg_path}, .arg_type = {#__arg_type}, \ 128 .arg_set = (RkAiqUapi)__arg_set, .arg_get = (RkAiqUapi)__arg_get, \ 129 .uapi_caller = (RkAiqUapiCaller)__rkaiq_uapi_##__arg_type##_call, \ 130 } 131 132 #endif 133 134 #define __RKAIQUAPI_SET_WRAPPER(func, type_name) \ 135 int func##2(const rk_aiq_sys_ctx_t *sys_ctx, const type_name *ptr) { \ 136 return func(sys_ctx, *ptr); \ 137 } 138 139 #define __RKAIQUAPI_SET_WRAPPER_NAME(func) func##2 140 141 int rkaiq_uapi_unified_ctl(rk_aiq_sys_ctx_t *sys_ctx, const char *js_str, 142 char **ret_str, int op_mode); 143 144 #endif /*__RK_AIQ_USER_API2_HELPER_H__*/ 145