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/timesync.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
23*84e33947SAndroid Build Coastguard Worker #include "chpp/app.h"
24*84e33947SAndroid Build Coastguard Worker #include "chpp/common/timesync.h"
25*84e33947SAndroid Build Coastguard Worker #include "chpp/log.h"
26*84e33947SAndroid Build Coastguard Worker #include "chpp/macros.h"
27*84e33947SAndroid Build Coastguard Worker #include "chpp/services.h"
28*84e33947SAndroid Build Coastguard Worker #include "chpp/time.h"
29*84e33947SAndroid Build Coastguard Worker #include "chpp/transport.h"
30*84e33947SAndroid Build Coastguard Worker
31*84e33947SAndroid Build Coastguard Worker /**
32*84e33947SAndroid Build Coastguard Worker * Processes the GetTime (0x0001) request and responds with the time as on the
33*84e33947SAndroid Build Coastguard Worker * service. This should be the same clock used to timestamp any data samples
34*84e33947SAndroid Build Coastguard Worker * provided to CHPP.
35*84e33947SAndroid Build Coastguard Worker *
36*84e33947SAndroid Build Coastguard Worker * @param context Maintains status for each app layer instance.
37*84e33947SAndroid Build Coastguard Worker * @param requestHeader Header of the request.
38*84e33947SAndroid Build Coastguard Worker */
chppTimesyncGetTime(struct ChppAppState * context,const struct ChppAppHeader * requestHeader)39*84e33947SAndroid Build Coastguard Worker static void chppTimesyncGetTime(struct ChppAppState *context,
40*84e33947SAndroid Build Coastguard Worker const struct ChppAppHeader *requestHeader) {
41*84e33947SAndroid Build Coastguard Worker struct ChppTimesyncResponse *response =
42*84e33947SAndroid Build Coastguard Worker chppAllocResponseFixed(requestHeader, struct ChppTimesyncResponse);
43*84e33947SAndroid Build Coastguard Worker size_t responseLen = sizeof(*response);
44*84e33947SAndroid Build Coastguard Worker
45*84e33947SAndroid Build Coastguard Worker if (response == NULL) {
46*84e33947SAndroid Build Coastguard Worker CHPP_LOG_OOM();
47*84e33947SAndroid Build Coastguard Worker CHPP_DEBUG_ASSERT(false);
48*84e33947SAndroid Build Coastguard Worker } else {
49*84e33947SAndroid Build Coastguard Worker response->timeNs = chppGetCurrentTimeNs();
50*84e33947SAndroid Build Coastguard Worker CHPP_LOGD("chppTimesyncGetTime returning %" PRIuSIZE
51*84e33947SAndroid Build Coastguard Worker " bytes at time=%" PRIu64,
52*84e33947SAndroid Build Coastguard Worker responseLen, response->timeNs / CHPP_NSEC_PER_MSEC);
53*84e33947SAndroid Build Coastguard Worker
54*84e33947SAndroid Build Coastguard Worker chppEnqueueTxDatagramOrFail(context->transportContext, response,
55*84e33947SAndroid Build Coastguard Worker responseLen);
56*84e33947SAndroid Build Coastguard Worker }
57*84e33947SAndroid Build Coastguard Worker }
58*84e33947SAndroid Build Coastguard Worker
59*84e33947SAndroid Build Coastguard Worker /************************************************
60*84e33947SAndroid Build Coastguard Worker * Public Functions
61*84e33947SAndroid Build Coastguard Worker ***********************************************/
62*84e33947SAndroid Build Coastguard Worker
chppDispatchTimesyncClientRequest(struct ChppAppState * context,const uint8_t * buf,size_t len)63*84e33947SAndroid Build Coastguard Worker bool chppDispatchTimesyncClientRequest(struct ChppAppState *context,
64*84e33947SAndroid Build Coastguard Worker const uint8_t *buf, size_t len) {
65*84e33947SAndroid Build Coastguard Worker UNUSED_VAR(len);
66*84e33947SAndroid Build Coastguard Worker const struct ChppAppHeader *rxHeader = (const struct ChppAppHeader *)buf;
67*84e33947SAndroid Build Coastguard Worker bool success = true;
68*84e33947SAndroid Build Coastguard Worker
69*84e33947SAndroid Build Coastguard Worker switch (rxHeader->command) {
70*84e33947SAndroid Build Coastguard Worker case CHPP_TIMESYNC_COMMAND_GETTIME: {
71*84e33947SAndroid Build Coastguard Worker chppTimesyncGetTime(context, rxHeader);
72*84e33947SAndroid Build Coastguard Worker break;
73*84e33947SAndroid Build Coastguard Worker }
74*84e33947SAndroid Build Coastguard Worker default: {
75*84e33947SAndroid Build Coastguard Worker success = false;
76*84e33947SAndroid Build Coastguard Worker }
77*84e33947SAndroid Build Coastguard Worker }
78*84e33947SAndroid Build Coastguard Worker return success;
79*84e33947SAndroid Build Coastguard Worker }
80