1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef STARTUP_INIT_GROUP_MANAGER_H
16 #define STARTUP_INIT_GROUP_MANAGER_H
17 #include <stdlib.h>
18 #include <string.h>
19 #include "init_service.h"
20 #include "init_hashmap.h"
21 #include "init_cmdexecutor.h"
22 
23 #ifdef __cplusplus
24 #if __cplusplus
25 extern "C" {
26 #endif
27 #endif
28 
29 #define GROUP_IMPORT_MAX_LEVEL 5
30 #define GROUP_NAME_MAX_LENGTH 64
31 #define GROUP_HASHMAP_BUCKET 32
32 
33 #define GROUP_DEFAULT_PATH STARTUP_INIT_UT_PATH"/system/etc"
34 #define BOOT_GROUP_NAME "bootgroup"
35 #define BOOT_GROUP_DEFAULT "device.boot.group"
36 
37 typedef enum {
38     GROUP_BOOT,
39     GROUP_CHARGE,
40     GROUP_UNKNOW
41 } InitGroupType;
42 
43 typedef enum {
44     NODE_TYPE_JOBS,
45     NODE_TYPE_SERVICES,
46     NODE_TYPE_PLUGINS,
47     NODE_TYPE_CMDS,
48     NODE_TYPE_GROUPS,
49     NODE_TYPE_MAX
50 } InitNodeType;
51 
52 typedef struct InitGroupNode_ {
53     struct InitGroupNode_ *next;
54     HashNode hashNode;
55     unsigned char type;
56     union {
57         Service *service;
58         PluginCmd *cmd;
59     } data;
60     char name[0];
61 } InitGroupNode;
62 
63 typedef struct {
64     uint8_t initFlags;
65     InitGroupType groupMode;
66     InitGroupNode *groupNodes[NODE_TYPE_MAX];
67     HashMapHandle hashMap[NODE_TYPE_GROUPS];
68     char groupModeStr[GROUP_NAME_MAX_LENGTH];
69     ServiceSocket *serviceSocketNode;
70 } InitWorkspace;
71 
72 void InitServiceSpace(void);
73 void CloseServiceSpace(void);
74 int InitParseGroupCfg(void);
75 
76 ServiceSocket *GetOnDemandSocketList(void);
77 void AddOnDemandSocket(ServiceSocket *socketNode);
78 void RemoveOnDemandSocket(ServiceSocket *sockopt);
79 int GenerateHashCode(const char *key);
80 InitGroupNode *AddGroupNode(int type, const char *name);
81 InitGroupNode *GetGroupNode(int type, const char *name);
82 InitGroupNode *GetNextGroupNode(int type, const InitGroupNode *curr);
83 void DelGroupNode(int type, const char *name);
84 int CheckNodeValid(int type, const char *name);
85 HashMapHandle GetGroupHashMap(int type);
86 #ifdef STARTUP_INIT_TEST
87 InitWorkspace *GetInitWorkspace(void);
88 #endif
89 int GetBootModeFromMisc(void);
90 void clearMisc(void);
91 void ReleaseCmd(PluginCmd *cmd);
92 #ifdef __cplusplus
93 #if __cplusplus
94 }
95 #endif
96 #endif
97 
98 #endif // STARTUP_INIT_GROUP_MANAGER_H
99