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 #include "task.h"
16
17 namespace Hdc {
18 // -----------------------------------------------------------
19 // notice!!! The constructor is called at the Child thread, so in addition to initialization, do not excess actions, if
20 // destructor is required, please clean up in the subclasses.
HdcTaskBase(HTaskInfo hTaskInfo)21 HdcTaskBase::HdcTaskBase(HTaskInfo hTaskInfo)
22 {
23 taskInfo = hTaskInfo;
24 loopTask = hTaskInfo->runLoop;
25 clsSession = hTaskInfo->ownerSessionClass;
26 childReady = false;
27 singalStop = false;
28 refCount = 0;
29 if (taskInfo->masterSlave) {
30 SendToAnother(CMD_KERNEL_WAKEUP_SLAVETASK, nullptr, 0);
31 }
32 WRITE_LOG(LOG_DEBUG, "HdcTaskBase channelId:%u", taskInfo->channelId);
33 }
34
~HdcTaskBase()35 HdcTaskBase::~HdcTaskBase()
36 {
37 WRITE_LOG(LOG_DEBUG, "~HdcTaskBase channelId:%u", taskInfo->channelId);
38 }
39
ReadyForRelease()40 bool HdcTaskBase::ReadyForRelease()
41 {
42 return refCount == 0;
43 }
44
45 // Only the Task work thread call is allowed to use only when Workfortask returns FALSE.
TaskFinish()46 void HdcTaskBase::TaskFinish()
47 {
48 StartTraceScope("HdcTaskBase::TaskFinish");
49 uint8_t count = 1;
50 WRITE_LOG(LOG_DEBUG, "HdcTaskBase::TaskFinish notify begin channelId:%u", taskInfo->channelId);
51 SendToAnother(CMD_KERNEL_CHANNEL_CLOSE, &count, 1);
52 WRITE_LOG(LOG_DEBUG, "HdcTaskBase::TaskFinish notify end channelId:%u", taskInfo->channelId);
53 }
54
SendToAnother(const uint16_t command,uint8_t * bufPtr,const int size)55 bool HdcTaskBase::SendToAnother(const uint16_t command, uint8_t *bufPtr, const int size)
56 {
57 StartTraceScope("HdcTaskBase::SendToAnother");
58 if (singalStop) {
59 WRITE_LOG(LOG_FATAL, "TaskFinish singalStop channelId:%u command:%u", taskInfo->channelId, command);
60 return false;
61 }
62 if (taskInfo->channelTask) {
63 HdcChannelBase *channelBase = reinterpret_cast<HdcChannelBase *>(taskInfo->channelClass);
64 channelBase->SendWithCmd(taskInfo->channelId, command, bufPtr, size);
65 return true;
66 } else {
67 HdcSessionBase *sessionBase = reinterpret_cast<HdcSessionBase *>(taskInfo->ownerSessionClass);
68 return sessionBase->Send(taskInfo->sessionId, taskInfo->channelId, command, bufPtr, size) > 0;
69 }
70 }
71
LogMsg(MessageLevel level,const char * msg,...)72 void HdcTaskBase::LogMsg(MessageLevel level, const char *msg, ...)
73 {
74 va_list vaArgs;
75 va_start(vaArgs, msg);
76 string log = Base::StringFormat(msg, vaArgs);
77 va_end(vaArgs);
78
79 if (taskInfo->channelTask) {
80 string logInfo = "";
81 switch (level) {
82 case MSG_FAIL:
83 logInfo = MESSAGE_FAIL;
84 break;
85 case MSG_INFO:
86 logInfo = MESSAGE_INFO;
87 break;
88 default: // successful, not append extra info
89 break;
90 }
91
92 fprintf(stdout, "%s%s\n", logInfo.c_str(), log.c_str());
93 fflush(stdout);
94 } else {
95 HdcSessionBase *sessionBase = reinterpret_cast<HdcSessionBase *>(clsSession);
96 sessionBase->LogMsg(taskInfo->sessionId, taskInfo->channelId, level, log.c_str());
97 }
98 }
99
ServerCommand(const uint16_t command,uint8_t * bufPtr,const int size)100 bool HdcTaskBase::ServerCommand(const uint16_t command, uint8_t *bufPtr, const int size)
101 {
102 HdcSessionBase *hSession = (HdcSessionBase *)taskInfo->ownerSessionClass;
103 return hSession->ServerCommand(taskInfo->sessionId, taskInfo->channelId, command, bufPtr, size);
104 }
105
106 // cross thread
ThreadCtrlCommunicate(const uint8_t * bufPtr,const int size)107 int HdcTaskBase::ThreadCtrlCommunicate(const uint8_t *bufPtr, const int size)
108 {
109 HdcSessionBase *sessionBase = (HdcSessionBase *)taskInfo->ownerSessionClass;
110 HSession hSession = sessionBase->AdminSession(OP_QUERY, taskInfo->sessionId, nullptr);
111 if (!hSession) {
112 WRITE_LOG(LOG_FATAL, "ThreadCtrlCommunicate hSession nullptr sessionId:%u", taskInfo->sessionId);
113 return -1;
114 }
115 int fd = hSession->ctrlFd[STREAM_WORK];
116 return Base::SendToPollFd(fd, bufPtr, size);
117 }
118 }
119