param_osadp.c 9.4 KB
Newer Older
M
Mupceet 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
M
Mupceet 已提交
15 16
#include "param_osadp.h"

M
Mupceet 已提交
17 18 19 20 21 22 23 24 25 26 27 28
#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
M
Mupceet 已提交
29

M
Mupceet 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#define NSEC_PER_MSEC 1000000LL
#define MSEC_PER_SEC 1000LL

static void TimerHandle(union sigval v)
{
    ParamTimer *timer = (ParamTimer *)v.sival_ptr;
    PARAM_CHECK(timer != NULL, return, "Invalid timer");
    if (timer->timeProcessor != NULL) {
        timer->timeProcessor(timer, timer->context);
    }
}

static void SetTimeSpec(struct timespec *ts, int64_t msec)
{
    ts->tv_sec = msec / MSEC_PER_SEC; // 1000LL ms --> m
M
Mupceet 已提交
45
    ts->tv_nsec = (msec - ts->tv_sec * MSEC_PER_SEC) * NSEC_PER_MSEC;
M
Mupceet 已提交
46 47 48 49
}

static int StartTimer(const ParamTimer *paramTimer, int64_t whenMsec, int64_t repeat)
{
M
Mupceet 已提交
50
    UNUSED(repeat);
M
Mupceet 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    struct itimerspec ts;
    SetTimeSpec(&ts.it_value, whenMsec);
    SetTimeSpec(&ts.it_interval, whenMsec);
    int32_t ret = timer_settime(paramTimer->timerId, 0, &ts, NULL);
    if (ret < 0) {
        PARAM_LOGE("Failed to start timer");
        return -1;
    }
    return 0;
}

int ParamTimerCreate(ParamTaskPtr *timer, ProcessTimer process, void *context)
{
    PARAM_CHECK(timer != NULL && process != NULL, return -1, "Invalid timer");
    ParamTimer *paramTimer = malloc(sizeof(ParamTimer));
    PARAM_CHECK(paramTimer != NULL, return -1, "Failed to create timer");
    paramTimer->timeProcessor = process;
    paramTimer->context = context;

    struct sigevent evp;
    (void)memset_s(&evp, sizeof(evp), 0, sizeof(evp));
    evp.sigev_value.sival_ptr = paramTimer;
    evp.sigev_notify = SIGEV_THREAD;
    evp.sigev_notify_function = TimerHandle;
    int32_t ret = timer_create(CLOCK_REALTIME, &evp, &paramTimer->timerId);
    if (ret < 0) {
        PARAM_LOGE("Failed to create timer");
        free(paramTimer);
        return -1;
    }
    *timer = paramTimer;
    return 0;
}

int ParamTimerStart(const ParamTaskPtr timer, uint64_t timeout, uint64_t repeat)
{
    PARAM_CHECK(timer != NULL, return -1, "Invalid timer");
    ParamTimer *paramTimer = (ParamTimer *)timer;
    PARAM_LOGE("ParamTimerStart timeout %llu ", timeout);
    int32_t ret = StartTimer(paramTimer, timeout, repeat);
    if (ret < 0) {
        PARAM_LOGE("Failed to start timer");
        return -1;
    }
    return 0;
}

void ParamTimerClose(ParamTaskPtr timer)
{
    PARAM_CHECK(timer != NULL, return, "Invalid timer");
    ParamTimer *paramTimer = (ParamTimer *)timer;
    timer_delete(paramTimer->timerId);
    free(paramTimer);
}

#ifdef __LITEOS_A__
void *GetSharedMem(const char *fileName, MemHandle *handle, uint32_t spaceSize, int readOnly)
{
    PARAM_CHECK(fileName != NULL && handle != NULL, return NULL, "Invalid filename or handle");
    int mode = readOnly ? O_RDONLY : O_CREAT | O_RDWR;
    void *areaAddr = NULL;
    if (!readOnly) {
        int fd = open(fileName, mode, S_IRWXU | S_IRWXG | S_IROTH);
        PARAM_CHECK(fd >= 0, return NULL, "Open file %s mode %x fail error %d", fileName, mode, errno);
        close(fd);
    }
    key_t key = ftok(fileName, 0x03);  // 0x03 flags for shmget
    PARAM_CHECK(key != -1, return NULL, "Invalid errno %d key for %s", errno, fileName);
    handle->shmid = shmget(key, spaceSize, IPC_CREAT | IPC_EXCL | 0666);  // 0666
    if (handle->shmid == -1) {
        handle->shmid = shmget(key, spaceSize, 0);  // 0666
    }
    PARAM_CHECK(handle->shmid != -1, return NULL, "Invalid shmId errno %d for %s", errno, fileName);
    areaAddr = (void *)shmat(handle->shmid, NULL, 0);
    return areaAddr;
}

void FreeSharedMem(const MemHandle *handle, void *mem, uint32_t dataSize)
{
    PARAM_CHECK(mem != NULL && handle != NULL, return, "Invalid mem or handle");
    shmdt(mem);
    shmctl(handle->shmid, IPC_RMID, NULL);
}

int ParamRWMutexCreate(ParamRWMutex *lock)
{
    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
    pthread_rwlockattr_t rwlockatt;
    pthread_rwlockattr_init(&rwlockatt);
    pthread_rwlockattr_setpshared(&rwlockatt, PTHREAD_PROCESS_SHARED);
    pthread_rwlock_init(&lock->rwlock, &rwlockatt);
    return 0;
}

