• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
103 
104     return true;
105 }
106 
DeviceAttributeDeserialize(struct HdfSBuf * sbuf)107 struct HdfDeviceInfo *DeviceAttributeDeserialize(struct HdfSBuf *sbuf)
108 {
109     if (sbuf == NULL) {
110         return NULL;
111     }
112 
113     struct HdfDeviceInfo *attribute = HdfDeviceInfoNewInstance();
114     if (attribute == NULL) {
115         HDF_LOGE("OsalMemCalloc failed, attribute is null");
116         return NULL;
117     }
118 
119     if (!HdfSbufReadUint32(sbuf, &attribute->deviceId) || !HdfSbufReadUint16(sbuf, &attribute->policy)) {
120         HDF_LOGE("invalid deviceId or policy");
121         DeviceSerializedAttributeRelease(attribute);
122         return NULL;
123     }
124 
125     if (DeviceAttributeSet(attribute, sbuf)) {
126         return attribute;
127     }
128 
129     DeviceSerializedAttributeRelease(attribute);
130     return NULL;
131 }
132 
DeviceSerializedAttributeRelease(struct HdfDeviceInfo * attribute)133 void DeviceSerializedAttributeRelease(struct HdfDeviceInfo *attribute)
134 {
135     if (attribute == NULL) {
136         return;
137     }
138 
139     if (attribute->moduleName != NULL) {
140         OsalMemFree((void *)attribute->moduleName);
141     }
142     if (attribute->svcName != NULL) {
143         OsalMemFree((void *)attribute->svcName);
144     }
145     if (attribute->deviceName != NULL) {
146         OsalMemFree((void *)attribute->deviceName);
147     }
148     if (attribute->deviceMatchAttr != NULL) {
149         OsalMemFree((void *)attribute->deviceMatchAttr);
150     }
151     OsalMemFree(attribute);
152 }
153