1 /* 2 * ipc_wpa.c 3 * 4 * Copyright 2001-2009 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 19 /**************************************************************************** 20 * 21 * MODULE: Ipc_Wpa.c 22 * 23 * PURPOSE: 24 * 25 * DESCRIPTION: 26 * ============ 27 * 28 * 29 ****************************************************************************/ 30 31 /* includes */ 32 /************/ 33 #include <sys/types.h> 34 35 36 #include "cu_osapi.h" 37 #include "oserr.h" 38 #include "wpa_ctrl.h" 39 #include "ipc_wpa.h" 40 41 /* defines */ 42 /***********/ 43 #define IPC_WPA_CTRL_OPEN_RETRIES 5 44 45 /* local types */ 46 /***************/ 47 /* Module control block */ 48 typedef struct TIpcWpa 49 { 50 struct wpa_ctrl *pWpaCtrl; 51 #if 0 52 S32 socket; 53 struct sockaddr_in local; 54 struct sockaddr_in dest; 55 #endif 56 } TIpcWpa; 57 58 /* local variables */ 59 /*******************/ 60 61 /* local fucntions */ 62 /*******************/ 63 static S32 IpcWpa_Sockets_Open(TIpcWpa* pIpcWpa, PS8 pSupplIfFile) 64 { 65 #ifdef WPA_SUPPLICANT 66 S32 i; 67 68 for(i=0; i< IPC_WPA_CTRL_OPEN_RETRIES; i++) 69 { 70 pIpcWpa->pWpaCtrl = wpa_ctrl_open((char*)pSupplIfFile); 71 if(pIpcWpa->pWpaCtrl) 72 break; 73 } 74 #else 75 pIpcWpa->pWpaCtrl = NULL; 76 #endif 77 if(pIpcWpa->pWpaCtrl == NULL) 78 { 79 os_error_printf(CU_MSG_ERROR, (PS8)"ERROR - IpcWpa_Sockets_Open - can't connect the socket\n"); 80 return EOALERR_IPC_WPA_ERROR_CANT_CONNECT_TO_SUPPL; 81 } 82 83 return OK; 84 } 85 86 static VOID IpcWpa_Sockets_Close(TIpcWpa* pIpcWpa) 87 { 88 #ifdef WPA_SUPPLICANT 89 wpa_ctrl_close(pIpcWpa->pWpaCtrl); 90 #endif 91 } 92 93 94 /* functions */ 95 /*************/ 96 THandle IpcWpa_Create(PS32 pRes, PS8 pSupplIfFile) 97 { 98 TIpcWpa* pIpcWpa = (TIpcWpa*)os_MemoryCAlloc(sizeof(TIpcWpa), sizeof(U8)); 99 if(pIpcWpa == NULL) 100 { 101 *pRes = OK; 102 os_error_printf(CU_MSG_ERROR, (PS8)"ERROR - IpcWpa_Create - cant allocate control block\n"); 103 return NULL; 104 } 105 106 *pRes = IpcWpa_Sockets_Open(pIpcWpa, pSupplIfFile); 107 if(*pRes) 108 { 109 IpcWpa_Destroy(pIpcWpa); 110 return NULL; 111 } 112 113 return pIpcWpa; 114 } 115 116 VOID IpcWpa_Destroy(THandle hIpcWpa) 117 { 118 TIpcWpa* pIpcWpa = (TIpcWpa*)hIpcWpa; 119 120 if(pIpcWpa->pWpaCtrl) 121 IpcWpa_Sockets_Close(pIpcWpa); 122 123 os_MemoryFree(pIpcWpa); 124 } 125 126 S32 IpcWpa_Command(THandle hIpcWpa, PS8 cmd, S32 print) 127 { 128 #ifdef WPA_SUPPLICANT 129 TIpcWpa* pIpcWpa = (TIpcWpa*)hIpcWpa; 130 S8 Resp[IPC_WPA_RESP_MAX_LEN]; 131 TI_SIZE_T RespLen = IPC_WPA_RESP_MAX_LEN - 1; 132 S32 ret; 133 134 ret = wpa_ctrl_request(pIpcWpa->pWpaCtrl, (char*)cmd, os_strlen(cmd), (char*)Resp, (size_t*)&RespLen, NULL); 135 136 if (ret == -2) 137 { 138 os_error_printf(CU_MSG_ERROR, (PS8)"'%s' command timed out.\n", cmd); 139 return EOALERR_IPC_WPA_ERROR_CMD_TIMEOUT; 140 } 141 else if (ret < 0) 142 { 143 os_error_printf(CU_MSG_ERROR, (PS8)"'%s' command failed (%d).\n", cmd, ret); 144 return EOALERR_IPC_WPA_ERROR_CMD_FAILED; 145 } 146 if (print) 147 { 148 Resp[RespLen] = '\0'; 149 os_error_printf(CU_MSG_INFO2, (PS8)"%s", Resp); 150 } 151 return OK; 152 #else 153 return EOALERR_IPC_WPA_ERROR_CMD_FAILED; 154 #endif 155 } 156 157 S32 IpcWpa_CommandWithResp(THandle hIpcWpa, PS8 cmd, S32 print, PS8 pResp, PU32 pRespLen) 158 { 159 #ifdef WPA_SUPPLICANT 160 TIpcWpa* pIpcWpa = (TIpcWpa*)hIpcWpa; 161 S32 ret; 162 163 *pRespLen = IPC_WPA_RESP_MAX_LEN - 1; 164 ret = wpa_ctrl_request(pIpcWpa->pWpaCtrl, (char*)cmd, os_strlen(cmd), (char*)pResp, (size_t*)pRespLen, NULL); 165 166 if (ret == -2) 167 { 168 os_error_printf(CU_MSG_ERROR, (PS8)"'%s' command timed out.\n", cmd); 169 return EOALERR_IPC_WPA_ERROR_CMD_TIMEOUT; 170 } 171 else if (ret < 0) 172 { 173 os_error_printf(CU_MSG_ERROR, (PS8)"'%s' command failed.\n", cmd); 174 return EOALERR_IPC_WPA_ERROR_CMD_FAILED; 175 } 176 if (print) 177 { 178 pResp[*pRespLen] = '\0'; 179 os_error_printf(CU_MSG_INFO2, (PS8)"%s", pResp); 180 } 181 return OK; 182 #else 183 return EOALERR_IPC_WPA_ERROR_CMD_FAILED; 184 #endif 185 } 186