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