/****************************************************************************
 * apps/system/init/action.h
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.  The
 * ASF licenses this file to you 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 __APPS_SYSTEM_INIT_ACTION_H
#define __APPS_SYSTEM_INIT_ACTION_H

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/list.h>

#include <time.h>

#include "parser.h"

/****************************************************************************
 * Public Types
 ****************************************************************************/

enum action_cmd_op_e
{
  CMD_OP_NONE,
  CMD_OP_AND,
  CMD_OP_OR,
};

struct action_cmd_s
{
  struct list_node node;
  int argc;
  FAR char *argv[CONFIG_SYSTEM_INIT_ACTION_CMD_ARGS_MAX];
  enum action_cmd_op_e op;
};

struct action_event_s
{
  FAR const char *key;
  FAR char *value;
  bool invert;
  bool pending;
};

struct action_s
{
  struct list_node node;
  struct list_node ready_node;
  struct action_event_s events[CONFIG_SYSTEM_INIT_ACTION_EVENTS_MAX];
  struct list_node cmds;                     /* struct action_cmd_s */
  int prev_ret;
};

struct action_manager_s
{
  struct list_node actions;                  /* struct action_s */
  struct list_node ready_actions;            /* struct action_s */

  FAR struct action_s *current;

  FAR struct action_cmd_s *running;
  int pid_running;
#if defined(CONFIG_SYSTEM_INIT_ACTION_WARN_SLOW) && \
    CONFIG_SYSTEM_INIT_ACTION_WARN_SLOW > 0
  struct timespec time_run;
#endif
  FAR struct service_manager_s *sm;

  FAR struct init_poller_s *prop;
};

typedef CODE int (*init_action_event_cb)(FAR struct action_manager_s *,
                                         FAR struct action_s *,
                                         FAR struct action_event_s *,
                                         FAR void *arg);

/****************************************************************************
 * Public Function Prototypes
 ****************************************************************************/

int  init_action_add_event(FAR struct action_manager_s *am,
                           FAR const char *name);
int  init_action_run_command(FAR struct action_manager_s *am);
void init_action_reap_command(FAR struct action_manager_s *am, int ret);
int  init_action_parse(FAR const struct parser_s *parser,
                       bool create, FAR char *buf);
int  init_action_foreach_event(FAR struct action_manager_s *am,
                               init_action_event_cb cb,
                               FAR void *arg);
void init_action_trigger_event(FAR struct action_manager_s *am,
                               FAR const char *key,
                               FAR const char *value);
#ifdef CONFIG_SYSTEM_INIT_DEBUG
void init_dump_actions(FAR struct list_node *head);
#else
#  define init_dump_actions(h)
#endif
#endif /* __APPS_SYSTEM_INIT_ACTION_H */