[FEATURE] Implement kernel -> user connection
[platform/core/system/swap-manager.git] / daemon / daemon.h
1 /*
2  *  DA manager
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
8  * Jaewon Lim <jaewon81.lim@samsung.com>
9  * Woojin Jung <woojin2.jung@samsung.com>
10  * Juyoung Kim <j0.kim@samsung.com>
11  * Cherepanov Vitaliy <v.cherepanov@samsung.com>
12  * Nikita Kalyazin    <n.kalyazin@samsung.com>
13  *
14  * Licensed under the Apache License, Version 2.0 (the "License");
15  * you may not use this file except in compliance with the License.
16  * You may obtain a copy of the License at
17  *
18  * http://www.apache.org/licenses/LICENSE-2.0
19  *
20  * Unless required by applicable law or agreed to in writing, software
21  * distributed under the License is distributed on an "AS IS" BASIS,
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23  * See the License for the specific language governing permissions and
24  * limitations under the License.
25  *
26  * Contributors:
27  * - S-Core Co., Ltd
28  * - Samsung RnD Institute Russia
29  *
30  */
31
32
33 #ifndef _DAEMON_H_
34 #define _DAEMON_H_
35
36 #include <stdint.h>             // for uint64_t, int64_t
37 #include <pthread.h>    // for pthread_mutex_t
38
39 #include "da_protocol.h"
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45
46 #define PROTOCOL_VERSION                        "2.1"
47
48 #define RUN_APP_LOADER
49
50 #define MAX_TARGET_COUNT                        8
51 #define DA_MSG_MAX                                      4096
52 #define RECV_BUF_MAX                            4104    // = sizeof(msg_t)
53
54 #define MAX_DEVICE                              10
55 #define MAX_FILENAME                    128
56
57 /*
58 enum ErrorCode
59 {
60         ERR_LOCKFILE_CREATE_FAILED = -101,
61         ERR_ALREADY_RUNNING = -102,
62         ERR_INITIALIZE_SYSTEM_INFO_FAILED = -103,
63         ERR_HOST_SERVER_SOCKET_CREATE_FAILED = -104,
64         ERR_TARGET_SERVER_SOCKET_CREATE_FAILED = -105,
65         ERR_SIGNAL_MASK_SETTING_FAILED = -106,
66         ERR_WRONG_MESSAGE_FORMAT = -201,
67         ERR_WRONG_MESSAGE_TYPE = -202,
68         ERR_WRONG_MESSAGE_DATA = -203,
69         ERR_CANNOT_START_PROFILING = -204,
70         ERR_WRONG_PROTOCOL_VERSION = -205
71 };*/
72
73 enum TargetMessageType
74 {
75         MSG_DEVICE = 1,
76         MSG_TIME = 2,
77         MSG_SAMPLE = 3,
78         MSG_RESOURCE = 4,
79         MSG_LOG = 5,
80         MSG_IMAGE = 6,
81         MSG_TERMINATE = 7,
82         MSG_PID = 8,
83         MSG_MSG = 9,
84         MSG_ALLOC = 10,
85         MSG_ERROR = 11
86 };
87 #define IS_PROBE_MSG(type) (((type) & 0x0100) == 0x0100)
88
89 enum HostMessageType
90 {
91         MSG_HOST_BEGIN = 100,
92         MSG_START = 100,
93         MSG_STOP = 101,
94         MSG_PAUSE = 102,
95         MSG_OPTION = 103,
96         MSG_ISALIVE = 104,
97         MSG_ALIVE = 105,
98         MSG_BATT_START = 106,
99         MSG_BATT_STOP = 107,
100         MSG_CAPTURE_SCREEN = 108,
101         MSG_RECORD = 801,
102         MSG_REPLAY = 802,
103         MSG_OK = 901,
104         MSG_NOTOK = 902,
105         MSG_VERSION = 999,
106         MSG_HOST_END = 999
107 };
108
109 enum DAState
110 {
111         DAS_NONE = 0,
112         DAS_START_BEGIN = 1,
113         DAS_TARGET_ARM_START = 1,
114         DAS_TARGET_X86_START = 2,
115         DAS_EMUL_ARM_START = 3,
116         DAS_EMUL_X86_START = 4,
117         DAS_TARGET_ARM_BATT_START = 5,
118         DAS_TARGET_X86_BATT_START = 6,
119         DAS_EMUL_ARM_BATT_START = 7,
120         DAS_EMUL_X86_BATT_START = 8,
121         DAS_START_END = 8,
122         DAS_STOP = 9,
123         DAS_TERMINATE = 10
124 };
125
126
127 #ifndef likely
128 #define likely(x)       __builtin_expect((x), 1)
129 #define unlikely(x)     __builtin_expect((x), 0)
130 #endif
131
132 #define EVENT_STOP              0x00000001
133 #define EVENT_PID               0x00000002
134 #define EVENT_ERROR             0x00000004
135
136 typedef struct
137 {
138         unsigned int    type;
139         unsigned int    length;
140         char                    data[DA_MSG_MAX];
141 } msg_target_t;
142
143 typedef struct
144 {
145         int                                     control_socket;
146         int                                     data_socket;
147         pthread_mutex_t         data_socket_mutex;
148 } __da_host_info;
149
150 typedef struct
151 {
152         int64_t                 allocmem;               // written only by recv thread
153         pid_t                           pid;                    // written only by recv thread
154         int                             socket;                 // written only by main thread
155         pthread_t               recv_thread;    // written only by main thread
156         int                             event_fd;               // for thread communication (from recv thread to main thread)
157         int                             initial_log;    // written only by main thread
158 } __da_target_info;
159
160 typedef struct
161 {
162         int brightness;
163         int voltage;
164         int procmeminfo;
165         FILE *video;
166         FILE *procstat;
167         FILE *networkstat;
168         FILE *diskstats;
169 } __file_descriptors;
170
171 typedef struct
172 {
173         int host_server_socket;
174         int target_server_socket;
175         int kernel_socket;
176         int target_count;
177         int apps_to_run;
178         unsigned int config_flag;
179         int app_launch_timerfd;
180         int connect_timeout_timerfd;
181         pthread_t sampling_thread;
182         pthread_t replay_thread;
183         pthread_t transfer_thread;
184         int buf_fd;
185         int user_ev_fd;
186         int efd;
187         __da_host_info host;
188         __da_target_info target[MAX_TARGET_COUNT];
189         __file_descriptors fd;
190         char appPath[128]; // application executable path
191 } __da_manager;
192
193 extern __da_manager manager;
194
195 uint64_t get_total_alloc_size(void);
196 void initialize_log(void);
197 int daemonLoop(void);
198 void unlink_portfile(void);
199
200 int makeRecvThread(int index);
201 int samplingStart(void);
202 int samplingStop(void);
203
204
205
206
207 // TODO maybe need move to other file
208 int prepare_profiling(void);
209 int start_profiling(void);
210 void stop_profiling(void);
211 int reconfigure(struct conf_t conf);
212 int sendACKCodeToHost(enum HostMessageType resp, int msgcode);
213 void terminate_all(void);
214
215 #ifdef __cplusplus
216 }
217 #endif
218
219 #endif // _DAEMON_H_