aboutsummaryrefslogtreecommitdiff
path: root/chpp/app.c
blob: 81aadfdbb2e8f88ec2b16cb1f357bd1273b8acce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "chpp/app.h"

#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "chpp/clients.h"
#include "chpp/clients/discovery.h"
#ifdef CHPP_CLIENT_ENABLED_LOOPBACK
#include "chpp/clients/loopback.h"
#endif
#ifdef CHPP_CLIENT_ENABLED_TIMESYNC
#include "chpp/clients/timesync.h"
#endif
#include "chpp/log.h"
#include "chpp/macros.h"
#include "chpp/notifier.h"
#include "chpp/pal_api.h"
#include "chpp/services.h"
#include "chpp/services/discovery.h"
#include "chpp/services/loopback.h"
#include "chpp/services/nonhandle.h"
#include "chpp/services/timesync.h"
#include "chre_api/chre/common.h"

/************************************************
 *  Prototypes
 ***********************************************/

static bool chppProcessPredefinedClientRequest(struct ChppAppState *context,
                                               uint8_t *buf, size_t len);
static bool chppProcessPredefinedServiceResponse(struct ChppAppState *context,
                                                 uint8_t *buf, size_t len);
static bool chppProcessPredefinedClientNotification(
    struct ChppAppState *context, uint8_t *buf, size_t len);
static bool chppProcessPredefinedServiceNotification(
    struct ChppAppState *context, uint8_t *buf, size_t len);

static bool chppDatagramLenIsOk(struct ChppAppState *context,
                                struct ChppAppHeader *rxHeader, size_t len);
ChppDispatchFunction *chppGetDispatchFunction(struct ChppAppState *context,
                                              uint8_t handle,
                                              enum ChppMessageType type);
ChppNotifierFunction *chppGetClientResetNotifierFunction(
    struct ChppAppState *context, uint8_t index);
ChppNotifierFunction *chppGetServiceResetNotifierFunction(
    struct ChppAppState *context, uint8_t index);
static inline const struct ChppService *chppServiceOfHandle(
    struct ChppAppState *appContext, uint8_t handle);
static inline const struct ChppClient *chppClientOfHandle(
    struct ChppAppState *appContext, uint8_t handle);
static inline void *chppServiceContextOfHandle(struct ChppAppState *appContext,
                                               uint8_t handle);
static inline void *chppClientContextOfHandle(struct ChppAppState *appContext,
                                              uint8_t handle);
static void *chppClientServiceContextOfHandle(struct ChppAppState *appContext,
                                              uint8_t handle,
                                              enum ChppMessageType type);

static void chppProcessPredefinedHandleDatagram(struct ChppAppState *context,
                                                uint8_t *buf, size_t len);
static void chppProcessNegotiatedHandleDatagram(struct ChppAppState *context,
                                                uint8_t *buf, size_t len);

/************************************************
 *  Private Functions
 ***********************************************/

/**
 * Processes a client request that is determined to be for a predefined CHPP
 * service.
 *
 * @param context Maintains status for each app layer instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 *
 * @return False if handle is invalid. True otherwise.
 */
static bool chppProcessPredefinedClientRequest(struct ChppAppState *context,
                                               uint8_t *buf, size_t len) {
  struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
  bool handleValid = true;
  bool dispatchResult = true;

  switch (rxHeader->handle) {
    case CHPP_HANDLE_LOOPBACK: {
      dispatchResult = chppDispatchLoopbackClientRequest(context, buf, len);
      break;
    }

    case CHPP_HANDLE_TIMESYNC: {
      dispatchResult = chppDispatchTimesyncClientRequest(context, buf, len);
      break;
    }

    case CHPP_HANDLE_DISCOVERY: {
      dispatchResult = chppDispatchDiscoveryClientRequest(context, buf, len);
      break;
    }

    default: {
      handleValid = false;
    }
  }

  if (dispatchResult == false) {
    CHPP_LOGE("H#%" PRIu8 " unknown request. cmd=%#x, ID=%" PRIu8,
              rxHeader->handle, rxHeader->command, rxHeader->transaction);
  }

  return handleValid;
}

