1*84e33947SAndroid Build Coastguard Worker /*
2*84e33947SAndroid Build Coastguard Worker * Copyright (C) 2020 The Android Open Source Project
3*84e33947SAndroid Build Coastguard Worker *
4*84e33947SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*84e33947SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*84e33947SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*84e33947SAndroid Build Coastguard Worker *
8*84e33947SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*84e33947SAndroid Build Coastguard Worker *
10*84e33947SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*84e33947SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*84e33947SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*84e33947SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*84e33947SAndroid Build Coastguard Worker * limitations under the License.
15*84e33947SAndroid Build Coastguard Worker */
16*84e33947SAndroid Build Coastguard Worker
17*84e33947SAndroid Build Coastguard Worker #include "chpp/services/loopback.h"
18*84e33947SAndroid Build Coastguard Worker
19*84e33947SAndroid Build Coastguard Worker #include <stdbool.h>
20*84e33947SAndroid Build Coastguard Worker #include <stddef.h>
21*84e33947SAndroid Build Coastguard Worker #include <stdint.h>
22*84e33947SAndroid Build Coastguard Worker #include <string.h>
23*84e33947SAndroid Build Coastguard Worker
24*84e33947SAndroid Build Coastguard Worker #include "chpp/app.h"
25*84e33947SAndroid Build Coastguard Worker #include "chpp/log.h"
26*84e33947SAndroid Build Coastguard Worker #include "chpp/memory.h"
27*84e33947SAndroid Build Coastguard Worker #include "chpp/transport.h"
28*84e33947SAndroid Build Coastguard Worker
29*84e33947SAndroid Build Coastguard Worker /************************************************
30*84e33947SAndroid Build Coastguard Worker * Public Functions
31*84e33947SAndroid Build Coastguard Worker ***********************************************/
32*84e33947SAndroid Build Coastguard Worker
chppDispatchLoopbackClientRequest(struct ChppAppState * context,uint8_t * buf,size_t len)33*84e33947SAndroid Build Coastguard Worker bool chppDispatchLoopbackClientRequest(struct ChppAppState *context,
34*84e33947SAndroid Build Coastguard Worker uint8_t *buf, size_t len) {
35*84e33947SAndroid Build Coastguard Worker uint8_t *response = chppMalloc(len);
36*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
37*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
38*84e33947SAndroid Build Coastguard Worker chppEnqueueTxErrorDatagram(context->transportContext,
39*84e33947SAndroid Build Coastguard Worker CHPP_TRANSPORT_ERROR_OOM);
40*84e33947SAndroid Build Coastguard Worker
41*84e33947SAndroid Build Coastguard Worker } else {
42*84e33947SAndroid Build Coastguard Worker CHPP_LOGD("Looping back len=%" PRIuSIZE, len);
43*84e33947SAndroid Build Coastguard Worker
44*84e33947SAndroid Build Coastguard Worker memcpy(response, buf, len);
45*84e33947SAndroid Build Coastguard Worker
46*84e33947SAndroid Build Coastguard Worker struct ChppAppHeader *responseHeader = (struct ChppAppHeader *)response;
47*84e33947SAndroid Build Coastguard Worker responseHeader->type = CHPP_MESSAGE_TYPE_SERVICE_RESPONSE;
48*84e33947SAndroid Build Coastguard Worker
49*84e33947SAndroid Build Coastguard Worker chppEnqueueTxDatagramOrFail(context->transportContext, response, len);
50*84e33947SAndroid Build Coastguard Worker }
51*84e33947SAndroid Build Coastguard Worker
52*84e33947SAndroid Build Coastguard Worker return true;
53*84e33947SAndroid Build Coastguard Worker }
54