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