summaryrefslogtreecommitdiff
path: root/InfraStack/OSAgnostic/Product/AppSrvInfra/KDapiAdapter.c
blob: f9600fb2094d92be53cec676b642ac91fb441ce2 (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/**************************************************************************
Copyright (c) 2007-2009, Intel Corporation. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

 1. Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

 3. Neither the name of the Intel Corporation nor the names of its
    contributors may be used to endorse or promote products derived from
    this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

***************************************************************************/
#pragma warning(disable : 4201) // warning C4201: nonstandard extension used : nameless struct/union

#include "KDapiAdapter.h"
#include "SVstatus.h"

#include "TraceModule.h"
#include "L4ConfigurationManager.h"
#include "L5Common.h"
#include "WrappersUtils.h"

#include "Act.h"
#include "L4MsgProxy.h"
#include "wimax_osal_primitives.h"
#include "wimax_osal_string.h"


static IndicationCallback indCallback = NULL;
static TraceIndicationCallback trcCallback = NULL;
static UINT32 registeredInds[MAX_STATUS_IND_IDS];
static TARGET_ID targetId = MINIPORT_DRIVER;
static UINT32 gDump;
extern DRIVER_STATUS isDriverConnected;
static BOOL useDeviceTraces = FALSE;
static BOOL isStarted = FALSE;
static char *msgBuffer = NULL;

KDAPI_ADAPTER_RESULT KDAPI_ADAPTER_RestartDriverConnection()
{
	KDAPI_ADAPTER_RESULT status = KDAPI_ADAPTER_SUCCESS;

	TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_DEBUG, "Enter KDAPI_ADAPTER_RestartDriverConnection");

	KDAPI_RestartDriverConnection();

	TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_DEBUG, "Leave KDAPI_ADAPTER_RestartDriverConnection");		

	return status;
}

void KDAPI_ADAPTER_DsimStatusIndicationHandler( UINT32 statusID, char* buf, UINT32 bufSize )
{
	int i;

	TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_INFO, "Indication arrived, sent to 0x%x", indCallback);
	if( indCallback != NULL )
	{
		// check if this statusID is registered
		for( i = 0; i < MAX_STATUS_IND_IDS; i++ )
		{
			if( registeredInds[i] == statusID )
			{
				if (gDump)
				{
					DumpHexString("FromDriver:",(UCHAR*)buf, bufSize);
				}
				indCallback( statusID, buf, bufSize );
			}
		}
	}
}

void KDAPI_ADAPTER_DriverStatusIndicationHandler( PSTATUS_ENTRY statusEntry )
{
	TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_INFO, "Indication arrived, sent to 0x%x", indCallback);
	if( indCallback != NULL )
	{
		if (statusEntry->statusBufLen > STATUS_INDICATION_PAYLOAD_MAX_SIZE)
		{
			TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALERT, "indication length is bigger than STATUS_INDICATION_PAYLOAD_MAX_SIZE");
			l4Proxy_pFuncs->pfnSendErrorReport(L5_TARGET_KDAPI_ADAPTER, DEVICE_PROBLEM,  __FUNCTION__, __LINE__);			
		}
		else
		{			
			if (gDump)
			{
				DumpHexString("FromDriver:",statusEntry->statusBuf, statusEntry->statusBufLen);
			}

			indCallback( statusEntry->status, statusEntry->statusBuf, statusEntry->statusBufLen );
		}
	}
}


void KDAPI_ADAPTER_TracesIndicationHandler( int bufferLength , int *location)
{
	if( trcCallback != NULL )
	{
		trcCallback( bufferLength, location );		
	}
}


// Connect and Disconnect are called over and over by the driver monitor until the Connect succeeds

// Stop is called once by the driver monitor when ACT requests, before starting all finalizations

