1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "chpp/services/discovery.h"
18
19 #include <stdbool.h>
20 #include <stddef.h>
21 #include <stdint.h>
22
23 #include "chpp/app.h"
24 #include "chpp/common/discovery.h"
25 #include "chpp/log.h"
26 #include "chpp/macros.h"
27 #include "chpp/services.h"
28 #include "chpp/transport.h"
29
30 /************************************************
31 * Prototypes
32 ***********************************************/
33
34 static void chppDiscoveryDiscoverAll(struct ChppAppState *context,
35 const struct ChppAppHeader *rxHeader);
36
37 /************************************************
38 * Private Functions
39 ***********************************************/
40
41 /**
42 * Processes the Discover All Services (0x0001) request.
43 *
44 * @param context Maintains status for each app layer instance.
45 * @param requestHeader Request datagram header. Cannot be null.
46 */
chppDiscoveryDiscoverAll(struct ChppAppState * context,const struct ChppAppHeader * requestHeader)47 static void chppDiscoveryDiscoverAll(
48 struct ChppAppState *context, const struct ChppAppHeader *requestHeader) {
49 // Allocate response
50 size_t responseLen =
51 sizeof(struct ChppAppHeader) +
52 context->registeredServiceCount * sizeof(struct ChppServiceDescriptor);
53
54 struct ChppDiscoveryResponse *response = chppAllocServiceResponseTypedArray(
55 requestHeader, struct ChppDiscoveryResponse,
56 context->registeredServiceCount, services);
57
58 if (response == NULL) {
59 CHPP_LOG_OOM();
60 CHPP_ASSERT(false);
61
62 } else {
63 CHPP_LOGI("Discovery resp count=%" PRIu8, context->registeredServiceCount);
64
65 response->header.error = CHPP_APP_ERROR_NONE;
66
67 // Populate list of services
68 for (size_t i = 0; i < context->registeredServiceCount; i++) {
69 response->services[i] = context->registeredServices[i]->descriptor;
70 }
71
72 // Send out response datagram
73 chppEnqueueTxDatagramOrFail(context->transportContext, response,
74 responseLen);
75 }
76 }
77
78 /************************************************
79 * Public Functions
80 ***********************************************/
81
chppDispatchDiscoveryClientRequest(struct ChppAppState * context,const uint8_t * buf,size_t len)82 bool chppDispatchDiscoveryClientRequest(struct ChppAppState *context,
83 const uint8_t *buf, size_t len) {
84 UNUSED_VAR(len);
85 const struct ChppAppHeader *rxHeader = (const struct ChppAppHeader *)buf;
86 bool success = true;
87
88 switch (rxHeader->command) {
89 case CHPP_DISCOVERY_COMMAND_DISCOVER_ALL: {
90 // Send back a list of services supported by this platform.
91 chppDiscoveryDiscoverAll(context, rxHeader);
92 break;
93 }
94 default: {
95 success = false;
96 }
97 }
98 return success;
99 }
100