/**
 * Processes a service response that is determined to be for a predefined CHPP
 * client.
 *
 * @param context Maintains status for each app layer instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 *
 * @return False if handle is invalid. True otherwise.
 */
static bool chppProcessPredefinedServiceResponse(struct ChppAppState *context,
                                                 uint8_t *buf, size_t len) {
  // Possibly unused if compiling without the clients below enabled
  UNUSED_VAR(context);
  UNUSED_VAR(buf);
  UNUSED_VAR(len);

  struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
  bool handleValid = true;
  bool dispatchResult = true;

  switch (rxHeader->handle) {
#ifdef CHPP_CLIENT_ENABLED_LOOPBACK
    case CHPP_HANDLE_LOOPBACK: {
      dispatchResult = chppDispatchLoopbackServiceResponse(context, buf, len);
      break;
    }
#endif  // CHPP_CLIENT_ENABLED_LOOPBACK

#ifdef CHPP_CLIENT_ENABLED_TIMESYNC
    case CHPP_HANDLE_TIMESYNC: {
      dispatchResult = chppDispatchTimesyncServiceResponse(context, buf, len);
      break;
    }
#endif  // CHPP_CLIENT_ENABLED_TIMESYNC

#ifdef CHPP_CLIENT_ENABLED_DISCOVERY
    case CHPP_HANDLE_DISCOVERY: {
      dispatchResult = chppDispatchDiscoveryServiceResponse(context, buf, len);
      break;
    }
#endif  // CHPP_CLIENT_ENABLED_DISCOVERY

    default: {
      handleValid = false;
    }
  }

  if (dispatchResult == false) {
    CHPP_LOGE("H#%" PRIu8 " unknown response. cmd=%#x, ID=%" PRIu8
              ", len=%" PRIuSIZE,
              rxHeader->handle, rxHeader->command, rxHeader->transaction, len);
  }

  return handleValid;
}

/**
 * Processes a client notification that is determined to be for a predefined
 * CHPP service.
 *
 * @param context Maintains status for each app layer instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 *
 * @return False if handle is invalid. True otherwise.
 */
static bool chppProcessPredefinedClientNotification(
    struct ChppAppState *context, uint8_t *buf, size_t len) {
  UNUSED_VAR(context);
  UNUSED_VAR(len);
  UNUSED_VAR(buf);
  // No predefined services support these.
  return false;
}

/**
 * Processes a service notification that is determined to be for a predefined
 * CHPP client.
 *
 * @param context Maintains status for each app layer instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 *
 * @return False if handle is invalid. True otherwise.
 */
static bool chppProcessPredefinedServiceNotification(
    struct ChppAppState *context, uint8_t *buf, size_t len) {
  UNUSED_VAR(context);
  UNUSED_VAR(len);
  UNUSED_VAR(buf);
  // No predefined clients support these.
  return false;
}

/**
 * Verifies if the length of a Rx Datagram from the transport layer is
 * sufficient for the associated service.
 *
 * @param context Maintains status for each app layer instance.
 * @param rxHeader The pointer to the datagram RX header.
 * @param len Length of the datagram in bytes.
 *
 * @return true if length is ok.
 */
