Feature Enhancement : Code is merged to Tizen 2.3 base code
[platform/core/multimedia/libmm-common.git] / mm_attrs_private.c
1 /*
2  * libmm-common
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jonghyuk Choi <jhchoi.choi@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22  
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <malloc.h>
28 #include <assert.h>
29 #include <stdbool.h>
30 #include "mm_debug.h"
31 #include "mm_attrs_private.h"
32
33 int mmf_value_init(mmf_value_t *value, int type)
34 {
35         return_val_if_fail(value, -1);
36         return_val_if_fail(MMF_IS_VALUE_TYPE(type), -1);
37         memset(value, 0, sizeof(*value));
38         value->type = type;
39         return 0;
40 }
41
42 int mmf_value_copy(mmf_value_t *dest, const mmf_value_t *src)
43 {
44         return_val_if_fail(dest && src && src->type == dest->type, -1);
45         switch (src->type) {
46         case MM_ATTRS_TYPE_INT:
47                 dest->value.i_val = src->value.i_val;
48                 break;
49         case MM_ATTRS_TYPE_DOUBLE:
50                 dest->value.d_val = src->value.d_val;
51                 break;
52         case MM_ATTRS_TYPE_STRING:
53                 if (dest->value.s_val) {
54                         free(dest->value.s_val);
55                         dest->value.s_val = NULL;
56                         dest->size = 0;
57                 }
58                 if (src->value.s_val) {
59                         dest->value.s_val = strdup(src->value.s_val);
60                         dest->size = src->size;
61                 }
62                 break;
63         case MM_ATTRS_TYPE_DATA:
64                 dest->value.p_val = src->value.p_val;
65                 dest->size = src->size;
66                 break;
67         default:
68                 break;
69         }
70         return 0;
71 }
72
73 int mmf_value_set_int(mmf_value_t *v, int ival)
74 {
75         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_INT, -1);
76         v->value.i_val = ival;
77         return 0;
78 }
79
80 int mmf_value_get_int(const mmf_value_t *v)
81 {
82         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_INT, -1);
83         return v->value.i_val;
84 }
85
86 int mmf_value_set_double(mmf_value_t *v, double dval)
87 {
88         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_DOUBLE, -1);
89         v->value.d_val = dval;
90         return 0;
91 }
92
93 double mmf_value_get_double(mmf_value_t *v)
94 {
95         return_val_if_fail(v->type == MMF_VALUE_TYPE_DOUBLE, -1);
96         return v->value.d_val;
97 }
98
99 int mmf_value_set_string(mmf_value_t *v, const char *sval,int size)
100 {
101         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_STRING, -1);
102         if (v->value.s_val != NULL) {
103                 free(v->value.s_val);
104                 v->value.s_val = NULL;
105                 v->size = 0;
106         }
107         if (sval){
108                 v->value.s_val = strdup(sval);
109                 v->size = size;
110                 }
111         else{
112                 v->value.s_val = NULL;
113                 v->size = 0;
114                 }
115         return 0;
116 }
117
118 const char* mmf_value_get_string(const mmf_value_t *v, int *size)
119 {
120         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_STRING, NULL);
121         *size=v->size;
122         return v->value.s_val;
123 }
124
125 int mmf_value_set_data(mmf_value_t *v, void *data,int size)
126 {
127         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_DATA, -1);
128         v->value.p_val = data;
129         v->size=size;
130         return 0;
131 }
132
133 void* mmf_value_get_data(const mmf_value_t *v,int *size)
134 {
135         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_DATA, NULL);
136         *size=v->size;
137         return v->value.p_val;
138 }
139
140 void mmf_value_dump(const mmf_value_t *value)
141 {
142         return_if_fail(value);
143         switch (value->type) {
144         case MMF_VALUE_TYPE_INT:
145                 //mmf_debug(MMF_DEBUG_LOG, "value[int]: %d\n", value->value.i_val);
146                 break;
147         case MMF_VALUE_TYPE_DOUBLE:
148                 //mmf_debug(MMF_DEBUG_LOG, "value[double]: %f\n", value->value.d_val);
149                 break;
150         case MMF_VALUE_TYPE_STRING:
151                 //mmf_debug(MMF_DEBUG_LOG, "value[string]: %s\n", value->value.s_val);
152                 break;
153         case MMF_VALUE_TYPE_DATA:
154                 //mmf_debug(MMF_DEBUG_LOG, "value[data]: %p\n", value->value.p_val);
155                 break;
156         default:
157                 //mmf_debug(MMF_DEBUG_LOG, "value invalid!!\n");
158                 break;
159         }
160 }
161
162 int mmf_value_clear(mmf_value_t *value)
163 {
164         return_val_if_fail(value, -1);
165         if (value->type == MMF_VALUE_TYPE_STRING) {
166                 if (value->value.s_val) {
167                         free(value->value.s_val);
168                         value->value.s_val = NULL;
169                         value->size = 0;
170                 }
171         }
172         return 0;
173 }
174
175 int mmf_value_spec_init(mmf_value_spec_t *vs, int vs_type)
176 {
177         return_val_if_fail(vs, -1);
178         memset(vs, 0, sizeof(*vs));
179         vs->type = vs_type;
180         return 0;
181 }
182
183 int mmf_value_spec_set_int_range(mmf_value_spec_t *vs, int min, int max, int dval)
184 {
185         return_val_if_fail(vs && vs->type == MMF_VALUE_SPEC_INT_RANGE, -1);
186
187         vs->spec.int_spec.range.min = min;
188         vs->spec.int_spec.range.max = max;
189         vs->spec.int_spec.range.dval = dval;
190
191         return 0;
192 }
193
194 int mmf_value_spec_get_int_range(mmf_value_spec_t *vs, int *min, int *max, int *dval)
195 {
196         return_val_if_fail(vs && min && max && dval && vs->type == MMF_VALUE_SPEC_INT_RANGE, -1);
197
198         *min = vs->spec.int_spec.range.min;
199         *max = vs->spec.int_spec.range.max;
200         *dval = vs->spec.int_spec.range.dval;
201
202         return 0;
203 }
204
205 int mmf_value_spec_set_int_array(mmf_value_spec_t *vs, const int *array, int count, int dval)
206 {
207         int *array_copy = NULL;
208
209         return_val_if_fail(vs && array && vs->type == MMF_VALUE_SPEC_INT_ARRAY && count > 0, -1);
210
211         array_copy = (int *) malloc(sizeof(int) * count);
212         if (array_copy == NULL) {
213                 debug_error("malloc failed");
214                 return -1;
215         }
216
217         memcpy(array_copy, array, sizeof(int) * count);
218         vs->spec.int_spec.array.array = array_copy;
219         vs->spec.int_spec.array.count = count;
220         vs->spec.int_spec.array.dval = dval;
221
222         return 0;
223 }
224
225 int mmf_value_spec_get_int_array(mmf_value_spec_t *vs, int **array, int *count, int *dval)
226 {
227         return_val_if_fail(vs && array && count && dval && vs->type == MMF_VALUE_SPEC_INT_ARRAY, -1);
228
229         *array = vs->spec.int_spec.array.array;
230         *count = vs->spec.int_spec.array.count;
231         *dval = vs->spec.int_spec.array.dval;
232
233         return 0;
234 }
235
236 int mmf_value_spec_set_double_range(mmf_value_spec_t *vs, double min, double max, double dval)
237 {
238         return_val_if_fail(vs && vs->type == MMF_VALUE_SPEC_DOUBLE_RANGE, -1);
239
240         vs->spec.double_spec.range.min = min;
241         vs->spec.double_spec.range.max = max;
242         vs->spec.double_spec.range.dval = dval;
243
244         return 0;
245 }
246
247 int mmf_value_spec_get_double_range(mmf_value_spec_t *vs, double *min, double *max, double *dval)
248 {
249         return_val_if_fail(vs && min && max && dval && vs->type == MMF_VALUE_SPEC_DOUBLE_RANGE, -1);
250
251         *min = vs->spec.double_spec.range.min;
252         *max = vs->spec.double_spec.range.max;
253         *dval = vs->spec.double_spec.range.dval;
254
255         return 0;
256 }
257
258 int mmf_value_spec_set_double_array(mmf_value_spec_t *vs, const double *array, int count, double dval)
259 {
260         double *array_copy = NULL;
261
262         return_val_if_fail(vs && array && vs->type == MMF_VALUE_SPEC_DOUBLE_ARRAY && count > 0, -1);
263
264         array_copy = (double *) malloc(sizeof(double) * count);
265         if (array_copy == NULL) {
266                 debug_error("malloc failed");
267                 return -1;
268         }
269
270         memcpy(array_copy, array, sizeof(double) * count);
271         vs->spec.double_spec.array.array = array_copy;
272         vs->spec.double_spec.array.count = count;
273         vs->spec.double_spec.array.dval = dval;
274
275         return 0;
276 }
277
278 int mmf_value_spec_get_double_array(mmf_value_spec_t *vs, double **array, int *count, double *dval)
279 {
280         return_val_if_fail(vs && array && count && dval && vs->type == MMF_VALUE_SPEC_DOUBLE_ARRAY, -1);
281
282         *array = vs->spec.double_spec.array.array;
283         *count = vs->spec.double_spec.array.count;
284         *dval = vs->spec.double_spec.array.dval;
285
286         return 0;
287 }
288
289 int mmf_value_spec_clear(mmf_value_spec_t *vs)
290 {
291         return_val_if_fail(vs, -1);
292         switch(vs->type) {
293         case MMF_VALUE_SPEC_INT_ARRAY:
294                 if (vs->spec.int_spec.array.array) {
295                         free(vs->spec.int_spec.array.array);
296                         vs->spec.int_spec.array.array = NULL;
297                         vs->spec.int_spec.array.count = 0;
298                         vs->spec.int_spec.array.dval = 0;
299                 }
300                 break;
301         case MMF_VALUE_SPEC_DOUBLE_ARRAY:
302                 if (vs->spec.double_spec.array.array) {
303                         free(vs->spec.double_spec.array.array);
304                         vs->spec.double_spec.array.array = NULL;
305                         vs->spec.double_spec.array.count = 0;
306                         vs->spec.double_spec.array.dval = 0;
307                 }
308                 break;
309
310         default:
311                 break;
312         }
313
314         return 0;
315 }
316
317 int mmf_attribute_init(mmf_attribute_t *item, const char *name, int value_type, int flags)
318 {
319         return_val_if_fail(item && name, -1);
320
321         item->name = strdup(name);
322         item->flags = flags;
323
324         mmf_value_spec_init(&item->value_spec, MMF_VALUE_SPEC_NONE);
325         mmf_value_init(&item->value, value_type);
326         mmf_value_init(&item->tmpval, value_type);
327
328         return 0;
329 }
330
331 bool mmf_attribute_check_flags(mmf_attribute_t *item, int flags)
332 {
333         return_val_if_fail(item, false);
334         return item->flags & flags;
335 }
336
337 bool mmf_attribute_validate_int(mmf_attribute_t *item, int val)
338 {
339         return_val_if_fail(item, false);
340         return_val_if_fail(item->value.type == MMF_VALUE_TYPE_INT, false);
341         
342         bool valid = true;
343         int i = 0;
344         
345         switch (item->value_spec.type) {
346         case MMF_VALUE_SPEC_INT_RANGE:
347                 if (val < item->value_spec.spec.int_spec.range.min || 
348                                 val > item->value_spec.spec.int_spec.range.max) {
349                         valid = false;
350                         //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of range\n", item->name);
351                 }
352                 break;
353         case MMF_VALUE_SPEC_INT_ARRAY:
354                 valid = false;
355                 for (i = 0; i < item->value_spec.spec.int_spec.array.count; i++) {
356                         if (val == item->value_spec.spec.int_spec.array.array[i]) {
357                                 valid = true;
358                                 break;
359                         }
360                 }
361                 if (!valid) {
362                         //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of array\n", item->name);
363                 }
364                 break;
365         default:
366                 break;          
367         }
368         
369         return valid;
370 }
371
372 bool mmf_attribute_validate_double(mmf_attribute_t *item, double val)
373 {
374         return_val_if_fail(item, false);
375         return_val_if_fail(item->value.type == MMF_VALUE_TYPE_DOUBLE, false);
376         
377         bool valid = true;
378         int i = 0;
379         
380         switch (item->value_spec.type) {
381         case MMF_VALUE_SPEC_DOUBLE_RANGE:
382                 if (val < item->value_spec.spec.double_spec.range.min || 
383                                 val > item->value_spec.spec.double_spec.range.max) {
384                         valid = false;
385                         //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of range\n", item->name);
386                 }
387                 break;
388         case MMF_VALUE_SPEC_DOUBLE_ARRAY:
389                 valid = false;
390                 for (i = 0; i < item->value_spec.spec.double_spec.array.count; i++) {
391                         if (val == item->value_spec.spec.double_spec.array.array[i]) {
392                                 valid = true;
393                                 break;
394                         }
395                 }
396                 if (!valid) {
397                         //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of array\n", item->name);
398                 }
399                 break;
400         default:
401                 break;          
402         }
403         
404         return valid;
405 }
406
407 void mmf_attribute_clear(mmf_attribute_t *item)
408 {
409         assert(item);
410         if (item->name) {
411                 free(item->name);
412                 item->name = NULL;
413         }
414
415         mmf_value_clear(&item->tmpval);
416         mmf_value_clear(&item->value);
417         mmf_value_spec_clear(&item->value_spec);
418 }
419
420 bool mmf_attribute_is_modified(mmf_attribute_t *item)
421 {
422         return_val_if_fail(item, false);
423         return (item->flags & MM_ATTRS_FLAG_MODIFIED);
424 }
425
426 void mmf_attribute_set_modified(mmf_attribute_t *item)
427 {
428         return_if_fail(item);
429         if (!(item->flags & MM_ATTRS_FLAG_MODIFIED)) {
430                 mmf_value_copy(&item->tmpval, &item->value);
431                 item->flags |= MM_ATTRS_FLAG_MODIFIED;
432         }
433 }
434
435 void mmf_attribute_set_readonly(mmf_attribute_t *item)
436 {
437         return_if_fail(item);
438         if (item->flags & MM_ATTRS_FLAG_WRITABLE)
439                 item->flags -= MM_ATTRS_FLAG_WRITABLE;
440 }
441
442 void mmf_attribute_set_disabled(mmf_attribute_t *item)
443 {
444         return_if_fail(item);
445         if (item->flags & MM_ATTRS_FLAG_WRITABLE)
446                 item->flags -= MM_ATTRS_FLAG_WRITABLE;
447         if (item->flags & MM_ATTRS_FLAG_READABLE)
448                 item->flags -= MM_ATTRS_FLAG_READABLE;
449         if (item->flags & MM_ATTRS_FLAG_MODIFIED)
450                 item->flags -= MM_ATTRS_FLAG_MODIFIED;
451 }
452
453 void mmf_attribute_commit(mmf_attribute_t *item)
454 {
455         return_if_fail(item);
456         if (item->flags & MM_ATTRS_FLAG_MODIFIED) {
457                 mmf_value_copy(&item->value, &item->tmpval);
458                 mmf_value_clear(&item->tmpval);
459                 item->flags ^= MM_ATTRS_FLAG_MODIFIED;
460         }
461 }
462
463 int mmf_attribute_set_int(mmf_attribute_t *item, int val)
464 {
465         return_val_if_fail(item, -1);
466         if (mmf_value_set_int(&item->tmpval, val) == 0) {
467                 item->flags |= MM_ATTRS_FLAG_MODIFIED;
468                 return 0;
469         }
470         return -1;
471 }
472
473 int mmf_attribute_set_double(mmf_attribute_t *item, double val)
474 {
475         return_val_if_fail(item, -1);
476         if (mmf_value_set_double(&item->tmpval, val) == 0) {
477                 item->flags |= MM_ATTRS_FLAG_MODIFIED;
478                 return 0;
479         }
480         return -1;
481 }
482
483 int mmf_attribute_set_string(mmf_attribute_t *item, const char *string, int size)
484 {
485         return_val_if_fail(item, -1);
486
487         if (mmf_value_set_string(&item->tmpval, string,size) == 0) {
488                 if (string) 
489                         item->flags |= MM_ATTRS_FLAG_MODIFIED; 
490                 
491                 return 0;
492         }
493         return -1;
494 }
495
496 int mmf_attribute_set_data(mmf_attribute_t *item, void *data, int size)
497 {
498         return_val_if_fail(item, -1);
499
500         if (mmf_value_set_data(&item->tmpval, data,size) == 0) {
501                 item->flags |= MM_ATTRS_FLAG_MODIFIED;
502                 return 0;
503         }
504         return -1;
505 }
506
507 MMHandleType mmf_attrs_new(int count)
508 {
509         return_val_if_fail(count > 0, 0);
510         mmf_attrs_t *attrs;
511         attrs = (mmf_attrs_t *) malloc (sizeof(mmf_attrs_t));
512
513         if (attrs == NULL) {
514                 debug_error("malloc failed");
515                 return 0;
516         }
517
518         attrs->count = count;
519         attrs->items = (mmf_attribute_t *) malloc (sizeof(mmf_attribute_t) * count);
520         memset(attrs->items, 0, sizeof(mmf_attribute_t) * count);
521
522         if (pthread_mutex_init(&attrs->write_lock, NULL) != 0) {
523                 //mmf_debug(MMF_DEBUG_ERROR, "mutex init failed");
524                 if (attrs) {
525                         if (attrs->items) {
526                                 free(attrs->items);
527                                 attrs->items = NULL;
528                         }
529                         free(attrs);
530                         attrs=NULL;
531                 }
532                 return 0;
533         }
534
535         return (MMHandleType) attrs;
536 }
537
538 MMHandleType mmf_attrs_new_from_data(const char *name,
539                                      mmf_attrs_construct_info_t *info,
540                                      int count,
541                                      mmf_attrs_commit_func_t commit_func,
542                                      void *commit_param)
543 {
544         return_val_if_fail(info && count > 0, 0);
545         MMHandleType h;
546         mmf_attrs_t *attrs;
547
548         h = mmf_attrs_new(count);
549         if(!h) {
550                 return 0;
551         }
552         mmf_attrs_init(h, info, count);
553         attrs = (mmf_attrs_t *) h;
554         attrs->name = NULL;
555         if (name)
556                 attrs->name = strdup(name);
557         attrs->commit_func = commit_func;
558         attrs->commit_param = commit_param;
559         return h;
560 }
561
562 void mmf_attrs_free(MMHandleType h)
563 {
564         return_if_fail(h);
565         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
566         if (attrs) {
567                 if (attrs->name) {
568                         free(attrs->name);
569                         attrs->name = NULL;
570                 }
571                 if (attrs->items) {
572                         int i;
573                         for (i = 0; i < attrs->count; i++) {
574                                 mmf_attribute_clear(&attrs->items[i]);
575                         }
576                         free(attrs->items);
577                         attrs->items = NULL;
578                 }
579                 pthread_mutex_destroy(&attrs->write_lock);
580                 free(attrs);
581         }
582 }
583
584 int mmf_attrs_init(MMHandleType h, mmf_attrs_construct_info_t *info, int count)
585 {
586         return_val_if_fail(h && info && count > 0, -1);
587         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
588         int i;
589         int size = 0;
590
591         for (i = 0; i < count; i++) {
592                 mmf_attribute_init(&attrs->items[i],
593                                    info[i].name,
594                                    info[i].value_type,
595                                    info[i].flags);
596
597                 switch (info[i].value_type) {
598                 case MMF_VALUE_TYPE_INT:
599                         assert(attrs->items[i].value.value.i_val == 0);
600                         mmf_value_set_int(&attrs->items[i].value,
601                                           (int)info[i].default_value);
602                         break;
603                 case MMF_VALUE_TYPE_DOUBLE:
604                 {
605                         int i_val = (int)info[i].default_value;
606                         double d_val = (double) i_val;
607                         assert(attrs->items[i].value.value.d_val == 0);
608                         mmf_value_set_double(&attrs->items[i].value, d_val);
609                         break;
610                 }
611                 case MMF_VALUE_TYPE_STRING:
612                         assert(attrs->items[i].value.value.s_val == NULL);
613                         if (info[i].default_value) {
614                                 size = strlen(info[i].default_value)+1;
615                         }
616                         mmf_value_set_string(&attrs->items[i].value,
617                                              (const char *)info[i].default_value,size);
618                         break;
619                 case MMF_VALUE_TYPE_DATA:
620                         assert(attrs->items[i].value.value.p_val == NULL);
621                         if (info[i].default_value) {
622                                 size = sizeof(info[i].default_value)+1;
623                         }
624                         mmf_value_set_data(&attrs->items[i].value, info[i].default_value,size);
625                         break;
626                 default:
627                         //mmf_debug(MMF_DEBUG_LOG, "ERROR: Invalid MMF_VALUE_TYPE\n");
628                         assert(0);
629                         break;
630                 }
631         }
632
633         return 0;
634 }
635
636 int mmf_attrs_commit(MMHandleType h)
637 {
638         return_val_if_fail(h, -1);
639         
640         mmf_attrs_t *attrs = (mmf_attrs_t * )h;
641         int i;
642         int ret = 0;
643
644         MM_ATTRS_WRITE_LOCK(attrs);
645
646         for (i = 0; i < attrs->count; ++i) {
647                 if (mmf_attribute_is_modified(&attrs->items[i])) {
648                         if (attrs->commit_func) {
649                                 if (attrs->commit_func(i, attrs->items[i].name,
650                                                                                 &attrs->items[i].tmpval,
651                                                                                 attrs->commit_param)) {
652                                         mmf_attribute_commit(&attrs->items[i]);
653                                 } else {
654                                         /* without this, there is no way to solve modify when commit_func failed. */
655                                         if (attrs->items[i].flags & MM_ATTRS_FLAG_MODIFIED)
656                                                 attrs->items[i].flags ^= MM_ATTRS_FLAG_MODIFIED;
657                                         ret = -1;
658                                 }
659                         } else {
660                                 mmf_attribute_commit(&attrs->items[i]);
661                         }
662                 }
663         }
664
665         MM_ATTRS_WRITE_UNLOCK(attrs);
666
667         return ret;
668 }
669
670 int mmf_attrs_commit_err(MMHandleType h, char **err_attr_name)
671 {
672         mmf_attrs_t *attrs = (mmf_attrs_t * )h;
673         int i;
674         int ret = 0;
675
676         return_val_if_fail(h, -1);
677
678         MM_ATTRS_WRITE_LOCK(attrs);
679
680         for (i = 0; i < attrs->count; ++i) {
681                 if (mmf_attribute_is_modified(&attrs->items[i])) {
682                         if (attrs->commit_func) {
683                                 if (attrs->commit_func(i, attrs->items[i].name,
684                                                                                 &attrs->items[i].tmpval,
685                                                                                 attrs->commit_param)) {
686                                         mmf_attribute_commit(&attrs->items[i]);
687                                 } else {
688                                         /* without this, there is no way to solve modify when commit_func failed. */
689                                         if (attrs->items[i].flags & MM_ATTRS_FLAG_MODIFIED)
690                                                 attrs->items[i].flags ^= MM_ATTRS_FLAG_MODIFIED;
691                                         ret = -1;
692
693                                         /* Set Error information */
694                                         if (err_attr_name)
695                                                 *err_attr_name = strdup(attrs->items[i].name);
696
697                                         break;
698                                 }
699                         } else {
700                                 mmf_attribute_commit(&attrs->items[i]);
701                         }
702                 }
703         }
704
705         MM_ATTRS_WRITE_UNLOCK(attrs);
706
707         return ret;
708 }
709
710 int mmf_attrs_set_valid_type(MMHandleType h, int idx, int v_type)
711 {
712         return_val_if_fail(h, -1);
713         return_val_if_fail(idx>=0, -1);
714         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
715         return mmf_value_spec_init(&attrs->items[idx].value_spec, v_type);
716 }
717
718 int mmf_attrs_set_valid_range(MMHandleType h, int idx, int min, int max, int dval)
719 {
720         return_val_if_fail(h, -1);
721         return_val_if_fail(idx>=0, -1);
722         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
723         mmf_value_spec_clear(&attrs->items[idx].value_spec);
724         assert(attrs->items[idx].value_spec.type == MMF_VALUE_SPEC_INT_RANGE);
725         return mmf_value_spec_set_int_range(&attrs->items[idx].value_spec, min, max, dval);
726 }
727
728 int mmf_attrs_set_valid_array(MMHandleType h, int idx, const int *array, int count, int dval)
729 {
730         return_val_if_fail(h, -1);
731         return_val_if_fail(idx>=0, -1);
732         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
733         mmf_value_spec_clear(&attrs->items[idx].value_spec);
734         assert(attrs->items[idx].value_spec.type == MMF_VALUE_SPEC_INT_ARRAY);
735         return mmf_value_spec_set_int_array(&attrs->items[idx].value_spec, array, count, dval);
736 }
737
738 int mmf_attrs_set_valid_double_range(MMHandleType h, int idx, double min, double max, double dval)
739 {
740         return_val_if_fail(h, -1);
741         return_val_if_fail(idx>=0, -1);
742         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
743         mmf_value_spec_clear(&attrs->items[idx].value_spec);
744         assert(attrs->items[idx].value_spec.type == MMF_VALUE_SPEC_DOUBLE_RANGE);
745         return mmf_value_spec_set_double_range(&attrs->items[idx].value_spec, min, max, dval);
746 }
747
748 int mmf_attrs_set_valid_double_array(MMHandleType h, int idx, const double *array, int count, double dval)
749 {
750         return_val_if_fail(h, -1);
751         return_val_if_fail(idx>=0, -1);
752         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
753         mmf_value_spec_clear(&attrs->items[idx].value_spec);
754         assert(attrs->items[idx].value_spec.type == MMF_VALUE_SPEC_DOUBLE_ARRAY);
755         return mmf_value_spec_set_double_array(&attrs->items[idx].value_spec, array, count, dval);
756 }