• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 
16 #ifndef SHM_UTILS_H
17 #define SHM_UTILS_H
18 
19 #include <cerrno>
20 #include <cstdarg>
21 #include <cstdint>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <pthread.h>
25 #include <securec.h>
26 #include <sys/shm.h>
27 #include <sys/time.h>
28 #include <unistd.h>
29 
30 #define MAX_WAIT_TIMEOUT 10
31 #define SHM_SEND_KEY 123456
32 #define SHM_RECV_KEY 123466
33 
34 struct shared_use_st {
35     int written;     // 作为一个标志,非0:表示可读,0表示可写
36     char data[1024]; // 记录写入和读取的文本
37 };
38 
39 #define LOG(format, ...)                                                        \
40     do {                                                                        \
41         time_t timeSec;                                                         \
42         time(&timeSec);                                                         \
43         struct tm tmRst;                                                        \
44         localtime_r(&timeSec, &tmRst);                                          \
45         char strTime[10];                                                       \
46         strftime(strTime, sizeof(strTime), "%H:%M:%S", &tmRst);                 \
47         fprintf(stdout, "[shm-utils] %s " format "\n", strTime, ##__VA_ARGS__); \
48     } while (0)
49 
50 int createShm(int key);
51 int disconnectShm(void);
52 int writeCodeDataToShm(int code, char* buf);
53 int waitDataWithCode(char* code, char* data);
54 int writeDataToShm(char* buf);
55 int deleteShm(void);
56 char* Int2String(int num, char* str);
57 void initShm(void);
58 int readDataFromShm(char* buf);
59 int readDataFromShmNoClear(char* buf);
60 #endif