![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
mbed base bard check program for BlueTooth USB dongle module (3 switches, 6 leds, I2C LCD, A/D)
Fork of BTstack by
BTstack/l2cap_signaling.c@0:1ed23ab1345f, 2012-06-26 (annotated)
- Committer:
- va009039
- Date:
- Tue Jun 26 14:27:45 2012 +0000
- Revision:
- 0:1ed23ab1345f
fix overflow spp_service_buffer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:1ed23ab1345f | 1 | /* |
va009039 | 0:1ed23ab1345f | 2 | * Copyright (C) 2009-2012 by Matthias Ringwald |
va009039 | 0:1ed23ab1345f | 3 | * |
va009039 | 0:1ed23ab1345f | 4 | * Redistribution and use in source and binary forms, with or without |
va009039 | 0:1ed23ab1345f | 5 | * modification, are permitted provided that the following conditions |
va009039 | 0:1ed23ab1345f | 6 | * are met: |
va009039 | 0:1ed23ab1345f | 7 | * |
va009039 | 0:1ed23ab1345f | 8 | * 1. Redistributions of source code must retain the above copyright |
va009039 | 0:1ed23ab1345f | 9 | * notice, this list of conditions and the following disclaimer. |
va009039 | 0:1ed23ab1345f | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
va009039 | 0:1ed23ab1345f | 11 | * notice, this list of conditions and the following disclaimer in the |
va009039 | 0:1ed23ab1345f | 12 | * documentation and/or other materials provided with the distribution. |
va009039 | 0:1ed23ab1345f | 13 | * 3. Neither the name of the copyright holders nor the names of |
va009039 | 0:1ed23ab1345f | 14 | * contributors may be used to endorse or promote products derived |
va009039 | 0:1ed23ab1345f | 15 | * from this software without specific prior written permission. |
va009039 | 0:1ed23ab1345f | 16 | * 4. Any redistribution, use, or modification is done solely for |
va009039 | 0:1ed23ab1345f | 17 | * personal benefit and not for any commercial purpose or for |
va009039 | 0:1ed23ab1345f | 18 | * monetary gain. |
va009039 | 0:1ed23ab1345f | 19 | * |
va009039 | 0:1ed23ab1345f | 20 | * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS |
va009039 | 0:1ed23ab1345f | 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
va009039 | 0:1ed23ab1345f | 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
va009039 | 0:1ed23ab1345f | 23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS |
va009039 | 0:1ed23ab1345f | 24 | * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
va009039 | 0:1ed23ab1345f | 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
va009039 | 0:1ed23ab1345f | 26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
va009039 | 0:1ed23ab1345f | 27 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
va009039 | 0:1ed23ab1345f | 28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
va009039 | 0:1ed23ab1345f | 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
va009039 | 0:1ed23ab1345f | 30 | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
va009039 | 0:1ed23ab1345f | 31 | * SUCH DAMAGE. |
va009039 | 0:1ed23ab1345f | 32 | * |
va009039 | 0:1ed23ab1345f | 33 | * Please inquire about commercial licensing options at btstack@ringwald.ch |
va009039 | 0:1ed23ab1345f | 34 | * |
va009039 | 0:1ed23ab1345f | 35 | */ |
va009039 | 0:1ed23ab1345f | 36 | |
va009039 | 0:1ed23ab1345f | 37 | /* |
va009039 | 0:1ed23ab1345f | 38 | * l2cap_signaling.h |
va009039 | 0:1ed23ab1345f | 39 | * |
va009039 | 0:1ed23ab1345f | 40 | * Created by Matthias Ringwald on 7/23/09. |
va009039 | 0:1ed23ab1345f | 41 | */ |
va009039 | 0:1ed23ab1345f | 42 | |
va009039 | 0:1ed23ab1345f | 43 | #include "l2cap_signaling.h" |
va009039 | 0:1ed23ab1345f | 44 | |
va009039 | 0:1ed23ab1345f | 45 | #include <string.h> |
va009039 | 0:1ed23ab1345f | 46 | |
va009039 | 0:1ed23ab1345f | 47 | static char *l2cap_signaling_commands_format[] = { |
va009039 | 0:1ed23ab1345f | 48 | "D", // 0x01 command reject: reason {cmd not understood (0), sig MTU exceeded (2:max sig MTU), invalid CID (4:req CID)}, data len, data |
va009039 | 0:1ed23ab1345f | 49 | "22", // 0x02 connection request: PSM, Source CID |
va009039 | 0:1ed23ab1345f | 50 | "2222", // 0x03 connection response: Dest CID, Source CID, Result, Status |
va009039 | 0:1ed23ab1345f | 51 | "22D", // 0x04 config request: Dest CID, Flags, Configuration options |
va009039 | 0:1ed23ab1345f | 52 | "222D", // 0x05 config response: Source CID, Flags, Result, Configuration options |
va009039 | 0:1ed23ab1345f | 53 | "22", // 0x06 disconection request: Dest CID, Source CID |
va009039 | 0:1ed23ab1345f | 54 | "22", // 0x07 disconection response: Dest CID, Source CID |
va009039 | 0:1ed23ab1345f | 55 | "D", // 0x08 echo request: Data |
va009039 | 0:1ed23ab1345f | 56 | "D", // 0x09 echo response: Data |
va009039 | 0:1ed23ab1345f | 57 | "2", // 0x0a information request: InfoType {1=Connectionless MTU, 2=Extended features supported} |
va009039 | 0:1ed23ab1345f | 58 | "22D", // 0x0b information response: InfoType, Result, Data |
va009039 | 0:1ed23ab1345f | 59 | }; |
va009039 | 0:1ed23ab1345f | 60 | |
va009039 | 0:1ed23ab1345f | 61 | uint8_t sig_seq_nr = 0xff; |
va009039 | 0:1ed23ab1345f | 62 | uint16_t source_cid = 0x40; |
va009039 | 0:1ed23ab1345f | 63 | |
va009039 | 0:1ed23ab1345f | 64 | uint8_t l2cap_next_sig_id(void){ |
va009039 | 0:1ed23ab1345f | 65 | if (sig_seq_nr == 0xff) { |
va009039 | 0:1ed23ab1345f | 66 | sig_seq_nr = 1; |
va009039 | 0:1ed23ab1345f | 67 | } else { |
va009039 | 0:1ed23ab1345f | 68 | sig_seq_nr++; |
va009039 | 0:1ed23ab1345f | 69 | } |
va009039 | 0:1ed23ab1345f | 70 | return sig_seq_nr; |
va009039 | 0:1ed23ab1345f | 71 | } |
va009039 | 0:1ed23ab1345f | 72 | |
va009039 | 0:1ed23ab1345f | 73 | uint16_t l2cap_next_local_cid(void){ |
va009039 | 0:1ed23ab1345f | 74 | return source_cid++; |
va009039 | 0:1ed23ab1345f | 75 | } |
va009039 | 0:1ed23ab1345f | 76 | |
va009039 | 0:1ed23ab1345f | 77 | uint16_t l2cap_create_signaling_internal(uint8_t * acl_buffer, hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){ |
va009039 | 0:1ed23ab1345f | 78 | |
va009039 | 0:1ed23ab1345f | 79 | // 0 - Connection handle : PB=10 : BC=00 |
va009039 | 0:1ed23ab1345f | 80 | bt_store_16(acl_buffer, 0, handle | (2 << 12) | (0 << 14)); |
va009039 | 0:1ed23ab1345f | 81 | // 6 - L2CAP channel = 1 |
va009039 | 0:1ed23ab1345f | 82 | bt_store_16(acl_buffer, 6, 1); |
va009039 | 0:1ed23ab1345f | 83 | // 8 - Code |
va009039 | 0:1ed23ab1345f | 84 | acl_buffer[8] = cmd; |
va009039 | 0:1ed23ab1345f | 85 | // 9 - id (!= 0 sequentially) |
va009039 | 0:1ed23ab1345f | 86 | acl_buffer[9] = identifier; |
va009039 | 0:1ed23ab1345f | 87 | |
va009039 | 0:1ed23ab1345f | 88 | // 12 - L2CAP signaling parameters |
va009039 | 0:1ed23ab1345f | 89 | uint16_t pos = 12; |
va009039 | 0:1ed23ab1345f | 90 | const char *format = l2cap_signaling_commands_format[cmd-1]; |
va009039 | 0:1ed23ab1345f | 91 | uint16_t word; |
va009039 | 0:1ed23ab1345f | 92 | uint8_t * ptr; |
va009039 | 0:1ed23ab1345f | 93 | while (*format) { |
va009039 | 0:1ed23ab1345f | 94 | switch(*format) { |
va009039 | 0:1ed23ab1345f | 95 | case '1': // 8 bit value |
va009039 | 0:1ed23ab1345f | 96 | case '2': // 16 bit value |
va009039 | 0:1ed23ab1345f | 97 | word = va_arg(argptr, int); |
va009039 | 0:1ed23ab1345f | 98 | // minimal va_arg is int: 2 bytes on 8+16 bit CPUs |
va009039 | 0:1ed23ab1345f | 99 | acl_buffer[pos++] = word & 0xff; |
va009039 | 0:1ed23ab1345f | 100 | if (*format == '2') { |
va009039 | 0:1ed23ab1345f | 101 | acl_buffer[pos++] = word >> 8; |
va009039 | 0:1ed23ab1345f | 102 | } |
va009039 | 0:1ed23ab1345f | 103 | break; |
va009039 | 0:1ed23ab1345f | 104 | case 'D': // variable data. passed: len, ptr |
va009039 | 0:1ed23ab1345f | 105 | word = va_arg(argptr, int); |
va009039 | 0:1ed23ab1345f | 106 | ptr = va_arg(argptr, uint8_t *); |
va009039 | 0:1ed23ab1345f | 107 | memcpy(&acl_buffer[pos], ptr, word); |
va009039 | 0:1ed23ab1345f | 108 | pos += word; |
va009039 | 0:1ed23ab1345f | 109 | break; |
va009039 | 0:1ed23ab1345f | 110 | default: |
va009039 | 0:1ed23ab1345f | 111 | break; |
va009039 | 0:1ed23ab1345f | 112 | } |
va009039 | 0:1ed23ab1345f | 113 | format++; |
va009039 | 0:1ed23ab1345f | 114 | }; |
va009039 | 0:1ed23ab1345f | 115 | va_end(argptr); |
va009039 | 0:1ed23ab1345f | 116 | |
va009039 | 0:1ed23ab1345f | 117 | // Fill in various length fields: it's the number of bytes following for ACL lenght and l2cap parameter length |
va009039 | 0:1ed23ab1345f | 118 | // - the l2cap payload length is counted after the following channel id (only payload) |
va009039 | 0:1ed23ab1345f | 119 | |
va009039 | 0:1ed23ab1345f | 120 | // 2 - ACL length |
va009039 | 0:1ed23ab1345f | 121 | bt_store_16(acl_buffer, 2, pos - 4); |
va009039 | 0:1ed23ab1345f | 122 | // 4 - L2CAP packet length |
va009039 | 0:1ed23ab1345f | 123 | bt_store_16(acl_buffer, 4, pos - 6 - 2); |
va009039 | 0:1ed23ab1345f | 124 | // 10 - L2CAP signaling parameter length |
va009039 | 0:1ed23ab1345f | 125 | bt_store_16(acl_buffer, 10, pos - 12); |
va009039 | 0:1ed23ab1345f | 126 | |
va009039 | 0:1ed23ab1345f | 127 | return pos; |
va009039 | 0:1ed23ab1345f | 128 | } |