1 /* 2 * Copyright 2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com 3 * 4 * All rights reserved. Distributed under the terms of the MIT License. 5 * 6 */ 7 8 9 #include <bluetooth/bluetooth.h> 10 #include <bluetooth/HCI/btHCI_command.h> 11 12 #include <malloc.h> 13 14 #include "CommandManager.h" 15 16 17 inline void* buildCommand(uint8 ogf, uint8 ocf, void** param, size_t psize, size_t* outsize) 18 { 19 struct hci_command_header* header; 20 21 #ifdef BT_IOCTLS_PASS_SIZE 22 header = (struct hci_command_header*) malloc(psize + sizeof(struct hci_command_header)); 23 *outsize = psize + sizeof(struct hci_command_header); 24 #else 25 size_t* size = (size_t*)malloc(psize + sizeof(struct hci_command_header) + sizeof(size_t)); 26 *outsize = psize + sizeof(struct hci_command_header) + sizeof(size_t); 27 28 *size = psize + sizeof(struct hci_command_header); 29 header = (struct hci_command_header*) (((uint8*)size)+4); 30 #endif 31 32 33 if (header != NULL) { 34 35 header->opcode = B_HOST_TO_LENDIAN_INT16(PACK_OPCODE(ogf, ocf)); 36 header->clen = psize; 37 38 if (param != NULL && psize != 0) { 39 *param = ((uint8*)header) + sizeof(struct hci_command_header); 40 } 41 } 42 #ifdef BT_IOCTLS_PASS_SIZE 43 return header; 44 #else 45 return (void*)size; 46 #endif 47 } 48 49 50 #if 0 51 #pragma mark - CONTROL BASEBAND - 52 #endif 53 54 55 void* buildReset(size_t* outsize) 56 { 57 return buildCommand(OGF_CONTROL_BASEBAND, OCF_RESET, NULL, 0, outsize); 58 } 59 60 61 void* buildReadLocalName(size_t* outsize) 62 { 63 return buildCommand(OGF_CONTROL_BASEBAND, OCF_READ_LOCAL_NAME, NULL, 0, outsize); 64 } 65 66 67 void* buildWriteScan(uint8 scanmode, size_t* outsize) 68 { 69 struct hci_write_scan_enable* param; 70 void* command = buildCommand(OGF_CONTROL_BASEBAND, OCF_WRITE_SCAN_ENABLE, (void**) ¶m, sizeof(struct hci_write_scan_enable), outsize); 71 72 73 if (command != NULL) { 74 param->scan = scanmode; 75 } 76 77 return command; 78 79 } 80 81 82 void* buildAuthEnable(uint8 auth, size_t* outsize) 83 { 84 struct hci_write_authentication_enable* param; 85 void* command = buildCommand(OGF_CONTROL_BASEBAND, OCF_WRITE_AUTH_ENABLE, (void**) ¶m, sizeof(struct hci_write_authentication_enable), outsize); 86 87 88 if (command != NULL) { 89 param->authentication = auth; 90 } 91 92 return command; 93 94 } 95 96 97 #if 0 98 #pragma mark - LINK CONTROL - 99 #endif 100 101 void* buildCreateConnection(bdaddr_t bdaddr) 102 { 103 /* 104 cm4.size = sizeof(struct hci_command_header)+sizeof(struct hci_cp_create_conn); 105 cm4.header.opcode = B_HOST_TO_LENDIAN_INT16(hci_opcode_pack(OGF_LINK_CONTROL, OCF_CREATE_CONN)); 106 cm4.body.bdaddr.b[0] = 0x92; 107 cm4.body.bdaddr.b[1] = 0xd3; 108 cm4.body.bdaddr.b[2] = 0xaf; 109 cm4.body.bdaddr.b[3] = 0xd9; 110 cm4.body.bdaddr.b[4] = 0x0a; 111 cm4.body.bdaddr.b[5] = 0x00; 112 cm4.body.pkt_type = 0xFFFF; 113 cm4.body.pscan_rep_mode = 1; 114 cm4.body.pscan_mode = 0; 115 cm4.body.clock_offset = 0xc7; 116 cm4.body.role_switch = 1; 117 cm4.header.clen = 13; 118 ioctl(fd1, ISSUE_BT_COMMAND, &cm4, sizeof(cm4)); 119 */ 120 121 return NULL; 122 } 123 124 125 void* buildRemoteNameRequest(bdaddr_t bdaddr,uint8 pscan_rep_mode, uint16 clock_offset, size_t* outsize) 126 { 127 128 struct hci_remote_name_request* param; 129 void* command = buildCommand(OGF_LINK_CONTROL, OCF_REMOTE_NAME_REQUEST, (void**) ¶m, sizeof(struct hci_remote_name_request), outsize); 130 131 if (command != NULL) { 132 param->bdaddr = bdaddr; 133 param->pscan_rep_mode = pscan_rep_mode; 134 param->clock_offset = clock_offset; 135 } 136 137 return command; 138 } 139 140 141 void* buildInquiry(uint32 lap, uint8 length, uint8 num_rsp, size_t* outsize) 142 { 143 144 struct hci_cp_inquiry* param; 145 void* command = buildCommand(OGF_LINK_CONTROL, OCF_INQUIRY, (void**) ¶m, sizeof(struct hci_cp_inquiry), outsize); 146 147 if (command != NULL) { 148 149 param->lap[2] = (lap >> 16) & 0xFF; 150 param->lap[1] = (lap >> 8) & 0xFF; 151 param->lap[0] = (lap >> 0) & 0xFF; 152 param->length = length; 153 param->num_rsp = num_rsp; 154 } 155 156 return command; 157 } 158 159 160 #if 0 161 #pragma mark - INFORMATIONAL_PARAM - 162 #endif 163 164 165 void* buildReadBufferSize(size_t* outsize) 166 { 167 return buildCommand(OGF_INFORMATIONAL_PARAM, OCF_READ_BUFFER_SIZE, NULL, 0, outsize); 168 } 169 170 171 void* buildReadBdAddr(size_t* outsize) 172 { 173 return buildCommand(OGF_INFORMATIONAL_PARAM, OCF_READ_BD_ADDR, NULL, 0, outsize); 174 } 175 176 177 178 179