summaryrefslogtreecommitdiff
path: root/drivers/staging/nmf-cm/cm/engine/repository_mgt/src/repository_mgt.c
blob: f6ccb4a9992df7e8f6aa1c01cf54cb2fc371c603 (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
/*
 * 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/utils/inc/string.h>

#include <cm/engine/component/inc/component_type.h>
#include <cm/engine/component/inc/bind.h>
#include <cm/engine/configuration/inc/configuration.h>
#include <cm/engine/component/inc/introspection.h>
#include <cm/engine/os_adaptation_layer/inc/os_adaptation_layer.h>
#include <cm/engine/repository_mgt/inc/repository_mgt.h>
#include <cm/engine/api/repository_mgt_engine.h>
#include <cm/engine/trace/inc/trace.h>


#undef NHASH
#define NHASH 157       //Use a prime number!
#define MULT 17

static t_rep_component *componentCaches[NHASH];

static unsigned int repcomponentHash(const char *str)
{
    unsigned int h = 0;
    for(; *str; str++)
        h = MULT * h + *str;
    return h % NHASH;
}

static void repcomponentAdd(t_rep_component *component)
{
    unsigned int h = repcomponentHash(component->name);

    if(componentCaches[h] != NULL)
        componentCaches[h]->prev = component;
    component->next = componentCaches[h];
    component->prev = NULL;
    componentCaches[h] = component;
}

static void repcomponentRemove(t_rep_component *component)
{
    unsigned int h = repcomponentHash(component->name);

    if(component->prev != NULL)
        component->prev->next = component->next;
    if(component->next != NULL)
        component->next->prev = component->prev;
    if(component == componentCaches[h])
        componentCaches[h] = component->next;
}


PUBLIC t_cm_error cm_REP_lookupComponent(const char *name, t_rep_component **component)
{
    t_rep_component *tmp;

    for(tmp = componentCaches[repcomponentHash(name)]; tmp != NULL; tmp = tmp->next)
    {
        if(cm_StringCompare(name, tmp->name, MAX_TEMPLATE_NAME_LENGTH) == 0)
        {
            if(component != NULL)
                *component = tmp;
            return CM_OK;
        }
    }

    return CM_COMPONENT_NOT_FOUND;
}

t_elfdescription* cm_REP_getComponentFile(t_dup_char templateName, t_elfdescription* elfhandle)
{
    if(elfhandle == NULL)
    {
        t_rep_component *pRepComponent;

        for(pRepComponent = componentCaches[repcomponentHash(templateName)]; pRepComponent != NULL; pRepComponent = pRepComponent->next)
        {
            if(pRepComponent->name == templateName)
                return pRepComponent->elfhandle;
        }

        return NULL;
    }

    return elfhandle;
}


PUBLIC void cm_REP_Destroy(void)
{
	t_rep_component *component, *next;
	int i;

	for(i = 0; i < NHASH; i++)
	{
	    for (component = componentCaches[i]; component != NULL; component = next)
	    {
	        next = component->next;
	        cm_ELF_CloseFile(FALSE, component->elfhandle);
	        cm_StringRelease(component->name);
	        OSAL_Free(component);
	    }
	    componentCaches[i] = NULL;
	}
}

PUBLIC EXPORT_SHARED t_cm_error CM_ENGINE_GetRequiredComponentFiles(
        // IN
        t_action_to_do action,
        const t_cm_instance_handle client,
        const char *requiredItfClientName,
        const t_cm_instance_handle server,
        const char *providedItfServerName,
        // OUT component to be pushed
        char fileList[][MAX_INTERFACE_TYPE_NAME_LENGTH],
        t_uint32 listSize,
        // OUT interface information
        char type[MAX_INTERFACE_TYPE_NAME_LENGTH],
        t_uint32 *methodNumber)
{
    t_cm_error error;
    t_component_instance* compClient, *compServer;
    int n;

    OSAL_LOCK_API();

    // No component required
    for(n = 0; n < listSize; n++)
        fileList[n][0] = 0;

    compClient = cm_lookupComponent(client);
    compServer = cm_lookupComponent(server);
    switch(action)
    {
    case BIND_FROMUSER:{
        t_interface_provide_description itfProvide;

        // Check server validity
        if((error = cm_checkValidServer(compServer, providedItfServerName,
                &itfProvide)) == CM_OK)
        {
            cm_StringCopy(type, itfProvide.server->Template->provides[itfProvide.provideIndex].interface->type, MAX_INTERFACE_TYPE_NAME_LENGTH);

            cm_StringCopy(fileList[0], "_sk.", MAX_INTERFACE_TYPE_NAME_LENGTH);
            cm_StringConcatenate(fileList[0], itfProvide.server->Template->provides[itfProvide.provideIndex].interface->type, MAX_INTERFACE_TYPE_NAME_LENGTH);
        }
    } break;

    case BIND_TOUSER: {
        /* Get Components names for a BindComponentToCMCore */
        t_interface_require_description itfRequire;
        t_bool bindable;

        // Check client validity
        if((error = cm_checkValidClient(compClient, requiredItfClientName,
                &itfRequire, &bindable)) == CM_OK)
        {
            cm_StringCopy(type, itfRequire.client->Template->requires[itfRequire.requireIndex].interface->type, MAX_INTERFACE_TYPE_NAME_LENGTH);
            *methodNumber = itfRequire.client->Template->requires[itfRequire.requireIndex].interface->methodNumber;

            cm_StringCopy(fileList[0], "_st.", MAX_INTERFACE_TYPE_NAME_LENGTH);
            cm_StringConcatenate(fileList[0], itfRequire.client->Template->requires[itfRequire.requireIndex].interface->type, MAX_INTERFACE_TYPE_NAME_LENGTH);
        }
    }; break;

    case BIND_ASYNC: {
        /* Get Components names for an asynchronous binding */
        t_interface_require_description itfRequire;
        t_interface_provide_description itfProvide;
        t_bool bindable;

        // Check invalid binding
        if((error = cm_checkValidBinding(compClient, requiredItfClientName,
                compServer, providedItfServerName,
                &itfRequire, &itfProvide, &bindable)) == CM_OK)
        {
                if(compClient->Template->dspId != compServer->Template->dspId)
                {
                    cm_StringCopy(fileList[0], "_sk.", MAX_INTERFACE_TYPE_NAME_LENGTH);
                    cm_StringConcatenate(fileList[0], itfRequire.client->Template->requires[itfRequire.requireIndex].interface->type, MAX_INTERFACE_TYPE_NAME_LENGTH);

                    cm_StringCopy(fileList[1], "_st.", MAX_INTERFACE_TYPE_NAME_LENGTH);
                    cm_StringConcatenate(fileList[1], itfRequire.client->Template->requires[itfRequire.requireIndex].interface->type, MAX_INTERFACE_TYPE_NAME_LENGTH);
                }
                else
                {
                    cm_StringCopy(fileList[0], "_ev.", MAX_INTERFACE_TYPE_NAME_LENGTH);
                    cm_StringConcatenate(fileList[0], itfRequire.client->Template->requires[itfRequire.requireIndex].interface->type, MAX_INTERFACE_TYPE_NAME_LENGTH);
                }
        }
    }; break;

    case BIND_TRACE: {
        /* Get Components names for an asynchronous binding */
        t_interface_require_description itfRequire;
        t_interface_provide_description itfProvide;
        t_bool bindable;

        // Check invalid binding
        if((error = cm_checkValidBinding(compClient, requiredItfClientName,
                compServer, providedItfServerName,
                &itfRequire, &itfProvide, &bindable)) == CM_OK)
        {
            cm_StringCopy(fileList[0], "_tr.", MAX_INTERFACE_TYPE_NAME_LENGTH);
            cm_StringConcatenate(fileList[0], itfRequire.client->Template->requires[itfRequire.requireIndex].interface->type, MAX_INTERFACE_TYPE_NAME_LENGTH);
        }
    }; break;

    default:
        error = CM_OK;
        break;
    }

    if(error == CM_OK)
    {
        for(n = 0; n < listSize; n++)
        {
            t_rep_component *comp;

            // If already loaded, don't ask to load it and put the name to NULL
            if (fileList[n][0] != 0 &&
                    cm_REP_lookupComponent(fileList[n], &comp) == CM_OK)
                fileList[n][0] = 0;
        }
    }


    OSAL_UNLOCK_API();
    return error;
}

