10704ebd2Sopenharmony_ci/*
20704ebd2Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
30704ebd2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40704ebd2Sopenharmony_ci * you may not use this file except in compliance with the License.
50704ebd2Sopenharmony_ci * You may obtain a copy of the License at
60704ebd2Sopenharmony_ci *
70704ebd2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
80704ebd2Sopenharmony_ci *
90704ebd2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100704ebd2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110704ebd2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120704ebd2Sopenharmony_ci * See the License for the specific language governing permissions and
130704ebd2Sopenharmony_ci * limitations under the License.
140704ebd2Sopenharmony_ci */
150704ebd2Sopenharmony_ci
160704ebd2Sopenharmony_ci#ifndef SHM_UTILS_H
170704ebd2Sopenharmony_ci#define SHM_UTILS_H
180704ebd2Sopenharmony_ci
190704ebd2Sopenharmony_ci#include <cerrno>
200704ebd2Sopenharmony_ci#include <cstdarg>
210704ebd2Sopenharmony_ci#include <cstdint>
220704ebd2Sopenharmony_ci#include <cstdio>
230704ebd2Sopenharmony_ci#include <cstdlib>
240704ebd2Sopenharmony_ci#include <pthread.h>
250704ebd2Sopenharmony_ci#include <securec.h>
260704ebd2Sopenharmony_ci#include <sys/shm.h>
270704ebd2Sopenharmony_ci#include <sys/time.h>
280704ebd2Sopenharmony_ci#include <unistd.h>
290704ebd2Sopenharmony_ci
300704ebd2Sopenharmony_ci#define MAX_WAIT_TIMEOUT 10
310704ebd2Sopenharmony_ci#define SHM_SEND_KEY 123456
320704ebd2Sopenharmony_ci#define SHM_RECV_KEY 123466
330704ebd2Sopenharmony_ci
340704ebd2Sopenharmony_cistruct shared_use_st {
350704ebd2Sopenharmony_ci    int written;     // 作为一个标志,非0:表示可读,0表示可写
360704ebd2Sopenharmony_ci    char data[1024]; // 记录写入和读取的文本
370704ebd2Sopenharmony_ci};
380704ebd2Sopenharmony_ci
390704ebd2Sopenharmony_ci#define LOG(format, ...)                                                        \
400704ebd2Sopenharmony_ci    do {                                                                        \
410704ebd2Sopenharmony_ci        time_t timeSec;                                                         \
420704ebd2Sopenharmony_ci        time(&timeSec);                                                         \
430704ebd2Sopenharmony_ci        struct tm tmRst;                                                        \
440704ebd2Sopenharmony_ci        localtime_r(&timeSec, &tmRst);                                          \
450704ebd2Sopenharmony_ci        char strTime[10];                                                       \
460704ebd2Sopenharmony_ci        strftime(strTime, sizeof(strTime), "%H:%M:%S", &tmRst);                 \
470704ebd2Sopenharmony_ci        fprintf(stdout, "[shm-utils] %s " format "\n", strTime, ##__VA_ARGS__); \
480704ebd2Sopenharmony_ci    } while (0)
490704ebd2Sopenharmony_ci
500704ebd2Sopenharmony_ciint createShm(int key);
510704ebd2Sopenharmony_ciint disconnectShm(void);
520704ebd2Sopenharmony_ciint writeCodeDataToShm(int code, char* buf);
530704ebd2Sopenharmony_ciint waitDataWithCode(char* code, char* data);
540704ebd2Sopenharmony_ciint writeDataToShm(char* buf);
550704ebd2Sopenharmony_ciint deleteShm(void);
560704ebd2Sopenharmony_cichar* Int2String(int num, char* str);
570704ebd2Sopenharmony_civoid initShm(void);
580704ebd2Sopenharmony_ciint readDataFromShm(char* buf);
590704ebd2Sopenharmony_ciint readDataFromShmNoClear(char* buf);
600704ebd2Sopenharmony_ci#endif