summaryrefslogtreecommitdiff
path: root/drivers/staging/nmf-cm/cm/engine/component/src/introspection.c
blob: 7e2f7cd65d0e363f61d1d803f621da8a9930fe49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * Copyright (C) ST-Ericsson SA 2010
 * Author: Jean-Philippe FASSINO <jean-philippe.fassino@stericsson.com> for ST-Ericsson.
 * License terms: GNU General Public License (GPL) version 2.
 */
#include <cm/engine/component/inc/introspection.h>
#include <cm/engine/trace/inc/trace.h>
#include <cm/engine/utils/inc/string.h>

/*
 *
 */
t_cm_error cm_getComponentProperty(
	const t_component_instance *component,
	const char                 *propName,
    char                       value[MAX_PROPERTY_VALUE_LENGTH],
    t_uint32                   valueLength){
	t_component_template* template = component->Template;
    int i;

    for(i = 0; i < template->propertyNumber; i++) {
        if(cm_StringCompare(template->properties[i].name, propName, MAX_PROPERTY_NAME_LENGTH) == 0) {
        	cm_StringCopy(
                value,
                template->properties[i].value,
                valueLength);
            return CM_OK;
        }
    }

    return CM_NO_SUCH_PROPERTY;
}

/**
 *
 */
static t_attribute* cm_getAttributeDescriptor(
        const t_component_instance  *component,
        const char                  *attrName)
{
    int i;

    for(i = 0; i < component->Template->attributeNumber; i++)
    {
        if(cm_StringCompare(component->Template->attributes[i].name, attrName, MAX_ATTRIBUTE_NAME_LENGTH) == 0)
        {
            return &component->Template->attributes[i];
        }
    }

    return NULL;
}

t_dsp_address cm_getAttributeMpcAddress(
        const t_component_instance  *component,
        const char                  *attrName)
{
    t_attribute* attribute;
    t_uint32 dspAddress;

    if((attribute = cm_getAttributeDescriptor(component, attrName)) == NULL)
        return 0x0;

    cm_DSP_GetDspAddress(component->memories[attribute->memory.memory->id], &dspAddress);

    return (dspAddress +
            attribute->memory.offset);
}

t_cm_logical_address cm_getAttributeHostAddr(
        const t_component_instance  *component,
        const char                  *attrName)
{
    t_attribute* attribute;

    if((attribute = cm_getAttributeDescriptor(component, attrName)) == NULL)
        return 0x0;

    // TODO JPF: component->Template->attributes[i].memory.offset could be converted in byte during load
    return cm_DSP_GetHostLogicalAddress(component->memories[attribute->memory.memory->id]) +
            attribute->memory.offset * attribute->memory.memory->memEntSize;
}


t_cm_error cm_readAttribute(
        const t_component_instance* component,
        const char* attrName,
        t_uint32* value)
{
    t_attribute* attribute;
    t_cm_logical_address hostAddr;

    if((attribute = cm_getAttributeDescriptor(component, attrName)) == NULL)
    {
        ERROR("CM_NO_SUCH_ATTRIBUTE(%s, %s)\n", component->pathname, attrName, 0, 0, 0, 0);
        return CM_NO_SUCH_ATTRIBUTE;
    }

    // TODO JPF: component->Template->attributes[i].memory.offset could be converted in byte during load
    hostAddr = cm_DSP_GetHostLogicalAddress(component->memories[attribute->memory.memory->id]) +
            attribute->memory.offset * attribute->memory.memory->memEntSize;

    if(attribute->memory.memory->memEntSize != 2)
        *value = *((t_uint32 *)hostAddr) & ~MASK_BYTE3;
    else
        *value = *((t_uint16 *)hostAddr);

    LOG_INTERNAL(3, "cm_readAttribute: [%s:%s, %x]=%x\n",
            component->pathname, attrName, hostAddr, *value, 0, 0);

    return CM_OK;
}

t_uint32 cm_readAttributeNoError(
        const t_component_instance* component,
        const char* attrName)
{
    t_uint32 value;

    if(cm_readAttribute(component, attrName, &value) != CM_OK)
        value = 0;

    return value;
}

t_cm_error cm_writeAttribute(
        const t_component_instance* component,
        const char* attrName,
        t_uint32 value)
{
    t_attribute* attribute;
    t_cm_logical_address hostAddr;

    if((attribute = cm_getAttributeDescriptor(component, attrName)) == NULL)
    {
        ERROR("CM_NO_SUCH_ATTRIBUTE(%s, %s)\n", component->pathname, attrName, 0, 0, 0, 0);
        return CM_NO_SUCH_ATTRIBUTE;
    }

    // TODO JPF: component->Template->attributes[i].memory.offset could be converted in byte during load
    hostAddr = cm_DSP_GetHostLogicalAddress(component->memories[attribute->memory.memory->id]) +
            attribute->memory.offset * attribute->memory.memory->memEntSize;

    if(attribute->memory.memory->memEntSize != 2)
        *((t_uint32 *)hostAddr) = value & ~MASK_BYTE3;
    else
        *((t_uint16 *)hostAddr) = value;

    /* be sure attribute is write into memory */
    OSAL_mb();

    LOG_INTERNAL(3, "cm_writeAttribute: [%s:%s, %x]=%x\n",
            component->pathname, attrName, hostAddr, value, 0, 0);

    return CM_OK;
}


/**
 *
 */
