1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "hdf_log.h"
10 #include "hdf_sbuf.h"
11 #include "hdf_service_status.h"
12 
ServiceStatusMarshalling(struct ServiceStatus * status,struct HdfSBuf * buf)13 int ServiceStatusMarshalling(struct ServiceStatus *status, struct HdfSBuf *buf)
14 {
15     if (status == NULL || buf == NULL || status->serviceName == NULL) {
16         return HDF_ERR_INVALID_PARAM;
17     }
18     if (!HdfSbufWriteString(buf, status->serviceName)
19         || !HdfSbufWriteUint16(buf, status->deviceClass)
20         || !HdfSbufWriteUint16(buf, status->status)
21         || !HdfSbufWriteString(buf, status->info != NULL ? status->info : "")) {
22         HDF_LOGI("failed to marshalling service status");
23         return HDF_FAILURE;
24     }
25 
26     return HDF_SUCCESS;
27 }
28 
ServiceStatusUnMarshalling(struct ServiceStatus * status,struct HdfSBuf * buf)29 int ServiceStatusUnMarshalling(struct ServiceStatus *status, struct HdfSBuf *buf)
30 {
31     if (status == NULL || buf == NULL) {
32         return HDF_ERR_INVALID_PARAM;
33     }
34     status->serviceName = HdfSbufReadString(buf);
35     if (status->serviceName == NULL) {
36         HDF_LOGI("failed to unmarshalling service status, service name is null");
37         return HDF_FAILURE;
38     }
39 
40     if (!HdfSbufReadUint16(buf, &status->deviceClass) || !HdfSbufReadUint16(buf, &status->status)) {
41         HDF_LOGI("failed to unmarshalling service status, deviceClass or status invalid");
42         return HDF_FAILURE;
43     }
44 
45     status->info = HdfSbufReadString(buf);
46     return HDF_SUCCESS;
47 }
48