• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 "dev_session_mgr.h"
17 
18 #include <inttypes.h>
19 #include "callback_manager.h"
20 #include "device_auth_defines.h"
21 #include "hc_dev_info.h"
22 #include "hc_log.h"
23 #include "hc_mutex.h"
24 #include "hc_time.h"
25 #include "hc_vector.h"
26 
27 typedef struct {
28     DevSession *session;
29     int64_t createTime;
30 } SessionInfo;
31 
32 DECLARE_HC_VECTOR(SessionInfoList, SessionInfo)
33 IMPLEMENT_HC_VECTOR(SessionInfoList, SessionInfo, 1)
34 
35 static SessionInfoList g_sessionInfoList;
36 static HcMutex g_sessionMutex;
37 
GetSessionInfo(int64_t sessionId,SessionInfo ** returnObj)38 static int32_t GetSessionInfo(int64_t sessionId, SessionInfo **returnObj)
39 {
40     uint32_t index;
41     SessionInfo *ptr;
42     FOR_EACH_HC_VECTOR(g_sessionInfoList, index, ptr) {
43         DevSession *session = ptr->session;
44         if (session->id == sessionId) {
45             *returnObj = ptr;
46             return HC_SUCCESS;
47         }
48     }
49     return HC_ERR_SESSION_NOT_EXIST;
50 }
51 
RemoveTimeoutSession(void)52 static void RemoveTimeoutSession(void)
53 {
54     uint32_t index = 0;
55     while (index < g_sessionInfoList.size(&(g_sessionInfoList))) {
56         SessionInfo *sessionInfo = g_sessionInfoList.getp(&(g_sessionInfoList), index);
57         int64_t runningTime = HcGetIntervalTime(sessionInfo->createTime);
58         if (runningTime < TIME_OUT_VALUE) {
59             index++;
60             continue;
61         }
62         DevSession *session = sessionInfo->session;
63         LOGI("session timeout. [AppId]: %" LOG_PUB "s, [Id]: %" LOG_PUB PRId64, session->appId, session->id);
64         LOGI("session timeout. [TimeLimit(/s)]: %" LOG_PUB "d, [RunningTime(/s)]: %" LOG_PUB PRId64,
65             TIME_OUT_VALUE, runningTime);
66         ProcessErrorCallback(session->id, session->opCode, HC_ERR_TIME_OUT, NULL, &session->callback);
67         session->destroy(session);
68         g_sessionInfoList.eraseElement(&(g_sessionInfoList), sessionInfo, index);
69     }
70 }
71 
CheckEnvForOpenSession(int64_t sessionId)72 static int32_t CheckEnvForOpenSession(int64_t sessionId)
73 {
74     SessionInfo *sessionInfo;
75     int32_t res = GetSessionInfo(sessionId, &sessionInfo);
76     if (res == HC_SUCCESS) {
77         LOGE("session has existed. [Id]: %" LOG_PUB PRId64, sessionId);
78         return HC_ERR_REQUEST_EXIST;
79     }
80     uint32_t curSessionNum = HC_VECTOR_SIZE(&g_sessionInfoList);
81     if (curSessionNum >= MAX_AUTH_SESSION_COUNT) {
82         LOGE("The number of sessions has reached the maximum limit. [CurNum]: %" LOG_PUB "u, [NumLimit]: %" LOG_PUB "d",
83             curSessionNum, MAX_AUTH_SESSION_COUNT);
84         return HC_ERR_SESSION_IS_FULL;
85     }
86     return HC_SUCCESS;
87 }
88 
AddNewSessionToList(DevSession * session)89 static int32_t AddNewSessionToList(DevSession *session)
90 {
91     SessionInfo newSessionInfo;
92     newSessionInfo.session = session;
93     newSessionInfo.createTime = HcGetCurTime();
94     if (g_sessionInfoList.pushBackT(&g_sessionInfoList, newSessionInfo) == NULL) {
95         LOGE("push session to list fail.");
96         return HC_ERR_MEMORY_COPY;
97     }
98     return HC_SUCCESS;
99 }
100 
InitDevSessionManager(void)101 int32_t InitDevSessionManager(void)
102 {
103     int32_t res = InitHcMutex(&g_sessionMutex, false);
104     if (res != HC_SUCCESS) {
105         LOGE("Init session mutex failed.");
106         return res;
107     }
108     g_sessionInfoList = CREATE_HC_VECTOR(SessionInfoList);
109     return HC_SUCCESS;
110 }
111 
DestroyDevSessionManager(void)112 void DestroyDevSessionManager(void)
113 {
114     uint32_t index;
115     SessionInfo *ptr;
116     (void)LockHcMutex(&g_sessionMutex);
117     FOR_EACH_HC_VECTOR(g_sessionInfoList, index, ptr) {
118         ptr->session->destroy(ptr->session);
119     }
120     DESTROY_HC_VECTOR(SessionInfoList, &g_sessionInfoList);
121     UnlockHcMutex(&g_sessionMutex);
122     DestroyHcMutex(&g_sessionMutex);
123 }
124 
IsSessionExist(int64_t sessionId)125 bool IsSessionExist(int64_t sessionId)
126 {
127     (void)LockHcMutex(&g_sessionMutex);
128     SessionInfo *sessionInfo;
129     int32_t res = GetSessionInfo(sessionId, &sessionInfo);
130     UnlockHcMutex(&g_sessionMutex);
131     return res == HC_SUCCESS;
132 }
133 
OpenDevSession(int64_t sessionId,const char * appId,SessionInitParams * params)134 int32_t OpenDevSession(int64_t sessionId, const char *appId, SessionInitParams *params)
135 {
136     if ((appId == NULL) || (params == NULL)) {
137         LOGE("invalid params.");
138         return HC_ERR_INVALID_PARAMS;
139     }
140     (void)LockHcMutex(&g_sessionMutex);
141     RemoveTimeoutSession();
142     int32_t res = CheckEnvForOpenSession(sessionId);
143     if (res != HC_SUCCESS) {
144         UnlockHcMutex(&g_sessionMutex);
145         return res;
146     }
147     DevSession *session;
148     res = CreateDevSession(sessionId, appId, params, &session);
149     if (res != HC_SUCCESS) {
150         LOGE("create session fail. [AppId]: %" LOG_PUB "s, [Id]: %" LOG_PUB PRId64, appId, sessionId);
151         UnlockHcMutex(&g_sessionMutex);
152         return res;
153     }
154     res = AddNewSessionToList(session);
155     if (res != HC_SUCCESS) {
156         session->destroy(session);
157         UnlockHcMutex(&g_sessionMutex);
158         return res;
159     }
160     LOGI("create session success. [AppId]: %" LOG_PUB "s, [CurNum]: %" LOG_PUB "u, [Id]: %" LOG_PUB PRId64,
161         appId, HC_VECTOR_SIZE(&g_sessionInfoList), sessionId);
162     UnlockHcMutex(&g_sessionMutex);
163     return HC_SUCCESS;
164 }
165 
StartDevSession(int64_t sessionId)166 int32_t StartDevSession(int64_t sessionId)
167 {
168     (void)LockHcMutex(&g_sessionMutex);
169     RemoveTimeoutSession();
170     SessionInfo *sessionInfo;
171     int32_t res = GetSessionInfo(sessionId, &sessionInfo);
172     if (res != HC_SUCCESS) {
173         LOGE("session not found. [Id]: %" LOG_PUB PRId64, sessionId);
174         UnlockHcMutex(&g_sessionMutex);
175         return res;
176     }
177     DevSession *session = sessionInfo->session;
178     res = session->start(session);
179     UnlockHcMutex(&g_sessionMutex);
180     return res;
181 }
182 
ProcessDevSession(int64_t sessionId,const CJson * receviedMsg,bool * isFinish)183 int32_t ProcessDevSession(int64_t sessionId, const CJson *receviedMsg, bool *isFinish)
184 {
185     if ((receviedMsg == NULL) || (isFinish == NULL)) {
186         LOGE("invalid params.");
187         return HC_ERR_INVALID_PARAMS;
188     }
189     (void)LockHcMutex(&g_sessionMutex);
190     RemoveTimeoutSession();
191     SessionInfo *sessionInfo;
192     int32_t res = GetSessionInfo(sessionId, &sessionInfo);
193     if (res != HC_SUCCESS) {
194         LOGE("session not found. [Id]: %" LOG_PUB PRId64, sessionId);
195         UnlockHcMutex(&g_sessionMutex);
196         return res;
197     }
198     DevSession *session = sessionInfo->session;
199     res = session->process(session, receviedMsg, isFinish);
200     UnlockHcMutex(&g_sessionMutex);
201     return res;
202 }
203 
CloseDevSession(int64_t sessionId)204 void CloseDevSession(int64_t sessionId)
205 {
206     (void)LockHcMutex(&g_sessionMutex);
207     RemoveTimeoutSession();
208     uint32_t index;
209     SessionInfo *ptr;
210     FOR_EACH_HC_VECTOR(g_sessionInfoList, index, ptr) {
211         DevSession *session = ptr->session;
212         if (session->id == sessionId) {
213             session->destroy(session);
214             HC_VECTOR_POPELEMENT(&g_sessionInfoList, ptr, index);
215             LOGI("close session success. [CurNum]: %" LOG_PUB "u, [Id]: %" LOG_PUB PRId64,
216                 HC_VECTOR_SIZE(&g_sessionInfoList), sessionId);
217             UnlockHcMutex(&g_sessionMutex);
218             return;
219         }
220     }
221     LOGI("session not exist. [Id]: %" LOG_PUB PRId64, sessionId);
222     UnlockHcMutex(&g_sessionMutex);
223 }
224 
CancelDevSession(int64_t sessionId,const char * appId)225 void CancelDevSession(int64_t sessionId, const char *appId)
226 {
227     if (appId == NULL) {
228         LOGE("appId is NULL.");
229         return;
230     }
231     (void)LockHcMutex(&g_sessionMutex);
232     RemoveTimeoutSession();
233     uint32_t index;
234     SessionInfo *ptr;
235     FOR_EACH_HC_VECTOR(g_sessionInfoList, index, ptr) {
236         DevSession *session = ptr->session;
237         if (session->id == sessionId && strcmp(session->appId, appId) == 0) {
238             session->destroy(session);
239             HC_VECTOR_POPELEMENT(&g_sessionInfoList, ptr, index);
240             LOGI("cancel session success. [CurNum]: %" LOG_PUB "u, [Id]: %" LOG_PUB PRId64,
241                 HC_VECTOR_SIZE(&g_sessionInfoList), sessionId);
242             UnlockHcMutex(&g_sessionMutex);
243             return;
244         }
245     }
246     LOGI("session not exist. [Id]: %" LOG_PUB PRId64, sessionId);
247     UnlockHcMutex(&g_sessionMutex);
248 }
249