static bool chppDatagramLenIsOk(struct ChppAppState *context,
                                struct ChppAppHeader *rxHeader, size_t len) {
  size_t minLen = SIZE_MAX;
  uint8_t handle = rxHeader->handle;

  if (handle < CHPP_HANDLE_NEGOTIATED_RANGE_START) {  // Predefined
    switch (handle) {
      case CHPP_HANDLE_NONE:
        minLen = sizeof_member(struct ChppAppHeader, handle);
        break;

      case CHPP_HANDLE_LOOPBACK:
        minLen = sizeof_member(struct ChppAppHeader, handle) +
                 sizeof_member(struct ChppAppHeader, type);
        break;

      case CHPP_HANDLE_TIMESYNC:
      case CHPP_HANDLE_DISCOVERY:
        minLen = sizeof(struct ChppAppHeader);
        break;

      default:
        // len remains SIZE_MAX
        CHPP_LOGE("Invalid H#%" PRIu8, handle);
    }

  } else {  // Negotiated
    enum ChppMessageType messageType =
        CHPP_APP_GET_MESSAGE_TYPE(rxHeader->type);

    switch (messageType) {
      case CHPP_MESSAGE_TYPE_CLIENT_REQUEST:
      case CHPP_MESSAGE_TYPE_CLIENT_NOTIFICATION: {
        const struct ChppService *service =
            chppServiceOfHandle(context, handle);
        if (service != NULL) {
          minLen = service->minLength;
        }
        break;
      }
      case CHPP_MESSAGE_TYPE_SERVICE_RESPONSE:
      case CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION: {
        const struct ChppClient *client = chppClientOfHandle(context, handle);
        if (client != NULL) {
          minLen = client->minLength;
        }
        break;
      }
      default: {
        break;
      }
    }

    if (minLen == SIZE_MAX) {
      CHPP_LOGE("Invalid type=%d or H#%" PRIu8, messageType, handle);
    }
  }

  if ((len < minLen) && (minLen != SIZE_MAX)) {
    CHPP_LOGE("Datagram len=%" PRIuSIZE " < %" PRIuSIZE " for H#%" PRIu8, len,
              minLen, handle);
  }
  return (len >= minLen) && (minLen != SIZE_MAX);
}

/**
 * Returns the dispatch function of a particular negotiated client/service
 * handle and message type. This shall be null if it is unsupported by the
 * service.
 *
 * @param context Maintains status for each app layer instance.
 * @param handle Handle number for the client/service.
 * @param type Message type.
 *
 * @return Pointer to a function that dispatches incoming datagrams for any
 * particular client/service.
 */
ChppDispatchFunction *chppGetDispatchFunction(struct ChppAppState *context,
                                              uint8_t handle,
                                              enum ChppMessageType type) {
  // chppDatagramLenIsOk() has already confirmed that the handle # is valid.
  // Therefore, no additional checks are necessary for chppClientOfHandle(),
  // chppServiceOfHandle(), or chppClientServiceContextOfHandle().

  switch (CHPP_APP_GET_MESSAGE_TYPE(type)) {
    case CHPP_MESSAGE_TYPE_CLIENT_REQUEST: {
      return chppServiceOfHandle(context, handle)->requestDispatchFunctionPtr;
    }
    case CHPP_MESSAGE_TYPE_SERVICE_RESPONSE: {
      struct ChppClientState *clientState =
          (struct ChppClientState *)chppClientServiceContextOfHandle(
              context, handle, type);
      if (clientState->openState == CHPP_OPEN_STATE_CLOSED) {
        CHPP_LOGE("RX service response but client closed");
        break;
      }
      return chppClientOfHandle(context, handle)->responseDispatchFunctionPtr;
    }
    case CHPP_MESSAGE_TYPE_CLIENT_NOTIFICATION: {
      return chppServiceOfHandle(context, handle)
          ->notificationDispatchFunctionPtr;
    }
    case CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION: {
      struct ChppClientState *clientState =
          (struct ChppClientState *)chppClientServiceContextOfHandle(
              context, handle, type);
      if (clientState->openState == CHPP_OPEN_STATE_CLOSED) {
        CHPP_LOGE("RX service notification but client closed");
        break;
      }
      return chppClientOfHandle(context, handle)
          ->notificationDispatchFunctionPtr;
    }
  }

  return NULL;
}

/**
 * Returns the reset notification function pointer of a particular negotiated
 * client. The function pointer will be set to null by clients that do not need
 * or support a reset notification.
 *
 * @param context Maintains status for each app layer instance.
 * @param index Index of the registered client.
 *
 * @return Pointer to the reset notification function.
 */
ChppNotifierFunction *chppGetClientResetNotifierFunction(
    struct ChppAppState *context, uint8_t index) {
  return context->registeredClients[index]->resetNotifierFunctionPtr;
}

/**
 * Returns the reset function pointer of a particular registered service. The
 * function pointer will be set to null by services that do not need or support
 * a reset notification.
 *
 * @param context Maintains status for each app layer instance.
 * @param index Index of the registered service.
 *
 * @return Pointer to the reset function.
 */
