1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
9 BEGIN_BLUETOOTH_NAMESPACE
12 AppendHeaderName(uint8_t* aRetBuf
, int aBufferSize
, const char* aName
,
15 int headerLength
= aLength
+ 3;
17 aRetBuf
[0] = ObexHeaderId::Name
;
18 aRetBuf
[1] = (headerLength
& 0xFF00) >> 8;
19 aRetBuf
[2] = headerLength
& 0x00FF;
21 memcpy(&aRetBuf
[3], aName
, (aLength
< aBufferSize
- 3)? aLength
28 AppendHeaderBody(uint8_t* aRetBuf
, int aBufferSize
, const uint8_t* aData
,
31 int headerLength
= aLength
+ 3;
33 aRetBuf
[0] = ObexHeaderId::Body
;
34 aRetBuf
[1] = (headerLength
& 0xFF00) >> 8;
35 aRetBuf
[2] = headerLength
& 0x00FF;
37 memcpy(&aRetBuf
[3], aData
, (aLength
< aBufferSize
- 3)? aLength
44 AppendHeaderEndOfBody(uint8_t* aRetBuf
)
46 aRetBuf
[0] = ObexHeaderId::EndOfBody
;
54 AppendHeaderLength(uint8_t* aRetBuf
, int aObjectLength
)
56 aRetBuf
[0] = ObexHeaderId::Length
;
57 aRetBuf
[1] = (aObjectLength
& 0xFF000000) >> 24;
58 aRetBuf
[2] = (aObjectLength
& 0x00FF0000) >> 16;
59 aRetBuf
[3] = (aObjectLength
& 0x0000FF00) >> 8;
60 aRetBuf
[4] = aObjectLength
& 0x000000FF;
66 AppendHeaderConnectionId(uint8_t* aRetBuf
, int aConnectionId
)
68 aRetBuf
[0] = ObexHeaderId::ConnectionId
;
69 aRetBuf
[1] = (aConnectionId
& 0xFF000000) >> 24;
70 aRetBuf
[2] = (aConnectionId
& 0x00FF0000) >> 16;
71 aRetBuf
[3] = (aConnectionId
& 0x0000FF00) >> 8;
72 aRetBuf
[4] = aConnectionId
& 0x000000FF;
78 SetObexPacketInfo(uint8_t* aRetBuf
, uint8_t aOpcode
, int aPacketLength
)
81 aRetBuf
[1] = (aPacketLength
& 0xFF00) >> 8;
82 aRetBuf
[2] = aPacketLength
& 0x00FF;
86 ParseHeaders(const uint8_t* aHeaderStart
,
88 ObexHeaderSet
* aRetHandlerSet
)
90 const uint8_t* ptr
= aHeaderStart
;
92 while (ptr
- aHeaderStart
< aTotalLength
) {
93 ObexHeaderId headerId
= (ObexHeaderId
)*ptr
++;
95 uint16_t contentLength
= 0;
96 uint8_t highByte
, lowByte
;
98 // Defined in 2.1 OBEX Headers, IrOBEX 1.2
99 switch (headerId
>> 6)
102 // Null-terminated Unicode text, length prefixed with 2-byte
105 // byte sequence, length prefixed with 2 byte unsigned integer.
108 contentLength
= (((uint16_t)highByte
<< 8) | lowByte
) - 3;
122 // Length check to prevent from memory pollusion.
123 if (ptr
+ contentLength
> aHeaderStart
+ aTotalLength
) {
124 // Severe error occurred. We can't even believe the received data, so
125 // clear all headers.
127 aRetHandlerSet
->ClearHeaders();
131 aRetHandlerSet
->AddHeader(new ObexHeader(headerId
, contentLength
, ptr
));
132 ptr
+= contentLength
;
138 END_BLUETOOTH_NAMESPACE