PUBLIC EXPORT_SHARED t_cm_error CM_ENGINE_PushComponent(const char *name, const void *data, t_cm_size size)
{
	t_rep_component *comp;
	t_cm_error error;

	OSAL_LOCK_API();

	if (cm_REP_lookupComponent(name, &comp) == CM_OK) {
		/* Component is already there: silently ignore it */
		OSAL_UNLOCK_API();
		return CM_OK;
	}

	comp = OSAL_Alloc(sizeof(*comp));
	if (comp == NULL) {
		OSAL_UNLOCK_API();
		return CM_NO_MORE_MEMORY;
	}

	comp->name = cm_StringDuplicate(name);
	if(comp->name == NULL)
	{
        OSAL_Free(comp);
        OSAL_UNLOCK_API();
        return CM_NO_MORE_MEMORY;
	}

    if((error = cm_ELF_CheckFile(
            data,
            FALSE,
            &comp->elfhandle)) != CM_OK) {
        cm_StringRelease(comp->name);
        OSAL_Free(comp);
        OSAL_UNLOCK_API();
        return error;
    }
/*
	if (OSAL_Copy(comp->data, data, size)) {
		OSAL_Free(comp);
		OSAL_UNLOCK_API();
		return CM_UNKNOWN_MEMORY_HANDLE;
	}*/

    repcomponentAdd(comp);

	OSAL_UNLOCK_API();
	return CM_OK;
}

PUBLIC EXPORT_SHARED t_cm_error CM_ENGINE_ReleaseComponent (const char *name)
{
	t_rep_component *component;
	t_cm_error err;

	OSAL_LOCK_API();
	err = cm_REP_lookupComponent(name , &component);

	if (CM_OK == err)
	{
	    repcomponentRemove(component);

	    cm_ELF_CloseFile(FALSE, component->elfhandle);
	    cm_StringRelease(component->name);
	    OSAL_Free(component);
	}

	OSAL_UNLOCK_API();

	return err;
}

PUBLIC EXPORT_SHARED t_bool CM_ENGINE_IsComponentCacheEmpty(void)
{
	int i;

	OSAL_LOCK_API();
	for(i = 0; i < NHASH; i++) {
		if (componentCaches[i] != NULL) {
			OSAL_UNLOCK_API();
			return FALSE;
		}
	}
	OSAL_UNLOCK_API();
	return TRUE;
}