PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wl1271_softAP/CUDK/configurationutility/src/cu_hostapd.c

https://github.com/MiniCMX/android_hardware_ti_wlan
C | 357 lines | 258 code | 74 blank | 25 comment | 45 complexity | 1df2b0c8cfef749f5d09659d48f594fa MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. /*
  2. * cu_hostapd.c
  3. *
  4. * Copyright 2001-2010 Texas Instruments, Inc. - http://www.ti.com/
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include <stdio.h>
  19. #include "cu_osapi.h"
  20. #include "convert.h"
  21. #include "cu_common.h"
  22. #include "cu_os.h"
  23. #include "ipc_event.h"
  24. #include "oserr.h"
  25. #include "cu_hostapd.h"
  26. static TDictionary *pDictionary = NULL;
  27. static int IsLineContainsKey(TI_UINT8 *pLine);
  28. static void PrintDictionary(TDictionary *pDic);
  29. static void FillDictionary(FILE *pFile, TDictionary *pDic);
  30. static void FlushDictionaryToFile(TI_UINT8 *fileName, TDictionary *pDic);
  31. static void GetKeyValueFromLine(TI_UINT8 *pLine, TKeyValuePair *pKeyValPair);
  32. static void CopyFile(TI_UINT8 *filePathSource, TI_UINT8 *filePathDest);
  33. static TI_BOOL IsKeyInDictionary(keyInfo *pKeyInfo);
  34. static char* GetValueByKeyName(char *pKeyName, TI_BOOL *refKeyModified);
  35. static void removeKey( TI_UINT8 index);
  36. void CuHostapd_PrintMenu(void)
  37. {
  38. int i;
  39. char *pKeyDesc;
  40. TI_BOOL keyInFile;
  41. for (i =0 ; i< HOSTAPD_PARAM_LAST ; i++)
  42. {
  43. keyInFile = IsKeyInDictionary(&hostapdKeysList[i]);
  44. printf("%d. %s => %s %s\n", (i+1), hostapdKeysList[i].keyName,
  45. &hostapdKeysList[i].keyDescription, keyInFile ? " ":"N/A") ;
  46. }
  47. }
  48. static char* GetValueByKeyName(char *pKeyName, TI_BOOL *refKeyModified)
  49. {
  50. int i;
  51. for (i =0 ; i< pDictionary->uNumOfKeysFound ; i++)
  52. {
  53. if (0 == os_strcmp((PS8)pDictionary->keys[i].key, pKeyName))
  54. {
  55. *refKeyModified = pDictionary->aKeyModifiedFlag[i];
  56. return (PS8)pDictionary->keys[i].value;
  57. }
  58. }
  59. *refKeyModified = TI_FALSE;
  60. return "N/A";
  61. }
  62. static TI_BOOL IsKeyInDictionary(keyInfo *pKeyInfo)
  63. {
  64. int i;
  65. for (i =0 ; i< pDictionary->uNumOfKeysFound ; i++)
  66. {
  67. if (0 == os_strcmp((PS8)pDictionary->keys[i].key, (PS8)pKeyInfo->keyName))
  68. {
  69. return TI_TRUE;
  70. }
  71. }
  72. return TI_FALSE;
  73. }
  74. void CuHostapd_SaveChanges(void)
  75. {
  76. FlushDictionaryToFile((TI_UINT8*)HOSTAPD_FILE_NAME_ORIGINAL, pDictionary);
  77. /*IpcEvent_ReconfigHostapd();*/
  78. }
  79. void CuHostapd_ShowStatus(void)
  80. {
  81. int i;
  82. char *pVal = NULL;
  83. TI_BOOL bKeyWasModified = TI_FALSE;
  84. for (i =0 ; i < HOSTAPD_PARAM_LAST ; i++)
  85. {
  86. pVal = GetValueByKeyName(hostapdKeysList[i].keyName, &bKeyWasModified);
  87. printf("%d. %s%s = %s\n", (i+1), (bKeyWasModified == TI_TRUE? "*" : "") ,
  88. hostapdKeysList[i].keyName, pVal) ;
  89. }
  90. printf("\n *key was modified but has not been applied yet. \n");
  91. }
  92. void CuHostapd_UpdateKeyInDictionary(TKeyValuePair *pKeyVal)
  93. {
  94. int i;
  95. TI_BOOL isFound = TI_FALSE;
  96. for (i=0 ; i< pDictionary->uNumOfKeysFound ; i++)
  97. {
  98. if (os_strcmp((PS8)pDictionary->keys[i].key, (PS8)pKeyVal->key) == 0) /* if key found store it in dictionary */
  99. {
  100. if( pKeyVal->uValueLength == 0 || pKeyVal->uValueLength > MAX_VALUE_LENGTH)
  101. {
  102. /* Remove record */
  103. printf("Remove key(%d) = %s, value = %s\n", i, pDictionary->keys[i].key, pDictionary->keys[i].value);
  104. removeKey(i);
  105. }
  106. else
  107. {
  108. // printf("Key to update found: old value = %s, NewValue = %s \n",pDictionary->keys[i].value, pKeyVal->value);
  109. os_memcpy((PVOID)pDictionary->keys[i].value, (PVOID)pKeyVal->value, pKeyVal->uValueLength);
  110. pDictionary->keys[i].value[pKeyVal->uValueLength] = '\0';
  111. pDictionary->keys[i].uValueLength = pKeyVal->uValueLength;
  112. pDictionary->aKeyModifiedFlag[i] = TI_TRUE;
  113. }
  114. return;
  115. }
  116. }
  117. /* Key was not found in dictionary. Try to find key in hostapdKeysList */
  118. for(i = 0; i < HOSTAPD_PARAM_LAST; i++)
  119. {
  120. if (0 == os_strcmp((PS8)hostapdKeysList[i].keyName, (PS8)pKeyVal->key))
  121. {
  122. isFound = TI_TRUE;
  123. break;
  124. }
  125. }
  126. if(isFound)
  127. {
  128. if( pKeyVal->uValueLength == 0 || pKeyVal->uValueLength > MAX_VALUE_LENGTH)
  129. {
  130. printf("Illegal value!!!\n");
  131. return;
  132. }
  133. /* Add new record to dictionary */
  134. i = pDictionary->uNumOfKeysFound;
  135. os_memcpy((PVOID)pDictionary->keys[i].key, (PVOID)pKeyVal->key, (PVOID)pKeyVal->uKeyLength);
  136. pDictionary->keys[i].key[pKeyVal->uKeyLength] = '\0';
  137. os_memcpy((PVOID)pDictionary->keys[i].value, (PVOID)pKeyVal->value, pKeyVal->uValueLength);
  138. pDictionary->keys[i].value[pKeyVal->uValueLength] = '\0';
  139. pDictionary->keys[i].uValueLength = pKeyVal->uValueLength;
  140. pDictionary->aKeyModifiedFlag[i] = TI_TRUE;
  141. pDictionary->uNumOfKeysFound++;
  142. printf("New record <<%s=%s>> was added to hostapd configuration.\n",pDictionary->keys[i].key,pDictionary->keys[i].value);
  143. }
  144. else
  145. {
  146. printf("Error! The '%s' Key was not found or disabled! \n", pKeyVal->key);
  147. }
  148. }
  149. void CuHostapd_Destroy (void)
  150. {
  151. printf("\n ***** Destroying Hostapd!! ***** \n");
  152. os_MemoryFree(pDictionary);
  153. }
  154. void CuHostapd_LoadConfFileToMemory (void)
  155. {
  156. FILE *pFile;
  157. /* Duplicate the original file and work on it only from now on */
  158. CopyFile((TI_UINT8*)HOSTAPD_FILE_NAME_ORIGINAL, (TI_UINT8*)HOSTAPD_FILE_NAME_TEMP);
  159. pFile = os_fopen (HOSTAPD_FILE_NAME_ORIGINAL, OS_FOPEN_READ);
  160. if (pFile == NULL)
  161. {
  162. perror ("\nError opening file\n");
  163. return;
  164. }
  165. else
  166. {
  167. pDictionary = os_MemoryAlloc(sizeof(TDictionary));
  168. FillDictionary(pFile, pDictionary);
  169. os_fclose ((PVOID)pFile);
  170. }
  171. }
  172. static void FillDictionary(FILE *pFile, TDictionary *pDic)
  173. {
  174. TI_UINT8 line[100];
  175. TI_UINT8 i=0;
  176. pDic->uNumOfKeysFound = 0;
  177. while ( os_fgets(line, 99, (PVOID)pFile) != NULL )
  178. {
  179. if (IsLineContainsKey(line))
  180. {
  181. GetKeyValueFromLine(line, &pDic->keys[i]);
  182. pDic->uNumOfKeysFound++;
  183. i++;
  184. }
  185. }
  186. }
  187. static void PrintDictionary(TDictionary *pDic)
  188. {
  189. int i;
  190. printf("Dictionary Keys Values list:\n");
  191. for (i=0 ; i< pDic->uNumOfKeysFound ; i++)
  192. {
  193. printf("%d. %s = %s \n", i, pDic->keys[i].key ,pDic->keys[i].value);
  194. }
  195. }
  196. static int IsLineContainsKey(TI_UINT8 *pLine)
  197. {
  198. if ((*pLine >= 'a' && *pLine <= 'z') || (*pLine >= 'A' && *pLine <= 'Z'))
  199. {
  200. return 1;
  201. }
  202. return 0;
  203. };
  204. static void GetKeyValueFromLine(TI_UINT8 *pLine, TKeyValuePair *pKeyValPair)
  205. {
  206. TI_UINT8 endOfLineDelimiter = 10;
  207. TI_UINT8 *pTI_UINT8 = pLine;
  208. TI_UINT8 i=0;
  209. pKeyValPair->uKeyLength = 0;
  210. pKeyValPair->uValueLength = 0;
  211. while (*pTI_UINT8 != '=')
  212. {
  213. pKeyValPair->key[i] = *pTI_UINT8;
  214. pKeyValPair->uKeyLength++;
  215. pTI_UINT8 ++;
  216. i++;
  217. }
  218. pKeyValPair->key[i] = '\0';
  219. pTI_UINT8++;
  220. i=0;
  221. while (*pTI_UINT8 != endOfLineDelimiter)
  222. {
  223. pKeyValPair->value[i] = *pTI_UINT8;
  224. pKeyValPair->uValueLength++;
  225. pTI_UINT8++;
  226. i++;
  227. }
  228. pKeyValPair->value[i] = '\0';
  229. }
  230. static void FlushDictionaryToFile(TI_UINT8 *fileName, TDictionary *pDic)
  231. {
  232. FILE *pFile;
  233. int i=0;
  234. TI_UINT8 *header = "\n\n##################### HOSTAPD Configuration File - TEXAS INSTRUMENTS #####################\n";
  235. pFile = os_fopen (fileName, OS_FOPEN_WRITE);
  236. if(pFile == NULL)
  237. {
  238. printf("Can't open file %s\n", fileName);
  239. return;
  240. }
  241. fprintf(pFile, "%s\n\n", header);
  242. for (i=0 ; i< pDic->uNumOfKeysFound ; i++)
  243. {
  244. fprintf(pFile, "%s=%s\n", pDic->keys[i].key, pDic->keys[i].value);
  245. }
  246. /* After flushing reset the 'Modified' flag */
  247. for (i =0 ; i < MAX_NUM_OF_KEYS ; i++)
  248. {
  249. pDic->aKeyModifiedFlag[i] = TI_FALSE;
  250. }
  251. fclose(pFile);
  252. }
  253. static void CopyFile(TI_UINT8 *filePathSource, TI_UINT8 *filePathDest)
  254. {
  255. FILE *pFileSource, *pFileDest;
  256. int uFileSize = 0;
  257. char *pFileBuffer;
  258. pFileSource = os_fopen (filePathSource, OS_FOPEN_READ_BINARY);
  259. pFileDest = os_fopen (filePathDest, OS_FOPEN_WRITE_BINARY);
  260. if (pFileSource == 0 || pFileDest == 0)
  261. {
  262. printf("\n Error while opening hostapd config file! \n");
  263. return;
  264. }
  265. uFileSize = os_getFileSize(pFileSource);
  266. pFileBuffer = os_MemoryAlloc(uFileSize);
  267. os_fread(pFileBuffer, 1, uFileSize, pFileSource);
  268. os_fwrite(pFileBuffer, 1, uFileSize, pFileDest);
  269. /* Free the resources */
  270. os_MemoryFree(pFileBuffer);
  271. os_fclose((PVOID)pFileDest);
  272. os_fclose((PVOID)pFileSource);
  273. }
  274. static void removeKey( TI_UINT8 index)
  275. {
  276. TI_UINT8 i;
  277. for(i = index; i < pDictionary->uNumOfKeysFound - 1; i++)
  278. {
  279. os_memcpy(&pDictionary->keys[i], &pDictionary->keys[i+1], sizeof(TKeyValuePair));
  280. pDictionary->aKeyModifiedFlag[i] = pDictionary->aKeyModifiedFlag[i+1];
  281. }
  282. pDictionary->uNumOfKeysFound--;
  283. }