summaryrefslogtreecommitdiffstats
path: root/tinySIGCOMP/src/tcomp_manager.c
blob: 0818439791ac774a25dc841abf73e7cccc6ca8c3 (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*	
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/

/**@file tcomp_manager.c
 * @brief  SigComp Manager.
 *
 * @author Mamadou Diop <diopmamadou(at)yahoo.fr>
 *

 */
#include "tcomp_manager.h"
#include "tsk_debug.h"

#include "tcomp_compressordisp.h"
#include "tcomp_decompressordisp.h"
#include "tcomp_statehandler.h"

#include "tsk_object.h"
#include "tsk_safeobj.h"

#define MAX_DMS	131072
#define MAX_SMS	131072
#define MAX_CPB	128

/**@defgroup tcomp_manager_group SigComp manager.
*/

/**SigComp manager.
*/
typedef struct tcomp_manager_s
{
	TSK_DECLARE_OBJECT;
	
	tcomp_compressordisp_t *dispatcher_compressor;
	tcomp_decompressordisp_t *dispatcher_decompressor;
	tcomp_statehandler_t *stateHandler;
	
	TSK_DECLARE_SAFEOBJ;
}
tcomp_manager_t;


/**Creates new SigComp manager.
*/
tcomp_manager_handle_t* tcomp_manager_create()
{
	return tsk_object_new(tcomp_manager_def_t);
}

/** Defines whether the compressor must only use ACKed states (should be false)
*/
int tcomp_manager_setUseOnlyACKedStates(tcomp_manager_handle_t* self, tsk_bool_t useOnlyACKedStates)
{
	if(!self){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}
	return tcomp_statehandler_setUseOnlyACKedStates(((tcomp_manager_t*)self)->stateHandler, useOnlyACKedStates);
}

/**@ingroup tcomp_manager_group
*/
tsk_size_t tcomp_manager_compress(tcomp_manager_handle_t *handle, const void* compartmentId, tsk_size_t compartmentIdSize, const void* input_ptr, tsk_size_t input_size, void* output_ptr, tsk_size_t output_size, tsk_bool_t stream)
{
	tcomp_manager_t *manager = handle;
	tsk_size_t ret_size = output_size;

	if(!manager){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}
	
	if(tcomp_compressordisp_compress(manager->dispatcher_compressor, tcomp_buffer_createHash(compartmentId, compartmentIdSize), 
		input_ptr, input_size, output_ptr, &ret_size, stream))
	{
		return ret_size;
	}

	return 0;
}

/**@ingroup tcomp_manager_group
*/
tsk_size_t tcomp_manager_decompress(tcomp_manager_handle_t *handle, const void* input_ptr, tsk_size_t input_size, tcomp_result_t *lpResult)
{
	tcomp_manager_t *manager = handle;

	if(!manager){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}
	
	if(!lpResult || !lpResult->output_buffer){
		TSK_DEBUG_ERROR("You MUST initialize the sigcomp result and set a valid output buffer.");
		return 0;
	}

	/* Reset previous values */
	tcomp_result_reset(lpResult);
	
	if(tcomp_decompressordisp_decompress(manager->dispatcher_decompressor, input_ptr, input_size, lpResult)){
		return *tcomp_buffer_getIndexBytes(lpResult->output_buffer);
	}

	return 0;
}

/**@ingroup tcomp_manager_group
*/
tsk_size_t tcomp_manager_getNextStreamMessage(tcomp_manager_handle_t *handle, tcomp_result_t *lpResult)
{
	tcomp_manager_t *manager = handle;
	if(!manager){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}

	if(!lpResult || !tcomp_buffer_getSize(lpResult->output_buffer)){
		TSK_DEBUG_ERROR("Invalid result.");
		return 0;
	}

	if(!lpResult->isStreamBased){
		TSK_DEBUG_ERROR("You MUST provide stream buffer.");
		return 0;
	}

	_tcomp_result_reset(lpResult, tsk_false, tsk_false);
	
	if(tcomp_decompressordisp_getNextMessage(manager->dispatcher_decompressor, lpResult)){
		return *tcomp_buffer_getIndexBytes(lpResult->output_buffer);
	}

	return 0;
}

/**@ingroup tcomp_manager_group
*/
void tcomp_manager_provideCompartmentId(tcomp_manager_handle_t *handle, tcomp_result_t *lpResult)
{
	tcomp_manager_t *manager = handle;
	if(!manager){
		TSK_DEBUG_ERROR("Invalid parameter");
		return;
	}

	tcomp_statehandler_handleResult(manager->stateHandler, &lpResult);
}

/**@ingroup tcomp_manager_group
*/
void tcomp_manager_closeCompartment(tcomp_manager_handle_t *handle, const void *compartmentId, tsk_size_t compartmentIdSize)
{
	tcomp_manager_t *manager = handle;
	if(!manager){
		TSK_DEBUG_ERROR("Invalid parameter");
		return;
	}

	tcomp_statehandler_deleteCompartment(manager->stateHandler, tcomp_buffer_createHash(compartmentId, compartmentIdSize));
}

/**@ingroup tcomp_manager_group
* Sets the decompression memory size (RFC 3320 section 3.3).
* @param handle The SigComp manager.
* @param dms The new decompression memory size value.
* @retval Zero if succeed and non-zero error code otherwise.
*/
int tcomp_manager_setDecompression_Memory_Size(tcomp_manager_handle_t *handle, uint32_t dms)
{
	tcomp_manager_t *manager = handle;
	if(!manager){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}
	
	return tcomp_params_setDmsValue(manager->stateHandler->sigcomp_parameters, (dms > MAX_DMS ? MAX_DMS : dms));
}

/**@ingroup tcomp_manager_group
* Gets the decompression memory size (RFC 3320 section 3.3).
* @param handle The SigComp manager.
* @retval The current decompression memory size value.
*/
uint32_t tcomp_manager_getDecompression_Memory_Size(tcomp_manager_handle_t *handle)
{
	tcomp_manager_t *manager = handle;
	if(!manager){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}
	return (manager->stateHandler && manager->stateHandler->sigcomp_parameters)
		? manager->stateHandler->sigcomp_parameters->dmsValue
		: 0;
}

/**@ingroup tcomp_manager_group
* Sets the state memory size (RFC 3320 section 3.3).
* @param handle The SigComp manager.
* @param sms The new state memory size value.
* @retval Zero if succeed and non-zero error code otherwise.
*/
int tcomp_manager_setState_Memory_Size(tcomp_manager_handle_t *handle, uint32_t sms)
{
	tcomp_manager_t *manager = handle;
	if(!manager){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	return tcomp_params_setSmsValue(manager->stateHandler->sigcomp_parameters, (sms > MAX_SMS ? MAX_SMS : sms));
}

/**@ingroup tcomp_manager_group
* Sets the Cycle Per Bit (RFC 3320 section 3.3).
* @param handle The SigComp manager.
* @param cpb The new cycle per bit value.
* @retval Zero if succeed and non-zero error code otherwise.
*/
int tcomp_manager_setCycles_Per_Bit(tcomp_manager_handle_t *handle, uint8_t cpb)
{
	tcomp_manager_t *manager = handle;
	if(!manager){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	return tcomp_params_setCpbValue(manager->stateHandler->sigcomp_parameters, (cpb > MAX_CPB ? MAX_CPB : cpb));
}

/**@ingroup tcomp_manager_group
* Sets the SigComp version (RFC 3320 section 3.3).
* @param handle The SigComp manager.
* @param version The SigComp version. Only 2.0 is supported.
* @retval Zero if succeed and non-zero error code otherwise.
*/
int tcomp_manager_setSigComp_Version(tcomp_manager_handle_t *handle, uint8_t version)
{
	tcomp_manager_t *manager = handle;
	if(!manager){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	manager->stateHandler->sigcomp_parameters->SigComp_version = version;
	return 0;
}

/**@ingroup tcomp_manager_group
* Adds a new compressor to the dispatcher.
* @param handle The SigComp manager holding the dispatcher.
* @param compressor The compressor to add.
* @retval Zero if succeed and non-zero error code otherwise.
*/
int tcomp_manager_addCompressor(tcomp_manager_handle_t *handle, tcomp_compressor_compress_f compressor)
{
	tcomp_manager_t *manager = handle;
	if(!manager){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	return tcomp_compressordisp_addCompressor(manager->dispatcher_compressor, compressor);
}

/**@ingroup tcomp_manager_group
* Removes the compressor from the dispatcher.
* @param handle The SigComp manager holding the dispatcher.
* @param compressor The compressor to add.
* @retval Zero if succeed and non-zero error code otherwise.
*/
int tcomp_manager_removeCompressor(tcomp_manager_handle_t *handle, tcomp_compressor_compress_f compressor)
{
	tcomp_manager_t *manager = handle;
	if(!manager){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	return tcomp_compressordisp_removeCompressor(manager->dispatcher_compressor, compressor);
}

/**@ingroup tcomp_manager_group
* Adds SIP/SDP dictionary (RFC 3485) to the state handler.
* @param handle The SigComp manager holding the state handler.
* @retval Zero if succeed and non-zero otherwise.
*/
int tcomp_manager_addSipSdpDictionary(tcomp_manager_handle_t *handle)
{
	tcomp_manager_t *manager = handle;
	if(!manager){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	return tcomp_statehandler_addSipSdpDictionary(manager->stateHandler);
}

/**@ingroup tcomp_manager_group
* Adds Presence dictionary (RFC 5112) to the state handler.
* @param handle The SigComp manager holding the state handler.
* @retval Zero if succeed and non-zero otherwise.
*/
int tcomp_manager_addPresenceDictionary(tcomp_manager_handle_t *handle)
{
	tcomp_manager_t *manager = handle;
	if(!manager){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	return tcomp_statehandler_addPresenceDictionary(manager->stateHandler);
}











//========================================================
//	SigComp manager object definition
//

static void* tcomp_manager_ctor(void * self, va_list * app)
{
	tcomp_manager_t *manager = self;
	if(manager)
	{
		manager->stateHandler = tcomp_statehandler_create();
		manager->dispatcher_compressor = tcomp_compressordisp_create(manager->stateHandler);
		manager->dispatcher_decompressor = tcomp_decompressordisp_create(manager->stateHandler);

		/* Initialize safeobject */
		tsk_safeobj_init(manager);
	}
	else{
		TSK_DEBUG_ERROR("Failed to create new manager.");
	}

	return self;
}

static void* tcomp_manager_dtor(void *self)
{
	tcomp_manager_t *manager = self;
	if(manager){		
		TSK_OBJECT_SAFE_FREE(manager->stateHandler);
		TSK_OBJECT_SAFE_FREE(manager->dispatcher_compressor);
		TSK_OBJECT_SAFE_FREE(manager->dispatcher_decompressor);

		/* Deinitialize safeobject */
		tsk_safeobj_deinit(manager);
	}
	else{
		TSK_DEBUG_ERROR("Null manager.");
	}
	
	return self;
}

static const tsk_object_def_t tcomp_manager_def_s = 
{
	sizeof(tcomp_manager_t),
	tcomp_manager_ctor,
	tcomp_manager_dtor,
	tsk_null
};
const tsk_object_def_t *tcomp_manager_def_t = &tcomp_manager_def_s;
OpenPOWER on IntegriCloud