2 * Copyright (C) 2013 Paul Kocialkowski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #define LOG_TAG "exynos_param"
26 #include <utils/Log.h>
28 #include "exynos_camera.h"
30 int list_head_insert(struct list_head *list, struct list_head *prev,
31 struct list_head *next)
47 void list_head_remove(struct list_head *list)
52 if(list->next != NULL)
53 list->next->prev = list->prev;
54 if(list->prev != NULL)
55 list->prev->next = list->next;
58 int exynos_param_register(struct exynos_camera *exynos_camera, char *key,
59 union exynos_param_data data, enum exynos_param_type type)
61 struct list_head *list_end;
62 struct list_head *list;
63 struct exynos_param *param;
65 if (exynos_camera == NULL || key == NULL)
68 param = (struct exynos_param *) calloc(1, sizeof(struct exynos_param));
72 param->key = strdup(key);
74 case EXYNOS_PARAM_INT:
75 param->data.integer = data.integer;
77 case EXYNOS_PARAM_FLOAT:
78 param->data.floating = data.floating;
80 case EXYNOS_PARAM_STRING:
81 param->data.string = strdup(data.string);
84 LOGE("%s: Invalid type", __func__);
89 list_end = (struct list_head *) exynos_camera->params;
90 while (list_end != NULL && list_end->next != NULL)
91 list_end = list_end->next;
93 list = (struct list_head *) param;
94 list_head_insert(list, list_end, NULL);
96 if (exynos_camera->params == NULL)
97 exynos_camera->params = param;
103 if (param->key != NULL)
112 void exynos_param_unregister(struct exynos_camera *exynos_camera,
113 struct exynos_param *param)
115 struct list_head *list;
117 if (exynos_camera == NULL || param == NULL)
120 list = (struct list_head *) exynos_camera->params;
121 while (list != NULL) {
122 if ((void *) list == (void *) param) {
123 list_head_remove(list);
125 if ((void *) list == (void *) exynos_camera->params)
126 exynos_camera->params = (struct exynos_param *) list->next;
128 if (param->type == EXYNOS_PARAM_STRING && param->data.string != NULL)
129 free(param->data.string);
131 memset(param, 0, sizeof(struct exynos_param));
142 struct exynos_param *exynos_param_find_key(struct exynos_camera *exynos_camera,
145 struct exynos_param *param;
146 struct list_head *list;
148 if (exynos_camera == NULL || key == NULL)
151 list = (struct list_head *) exynos_camera->params;
152 while (list != NULL) {
153 param = (struct exynos_param *) list;
154 if (param->key == NULL)
157 if (strcmp(param->key, key) == 0)
167 int exynos_param_data_set(struct exynos_camera *exynos_camera, char *key,
168 union exynos_param_data data, enum exynos_param_type type)
170 struct exynos_param *param;
172 if (exynos_camera == NULL || key == NULL)
176 if (strchr(key, '=') || strchr(key, ';'))
179 if (type == EXYNOS_PARAM_STRING && data.string != NULL &&
180 (strchr(data.string, '=') || strchr(data.string, ';')))
183 param = exynos_param_find_key(exynos_camera, key);
185 // The key isn't in the list yet
186 exynos_param_register(exynos_camera, key, data, type);
190 if (param->type != type)
191 LOGE("%s: Mismatching types for key %s", __func__, key);
193 if (param->type == EXYNOS_PARAM_STRING && param->data.string != NULL)
194 free(param->data.string);
197 case EXYNOS_PARAM_INT:
198 param->data.integer = data.integer;
200 case EXYNOS_PARAM_FLOAT:
201 param->data.floating = data.floating;
203 case EXYNOS_PARAM_STRING:
204 param->data.string = strdup(data.string);
207 LOGE("%s: Invalid type", __func__);
215 int exynos_param_data_get(struct exynos_camera *exynos_camera, char *key,
216 union exynos_param_data *data, enum exynos_param_type type)
218 struct exynos_param *param;
220 if (exynos_camera == NULL || key == NULL || data == NULL)
223 param = exynos_param_find_key(exynos_camera, key);
224 if (param == NULL || param->type != type)
227 memcpy(data, ¶m->data, sizeof(param->data));
232 int exynos_param_int_get(struct exynos_camera *exynos_camera,
235 union exynos_param_data data;
238 if (exynos_camera == NULL || key == NULL)
241 rc = exynos_param_data_get(exynos_camera, key, &data, EXYNOS_PARAM_INT);
243 LOGE("%s: Unable to get data for key %s", __func__, key);
250 float exynos_param_float_get(struct exynos_camera *exynos_camera,
253 union exynos_param_data data;
256 if (exynos_camera == NULL || key == NULL)
259 rc = exynos_param_data_get(exynos_camera, key, &data, EXYNOS_PARAM_FLOAT);
261 LOGE("%s: Unable to get data for key %s", __func__, key);
265 return data.floating;
268 char *exynos_param_string_get(struct exynos_camera *exynos_camera,
271 union exynos_param_data data;
274 if (exynos_camera == NULL || key == NULL)
277 rc = exynos_param_data_get(exynos_camera, key, &data, EXYNOS_PARAM_STRING);
279 LOGE("%s: Unable to get data for key %s", __func__, key);
286 int exynos_param_int_set(struct exynos_camera *exynos_camera,
287 char *key, int integer)
289 union exynos_param_data data;
292 if (exynos_camera == NULL || key == NULL)
295 data.integer = integer;
297 rc = exynos_param_data_set(exynos_camera, key, data, EXYNOS_PARAM_INT);
299 LOGE("%s: Unable to set data for key %s", __func__, key);
306 int exynos_param_float_set(struct exynos_camera *exynos_camera,
307 char *key, float floating)
309 union exynos_param_data data;
312 if (exynos_camera == NULL || key == NULL)
315 data.floating = floating;
317 rc = exynos_param_data_set(exynos_camera, key, data, EXYNOS_PARAM_FLOAT);
319 LOGE("%s: Unable to set data for key %s", __func__, key);
326 int exynos_param_string_set(struct exynos_camera *exynos_camera,
327 char *key, char *string)
329 union exynos_param_data data;
332 if (exynos_camera == NULL || key == NULL || string == NULL)
335 data.string = string;
337 rc = exynos_param_data_set(exynos_camera, key, data, EXYNOS_PARAM_STRING);
339 LOGE("%s: Unable to set data for key %s", __func__, key);
346 char *exynos_params_string_get(struct exynos_camera *exynos_camera)
348 struct exynos_param *param;
349 struct list_head *list;
355 if (exynos_camera == NULL)
358 list = (struct list_head *) exynos_camera->params;
359 while (list != NULL) {
360 param = (struct exynos_param *) list;
361 if (param->key == NULL)
362 goto list_continue_length;
364 length += strlen(param->key);
367 switch (param->type) {
368 case EXYNOS_PARAM_INT:
369 case EXYNOS_PARAM_FLOAT:
372 case EXYNOS_PARAM_STRING:
373 length += strlen(param->data.string);
376 LOGE("%s: Invalid type", __func__);
382 list_continue_length:
389 string = calloc(1, length);
392 list = (struct list_head *) exynos_camera->params;
393 while (list != NULL) {
394 param = (struct exynos_param *) list;
395 if (param->key == NULL)
398 l = sprintf(s, "%s=", param->key);
401 switch (param->type) {
402 case EXYNOS_PARAM_INT:
403 l = snprintf(s, 16, "%d", param->data.integer);
406 case EXYNOS_PARAM_FLOAT:
407 l = snprintf(s, 16, "%g", param->data.floating);
410 case EXYNOS_PARAM_STRING:
411 l = sprintf(s, "%s", param->data.string);
415 LOGE("%s: Invalid type", __func__);
419 if (list->next != NULL) {
434 int exynos_params_string_set(struct exynos_camera *exynos_camera, char *string)
436 union exynos_param_data data;
437 enum exynos_param_type type;
450 if (exynos_camera == NULL || string == NULL)
463 v = strchr(k+1, ';');
469 if (isdigit(k[0]) || k[0] == '-') {
470 type = EXYNOS_PARAM_INT;
472 for (i=0 ; k[i] != '\0' ; i++) {
474 type = EXYNOS_PARAM_FLOAT;
475 } else if (!isdigit(k[i])) {
476 type = EXYNOS_PARAM_STRING;
481 type = EXYNOS_PARAM_STRING;
485 case EXYNOS_PARAM_INT:
486 data.integer = atoi(value);
488 case EXYNOS_PARAM_FLOAT:
489 data.floating = atof(value);
491 case EXYNOS_PARAM_STRING:
495 LOGE("%s: Invalid type", __func__);
499 rc = exynos_param_data_set(exynos_camera, key, data, type);
501 LOGE("%s: Unable to set data for key %s", __func__, key);