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