1d9f0492fSopenharmony_ci/*
2d9f0492fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License.
5d9f0492fSopenharmony_ci * You may obtain a copy of the License at
6d9f0492fSopenharmony_ci *
7d9f0492fSopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
8d9f0492fSopenharmony_ci *
9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and
13d9f0492fSopenharmony_ci * limitations under the License.
14d9f0492fSopenharmony_ci */
15d9f0492fSopenharmony_ci#include "param_osadp.h"
16d9f0492fSopenharmony_ci
17d9f0492fSopenharmony_ci#include <errno.h>
18d9f0492fSopenharmony_ci#include <sys/mman.h>
19d9f0492fSopenharmony_ci
20d9f0492fSopenharmony_ci#include "param_message.h"
21d9f0492fSopenharmony_ci#include "param_utils.h"
22d9f0492fSopenharmony_ci
23d9f0492fSopenharmony_ciINIT_LOCAL_API void paramMutexEnvInit(void)
24d9f0492fSopenharmony_ci{
25d9f0492fSopenharmony_ci}
26d9f0492fSopenharmony_ci
27d9f0492fSopenharmony_ciINIT_LOCAL_API int ParamRWMutexCreate(ParamRWMutex *lock)
28d9f0492fSopenharmony_ci{
29d9f0492fSopenharmony_ci    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
30d9f0492fSopenharmony_ci    pthread_rwlockattr_t rwlockatt;
31d9f0492fSopenharmony_ci    pthread_rwlockattr_init(&rwlockatt);
32d9f0492fSopenharmony_ci    pthread_rwlockattr_setpshared(&rwlockatt, PTHREAD_PROCESS_SHARED);
33d9f0492fSopenharmony_ci    pthread_rwlock_init(&lock->rwlock, &rwlockatt);
34d9f0492fSopenharmony_ci    return 0;
35d9f0492fSopenharmony_ci}
36d9f0492fSopenharmony_ci
37d9f0492fSopenharmony_ciINIT_LOCAL_API int ParamRWMutexWRLock(ParamRWMutex *lock)
38d9f0492fSopenharmony_ci{
39d9f0492fSopenharmony_ci    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
40d9f0492fSopenharmony_ci    pthread_rwlock_wrlock(&lock->rwlock);
41d9f0492fSopenharmony_ci    return 0;
42d9f0492fSopenharmony_ci}
43d9f0492fSopenharmony_ciINIT_LOCAL_API int ParamRWMutexRDLock(ParamRWMutex *lock)
44d9f0492fSopenharmony_ci{
45d9f0492fSopenharmony_ci    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
46d9f0492fSopenharmony_ci    pthread_rwlock_rdlock(&lock->rwlock);
47d9f0492fSopenharmony_ci    return 0;
48d9f0492fSopenharmony_ci}
49d9f0492fSopenharmony_ciINIT_LOCAL_API int ParamRWMutexUnlock(ParamRWMutex *lock)
50d9f0492fSopenharmony_ci{
51d9f0492fSopenharmony_ci    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
52d9f0492fSopenharmony_ci    pthread_rwlock_unlock(&lock->rwlock);
53d9f0492fSopenharmony_ci    return 0;
54d9f0492fSopenharmony_ci}
55d9f0492fSopenharmony_ci
56d9f0492fSopenharmony_ciINIT_LOCAL_API int ParamRWMutexDelete(ParamRWMutex *lock)
57d9f0492fSopenharmony_ci{
58d9f0492fSopenharmony_ci    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
59d9f0492fSopenharmony_ci    int ret = pthread_rwlock_destroy(&lock->rwlock);
60d9f0492fSopenharmony_ci    PARAM_CHECK(ret == 0, return -1, "Failed to mutex lock ret %d", ret);
61d9f0492fSopenharmony_ci    return 0;
62d9f0492fSopenharmony_ci}
63d9f0492fSopenharmony_ci
64d9f0492fSopenharmony_ciINIT_LOCAL_API int ParamMutexCreate(ParamMutex *mutex)
65d9f0492fSopenharmony_ci{
66d9f0492fSopenharmony_ci    return 0;
67d9f0492fSopenharmony_ci}
68d9f0492fSopenharmony_ciINIT_LOCAL_API int ParamMutexPend(ParamMutex *mutex)
69d9f0492fSopenharmony_ci{
70d9f0492fSopenharmony_ci    return 0;
71d9f0492fSopenharmony_ci}
72d9f0492fSopenharmony_ciINIT_LOCAL_API int ParamMutexPost(ParamMutex *mutex)
73d9f0492fSopenharmony_ci{
74d9f0492fSopenharmony_ci    return 0;
75d9f0492fSopenharmony_ci}
76d9f0492fSopenharmony_ciINIT_LOCAL_API int ParamMutexDelete(ParamMutex *mutex)
77d9f0492fSopenharmony_ci{
78d9f0492fSopenharmony_ci    return 0;
79d9f0492fSopenharmony_ci}
80d9f0492fSopenharmony_ci
81d9f0492fSopenharmony_ciINIT_LOCAL_API void *GetSharedMem(const char *fileName, MemHandle *handle, uint32_t spaceSize, int readOnly)
82d9f0492fSopenharmony_ci{
83d9f0492fSopenharmony_ci    PARAM_CHECK(fileName != NULL, return NULL, "Invalid filename or handle");
84d9f0492fSopenharmony_ci    int mode = readOnly ? O_RDONLY : O_CREAT | O_RDWR | O_TRUNC;
85d9f0492fSopenharmony_ci    int fd = open(fileName, mode, S_IRWXU | S_IRWXG | S_IROTH);
86d9f0492fSopenharmony_ci    PARAM_ONLY_CHECK(fd >= 0, return NULL);
87d9f0492fSopenharmony_ci
88d9f0492fSopenharmony_ci    int prot = PROT_READ;
89d9f0492fSopenharmony_ci    if (!readOnly) {
90d9f0492fSopenharmony_ci        prot = PROT_READ | PROT_WRITE;
91d9f0492fSopenharmony_ci        ftruncate(fd, spaceSize);
92d9f0492fSopenharmony_ci    }
93d9f0492fSopenharmony_ci    void *areaAddr = (void *)mmap(NULL, spaceSize, prot, MAP_SHARED, fd, 0);
94d9f0492fSopenharmony_ci    PARAM_CHECK(areaAddr != MAP_FAILED && areaAddr != NULL, close(fd);
95d9f0492fSopenharmony_ci        return NULL, "Failed to map memory error %d fileName %s ", errno, fileName);
96d9f0492fSopenharmony_ci    close(fd);
97d9f0492fSopenharmony_ci    return areaAddr;
98d9f0492fSopenharmony_ci}
99d9f0492fSopenharmony_ci
100d9f0492fSopenharmony_ciINIT_LOCAL_API void FreeSharedMem(const MemHandle *handle, void *mem, uint32_t dataSize)
101d9f0492fSopenharmony_ci{
102d9f0492fSopenharmony_ci    PARAM_CHECK(mem != NULL && handle != NULL, return, "Invalid mem or handle");
103d9f0492fSopenharmony_ci    munmap((char *)mem, dataSize);
104d9f0492fSopenharmony_ci}
105