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