ChppNotifierFunction *chppGetServiceResetNotifierFunction(
    struct ChppAppState *context, uint8_t index) {
  return context->registeredServices[index]->resetNotifierFunctionPtr;
}

/**
 * Returns a pointer to the ChppService struct of the service matched to a
 * negotiated handle. Returns null if a service doesn't exist for the handle.
 *
 * @param context Maintains status for each app layer instance.
 * @param handle Handle number.
 *
 * @return Pointer to the ChppService struct of a particular service handle.
 */
static inline const struct ChppService *chppServiceOfHandle(
    struct ChppAppState *context, uint8_t handle) {
  uint8_t serviceIndex = CHPP_SERVICE_INDEX_OF_HANDLE(handle);
  if (serviceIndex < context->registeredServiceCount) {
    return context->registeredServices[serviceIndex];
  }

  return NULL;
}

/**
 * Returns a pointer to the ChppClient struct of the client matched to a
 * negotiated handle. Returns null if a client doesn't exist for the handle.
 *
 * @param context Maintains status for each app layer instance.
 * @param handle Handle number.
 *
 * @return Pointer to the ChppClient struct matched to a particular handle.
 */
static inline const struct ChppClient *chppClientOfHandle(
    struct ChppAppState *context, uint8_t handle) {
  uint8_t serviceIndex = CHPP_SERVICE_INDEX_OF_HANDLE(handle);
  if (serviceIndex < context->discoveredServiceCount) {
    uint8_t clientIndex = context->clientIndexOfServiceIndex[serviceIndex];
    if (clientIndex < context->registeredClientCount) {
      return context->registeredClients[clientIndex];
    }
  }

  return NULL;
}

/**
 * Returns a pointer to the service struct of a particular negotiated service
 * handle.
 * It is up to the caller to ensure the handle number is valid.
 *
 * @param context Maintains status for each app layer instance.
 * @param handle Handle number for the service.
 *
 * @return Pointer to the context struct of the service.
 */
static inline void *chppServiceContextOfHandle(struct ChppAppState *context,
                                               uint8_t handle) {
  CHPP_DEBUG_ASSERT(CHPP_SERVICE_INDEX_OF_HANDLE(handle) <
                    context->registeredServiceCount);
  return context
      ->registeredServiceContexts[CHPP_SERVICE_INDEX_OF_HANDLE(handle)];
}

/**
 * Returns a pointer to the client struct of a particular negotiated client
 * handle.
 * It is up to the caller to ensure the handle number is valid.
 *
 * @param context Maintains status for each app layer instance.
 * @param handle Handle number for the service.
 *
 * @return Pointer to the ChppService struct of the client.
 */
static inline void *chppClientContextOfHandle(struct ChppAppState *context,
                                              uint8_t handle) {
  CHPP_DEBUG_ASSERT(CHPP_SERVICE_INDEX_OF_HANDLE(handle) <
                    context->registeredClientCount);
  return context
      ->registeredClientContexts[context->clientIndexOfServiceIndex
                                     [CHPP_SERVICE_INDEX_OF_HANDLE(handle)]];
}

/**
 * Returns a pointer to the client/service struct of a particular negotiated
 * client/service handle.
 * It is up to the caller to ensure the handle number is valid.
 *
 * @param appContext Maintains status for each app layer instance.
 * @param handle Handle number for the service.
 * @param type Message type (indicates if this is for a client or service).
 *
 * @return Pointer to the client/service struct of the service handle.
 */
static void *chppClientServiceContextOfHandle(struct ChppAppState *appContext,
                                              uint8_t handle,
                                              enum ChppMessageType type) {
  switch (CHPP_APP_GET_MESSAGE_TYPE(type)) {
    case CHPP_MESSAGE_TYPE_CLIENT_REQUEST:
    case CHPP_MESSAGE_TYPE_CLIENT_NOTIFICATION: {
      return chppServiceContextOfHandle(appContext, handle);
      break;
    }
    case CHPP_MESSAGE_TYPE_SERVICE_RESPONSE:
    case CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION: {
      return chppClientContextOfHandle(appContext, handle);
      break;
    }
    default: {
      CHPP_LOGE("Unknown type=0x%" PRIx8 " (H#%" PRIu8 ")", type, handle);
      return NULL;
    }
  }
}

