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_TIMEOUT "TIMEOUT"
33 #define TAG_EXTRA "EXTRA"
34 #define TAG_EXTRA_ARRAY "EXTRA_ARRAY"
35 #define TAG_EXTRA_ARRAY_VAL "EXTRA_ARRAY_VAL"
36 #define TAG_ALTERNATIVE_LOADER "ALTERNATIVE_LOADER"
37 #define TAG_HW_ACC "HW_ACC"
38 #define TAG_CPU_THRESHOLD_MAX "CPU_THRESHOLD_MAX"
39 #define TAG_CPU_THRESHOLD_MIN "CPU_THRESHOLD_MIN"
42 #define VAL_METHOD_TIMEOUT "TIMEOUT"
43 #define VAL_METHOD_DEMAND "DEMAND"
44 #define VAL_METHOD_VISIBILITY "VISIBILITY"
47 static loader_info_t *__create_loader_info()
49 loader_info_t *info = malloc(sizeof(loader_info_t));
57 info->app_types = NULL;
59 info->alternative_loaders = NULL;
60 info->detection_method = METHOD_TIMEOUT | METHOD_VISIBILITY;
61 info->timeout_val = 5000;
62 info->extra = bundle_create();
63 info->cpu_threshold_max = DEFAULT_CPU_THRESHOLD_MAX;
64 info->cpu_threshold_min = DEFAULT_CPU_THRESHOLD_MIN;
69 static void __parse_detection_method(loader_info_t *info, char *line)
74 token = strtok_r(line, " |\t\r\n", &savedptr);
75 info->detection_method = 0;
77 if (!strcmp(token, VAL_METHOD_TIMEOUT))
78 info->detection_method |= METHOD_TIMEOUT;
79 else if (!strcmp(token, VAL_METHOD_VISIBILITY))
80 info->detection_method |= METHOD_VISIBILITY;
81 else if (!strcmp(token, VAL_METHOD_DEMAND))
82 info->detection_method |= METHOD_DEMAND;
84 token = strtok_r(NULL, " |\t\r\n", &savedptr);
87 _D("detection_method:%d", info->detection_method);
90 static void __parse_app_types(loader_info_t *info, char *line)
95 token = strtok_r(line, " |\t\r\n", &savedptr);
97 info->app_types = g_list_append(info->app_types, strdup(token));
98 token = strtok_r(NULL, " |\t\r\n", &savedptr);
102 static void __parse_extra(loader_info_t *info, char *line)
108 if (info->extra == NULL)
111 sscanf(line, "%ms %ms %ms", &tok1, &tok2, &tok3);
113 if (!tok1 || !tok2 || !tok3)
116 if (strlen(tok2) == 0 || strlen(tok3) == 0)
119 bundle_add_str(info->extra, tok2, tok3);
130 static void __add_extra_array_from_list(bundle *b, const char *key, GList *list)
137 if (b == NULL || key == NULL || list == NULL)
140 len = g_list_length(list);
141 array = malloc(sizeof(const char *) * len);
144 for (i = 0; i < len; i++) {
145 array[i] = cur->data;
146 cur = g_list_next(cur);
149 bundle_add_str_array(b, key, array, len);
153 static void __flush_extra_array(bundle *b, char *key, GList *list)
156 __add_extra_array_from_list(b, key, list);
157 g_list_free_full(list, free);
163 static GList *__parse_file(GList *list, const char *path)
169 loader_info_t *cur_info = NULL;
171 GList *extra_array = NULL;
173 fp = fopen(path, "rt");
177 while (fgets(buf, sizeof(buf), fp) != NULL) {
180 sscanf(buf, "%ms %ms", &tok1, &tok2);
181 if (tok1 && strcasecmp(TAG_LOADER, tok1) == 0) {
182 if (cur_info != NULL) {
183 __flush_extra_array(cur_info->extra, key,
187 list = g_list_append(list, cur_info);
189 cur_info = __create_loader_info();
193 if (!tok1 || !tok2 || !cur_info)
195 if (tok1[0] == '\0' || tok2[0] == '\0' || tok1[0] == '#')
198 if (strcasecmp(TAG_NAME, tok1) == 0) {
199 cur_info->name = strdup(tok2);
200 } else if (strcasecmp(TAG_EXE, tok1) == 0) {
201 cur_info->exe = strdup(tok2);
202 } else if (strcasecmp(TAG_APP_TYPE, tok1) == 0) {
203 __parse_app_types(cur_info, &buf[strlen(tok1)]);
204 } else if (strcasecmp(TAG_DETECTION_METHOD, tok1) == 0) {
205 __parse_detection_method(cur_info, &buf[strlen(tok1)]);
206 } else if (strcasecmp(TAG_TIMEOUT, tok1) == 0) {
207 cur_info->timeout_val = atoi(tok2);
208 } else if (strcasecmp(TAG_EXTRA, tok1) == 0) {
209 __parse_extra(cur_info, buf);
210 } else if (strcasecmp(TAG_EXTRA_ARRAY, tok1) == 0) {
211 __flush_extra_array(cur_info->extra, key, extra_array);
214 } else if (strcasecmp(TAG_EXTRA_ARRAY_VAL, tok1) == 0) {
215 extra_array = g_list_append(extra_array, strdup(tok2));
216 } else if (strcasecmp(TAG_HW_ACC, tok1) == 0) {
217 cur_info->hw_acc = strdup(tok2);
218 } else if (strcasecmp(TAG_ALTERNATIVE_LOADER, tok1) == 0) {
219 cur_info->alternative_loaders =
220 g_list_append(cur_info->alternative_loaders,
222 } else if (strcasecmp(TAG_CPU_THRESHOLD_MAX, tok1) == 0) {
223 cur_info->cpu_threshold_max = atoi(tok2);
224 } else if (strcasecmp(TAG_CPU_THRESHOLD_MIN, tok1) == 0) {
225 cur_info->cpu_threshold_min = atoi(tok2);
229 if (cur_info != NULL) {
230 __flush_extra_array(cur_info->extra, key, extra_array);
231 list = g_list_append(list, cur_info);
244 GList *_loader_info_load(const char *path)
247 struct dirent *entry = NULL;
252 dir_info = opendir(path);
253 if (dir_info == NULL)
256 while ((entry = readdir(dir_info)) != NULL) {
257 if (entry->d_name[0] == '.')
259 ext = strrchr(entry->d_name, '.');
260 if (ext && !strcmp(ext, ".loader")) {
261 snprintf(buf, sizeof(buf), "%s/%s", path, entry->d_name);
262 list = __parse_file(list, buf);
270 static void __free_info(gpointer data)
277 info = (loader_info_t *)data;
282 g_list_free_full(info->app_types, free);
285 bundle_free(info->extra);
286 if (info->alternative_loaders)
287 g_list_free_full(info->alternative_loaders, free);
292 void _loader_info_dispose(GList *info)
294 g_list_free_full(info, __free_info);
297 static int __comp_str(gconstpointer a, gconstpointer b)
304 static int __comp_app_type_with_hw_acc(gconstpointer a, gconstpointer b)
306 loader_info_t *info = (loader_info_t *)a;
308 if (info == NULL || info->app_types == NULL || b == NULL)
311 if (g_list_find_custom(info->app_types, b, __comp_str) &&
312 (info->hw_acc == NULL || !strcasecmp(VAL_ON, info->hw_acc)))
318 static int __comp_app_type_with_sw_acc(gconstpointer a, gconstpointer b)
320 loader_info_t *info = (loader_info_t *)a;
322 if (info == NULL || info->app_types == NULL || b == NULL)
325 if (g_list_find_custom(info->app_types, b, __comp_str) &&
326 (info->hw_acc == NULL || !strcasecmp(VAL_OFF, info->hw_acc)))
332 static int __comp_name(gconstpointer a, gconstpointer b)
334 loader_info_t *info = (loader_info_t *)a;
336 if (info == NULL || info->name == NULL || b == NULL)
339 return strcmp(info->name, b);
342 int _loader_info_find_type(GList *info, const char *app_type, bool hwacc)
347 cur = g_list_find_custom(info, app_type,
348 __comp_app_type_with_hw_acc);
350 cur = g_list_find_custom(info, app_type,
351 __comp_app_type_with_sw_acc);
357 loader_info_t *cur_info = (loader_info_t *)cur->data;
359 return cur_info->type;
362 int _loader_info_find_type_by_loader_name(GList *info, const char *loader_name)
366 cur = g_list_find_custom(info, loader_name, __comp_name);
370 loader_info_t *cur_info = (loader_info_t *)cur->data;
372 return cur_info->type;
375 static int *__make_type_array(GList *info, GList *loaders, int *len)
384 l = g_list_length(loaders);
389 t = malloc(sizeof(int) * l);
397 c = g_list_find_custom(info, cur->data, __comp_name);
400 i = (loader_info_t *)c->data;
405 cur = g_list_next(cur);
411 int *_loader_get_alternative_types(GList *info, int type, int *len)
421 i = (loader_info_t *)cur->data;
422 if (i->type == type) {
423 if (!i->alternative_loaders)
426 return __make_type_array(info, i->alternative_loaders,
429 cur = g_list_next(cur);