12ee81decSopenharmony_ci/*
22ee81decSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
32ee81decSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42ee81decSopenharmony_ci * you may not use this file except in compliance with the License.
52ee81decSopenharmony_ci * You may obtain a copy of the License at
62ee81decSopenharmony_ci *
72ee81decSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
82ee81decSopenharmony_ci *
92ee81decSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102ee81decSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112ee81decSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122ee81decSopenharmony_ci * See the License for the specific language governing permissions and
132ee81decSopenharmony_ci * limitations under the License.
142ee81decSopenharmony_ci */
152ee81decSopenharmony_ci
162ee81decSopenharmony_ci#include "dslm_messenger_wrapper.h"
172ee81decSopenharmony_ci
182ee81decSopenharmony_ci#include <stddef.h>
192ee81decSopenharmony_ci#include <stdint.h>
202ee81decSopenharmony_ci#include <stdbool.h>
212ee81decSopenharmony_ci
222ee81decSopenharmony_ci#include "messenger.h"
232ee81decSopenharmony_ci#include "utils_mutex.h"
242ee81decSopenharmony_ci#include "device_security_defines.h"
252ee81decSopenharmony_ci
262ee81decSopenharmony_ciMessenger *g_messenger = NULL;
272ee81decSopenharmony_cistatic Mutex g_mutex = INITED_MUTEX;
282ee81decSopenharmony_ci
292ee81decSopenharmony_ciuint32_t InitMessenger(const MessageReceiver messageReceiver, const StatusReceiver statusReceiver,
302ee81decSopenharmony_ci    const SendResultNotifier notifier)
312ee81decSopenharmony_ci{
322ee81decSopenharmony_ci    MessengerConfig config = {
332ee81decSopenharmony_ci        .pkgName = GetMessengerPackageName(),
342ee81decSopenharmony_ci        #ifdef L2_STANDARD
352ee81decSopenharmony_ci        .primarySockName = GetMessengerPrimarySessionName(),
362ee81decSopenharmony_ci        .secondarySockName = GetMessengerSecondarySessionName(),
372ee81decSopenharmony_ci        #else
382ee81decSopenharmony_ci        .primarySessName = GetMessengerPrimarySessionName(),
392ee81decSopenharmony_ci        .secondarySessName = GetMessengerSecondarySessionName(),
402ee81decSopenharmony_ci        #endif
412ee81decSopenharmony_ci        .messageReceiver = messageReceiver,
422ee81decSopenharmony_ci        .statusReceiver = statusReceiver,
432ee81decSopenharmony_ci        .sendResultNotifier = notifier,
442ee81decSopenharmony_ci    };
452ee81decSopenharmony_ci    InitMutex(&g_mutex);
462ee81decSopenharmony_ci    LockMutex(&g_mutex);
472ee81decSopenharmony_ci    g_messenger = CreateMessenger(&config);
482ee81decSopenharmony_ci    UnlockMutex(&g_mutex);
492ee81decSopenharmony_ci    if (g_messenger == NULL) {
502ee81decSopenharmony_ci        return ERR_MSG_NOT_INIT;
512ee81decSopenharmony_ci    }
522ee81decSopenharmony_ci
532ee81decSopenharmony_ci    return SUCCESS;
542ee81decSopenharmony_ci}
552ee81decSopenharmony_ci
562ee81decSopenharmony_ciuint32_t DeinitMessenger(void)
572ee81decSopenharmony_ci{
582ee81decSopenharmony_ci    LockMutex(&g_mutex);
592ee81decSopenharmony_ci    if (g_messenger == NULL) {
602ee81decSopenharmony_ci        UnlockMutex(&g_mutex);
612ee81decSopenharmony_ci        return SUCCESS;
622ee81decSopenharmony_ci    }
632ee81decSopenharmony_ci    DestroyMessenger(g_messenger);
642ee81decSopenharmony_ci    UnlockMutex(&g_mutex);
652ee81decSopenharmony_ci    return SUCCESS;
662ee81decSopenharmony_ci}
672ee81decSopenharmony_ci
682ee81decSopenharmony_cibool GetMessengerStatus(void)
692ee81decSopenharmony_ci{
702ee81decSopenharmony_ci    LockMutex(&g_mutex);
712ee81decSopenharmony_ci    if (g_messenger == NULL) {
722ee81decSopenharmony_ci        UnlockMutex(&g_mutex);
732ee81decSopenharmony_ci        return false;
742ee81decSopenharmony_ci    }
752ee81decSopenharmony_ci    bool ret = IsMessengerReady(g_messenger);
762ee81decSopenharmony_ci    UnlockMutex(&g_mutex);
772ee81decSopenharmony_ci    return ret;
782ee81decSopenharmony_ci}
792ee81decSopenharmony_ci
802ee81decSopenharmony_civoid SendMsgToDevice(uint64_t transNo, const DeviceIdentify *devId, const uint8_t *msg, uint32_t msgLen)
812ee81decSopenharmony_ci{
822ee81decSopenharmony_ci    LockMutex(&g_mutex);
832ee81decSopenharmony_ci    if (g_messenger == NULL) {
842ee81decSopenharmony_ci        UnlockMutex(&g_mutex);
852ee81decSopenharmony_ci        return;
862ee81decSopenharmony_ci    }
872ee81decSopenharmony_ci    SendMsgTo(g_messenger, transNo, devId, msg, msgLen);
882ee81decSopenharmony_ci    UnlockMutex(&g_mutex);
892ee81decSopenharmony_ci    return;
902ee81decSopenharmony_ci}
912ee81decSopenharmony_ci
922ee81decSopenharmony_cibool GetPeerDeviceOnlineStatus(const DeviceIdentify *devId, int32_t *level)
932ee81decSopenharmony_ci{
942ee81decSopenharmony_ci    LockMutex(&g_mutex);
952ee81decSopenharmony_ci    if (g_messenger == NULL) {
962ee81decSopenharmony_ci        UnlockMutex(&g_mutex);
972ee81decSopenharmony_ci        return false;
982ee81decSopenharmony_ci    }
992ee81decSopenharmony_ci    if (devId == NULL || level == NULL) {
1002ee81decSopenharmony_ci        UnlockMutex(&g_mutex);
1012ee81decSopenharmony_ci        return false;
1022ee81decSopenharmony_ci    }
1032ee81decSopenharmony_ci    bool ret = GetDeviceOnlineStatus(g_messenger, devId, level);
1042ee81decSopenharmony_ci    UnlockMutex(&g_mutex);
1052ee81decSopenharmony_ci    return ret;
1062ee81decSopenharmony_ci}
1072ee81decSopenharmony_ci
1082ee81decSopenharmony_ciconst DeviceIdentify *GetSelfDevice(int32_t *level)
1092ee81decSopenharmony_ci{
1102ee81decSopenharmony_ci    LockMutex(&g_mutex);
1112ee81decSopenharmony_ci    static DeviceIdentify deviceId = {0, {0}};
1122ee81decSopenharmony_ci    if (deviceId.length == 0 || deviceId.identity[0] == 0) {
1132ee81decSopenharmony_ci        if (g_messenger != NULL) {
1142ee81decSopenharmony_ci            GetSelfDeviceIdentify(g_messenger, &deviceId, level);
1152ee81decSopenharmony_ci        }
1162ee81decSopenharmony_ci    }
1172ee81decSopenharmony_ci    UnlockMutex(&g_mutex);
1182ee81decSopenharmony_ci    return &deviceId;
1192ee81decSopenharmony_ci}
1202ee81decSopenharmony_ci
1212ee81decSopenharmony_ci__attribute__((weak)) const char *GetMessengerPackageName(void)
1222ee81decSopenharmony_ci{
1232ee81decSopenharmony_ci    return "ohos.dslm";
1242ee81decSopenharmony_ci}
1252ee81decSopenharmony_ci
1262ee81decSopenharmony_ci__attribute__((weak)) const char *GetMessengerPrimarySessionName(void)
1272ee81decSopenharmony_ci{
1282ee81decSopenharmony_ci    return "device.security.level";
1292ee81decSopenharmony_ci}
1302ee81decSopenharmony_ci
1312ee81decSopenharmony_ci__attribute__((weak)) const char *GetMessengerSecondarySessionName(void)
1322ee81decSopenharmony_ci{
1332ee81decSopenharmony_ci#ifdef SECONDARY_SOCKET_NAME
1342ee81decSopenharmony_ci    return SECONDARY_SOCKET_NAME;
1352ee81decSopenharmony_ci#else
1362ee81decSopenharmony_ci    return NULL;
1372ee81decSopenharmony_ci#endif
1382ee81decSopenharmony_ci}