t_dsp_address cm_getFunction(
        const t_component_instance* component,
        const char* interfaceName,
        const char* methodName)
{
    t_interface_provide_description itfProvide;
    t_interface_provide* provide;
    t_interface_provide_loaded* provideLoaded;
    t_cm_error error;
    int i;

    // Get interface description
    if((error = cm_getProvidedInterface(component, interfaceName, &itfProvide)) != CM_OK)
        return error;

    provide = &component->Template->provides[itfProvide.provideIndex];
    provideLoaded = &component->Template->providesLoaded[itfProvide.provideIndex];

    for(i = 0; i < provide->interface->methodNumber; i++)
    {
        if(cm_StringCompare(provide->interface->methodNames[i], methodName, MAX_INTERFACE_METHOD_NAME_LENGTH) == 0)
        {
            return provideLoaded->indexesLoaded[itfProvide.collectionIndex][i].methodAddresses;
        }
    }

    return 0x0;
}

/**
 *
 */
PRIVATE t_uint8 compareItfName(const char* simplename, const char* complexname, int *collectionIndex) {
    int i;

    // Search if simplename is a prefix of complexname ??
    for(i = 0; simplename[i] != 0; i++) {
        if(simplename[i] != complexname[i])
            return 1;                       // NO
    }

    // YES
    if(complexname[i] == '[') {
        // This is a collection
        int value = 0;
        i++;
        if(complexname[i] < '0' || complexname[i] > '9') {
            return 1;
        }
        for(; complexname[i] >= '0' && complexname[i] <= '9'; i++) {
            value = value * 10 + (complexname[i] - '0');
        }
        if(complexname[i++] != ']')
            return 1;
        *collectionIndex = value;
    } else
        *collectionIndex = -1;

    if(complexname[i] != 0) {
        // Complexe name has not been fully parsed -> different name
        return 1;
    }

    return 0;
}


/**
 *
 */
PUBLIC t_cm_error cm_getProvidedInterface(const t_component_instance* server,
        const char* itfName,
        t_interface_provide_description *itfProvide){
    int i;

    for(i = 0; i < server->Template->provideNumber; i++)
    {
        int collectionIndex;
        if(compareItfName(server->Template->provides[i].name, itfName, &collectionIndex) == 0)
        {
            t_interface_provide *provide = &server->Template->provides[i];
            if(collectionIndex >= 0)
            {
                if(! (provide->provideTypes & COLLECTION_PROVIDE)) {
                    ERROR("CM_NO_SUCH_PROVIDED_INTERFACE(%s, %s)\n",
                            server->pathname, itfName, 0, 0, 0, 0);
                    goto out;
                }
                if(collectionIndex >= provide->collectionSize) {
                    ERROR("CM_NO_SUCH_PROVIDED_INTERFACE(%s, %s): out of range [0..%d[\n",
                            server->pathname, itfName, provide->collectionSize,
                            0, 0, 0);
                    goto out;
                }
            }
            else
            {
                if(provide->provideTypes & COLLECTION_PROVIDE) {
                    ERROR("CM_NO_SUCH_PROVIDED_INTERFACE(%s, %s): interface is a collection [0..%d[\n",
                            server->pathname, itfName, provide->collectionSize,
                            0, 0, 0);
                    goto out;
                }
                collectionIndex = 0;
            }
            itfProvide->provideIndex = i;
            itfProvide->server = server;
            itfProvide->collectionIndex = collectionIndex;
            itfProvide->origName = itfName;
            return CM_OK;
        }
    }

    ERROR("CM_NO_SUCH_PROVIDED_INTERFACE(%s, %s)\n", server->pathname, itfName, 0, 0, 0, 0);
out:
    itfProvide->provideIndex = 0;
    itfProvide->server = NULL;
    itfProvide->collectionIndex = 0;
    itfProvide->origName = NULL;
    return CM_NO_SUCH_PROVIDED_INTERFACE;
}

/**
 *
 */
t_cm_error cm_getRequiredInterface(const t_component_instance* client,
        const char* itfName,
        t_interface_require_description *itfRequire){
    int i;

    for(i = 0; i < client->Template->requireNumber; i++) {
        int collectionIndex;
        if(compareItfName(client->Template->requires[i].name, itfName, &collectionIndex) == 0) {
            t_interface_require *require = &client->Template->requires[i];
             if(collectionIndex >= 0) {
                if(! (require->requireTypes & COLLECTION_REQUIRE)) {
                    ERROR("CM_NO_SUCH_REQUIRED_INTERFACE(%s, %s)\n",
                        client->pathname, itfName, 0, 0, 0, 0);
                    return CM_NO_SUCH_REQUIRED_INTERFACE;
                }
                if(collectionIndex >= require->collectionSize) {
                    ERROR("CM_NO_SUCH_REQUIRED_INTERFACE(%s, %s): out of range [0..%d[\n",
                        client->pathname, itfName, require->collectionSize,
                        0, 0, 0);
                    return CM_NO_SUCH_REQUIRED_INTERFACE;
                }
            } else {
                if(require->requireTypes & COLLECTION_REQUIRE) {
                    ERROR("CM_NO_SUCH_REQUIRED_INTERFACE(%s, %s): interface is a collection [0..%d[\n",
                        client->pathname, itfName, require->collectionSize,
                        0, 0, 0);
                    return CM_NO_SUCH_REQUIRED_INTERFACE;
                }
                collectionIndex = 0;
            }
            itfRequire->client = client;
            itfRequire->requireIndex = i;
            itfRequire->collectionIndex = collectionIndex;
            itfRequire->origName = itfName;
            return CM_OK;
        }
    }

    ERROR("CM_NO_SUCH_REQUIRED_INTERFACE(%s, %s)\n", client->pathname, itfName, 0, 0, 0, 0);
    return CM_NO_SUCH_REQUIRED_INTERFACE;
}