/**
 * Processes a received datagram that is determined to be for a predefined CHPP
 * service. Responds with an error if unsuccessful.
 *
 * @param context Maintains status for each app layer instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 */
static void chppProcessPredefinedHandleDatagram(struct ChppAppState *context,
                                                uint8_t *buf, size_t len) {
  struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
  bool success = true;

  switch (CHPP_APP_GET_MESSAGE_TYPE(rxHeader->type)) {
    case CHPP_MESSAGE_TYPE_CLIENT_REQUEST: {
      success = chppProcessPredefinedClientRequest(context, buf, len);
      break;
    }
    case CHPP_MESSAGE_TYPE_CLIENT_NOTIFICATION: {
      success = chppProcessPredefinedClientNotification(context, buf, len);
      break;
    }
    case CHPP_MESSAGE_TYPE_SERVICE_RESPONSE: {
      success = chppProcessPredefinedServiceResponse(context, buf, len);
      break;
    }
    case CHPP_MESSAGE_TYPE_SERVICE_NOTIFICATION: {
      success = chppProcessPredefinedServiceNotification(context, buf, len);
      break;
    }
    default: {
      success = false;
    }
  }

  if (success == false) {
    CHPP_LOGE("H#%" PRIu8 " undefined msg type=0x%" PRIx8 " (len=%" PRIuSIZE
              ", ID=%" PRIu8 ")",
              rxHeader->handle, rxHeader->type, len, rxHeader->transaction);
    chppEnqueueTxErrorDatagram(context->transportContext,
                               CHPP_TRANSPORT_ERROR_APPLAYER);
  }
}

/**
 * Processes a received datagram that is determined to be for a negotiated CHPP
 * client or service. Responds with an error if unsuccessful.
 *
 * @param context Maintains status for each app layer instance.
 * @param buf Input data. Cannot be null.
 * @param len Length of input data in bytes.
 */
static void chppProcessNegotiatedHandleDatagram(struct ChppAppState *context,
                                                uint8_t *buf, size_t len) {
  struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;
  enum ChppMessageType messageType = CHPP_APP_GET_MESSAGE_TYPE(rxHeader->type);

  void *clientServiceContext =
      chppClientServiceContextOfHandle(context, rxHeader->handle, messageType);
  if (clientServiceContext == NULL) {
    CHPP_LOGE("H#%" PRIu8 " missing ctx (msg=0x%" PRIx8 " len=%" PRIuSIZE
              ", ID=%" PRIu8 ")",
              rxHeader->handle, rxHeader->type, len, rxHeader->transaction);
    chppEnqueueTxErrorDatagram(context->transportContext,
                               CHPP_TRANSPORT_ERROR_APPLAYER);
    CHPP_DEBUG_ASSERT(false);
    return;
  }

  ChppDispatchFunction *dispatchFunc =
      chppGetDispatchFunction(context, rxHeader->handle, messageType);
  if (dispatchFunc == NULL) {
    CHPP_LOGE("H#%" PRIu8 " unsupported msg=0x%" PRIx8 " (len=%" PRIuSIZE
              ", ID=%" PRIu8 ")",
              rxHeader->handle, rxHeader->type, len, rxHeader->transaction);
    chppEnqueueTxErrorDatagram(context->transportContext,
                               CHPP_TRANSPORT_ERROR_APPLAYER);
    return;
  }

  // All good. Dispatch datagram and possibly notify a waiting client
  enum ChppAppErrorCode error = dispatchFunc(clientServiceContext, buf, len);

  if (error != CHPP_APP_ERROR_NONE) {
    CHPP_LOGE("RX dispatch err=0x%" PRIx16 " H#%" PRIu8 " type=0x%" PRIx8
              " ID=%" PRIu8 " cmd=0x%" PRIx16 " len=%" PRIuSIZE,
              error, rxHeader->handle, rxHeader->type, rxHeader->transaction,
              rxHeader->command, len);

    // Only client requests require a dispatch failure response.
    if (messageType == CHPP_MESSAGE_TYPE_CLIENT_REQUEST) {
      struct ChppAppHeader *response =
          chppAllocServiceResponseFixed(rxHeader, struct ChppAppHeader);
      if (response == NULL) {
        CHPP_LOG_OOM();
      } else {
        response->error = (uint8_t)error;
        chppEnqueueTxDatagramOrFail(context->transportContext, response,
                                    sizeof(*response));
      }
    }
    return;
  }

  if (messageType == CHPP_MESSAGE_TYPE_SERVICE_RESPONSE) {
    // Datagram is a service response. Check for synchronous operation and
    // notify waiting client if needed.

    struct ChppClientState *clientState =
        (struct ChppClientState *)clientServiceContext;
    chppMutexLock(&clientState->responseMutex);
    clientState->responseReady = true;
    CHPP_LOGD(
        "Finished dispatching a service response. Notifying a potential "
        "synchronous client");
    chppConditionVariableSignal(&clientState->responseCondVar);
    chppMutexUnlock(&clientState->responseMutex);
  }
}

