2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include "loader_info.h"
25 #include "launchpad_common.h"
27 #define TAG_LOADER "[LOADER]"
28 #define TAG_NAME "NAME"
30 #define TAG_APP_TYPE "APP_TYPE"
31 #define TAG_DETECTION_METHOD "DETECTION_METHOD"
32 #define TAG_ACTIVATION_METHOD "ACTIVATION_METHOD"
33 #define TAG_DEACTIVATION_METHOD "DEACTIVATION_METHOD"
35 #define TAG_TIMEOUT "TIMEOUT"
36 #define TAG_EXTRA "EXTRA"
37 #define TAG_EXTRA_ARRAY "EXTRA_ARRAY"
38 #define TAG_EXTRA_ARRAY_VAL "EXTRA_ARRAY_VAL"
39 #define TAG_ALTERNATIVE_LOADER "ALTERNATIVE_LOADER"
40 #define TAG_HW_ACC "HW_ACC"
41 #define TAG_CPU_THRESHOLD_MAX "CPU_THRESHOLD_MAX"
42 #define TAG_CPU_THRESHOLD_MIN "CPU_THRESHOLD_MIN"
43 #define TAG_ON_BOOT "ON_BOOT"
47 #define VAL_METHOD_TIMEOUT "TIMEOUT"
48 #define VAL_METHOD_DEMAND "DEMAND"
49 #define VAL_METHOD_VISIBILITY "VISIBILITY"
50 #define VAL_METHOD_REQUEST "REQUEST"
51 #define VAL_METHOD_AVAILABLE_MEMORY "AVAILABLE_MEMORY"
52 #define VAL_METHOD_TTL "TTL"
53 #define VAL_METHOD_OUT_OF_MEMORY "OUT_OF_MEMORY"
55 static loader_info_t *__create_loader_info()
59 info = malloc(sizeof(loader_info_t));
68 info->app_types = NULL;
70 info->alternative_loaders = NULL;
71 info->detection_method = METHOD_TIMEOUT | METHOD_VISIBILITY |
73 info->timeout_val = 5000;
74 info->extra = bundle_create();
75 info->cpu_threshold_max = DEFAULT_CPU_THRESHOLD_MAX;
76 info->cpu_threshold_min = DEFAULT_CPU_THRESHOLD_MIN;
78 info->app_exists = false;
79 info->activation_method = 0;
80 info->deactivation_method = 0;
81 info->ttl = 600; /* 10 minutes */
86 static void __parse_detection_method(loader_info_t *info, char *line)
91 token = strtok_r(line, " |\t\r\n", &savedptr);
92 info->detection_method = 0;
94 if (!strcmp(token, VAL_METHOD_TIMEOUT))
95 info->detection_method |= METHOD_TIMEOUT;
96 else if (!strcmp(token, VAL_METHOD_VISIBILITY))
97 info->detection_method |= METHOD_VISIBILITY;
98 else if (!strcmp(token, VAL_METHOD_DEMAND))
99 info->detection_method |= METHOD_DEMAND;
101 token = strtok_r(NULL, " |\t\r\n", &savedptr);
104 info->detection_method |= METHOD_INSTALL;
105 _D("detection_method:%d", info->detection_method);
108 static void __parse_activation_method(loader_info_t *info, char *line)
113 token = strtok_r(line, " |\t\r\n", &savedptr);
114 info->activation_method = 0;
116 if (!strcmp(token, VAL_METHOD_REQUEST))
117 info->activation_method |= METHOD_REQUEST;
118 else if (!strcmp(token, VAL_METHOD_AVAILABLE_MEMORY))
119 info->activation_method |= METHOD_AVAILABLE_MEMORY;
121 token = strtok_r(NULL, " |\t\r\n", &savedptr);
124 _D("activation_method:%d", info->activation_method);
127 static void __parse_deactivation_method(loader_info_t *info, char *line)
132 token = strtok_r(line, " |\t\r\n", &savedptr);
133 info->deactivation_method = 0;
135 if (!strcmp(token, VAL_METHOD_TTL))
136 info->deactivation_method |= METHOD_TTL;
137 else if (!strcmp(token, VAL_METHOD_OUT_OF_MEMORY))
138 info->deactivation_method |= METHOD_OUT_OF_MEMORY;
140 token = strtok_r(NULL, " |\t\r\n", &savedptr);
143 _D("deactivation_method:%d", info->deactivation_method);
146 static void __parse_app_types(loader_info_t *info, char *line)
151 token = strtok_r(line, " |\t\r\n", &savedptr);
153 info->app_types = g_list_append(info->app_types, strdup(token));
154 token = strtok_r(NULL, " |\t\r\n", &savedptr);
158 static void __parse_extra(loader_info_t *info, char *line)
164 if (info->extra == NULL)
167 sscanf(line, "%ms %ms %ms", &tok1, &tok2, &tok3);
169 if (!tok1 || !tok2 || !tok3)
172 if (strlen(tok2) == 0 || strlen(tok3) == 0)
175 bundle_add_str(info->extra, tok2, tok3);
186 static void __add_extra_array_from_list(bundle *b, const char *key, GList *list)
193 if (b == NULL || key == NULL || list == NULL)
196 len = g_list_length(list);
197 array = malloc(sizeof(const char *) * len);
202 for (i = 0; i < len; i++) {
203 array[i] = cur->data;
204 cur = g_list_next(cur);
207 bundle_add_str_array(b, key, array, len);
211 static void __flush_extra_array(bundle *b, char *key, GList *list)
214 __add_extra_array_from_list(b, key, list);
215 g_list_free_full(list, free);
221 static GList *__parse_file(GList *list, const char *path)
227 loader_info_t *cur_info = NULL;
229 GList *extra_array = NULL;
231 fp = fopen(path, "rt");
235 while (fgets(buf, sizeof(buf), fp) != NULL) {
238 sscanf(buf, "%ms %ms", &tok1, &tok2);
239 if (tok1 && strcasecmp(TAG_LOADER, tok1) == 0) {
240 if (cur_info != NULL) {
241 __flush_extra_array(cur_info->extra, key,
245 list = g_list_append(list, cur_info);
247 cur_info = __create_loader_info();
253 if (!tok1 || !tok2 || !cur_info)
255 if (tok1[0] == '\0' || tok2[0] == '\0' || tok1[0] == '#')
258 if (strcasecmp(TAG_NAME, tok1) == 0) {
259 cur_info->name = strdup(tok2);
260 } else if (strcasecmp(TAG_EXE, tok1) == 0) {
261 cur_info->exe = strdup(tok2);
262 } else if (strcasecmp(TAG_APP_TYPE, tok1) == 0) {
263 __parse_app_types(cur_info, &buf[strlen(tok1)]);
264 } else if (strcasecmp(TAG_DETECTION_METHOD, tok1) == 0) {
265 __parse_detection_method(cur_info, &buf[strlen(tok1)]);
266 } else if (strcasecmp(TAG_ACTIVATION_METHOD, tok1) == 0) {
267 __parse_activation_method(cur_info,
269 } else if (strcasecmp(TAG_DEACTIVATION_METHOD, tok1) == 0) {
270 __parse_deactivation_method(cur_info,
272 } else if (strcasecmp(TAG_TTL, tok1) == 0) {
273 cur_info->ttl = strtoul(tok2, NULL, 10);
274 } else if (strcasecmp(TAG_TIMEOUT, tok1) == 0) {
275 cur_info->timeout_val = atoi(tok2);
276 } else if (strcasecmp(TAG_EXTRA, tok1) == 0) {
277 __parse_extra(cur_info, buf);
278 } else if (strcasecmp(TAG_EXTRA_ARRAY, tok1) == 0) {
279 __flush_extra_array(cur_info->extra, key, extra_array);
282 } else if (strcasecmp(TAG_EXTRA_ARRAY_VAL, tok1) == 0) {
283 extra_array = g_list_append(extra_array, strdup(tok2));
284 } else if (strcasecmp(TAG_HW_ACC, tok1) == 0) {
285 cur_info->hw_acc = strdup(tok2);
286 } else if (strcasecmp(TAG_ALTERNATIVE_LOADER, tok1) == 0) {
287 cur_info->alternative_loaders =
288 g_list_append(cur_info->alternative_loaders,
290 } else if (strcasecmp(TAG_CPU_THRESHOLD_MAX, tok1) == 0) {
291 cur_info->cpu_threshold_max = atoi(tok2);
292 } else if (strcasecmp(TAG_CPU_THRESHOLD_MIN, tok1) == 0) {
293 cur_info->cpu_threshold_min = atoi(tok2);
294 } else if (strcasecmp(TAG_ON_BOOT, tok1) == 0) {
295 if (tok2 && strcasecmp(VAL_OFF, tok2) == 0)
296 cur_info->on_boot = false;
300 if (cur_info != NULL) {
301 __flush_extra_array(cur_info->extra, key, extra_array);
302 list = g_list_append(list, cur_info);
315 GList *_loader_info_load(const char *path)
318 struct dirent *entry = NULL;
323 dir_info = opendir(path);
324 if (dir_info == NULL)
327 while ((entry = readdir(dir_info)) != NULL) {
328 if (entry->d_name[0] == '.')
330 ext = strrchr(entry->d_name, '.');
331 if (ext && !strcmp(ext, ".loader")) {
332 snprintf(buf, sizeof(buf), "%s/%s", path, entry->d_name);
333 list = __parse_file(list, buf);
341 static void __free_info(gpointer data)
348 info = (loader_info_t *)data;
353 g_list_free_full(info->app_types, free);
356 bundle_free(info->extra);
357 if (info->alternative_loaders)
358 g_list_free_full(info->alternative_loaders, free);
363 void _loader_info_dispose(GList *info)
365 g_list_free_full(info, __free_info);
368 static int __comp_str(gconstpointer a, gconstpointer b)
375 static int __comp_app_type_with_hw_acc(gconstpointer a, gconstpointer b)
377 loader_info_t *info = (loader_info_t *)a;
379 if (info == NULL || info->app_types == NULL || b == NULL)
382 if (g_list_find_custom(info->app_types, b, __comp_str) &&
383 (info->hw_acc == NULL || !strcasecmp(VAL_ON, info->hw_acc)))
389 static int __comp_app_type_with_sw_acc(gconstpointer a, gconstpointer b)
391 loader_info_t *info = (loader_info_t *)a;
393 if (info == NULL || info->app_types == NULL || b == NULL)
396 if (g_list_find_custom(info->app_types, b, __comp_str) &&
397 (info->hw_acc == NULL || !strcasecmp(VAL_OFF, info->hw_acc)))
403 static int __comp_name(gconstpointer a, gconstpointer b)
405 loader_info_t *info = (loader_info_t *)a;
407 if (info == NULL || info->name == NULL || b == NULL)
410 return strcmp(info->name, b);
413 int _loader_info_find_type(GList *info, const char *app_type, bool hwacc)
418 cur = g_list_find_custom(info, app_type,
419 __comp_app_type_with_hw_acc);
421 cur = g_list_find_custom(info, app_type,
422 __comp_app_type_with_sw_acc);
428 loader_info_t *cur_info = (loader_info_t *)cur->data;
430 return cur_info->type;
433 int _loader_info_find_type_by_loader_name(GList *info, const char *loader_name)
437 cur = g_list_find_custom(info, loader_name, __comp_name);
441 loader_info_t *cur_info = (loader_info_t *)cur->data;
443 return cur_info->type;
446 static int *__make_type_array(GList *info, GList *loaders, int *len)
455 l = g_list_length(loaders);
460 t = malloc(sizeof(int) * l);
468 c = g_list_find_custom(info, cur->data, __comp_name);
471 i = (loader_info_t *)c->data;
476 cur = g_list_next(cur);
482 int *_loader_get_alternative_types(GList *info, int type, int *len)
492 i = (loader_info_t *)cur->data;
493 if (i->type == type) {
494 if (!i->alternative_loaders)
497 return __make_type_array(info, i->alternative_loaders,
500 cur = g_list_next(cur);
506 int _loader_info_foreach(GList *info, loader_info_foreach_cb callback,
512 if (!info || !callback)
517 i = (loader_info_t *)cur->data;
519 cur = g_list_next(cur);
525 bool _loader_info_exist_app_type(loader_info_t *info, const char *app_type)
529 list = g_list_find_custom(info->app_types, app_type, __comp_str);