• 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 #ifndef HDC_FILE_DESCRIPTOR_H
16 #define HDC_FILE_DESCRIPTOR_H
17 #include "common.h"
18 
19 namespace Hdc {
20 class HdcFileDescriptor {
21 public:
22     // callerContext, normalFinish, errorString
23     using CmdResultCallback = std::function<bool(const void *, const bool, const string)>;
24     // callerContext, readBuf, readIOByes
25     using CallBackWhenRead = std::function<bool(const void *, uint8_t *, const int)>;
26     HdcFileDescriptor(uv_loop_t *loopIn, int fdToRead, void *callerContextIn, CallBackWhenRead callbackReadIn,
27                       CmdResultCallback callbackFinishIn);
28     virtual ~HdcFileDescriptor();
29     int Write(uint8_t *data, int size);
30     int WriteWithMem(uint8_t *data, int size);
31 
32     bool ReadyForRelease();
33     bool StartWork();
34     void StopWork(bool tryCloseFdIo, std::function<void()> closeFdCallback);
35 
36 protected:
37 private:
38     struct CtxFileIO {
39         uv_fs_t fs;
40         uint8_t *bufIO;
41         HdcFileDescriptor *thisClass;
42     };
43     static void OnFileIO(uv_fs_t *req);
44     int LoopRead();
45 
46     std::function<void()> callbackCloseFd;
47     CmdResultCallback callbackFinish;
48     CallBackWhenRead callbackRead;
49     uv_loop_t *loop;
50     uv_fs_t reqClose;
51     void *callerContext;
52     bool workContinue;
53     int fdIO;
54     int refIO;
55 };
56 }  // namespace Hdc
57 
58 #endif  // HDC_FILE_DESCRIPTOR_H