1 /*
2  * netutil.c - CC31xx/CC32xx Host Driver Implementation
3  *
4  * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
5  *
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  *
14  *    Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the
17  *    distribution.
18  *
19  *    Neither the name of Texas Instruments Incorporated nor the names of
20  *    its contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35 */
36 
37 
38 /*****************************************************************************/
39 /* Include files                                                             */
40 /*****************************************************************************/
41 #include <ti/drivers/net/wifi/simplelink.h>
42 #include <ti/drivers/net/wifi/source/protocol.h>
43 #include <ti/drivers/net/wifi/source/driver.h>
44 #include <ti/drivers/net/wifi/source/flowcont.h>
45 
46 /*****************************************************************************/
47 /* Internal functions                                                        */
48 /*****************************************************************************/
49 
50 /*****************************************************************************/
51 /* API Functions                                                             */
52 /*****************************************************************************/
53 
54 
55 /******************************************************************************
56 sl_UtilsGet
57 ******************************************************************************/
58 
59 typedef union
60 {
61     SlNetUtilSetGet_t    Cmd;
62     SlNetUtilSetGet_t    Rsp;
63 } SlNetUtilMsgGet_u;
64 
65 #if _SL_INCLUDE_FUNC(sl_NetUtilGet)
66 
67 const _SlCmdCtrl_t _SlNetUtilGetCmdCtrl =
68 {
69     SL_OPCODE_NETUTIL_GET,
70     sizeof(SlNetUtilSetGet_t),
71     sizeof(SlNetUtilSetGet_t)
72 };
73 
sl_NetUtilGet(const _u16 Option,const _u32 ObjID,_u8 * pValues,_u16 * pValueLen)74 _i16 sl_NetUtilGet(const _u16 Option, const _u32 ObjID, _u8 *pValues, _u16 *pValueLen)
75 {
76     SlNetUtilMsgGet_u       Msg;
77     _SlCmdExt_t             CmdExt;
78 
79     _SlDrvResetCmdExt(&CmdExt);
80     CmdExt.RxPayloadLen         = *pValueLen;
81     CmdExt.pRxPayload           = (_u8 *)pValues;
82 
83     Msg.Cmd.Option              = Option;
84     Msg.Cmd.ObjId               = ObjID;
85     Msg.Cmd.ValueLen            = *pValueLen;
86 
87     VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlNetUtilGetCmdCtrl, &Msg, &CmdExt));
88 
89     if(CmdExt.RxPayloadLen < CmdExt.ActualRxPayloadLen)
90     {
91         *pValueLen = CmdExt.RxPayloadLen;
92         return SL_ESMALLBUF;
93     }
94     else
95     {
96         *pValueLen = CmdExt.ActualRxPayloadLen;
97     }
98 
99     return (_i16)Msg.Rsp.Status;
100 }
101 #endif
102 
103 
104 /***************************************************************************
105 _SlNetUtilHandleAsync_Cmd - handles NetUtil Cmd response, signalling to
106 a waiting object
107 ****************************************************************************/
_SlNetUtilHandleAsync_Cmd(void * pVoidBuf)108 void _SlNetUtilHandleAsync_Cmd(void *pVoidBuf)
109 {
110     _SlNetUtilCmdData_t *pOutData;
111     SlNetUtilCmdRsp_t   *pMsgArgs   = (SlNetUtilCmdRsp_t *)_SL_RESP_ARGS_START(pVoidBuf);
112 
113     SL_DRV_PROTECTION_OBJ_LOCK_FOREVER();
114 
115     VERIFY_SOCKET_CB(NULL != g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].pRespArgs);
116 
117     pOutData = (_SlNetUtilCmdData_t*)g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].pRespArgs;
118 
119     pOutData->Status = pMsgArgs->Status;
120 
121     if(SL_RET_CODE_OK == pMsgArgs->Status)
122     {
123         if (*(pOutData->pOutputLen) < pMsgArgs->OutputLen)
124         {
125             pOutData->Status = SL_ESMALLBUF;
126         }
127         else
128         {
129             *(pOutData->pOutputLen) = pMsgArgs->OutputLen;
130 
131             if(*(pOutData->pOutputLen) > 0)
132             {
133                 /* copy only the data from the global async buffer  */
134                 sl_Memcpy(pOutData->pOutputValues, (char*)pMsgArgs + sizeof(SlNetUtilCmdRsp_t), *(pOutData->pOutputLen));
135             }
136         }
137     }
138 
139     _SlDrvSyncObjSignal(&g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].SyncObj);
140     _SlDrvProtectionObjUnLock();
141     return;
142 }
143 
144 
145 /*****************************************************************************
146 sl_NetUtilCmd
147 ******************************************************************************/
148 typedef union
149 {
150     SlNetUtilCmd_t      Cmd;
151     _BasicResponse_t    Rsp;
152 } SlNetUtilCmdMsg_u;
153 
154 #if _SL_INCLUDE_FUNC(sl_NetUtilCmd)
155 const _SlCmdCtrl_t _SlNetUtilCmdCtrl =
156 {
157     SL_OPCODE_NETUTIL_COMMAND,
158     sizeof(SlNetUtilCmd_t),
159     sizeof(_BasicResponse_t)
160 };
161 
sl_NetUtilCmd(const _u16 Cmd,const _u8 * pAttrib,const _u16 AttribLen,const _u8 * pInputValues,const _u16 InputLen,_u8 * pOutputValues,_u16 * pOutputLen)162 _i16 sl_NetUtilCmd(const _u16 Cmd, const _u8 *pAttrib,  const _u16 AttribLen,
163                    const _u8 *pInputValues, const _u16 InputLen,
164                    _u8 *pOutputValues, _u16 *pOutputLen)
165 {
166     _i16                RetVal=0;
167     SlNetUtilCmdMsg_u   Msg;
168     _i16                ObjIdx = MAX_CONCURRENT_ACTIONS;
169     _SlCmdExt_t         CmdExt;
170     _SlNetUtilCmdData_t OutData;
171 
172     /* prepare the Cmd (control structure and data-buffer) */
173     Msg.Cmd.Cmd         = Cmd;
174     Msg.Cmd.AttribLen   = AttribLen;
175     Msg.Cmd.InputLen    = InputLen;
176     Msg.Cmd.OutputLen   = *pOutputLen;
177 
178     _SlDrvResetCmdExt(&CmdExt);
179     _SlDrvMemZero(&OutData, sizeof(_SlNetUtilCmdData_t));
180 
181     if(AttribLen > 0)
182     {
183         CmdExt.pTxPayload1 = (_u8*)pAttrib;
184         CmdExt.TxPayload1Len = AttribLen;
185     }
186 
187     if (InputLen > 0)
188     {
189         CmdExt.pTxPayload2 = (_u8*)pInputValues;
190         CmdExt.TxPayload2Len = InputLen;
191     }
192 
193     /* Set the pointers to be filled upon the async event reception */
194     OutData.pOutputValues = pOutputValues;
195     OutData.pOutputLen = pOutputLen;
196 
197     ObjIdx = _SlDrvProtectAsyncRespSetting((_u8*)&OutData, NETUTIL_CMD_ID, SL_MAX_SOCKETS);
198     if (ObjIdx < 0)
199     {
200         return ObjIdx;
201     }
202 
203     /* send the command */
204     VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlNetUtilCmdCtrl, &Msg, &CmdExt));
205 
206     if(SL_OS_RET_CODE_OK == (_i16)Msg.Rsp.status)
207     {
208         /* after the async event is signaled, the data will be copied to the pOutputValues buffer  */
209         RetVal = _SlDrvWaitForInternalAsyncEvent(ObjIdx, 0, 0);
210 
211         if(RetVal >= 0)
212         {
213             /* the response header status */
214             RetVal = OutData.Status;
215         }
216     }
217     else
218     {
219         RetVal = Msg.Rsp.status;
220     }
221 
222     _SlDrvReleasePoolObj((_u8)ObjIdx);
223 
224     return RetVal;
225 }
226 #endif
227