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