1/*
2 * smeDebug.c
3 *
4 * Copyright(c) 1998 - 2009 Texas Instruments. All rights reserved.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 *  * Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 *  * Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in
15 *    the documentation and/or other materials provided with the
16 *    distribution.
17 *  * Neither the name Texas Instruments nor the names of its
18 *    contributors may be used to endorse or promote products derived
19 *    from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file smeDbg.c
35 *  \brief This file include the SME debug module implementation
36 *  \
37 *  \date 13-February-2006
38 */
39
40#include "smePrivate.h"
41#include "smeDebug.h"
42#include "report.h"
43
44void printSmeDbgFunctions (void);
45void sme_dbgPrintObject (TI_HANDLE hSme);
46void sme_printStats (TI_HANDLE hSme);
47void sme_resetStats(TI_HANDLE hSme);
48void sme_printBssidList(TI_HANDLE hSme);
49
50#define CHAN_FREQ_TABLE_SIZE        (sizeof(ChanFreq) / sizeof(struct CHAN_FREQ))
51
52struct  CHAN_FREQ {
53    unsigned char  chan;
54    unsigned long  freq;
55} ChanFreq[] = {
56    {1,2412000}, {2,2417000}, {3,2422000}, {4,2427000},
57    {5,2432000}, {6,2437000}, {7,2442000}, {8,2447000},
58    {9,2452000},
59    {10,2457000}, {11,2462000}, {12,2467000}, {13,2472000},
60    {14,2484000}, {36,5180000}, {40,5200000}, {44,5220000},
61    {48,5240000}, {52,5260000}, {56,5280000}, {60,5300000},
62    {64,5320000},
63    {100,5500000}, {104,5520000}, {108,5540000}, {112,5560000},
64    {116,5580000}, {120,5600000}, {124,5620000}, {128,5640000},
65    {132,5660000}, {136,5680000}, {140,5700000}, {149,5745000},
66    {153,5765000}, {157,5785000}, {161,5805000} };
67
68TI_UINT32 scanResultTable_CalculateBssidListSize (TI_HANDLE hScanResultTable, TI_BOOL bAllVarIes);
69TI_STATUS scanResultTable_GetBssidList (TI_HANDLE hScanResultTable,
70                                        OS_802_11_BSSID_LIST_EX *pBssidList,
71                                        TI_UINT32 *pLength,
72                                        TI_BOOL bAllVarIes);
73
74/**
75 * \fn     smeDebugFunction
76 * \brief  Main SME debug function
77 *
78 * Main SME debug function
79 *
80 * \param  hSme - handle to the SME object
81 * \param  funcType - the specific debug function
82 * \param  pParam - parameters for the debug function
83 * \return None
84 */
85void smeDebugFunction (TI_HANDLE hSme, TI_UINT32 funcType, void *pParam)
86{
87    switch (funcType)
88    {
89    case DBG_SME_PRINT_HELP:
90        printSmeDbgFunctions();
91        break;
92
93    case DBG_SME_PRINT_OBJECT:
94        sme_dbgPrintObject( hSme );
95        break;
96
97    case DBG_SME_PRINT_STATS:
98        sme_printStats( hSme );
99        break;
100
101    case DBG_SME_CLEAR_STATS:
102        sme_resetStats( hSme );
103        break;
104
105   case DBG_SME_BSSID_LIST:
106	sme_printBssidList( hSme );
107	break;
108
109	default:
110   		WLAN_OS_REPORT(("Invalid function type in SME debug function: %d\n", funcType));
111        break;
112    }
113}
114
115int sme_strlen(char *s)
116{
117    int x=0;
118    while (*s++)
119        x++;
120    return(x);
121}
122
123
124char* sme_strcpy(char *s1,char *s2)
125{
126    while (*s2)
127    {
128        *s1++ = *s2++;
129    }
130    *s1 = '\0';
131
132	return s1;
133}
134
135
136int sme_memcmp(char* s1, char* s2, int n)
137{
138	while(n-- > 0 && *s1 == *s2)
139		s1++, s2++;
140
141	return( n < 0 ? 0 : *s1 - *s2 );
142}
143
144
145
146
147/**
148 * \fn     printSmeDbgFunctions
149 * \brief  Print the SME debug menu
150 *
151 * Print the SME debug menu
152 *
153 * \param  hSme - handle to the SME object
154 * \return None
155 */
156void printSmeDbgFunctions(void)
157{
158    WLAN_OS_REPORT(("   SME Debug Functions   \n"));
159	WLAN_OS_REPORT(("-------------------------\n"));
160	WLAN_OS_REPORT(("1900 - Print the SME Debug Help\n"));
161	WLAN_OS_REPORT(("1901 - Print the SME object\n"));
162	WLAN_OS_REPORT(("1902 - Print the SME statistics\n"));
163    WLAN_OS_REPORT(("1903 - Reset the SME statistics\n"));
164	WLAN_OS_REPORT(("1904 - Print BSSID list\n"));
165}
166
167#ifdef REPORT_LOG
168static TI_UINT8 Freq2Chan(TI_UINT32 freq)
169{
170    TI_UINT32 i;
171
172    for(i=0; i<CHAN_FREQ_TABLE_SIZE; i++)
173        if(ChanFreq[i].freq == freq)
174            return ChanFreq[i].chan;
175
176    return 0;
177}
178#endif
179
180static void PrintBssidList(OS_802_11_BSSID_LIST_EX* bssidList, TI_UINT32 IsFullPrint, TMacAddr CurrentBssid)
181{
182    TI_UINT32 i;
183    TI_INT8  connectionTypeStr[50];
184    POS_802_11_BSSID_EX pBssid = &bssidList->Bssid[0];
185
186    WLAN_OS_REPORT(("BssId List: Num=%u\n", bssidList->NumberOfItems));
187    WLAN_OS_REPORT(("         MAC        Privacy Rssi  Mode    Channel    SSID\n"));
188    for(i=0; i<bssidList->NumberOfItems; i++)
189    {
190        switch (pBssid->InfrastructureMode)
191        {
192            case os802_11IBSS:
193                sme_strcpy (connectionTypeStr, "Adhoc");
194                break;
195            case os802_11Infrastructure:
196                sme_strcpy (connectionTypeStr, "Infra");
197                break;
198            case os802_11AutoUnknown:
199                sme_strcpy (connectionTypeStr, "Auto");
200                break;
201            default:
202                sme_strcpy (connectionTypeStr, " --- ");
203                break;
204        }
205        WLAN_OS_REPORT(("%s%02x.%02x.%02x.%02x.%02x.%02x   %3u   %4d   %s %6d       %s\n",
206            (!sme_memcmp(CurrentBssid, pBssid->MacAddress, MAC_ADDR_LEN))?"*":" ",
207            pBssid->MacAddress[0],
208            pBssid->MacAddress[1],
209            pBssid->MacAddress[2],
210            pBssid->MacAddress[3],
211            pBssid->MacAddress[4],
212            pBssid->MacAddress[5],
213            pBssid->Privacy,
214            pBssid->Rssi,
215            connectionTypeStr,
216            Freq2Chan(pBssid->Configuration.Union.channel),
217            (pBssid->Ssid.Ssid[0] == '\0')?(TI_INT8*)"****":((TI_INT8*)pBssid->Ssid.Ssid) ));
218
219        if (IsFullPrint)
220        {
221            WLAN_OS_REPORT(("   BeaconInterval %d\n",  pBssid->Configuration.BeaconPeriod));
222            WLAN_OS_REPORT(("   Capabilities   0x%x\n",  pBssid->Capabilities));
223        }
224#ifdef _WINDOWS /*temp fix until bringing the dual OS fix*/
225		pBssid = (POS_802_11_BSSID_EX)((TI_INT8*)pBssid + (pBssid->Length ? pBssid->Length : sizeof(OS_802_11_BSSID_EX)));
226#else /*for Linux*/
227		pBssid = &bssidList->Bssid[i+1];
228#endif
229    }
230}
231
232void sme_printBssidList(TI_HANDLE hSme)
233{
234	TSme* sme = (TSme*) hSme;
235	TI_UINT32 length;
236	TI_UINT8* blist;
237	TMacAddr temp_bssid = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
238
239	length = scanResultTable_CalculateBssidListSize (sme->hScanResultTable, TI_FALSE);
240
241	blist = os_memoryAlloc(NULL, length);
242
243	if(!blist)
244    {
245		WLAN_OS_REPORT(("ERROR. sme_printBssidList(): Cannot allocate memory!! length = %d\n", length));
246		return;
247	}
248
249	scanResultTable_GetBssidList (sme->hScanResultTable, (POS_802_11_BSSID_LIST_EX)blist, &length, TI_FALSE);
250
251	PrintBssidList((OS_802_11_BSSID_LIST_EX*)blist, 0, temp_bssid);
252
253	os_memoryFree(NULL, blist, length);
254}
255
256/**
257 * \fn     sme_dbgPrintObject
258 * \brief  Print the SME object
259 *
260 * Print the SME object
261 *
262 * \param  hSme - handle to the SME object
263 * \return None
264 */
265void sme_dbgPrintObject (TI_HANDLE hSme)
266{
267    WLAN_OS_REPORT(("Not yet implemented!\n"));
268}
269
270/**
271 * \fn     sme_printStats
272 * \brief  Print the SME statistics
273 *
274 * Print the SME statistics
275 *
276 * \param  hSme - handle to the SME object
277 * \return None
278 */
279void sme_printStats (TI_HANDLE hSme)
280{
281    WLAN_OS_REPORT(("Not yet implemented!\n"));
282}
283
284/**
285 * \fn     sme_resetStats
286 * \brief  Reset the SME statistics
287 *
288 * Reset the SME statistics
289 *
290 * \param  hSme - handle to the SME object
291 * \return None
292 */
293void sme_resetStats(TI_HANDLE hSme)
294{
295    WLAN_OS_REPORT(("Not yet implemented!\n"));
296}
297
298