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"
40 #define TAG_ON_BOOT "ON_BOOT"
43 #define VAL_METHOD_TIMEOUT "TIMEOUT"
44 #define VAL_METHOD_DEMAND "DEMAND"
45 #define VAL_METHOD_VISIBILITY "VISIBILITY"
48 static loader_info_t *__create_loader_info()
52 info = malloc(sizeof(loader_info_t));
61 info->app_types = NULL;
63 info->alternative_loaders = NULL;
64 info->detection_method = METHOD_TIMEOUT | METHOD_VISIBILITY;
65 info->timeout_val = 5000;
66 info->extra = bundle_create();
67 info->cpu_threshold_max = DEFAULT_CPU_THRESHOLD_MAX;
68 info->cpu_threshold_min = DEFAULT_CPU_THRESHOLD_MIN;
74 static void __parse_detection_method(loader_info_t *info, char *line)
79 token = strtok_r(line, " |\t\r\n", &savedptr);
80 info->detection_method = 0;
82 if (!strcmp(token, VAL_METHOD_TIMEOUT))
83 info->detection_method |= METHOD_TIMEOUT;
84 else if (!strcmp(token, VAL_METHOD_VISIBILITY))
85 info->detection_method |= METHOD_VISIBILITY;
86 else if (!strcmp(token, VAL_METHOD_DEMAND))
87 info->detection_method |= METHOD_DEMAND;
89 token = strtok_r(NULL, " |\t\r\n", &savedptr);
92 _D("detection_method:%d", info->detection_method);
95 static void __parse_app_types(loader_info_t *info, char *line)
100 token = strtok_r(line, " |\t\r\n", &savedptr);
102 info->app_types = g_list_append(info->app_types, strdup(token));
103 token = strtok_r(NULL, " |\t\r\n", &savedptr);
107 static void __parse_extra(loader_info_t *info, char *line)
113 if (info->extra == NULL)
116 sscanf(line, "%ms %ms %ms", &tok1, &tok2, &tok3);
118 if (!tok1 || !tok2 || !tok3)
121 if (strlen(tok2) == 0 || strlen(tok3) == 0)
124 bundle_add_str(info->extra, tok2, tok3);
135 static void __add_extra_array_from_list(bundle *b, const char *key, GList *list)
142 if (b == NULL || key == NULL || list == NULL)
145 len = g_list_length(list);
146 array = malloc(sizeof(const char *) * len);
149 for (i = 0; i < len; i++) {
150 array[i] = cur->data;
151 cur = g_list_next(cur);
154 bundle_add_str_array(b, key, array, len);
158 static void __flush_extra_array(bundle *b, char *key, GList *list)
161 __add_extra_array_from_list(b, key, list);
162 g_list_free_full(list, free);
168 static GList *__parse_file(GList *list, const char *path)
174 loader_info_t *cur_info = NULL;
176 GList *extra_array = NULL;
178 fp = fopen(path, "rt");
182 while (fgets(buf, sizeof(buf), fp) != NULL) {
185 sscanf(buf, "%ms %ms", &tok1, &tok2);
186 if (tok1 && strcasecmp(TAG_LOADER, tok1) == 0) {
187 if (cur_info != NULL) {
188 __flush_extra_array(cur_info->extra, key,
192 list = g_list_append(list, cur_info);
194 cur_info = __create_loader_info();
200 if (!tok1 || !tok2 || !cur_info)
202 if (tok1[0] == '\0' || tok2[0] == '\0' || tok1[0] == '#')
205 if (strcasecmp(TAG_NAME, tok1) == 0) {
206 cur_info->name = strdup(tok2);
207 } else if (strcasecmp(TAG_EXE, tok1) == 0) {
208 cur_info->exe = strdup(tok2);
209 } else if (strcasecmp(TAG_APP_TYPE, tok1) == 0) {
210 __parse_app_types(cur_info, &buf[strlen(tok1)]);
211 } else if (strcasecmp(TAG_DETECTION_METHOD, tok1) == 0) {
212 __parse_detection_method(cur_info, &buf[strlen(tok1)]);
213 } else if (strcasecmp(TAG_TIMEOUT, tok1) == 0) {
214 cur_info->timeout_val = atoi(tok2);
215 } else if (strcasecmp(TAG_EXTRA, tok1) == 0) {
216 __parse_extra(cur_info, buf);
217 } else if (strcasecmp(TAG_EXTRA_ARRAY, tok1) == 0) {
218 __flush_extra_array(cur_info->extra, key, extra_array);
221 } else if (strcasecmp(TAG_EXTRA_ARRAY_VAL, tok1) == 0) {
222 extra_array = g_list_append(extra_array, strdup(tok2));
223 } else if (strcasecmp(TAG_HW_ACC, tok1) == 0) {
224 cur_info->hw_acc = strdup(tok2);
225 } else if (strcasecmp(TAG_ALTERNATIVE_LOADER, tok1) == 0) {
226 cur_info->alternative_loaders =
227 g_list_append(cur_info->alternative_loaders,
229 } else if (strcasecmp(TAG_CPU_THRESHOLD_MAX, tok1) == 0) {
230 cur_info->cpu_threshold_max = atoi(tok2);
231 } else if (strcasecmp(TAG_CPU_THRESHOLD_MIN, tok1) == 0) {
232 cur_info->cpu_threshold_min = atoi(tok2);
233 } else if (strcasecmp(TAG_ON_BOOT, tok1) == 0) {
234 if (tok2 && strcasecmp(VAL_OFF, tok2) == 0)
235 cur_info->on_boot = false;
239 if (cur_info != NULL) {
240 __flush_extra_array(cur_info->extra, key, extra_array);
241 list = g_list_append(list, cur_info);
254 GList *_loader_info_load(const char *path)
257 struct dirent *entry = NULL;
262 dir_info = opendir(path);
263 if (dir_info == NULL)
266 while ((entry = readdir(dir_info)) != NULL) {
267 if (entry->d_name[0] == '.')
269 ext = strrchr(entry->d_name, '.');
270 if (ext && !strcmp(ext, ".loader")) {
271 snprintf(buf, sizeof(buf), "%s/%s", path, entry->d_name);
272 list = __parse_file(list, buf);
280 static void __free_info(gpointer data)
287 info = (loader_info_t *)data;
292 g_list_free_full(info->app_types, free);
295 bundle_free(info->extra);
296 if (info->alternative_loaders)
297 g_list_free_full(info->alternative_loaders, free);
302 void _loader_info_dispose(GList *info)
304 g_list_free_full(info, __free_info);
307 static int __comp_str(gconstpointer a, gconstpointer b)
314 static int __comp_app_type_with_hw_acc(gconstpointer a, gconstpointer b)
316 loader_info_t *info = (loader_info_t *)a;
318 if (info == NULL || info->app_types == NULL || b == NULL)
321 if (g_list_find_custom(info->app_types, b, __comp_str) &&
322 (info->hw_acc == NULL || !strcasecmp(VAL_ON, info->hw_acc)))
328 static int __comp_app_type_with_sw_acc(gconstpointer a, gconstpointer b)
330 loader_info_t *info = (loader_info_t *)a;
332 if (info == NULL || info->app_types == NULL || b == NULL)
335 if (g_list_find_custom(info->app_types, b, __comp_str) &&
336 (info->hw_acc == NULL || !strcasecmp(VAL_OFF, info->hw_acc)))
342 static int __comp_name(gconstpointer a, gconstpointer b)
344 loader_info_t *info = (loader_info_t *)a;
346 if (info == NULL || info->name == NULL || b == NULL)
349 return strcmp(info->name, b);
352 int _loader_info_find_type(GList *info, const char *app_type, bool hwacc)
357 cur = g_list_find_custom(info, app_type,
358 __comp_app_type_with_hw_acc);
360 cur = g_list_find_custom(info, app_type,
361 __comp_app_type_with_sw_acc);
367 loader_info_t *cur_info = (loader_info_t *)cur->data;
369 return cur_info->type;
372 int _loader_info_find_type_by_loader_name(GList *info, const char *loader_name)
376 cur = g_list_find_custom(info, loader_name, __comp_name);
380 loader_info_t *cur_info = (loader_info_t *)cur->data;
382 return cur_info->type;
385 static int *__make_type_array(GList *info, GList *loaders, int *len)
394 l = g_list_length(loaders);
399 t = malloc(sizeof(int) * l);
407 c = g_list_find_custom(info, cur->data, __comp_name);
410 i = (loader_info_t *)c->data;
415 cur = g_list_next(cur);
421 int *_loader_get_alternative_types(GList *info, int type, int *len)
431 i = (loader_info_t *)cur->data;
432 if (i->type == type) {
433 if (!i->alternative_loaders)
436 return __make_type_array(info, i->alternative_loaders,
439 cur = g_list_next(cur);