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 "hdf_device_thread.h"
17 #include "hdf_log.h"
18 #include "osal_mem.h"
19 #include "osal_message.h"
20
21 #define HDF_LOG_TAG hdf_device_thread
22
DeviceThreadMessageHandler(struct HdfMessageTask * task,struct HdfMessage * msg)23 int32_t DeviceThreadMessageHandler(struct HdfMessageTask *task, struct HdfMessage *msg)
24 {
25 (void)task;
26 struct HdfDevice *device = (struct HdfDevice *)msg->data[0];
27 switch (msg->messageId) {
28 case DEVICE_SERVICE_MESSAGE_LAUNCH: {
29 (void)device;
30 struct HdfDeviceNode *devService = (struct HdfDeviceNode *)msg->data[1];
31 if (devService != NULL && devService->super.LaunchNode != NULL) {
32 devService->super.LaunchNode(devService);
33 }
34 break;
35 }
36 case DEVICE_SERVICE_MESSAGE_SUSPEND: {
37 break;
38 }
39 case DEVICE_SERVICE_MESSAGE_RESUME: {
40 break;
41 }
42 default: {
43 HDF_LOGE("Unsupported message, message id is %{public}u", msg->messageId);
44 break;
45 }
46 }
47 return HDF_SUCCESS;
48 }
49
DeviceThreadAttach(struct DeviceThread * inst,struct IHdfDevice * device,struct HdfDeviceNode * service)50 int DeviceThreadAttach(struct DeviceThread *inst, struct IHdfDevice *device, struct HdfDeviceNode *service)
51 {
52 if (inst == NULL || device == NULL || service == NULL) {
53 return HDF_ERR_INVALID_PARAM;
54 }
55
56 // Thread is already running
57 struct HdfMessageTask *task = &inst->task;
58 struct HdfMessage *message = HdfMessageObtain(sizeof(struct HdfDeviceNode *));
59 if (message == NULL) {
60 HDF_LOGE("DeviceThreadAttach:obtain message error");
61 return HDF_ERR_MALLOC_FAIL;
62 }
63 message->messageId = DEVICE_SERVICE_MESSAGE_LAUNCH;
64 message->data[0] = (void *)device;
65 message->data[1] = (void *)service;
66 return task->SendMessage(task, message, true);
67 }
68
DeviceThreadMain(void * args)69 void DeviceThreadMain(void *args)
70 {
71 struct DeviceThread *currentThread = (struct DeviceThread *)args;
72 if (currentThread != NULL) {
73 struct HdfMessageLooper *looper = ¤tThread->looper;
74 if (looper->Start != NULL) {
75 looper->Start(looper);
76 }
77 }
78 }
79
DeviceThreadConstruct(struct DeviceThread * inst)80 void DeviceThreadConstruct(struct DeviceThread *inst)
81 {
82 static struct IHdfMessageHandler handler = {
83 .Dispatch = DeviceThreadMessageHandler
84 };
85
86 HdfMessageLooperConstruct(&inst->looper);
87 HdfMessageTaskConstruct(&inst->task, &inst->looper, &handler);
88 inst->super.ThreadEntry = DeviceThreadMain;
89 HdfThreadConstruct(&inst->super);
90 }
91
DeviceThreadNewInstance(void)92 struct DeviceThread *DeviceThreadNewInstance(void)
93 {
94 struct DeviceThread *thread =
95 (struct DeviceThread *)OsalMemCalloc(sizeof(struct DeviceThread));
96 if (thread != NULL) {
97 DeviceThreadConstruct(thread);
98 }
99 return thread;
100 }
101
DeviceThreadFreeInstance(struct DeviceThread * thread)102 void DeviceThreadFreeInstance(struct DeviceThread *thread)
103 {
104 if (thread != NULL) {
105 HdfThreadDestruct(&thread->super);
106 OsalMemFree(thread);
107 }
108 }
109
110