1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#define LOG_TAG "EffectsFactoryState"
18
19#include "EffectsFactoryState.h"
20
21#include "log/log.h"
22
23list_elem_t *gLibraryList;
24list_elem_t *gSkippedEffects;
25list_sub_elem_t *gSubEffectList;
26pthread_mutex_t gLibLock = PTHREAD_MUTEX_INITIALIZER;
27
28list_elem_t *gLibraryFailedList;  //list of lib_failed_entry_t: libraries failed to load
29
30
31int findEffect(const effect_uuid_t *type,
32               const effect_uuid_t *uuid,
33               lib_entry_t **lib,
34               effect_descriptor_t **desc)
35{
36    list_elem_t *e = gLibraryList;
37    lib_entry_t *l = NULL;
38    effect_descriptor_t *d = NULL;
39    int found = 0;
40    int ret = 0;
41
42    while (e && !found) {
43        l = (lib_entry_t *)e->object;
44        list_elem_t *efx = l->effects;
45        while (efx) {
46            d = (effect_descriptor_t *)efx->object;
47            if (type != NULL && memcmp(&d->type, type, sizeof(effect_uuid_t)) == 0) {
48                found = 1;
49                break;
50            }
51            if (uuid != NULL && memcmp(&d->uuid, uuid, sizeof(effect_uuid_t)) == 0) {
52                found = 1;
53                break;
54            }
55            efx = efx->next;
56        }
57        e = e->next;
58    }
59    if (!found) {
60        ALOGV("findEffect() effect not found");
61        ret = -ENOENT;
62    } else {
63        ALOGV("findEffect() found effect: %s in lib %s", d->name, l->name);
64        *lib = l;
65        if (desc) {
66            *desc = d;
67        }
68    }
69
70    return ret;
71}
72
73int stringToUuid(const char *str, effect_uuid_t *uuid)
74{
75    int tmp[10];
76
77    if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
78            tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
79        return -EINVAL;
80    }
81    uuid->timeLow = (uint32_t)tmp[0];
82    uuid->timeMid = (uint16_t)tmp[1];
83    uuid->timeHiAndVersion = (uint16_t)tmp[2];
84    uuid->clockSeq = (uint16_t)tmp[3];
85    uuid->node[0] = (uint8_t)tmp[4];
86    uuid->node[1] = (uint8_t)tmp[5];
87    uuid->node[2] = (uint8_t)tmp[6];
88    uuid->node[3] = (uint8_t)tmp[7];
89    uuid->node[4] = (uint8_t)tmp[8];
90    uuid->node[5] = (uint8_t)tmp[9];
91
92    return 0;
93}
94
95int uuidToString(const effect_uuid_t *uuid, char *str, size_t maxLen)
96{
97
98    snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
99            uuid->timeLow,
100            uuid->timeMid,
101            uuid->timeHiAndVersion,
102            uuid->clockSeq,
103            uuid->node[0],
104            uuid->node[1],
105            uuid->node[2],
106            uuid->node[3],
107            uuid->node[4],
108            uuid->node[5]);
109
110    return 0;
111}
112
113
114void dumpEffectDescriptor(effect_descriptor_t *desc, char *str, size_t len, int indent) {
115    char s[256];
116    char ss[256];
117    char idt[indent + 1];
118
119    memset(idt, ' ', indent);
120    idt[indent] = 0;
121
122    str[0] = 0;
123
124    snprintf(s, sizeof(s), "%s%s / %s\n", idt, desc->name, desc->implementor);
125    strlcat(str, s, len);
126
127    uuidToString(&desc->uuid, s, sizeof(s));
128    snprintf(ss, sizeof(ss), "%s  UUID: %s\n", idt, s);
129    strlcat(str, ss, len);
130
131    uuidToString(&desc->type, s, sizeof(s));
132    snprintf(ss, sizeof(ss), "%s  TYPE: %s\n", idt, s);
133    strlcat(str, ss, len);
134
135    sprintf(s, "%s  apiVersion: %08X\n%s  flags: %08X\n", idt,
136            desc->apiVersion, idt, desc->flags);
137    strlcat(str, s, len);
138}
139