xref: /developtools/hdc/src/common/debug.cpp (revision cc290419)
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 "debug.h"
16#include "base.h"
17
18namespace Hdc {
19namespace Debug {
20    int WriteHexToDebugFile(const char *fileName, const uint8_t *buf, const int bufLen)
21    {
22        char pathName[BUF_SIZE_DEFAULT];
23        if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "/mnt/hgfs/vtmp/%s", fileName) < 0) {
24            return ERR_BUF_OVERFLOW;
25        }
26        string srcPath = pathName;
27        string resolvedPath = Base::CanonicalizeSpecPath(srcPath);
28        FILE *fp = fopen(resolvedPath.c_str(), "a+");
29        if (fp == nullptr) {
30            if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "/tmp/%s", fileName) < 0) {
31                WRITE_LOG(LOG_DEBUG, "Write hex to %s failed!", pathName);
32                return ERR_FILE_OPEN;
33            }
34
35            srcPath = pathName;
36            resolvedPath = Base::CanonicalizeSpecPath(srcPath);
37            if ((fp = fopen(resolvedPath.c_str(), "a+")) == nullptr) {
38                WRITE_LOG(LOG_DEBUG, "Write hex to %s failed!", pathName);
39                return ERR_FILE_OPEN;
40            }
41        }
42        fwrite(buf, 1, bufLen, fp);
43        fflush(fp);
44        fclose(fp);
45        return RET_SUCCESS;
46    }
47
48    int ReadHexFromDebugFile(const char *fileName, uint8_t *buf, const int bufLen)
49    {
50        char pathName[BUF_SIZE_DEFAULT];
51        if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "/mnt/hgfs/vtmp/%s", fileName) < 0) {
52            return ERR_BUF_OVERFLOW;
53        }
54        FILE *fp = fopen(pathName, "r");
55        if (fp == nullptr) {
56            if (snprintf_s(pathName, sizeof(pathName), sizeof(pathName) - 1, "/tmp/%s", fileName) < 0 ||
57                (fp = fopen(pathName, "r")) == nullptr) {
58                if (fp != nullptr) {
59                    fclose(fp);
60                }
61                WRITE_LOG(LOG_DEBUG, "Write hex to %s failed!", pathName);
62                return ERR_FILE_WRITE;
63            }
64        }
65        struct stat statbuf;
66        stat(pathName, &statbuf);
67        int size = statbuf.st_size;
68        if (size > bufLen) {
69            fclose(fp);
70            return ERR_BUF_SIZE;
71        }
72        int ret = fread(buf, 1, size, fp);
73        fflush(fp);
74        fclose(fp);
75        if (ret != size) {
76            return ERR_FILE_READ;
77        }
78        return size;
79    }
80
81    void DetermineThread(HSession hSession)
82    {
83        if (uv_thread_self() == hSession->hWorkThread) {
84            WRITE_LOG(LOG_WARN, "At main workthread");
85        } else if (uv_thread_self() == hSession->hWorkChildThread) {
86            WRITE_LOG(LOG_WARN, "At child workthread");
87        } else {
88            WRITE_LOG(LOG_WARN, "At unknown workthread");
89        }
90    }
91
92    int PrintfHexBuf(const uint8_t *buf, int bufLen)
93    {
94        int i = 0;
95        for (i = 0; i < bufLen; ++i) {
96            printf("0x%02x, ", buf[i]);
97            fflush(stdout);
98        }
99        printf("\r\n");
100        fflush(stdout);
101        return 0;
102    }
103}
104}  // namespace Hdc