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