1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/bluetooth/ObexBase.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,138 @@ 1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "ObexBase.h" 1.11 + 1.12 +BEGIN_BLUETOOTH_NAMESPACE 1.13 + 1.14 +int 1.15 +AppendHeaderName(uint8_t* aRetBuf, int aBufferSize, const char* aName, 1.16 + int aLength) 1.17 +{ 1.18 + int headerLength = aLength + 3; 1.19 + 1.20 + aRetBuf[0] = ObexHeaderId::Name; 1.21 + aRetBuf[1] = (headerLength & 0xFF00) >> 8; 1.22 + aRetBuf[2] = headerLength & 0x00FF; 1.23 + 1.24 + memcpy(&aRetBuf[3], aName, (aLength < aBufferSize - 3)? aLength 1.25 + : aBufferSize - 3); 1.26 + 1.27 + return headerLength; 1.28 +} 1.29 + 1.30 +int 1.31 +AppendHeaderBody(uint8_t* aRetBuf, int aBufferSize, const uint8_t* aData, 1.32 + int aLength) 1.33 +{ 1.34 + int headerLength = aLength + 3; 1.35 + 1.36 + aRetBuf[0] = ObexHeaderId::Body; 1.37 + aRetBuf[1] = (headerLength & 0xFF00) >> 8; 1.38 + aRetBuf[2] = headerLength & 0x00FF; 1.39 + 1.40 + memcpy(&aRetBuf[3], aData, (aLength < aBufferSize - 3)? aLength 1.41 + : aBufferSize - 3); 1.42 + 1.43 + return headerLength; 1.44 +} 1.45 + 1.46 +int 1.47 +AppendHeaderEndOfBody(uint8_t* aRetBuf) 1.48 +{ 1.49 + aRetBuf[0] = ObexHeaderId::EndOfBody; 1.50 + aRetBuf[1] = 0x00; 1.51 + aRetBuf[2] = 0x03; 1.52 + 1.53 + return 3; 1.54 +} 1.55 + 1.56 +int 1.57 +AppendHeaderLength(uint8_t* aRetBuf, int aObjectLength) 1.58 +{ 1.59 + aRetBuf[0] = ObexHeaderId::Length; 1.60 + aRetBuf[1] = (aObjectLength & 0xFF000000) >> 24; 1.61 + aRetBuf[2] = (aObjectLength & 0x00FF0000) >> 16; 1.62 + aRetBuf[3] = (aObjectLength & 0x0000FF00) >> 8; 1.63 + aRetBuf[4] = aObjectLength & 0x000000FF; 1.64 + 1.65 + return 5; 1.66 +} 1.67 + 1.68 +int 1.69 +AppendHeaderConnectionId(uint8_t* aRetBuf, int aConnectionId) 1.70 +{ 1.71 + aRetBuf[0] = ObexHeaderId::ConnectionId; 1.72 + aRetBuf[1] = (aConnectionId & 0xFF000000) >> 24; 1.73 + aRetBuf[2] = (aConnectionId & 0x00FF0000) >> 16; 1.74 + aRetBuf[3] = (aConnectionId & 0x0000FF00) >> 8; 1.75 + aRetBuf[4] = aConnectionId & 0x000000FF; 1.76 + 1.77 + return 5; 1.78 +} 1.79 + 1.80 +void 1.81 +SetObexPacketInfo(uint8_t* aRetBuf, uint8_t aOpcode, int aPacketLength) 1.82 +{ 1.83 + aRetBuf[0] = aOpcode; 1.84 + aRetBuf[1] = (aPacketLength & 0xFF00) >> 8; 1.85 + aRetBuf[2] = aPacketLength & 0x00FF; 1.86 +} 1.87 + 1.88 +bool 1.89 +ParseHeaders(const uint8_t* aHeaderStart, 1.90 + int aTotalLength, 1.91 + ObexHeaderSet* aRetHandlerSet) 1.92 +{ 1.93 + const uint8_t* ptr = aHeaderStart; 1.94 + 1.95 + while (ptr - aHeaderStart < aTotalLength) { 1.96 + ObexHeaderId headerId = (ObexHeaderId)*ptr++; 1.97 + 1.98 + uint16_t contentLength = 0; 1.99 + uint8_t highByte, lowByte; 1.100 + 1.101 + // Defined in 2.1 OBEX Headers, IrOBEX 1.2 1.102 + switch (headerId >> 6) 1.103 + { 1.104 + case 0x00: 1.105 + // Null-terminated Unicode text, length prefixed with 2-byte 1.106 + // unsigned integer. 1.107 + case 0x01: 1.108 + // byte sequence, length prefixed with 2 byte unsigned integer. 1.109 + highByte = *ptr++; 1.110 + lowByte = *ptr++; 1.111 + contentLength = (((uint16_t)highByte << 8) | lowByte) - 3; 1.112 + break; 1.113 + 1.114 + case 0x02: 1.115 + // 1 byte quantity 1.116 + contentLength = 1; 1.117 + break; 1.118 + 1.119 + case 0x03: 1.120 + // 4 byte quantity 1.121 + contentLength = 4; 1.122 + break; 1.123 + } 1.124 + 1.125 + // Length check to prevent from memory pollusion. 1.126 + if (ptr + contentLength > aHeaderStart + aTotalLength) { 1.127 + // Severe error occurred. We can't even believe the received data, so 1.128 + // clear all headers. 1.129 + MOZ_ASSERT(false); 1.130 + aRetHandlerSet->ClearHeaders(); 1.131 + return false; 1.132 + } 1.133 + 1.134 + aRetHandlerSet->AddHeader(new ObexHeader(headerId, contentLength, ptr)); 1.135 + ptr += contentLength; 1.136 + } 1.137 + 1.138 + return true; 1.139 +} 1.140 + 1.141 +END_BLUETOOTH_NAMESPACE