KDAPI_ADAPTER_RESULT ConvertToKDAPI_ADAPTER_RESULT( UINT32 res, UINT32 detailedRes )
{
	if((targetId == DRIVER_SIM) || (targetId == REMOTE_HOST))
	{
		UNREFERENCED_PARAMETER(detailedRes);

		if( res == KDAPI_FOR_SIM_SUCCESS )
		{
			return KDAPI_ADAPTER_SUCCESS;
		}

		if( res == KDAPI_FOR_SIM_BUFFER_TOO_SHORT )
		{
			TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALERT, "Bad buffer");
			return KDAPI_ADAPTER_BUFFER_TOO_SHORT;
		}

		if( res == KDAPI_FOR_SIM_BAD_ARGUMENTS )
		{
			TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALERT, "Bad arguments");
			return KDAPI_ADAPTER_BAD_ARGUMENTS;
		}

		if( res == KDAPI_FOR_SIM_NOT_ACTIVE )
		{
			TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALERT, "Not connected to the driver");
			return KDAPI_ADAPTER_NOT_ACTIVE;
		}

		if( ( res == KDAPI_FOR_SIM_DRIVER_GENERATED_ERR ) || ( res == KDAPI_FOR_SIM_SOCKET_DISCONNECTED ) )
		{
			TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALERT, "Driver complained");
			return KDAPI_ADAPTER_DRIVER_GENERATED_ERR;
		}

		// KDAPI_FOR_SIM_ERROR
		TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALERT, "Unknown error");
		return KDAPI_ADAPTER_ERROR;
	}
	else if (targetId == MINIPORT_DRIVER)
	{
		if( res == KDAPI_SUCCESS )
		{
			return KDAPI_ADAPTER_SUCCESS;
		}

		if( res == KDAPI_NO_ROOM_FOR_INDICATION )
		{
			TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALERT, "No room for indications");
			return KDAPI_ADAPTER_NO_ROOM_FOR_INDICATION;
		}

		if( res == KDAPI_INDICATION_NOT_REGISTERED )
		{
			TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALERT, "Cannot unregister to unregistered indication");
			return KDAPI_ADAPTER_INDICATION_NOT_REGISTERED;
		}

		if( res == KDAPI_BAD_ARGUMENTS )
		{
			TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALERT, "Bad arguments");
			return KDAPI_ADAPTER_BAD_ARGUMENTS;
		}

		if( res == KDAPI_NOT_ACTIVE )
		{
			TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALERT, "Not connected to the driver");
			return KDAPI_ADAPTER_NOT_ACTIVE;
		}

		if( res == KDAPI_DETAILED_ERR )
		{
			if( (STATUS_INVALID_PARAMETER == detailedRes) ||
				(STATUS_NOT_SUPPORTED == detailedRes) )
			{
				TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALERT, "Driver complained");
				return KDAPI_ADAPTER_DRIVER_GENERATED_ERR;
			}

			if( (STATUS_INVALID_BUFFER_SIZE == detailedRes) ||
				(STATUS_BUFFER_TOO_SMALL == detailedRes) )
			{
				TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALERT, "Bad buffer");
				return KDAPI_ADAPTER_BUFFER_TOO_SHORT;
			}
		}
	}

	// KDAPI_ERROR ||
	// KDAPI_DETAILED_ERR:
	//STATUS_UNSUCCESSFUL == detailedRes ||
	//STATUS_DEVICE_NOT_READY == detailedRes ||
	//STATUS_NO_MORE_ENTRIES == detailedRes ||
	//STATUS_PENDING == res ||
	//STATUS_BUFFER_OVERFLOW == detailedRes ||
	//NDIS_STATUS_FAILURE == detailedRes || 
	//NDIS_STATUS_RESOURCES == detailedRes
	TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALERT, "unknown error");
	return KDAPI_ADAPTER_ERROR;
	
}




KDAPI_ADAPTER_RESULT KDAPI_ADAPTER_SendReceiveMessage( 
	OID_NUM oidNum,
	void *send_buffer, int send_buffer_size,
	void *receive_buffer, int *receive_buffer_size )
{
	UINT32 res;
	UINT32 detailedRes = 0;

	if (!isStarted)
	{
		return KDAPI_ADAPTER_NOT_ACTIVE;
	}

	TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_INFO, "Send message to driver");
	//TODO: add code for message dump
	if (gDump)
	{
		DumpHexString("MsgToDriver:",send_buffer, send_buffer_size);
	}

	if(targetId == DRIVER_SIM)
	{
		res = KDAPI_FOR_SIM_SendQueryOID( 
			oidNum, send_buffer, receive_buffer, send_buffer_size, *receive_buffer_size, (UINT32*)receive_buffer_size );
	}
	else if (targetId == MINIPORT_DRIVER)
	{
		res = KDAPI_SendQueryOID( 
			oidNum, send_buffer, receive_buffer, send_buffer_size, *receive_buffer_size, (UINT32*)receive_buffer_size, &detailedRes );
	}
	else // remote host
	{
		res = KDAPI_ADAPTER_SUCCESS; // do nothing
	}

	if (gDump)
	{
		DumpHexString("MsgRespondFromDriver:",receive_buffer, *receive_buffer_size);
	}
	return ConvertToKDAPI_ADAPTER_RESULT( res, detailedRes );
}

