1 /*
2  * Copyright (C) 2022 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 "rpc_feature_set.h"
17 
18 #include <stddef.h>
19 
20 #define RPC_FEATURE_LAST 43
21 #define INVAL_TOKEN_ID 0x0
22 
23 enum {
24     ACCESS_TOKEN_FLAG = 0x1,
25 };
26 
27 static const uint32_t RPC_FEATURE_MAGIC_NUM = ('R' << 24) | ('F' << 16) | ('S' << 8) | RPC_FEATURE_LAST;
28 static const uint32_t RPC_ACCESS_TOKEN_TAG = 0;
29 static const uint32_t RPC_FEATURE_FLAG = 0x1;
30 static const uint32_t TOKEN_ID_SIZE = 4;
31 static const uint32_t RPC_FEATURE_ACK = 0x80000000;
32 
GetFeatureMagicNumber(void)33 static uint32_t GetFeatureMagicNumber(void)
34 {
35     return RPC_FEATURE_MAGIC_NUM;
36 }
37 
GetFeatureATTag(void)38 static uint32_t GetFeatureATTag(void)
39 {
40     return RPC_ACCESS_TOKEN_TAG;
41 }
42 
GetLocalRpcFeature(void)43 uint32_t GetLocalRpcFeature(void)
44 {
45     return RPC_FEATURE_FLAG;
46 }
47 
GetRpcFeatureAck(void)48 uint32_t GetRpcFeatureAck(void)
49 {
50     return RPC_FEATURE_ACK;
51 }
52 
IsATEnable(uint32_t featureSet)53 bool IsATEnable(uint32_t featureSet)
54 {
55     return (featureSet & ACCESS_TOKEN_FLAG) > 0;
56 }
57 
IsFeatureAck(uint32_t featureSet)58 bool IsFeatureAck(uint32_t featureSet)
59 {
60     return (featureSet & RPC_FEATURE_ACK) > 0;
61 }
62 
GetTokenIdSize(void)63 uint32_t GetTokenIdSize(void)
64 {
65     return TOKEN_ID_SIZE;
66 }
67 
GetFeatureSize(void)68 uint32_t GetFeatureSize(void)
69 {
70     return (uint32_t)sizeof(FeatureTransData);
71 }
72 
SetFeatureTransData(FeatureTransData * data,uint32_t size)73 bool SetFeatureTransData(FeatureTransData *data, uint32_t size)
74 {
75     if (data == NULL || size < GetFeatureSize()) {
76         return false;
77     }
78     data->magicNum = GetFeatureMagicNumber();
79     data->tag = GetFeatureATTag();
80 
81     return true;
82 }
83 
GetTokenFromData(FeatureTransData * data,uint32_t size)84 uint32_t GetTokenFromData(FeatureTransData *data, uint32_t size)
85 {
86     if (data == NULL || size < GetFeatureSize()) {
87         return INVAL_TOKEN_ID;
88     }
89     if (data->magicNum != GetFeatureMagicNumber() || data->tag != GetFeatureATTag()) {
90         return INVAL_TOKEN_ID;
91     }
92     return data->tokenId;
93 }