[FIX] empty string in sprintf
[platform/core/system/swap-manager.git] / daemon / da_data.c
1 /*
2  *  DA manager
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
8  * Cherepanov Vitaliy <v.cherepanov@samsung.com>
9  * Nikita Kalyazin    <:wn.kalyazin@samsung.com>
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Contributors:
24  * - Samsung RnD Institute Russia
25  *
26  */
27
28
29 #include "da_protocol.h"
30 #include "daemon.h"
31 #include "da_data.h"
32 #include "debug.h"
33
34 #include <sys/time.h>
35
36 int print_sys_info(struct system_info_t * sys_info)
37 {
38         int i = 0;
39         /* //FOR DEBUG
40         sys_info->energy=0x1;
41         sys_info->wifi_status=0x2;
42         sys_info->bt_status=0x3;
43         sys_info->gps_status=0x4;
44         sys_info->brightness_status=0x5;
45
46         sys_info->camera_status=0x6;
47         sys_info->sound_status=0x7;
48         sys_info->audio_status=0x8;
49         sys_info->vibration_status=0x9;
50         sys_info->voltage_status=0x10;
51
52         sys_info->rssi_status=0x11;
53         sys_info->video_status=0x12;
54         sys_info->call_status=0x13;
55         sys_info->dnet_status=0x14;
56         sys_info->cpu_frequency=0x15;
57
58         sys_info->app_cpu_usage=0.0;
59         sys_info->cpu_load=0x17;
60         sys_info->virtual_memory=0x18;
61         sys_info->resident_memory=0x19;
62         sys_info->shared_memory=0x20;
63
64         sys_info->pss_memory=0x21;
65         sys_info->total_alloc_size=(uint64_t)0x22;
66         sys_info->system_memory_total=(uint64_t)0x23;
67         sys_info->system_memory_used=(uint64_t)0x24;
68         sys_info->total_used_drive=0x25;
69
70         sys_info->count_of_threads=0x26;
71         sys_info->thread_load=0x27;
72         sys_info->count_of_processes=0x28;
73         sys_info->process_load=0x29;
74         sys_info->disk_read_size=0x30;
75
76         sys_info->disk_write_size=0x31;
77         sys_info->network_send_size=0x32;
78         sys_info->network_receive_size=0x33;
79         */
80
81         LOGI("isysinfo:\n\
82 \
83         energy = 0x%X\n\
84         wifi_status = 0x%X\n\
85         bt_status = 0x%X\n\
86         gps_status = 0x%X\n\
87         brightness_status = 0x%X\n\
88 \
89         camera_status = 0x%X\n\
90         sound_status = 0x%X\n\
91         audio_status = 0x%X\n\
92         vibration_status = 0x%X\n\
93         voltage_status = 0x%X\n\
94 \
95         rssi_status = 0x%X\n\
96         video_status = 0x%X\n\
97         call_status = 0x%X\n\
98         dnet_status = 0x%X\n\
99         cpu_frequency (pointer)= 0x%X\n\
100 \
101         app_cpu_usage = %f\n\
102         cpu_load (pointer)= 0x%X\n\
103         virtual_memory = 0x%X\n\
104         resident_memory = 0x%X\n\
105         shared_memory = 0x%X\n\
106 \
107         pss_memory = 0x%X\n\
108         total_alloc_size = 0x%X\n\
109         system_memory_total = %llu\n\
110         system_memory_used = %llu\n\
111         total_used_drive = %d\n\
112 \
113         count_of_threads = %u\n\
114         thread_load (pointer)= 0x%X\n\
115         count_of_processes = %u\n\
116         process_load = 0x%X\n\
117         disk_reads = 0x%X\n\
118 \
119         disk_sectors_read = 0x%X\n\
120         disk_writes = 0x%X\n\
121         disk_sectors_write = 0x%X\n\
122         network_send_size = 0x%X\n\
123         network_receive_size = 0x%X\n",
124
125         sys_info->energy,
126         sys_info->wifi_status,
127         sys_info->bt_status,
128         sys_info->gps_status,
129         sys_info->brightness_status,
130
131         sys_info->camera_status,
132         sys_info->sound_status,
133         sys_info->audio_status,
134         sys_info->vibration_status,
135         sys_info->voltage_status,
136
137         sys_info->rssi_status,
138         sys_info->video_status,
139         sys_info->call_status,
140         sys_info->dnet_status,
141         (unsigned int)sys_info->cpu_frequency,
142
143         sys_info->app_cpu_usage,
144         (unsigned int)sys_info->cpu_load,
145         sys_info->virtual_memory,
146         sys_info->resident_memory,
147         sys_info->shared_memory,
148
149         sys_info->pss_memory,
150         sys_info->total_alloc_size,
151         sys_info->system_memory_total,
152         sys_info->system_memory_used,
153         sys_info->total_used_drive,
154
155         sys_info->count_of_threads,
156         (unsigned int)sys_info->thread_load,
157         sys_info->count_of_processes,
158         (unsigned int)sys_info->process_load,
159         sys_info->disk_reads,
160
161         sys_info->disk_sectors_read,
162         sys_info->disk_writes,
163         sys_info->disk_sectors_write,
164         sys_info->network_send_size,
165         sys_info->network_receive_size
166         );
167         LOGI_("->\n");
168         for ( i=0; i<sys_info->count_of_processes; i++)
169         {
170                 LOGI_("\tpr %016X : %f\n",
171                 sys_info->process_load[i].id,
172                 sys_info->process_load[i].load
173                 );
174         }
175         for ( i=0; i<sys_info->count_of_threads; i++)
176         {
177                 LOGI_("\tth %016X : %f\n",
178                 sys_info->thread_load[i].pid,
179                 sys_info->thread_load[i].load
180                 );
181         }
182
183         // FIXME CPU core num hardcoded
184         for ( i=0; i<4; i++)
185         {
186                 LOGI_("\tCPU load #%d : %f\n",
187                 i,
188                 sys_info->cpu_load[i]
189                 );
190         }
191         // FIXME CPU core num hardcoded
192         for ( i=0; i<4; i++)
193         {
194                 LOGI_("\tCPU freq #%d : %f\n",
195                 i,
196                 sys_info->cpu_frequency[i]
197                 );
198         }
199 //      char *p = sys_info;
200         printBuf((char*)sys_info, sizeof(*sys_info));
201
202         return 0;
203 }
204
205 int fill_data_msg_head (struct msg_data_t *data, uint32_t msgid,
206                                                         uint32_t seq, uint32_t len)
207 {
208         struct timeval time;
209         data->id = msgid;
210         data->seq_num = seq; // TODO fill good value
211         gettimeofday(&time, NULL);
212         data->sec = time.tv_sec;
213         data->nsec = time.tv_usec * 1000;
214         data->len = len;
215         return 0;
216 }
217
218
219 //allocate memory, need free!!!
220 struct msg_data_t *gen_message_terminate(uint32_t id)
221 {
222         uint32_t payload_len = sizeof(uint32_t);
223         struct msg_data_t *data = malloc(sizeof(*data) + payload_len);
224         char *p;
225         fill_data_msg_head(data,NMSG_TERMINATE, 0, payload_len);
226         // TODO fill good value
227         p = data->payload;
228         pack_int32(p, id);
229         return data;
230 }
231
232 //allocate memory, need free!!!
233 struct msg_data_t *gen_message_error(const char * err_msg)
234 {
235         int payload_len = strlen(err_msg)+1;
236         struct msg_data_t *data = malloc(sizeof(*data) + payload_len);
237         char *p;
238         fill_data_msg_head(data, NMSG_ERROR, 0, payload_len);
239
240         p = data->payload;
241         pack_str(p,err_msg);
242         return data;
243 }
244
245 //allocatr memory, need free!!!
246 struct msg_data_t *gen_message_event(
247                       struct input_event *events,
248                       uint32_t events_count,
249                       uint32_t id)
250 {
251         uint32_t i = 0;
252         uint32_t payload_len = events_count * (sizeof(id) * 4) +
253                                                         sizeof(events_count);
254         struct msg_data_t *data = malloc(sizeof(*data) + payload_len);
255         memset(data,0,sizeof(*data) + payload_len);
256         char *p;
257
258         fill_data_msg_head(data, NMSG_RECORD, 0, payload_len);
259
260         p = data->payload;
261         pack_int32(p, events_count);
262
263         for (i=0; i<events_count; i++){
264                 pack_int32(p,id);
265                 pack_int32(p, (int32_t) events[i].type);
266                 pack_int32(p, (int32_t) events[i].code);
267                 pack_int32(p,events[i].value);
268         }
269
270         return data;
271 }
272
273 void inline free_msg_data(struct msg_data_t *msg)
274 {
275         free(msg);
276 };