KDAPI_ADAPTER_RESULT InitDriver(BOOL traceEnable)
{
	KDAPI_RESULT res;
	UINT32 detailedRes = 0;

	res = KDAPI_Init(	KDAPI_ADAPTER_DriverStatusIndicationHandler,
						KDAPI_ADAPTER_TracesIndicationHandler,
						&msgBuffer,
						&detailedRes,
						traceEnable);	
	
	TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALL, "Returning with %d", res);

	return (KDAPI_SUCCESS != res)?KDAPI_ADAPTER_ERROR:KDAPI_ADAPTER_SUCCESS;
}

KDAPI_ADAPTER_RESULT InitDsim()
{
	KDAPI_FOR_SIM_RESULT res;
	char dSimIpAddress[32] = {0};
	UINT32 dSimOidsPortNum = OidsPort;
	UINT32 dSimIndicationsPortNum = IndsPort;
	
	memset( registeredInds, -1, MAX_STATUS_IND_IDS*sizeof(UINT32) );

	L4Configurations_getDSimIpAddress(dSimIpAddress);
	L4Configurations_getDSimOidsPortNum(&dSimOidsPortNum);
	L4Configurations_getDSimIndicationsPortNum(&dSimIndicationsPortNum);

	//errno = wcstombs_s(&numOfCharConverted, dSimIpAddress_char, sizeof(dSimIpAddress_char), dSimIpAddress, sizeof(dSimIpAddress));

	res = KDAPI_FOR_SIM_Init( targetId, dSimIpAddress, (unsigned short)dSimOidsPortNum, (unsigned short)dSimIndicationsPortNum, KDAPI_ADAPTER_DsimStatusIndicationHandler );

	if (res != KDAPI_FOR_SIM_SUCCESS)
	{
		return KDAPI_ADAPTER_ERROR;
	}

	return KDAPI_ADAPTER_SUCCESS;
}

//KDapiAdapter interface
KDAPI_ADAPTER_RESULT KDAPI_ADAPTER_Init(BOOL traceEnable)
{
	UINT32 res;
	errno_t errno;

	char answer[MAX_ANSWER_SIZE];

	//set the useDeviceTraces flag
	useDeviceTraces = traceEnable;

	//determine the target (driver, dsim or remote):
	targetId = MINIPORT_DRIVER;
	res = L4Configurations_getDriverMode(answer);	
	if (0 == OSAL_stricmp("dsim", answer)) 
	{
		TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALL, "Target: DSim");
		targetId = DRIVER_SIM;
	}
	else if (0 == OSAL_stricmp("remote", answer))
	{
		TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALL, "Target: remote");
		targetId = REMOTE_HOST;
	}
	else // if nothing is written - we take it as driver
	{
		TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALL, "Target: Driver");
		targetId = MINIPORT_DRIVER;
	}

	//determine whether to use dump in logs or not:
	res = L4Configurations_getDumpEnable(&gDump);
	if (gDump != 0)
	{
		gDump = 1;
	}

	//initialize the target (driver or dsim):
	if((targetId == DRIVER_SIM) || (targetId == REMOTE_HOST))
	{
		res = InitDsim();
	}
	else if (targetId == MINIPORT_DRIVER)
	{
		TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_ALL, "Invoking InitDriver");
		res = InitDriver(useDeviceTraces);
	}

	if (res != KDAPI_SUCCESS)
	{
		return KDAPI_ADAPTER_ERROR;
	}
	
	return KDAPI_ADAPTER_SUCCESS;

}

KDAPI_ADAPTER_RESULT KDAPI_ADAPTER_Fini()
{

	if (targetId == MINIPORT_DRIVER)
	{
		KDAPI_Fini();
	}
	else 
	{
		KDAPI_FOR_SIM_Fini(targetId);
	}

	return KDAPI_ADAPTER_SUCCESS;
}

KDAPI_ADAPTER_RESULT KDAPI_ADAPTER_Connect()
{
	UINT32 res = 0;

	TRACE(TR_MOD_KDAPI, TR_SEV_DEBUG, "Entering");

	if (targetId == MINIPORT_DRIVER)
	{
		res = KDAPI_Connect();
		TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_DEBUG, " KDAPI Connect returned : %d", res);
	}
	else
	{
		res = KDAPI_FOR_SIM_Connect(targetId);
	}

	return ConvertToKDAPI_ADAPTER_RESULT(res, 0);
}

KDAPI_ADAPTER_RESULT KDAPI_ADAPTER_Disconnect()
{
	if (targetId == MINIPORT_DRIVER)
	{
		KDAPI_Disconnect();
	}
	else 
	{
		KDAPI_FOR_SIM_Disconnect(targetId);
	}

	return KDAPI_ADAPTER_SUCCESS;
}

