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 "softbus_client_context_manager.h"
17
18 #include "softbus_adapter_mem.h"
19 #include "softbus_errcode.h"
20 #include "softbus_log.h"
21
22 typedef struct {
23 unsigned int handle;
24 unsigned int token;
25 unsigned int cookie;
26 void *ctx;
27 } SoftBusClientContext;
28
29 static SoftBusClientContext *g_clientCtx = NULL;
30
ClientContextInit(void)31 int ClientContextInit(void)
32 {
33 if (g_clientCtx != NULL) {
34 return SOFTBUS_OK;
35 }
36 g_clientCtx = SoftBusCalloc(sizeof(SoftBusClientContext));
37 if (g_clientCtx == NULL) {
38 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "malloc failed.");
39 return SOFTBUS_MEM_ERR;
40 }
41 return SOFTBUS_OK;
42 }
43
ClientContextDeinit(void)44 void ClientContextDeinit(void)
45 {
46 if (g_clientCtx == NULL) {
47 return;
48 }
49
50 SoftBusFree(g_clientCtx);
51 g_clientCtx = NULL;
52 }
53
SetClientIdentity(unsigned int handle,unsigned int token,unsigned int cookie,void * ctx)54 void SetClientIdentity(unsigned int handle, unsigned int token, unsigned int cookie, void *ctx)
55 {
56 if (g_clientCtx == NULL) {
57 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "client ctx not init");
58 return;
59 }
60
61 g_clientCtx->handle = handle;
62 g_clientCtx->token = token;
63 g_clientCtx->cookie = cookie;
64 g_clientCtx->ctx = ctx;
65 }
66
GetClientIdentity(unsigned int * handle,unsigned int * token,unsigned int * cookie,void ** ctx)67 int GetClientIdentity(unsigned int *handle, unsigned int *token, unsigned int *cookie, void **ctx)
68 {
69 if (handle == NULL || token == NULL || cookie == NULL || ctx == NULL) {
70 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid param");
71 return SOFTBUS_ERR;
72 }
73
74 if (g_clientCtx == NULL) {
75 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "client ctx not init");
76 return SOFTBUS_ERR;
77 }
78
79 *handle = g_clientCtx->handle;
80 *token = g_clientCtx->token;
81 *cookie = g_clientCtx->cookie;
82 *ctx = g_clientCtx->ctx;
83
84 return SOFTBUS_OK;
85 }
86