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 uint8_t count = 1;
49 SendToAnother(CMD_KERNEL_CHANNEL_CLOSE, &count, 1);
50 WRITE_LOG(LOG_DEBUG, "HdcTaskBase::TaskFinish notify");
51 }
52
SendToAnother(const uint16_t command,uint8_t * bufPtr,const int size)53 bool HdcTaskBase::SendToAnother(const uint16_t command, uint8_t *bufPtr, const int size)
54 {
55 if (singalStop) {
56 return false;
57 }
58 HdcSessionBase *sessionBase = reinterpret_cast<HdcSessionBase *>(taskInfo->ownerSessionClass);
59 return sessionBase->Send(taskInfo->sessionId, taskInfo->channelId, command, bufPtr, size) > 0;
60 }
61
LogMsg(MessageLevel level,const char * msg,...)62 void HdcTaskBase::LogMsg(MessageLevel level, const char *msg, ...)
63 {
64 va_list vaArgs;
65 va_start(vaArgs, msg);
66 string log = Base::StringFormat(msg, vaArgs);
67 va_end(vaArgs);
68 HdcSessionBase *sessionBase = reinterpret_cast<HdcSessionBase *>(clsSession);
69 sessionBase->LogMsg(taskInfo->sessionId, taskInfo->channelId, level, log.c_str());
70 }
71
ServerCommand(const uint16_t command,uint8_t * bufPtr,const int size)72 bool HdcTaskBase::ServerCommand(const uint16_t command, uint8_t *bufPtr, const int size)
73 {
74 HdcSessionBase *hSession = (HdcSessionBase *)taskInfo->ownerSessionClass;
75 return hSession->ServerCommand(taskInfo->sessionId, taskInfo->channelId, command, bufPtr, size);
76 }
77
78 // cross thread
ThreadCtrlCommunicate(const uint8_t * bufPtr,const int size)79 int HdcTaskBase::ThreadCtrlCommunicate(const uint8_t *bufPtr, const int size)
80 {
81 HdcSessionBase *sessionBase = (HdcSessionBase *)taskInfo->ownerSessionClass;
82 HSession hSession = sessionBase->AdminSession(OP_QUERY, taskInfo->sessionId, nullptr);
83 if (!hSession) {
84 return -1;
85 }
86 uv_stream_t *handleStream = nullptr;
87 if (uv_thread_self() == hSession->hWorkThread) {
88 handleStream = (uv_stream_t *)&hSession->ctrlPipe[STREAM_MAIN];
89 } else if (uv_thread_self() == hSession->hWorkChildThread) {
90 handleStream = (uv_stream_t *)&hSession->ctrlPipe[STREAM_WORK];
91 } else {
92 return ERR_GENERIC;
93 }
94 return Base::SendToStream(handleStream, bufPtr, size);
95 }
96 }
97