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