• 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 #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         return false;
60     }
61     if (taskInfo->channelTask) {
62         HdcChannelBase *channelBase = reinterpret_cast<HdcChannelBase *>(taskInfo->channelClass);
63         channelBase->SendWithCmd(taskInfo->channelId, command, bufPtr, size);
64         return true;
65     } else {
66         HdcSessionBase *sessionBase = reinterpret_cast<HdcSessionBase *>(taskInfo->ownerSessionClass);
67         return sessionBase->Send(taskInfo->sessionId, taskInfo->channelId, command, bufPtr, size) > 0;
68     }
69 }
70 
LogMsg(MessageLevel level,const char * msg,...)71 void HdcTaskBase::LogMsg(MessageLevel level, const char *msg, ...)
72 {
73     va_list vaArgs;
74     va_start(vaArgs, msg);
75     string log = Base::StringFormat(msg, vaArgs);
76     va_end(vaArgs);
77 
78     if (taskInfo->channelTask) {
79         string logInfo = "";
80         switch (level) {
81             case MSG_FAIL:
82                 logInfo = MESSAGE_FAIL;
83                 break;
84             case MSG_INFO:
85                 logInfo = MESSAGE_INFO;
86                 break;
87             default:  // successful, not append extra info
88                 break;
89         }
90 
91         fprintf(stdout, "%s%s\n", logInfo.c_str(), log.c_str());
92         fflush(stdout);
93     } else {
94         HdcSessionBase *sessionBase = reinterpret_cast<HdcSessionBase *>(clsSession);
95         sessionBase->LogMsg(taskInfo->sessionId, taskInfo->channelId, level, log.c_str());
96     }
97 }
98 
ServerCommand(const uint16_t command,uint8_t * bufPtr,const int size)99 bool HdcTaskBase::ServerCommand(const uint16_t command, uint8_t *bufPtr, const int size)
100 {
101     HdcSessionBase *hSession = (HdcSessionBase *)taskInfo->ownerSessionClass;
102     return hSession->ServerCommand(taskInfo->sessionId, taskInfo->channelId, command, bufPtr, size);
103 }
104 
105 // cross thread
ThreadCtrlCommunicate(const uint8_t * bufPtr,const int size)106 int HdcTaskBase::ThreadCtrlCommunicate(const uint8_t *bufPtr, const int size)
107 {
108     HdcSessionBase *sessionBase = (HdcSessionBase *)taskInfo->ownerSessionClass;
109     HSession hSession = sessionBase->AdminSession(OP_QUERY, taskInfo->sessionId, nullptr);
110     if (!hSession) {
111         return -1;
112     }
113     int fd;
114     if (uv_thread_self() == hSession->hWorkThread) {
115         fd = hSession->ctrlFd[STREAM_MAIN];
116     } else if (uv_thread_self() == hSession->hWorkChildThread) {
117         fd = hSession->ctrlFd[STREAM_WORK];
118     } else {
119         return ERR_GENERIC;
120     }
121     return Base::SendToPollFd(fd, bufPtr, size);
122 }
123 }
124