/************************************************
 *  Public Functions
 ***********************************************/

void chppAppInit(struct ChppAppState *appContext,
                 struct ChppTransportState *transportContext) {
  // Default initialize all service/clients
  struct ChppClientServiceSet set;
  memset(&set, 0xff, sizeof(set));  // set all bits to 1

  chppAppInitWithClientServiceSet(appContext, transportContext, set);
}

void chppAppInitWithClientServiceSet(
    struct ChppAppState *appContext,
    struct ChppTransportState *transportContext,
    struct ChppClientServiceSet clientServiceSet) {
  CHPP_NOT_NULL(appContext);

  CHPP_LOGD("App Init");

  memset(appContext, 0, sizeof(*appContext));

  appContext->clientServiceSet = clientServiceSet;
  appContext->transportContext = transportContext;
  appContext->nextRequestTimeoutNs = CHPP_TIME_MAX;

  chppPalSystemApiInit(appContext);

#ifdef CHPP_SERVICE_ENABLED
  chppRegisterCommonServices(appContext);
#endif

#ifdef CHPP_CLIENT_ENABLED
  chppRegisterCommonClients(appContext);
  chppInitBasicClients(appContext);
#endif
}

void chppAppDeinit(struct ChppAppState *appContext) {
  CHPP_LOGD("App deinit");

#ifdef CHPP_CLIENT_ENABLED
  chppDeinitMatchedClients(appContext);
  chppDeinitBasicClients(appContext);
  chppDeregisterCommonClients(appContext);
#endif

#ifdef CHPP_SERVICE_ENABLED
  chppDeregisterCommonServices(appContext);
#endif

  chppPalSystemApiDeinit(appContext);
}

void chppAppProcessRxDatagram(struct ChppAppState *context, uint8_t *buf,
                              size_t len) {
  struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;

  if (len == 0) {
    CHPP_DEBUG_ASSERT_LOG(false, "App rx w/ len 0");

  } else if (len < sizeof(struct ChppAppHeader)) {
    uint8_t *handle = (uint8_t *)buf;
    CHPP_LOGD("RX datagram len=%" PRIuSIZE " H#%" PRIu8, len, *handle);

  } else if (rxHeader->error != CHPP_APP_ERROR_NONE) {
    CHPP_LOGE("RX datagram len=%" PRIuSIZE " H#%" PRIu8 " type=0x%" PRIx8
              " ID=%" PRIu8 " ERR=%" PRIu8 " cmd=0x%" PRIx16,
              len, rxHeader->handle, rxHeader->type, rxHeader->transaction,
              rxHeader->error, rxHeader->command);
  } else {
    CHPP_LOGD("RX datagram len=%" PRIuSIZE " H#%" PRIu8 " type=0x%" PRIx8
              " ID=%" PRIu8 " err=%" PRIu8 " cmd=0x%" PRIx16,
              len, rxHeader->handle, rxHeader->type, rxHeader->transaction,
              rxHeader->error, rxHeader->command);
  }

  if (!chppDatagramLenIsOk(context, rxHeader, len)) {
    chppEnqueueTxErrorDatagram(context->transportContext,
                               CHPP_TRANSPORT_ERROR_APPLAYER);

  } else {
    if (rxHeader->handle == CHPP_HANDLE_NONE) {
      chppDispatchNonHandle(context, buf, len);

    } else if (rxHeader->handle < CHPP_HANDLE_NEGOTIATED_RANGE_START) {
      chppProcessPredefinedHandleDatagram(context, buf, len);

    } else {
      chppProcessNegotiatedHandleDatagram(context, buf, len);
    }
  }

  chppDatagramProcessDoneCb(context->transportContext, buf);
}

