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/. */
7 #ifndef mozilla_dom_bluetooth_obexbase_h__
8 #define mozilla_dom_bluetooth_obexbase_h__
10 #include "BluetoothCommon.h"
11 #include "nsAutoPtr.h"
14 BEGIN_BLUETOOTH_NAMESPACE
16 const char FINAL_BIT
= 0x80;
19 * Defined in section 2.1 "OBEX Headers", IrOBEX ver 1.2
42 * Defined in section 3.3 "OBEX Operations and Opcode definitions",
45 enum ObexRequestCode
{
57 * Defined in section 3.2.1 "Response Code values", IrOBEX ver 1.2
59 enum ObexResponseCode
{
65 NonAuthoritativeInfo
= 0xA3,
68 PartialContent
= 0xA6,
70 MultipleChoices
= 0xB0,
71 MovedPermanently
= 0xB1,
72 MovedTemporarily
= 0xB2,
79 PaymentRequired
= 0xC2,
82 MethodNotAllowed
= 0xC5,
84 ProxyAuthenticationRequired
= 0xC7,
85 RequestTimeOut
= 0xC8,
88 LengthRequired
= 0xCB,
89 PreconditionFailed
= 0xCC,
90 RequestedEntityTooLarge
= 0xCD,
91 RequestUrlTooLarge
= 0xCE,
92 UnsupprotedMediaType
= 0xCF,
94 InternalServerError
= 0xD0,
95 NotImplemented
= 0xD1,
97 ServiceUnavailable
= 0xD3,
98 GatewayTimeout
= 0xD4,
99 HttpVersionNotSupported
= 0xD5,
102 DatabaseLocked
= 0xE1,
108 ObexHeader(ObexHeaderId aId
, int aDataLength
, const uint8_t* aData
)
110 , mDataLength(aDataLength
)
113 mData
= new uint8_t[mDataLength
];
114 memcpy(mData
, aData
, aDataLength
);
123 nsAutoArrayPtr
<uint8_t> mData
;
129 ObexHeaderSet(uint8_t aOpcode
) : mOpcode(aOpcode
)
137 void AddHeader(ObexHeader
* aHeader
)
139 mHeaders
.AppendElement(aHeader
);
142 void GetName(nsString
& aRetName
) const
146 int length
= mHeaders
.Length();
148 for (int i
= 0; i
< length
; ++i
) {
149 if (mHeaders
[i
]->mId
== ObexHeaderId::Name
) {
151 * According to section 2.2.2 [Name] of IrOBEX spec, we know that
152 * the Name header is "a null terminated Unicode text string describing
153 * the name of the object.", and that's the reason why we need to minus
154 * 1 to get the real length of the file name.
156 int nameLength
= mHeaders
[i
]->mDataLength
/ 2 - 1;
157 uint8_t* ptr
= mHeaders
[i
]->mData
.get();
159 for (int j
= 0; j
< nameLength
; ++j
) {
160 char16_t c
= ((((uint32_t)ptr
[j
* 2]) << 8) | ptr
[j
* 2 + 1]);
169 void GetContentType(nsString
& aRetContentType
) const
171 aRetContentType
.Truncate();
173 int length
= mHeaders
.Length();
175 for (int i
= 0; i
< length
; ++i
) {
176 if (mHeaders
[i
]->mId
== ObexHeaderId::Type
) {
177 uint8_t* ptr
= mHeaders
[i
]->mData
.get();
178 aRetContentType
.AssignASCII((const char*)ptr
);
184 // @return file length, 0 means file length is unknown.
185 void GetLength(uint32_t* aRetLength
) const
187 int length
= mHeaders
.Length();
190 for (int i
= 0; i
< length
; ++i
) {
191 if (mHeaders
[i
]->mId
== ObexHeaderId::Length
) {
192 uint8_t* ptr
= mHeaders
[i
]->mData
.get();
193 *aRetLength
= ((uint32_t)ptr
[0] << 24) |
194 ((uint32_t)ptr
[1] << 16) |
195 ((uint32_t)ptr
[2] << 8) |
202 void GetBodyLength(int* aRetBodyLength
) const
204 int length
= mHeaders
.Length();
207 for (int i
= 0; i
< length
; ++i
) {
208 if (mHeaders
[i
]->mId
== ObexHeaderId::Body
||
209 mHeaders
[i
]->mId
== ObexHeaderId::EndOfBody
) {
210 *aRetBodyLength
= mHeaders
[i
]->mDataLength
;
216 void GetBody(uint8_t** aRetBody
) const
218 int length
= mHeaders
.Length();
221 for (int i
= 0; i
< length
; ++i
) {
222 if (mHeaders
[i
]->mId
== ObexHeaderId::Body
||
223 mHeaders
[i
]->mId
== ObexHeaderId::EndOfBody
) {
224 uint8_t* ptr
= mHeaders
[i
]->mData
.get();
225 *aRetBody
= new uint8_t[mHeaders
[i
]->mDataLength
];
226 memcpy(*aRetBody
, ptr
, mHeaders
[i
]->mDataLength
);
232 bool Has(ObexHeaderId aId
) const
234 int length
= mHeaders
.Length();
235 for (int i
= 0; i
< length
; ++i
) {
236 if (mHeaders
[i
]->mId
== aId
) {
251 nsTArray
<nsAutoPtr
<ObexHeader
> > mHeaders
;
254 int AppendHeaderName(uint8_t* aRetBuf
, int aBufferSize
, const char* aName
,
256 int AppendHeaderBody(uint8_t* aRetBuf
, int aBufferSize
, const uint8_t* aData
,
258 int AppendHeaderEndOfBody(uint8_t* aRetBuf
);
259 int AppendHeaderLength(uint8_t* aRetBuf
, int aObjectLength
);
260 int AppendHeaderConnectionId(uint8_t* aRetBuf
, int aConnectionId
);
261 void SetObexPacketInfo(uint8_t* aRetBuf
, uint8_t aOpcode
, int aPacketLength
);
264 * @return true when the message was parsed without any error, false otherwise.
266 bool ParseHeaders(const uint8_t* aHeaderStart
,
268 ObexHeaderSet
* aRetHanderSet
);
270 END_BLUETOOTH_NAMESPACE