1 /* 2 * Copyright (C)2021-2022 Intel Corporation. 3 * SPDX-License-Identifier: BSD-3-Clause 4 */ 5 #ifndef _CMD_H_ 6 #define _CMD_H_ 7 #include <pthread.h> 8 #include <sys/queue.h> 9 10 11 #define SYNC_CMD "sync" 12 #define ACK_SYNC "ack_sync" 13 #define REQ_SYS_SHUTDOWN "req_sys_shutdown" 14 #define ACK_REQ_SYS_SHUTDOWN "ack_req_sys_shutdown" 15 #define POWEROFF_CMD "poweroff_cmd" 16 #define ACK_POWEROFF "ack_poweroff" 17 #define ACK_TIMEOUT "ack_timeout" 18 #define REQ_USER_VM_SHUTDOWN "req_user_vm_shutdown" 19 #define USER_VM_SHUTDOWN "user_vm_shutdown" 20 #define REQ_USER_VM_REBOOT "req_user_vm_reboot" 21 #define USER_VM_REBOOT "user_vm_reboot" 22 #define REQ_SYS_REBOOT "req_sys_reboot" 23 #define ACK_REQ_SYS_REBOOT "ack_req_sys_reboot" 24 25 #define ACK_REQ_USER_VM_SHUTDOWN "ack_req_user_vm_shutdown" 26 #define ACK_USER_VM_SHUTDOWN "ack_user_vm_shutdown" 27 #define ACK_REQ_USER_VM_REBOOT "ack_req_user_vm_reboot" 28 #define ACK_USER_VM_REBOOT "ack_user_vm_reboot" 29 #define FAIL_CONNECT "fail_connect" 30 #define USER_VM_DISCONNECT "user_vm_disconnect" 31 #define S5_REJECTED "system shutdown request is rejected" 32 33 #define SYNC_LEN (sizeof(SYNC_CMD)) 34 35 #define POWEROFF "poweroff" 36 #define REBOOT "reboot" 37 38 #define CMD_NAME_MAX 32U 39 40 enum command_id { 41 SYNC_ID = 0x0, 42 ACKED_SYNC_ID, 43 REQ_SYS_SHUTDOWN_ID, 44 ACKED_REQ_SYS_SHUTDOWN_ID, 45 POWEROFF_CMD_ID, 46 ACKED_POWEROFF_ID, 47 ACK_TIMEOUT_ID, 48 REQ_USER_VM_SHUTDOWN_ID, 49 USER_VM_SHUTDOWN_ID, 50 ACK_USER_VM_SHUTDOWN_ID, 51 REQ_USER_VM_REBOOT_ID, 52 USER_VM_REBOOT_ID, 53 ACK_USER_VM_REBOOT_ID, 54 REQ_SYS_REBOOT_ID, 55 ACK_REQ_SYS_REBOOT_ID, 56 CMD_END, 57 }; 58 59 typedef int (cmd_handler)(void *arg, int fd); 60 struct command_handlers { 61 void *arg; 62 cmd_handler *fn; 63 64 LIST_ENTRY(command_handlers) list; 65 }; 66 67 struct command { 68 const char name[CMD_NAME_MAX]; /**< command name */ 69 enum command_id id; /**< command id */ 70 71 /* command handler list */ 72 LIST_HEAD(cmd_handlers_list, command_handlers) cmd_handlers_head; 73 pthread_mutex_t cmd_handler_mtx; /**< mutex to protect command handler list */ 74 }; 75 /** 76 * @brief register command handler, other module can use this interface to 77 * register multiple handler for one command. 78 * 79 * @param fn the command handler which will be registered 80 * @param arg the parameter which will be passed into hanlder 81 * @param cmd_name the command name 82 */ 83 int register_command_handler(cmd_handler *fn, void *arg, const char *cmd_name); 84 /** 85 * @brief find a command instance by name 86 * 87 * @param name the command name 88 * @return command instance 89 */ 90 struct command *find_command(const char *name); 91 /** 92 * @brief dispatch the command and invoke registered handler. 93 * 94 * @param arg command instance 95 * @param fd the file descriptor of the device 96 * @return the flag indicates the state of command handler execution 97 */ 98 int dispatch_command_handlers(void *arg, int fd); 99 #endif 100