void chppAppProcessReset(struct ChppAppState *context) {
#ifdef CHPP_CLIENT_ENABLED_DISCOVERY
  if (!context->isDiscoveryComplete) {
    chppInitiateDiscovery(context);

  } else {
    // Notify matched clients that a reset happened
    for (uint8_t i = 0; i < context->discoveredServiceCount; i++) {
      uint8_t clientIndex = context->clientIndexOfServiceIndex[i];
      if (clientIndex != CHPP_CLIENT_INDEX_NONE) {
        // Discovered service has a matched client
        ChppNotifierFunction *ResetNotifierFunction =
            chppGetClientResetNotifierFunction(context, clientIndex);

        CHPP_LOGD("Client #%" PRIu8 " (H#%d) reset notifier found=%d",
                  clientIndex, CHPP_SERVICE_HANDLE_OF_INDEX(i),
                  (ResetNotifierFunction != NULL));

        if (ResetNotifierFunction != NULL) {
          ResetNotifierFunction(context->registeredClientContexts[clientIndex]);
        }
      }
    }
  }
#endif  // CHPP_CLIENT_ENABLED_DISCOVERY

  // Notify registered services that a reset happened
  for (uint8_t i = 0; i < context->registeredServiceCount; i++) {
    ChppNotifierFunction *ResetNotifierFunction =
        chppGetServiceResetNotifierFunction(context, i);

    CHPP_LOGD("Service #%" PRIu8 " (H#%d) reset notifier found=%d", i,
              CHPP_SERVICE_HANDLE_OF_INDEX(i), (ResetNotifierFunction != NULL));

    if (ResetNotifierFunction != NULL) {
      ResetNotifierFunction(context->registeredServiceContexts[i]);
    }
  }

#ifdef CHPP_CLIENT_ENABLED_TIMESYNC
  // Reinitialize time offset
  chppTimesyncClientReset(context);
#endif
}

void chppUuidToStr(const uint8_t uuid[CHPP_SERVICE_UUID_LEN],
                   char strOut[CHPP_SERVICE_UUID_STRING_LEN]) {
  snprintf(
      strOut, CHPP_SERVICE_UUID_STRING_LEN,
      "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
      uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
      uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14],
      uuid[15]);
}

uint8_t chppAppErrorToChreError(uint8_t chppError) {
  switch (chppError) {
    case CHPP_APP_ERROR_NONE:
    case CHPP_APP_ERROR_INVALID_ARG:
    case CHPP_APP_ERROR_BUSY:
    case CHPP_APP_ERROR_OOM:
    case CHPP_APP_ERROR_UNSUPPORTED:
    case CHPP_APP_ERROR_TIMEOUT:
    case CHPP_APP_ERROR_DISABLED:
    case CHPP_APP_ERROR_RATELIMITED: {
      // CHRE and CHPP error values are identical in these cases
      return chppError;
    }
    default: {
      return CHRE_ERROR;
    }
  }
}

uint8_t chppAppShortResponseErrorHandler(uint8_t *buf, size_t len,
                                         const char *responseName) {
  CHPP_ASSERT(len >= sizeof(struct ChppAppHeader));
  struct ChppAppHeader *rxHeader = (struct ChppAppHeader *)buf;

  if (rxHeader->error == CHPP_APP_ERROR_NONE) {
    CHPP_LOGE("%s resp short len=%" PRIuSIZE, responseName, len);
    return CHRE_ERROR;
  }

  CHPP_LOGD("%s resp short len=%" PRIuSIZE, responseName, len);
  return chppAppErrorToChreError(rxHeader->error);
}