Debian Codegraph @

const.go

raw symbols SCIP
1 package gatt
2
3 // This file includes constants from the BLE spec.
4
5 var (
6 attrGAPUUID = UUID16(0x1800)
7 attrGATTUUID = UUID16(0x1801)
8
9 attrPrimaryServiceUUID = UUID16(0x2800)
10 attrSecondaryServiceUUID = UUID16(0x2801)
11 attrIncludeUUID = UUID16(0x2802)
12 attrCharacteristicUUID = UUID16(0x2803)
13
14 attrClientCharacteristicConfigUUID = UUID16(0x2902)
15 attrServerCharacteristicConfigUUID = UUID16(0x2903)
16
17 attrDeviceNameUUID = UUID16(0x2A00)
18 attrAppearanceUUID = UUID16(0x2A01)
19 attrPeripheralPrivacyUUID = UUID16(0x2A02)
20 attrReconnectionAddrUUID = UUID16(0x2A03)
21 attrPeferredParamsUUID = UUID16(0x2A04)
22 attrServiceChangedUUID = UUID16(0x2A05)
23 )
24
25 const (
26 gattCCCNotifyFlag = 0x0001
27 gattCCCIndicateFlag = 0x0002
28 )
29
30 const (
31 attOpError = 0x01
32 attOpMtuReq = 0x02
33 attOpMtuRsp = 0x03
34 attOpFindInfoReq = 0x04
35 attOpFindInfoRsp = 0x05
36 attOpFindByTypeValueReq = 0x06
37 attOpFindByTypeValueRsp = 0x07
38 attOpReadByTypeReq = 0x08
39 attOpReadByTypeRsp = 0x09
40 attOpReadReq = 0x0a
41 attOpReadRsp = 0x0b
42 attOpReadBlobReq = 0x0c
43 attOpReadBlobRsp = 0x0d
44 attOpReadMultiReq = 0x0e
45 attOpReadMultiRsp = 0x0f
46 attOpReadByGroupReq = 0x10
47 attOpReadByGroupRsp = 0x11
48 attOpWriteReq = 0x12
49 attOpWriteRsp = 0x13
50 attOpWriteCmd = 0x52
51 attOpPrepWriteReq = 0x16
52 attOpPrepWriteRsp = 0x17
53 attOpExecWriteReq = 0x18
54 attOpExecWriteRsp = 0x19
55 attOpHandleNotify = 0x1b
56 attOpHandleInd = 0x1d
57 attOpHandleCnf = 0x1e
58 attOpSignedWriteCmd = 0xd2
59 )
60
61 type attEcode byte
62
63 const (
64 attEcodeSuccess attEcode = 0x00 // Success
65 attEcodeInvalidHandle attEcode = 0x01 // The attribute handle given was not valid on this server.
66 attEcodeReadNotPerm attEcode = 0x02 // The attribute cannot be read.
67 attEcodeWriteNotPerm attEcode = 0x03 // The attribute cannot be written.
68 attEcodeInvalidPDU attEcode = 0x04 // The attribute PDU was invalid.
69 attEcodeAuthentication attEcode = 0x05 // The attribute requires authentication before it can be read or written.
70 attEcodeReqNotSupp attEcode = 0x06 // Attribute server does not support the request received from the client.
71 attEcodeInvalidOffset attEcode = 0x07 // Offset specified was past the end of the attribute.
72 attEcodeAuthorization attEcode = 0x08 // The attribute requires authorization before it can be read or written.
73 attEcodePrepQueueFull attEcode = 0x09 // Too many prepare writes have been queued.
74 attEcodeAttrNotFound attEcode = 0x0a // No attribute found within the given attribute handle range.
75 attEcodeAttrNotLong attEcode = 0x0b // The attribute cannot be read or written using the Read Blob Request.
76 attEcodeInsuffEncrKeySize attEcode = 0x0c // The Encryption Key Size used for encrypting this link is insufficient.
77 attEcodeInvalAttrValueLen attEcode = 0x0d // The attribute value length is invalid for the operation.
78 attEcodeUnlikely attEcode = 0x0e // The attribute request that was requested has encountered an error that was unlikely, and therefore could not be completed as requested.
79 attEcodeInsuffEnc attEcode = 0x0f // The attribute requires encryption before it can be read or written.
80 attEcodeUnsuppGrpType attEcode = 0x10 // The attribute type is not a supported grouping attribute as defined by a higher layer specification.
81 attEcodeInsuffResources attEcode = 0x11 // Insufficient Resources to complete the request.
82 )
83
84 func (a attEcode) Error() string {
85 switch i := int(a); {
86 case i < 0x11:
87 return attEcodeName[a]
88 case i >= 0x12 && i <= 0x7F: // Reserved for future use
89 return "reserved error code"
90 case i >= 0x80 && i <= 0x9F: // Application Error, defined by higher level
91 return "reserved error code"
92 case i >= 0xA0 && i <= 0xDF: // Reserved for future use
93 return "reserved error code"
94 case i >= 0xE0 && i <= 0xFF: // Common profile and service error codes
95 return "profile or service error"
96 default: // can't happen, just make compiler happy
97 return "unkown error"
98 }
99 }
100
101 var attEcodeName = map[attEcode]string{
102 attEcodeSuccess: "success",
103 attEcodeInvalidHandle: "invalid handle",
104 attEcodeReadNotPerm: "read not permitted",
105 attEcodeWriteNotPerm: "write not permitted",
106 attEcodeInvalidPDU: "invalid PDU",
107 attEcodeAuthentication: "insufficient authentication",
108 attEcodeReqNotSupp: "request not supported",
109 attEcodeInvalidOffset: "invalid offset",
110 attEcodeAuthorization: "insufficient authorization",
111 attEcodePrepQueueFull: "prepare queue full",
112 attEcodeAttrNotFound: "attribute not found",
113 attEcodeAttrNotLong: "attribute not long",
114 attEcodeInsuffEncrKeySize: "insufficient encryption key size",
115 attEcodeInvalAttrValueLen: "invalid attribute value length",
116 attEcodeUnlikely: "unlikely error",
117 attEcodeInsuffEnc: "insufficient encryption",
118 attEcodeUnsuppGrpType: "unsupported group type",
119 attEcodeInsuffResources: "insufficient resources",
120 }
121
122 func attErrorRsp(op byte, h uint16, s attEcode) []byte {
123 return attErr{opcode: op, attr: h, status: s}.Marshal()
124 }
125
126 // attRspFor maps from att request
127 // codes to att response codes.
128 var attRspFor = map[byte]byte{
129 attOpMtuReq: attOpMtuRsp,
130 attOpFindInfoReq: attOpFindInfoRsp,
131 attOpFindByTypeValueReq: attOpFindByTypeValueRsp,
132 attOpReadByTypeReq: attOpReadByTypeRsp,
133 attOpReadReq: attOpReadRsp,
134 attOpReadBlobReq: attOpReadBlobRsp,
135 attOpReadMultiReq: attOpReadMultiRsp,
136 attOpReadByGroupReq: attOpReadByGroupRsp,
137 attOpWriteReq: attOpWriteRsp,
138 attOpPrepWriteReq: attOpPrepWriteRsp,
139 attOpExecWriteReq: attOpExecWriteRsp,
140 }
141
142 type attErr struct {
143 opcode uint8
144 attr uint16
145 status attEcode
146 }
147
148 // TODO: Reformulate in a way that lets the caller avoid allocs.
149 // Accept a []byte? Write directly to an io.Writer?
150 func (e attErr) Marshal() []byte {
151 // little-endian encoding for attr
152 return []byte{attOpError, e.opcode, byte(e.attr), byte(e.attr >> 8), byte(e.status)}
153 }