1/*
2 * Test harness for dispatcher.
3 *
4 * Copyright (C) 2015, Broadcom Corporation
5 * All Rights Reserved.
6 *
7 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
8 * the contents of this file may not be disclosed to third parties, copied
9 * or duplicated in any form, in whole or in part, without the prior
10 * written permission of Broadcom Corporation.
11 *
12 * $Id:$
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19#include "trace.h"
20#include "test.h"
21#include "dsp.h"
22#include "tmr.h"
23
24/* request handler */
25typedef void (*requestHandlerT)(void *context,
26	int reqLength, uint8 *reqData, uint8 *rspData);
27
28#define BUF_SIZE	32
29
30typedef struct {
31	requestHandlerT handler;
32	uint8 data[BUF_SIZE];
33} reqT;
34
35TEST_DECLARE();
36
37static void *testContext = (void *)0xabcd1234;
38static tmrT *timer;
39
40/* --------------------------------------------------------------- */
41
42static void processReqEvent(void *context,
43	int reqLength, uint8 *reqData, uint8 *rspData)
44{
45	reqT *req = (reqT *)reqData;
46
47	TEST(context == testContext, "context incorrect");
48	TEST(reqLength == sizeof(reqT), "request length incorrect");
49
50	if (rspData != 0) {
51		memcpy(rspData, req->data, sizeof(req->data));
52	}
53}
54
55static void processWlanEvent(void *context, uint32 eventType,
56	wl_event_msg_t *wlEvent, uint8 *data, uint32 length)
57{
58#if !TRACE_ENABLED
59	(void)eventType;
60#endif
61	(void)context;
62	(void)wlEvent;
63	(void)data;
64	(void)length;
65
66	TRACE(TRACE_DEBUG, "event_type=%d\n", eventType);
67}
68
69static void
70timeout(void *arg)
71{
72	(void)arg;
73	TRACE(TRACE_DEBUG, "timeout callback\n");
74}
75
76static void testdsp(void)
77{
78	int i;
79	reqT req;
80	uint8 rsp[BUF_SIZE];
81
82	req.handler = processReqEvent;
83	for (i = 0; i < BUF_SIZE; i++)
84		req.data[i] = i;
85
86	TEST(dspSubscribe(dsp(), 0, processWlanEvent),
87		"dspSubscribe failed");
88
89	timer = tmrCreate(dsp(), timeout, 0, "test");
90	tmrStart(timer, 5 * 1000, FALSE);
91
92	for (i = 0; i < 10; i++)
93		TEST(dspRequest(dsp(), testContext, sizeof(req), (uint8 *)&req),
94			"dspData failed");
95
96	for (i = 0; i < 10; i++) {
97		memset(req.data, i, sizeof(req.data));
98		memset(rsp, 0, sizeof(rsp));
99		TRACE_HEX_DUMP(TRACE_DEBUG, "req bufffer", sizeof(req.data), req.data);
100		TEST(dspRequestSynch(dsp(), testContext,
101			sizeof(req), (uint8 *)&req, rsp), "dspDataSync failed");
102		TRACE_HEX_DUMP(TRACE_DEBUG, "rsp bufffer", sizeof(rsp), rsp);
103		TEST(memcmp(req.data, rsp, sizeof(rsp)) == 0, "response data incorrect");
104	}
105
106	sleep(1);
107
108	TEST(dspUnsubscribe(dsp(), processWlanEvent),
109		"dspUnsubscribe failed");
110	dspFree();
111}
112
113int main(int argc, char **argv)
114{
115	(void) argc;
116	(void) argv;
117
118	TRACE_LEVEL_SET(TRACE_ALL);
119	TEST_INITIALIZE();
120
121	testdsp();
122
123	TEST_FINALIZE();
124	return 0;
125}
126