blob: 59c16cace5f2b3915b37e77c9cfbf0adba10d895 [file] [log] [blame]
Rohan Martin95c4bdb2012-11-01 15:20:17 -06001/*
2 * ANT Stack
3 *
4 * Copyright 2009 Dynastream Innovations
Rohan Martinaf8cf682012-11-16 21:11:07 -07005 *
Rohan Martin95c4bdb2012-11-01 15:20:17 -06006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
Rohan Martinaf8cf682012-11-16 21:11:07 -07009 *
Rohan Martin95c4bdb2012-11-01 15:20:17 -060010 * http://www.apache.org/licenses/LICENSE-2.0
Rohan Martinaf8cf682012-11-16 21:11:07 -070011 *
Rohan Martin95c4bdb2012-11-01 15:20:17 -060012 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Rohan Martinaf8cf682012-11-16 21:11:07 -070015 * See the License for the specific language governing permissions and
Rohan Martin95c4bdb2012-11-01 15:20:17 -060016 * limitations under the License.
17 */
18/******************************************************************************\
19*
Rohan Martinaf8cf682012-11-16 21:11:07 -070020* FILE NAME: ant_framing.h
Rohan Martin95c4bdb2012-11-01 15:20:17 -060021*
Rohan Martinaf8cf682012-11-16 21:11:07 -070022* BRIEF:
23* This file defines ANT specific HCI values used by the ANT chip with a
24* shared BlueZ HCI transport.
Rohan Martin95c4bdb2012-11-01 15:20:17 -060025*
26*
27\******************************************************************************/
28
29#ifndef __ANT_HCIFRAMING_H
30#define __ANT_HCIFRAMING_H
31
Rohan Martinaf8cf682012-11-16 21:11:07 -070032#if defined(BOARD_ANT_DEVICE_WL12XX)
Rohan Martin95c4bdb2012-11-01 15:20:17 -060033
Rohan Martinaf8cf682012-11-16 21:11:07 -070034#define ANT_COMMAND_OPCODE 0xFDD1
35#define ANT_EVENT_VENDOR_CODE 0x0500
Rohan Martin95c4bdb2012-11-01 15:20:17 -060036
Rohan Martinaf8cf682012-11-16 21:11:07 -070037typedef struct {
38 ANT_U16 vendor_msg_len;
39} __attribute__ ((packed)) hci_cmd_vendor_hdr_t;
40#define HCI_COMMAND_VENDOR_HDR_SIZE 2
Rohan Martin95c4bdb2012-11-01 15:20:17 -060041
Rohan Martinaf8cf682012-11-16 21:11:07 -070042typedef struct {
43 ANT_U16 vendor_code;
44 ANT_U16 vendor_msg_len;
45} __attribute__ ((packed)) hci_evt_vendor_hdr_t;
46#define HCI_EVENT_VENDOR_HDR_SIZE 4
Rohan Martin95c4bdb2012-11-01 15:20:17 -060047
Rohan Martinaf8cf682012-11-16 21:11:07 -070048#elif defined(BOARD_ANT_DEVICE_BCM433X)
Rohan Martin95c4bdb2012-11-01 15:20:17 -060049
Rohan Martinaf8cf682012-11-16 21:11:07 -070050#define ANT_COMMAND_OPCODE 0xFCEC
51#define ANT_EVENT_VENDOR_CODE 0x2D
Rohan Martin95c4bdb2012-11-01 15:20:17 -060052
Rohan Martinaf8cf682012-11-16 21:11:07 -070053typedef struct {
54 ANT_U8 command_id_padding;
55} __attribute__ ((packed)) hci_cmd_vendor_hdr_t;
56#define HCI_COMMAND_VENDOR_HDR_SIZE 1
57
58typedef struct {
59 ANT_U8 vendor_code;
60 ANT_U8 status;
61} __attribute__ ((packed)) hci_evt_vendor_hdr_t;
62#define HCI_EVENT_VENDOR_HDR_SIZE 2
63
64#define COMMAND_ID_PADDING 0xFF
Rohan Martin95c4bdb2012-11-01 15:20:17 -060065
66#endif
67
Rohan Martinaf8cf682012-11-16 21:11:07 -070068#include <bluetooth/bluetooth.h>
69#include <bluetooth/hci.h>
70
71#include "ant_native.h"
72#include "ant_types.h"
73#include "ant_utils.h"
74
75#define HCI_EVENT_HEADER_OVERHEAD (HCI_TYPE_LEN + HCI_EVENT_HDR_SIZE)
76#define HCI_EVENT_TOTAL_OVERHEAD (HCI_EVENT_HEADER_OVERHEAD + HCI_EVENT_VENDOR_HDR_SIZE)
77
78#define TYPE_MASK_EVENT_PACKET 0x00000010
79#define EVENT_MASK_0_COMMAND_COMPLETE 0x00004000
80#define EVENT_MASK_1_EVENT_VENDOR 0x80000000
81
82// -------------------------------------------------
83
84typedef struct {
85 ANT_U8 packet_type; // HCI_COMMAND_PKT
86 hci_command_hdr command_header; // from hci.h
87 hci_cmd_vendor_hdr_t vendor_header;
88} __attribute__ ((packed)) hci_command_vendor_header_t;
89
90
91typedef struct {
92 ANT_U8 packet_type; // HCI_EVENT_PKT
93 hci_event_hdr event_header; // from hci.h
94 hci_evt_vendor_hdr_t vendor_header;
95 ANT_U8 hci_payload[HCI_MAX_EVENT_SIZE];
96} __attribute__ ((packed)) hci_event_packet_t;
97
98typedef struct {
99 ANT_U8 packet_type; // HCI_EVENT_PKT
100 hci_event_hdr event_header; // from hci.h
101 evt_cmd_complete command_complete_hdr;// from hci.h
102 ANT_U8 response;
103} __attribute__ ((packed)) hci_command_complete_packet_t;
104
105// -------------------------------------------------
106
107/**
108 * @param header the struct to fill with chip specific hci header
109 * @param ant_message_len total ant length, size, id, and data
110 */
111static inline void create_command_header(hci_command_vendor_header_t *header, ANT_U8 ant_message_len)
112{
113 header->packet_type = HCI_COMMAND_PKT;
114 header->command_header.opcode = htobs(ANT_COMMAND_OPCODE);
115 header->command_header.plen = ant_message_len + HCI_COMMAND_VENDOR_HDR_SIZE;
116#if defined(BOARD_ANT_DEVICE_WL12XX)
117 header->vendor_header.vendor_msg_len = htobs(ant_message_len);
118#elif defined(BOARD_ANT_DEVICE_BCM433X)
119 header->vendor_header.command_id_padding = COMMAND_ID_PADDING;
120#endif
121}
122
123/**
124 * @param packet the struct to check if is an ANT hci payload
125 * @param packet_len the total length of the packet
126 * @return 0 if valid, -1 if not valid
127 */
128static inline int validate_hci_event_vendor(hci_event_packet_t *packet, int packet_len)
129{
130#if defined(BOARD_ANT_DEVICE_WL12XX)
131 ANT_U16 vendor_code = ANT_UTILS_LEtoHost16((ANT_U8 *)&packet->vendor_header.vendor_code);
132#elif defined(BOARD_ANT_DEVICE_BCM433X)
133 ANT_U8 vendor_code = packet->vendor_header.vendor_code;
134#endif
135 if (vendor_code != ANT_EVENT_VENDOR_CODE) {
136 ANT_ERROR("HCI packet vendor event code is not ANT");
137 return -1;
138 }
139
140#if defined(BOARD_ANT_DEVICE_WL12XX)
141 ANT_U16 vendor_msg_len = ANT_UTILS_LEtoHost16((ANT_U8 *)&packet->vendor_header.vendor_msg_len);
142 if (packet_len != vendor_msg_len + HCI_EVENT_TOTAL_OVERHEAD) {
143 ANT_ERROR("HCI packet length (%d) should be %d bytes more than HCI payload length (%d)",
144 packet_len, HCI_EVENT_TOTAL_OVERHEAD, vendor_msg_len);
145 return -1;
146 }
147#elif defined(BOARD_ANT_DEVICE_BCM433X)
148 (void)packet_len; // unused warning
149#endif
150
151 return 0;
152}
153
154/**
155 * @param packet the struct to validate
156 * @param packet_len the total length of the packet
157 * @return -1 if not valid, or length of the ANT hci payload
158 */
159static inline int validate_hci_event_packet(hci_event_packet_t *packet, int packet_len)
160{
161 // Make sure we have at the minimum the hci header, vendor header, and ANT size/id bytes
162 if (packet_len < HCI_EVENT_TOTAL_OVERHEAD) {
163 ANT_ERROR("HCI packet length (%d) is less than minimum (%d)",
164 packet_len, HCI_EVENT_TOTAL_OVERHEAD);
165 return -1;
166 }
167
168 if (packet->packet_type != HCI_EVENT_PKT) {
169 ANT_ERROR("HCI packet is not an event packet");
170 return -1;
171 }
172
173 if (packet->event_header.evt != EVT_VENDOR) {
174 ANT_ERROR("HCI packet is not a vendor event");
175 return -1;
176 }
177
178 if (packet_len != packet->event_header.plen + HCI_EVENT_HEADER_OVERHEAD) {
179 ANT_ERROR("HCI packet length (%d) should be %d bytes more than HCI event length (%d)",
180 packet_len, HCI_EVENT_HDR_SIZE, packet->event_header.plen);
181 return -1;
182 }
183
184 if (validate_hci_event_vendor(packet, packet_len) == -1)
185 {
186 return -1;
187 }
188 else
189 {
190 // return HCI payload len
191 return packet_len - HCI_EVENT_TOTAL_OVERHEAD;
192 }
193}
194
Rohan Martin95c4bdb2012-11-01 15:20:17 -0600195#endif /* __ANT_HCIFRAMING_H */