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 "disc_coap_capability.h"
17 
18 #include "anonymizer.h"
19 #include "disc_log.h"
20 #include "disc_nstackx_adapter.h"
21 #include "softbus_errcode.h"
22 
DiscCoapAssembleCapData(uint32_t capability,const char * capabilityData,uint32_t dataLen,char * outData,uint32_t outLen)23 int32_t DiscCoapAssembleCapData(uint32_t capability, const char *capabilityData, uint32_t dataLen, char *outData,
24     uint32_t outLen)
25 {
26     (void)capability;
27     (void)capabilityData;
28     (void)dataLen;
29     (void)outData;
30     (void)outLen;
31     return SOFTBUS_FUNC_NOT_SUPPORT;
32 }
33 
DiscCoapFillServiceData(const PublishOption * option,char * outData,uint32_t outDataLen,uint32_t allCap)34 int32_t DiscCoapFillServiceData(const PublishOption *option, char *outData, uint32_t outDataLen, uint32_t allCap)
35 {
36     (void)option;
37     (void)outData;
38     (void)outDataLen;
39     (void)allCap;
40     return SOFTBUS_OK;
41 }
42 
DiscFillBtype(uint32_t capability,uint32_t allCap,NSTACKX_DiscoverySettings * discSet)43 void DiscFillBtype(uint32_t capability, uint32_t allCap, NSTACKX_DiscoverySettings *discSet)
44 {
45     (void)allCap;
46     DISC_CHECK_AND_RETURN_LOGW(discSet != NULL, DISC_COAP, "discSet is NULL");
47     switch (capability) {
48         case 1 << OSD_CAPABILITY_BITMAP:
49             discSet->businessType = (uint8_t)NSTACKX_BUSINESS_TYPE_NULL;
50             break;
51         case 1 << DDMP_CAPABILITY_BITMAP:
52             discSet->businessType = (uint8_t)NSTACKX_BUSINESS_TYPE_AUTONET;
53             break;
54         case 1 << SHARE_CAPABILITY_BITMAP:
55             discSet->businessType = (uint8_t)NSTACKX_BUSINESS_TYPE_STRATEGY;
56             break;
57         default:
58             DISC_LOGW(DISC_COAP, "use the default bType, capability=%{public}u", capability);
59             discSet->businessType = (uint8_t)NSTACKX_BUSINESS_TYPE_NULL;
60             break;
61     }
62 }
63 
DiscCoapProcessDeviceInfo(const NSTACKX_DeviceInfo * nstackxInfo,DeviceInfo * devInfo,const DiscInnerCallback * discCb,SoftBusMutex * discCbLock)64 int32_t DiscCoapProcessDeviceInfo(const NSTACKX_DeviceInfo *nstackxInfo, DeviceInfo *devInfo,
65     const DiscInnerCallback *discCb, SoftBusMutex *discCbLock)
66 {
67     DISC_CHECK_AND_RETURN_RET_LOGE(nstackxInfo != NULL, SOFTBUS_INVALID_PARAM, DISC_COAP, "nstackx devInfo is NULL");
68     DISC_CHECK_AND_RETURN_RET_LOGE(devInfo != NULL, SOFTBUS_INVALID_PARAM, DISC_COAP, "devInfo is NULL");
69 
70     InnerDeviceInfoAddtions additions = {
71         .medium = COAP,
72     };
73     char *anonymizedName = NULL;
74     Anonymize(devInfo->devName, &anonymizedName);
75 
76     if (nstackxInfo->discoveryType == NSTACKX_DISCOVERY_TYPE_ACTIVE ||
77         nstackxInfo->mode == PUBLISH_MODE_PROACTIVE) {
78         DISC_LOGI(DISC_COAP, "DiscFound: devName=%{public}s, netIf=%{public}s",
79             AnonymizeWrapper(anonymizedName), nstackxInfo->networkName);
80         AnonymizeFree(anonymizedName);
81         DISC_CHECK_AND_RETURN_RET_LOGE(SoftBusMutexLock(discCbLock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR,
82             DISC_COAP, "lock failed");
83         if (discCb == NULL || discCb->OnDeviceFound == NULL) {
84             DISC_LOGW(DISC_COAP, "discCb is NULL");
85             (void)SoftBusMutexUnlock(discCbLock);
86             return SOFTBUS_INVALID_PARAM;
87         }
88         discCb->OnDeviceFound(devInfo, &additions);
89         (void)SoftBusMutexUnlock(discCbLock);
90         return SOFTBUS_OK;
91     }
92 
93     uint8_t bType = nstackxInfo->businessType;
94     DISC_LOGI(DISC_COAP, "DiscRecv: broadcast devName=%{public}s, bType=%{public}u",
95         AnonymizeWrapper(anonymizedName), bType);
96     AnonymizeFree(anonymizedName);
97     if (bType != NSTACKX_BUSINESS_TYPE_NULL && DiscCoapSendRsp(devInfo, bType) != SOFTBUS_OK) {
98         DISC_LOGE(DISC_COAP, "send response failed");
99         return SOFTBUS_DISCOVER_COAP_SEND_RSP_FAIL;
100     }
101     return SOFTBUS_OK;
102 }
103 
DiscCoapReportNotification(const NSTACKX_NotificationConfig * notification)104 void DiscCoapReportNotification(const NSTACKX_NotificationConfig *notification)
105 {
106     (void)notification;
107 }
108