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, hccp_warn("[init][ra_hdc_tlv]ra hdc message process unsuccessful ret(%d) phy_id(%u)",
39 : 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)",
41 : ret, phyId), 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, hccp_err("[deinit][ra_hdc_tlv]ra hdc message process failed ret(%d) phy_id(%u)",
57 : ret, phyId), ret);
58 :
59 0 : return 0;
60 : }
61 :
62 2 : STATIC void RaHdcTlvRequestHeadInit(struct RaTlvHandle *tlvHandle, unsigned int moduleType,
63 : struct TlvMsg *sendMsg, 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, hccp_warn("[request][ra_hdc_tlv]hdc message process unsuccessful ret(%d) phy_id(%u)",
82 : 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)",
84 : ret, phyId), 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, struct TlvMsg *recvMsg)
93 : {
94 1 : void *tlvData = opTlvCommon.txData.tlvData;
95 1 : int ret = 0;
96 :
97 1 : TLV_SETUP_RX(opTlvCommon.txData.opcode == RA_RS_TLV_REQUEST, tlvData, opTlvCommon);
98 :
99 1 : if (recvMsg->data == NULL || recvMsg->length == 0) {
100 1 : *opTlvCommon.rxData.recvBytes = 0;
101 1 : goto out;
102 : }
103 :
104 0 : CHK_PRT_RETURN(*opTlvCommon.rxData.recvBytes > recvMsg->length,
105 : hccp_err("[request][ra_hdc_tlv]rxData.recvBytes(%u) > recvLen(%u), phyId(%u)", *opTlvCommon.rxData.recvBytes,
106 : recvMsg->length, head->phyId), -EINVAL);
107 :
108 0 : ret = memcpy_s(recvMsg->data, recvMsg->length, opTlvCommon.rxData.recvData, *opTlvCommon.rxData.recvBytes);
109 0 : CHK_PRT_RETURN(ret != 0, hccp_err("[request][ra_hdc_tlv]memcpy_s recvData failed, ret(%d) rxData.recvBytes(%u)"
110 : " recvLen(%u) phyId(%u)", ret, *opTlvCommon.rxData.recvBytes, recvMsg->length, head->phyId), -ESAFEFUNC);
111 :
112 0 : out:
113 1 : recvMsg->type = head->type;
114 1 : recvMsg->length = *opTlvCommon.rxData.recvBytes;
115 1 : return ret;
116 : }
117 :
118 2 : int RaHdcTlvRequest(struct RaTlvHandle *tlvHandle, unsigned int moduleType,
119 : struct TlvMsg *sendMsg, struct TlvMsg *recvMsg)
120 : {
121 2 : unsigned int phyId = tlvHandle->initInfo.phyId;
122 2 : union OpTlvRequestDataCommon opTlvCommon = {0};
123 2 : struct TlvRequestMsgHead head = { 0 };
124 : unsigned int interfaceVersion;
125 : void *tlvData;
126 2 : int ret = 0;
127 :
128 2 : RaHdcGetInterfaceVersion(phyId, RA_RS_TLV_REQUEST_V2, &interfaceVersion);
129 2 : tlvData = calloc(1, (interfaceVersion == 0) ? sizeof(union OpTlvRequestData) : sizeof(union OpTlvRequestDataV2));
130 2 : CHK_PRT_RETURN(tlvData == NULL, hccp_err("[ra_hdc_tlv_request]calloc op tlv request data failed! phyId(%u)", phyId), -ENOMEM);
131 2 : TLV_SETUP_TX(interfaceVersion == 0, tlvData, opTlvCommon);
132 2 : RaHdcTlvRequestHeadInit(tlvHandle, moduleType, sendMsg, &head);
133 2 : if (sendMsg->length == 0) {
134 1 : ret = RaHdTlvRequestForSendNullMsg(phyId, opTlvCommon, &head, recvMsg);
135 1 : goto out;
136 : }
137 :
138 12 : while (head.offset < sendMsg->length) {
139 11 : head.sendBytes = (head.totalBytes - head.offset) >= opTlvCommon.txData.dataLength ?
140 11 : opTlvCommon.txData.dataLength : (head.totalBytes - head.offset);
141 11 : (void)memcpy_s(opTlvCommon.txData.head, sizeof(struct TlvRequestMsgHead),
142 : &head, sizeof(struct TlvRequestMsgHead));
143 :
144 11 : (void)memset_s(opTlvCommon.txData.data, opTlvCommon.txData.dataLength, 0, opTlvCommon.txData.dataLength);
145 11 : ret = memcpy_s(opTlvCommon.txData.data, opTlvCommon.txData.dataLength, (sendMsg->data + head.offset), head.sendBytes);
146 11 : if (ret != 0) {
147 0 : hccp_err("[request][ra_hdc_tlv]memcpy_s data failed ret(%d) phy_id(%u) send_bytes(%u)", ret, phyId, head.sendBytes);
148 0 : ret = -ESAFEFUNC;
149 0 : goto out;
150 : }
151 :
152 11 : ret = RaHdcProcessMsg(opTlvCommon.txData.opcode, phyId, opTlvCommon.txData.tlvData, opTlvCommon.txData.size);
153 11 : if (ret == -EUSERS || ret == -ENOTSUPP) {
154 0 : hccp_warn("[request][ra_hdc_tlv]hdc message process unsuccessful ret(%d) phy_id(%u)", ret, phyId);
155 0 : ret = -ESAFEFUNC;
156 0 : goto out;
157 11 : } else if (ret != 0) {
158 0 : hccp_err("[request][ra_hdc_tlv]hdc message process failed ret(%d) phy_id(%u)", ret, phyId);
159 0 : goto out;
160 : }
161 11 : head.offset += head.sendBytes;
162 : }
163 :
164 1 : ret = RaTlvRequestGetTlvMsg(&head, opTlvCommon, recvMsg);
165 1 : if (ret != 0) {
166 0 : hccp_err("[request][ra_hdc_tlv]RaTlvRequestGetTlvMsg failed ret(%d) phy_id(%u)", ret, phyId);
167 0 : goto out;
168 : }
169 1 : out:
170 2 : free(tlvData);
171 2 : tlvData = NULL;
172 2 : return ret;
173 : }
|