1 1.1 plunky /* $NetBSD: sdp_record.c,v 1.1 2009/05/12 10:05:06 plunky Exp $ */ 2 1.1 plunky 3 1.1 plunky /*- 4 1.1 plunky * Copyright (c) 2009 The NetBSD Foundation, Inc. 5 1.1 plunky * All rights reserved. 6 1.1 plunky * 7 1.1 plunky * This code is derived from software contributed to The NetBSD Foundation 8 1.1 plunky * by Iain Hibbert. 9 1.1 plunky * 10 1.1 plunky * Redistribution and use in source and binary forms, with or without 11 1.1 plunky * modification, are permitted provided that the following conditions 12 1.1 plunky * are met: 13 1.1 plunky * 1. Redistributions of source code must retain the above copyright 14 1.1 plunky * notice, this list of conditions and the following disclaimer. 15 1.1 plunky * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 plunky * notice, this list of conditions and the following disclaimer in the 17 1.1 plunky * documentation and/or other materials provided with the distribution. 18 1.1 plunky * 19 1.1 plunky * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 plunky * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 plunky * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 plunky * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 plunky * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 plunky * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 plunky * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 plunky * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 plunky * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 plunky * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 plunky * POSSIBILITY OF SUCH DAMAGE. 30 1.1 plunky */ 31 1.1 plunky 32 1.1 plunky #include <sys/cdefs.h> 33 1.1 plunky __RCSID("$NetBSD: sdp_record.c,v 1.1 2009/05/12 10:05:06 plunky Exp $"); 34 1.1 plunky 35 1.1 plunky #include <errno.h> 36 1.1 plunky #include <sdp.h> 37 1.1 plunky #include <stdlib.h> 38 1.1 plunky #include <string.h> 39 1.1 plunky #include <unistd.h> 40 1.1 plunky 41 1.1 plunky #include "sdp-int.h" 42 1.1 plunky 43 1.1 plunky /* 44 1.1 plunky * This is the interface to sdpd(8); These PDU IDs are NOT part 45 1.1 plunky * of the Bluetooth specification. 46 1.1 plunky */ 47 1.1 plunky 48 1.1 plunky bool 49 1.1 plunky sdp_record_insert(struct sdp_session *ss, bdaddr_t *bdaddr, 50 1.1 plunky uint32_t *handle, const sdp_data_t *rec) 51 1.1 plunky { 52 1.1 plunky struct iovec req[4]; 53 1.1 plunky sdp_data_t hdr; 54 1.1 plunky uint8_t data[5]; 55 1.1 plunky ssize_t len; 56 1.1 plunky bdaddr_t ba; 57 1.1 plunky uint16_t ec; 58 1.1 plunky 59 1.1 plunky /* 60 1.1 plunky * setup BluetoothDeviceAddress 61 1.1 plunky */ 62 1.1 plunky bdaddr_copy(&ba, (bdaddr == NULL) ? BDADDR_ANY : bdaddr); 63 1.1 plunky req[1].iov_base = &ba; 64 1.1 plunky req[1].iov_len = sizeof(bdaddr_t); 65 1.1 plunky 66 1.1 plunky /* 67 1.1 plunky * setup ServiceRecord 68 1.1 plunky */ 69 1.1 plunky len = rec->end - rec->next; 70 1.1 plunky if (len < 0 || len > UINT16_MAX) { 71 1.1 plunky errno = EINVAL; 72 1.1 plunky return false; 73 1.1 plunky } 74 1.1 plunky 75 1.1 plunky hdr.next = data; 76 1.1 plunky hdr.end = data + sizeof(data) + len; 77 1.1 plunky sdp_put_seq(&hdr, len); 78 1.1 plunky req[2].iov_base = data; 79 1.1 plunky req[2].iov_len = hdr.next - data; 80 1.1 plunky 81 1.1 plunky req[3].iov_base = rec->next; 82 1.1 plunky req[3].iov_len = len; 83 1.1 plunky 84 1.1 plunky /* 85 1.1 plunky * InsertRecord Transaction 86 1.1 plunky */ 87 1.1 plunky if (!_sdp_send_pdu(ss, SDP_PDU_RECORD_INSERT_REQUEST, 88 1.1 plunky req, __arraycount(req))) 89 1.1 plunky return false; 90 1.1 plunky 91 1.1 plunky len = _sdp_recv_pdu(ss, SDP_PDU_ERROR_RESPONSE); 92 1.1 plunky if (len == -1) 93 1.1 plunky return false; 94 1.1 plunky 95 1.1 plunky if (len != sizeof(uint16_t) + sizeof(uint32_t)) { 96 1.1 plunky errno = EIO; 97 1.1 plunky return false; 98 1.1 plunky } 99 1.1 plunky 100 1.1 plunky /* 101 1.1 plunky * extract and check ErrorCode (success == 0) 102 1.1 plunky */ 103 1.1 plunky ec = be16dec(ss->ibuf); 104 1.1 plunky if (ec != 0) { 105 1.1 plunky errno = _sdp_errno(ec); 106 1.1 plunky return false; 107 1.1 plunky } 108 1.1 plunky 109 1.1 plunky /* 110 1.1 plunky * extract ServiceRecordHandle if required 111 1.1 plunky */ 112 1.1 plunky if (handle != NULL) 113 1.1 plunky *handle = be32dec(ss->ibuf + sizeof(uint16_t)); 114 1.1 plunky 115 1.1 plunky return true; 116 1.1 plunky } 117 1.1 plunky 118 1.1 plunky bool 119 1.1 plunky sdp_record_update(struct sdp_session *ss, uint32_t handle, 120 1.1 plunky const sdp_data_t *rec) 121 1.1 plunky { 122 1.1 plunky struct iovec req[4]; 123 1.1 plunky sdp_data_t hdr; 124 1.1 plunky uint8_t data[5]; 125 1.1 plunky ssize_t len; 126 1.1 plunky uint16_t ec; 127 1.1 plunky 128 1.1 plunky /* 129 1.1 plunky * setup ServiceRecordHandle 130 1.1 plunky */ 131 1.1 plunky handle = htobe32(handle); 132 1.1 plunky req[1].iov_base = &handle; 133 1.1 plunky req[1].iov_len = sizeof(handle); 134 1.1 plunky 135 1.1 plunky /* 136 1.1 plunky * setup ServiceRecord 137 1.1 plunky */ 138 1.1 plunky len = rec->end - rec->next; 139 1.1 plunky if (len < 0 || len > UINT16_MAX) { 140 1.1 plunky errno = EINVAL; 141 1.1 plunky return false; 142 1.1 plunky } 143 1.1 plunky 144 1.1 plunky hdr.next = data; 145 1.1 plunky hdr.end = data + sizeof(data) + len; 146 1.1 plunky sdp_put_seq(&hdr, len); 147 1.1 plunky req[2].iov_base = data; 148 1.1 plunky req[2].iov_len = hdr.next - data; 149 1.1 plunky 150 1.1 plunky req[3].iov_base = rec->next; 151 1.1 plunky req[3].iov_len = len; 152 1.1 plunky 153 1.1 plunky /* 154 1.1 plunky * UpdateRecord Transaction 155 1.1 plunky */ 156 1.1 plunky if (!_sdp_send_pdu(ss, SDP_PDU_RECORD_UPDATE_REQUEST, 157 1.1 plunky req, __arraycount(req))) 158 1.1 plunky return false; 159 1.1 plunky 160 1.1 plunky len = _sdp_recv_pdu(ss, SDP_PDU_ERROR_RESPONSE); 161 1.1 plunky if (len == -1) 162 1.1 plunky return false; 163 1.1 plunky 164 1.1 plunky if (len != sizeof(uint16_t)) { 165 1.1 plunky errno = EIO; 166 1.1 plunky return false; 167 1.1 plunky } 168 1.1 plunky 169 1.1 plunky /* 170 1.1 plunky * extract and check ErrorCode (success == 0) 171 1.1 plunky */ 172 1.1 plunky if ((ec = be16dec(ss->ibuf)) != 0) { 173 1.1 plunky errno = _sdp_errno(ec); 174 1.1 plunky return false; 175 1.1 plunky } 176 1.1 plunky 177 1.1 plunky return true; 178 1.1 plunky } 179 1.1 plunky 180 1.1 plunky bool 181 1.1 plunky sdp_record_remove(struct sdp_session *ss, uint32_t handle) 182 1.1 plunky { 183 1.1 plunky struct iovec req[2]; 184 1.1 plunky ssize_t len; 185 1.1 plunky uint16_t ec; 186 1.1 plunky 187 1.1 plunky /* 188 1.1 plunky * setup ServiceRecordHandle 189 1.1 plunky */ 190 1.1 plunky handle = htobe32(handle); 191 1.1 plunky req[1].iov_base = &handle; 192 1.1 plunky req[1].iov_len = sizeof(handle); 193 1.1 plunky 194 1.1 plunky /* 195 1.1 plunky * RemoveRecord Transaction 196 1.1 plunky */ 197 1.1 plunky if (!_sdp_send_pdu(ss, SDP_PDU_RECORD_REMOVE_REQUEST, 198 1.1 plunky req, __arraycount(req))) 199 1.1 plunky return false; 200 1.1 plunky 201 1.1 plunky len = _sdp_recv_pdu(ss, SDP_PDU_ERROR_RESPONSE); 202 1.1 plunky if (len == -1) 203 1.1 plunky return false; 204 1.1 plunky 205 1.1 plunky if (len != sizeof(uint16_t)) { 206 1.1 plunky errno = EIO; 207 1.1 plunky return false; 208 1.1 plunky } 209 1.1 plunky 210 1.1 plunky /* 211 1.1 plunky * extract and check ErrorCode (success == 0) 212 1.1 plunky */ 213 1.1 plunky ec = be16dec(ss->ibuf); 214 1.1 plunky if (ec != 0) { 215 1.1 plunky errno = _sdp_errno(ec); 216 1.1 plunky return false; 217 1.1 plunky } 218 1.1 plunky 219 1.1 plunky return true; 220 1.1 plunky } 221