summaryrefslogtreecommitdiffstats
path: root/stack/obx/hdrs/obx_gen.c
blob: 9b25e04b43d867b0ef69a12737ee3e00595acb5a (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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/*****************************************************************************
**
**  Name:         obx_gen.c
**
**  File:         OBEX Headers Utility functions
**                - common/generic functions
**
**  Copyright (c) 2003-2011, Broadcom Corp., All Rights Reserved.
**  Broadcom Bluetooth Core. Proprietary and confidential.
**
*****************************************************************************/
#include <string.h>

#include "bt_target.h"
#include "wcassert.h"
#include "obx_api.h"
#include "gki.h"
#include "obx_int.h"

const UINT8 obx_hdr_start_offset[] =
{
    OBX_CONN_HDRS_OFFSET,       /* OBX_CONNECT_REQ_EVT */
    OBX_SESS_HDRS_OFFSET,       /* OBX_SESSION_REQ_EVT */
    OBX_DISCON_HDRS_OFFSET,     /* OBX_DISCONNECT_REQ_EVT */
    OBX_PUT_HDRS_OFFSET,        /* OBX_PUT_REQ_EVT */
    OBX_GET_HDRS_OFFSET,        /* OBX_GET_REQ_EVT */
    OBX_SETPATH_REQ_HDRS_OFFSET,/* OBX_SETPATH_REQ_EVT */
    OBX_ABORT_HDRS_OFFSET,      /* OBX_ABORT_REQ_EVT */
    OBX_ACTION_HDRS_OFFSET,     /* OBX_ACTION_REQ_EVT */
    OBX_CONN_HDRS_OFFSET,       /* OBX_CONNECT_RSP_EVT */
    OBX_RESPONSE_HDRS_OFFSET,   /* OBX_SESSION_RSP_EVT */
    OBX_RESPONSE_HDRS_OFFSET,   /* OBX_DISCONNECT_RSP_EVT */
    OBX_RESPONSE_HDRS_OFFSET,   /* OBX_PUT_RSP_EVT */
    OBX_RESPONSE_HDRS_OFFSET,   /* OBX_GET_RSP_EVT */
    OBX_RESPONSE_HDRS_OFFSET,   /* OBX_SETPATH_RSP_EVT */
    OBX_RESPONSE_HDRS_OFFSET,   /* OBX_ABORT_RSP_EVT */
    OBX_RESPONSE_HDRS_OFFSET    /* OBX_ACTION_RSP_EVT */
};

/*******************************************************************************
**
** Function     obx_access_rsp_code
**
** Description  This function is used to read/change response code
**
** Returns      void.
**
*******************************************************************************/
void obx_access_rsp_code(BT_HDR *p_pkt, UINT8 *p_rsp_code)
{
    UINT8   *p = (UINT8 *)(p_pkt + 1) + p_pkt->offset;
    if(*p_rsp_code == OBX_RSP_DEFAULT)
        *p_rsp_code = *p;
    else
        *p = *p_rsp_code;
}

/*******************************************************************************
**
** Function     obx_adjust_packet_len
**
** Description  Adjust the packet length in the OBEX packet
**
** Returns      void.
**
*******************************************************************************/
void obx_adjust_packet_len(BT_HDR *p_pkt)
{
    UINT8   *p = (UINT8 *)(p_pkt + 1) + p_pkt->offset + 1;
    UINT16_TO_BE_STREAM(p, p_pkt->len);
}

/*******************************************************************************
**
** Function     obx_read_header_len
**
** Description  ph is the beginning of an OBEX header
**
** Returns      total length of the header
**
*******************************************************************************/
UINT16 obx_read_header_len(UINT8 *ph)
{
    UINT16  len = 0;

    /*
    OBX_TRACE_DEBUG1( "obx_read_header_len: 0x%x", *ph);
    */
    switch(*ph&OBX_HI_TYPE_MASK)
    {
    case OBX_HI_TYPE_BYTE:
        len = 2;
        break;
    case OBX_HI_TYPE_INT:
        len = 5;
        break;
    case OBX_HI_TYPE_ARRAY:
    case OBX_HI_TYPE_UNIC:
        ph++;
        BE_STREAM_TO_UINT16(len, ph);
        break;
    }
    /*
    OBX_TRACE_DEBUG1( "len:%d", len);
    */
    return len;
}

/*******************************************************************************
**
** Function     obx_dup_pkt
**
** Description  This function duplicate the OBEX message
**
** Returns      BT_HDR *.
**
*******************************************************************************/
BT_HDR * obx_dup_pkt(BT_HDR *p_pkt)
{
    BT_HDR *p_new;
    UINT16 size = p_pkt->len + p_pkt->offset + BT_HDR_SIZE;

    if (size < GKI_MAX_BUF_SIZE)
    {
        /* Use the largest general pool to allow challenge tags appendage */
        p_new = (BT_HDR *)GKI_getbuf(GKI_MAX_BUF_SIZE);
    }
    else
    {
        p_new = (BT_HDR *) GKI_getpoolbuf(OBX_LRG_DATA_POOL_ID);
    }

    if (p_new)
        memcpy(p_new, p_pkt, size );

    return p_new;
}


/*******************************************************************************
**
** Function     OBX_HdrInit
**
** Description  This function is called to initialize an OBEX packet. This
**              function takes a GKI buffer and sets the offset in BT_HDR as
**              OBX_HDR_OFFSET, the len as 0. The layer_specific is set to the
**              length still available. This function compares the given
**              (pkt_size - BT_HDR_SIZE) with the peer MTU to get the lesser
**              of the two and set the layer_specific to
**              (lesser_size - OBX_HDR_OFFSET).
**              If composing a header for the CONNECT request (there is no
**              client handle yet), use OBX_HANDLE_NULL as the handle.
**
**              If the pkt_size is larger than the largest public pool size,
**              GKI_MAX_BUF_SIZE, then an attempt to grab a buffer from the reserved OBX 
**              data pool will be made.
** Returns      BT_HDR *.
**
*******************************************************************************/
BT_HDR *OBX_HdrInit(tOBX_HANDLE handle, UINT16 pkt_size)
{
    UINT16  mtu     = OBX_HandleToMtu(handle);
    BT_HDR  *p_pkt = NULL;
    UINT16  buf_size;
#if (BT_USE_TRACES == TRUE)
    UINT16  req_size = pkt_size;
#endif

    WC_ASSERT(pkt_size > (BT_HDR_SIZE + OBX_HDR_OFFSET));

    pkt_size -= BT_HDR_SIZE;
    if(pkt_size > mtu )
        pkt_size = mtu;
    pkt_size    += (BT_HDR_SIZE + OBX_HDR_OFFSET);

    OBX_TRACE_DEBUG4( "OBX_HdrInit: checking req_size %d, pkt_size:%d, max:%d, offset:%d",
        req_size, pkt_size, GKI_MAX_BUF_SIZE, OBX_HDR_OFFSET);
    /* See if packet will fit in regular public pool */
    if ((pkt_size) < GKI_MAX_BUF_SIZE)
    {
        p_pkt = (BT_HDR *) GKI_getbuf(pkt_size);
    }
    else    /* Must use the reserved OBX buffer pool */
    {
        p_pkt = (BT_HDR *) GKI_getpoolbuf(OBX_LRG_DATA_POOL_ID);
        if (!p_pkt)
        {
            OBX_TRACE_DEBUG1( "Out of Large buffers. Trying pkt_size:%d", GKI_MAX_BUF_SIZE);
            p_pkt = (BT_HDR *) GKI_getbuf(GKI_MAX_BUF_SIZE);
        }
    }

    if(p_pkt)
    {
        buf_size    = GKI_get_buf_size(p_pkt);
        buf_size    -= BT_HDR_SIZE;
        if(buf_size > mtu)
            buf_size = mtu;

        OBX_TRACE_DEBUG4( "OBX_HdrInit: req_size %d, pkt_size = %d, gki_size %d, buf_size %d",
                            req_size, pkt_size, GKI_get_buf_size(p_pkt), buf_size);

        p_pkt->offset   = OBX_HDR_OFFSET;
        p_pkt->len      = 0;
        p_pkt->event    = 0;

        /* layer specific contains remaining space in packet */
        p_pkt->layer_specific = buf_size - OBX_HDR_OFFSET ;
        p_pkt->layer_specific -= 2;

        OBX_TRACE_DEBUG2( "buf size: %d, ls:%d", buf_size, p_pkt->layer_specific);
    }
    else
    {
        OBX_TRACE_ERROR1("OBX_HdrInit: No buffers for size (%d)", pkt_size);
    }

    return p_pkt;
}


/*******************************************************************************
**
** Function     OBX_Add1ByteHdr
**
** Description  This function is called to add a header with type as UINT8
**              to an OBEX packet.
**
** Returns      TRUE, if the header is added successfully.
**              FALSE, if the operation failed. p_pkt is not altered.
**
*******************************************************************************/
BOOLEAN OBX_Add1ByteHdr(BT_HDR *p_pkt, UINT8 id, UINT8 data)
{
    UINT8   *p;
    BOOLEAN status = FALSE;
    UINT16  size = 2;    /* total length added by this header - 1/hi & 1/hv */

    if(p_pkt)
    {
        p = (UINT8 *)(p_pkt+1)+p_pkt->offset+p_pkt->len;
        /* verify that the HI is of correct type and the remaining length in the packet is good */
        if( ((id & OBX_HI_TYPE_MASK) == OBX_HI_TYPE_BYTE) && (p_pkt->layer_specific >= size) )
        {
            *p++        = id;
            *p++        = data;

            p_pkt->len  += size;
            p_pkt->layer_specific   -= size;
            status = TRUE;
        }
    }

    return status;
}

/*******************************************************************************
**
** Function     OBX_Add4ByteHdr
**
** Description  This function is called to add a header with type as UINT32
**              to an OBEX packet.
**
** Returns      TRUE, if the header is added successfully.
**              FALSE, if the operation failed. p_pkt is not altered.
**
*******************************************************************************/
BOOLEAN OBX_Add4ByteHdr(BT_HDR *p_pkt, UINT8 id, UINT32 data)
{
    UINT8   *p;
    BOOLEAN status = FALSE;
    UINT16  size = 5;    /* total length added by this header - 1/hi & 4/hv */

    if(p_pkt)
    {
        p = (UINT8 *)(p_pkt+1)+p_pkt->offset+p_pkt->len;
        /* verify that the HI is of correct type and the remaining length in the packet is good */
        if( ((id & OBX_HI_TYPE_MASK) == OBX_HI_TYPE_INT) && (p_pkt->layer_specific >= size) )
        {
            *p++        = id;
            UINT32_TO_BE_STREAM(p, data);

            p_pkt->len  += size;
            p_pkt->layer_specific   -= size;
            status = TRUE;
        }
    }

    return status;
}

/*******************************************************************************
**
** Function     OBX_AddByteStrStart
**
** Description  This function is called to get the address to the beginning of
**              the byte sequence for an OBEX header in an OBEX packet.
**
** Returns      The address to add the byte sequence.
**
*******************************************************************************/
UINT8 *OBX_AddByteStrStart(BT_HDR *p_pkt, UINT16 *p_len)
{
    UINT8 *p = (UINT8 *)(p_pkt + 1) + p_pkt->offset + p_pkt->len + 3;

    WC_ASSERT(p_len);

    if(*p_len > (p_pkt->layer_specific - 3) || *p_len == 0)
        *p_len = p_pkt->layer_specific - 3;
    return p;
}

/*******************************************************************************
**
** Function     OBX_AddByteStrHdr
**
** Description  This function is called to add a header with type as byte sequence
**              to an OBEX packet.
**
** Returns      TRUE, if the header is added successfully.
**              FALSE, if the operation failed. p_pkt is not altered.
**
*******************************************************************************/
BOOLEAN OBX_AddByteStrHdr(BT_HDR *p_pkt, UINT8 id, UINT8 *p_data, UINT16 len)
{
    UINT8   *p;
    BOOLEAN status = FALSE;
    UINT16  size = (len+3); /* total length added by this header - 1/hi & len+2/hv */

    if(p_pkt)
    {
        p = (UINT8 *)(p_pkt+1)+p_pkt->offset+p_pkt->len;
        /* verify that the HI is of correct type and the remaining length in the packet is good */
        if( ((id & OBX_HI_TYPE_MASK) == OBX_HI_TYPE_ARRAY) && (p_pkt->layer_specific >= size) )
        {
            *p++        = id;
            UINT16_TO_BE_STREAM(p, size);
            if(p_data)
                memcpy(p, p_data, len);

            p_pkt->len  += size;
            p_pkt->layer_specific   -= size;
            status = TRUE;
        }
    }

    return status;
}

/*******************************************************************************
**
** Function     OBX_AddUnicodeHdr
**
** Description  This function is called to add a header with type as Unicode string
**              to an OBEX packet.
**
** Returns      TRUE, if the header is added successfully.
**              FALSE, if the operation failed. p_pkt is not altered.
**
*******************************************************************************/
BOOLEAN OBX_AddUnicodeHdr(BT_HDR *p_pkt, UINT8 id, UINT16 *p_data, UINT16 len)
{
    UINT8   *p;
    BOOLEAN status = FALSE;
    UINT16  size, xx;

    if(p_pkt)
    {
        p = (UINT8 *)(p_pkt+1)+p_pkt->offset+p_pkt->len;
        size = (len*OBX_UNICODE_SIZE + 3); /* total length added by this header - 1/hi & len*OBX_UNICODE_SIZE+2/hv */
        OBX_TRACE_DEBUG4( "OBX_AddUnicodeHdr len: %d, size: %d, left: %d, id: 0x%x",
            len, size, p_pkt->layer_specific, id );

        /* verify that the HI is of correct type and the remaining length in the packet is good */
        if( ((id & OBX_HI_TYPE_MASK) == OBX_HI_TYPE_UNIC) && (p_pkt->layer_specific >= size) )
        {
            *p++        = id;
            UINT16_TO_BE_STREAM(p, size);
            for(xx=0; xx<len; xx++)
            {
                UINT16_TO_BE_STREAM(p, *p_data);
                p_data++;
            }

            p_pkt->len  += size;
            p_pkt->layer_specific   -= size;
            status = TRUE;
        }
    }

    return status;
}

/* Alternate Body header functions: for non-blocking scenario */
/*******************************************************************************
**
** Function     OBX_AddBodyStart
**
** Description  This function is called to get the address to the beginning of
**              the byte sequence for an OBEX body header in an OBEX packet.
**
** Returns      The address to add body content.
**
*******************************************************************************/
UINT8 *OBX_AddBodyStart(BT_HDR *p_pkt, UINT16 *p_len)
{
    UINT8 *p = (UINT8 *)(p_pkt + 1) + p_pkt->offset + p_pkt->len + 3;

    WC_ASSERT(p_len);

    if(*p_len > (p_pkt->layer_specific - 3) || *p_len == 0)
        *p_len = p_pkt->layer_specific - 3;
    return p;
}

/*******************************************************************************
**
** Function     OBX_AddBodyEnd
**
** Description  This function is called to add the HI and the length of HV of an
**              OBEX body header to an OBEX packet. If end is TRUE, HI is
**              OBX_HI_BODY_END. If FALSE, HI is OBX_HI_BODY. It is assumed that
**              the actual value of the body has been copied into the OBEX packet. 
**
** Returns      void
**
*******************************************************************************/
void OBX_AddBodyEnd(BT_HDR *p_pkt, UINT8 *p, UINT16 len, BOOLEAN end)
{
    UINT8 id = (end)?OBX_HI_BODY_END:OBX_HI_BODY;
    UINT8 *pb = (UINT8 *)(p_pkt + 1) + p_pkt->offset + p_pkt->len;
    *pb++   = id;
    len     += 3; /* 1/hi, 2/hv_len */
    UINT16_TO_BE_STREAM(pb, len);
    p_pkt->layer_specific   -= len;
    p_pkt->len              += len;
}

/*******************************************************************************
**
** Function     OBX_AddTriplet
**
** Description  This function is called to add a header with type as byte sequence
**              to an OBEX packet.
**
** Note:        The byte sequence uses a Tag-Length-Value encoding scheme
**              These headers include: Application Parameters header
**                                     Authenticate Challenge header
**                                     Authenticate Response header
**
** Returns      TRUE, if the header is added successfully.
**              FALSE, if the operation failed. p_pkt is not altered.
**
*******************************************************************************/
BOOLEAN OBX_AddTriplet(BT_HDR *p_pkt, UINT8 id, tOBX_TRIPLET *p_triplet, UINT8 num)
{
    UINT8   *p = (UINT8 *)(p_pkt+1)+p_pkt->offset+p_pkt->len;
    BOOLEAN status = FALSE;
    UINT16  size = 3;/* 1/hi & len+2/hv */
    UINT8   xx;

    /* calculate the total length added by this header */
    for(xx=0; xx< num; xx++)
        size += (p_triplet[xx].len + 2);

    /* verify that the HI is of correct type and the remaining length in the packet is good */
    if( ((id & OBX_HI_TYPE_MASK) == OBX_HI_TYPE_ARRAY) && (p_pkt->layer_specific >= size) )
    {
        *p++    = id;
        UINT16_TO_BE_STREAM(p, size);
        for(xx=0; xx< num; xx++)
        {
            *p++    = p_triplet[xx].tag;
            *p++    = p_triplet[xx].len;
            memcpy(p, p_triplet[xx].p_array, p_triplet[xx].len);
            p       += p_triplet[xx].len;
        }
        p_pkt->len  += size;
        p_pkt->layer_specific   -= size;
        status = TRUE;
    }

    return status;
}


/*******************************************************************************
**
** Function     OBX_CheckNext
**
** Description  This function is called to check if the given OBEX packet
**              contains the specified header.
**
** Returns      NULL, if the header is not in the OBEX packet.
**              The pointer to the specified header beginning from HI.
**
*******************************************************************************/
UINT8 * OBX_CheckNext(BT_HDR *p_pkt, UINT8 *p_start, UINT8 id)
{
    UINT8   *p;
    UINT8   *p_res = NULL;
    UINT16  len, start, skip;
    int     remain;

    if(p_pkt != NULL && p_start != NULL)
    {
        p = (UINT8 *)(p_pkt+1)+p_pkt->offset;
        if(p_pkt->event <= OBX_MAX_OFFSET_IND)
        {
            start = obx_hdr_start_offset[p_pkt->event-1];
            p++;
            BE_STREAM_TO_UINT16(len, p);
            remain  = len - start;
            p   = p - 3 + start;

            while(remain >0)
            {
                if(*p != id || p < p_start)
                {
                    skip = obx_read_header_len(p);
                    p       += skip;
                    /* Just in case this is a bad packet, make sure that remain is >= 0 */
                    if(skip && (remain > skip))
                        remain  -= skip;
                    else
                        remain = 0;
                }
                else
                {
                    p_res = p;
                    break;
                }
            }
        }
    }
    
    if (p_pkt)
    {
        OBX_TRACE_DEBUG2( "OBX_CheckNext: remain: %d len:%d", remain, p_pkt->len);
    }

    return p_res;
}


/*******************************************************************************
**
** Function     OBX_CheckHdr
**
** Description  This function is called to check if the given OBEX packet
**              contains the specified header.
**
** Returns      NULL, if the header is not in the OBEX packet.
**              The pointer to the specified header beginning from HI.
**
*******************************************************************************/
UINT8 * OBX_CheckHdr(BT_HDR *p_pkt, UINT8 id)
{ 
    UINT8   *p;
    UINT8   *p_res = NULL;
    UINT16  len, start, skip;
    int     remain;

    if(p_pkt != NULL)
    {
        p = (UINT8 *)(p_pkt+1)+p_pkt->offset;
        if(p_pkt->event <= OBX_MAX_OFFSET_IND)
        {
            start = obx_hdr_start_offset[p_pkt->event-1];
            p++;
            BE_STREAM_TO_UINT16(len, p);
            remain  = len - start;
            p   = p - 3 + start;

            while(remain >0)
            {
                if(*p != id)
                {
                    skip = obx_read_header_len(p);
                    p       += skip;
                    /* Just in case this is a bad packet, make sure that remain is >= 0 */
                    if(skip && (remain > skip))
                        remain  -= skip;
                    else
                        remain = 0;
                }
                else
                {
                    p_res = p;
                    break;
                }
            }
        }
    }

    return p_res;
}

/*******************************************************************************
**
** Function     OBX_ReadNumHdrs
**
** Description  This function is called to check the number of headers in the 
**              given OBEX packet
**
** Returns      number of headers.
**
*******************************************************************************/
UINT8 OBX_ReadNumHdrs(BT_HDR *p_pkt, UINT8 *p_num_body)
{
    UINT8   num_hdrs = 0, num_body = 0;
    UINT8   *p;
    UINT16  len, start, skip;
    int     remain = 0;

    if(p_pkt != NULL)
    {
        p = (UINT8 *)(p_pkt+1)+p_pkt->offset;
        if(p_pkt->event == 0)
        {
            /* GKI buffer just went through OBX_HdrInit; not processed by the state machine yet */
            remain = len = p_pkt->len;
        }
        else if(p_pkt->event <= OBX_MAX_OFFSET_IND)
        {
            start = obx_hdr_start_offset[p_pkt->event-1];
            p++;
            BE_STREAM_TO_UINT16(len, p);
            remain  = len - start;
            p   = p - 3 + start;
        }

        while(remain >0)
        {
            num_hdrs++;
            if(*p == OBX_HI_BODY || *p == OBX_HI_BODY_END)
                num_body++;

            skip = obx_read_header_len(p);
            p       += skip;
            /* Just in case this is a bad packet, make sure that remain is >= 0 */
            if(skip && (remain > skip))
                remain  -= skip;
            else
                remain = 0;

        }
    }
    if (p_num_body)
        *p_num_body = num_body;
    return num_hdrs;
}

/*******************************************************************************
**
** Function     OBX_ReadHdrLen
**
** Description  This function is called to check the length of the specified
**              header in the given OBEX packet.
**
** Returns      OBX_INVALID_HDR_LEN, if the header is not in the OBEX packet.
**              Otherwise the actual length of the header.
**
*******************************************************************************/
UINT16 OBX_ReadHdrLen(BT_HDR *p_pkt, UINT8 id)
{
    UINT8   *p;
    UINT16  len = OBX_INVALID_HDR_LEN;
 
    if( (p = OBX_CheckHdr(p_pkt, id)) != NULL)
        len = obx_read_header_len(p);
 
    return len;
}

/*******************************************************************************
**
** Function     OBX_Read1ByteHdr
**
** Description  This function is called to get the UINT8 HV of the given HI 
**              in the given OBEX packet.
**
** Returns      TRUE, if the header is in the OBEX packet.
**              FALSE, otherwise.
**
*******************************************************************************/
BOOLEAN OBX_Read1ByteHdr(BT_HDR *p_pkt, UINT8 id, UINT8 *p_data)
{
    BOOLEAN status = FALSE;
    UINT8   *p_start = OBX_CheckHdr(p_pkt, id);

    if(p_start)
    {
        *p_data = *(++p_start);
        status = TRUE;
    }
    return status;
}

/*******************************************************************************
**
** Function     OBX_Read4ByteHdr
**
** Description  This function is called to get the UINT32 HV of the given HI 
**              in the given OBEX packet.
**
** Returns      TRUE, if the header is in the OBEX packet.
**              FALSE, otherwise.
**
*******************************************************************************/
BOOLEAN OBX_Read4ByteHdr(BT_HDR *p_pkt, UINT8 id, UINT32 *p_data)
{
    BOOLEAN status = FALSE;
    UINT8   *p_start = OBX_CheckHdr(p_pkt, id);

    if(p_start)
    {
        p_start++;
        BE_STREAM_TO_UINT32(*p_data, p_start);
        status = TRUE;
    }
    return status;
}

/*******************************************************************************
**
** Function     OBX_ReadByteStrHdr
**
** Description  This function is called to get the byte sequence HV of the given
**              HI in the given OBEX packet.
**
** Returns      TRUE, if the header is in the OBEX packet.
**              FALSE, otherwise.
**
*******************************************************************************/
BOOLEAN OBX_ReadByteStrHdr(BT_HDR *p_pkt, UINT8 id, UINT8 **p_data, UINT16 *p_len, UINT8 next)
{
    BOOLEAN status = FALSE;
    UINT8   *p_start = OBX_CheckHdr(p_pkt, id);

    if(p_start)
    {
        next += 1;
        while(next && (id == *p_start++))
        {
            next--;
            BE_STREAM_TO_UINT16(*p_len, p_start);
            if(next == 0)
            {
                status = TRUE;
                *p_len -= 3;    /* get rid of hi and hv_len */
                *p_data = p_start;
            }
            else
                p_start = p_start + *p_len - 3;
        }
    }
    else
    {
        *p_len = 0;
    }
    return status;
}

/*******************************************************************************
**
** Function     OBX_ReadUnicodeHdr
**
** Description  This function is called to get the Unicode HV of the given
**              HI in the given OBEX packet.
**
** Returns      TRUE, if the header is in the OBEX packet.
**              FALSE, otherwise.
**
*******************************************************************************/
BOOLEAN OBX_ReadUnicodeHdr(BT_HDR *p_pkt, UINT8 id, UINT16 *p_data, UINT16 *p_len)
{
    BOOLEAN status = FALSE;
    UINT16  len, xx, max_len;
    UINT8   *p_start = OBX_CheckHdr(p_pkt, id);

    if(p_start)
    {
        max_len = *p_len;
        p_start++; /* 1/hi*/
        BE_STREAM_TO_UINT16(len, p_start);
        len -= 3; /* 1/hi, 2/hv_len */
        len /= OBX_UNICODE_SIZE; /* size in UINT16 */
        /* only conver the provided size */
        if( len > max_len)
            len = max_len;
        for(xx=0; xx<len; xx++)
        {
            BE_STREAM_TO_UINT16(*p_data, p_start);
            p_data++;
        }
        *p_len = len;
        status = TRUE;
        max_len -= len;
        while ( (p_start = OBX_CheckNext(p_pkt, p_start, id)) != NULL && (max_len > 0))
        {
            p_start++; /* 1/hi*/
            BE_STREAM_TO_UINT16(len, p_start);
            len -= 3; /* 1/hi, 2/hv_len */
            len /= OBX_UNICODE_SIZE; /* size in UINT16 */
            /* only conver the provided size */
            if( len > max_len)
                len = max_len;
            for(xx=0; xx<len; xx++)
            {
                BE_STREAM_TO_UINT16(*p_data, p_start);
                p_data++;
            }
            *p_len += len;
            max_len -= len;
        }
    }
    else
    {
        *p_len = 0;
    }
    return status;
}

/*******************************************************************************
**
** Function     OBX_ReadTriplet
**
** Description  This function is called to get the Triplet HV of the given
**              HI in the given OBEX packet.
**
** Returns      TRUE, if the header is in the OBEX packet.
**              FALSE, otherwise.
**
*******************************************************************************/
BOOLEAN OBX_ReadTriplet(BT_HDR *p_pkt, UINT8 id, tOBX_TRIPLET *p_triplet, UINT8 *p_num)
{
    BOOLEAN status = FALSE;
    UINT8   *p_start = OBX_CheckHdr(p_pkt, id);
    UINT16  len;
    UINT8   count = 0;

    if(p_start)
    {
        p_start++; /* 1/hi*/
        BE_STREAM_TO_UINT16(len, p_start);
        len -= 3;   /* 1/hi, 2/hv_len */
        while(len && *p_num > count)
        {
            p_triplet[count].tag = *p_start++;
            p_triplet[count].len = *p_start++;
            OBX_TRACE_DEBUG3( "OBX_ReadTriplet: count: %d, tag: %x, len: %d",
                count, p_triplet[count].tag, p_triplet[count].len);
            p_triplet[count].p_array = p_start;
            p_start += p_triplet[count].len;
            if(len > (p_triplet[count].len + 2) )
                len -= (p_triplet[count].len + 2);
            else
                len = 0;
            count++;
        }
        status = TRUE;
    }
    *p_num = count;
    return status;
}

/*******************************************************************************
**
** Function     OBX_ReadActionIdHdr
**
** Description  This function is called to get the HV of the Action ID header 
**              in the given OBEX packet.
**
** Returns      TRUE, if the header is in the OBEX packet.
**              FALSE, otherwise.
**
*******************************************************************************/
BOOLEAN OBX_ReadActionIdHdr(BT_HDR *p_pkt, UINT8 *p_data)
{
    BOOLEAN status = FALSE;
    UINT8   *p_start = OBX_CheckHdr(p_pkt, OBX_HI_ACTION_ID);

    if(p_start)
    {
        p_start++;
        /* check for valid values: 0-2 */
        /* do not allow 0x80 - 0xFF (vendor extention) for now. */
        if (*p_start <= OBX_ACT_PERMISSION)
        {
            *p_data = *(p_start);
            status = TRUE;
        }
    }
    return status;
}