PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/wireless/tiwlan1251/common/src/core/data_ctrl/Ctrl/TrafficMonitor/GeneralUtil.c

http://github.com/CyanogenMod/cm-kernel
C | 511 lines | 229 code | 81 blank | 201 comment | 62 complexity | 96f60077cf85393cba52e4c9bcaf85e5 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /****************************************************************************
  2. **+-----------------------------------------------------------------------+**
  3. **| |**
  4. **| Copyright(c) 1998 - 2008 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. ****************************************************************************/
  35. #include "GeneralUtilApi.h"
  36. #include "GeneralUtil.h"
  37. #include "report.h"
  38. #include "osApi.h"
  39. /*************************************************************************
  40. * LIST OBJ *
  41. **************************************************************************
  42. **************************************************************************
  43. *
  44. * The list object mange the allocation and deallocation of generic element.
  45. * The obj create a list of N generic elements and fined a free entry for the Alloc process.
  46. * And free the entry for dealloc.
  47. *
  48. *
  49. ***************************************************************************/
  50. /*************************************************************************
  51. * List_create *
  52. **************************************************************************
  53. * DESCRIPTION: This function initializes the List data module.
  54. *
  55. * INPUT: hOs - handle to Os Abstraction Layer
  56. * MaxNumOfElements - the number of elemnts that will be Managed by the list
  57. ContainerSize - The size of the basic data type managed by the list
  58. * OUTPUT:
  59. *
  60. *
  61. * RETURN: Handle to the allocated List data control block
  62. ************************************************************************/
  63. TI_HANDLE List_create(TI_HANDLE hOs,int MaxNumOfElements,int ContainerSize)
  64. {
  65. int index;
  66. List_t *List;
  67. if( hOs == NULL )
  68. {
  69. WLAN_OS_REPORT(("FATAL ERROR:List_create(): OS handle Error - Aborting\n"));
  70. return NULL;
  71. }
  72. /* alocate List block */
  73. List = (List_t*)os_memoryAlloc(hOs, sizeof(List_t));
  74. if(List == NULL)
  75. return NULL;
  76. /* alocate the List of Elements */
  77. List->ElementList =(ListElement_t*)os_memoryAlloc(hOs, (sizeof(ListElement_t)*MaxNumOfElements));
  78. if(List->ElementList == NULL)
  79. {
  80. os_memoryFree(List->hOs, List, sizeof(List_t));
  81. return NULL;
  82. }
  83. /*Allocate the Data containers*/
  84. for(index=0;index<MaxNumOfElements;index++)
  85. {
  86. List->ElementList[index].Container = os_memoryAlloc(hOs,ContainerSize);
  87. if(List->ElementList[index].Container == NULL)
  88. break;
  89. List->ElementList[index].Inuse = FALSE;
  90. }
  91. if (index != MaxNumOfElements) /*Not all the list element was allocated and*/
  92. { /*therefore we free the entire list and rap it up*/
  93. index--;
  94. for(;index>=0;index--)
  95. os_memoryFree(hOs,List->ElementList[index].Container,ContainerSize);
  96. os_memoryFree(List->hOs, List->ElementList, (sizeof(ListElement_t)*MaxNumOfElements));
  97. os_memoryFree(List->hOs,List,(sizeof(List_t)));
  98. return NULL;
  99. }
  100. List->MaxNumOfElements = MaxNumOfElements;
  101. List->ContainerSize = ContainerSize;
  102. return((TI_HANDLE)List);
  103. }
  104. /***************************************************************************
  105. * List_Destroy *
  106. ****************************************************************************
  107. * DESCRIPTION: This function unload the List data module.
  108. *
  109. * INPUTS: hCtrlData - the object
  110. *
  111. * OUTPUT:
  112. *
  113. * RETURNS: OK - Unload succesfull
  114. * NOK - Unload unsuccesfull
  115. ***************************************************************************/
  116. TI_STATUS List_Destroy(TI_HANDLE hList)
  117. {
  118. List_t* List = (List_t*)hList;
  119. int index;
  120. if(List!=NULL)
  121. {
  122. if(List->ElementList != NULL)
  123. {
  124. for(index=0;index<List->MaxNumOfElements;index++)
  125. os_memoryFree(List->hOs,List->ElementList[index].Container,List->ContainerSize);
  126. os_memoryFree(List->hOs,List->ElementList,(sizeof(ListElement_t)*List->MaxNumOfElements));
  127. }
  128. os_memoryFree(List->hOs, List, sizeof(List_t));
  129. }
  130. return OK;
  131. }
  132. /***************************************************************************
  133. * List_AllocElement *
  134. ****************************************************************************
  135. *
  136. *
  137. * Fined an empty entry in the list and returns
  138. * a pointer to a memory that contains an element that not in use.
  139. *
  140. * Note in multi Task environment we need to add semaphore to protect the
  141. * Function.
  142. *
  143. ***************************************************************************/
  144. TI_HANDLE List_AllocElement(TI_HANDLE hList)
  145. {
  146. List_t* List = (List_t*)hList;
  147. int index;
  148. if (List == NULL)
  149. return NULL;
  150. for(index=0;index<List->MaxNumOfElements;index++)
  151. {
  152. if(!(List->ElementList[index].Inuse))
  153. {
  154. List->ElementList[index].Inuse = TRUE;
  155. os_memoryZero(List->hOs,List->ElementList[index].Container,List->ContainerSize);
  156. return((TI_HANDLE)List->ElementList[index].Container);
  157. }
  158. }
  159. return NULL;
  160. }
  161. /***************************************************************************
  162. * List_FreeElement *
  163. ****************************************************************************
  164. *
  165. * Marks the entry that was allocated as free.
  166. * An alloc process can use this space.
  167. *
  168. *
  169. *
  170. ***************************************************************************/
  171. TI_STATUS List_FreeElement(TI_HANDLE hList,TI_HANDLE Container)
  172. {
  173. List_t* List = (List_t*)hList;
  174. int index;
  175. if (List == NULL)
  176. return NOK;
  177. for(index=0;index<List->MaxNumOfElements;index++)
  178. {
  179. if(List->ElementList[index].Container == Container)
  180. {
  181. if(!List->ElementList[index].Inuse)
  182. return NOK; /*double free not legal*/
  183. List->ElementList[index].Inuse = FALSE;
  184. return OK;
  185. }
  186. }
  187. return NOK;
  188. }
  189. /***************************************************************************
  190. * List_GetFirst *
  191. ****************************************************************************
  192. *
  193. * For purposes of searching the element list (going over all the element in the list)
  194. * Get first is used to reset an index for the search.
  195. * This function is work combined with GetNext.
  196. *
  197. * Note this function can't be used in multi Task environment.
  198. *
  199. ***************************************************************************/
  200. TI_HANDLE List_GetFirst(TI_HANDLE hList)
  201. {
  202. List_t* List = (List_t*)hList;
  203. int index;
  204. if (List == NULL)
  205. return NULL;
  206. for(index=0;index<List->MaxNumOfElements;index++)
  207. {
  208. if(List->ElementList[index].Inuse)
  209. {
  210. List->CurrentIndex = index;
  211. return (List->ElementList[index].Container);
  212. }
  213. }
  214. return NULL;
  215. }
  216. /***************************************************************************
  217. * List_GetNext *
  218. ****************************************************************************
  219. *
  220. * This function returns the next element in the list till null
  221. * that indicate that there no more element or we have reached the end of the list.
  222. * This function is work combined with GetFirst.
  223. *
  224. * Note this function can't be used in multi Task environment.
  225. *
  226. ***************************************************************************/
  227. TI_HANDLE List_GetNext(TI_HANDLE hList)
  228. {
  229. List_t* List = (List_t*)hList;
  230. int index;
  231. if (List == NULL)
  232. return NULL;
  233. /* the code works fine even if the elment is the last*/
  234. for(index=List->CurrentIndex+1;index<List->MaxNumOfElements;index++)
  235. {
  236. if(List->ElementList[index].Inuse)
  237. {
  238. List->CurrentIndex = index;
  239. return (List->ElementList[index].Container);
  240. }
  241. }
  242. return NULL;
  243. }
  244. /***************************************************************************
  245. * DISTRIBUTOR MANAGER *
  246. ****************************************************************************
  247. ***************************************************************************
  248. *
  249. * PURPOSE:The distributor manger supplies
  250. * 1. Register mechanism that has a callback function and the condition
  251. * (bit mask format) that will be used to distinguish if to call this callback.
  252. * 2. Event occurrence function that go over all the registered function and compare
  253. * the input mask to the callback mask condition.
  254. *
  255. *
  256. *
  257. ***************************************************************************/
  258. /***************************************************************************
  259. * DistributorMgr_Create *
  260. ****************************************************************************
  261. *
  262. ***************************************************************************/
  263. TI_HANDLE DistributorMgr_Create(TI_HANDLE hOs , int MaxNotifReqElment)
  264. {
  265. DistributorMgr_t *DistributorMgr;
  266. DistributorMgr = (DistributorMgr_t*)os_memoryAlloc(hOs, sizeof(DistributorMgr_t));
  267. if(DistributorMgr == NULL)
  268. return NULL;
  269. DistributorMgr->hOs = hOs;
  270. DistributorMgr->DistributionList = (List_t*)List_create(hOs,MaxNotifReqElment,sizeof(NotifReqElment_t));
  271. if (DistributorMgr->DistributionList == NULL)
  272. {
  273. os_memoryFree(hOs, DistributorMgr, sizeof(DistributorMgr_t));
  274. return NULL;
  275. }
  276. return (TI_HANDLE)DistributorMgr;
  277. }
  278. /************************************************************************/
  279. /* DistributorMgr_Destroy */
  280. /************************************************************************/
  281. TI_STATUS DistributorMgr_Destroy(TI_HANDLE hDistributorMgr)
  282. {
  283. DistributorMgr_t *DistributorMgr =(DistributorMgr_t*)hDistributorMgr;
  284. if(DistributorMgr == NULL)
  285. return NOK;
  286. List_Destroy(DistributorMgr->DistributionList);
  287. os_memoryFree(DistributorMgr->hOs, hDistributorMgr, sizeof(DistributorMgr_t));
  288. return OK;
  289. }
  290. /***************************************************************************
  291. * DistributorMgr_Reg *
  292. ****************************************************************************
  293. *
  294. * Use by the client to register a callback function
  295. * with the mask condition that will trigger the call.
  296. *
  297. * input
  298. * UINT16 Mask
  299. * TI_HANDLE CallBack
  300. * HANDLE Context
  301. * UINT32 Cookie
  302. *
  303. *
  304. ***************************************************************************/
  305. TI_HANDLE DistributorMgr_Reg(TI_HANDLE hDistributorMgr,UINT16 Mask,TI_HANDLE CallBack,
  306. TI_HANDLE Context,UINT32 Cookie)
  307. {
  308. DistributorMgr_t *DistributorMgr = (DistributorMgr_t*)hDistributorMgr;
  309. NotifReqElment_t *NotifReqElment;
  310. if(DistributorMgr == NULL)
  311. return NULL;
  312. NotifReqElment = (NotifReqElment_t*)List_AllocElement(DistributorMgr->DistributionList);
  313. if (NotifReqElment == NULL)
  314. return NULL ;
  315. NotifReqElment->CallBack = (GeneralEventCall_t)CallBack;
  316. NotifReqElment->Mask = Mask;
  317. NotifReqElment->Context = Context;
  318. NotifReqElment->Cookie = Cookie;
  319. NotifReqElment->HaltReq = FALSE;
  320. return (TI_HANDLE)NotifReqElment;
  321. }
  322. /***************************************************************************
  323. * DistributorMgr_ReReg *
  324. ****************************************************************************
  325. *
  326. ***************************************************************************/
  327. TI_STATUS DistributorMgr_ReReg(TI_HANDLE hDistributorMgr,TI_HANDLE ReqElmenth ,UINT16 Mask,TI_HANDLE CallBack,TI_HANDLE Context,UINT32 Cookie)
  328. {
  329. DistributorMgr_t *DistributorMgr = (DistributorMgr_t*)hDistributorMgr;
  330. NotifReqElment_t *NotifReqElment = (NotifReqElment_t*)ReqElmenth;
  331. if(DistributorMgr == NULL)
  332. return NOK;
  333. if (NotifReqElment == NULL)
  334. return NOK;
  335. NotifReqElment->CallBack = (GeneralEventCall_t)CallBack;
  336. NotifReqElment->Mask = Mask;
  337. NotifReqElment->Context = Context;
  338. NotifReqElment->Cookie = Cookie;
  339. return OK;
  340. }
  341. /***************************************************************************
  342. * DistributorMgr_AddToMask *
  343. ****************************************************************************
  344. *
  345. * Use this function to add mask bit to the bit mask condition that triggers the Callback
  346. *
  347. *
  348. ***************************************************************************/
  349. TI_STATUS DistributorMgr_AddToMask(TI_HANDLE hDistributorMgr,TI_HANDLE ReqElmenth,UINT16 Mask)
  350. {
  351. DistributorMgr_t *DistributorMgr = (DistributorMgr_t*)hDistributorMgr;
  352. NotifReqElment_t *NotifReqElment = (NotifReqElment_t*)ReqElmenth;
  353. if(DistributorMgr == NULL)
  354. return NOK;
  355. if (NotifReqElment == NULL)
  356. return NOK;
  357. NotifReqElment->Mask |= Mask;
  358. return OK;
  359. }
  360. /***************************************************************************
  361. * DistributorMgr_HaltNotif *
  362. ****************************************************************************
  363. *
  364. * Use this function to add mask bit to the bit mask condition that triggers the Callback
  365. *
  366. *
  367. ***************************************************************************/
  368. VOID DistributorMgr_HaltNotif(TI_HANDLE ReqElmenth)
  369. {
  370. NotifReqElment_t *NotifReqElment = (NotifReqElment_t*)ReqElmenth;
  371. if (NotifReqElment == NULL)
  372. return;
  373. NotifReqElment->HaltReq = TRUE;
  374. }
  375. /***************************************************************************
  376. * DistributorMgr_RestartNotif *
  377. ****************************************************************************
  378. *
  379. * Use this function to add mask bit to the bit mask condition that triggers the Callback
  380. *
  381. *
  382. ***************************************************************************/
  383. VOID DistributorMgr_RestartNotif(TI_HANDLE ReqElmenth)
  384. {
  385. NotifReqElment_t *NotifReqElment = (NotifReqElment_t*)ReqElmenth;
  386. if (NotifReqElment == NULL)
  387. return;
  388. NotifReqElment->HaltReq = FALSE;
  389. }
  390. /***************************************************************************
  391. * DistributorMgr_UnReg *
  392. ****************************************************************************
  393. *
  394. *
  395. ***************************************************************************/
  396. TI_STATUS DistributorMgr_UnReg(TI_HANDLE hDistributorMgr,TI_HANDLE RegEventHandle)
  397. {
  398. DistributorMgr_t *DistributorMgr = (DistributorMgr_t*)hDistributorMgr;
  399. if(DistributorMgr == NULL)
  400. return NOK;
  401. return List_FreeElement(DistributorMgr->DistributionList, RegEventHandle);
  402. }
  403. /***************************************************************************
  404. * DistributorMgr_EventCall *
  405. ****************************************************************************
  406. *
  407. * When the client needs to invoke the callback calls function that corresponds
  408. * to a specific event mask it will call this function with the desired mask.
  409. * And event count that can be used to aggregate the events.
  410. * that way calling this function not for every event
  411. *
  412. ***************************************************************************/
  413. VOID DistributorMgr_EventCall(TI_HANDLE hDistributorMgr,UINT16 Mask,int EventCount)
  414. {
  415. DistributorMgr_t *DistributorMgr = (DistributorMgr_t*)hDistributorMgr;
  416. NotifReqElment_t *NotifReqElment;
  417. if(DistributorMgr == NULL)
  418. return;
  419. NotifReqElment = (NotifReqElment_t*)List_GetFirst(DistributorMgr->DistributionList);
  420. while(NotifReqElment)
  421. {
  422. if((NotifReqElment->Mask & Mask) && !(NotifReqElment->HaltReq))
  423. {
  424. NotifReqElment->CallBack(NotifReqElment->Context,EventCount,Mask,
  425. NotifReqElment->Cookie);
  426. }
  427. NotifReqElment = (NotifReqElment_t*)List_GetNext(DistributorMgr->DistributionList);
  428. }
  429. }
  430. /*******************************************************/