1 /*
2 * Copyright (c) 2021-2022 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 "dev_attribute_serialize.h"
17 #include "hdf_log.h"
18 #include "osal_mem.h"
19
20 #define HDF_LOG_TAG dev_attr_serialze
21
22 #define ATTRIBUTE_PRIVATE_DATA_LENGTH_NULL 0
23 #define ATTRIBUTE_PRIVATE_DATA_LENGTH_NORMAL 1
24
DeviceAttributeSerialize(const struct HdfDeviceInfo * attribute,struct HdfSBuf * sbuf)25 bool DeviceAttributeSerialize(const struct HdfDeviceInfo *attribute, struct HdfSBuf *sbuf)
26 {
27 if (attribute == NULL || sbuf == NULL) {
28 return false;
29 }
30
31 if (!HdfSbufWriteUint32(sbuf, attribute->deviceId) ||
32 !HdfSbufWriteUint16(sbuf, attribute->policy) ||
33 !HdfSbufWriteString(sbuf, attribute->svcName) ||
34 !HdfSbufWriteString(sbuf, attribute->moduleName) ||
35 !HdfSbufWriteString(sbuf, attribute->deviceName)) {
36 return false;
37 }
38
39 if (attribute->deviceMatchAttr != NULL) {
40 if (!HdfSbufWriteUint32(sbuf, ATTRIBUTE_PRIVATE_DATA_LENGTH_NORMAL) ||
41 !HdfSbufWriteString(sbuf, attribute->deviceMatchAttr)) {
42 HDF_LOGE("failed to serialize device attribute");
43 return false;
44 }
45 } else {
46 if (!HdfSbufWriteUint32(sbuf, ATTRIBUTE_PRIVATE_DATA_LENGTH_NULL)) {
47 HDF_LOGE("failed to serialize device attribute");
48 return false;
49 }
50 }
51
52 return true;
53 }
54
DeviceAttributeSet(struct HdfDeviceInfo * attribute,struct HdfSBuf * sbuf)55 static bool DeviceAttributeSet(struct HdfDeviceInfo *attribute, struct HdfSBuf *sbuf)
56 {
57 const char *svcName = HdfSbufReadString(sbuf);
58 if (svcName == NULL) {
59 HDF_LOGE("Read from sbuf failed, svcName is null");
60 return false;
61 }
62 attribute->svcName = strdup(svcName);
63 if (attribute->svcName == NULL) {
64 HDF_LOGE("Read from sbuf failed, strdup svcName fail");
65 return false;
66 }
67
68 const char *moduleName = HdfSbufReadString(sbuf);
69 if (moduleName == NULL) {
70 HDF_LOGE("Read from parcel failed, moduleName is null");
71 return false;
72 }
73 attribute->moduleName = strdup(moduleName);
74 if (attribute->moduleName == NULL) {
75 HDF_LOGE("Read from sbuf failed, strdup moduleName fail");
76 return false;
77 }
78
79 const char *deviceName = HdfSbufReadString(sbuf);
80 if (deviceName == NULL) {
81 HDF_LOGE("Read from sbuf failed, deviceName is null");
82 return false;
83 }
84 attribute->deviceName = strdup(deviceName);
85 if (attribute->deviceName == NULL) {
86 HDF_LOGE("Read from sbuf failed, strdup deviceName fail");
87 return false;
88 }
89
90 uint32_t length;
91 if (!HdfSbufReadUint32(sbuf, &length)) {
92 HDF_LOGE("Device attribute readDeviceMatchAttr length failed");
93 return false;
94 }
95 if (length == ATTRIBUTE_PRIVATE_DATA_LENGTH_NORMAL) {
96 const char *deviceMatchAttr = HdfSbufReadString(sbuf);
97 if (deviceMatchAttr == NULL) {
98 HDF_LOGE("%s: Read from sbuf failed, deviceMatchAttr is null", __func__);
99 return false;
100 }
101 attribute->deviceMatchAttr = strdup(deviceMatchAttr);
102 if (attribute->deviceMatchAttr == NULL) {
103 HDF_LOGE("Read from sbuf failed, strdup deviceMatchAttr fail");
104 return false;
105 }
106 }
107
108 return true;
109 }
110
DeviceAttributeDeserialize(struct HdfSBuf * sbuf)111 struct HdfDeviceInfo *DeviceAttributeDeserialize(struct HdfSBuf *sbuf)
112 {
113 if (sbuf == NULL) {
114 return NULL;
115 }
116
117 struct HdfDeviceInfo *attribute = HdfDeviceInfoNewInstance();
118 if (attribute == NULL) {
119 HDF_LOGE("OsalMemCalloc failed, attribute is null");
120 return NULL;
121 }
122
123 if (attribute->deviceMatchAttr == NULL) {
124 HDF_LOGW("OsalMemCalloc failed, attribute->deviceMatchAttr is null");
125 }
126
127 if (!HdfSbufReadUint32(sbuf, &attribute->deviceId) || !HdfSbufReadUint16(sbuf, &attribute->policy)) {
128 HDF_LOGE("invalid deviceId or policy");
129 DeviceSerializedAttributeRelease(attribute);
130 return NULL;
131 }
132
133 if (DeviceAttributeSet(attribute, sbuf)) {
134 return attribute;
135 }
136
137 DeviceSerializedAttributeRelease(attribute);
138 return NULL;
139 }
140
DeviceSerializedAttributeRelease(struct HdfDeviceInfo * attribute)141 void DeviceSerializedAttributeRelease(struct HdfDeviceInfo *attribute)
142 {
143 if (attribute == NULL) {
144 return;
145 }
146
147 if (attribute->moduleName != NULL) {
148 OsalMemFree((void *)attribute->moduleName);
149 }
150 if (attribute->svcName != NULL) {
151 OsalMemFree((void *)attribute->svcName);
152 }
153 if (attribute->deviceName != NULL) {
154 OsalMemFree((void *)attribute->deviceName);
155 }
156 if (attribute->deviceMatchAttr != NULL) {
157 OsalMemFree((void *)attribute->deviceMatchAttr);
158 }
159 OsalMemFree(attribute);
160 }
161