KDAPI_ADAPTER_RESULT KDAPI_ADAPTER_Start()
{
	KDAPI_RESULT res;

	if (targetId == MINIPORT_DRIVER)
	{
		res = KDAPI_Start();
		if (KDAPI_SUCCESS!=res)
		{
			return KDAPI_ADAPTER_ERROR;
		}
	}
	else
	{
		if (KDAPI_SUCCESS != KDAPI_FOR_SIM_Start())
		{
			return KDAPI_ADAPTER_ERROR;
		}
	}

	// Notifying ACT that the driver is UP
	Act_DriverDeviceStatus(DRIVER_UP);

	TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_DEBUG, "KDAPI_ADAPTER_Start - OSAL_reset_event(kdapiAdapterStopEvent)");

	isStarted = TRUE;
	return KDAPI_ADAPTER_SUCCESS;
}

void KDAPI_ADAPTER_Stop()
{
	// Notifying ACT that the driver is DOWN
	Act_DriverDeviceStatus(DRIVER_DOWN);

	isStarted = FALSE;

	if (targetId == MINIPORT_DRIVER)
	{
		KDAPI_Stop();
	}
	else
	{
		KDAPI_FOR_SIM_Stop(targetId);
	}

	TRACE(TR_MOD_KDAPI_ADAPTER, TR_SEV_DEBUG, "KDAPI_ADAPTER_Stop - OSAL_set_event(kdapiAdapterStopEvent)");
}

KDAPI_ADAPTER_RESULT KDAPI_ADAPTER_RegisterOnIndication( STATUS_IND_ID indication_id )
{
	UINT32 res = KDAPI_ADAPTER_SUCCESS;
	UINT32 detailedRes = 0;

	if( indCallback == NULL )
	{
		return KDAPI_ADAPTER_ERROR;
	}

	if((targetId == DRIVER_SIM) || (targetId == REMOTE_HOST))
	{
		int i;
		// if already registered for that indication_id do nothing
		for( i = 0; i < MAX_STATUS_IND_IDS; i++ )
		{
			if( registeredInds[i] == indication_id )
			{
				return KDAPI_ADAPTER_SUCCESS;
			}
		}

		// search for an empty spot for the indication_id
		for( i = 0; i < MAX_STATUS_IND_IDS; i++ )
		{
			if( registeredInds[i] == -1 )
			{
				registeredInds[i] = indication_id;
				break;
			}
		}

		// no room for a new indication_id
		if( i == MAX_STATUS_IND_IDS )
		{
			return KDAPI_ADAPTER_NO_ROOM_FOR_INDICATION;
		}
	}
	else if (targetId == MINIPORT_DRIVER)
	{
		res = KDAPI_DriverStatusRegister( indication_id, &detailedRes );
	}

	return ConvertToKDAPI_ADAPTER_RESULT( res, detailedRes );
}

KDAPI_ADAPTER_RESULT KDAPI_ADAPTER_UnregisterOnIndication( STATUS_IND_ID indication_id )
{
	UINT32 res = KDAPI_ADAPTER_SUCCESS;
	UINT32 detailedRes = 0;

	if((targetId == DRIVER_SIM) || (targetId == REMOTE_HOST))
	{
		int i;

		// if the indication_id was registered, release its spot
		for( i = 0; i < MAX_STATUS_IND_IDS; i++ )
		{
			if( registeredInds[i] == indication_id )
			{
				registeredInds[i] = 0xffffffff;
				break;
			}
		}

		// the given indication_id wasn't registered
		if( i == MAX_STATUS_IND_IDS )
		{
			return KDAPI_ADAPTER_INDICATION_NOT_REGISTERED;
		}
	}
	else if (targetId == MINIPORT_DRIVER)
	{
		res = KDAPI_DriverStatusUnRegister( indication_id, &detailedRes );
	}

	return ConvertToKDAPI_ADAPTER_RESULT( res, detailedRes );
}


void KDAPI_ADAPTER_SetDriverIndicatiosCB(IndicationCallback callback)
{
	indCallback = callback;
}

EXTERN_C EXPORT void KDAPI_ADAPTER_SetTraceIndicatiosCB(TraceIndicationCallback callback, char **trcBuffer)
{
	trcCallback = callback;
	*trcBuffer = msgBuffer;
}

void KDAPI_ADAPTER_ResetDevice()
{
	if (targetId == MINIPORT_DRIVER)
	{
		KDAPI_ResetDevice();
	}
}