/******************************************************************************
 *                                                                            *
 * Copyright (C) 2023 The Android Open Source Project
 *
 * 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.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
 */

#pragma once
#define IA_MAX_ERROR_SUB_CODE (70)

/* these definitions are used by error handling function         */
/* the error handler will work on a structure which identifies a */
/* particular error with a module, context and error_code        */
/* within error_code, MSB identifies FATAL (1), NONFATAL (0)     */
/* next 4 ms_bs identify a class of error                        */

typedef struct {
  pWORD8 pb_module_name;
  pWORD8 ppb_class_names[16];
  WORD8 **ppppb_error_msg_pointers[2][7][20];
} ia_error_info_struct;

/* this error handler maps the code generated by a module to a error string */
/* pb_context is a string to specify where the module broke                 */
IA_ERRORCODE ia_error_handler(ia_error_info_struct *p_mod_err_info, WORD8 *pb_context,
                              IA_ERRORCODE code);

/* the following macro does a line job of returning back to the parent */
/* in case a fatal error occurs after calling the errorhandler         */
#define _IA_HANDLE_ERROR(p_mod_err_info, context, e, pv_output) \
  if ((e) != IA_NO_ERROR) {                                     \
    ia_error_handler((p_mod_err_info), (context), (e));         \
    if ((e)&IA_FATAL_ERROR) {                                   \
      IA_ERRORCODE err = IA_NO_ERROR;                           \
      err = ixheaace_delete((pVOID)pv_output);                  \
      if (pstr_in_cfg->pv_drc_cfg) {                            \
        pv_output->free_xheaace(pstr_in_cfg->pv_drc_cfg);       \
      }                                                         \
      if (pstr_drc_cfg_user) {                                  \
        free_global(pstr_drc_cfg_user);                         \
        pstr_drc_cfg_user = NULL;                               \
      }                                                         \
      if (ia_stsz_size != NULL) {                               \
        pv_output->free_xheaace(ia_stsz_size);                  \
        ia_stsz_size = NULL;                                    \
      }                                                         \
      if ((err)&IA_FATAL_ERROR) {                               \
        return (err);                                           \
      } else {                                                  \
        return (e);                                             \
      }                                                         \
    }                                                           \
  }
