• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "lnn_driver_request.h"
17 
18 #include <hdf_io_service.h>
19 #include <hdf_sbuf.h>
20 #include <securec.h>
21 
22 #include "softbus_errcode.h"
23 #include "softbus_log.h"
24 
25 #define DRIVER_SERVICE_NAME "hdf_dsoftbus"
26 
ParseReply(struct HdfSBuf * rspData,uint8_t * reply,uint32_t replyLen)27 static int32_t ParseReply(struct HdfSBuf *rspData, uint8_t *reply, uint32_t replyLen)
28 {
29     uint8_t *data = NULL;
30     uint32_t dataSize;
31 
32     if (reply == NULL) {
33         return SOFTBUS_ERR;
34     }
35     if (!HdfSbufReadBuffer(rspData, (const void **)&data, &dataSize)) {
36         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "read cmd reply fail");
37         return SOFTBUS_ERR;
38     }
39     if (dataSize > replyLen) {
40         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "no enough space save reply");
41         return SOFTBUS_ERR;
42     }
43     if (memcpy_s(reply, replyLen, data, dataSize) != EOK) {
44         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "memcpy reply fail");
45         return SOFTBUS_ERR;
46     }
47     return SOFTBUS_OK;
48 }
49 
LnnSendCmdToDriver(int32_t moduleId,const uint8_t * cmd,uint32_t cmdLen,uint8_t * reply,uint32_t replyLen)50 int32_t LnnSendCmdToDriver(int32_t moduleId, const uint8_t *cmd, uint32_t cmdLen,
51     uint8_t *reply, uint32_t replyLen)
52 {
53     int32_t rc = SOFTBUS_ERR;
54     struct HdfIoService *softbusService = NULL;
55     struct HdfSBuf *reqData = NULL;
56     struct HdfSBuf *rspData = NULL;
57 
58     if (cmd == NULL) {
59         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "invalid cmd for module %d", moduleId);
60         return SOFTBUS_INVALID_PARAM;
61     }
62     softbusService = HdfIoServiceBind(DRIVER_SERVICE_NAME);
63     if (softbusService == NULL) {
64         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "bind hdf softbus fail for module %d", moduleId);
65         return SOFTBUS_ERR;
66     }
67     if (softbusService->dispatcher == NULL ||
68         softbusService->dispatcher->Dispatch == NULL) {
69         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "bind hdf softbus fail for module %d", moduleId);
70         HdfIoServiceRecycle(softbusService);
71         return SOFTBUS_ERR;
72     }
73     reqData = HdfSbufObtainDefaultSize();
74     rspData = HdfSbufObtainDefaultSize();
75     do {
76         if (reqData == NULL || rspData == NULL) {
77             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "obtain sbuf fail for module %d", moduleId);
78             break;
79         }
80         if (!HdfSbufWriteBuffer(reqData, cmd, cmdLen)) {
81             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "write sbuf fail for module %d", moduleId);
82             break;
83         }
84         rc = softbusService->dispatcher->Dispatch(&softbusService->object, moduleId, reqData, rspData);
85         if (rc != 0) {
86             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "send command fail for module %d", moduleId);
87         }
88     } while (false);
89     if (rc == SOFTBUS_OK) {
90         rc = ParseReply(rspData, reply, replyLen);
91     }
92     if (reqData != NULL) {
93         HdfSbufRecycle(reqData);
94     }
95     if (rspData != NULL) {
96         HdfSbufRecycle(rspData);
97     }
98     HdfIoServiceRecycle(softbusService);
99     return rc;
100 }