• 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 "usbd_publisher.h"
17 #include "utils/hdf_log.h"
18 
19 #define HDF_LOG_TAG usbd_publisher
20 
NotifySubscriberDevice(const struct UsbdSubscriber * subscriber,enum UsbdDeviceAction act,int32_t busNum,int32_t devNum)21 void NotifySubscriberDevice(const struct UsbdSubscriber *subscriber, enum UsbdDeviceAction act, int32_t busNum,
22     int32_t devNum)
23 {
24     if (subscriber == NULL) {
25         HDF_LOGE("%{public}s: subscriber is NULL", __func__);
26         return;
27     }
28     struct HdfRemoteService *service = subscriber->remoteService;
29 
30     if (HdfRemoteServiceSetInterfaceDesc(service, "hdf.usb.usbdsubscriber") == false) {
31         HDF_LOGE("%{public}s:%{public}d set desc fail\n", __func__, __LINE__);
32         return;
33     }
34 
35     struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
36     struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC);
37     if (data == NULL || reply == NULL) {
38         HDF_LOGE("%{public}s failed to obtain hdf sbuf", __func__);
39         HdfSbufRecycle(data);
40         HdfSbufRecycle(reply);
41         return;
42     }
43 
44     if (HdfRemoteServiceWriteInterfaceToken(service, data) == false) {
45         HDF_LOGE("%{public}s:%{public}d write interface token fail\n", __func__, __LINE__);
46         HdfSbufRecycle(data);
47         HdfSbufRecycle(reply);
48         return;
49     }
50 
51     HdfSbufWriteInt32(data, act);
52     HdfSbufWriteInt32(data, busNum);
53     HdfSbufWriteInt32(data, devNum);
54     int32_t ret = service->dispatcher->Dispatch(service, CMD_NOTIFY_SUBSCRIBER_DEVICE_EVENT, data, reply);
55     if (ret != HDF_SUCCESS) {
56         HDF_LOGE("%{public}s failed to notify subscriber, ret: %{public}d", __func__, ret);
57     }
58     HdfSbufRecycle(data);
59     HdfSbufRecycle(reply);
60 }
61 
NotifyUsbPortSubscriber(const struct UsbdSubscriber * subscriber,int32_t portId,int32_t powerRole,int32_t dataRole,int32_t mode)62 void NotifyUsbPortSubscriber(const struct UsbdSubscriber *subscriber, int32_t portId, int32_t powerRole,
63     int32_t dataRole, int32_t mode)
64 {
65     if (subscriber == NULL) {
66         HDF_LOGE("%{public}s: subscriber is NULL", __func__);
67         return;
68     }
69     int32_t ret;
70     struct HdfRemoteService *service = subscriber->remoteService;
71 
72     if (HdfRemoteServiceSetInterfaceDesc(service, "hdf.usb.usbdsubscriber") == false) {
73         HDF_LOGE("%{public}s:%{public}d set desc fail\n", __func__, __LINE__);
74         return;
75     }
76 
77     struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
78     struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC);
79     if (data == NULL || reply == NULL) {
80         HDF_LOGE("%{public}s failed to obtain hdf sbuf", __func__);
81         HdfSbufRecycle(data);
82         HdfSbufRecycle(reply);
83         return;
84     }
85 
86     if (HdfRemoteServiceWriteInterfaceToken(service, data) == false) {
87         HDF_LOGE("%{public}s:%{public}d write interface token fail\n", __func__, __LINE__);
88         HdfSbufRecycle(data);
89         HdfSbufRecycle(reply);
90         return;
91     }
92 
93     HdfSbufWriteInt32(data, portId);
94     HdfSbufWriteInt32(data, powerRole);
95     HdfSbufWriteInt32(data, dataRole);
96     HdfSbufWriteInt32(data, mode);
97 
98     ret = service->dispatcher->Dispatch(service, CMD_NOTIFY_PORT_CHANGED, data, reply);
99     if (ret != HDF_SUCCESS) {
100         HDF_LOGE("%{public}s failed to notify subscriber, ret: %{public}d", __func__, ret);
101     } else {
102         HDF_LOGI("%{public}s: succeed to notify subscriber", __func__);
103     }
104     HdfSbufRecycle(data);
105     HdfSbufRecycle(reply);
106 }
107