int ParamRWMutexWRLock(ParamRWMutex *lock)
{
    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
    pthread_rwlock_wrlock(&lock->rwlock);
    return 0;
}
int ParamRWMutexRDLock(ParamRWMutex *lock)
{
    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
    pthread_rwlock_rdlock(&lock->rwlock);
    return 0;
}
int ParamRWMutexUnlock(ParamRWMutex *lock)
{
    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
    pthread_rwlock_unlock(&lock->rwlock);
    return 0;
}

int ParamRWMutexDelete(ParamRWMutex *lock)
{
    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
    uint32_t ret = pthread_rwlock_destroy(&lock->rwlock);
    PARAM_CHECK(ret == 0, return -1, "Failed to mutex lock ret %d", ret);
    return 0;
}

int ParamMutexCeate(ParamMutex *mutex)
{
    PARAM_CHECK(mutex != NULL, return -1, "Invalid mutex");
    pthread_mutexattr_t mutexattr;
    pthread_mutexattr_init(&mutexattr);
    pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);
    pthread_mutex_init(&mutex->mutex, &mutexattr);
    return 0;
}
int ParamMutexPend(ParamMutex *mutex)
{
    PARAM_CHECK(mutex != NULL, return -1, "Invalid mutex");
    if (pthread_mutex_lock(&mutex->mutex) != 0) {
        PARAM_LOGE("Failed to batch begin to save param errno %d", errno);
        return errno;
    }
    return 0;
}
int ParamMutexPost(ParamMutex *mutex)
{
    PARAM_CHECK(mutex != NULL, return -1, "Invalid mutex");
    pthread_mutex_unlock(&mutex->mutex);
    return 0;
}

int ParamMutexDelete(ParamMutex *mutex)
{
    PARAM_CHECK(mutex != NULL, return -1, "Invalid lock");
    uint32_t ret = pthread_mutex_destroy(&mutex->mutex);
    PARAM_CHECK(ret == 0, return -1, "Failed to mutex lock ret %d", ret);
    return 0;
}
#endif

#ifdef __LITEOS_M__
void *GetSharedMem(const char *fileName, MemHandle *handle, uint32_t spaceSize, int readOnly)
{
M
Mupceet 已提交
209
    PARAM_CHECK(spaceSize <= PARAM_WORKSPACE_MAX, return, "Invalid spaceSize %u", spaceSize);
M
Mupceet 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    UNUSED(fileName);
    UNUSED(handle);
    UNUSED(readOnly);
    return (void *)malloc(spaceSize);
}

void FreeSharedMem(const MemHandle *handle, void *mem, uint32_t dataSize)
{
    PARAM_CHECK(mem != NULL && handle != NULL, return, "Invalid mem or handle");
    UNUSED(handle);
    UNUSED(dataSize);
    free(mem);
}

int ParamRWMutexCreate(ParamRWMutex *lock)
{
    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
    uint32_t ret = LOS_MuxCreate(&lock->mutex);
    PARAM_CHECK(ret == LOS_OK, return -1, "Failed to init mutex ret %d", ret);
    return 0;
}

int ParamRWMutexWRLock(ParamRWMutex *lock)
{
    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
    uint32_t ret = LOS_MuxPend(&lock->mutex, LOS_WAIT_FOREVER);
    PARAM_CHECK(ret == LOS_OK, return -1, "Failed to mutex lock ret %d", ret);
    return 0;
}

int ParamRWMutexRDLock(ParamRWMutex *lock)
{
    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
    uint32_t ret = LOS_MuxPend(&lock->mutex, LOS_WAIT_FOREVER);
    PARAM_CHECK(ret == LOS_OK, return -1, "Failed to mutex lock ret %d", ret);
    return 0;
}

int ParamRWMutexUnlock(ParamRWMutex *lock)
{
    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
    uint32_t ret = LOS_MuxPost(&lock->mutex);
    PARAM_CHECK(ret == LOS_OK, return -1, "Failed to mutex lock ret %d", ret);
    return 0;
}

int ParamRWMutexDelete(ParamRWMutex *lock)
{
    PARAM_CHECK(lock != NULL, return -1, "Invalid lock");
    uint32_t ret = LOS_MuxDelete(&lock->mutex);
    PARAM_CHECK(ret == LOS_OK, return -1, "Failed to mutex lock ret %d", ret);
    return 0;
}

int ParamMutexCeate(ParamMutex *mutex)
{
    PARAM_CHECK(mutex != NULL, return -1, "Invalid lock");
    uint32_t ret = LOS_MuxCreate(&mutex->mutex);
    PARAM_CHECK(ret == LOS_OK, return -1, "Failed to init mutex ret %d", ret);
    return 0;
}

int ParamMutexPend(ParamMutex *mutex)
{
    PARAM_CHECK(mutex != NULL, return -1, "Invalid lock");
    uint32_t ret = LOS_MuxPend(&mutex->mutex, LOS_WAIT_FOREVER);
    PARAM_CHECK(ret == LOS_OK, return -1, "Failed to mutex lock ret %d", ret);
    return 0;
}

int ParamMutexPost(ParamMutex *mutex)
{
    PARAM_CHECK(mutex != NULL, return -1, "Invalid lock");
    uint32_t ret = LOS_MuxPost(&mutex->mutex);
    PARAM_CHECK(ret == LOS_OK, return -1, "Failed to mutex lock ret %d", ret);
    return 0;
}

int ParamMutexDelete(ParamMutex *mutex)
{
    PARAM_CHECK(mutex != NULL, return -1, "Invalid mutex");
    uint32_t ret = LOS_MuxDelete(&mutex->mutex);
    PARAM_CHECK(ret == LOS_OK, return -1, "Failed to delete mutex lock ret %d", ret);
    return 0;
}
#endif