Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "securec.h"
12 : #include "user_log.h"
13 : #include "ra_rs_comm.h"
14 : #include "ra_rs_err.h"
15 : #include "hccp_tlv.h"
16 : #include "ra_hdc_tlv.h"
17 :
18 0 : int RaHdcTlvInit(struct RaTlvHandle *tlvHandle)
19 : {
20 0 : unsigned int phyId = tlvHandle->initInfo.phyId;
21 0 : unsigned int opCode = RA_RS_TLV_INIT_V1;
22 0 : union OpTlvInitData tlvData = {0};
23 0 : unsigned int interfaceVersion = 0;
24 0 : int ret = 0;
25 :
26 0 : tlvData.txData.phyId = phyId;
27 :
28 0 : ret = RaHdcGetInterfaceVersion(phyId, RA_RS_TLV_INIT, &interfaceVersion);
29 0 : if (ret == 0 && interfaceVersion >= RA_RS_OPCODE_BASE_VERSION) {
30 0 : opCode = RA_RS_TLV_INIT;
31 : } else {
32 0 : ret = -ENOTSUPP;
33 0 : hccp_warn("[init][ra_hdc_tlv]ra tlv init version not support, phy_id(%u)", phyId);
34 0 : return ret;
35 : }
36 :
37 0 : ret = RaHdcProcessMsg(opCode, phyId, (char *)&tlvData, sizeof(union OpTlvInitData));
38 0 : CHK_PRT_RETURN(ret == -ENOTSUPP,
39 : hccp_warn("[init][ra_hdc_tlv]ra hdc message process unsuccessful ret(%d) phy_id(%u)", ret, phyId), ret);
40 0 : CHK_PRT_RETURN(ret != 0, hccp_err("[init][ra_hdc_tlv]ra hdc message process failed ret(%d) phy_id(%u)", ret, phyId),
41 : ret);
42 :
43 0 : tlvHandle->bufferSize = tlvData.rxData.bufferSize;
44 0 : return ret;
45 : }
46 :
47 0 : int RaHdcTlvDeinit(struct RaTlvHandle *tlvHandle)
48 : {
49 0 : unsigned int phyId = tlvHandle->initInfo.phyId;
50 0 : union OpTlvDeinitData tlvData = {0};
51 : int ret;
52 :
53 0 : tlvData.txData.phyId = phyId;
54 :
55 0 : ret = RaHdcProcessMsg(RA_RS_TLV_DEINIT, phyId, (char *)&tlvData, sizeof(union OpTlvDeinitData));
56 0 : CHK_PRT_RETURN(ret != 0,
57 : hccp_err("[deinit][ra_hdc_tlv]ra hdc message process failed ret(%d) phy_id(%u)", ret, phyId), ret);
58 :
59 0 : return 0;
60 : }
61 :
62 2 : STATIC void RaHdcTlvRequestHeadInit(struct RaTlvHandle *tlvHandle, unsigned int moduleType, struct TlvMsg *sendMsg,
63 : struct TlvRequestMsgHead *head)
64 : {
65 2 : head->moduleType = moduleType;
66 2 : head->phyId = tlvHandle->initInfo.phyId;
67 2 : head->totalBytes = sendMsg->length;
68 2 : head->type = sendMsg->type;
69 2 : head->offset = 0;
70 2 : }
71 :
72 1 : STATIC int RaHdTlvRequestForSendNullMsg(unsigned int phyId, union OpTlvRequestDataCommon opTlvCommon,
73 : struct TlvRequestMsgHead *head, struct TlvMsg *recvMsg)
74 : {
75 1 : void *tlvData = opTlvCommon.txData.tlvData;
76 1 : int ret = 0;
77 :
78 1 : (void)memcpy_s(opTlvCommon.txData.head, sizeof(struct TlvRequestMsgHead), head, sizeof(struct TlvRequestMsgHead));
79 :
80 1 : ret = RaHdcProcessMsg(opTlvCommon.txData.opcode, phyId, tlvData, opTlvCommon.txData.size);
81 1 : CHK_PRT_RETURN(ret == -EUSERS || ret == -ENOTSUPP,
82 : hccp_warn("[request][ra_hdc_tlv]hdc message process unsuccessful ret(%d) phy_id(%u)", ret, phyId), ret);
83 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[request][ra_hdc_tlv]hdc message process failed ret(%d) phy_id(%u)", ret, phyId),
84 : ret);
85 :
86 1 : TLV_SETUP_RX(opTlvCommon.txData.opcode == RA_RS_TLV_REQUEST, tlvData, opTlvCommon);
87 1 : recvMsg->type = head->type;
88 1 : recvMsg->length = *opTlvCommon.rxData.recvBytes;
89 1 : return ret;
90 : }
91 :
92 1 : STATIC int RaTlvRequestGetTlvMsg(struct TlvRequestMsgHead *head, union OpTlvRequestDataCommon opTlvCommon,
93 : struct TlvMsg *recvMsg)
94 : {
95 1 : void *tlvData = opTlvCommon.txData.tlvData;
96 1 : int ret = 0;
97 :
98 1 : TLV_SETUP_RX(opTlvCommon.txData.opcode == RA_RS_TLV_REQUEST, tlvData, opTlvCommon);
99 :
100 1 : if (recvMsg->data == NULL || recvMsg->length == 0) {
101 1 : *opTlvCommon.rxData.recvBytes = 0;
102 1 : goto out;
103 : }
104 :
105 0 : CHK_PRT_RETURN(*opTlvCommon.rxData.recvBytes > recvMsg->length,
106 : hccp_err("[request][ra_hdc_tlv]rxData.recvBytes(%u) > recvLen(%u), phyId(%u)", *opTlvCommon.rxData.recvBytes,
107 : recvMsg->length, head->phyId),
108 : -EINVAL);
109 :
110 0 : ret = memcpy_s(recvMsg->data, recvMsg->length, opTlvCommon.rxData.recvData, *opTlvCommon.rxData.recvBytes);
111 0 : CHK_PRT_RETURN(ret != 0,
112 : hccp_err("[request][ra_hdc_tlv]memcpy_s recvData failed, ret(%d) rxData.recvBytes(%u)"
113 : " recvLen(%u) phyId(%u)",
114 : ret, *opTlvCommon.rxData.recvBytes, recvMsg->length, head->phyId),
115 : -ESAFEFUNC);
116 :
117 0 : out:
118 1 : recvMsg->type = head->type;
119 1 : recvMsg->length = *opTlvCommon.rxData.recvBytes;
120 1 : return ret;
121 : }
122 :
123 2 : int RaHdcTlvRequest(struct RaTlvHandle *tlvHandle, unsigned int moduleType, struct TlvMsg *sendMsg,
124 : struct TlvMsg *recvMsg)
125 : {
126 2 : unsigned int phyId = tlvHandle->initInfo.phyId;
127 2 : union OpTlvRequestDataCommon opTlvCommon = {0};
128 2 : struct TlvRequestMsgHead head = {0};
129 : unsigned int interfaceVersion;
130 : void *tlvData;
131 2 : int ret = 0;
132 :
133 2 : RaHdcGetInterfaceVersion(phyId, RA_RS_TLV_REQUEST_V2, &interfaceVersion);
134 2 : tlvData = calloc(1, (interfaceVersion == 0) ? sizeof(union OpTlvRequestData) : sizeof(union OpTlvRequestDataV2));
135 2 : CHK_PRT_RETURN(tlvData == NULL, hccp_err("[ra_hdc_tlv_request]calloc op tlv request data failed! phyId(%u)", phyId),
136 : -ENOMEM);
137 2 : TLV_SETUP_TX(interfaceVersion == 0, tlvData, opTlvCommon);
138 2 : RaHdcTlvRequestHeadInit(tlvHandle, moduleType, sendMsg, &head);
139 2 : if (sendMsg->length == 0) {
140 1 : ret = RaHdTlvRequestForSendNullMsg(phyId, opTlvCommon, &head, recvMsg);
141 1 : goto out;
142 : }
143 :
144 12 : while (head.offset < sendMsg->length) {
145 11 : head.sendBytes = (head.totalBytes - head.offset) >= opTlvCommon.txData.dataLength
146 : ? opTlvCommon.txData.dataLength
147 11 : : (head.totalBytes - head.offset);
148 11 : (void)memcpy_s(opTlvCommon.txData.head, sizeof(struct TlvRequestMsgHead), &head,
149 : sizeof(struct TlvRequestMsgHead));
150 :
151 11 : (void)memset_s(opTlvCommon.txData.data, opTlvCommon.txData.dataLength, 0, opTlvCommon.txData.dataLength);
152 11 : ret = memcpy_s(opTlvCommon.txData.data, opTlvCommon.txData.dataLength, (sendMsg->data + head.offset),
153 11 : head.sendBytes);
154 11 : if (ret != 0) {
155 0 : hccp_err("[request][ra_hdc_tlv]memcpy_s data failed ret(%d) phy_id(%u) send_bytes(%u)", ret, phyId,
156 : head.sendBytes);
157 0 : ret = -ESAFEFUNC;
158 0 : goto out;
159 : }
160 :
161 11 : ret = RaHdcProcessMsg(opTlvCommon.txData.opcode, phyId, opTlvCommon.txData.tlvData, opTlvCommon.txData.size);
162 11 : if (ret == -EUSERS || ret == -ENOTSUPP) {
163 0 : hccp_warn("[request][ra_hdc_tlv]hdc message process unsuccessful ret(%d) phy_id(%u)", ret, phyId);
164 0 : ret = -ESAFEFUNC;
165 0 : goto out;
166 11 : } else if (ret != 0) {
167 0 : hccp_err("[request][ra_hdc_tlv]hdc message process failed ret(%d) phy_id(%u)", ret, phyId);
168 0 : goto out;
169 : }
170 11 : head.offset += head.sendBytes;
171 : }
172 :
173 1 : ret = RaTlvRequestGetTlvMsg(&head, opTlvCommon, recvMsg);
174 1 : if (ret != 0) {
175 0 : hccp_err("[request][ra_hdc_tlv]RaTlvRequestGetTlvMsg failed ret(%d) phy_id(%u)", ret, phyId);
176 0 : goto out;
177 : }
178 1 : out:
179 2 : free(tlvData);
180 2 : tlvData = NULL;
181 2 : return ret;
182 : }
|