• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "trans_session_service.h"
17 
18 #include "securec.h"
19 #include "softbus_adapter_mem.h"
20 #include "softbus_def.h"
21 #include "softbus_errcode.h"
22 #include "softbus_log.h"
23 #include "softbus_permission.h"
24 #include "softbus_qos.h"
25 #include "softbus_utils.h"
26 #include "trans_channel_manager.h"
27 #include "trans_session_manager.h"
28 
29 static bool g_transSessionInitFlag = false;
30 
TransServerInit(void)31 int TransServerInit(void)
32 {
33     if (g_transSessionInitFlag) {
34         return SOFTBUS_OK;
35     }
36     if (TransPermissionInit() != SOFTBUS_OK) {
37         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Init trans permission failed");
38         return SOFTBUS_ERR;
39     }
40     if (TransSessionMgrInit() != SOFTBUS_OK) {
41         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransSessionMgrInit failed");
42         return SOFTBUS_ERR;
43     }
44     if (TransChannelInit() != SOFTBUS_OK) {
45         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransChannelInit failed");
46         return SOFTBUS_ERR;
47     }
48     if (InitQos() != SOFTBUS_OK) {
49         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "QosInit Failed");
50         return SOFTBUS_ERR;
51     }
52     g_transSessionInitFlag = true;
53     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "trans session server list init succ");
54     return SOFTBUS_OK;
55 }
56 
TransServerDeinit(void)57 void TransServerDeinit(void)
58 {
59     if (g_transSessionInitFlag == false) {
60         return;
61     }
62 
63     TransSessionMgrDeinit();
64     TransChannelDeinit();
65     TransPermissionDeinit();
66     g_transSessionInitFlag = false;
67 }
68 
TransServerDeathCallback(const char * pkgName)69 void TransServerDeathCallback(const char *pkgName)
70 {
71     TransDelItemByPackageName(pkgName);
72     TransChannelDeathCallback(pkgName);
73 }
74 
TransCreateSessionServer(const char * pkgName,const char * sessionName,int32_t uid,int32_t pid)75 int32_t TransCreateSessionServer(const char *pkgName, const char *sessionName, int32_t uid, int32_t pid)
76 {
77     if (!IsValidString(pkgName, PKG_NAME_SIZE_MAX) ||
78         !IsValidString(sessionName, SESSION_NAME_SIZE_MAX)) {
79         return SOFTBUS_INVALID_PARAM;
80     }
81     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "TransCreateSessionServer:pkgName=%s, sessionName=%s",
82         pkgName, sessionName);
83     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "TransCreateSessionServer:uid=%d, pid=%d", uid, pid);
84 
85     SessionServer *newNode = (SessionServer *)SoftBusCalloc(sizeof(SessionServer));
86     if (newNode == NULL) {
87         return SOFTBUS_ERR;
88     }
89     if (strcpy_s(newNode->pkgName, sizeof(newNode->pkgName), pkgName) != EOK) {
90         SoftBusFree(newNode);
91         return SOFTBUS_ERR;
92     }
93     if (strcpy_s(newNode->sessionName, sizeof(newNode->sessionName), sessionName) != EOK) {
94         SoftBusFree(newNode);
95         return SOFTBUS_ERR;
96     }
97     newNode->type = SEC_TYPE_CIPHERTEXT;
98     newNode->uid = uid;
99     newNode->pid = pid;
100 
101     int32_t ret = TransSessionServerAddItem(newNode);
102     if (ret != SOFTBUS_OK) {
103         SoftBusFree(newNode);
104         if (ret == SOFTBUS_SERVER_NAME_REPEATED) {
105             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "session server is already created");
106         }
107         return ret;
108     }
109     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "CreateSessionServer ok");
110     return SOFTBUS_OK;
111 }
112 
TransRemoveSessionServer(const char * pkgName,const char * sessionName)113 int32_t TransRemoveSessionServer(const char *pkgName, const char *sessionName)
114 {
115     if (!IsValidString(pkgName, PKG_NAME_SIZE_MAX) ||
116         !IsValidString(sessionName, SESSION_NAME_SIZE_MAX)) {
117         return SOFTBUS_INVALID_PARAM;
118     }
119     return TransSessionServerDelItem(sessionName);
120 }
121 
TransOpenSession(const SessionParam * param,TransInfo * info)122 int32_t TransOpenSession(const SessionParam *param, TransInfo *info)
123 {
124     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "trans server opensession.");
125     if (!IsValidString(param->sessionName, SESSION_NAME_SIZE_MAX) ||
126         !IsValidString(param->peerSessionName, SESSION_NAME_SIZE_MAX) ||
127         !IsValidString(param->peerDeviceId, DEVICE_ID_SIZE_MAX)) {
128         return INVALID_CHANNEL_ID;
129     }
130     if (param->groupId == NULL || strlen(param->groupId) >= GROUP_ID_SIZE_MAX) {
131         return INVALID_CHANNEL_ID;
132     }
133 
134     if (!TransSessionServerIsExist(param->sessionName)) {
135         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "session server invalid");
136         return INVALID_CHANNEL_ID;
137     }
138 
139     return TransOpenChannel(param, info);
140 }
141