| 1 |
package gatt |
| 2 |
|
| 3 |
|
| 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 |
| 65 |
attEcodeInvalidHandle attEcode = 0x01 |
| 66 |
attEcodeReadNotPerm attEcode = 0x02 |
| 67 |
attEcodeWriteNotPerm attEcode = 0x03 |
| 68 |
attEcodeInvalidPDU attEcode = 0x04 |
| 69 |
attEcodeAuthentication attEcode = 0x05 |
| 70 |
attEcodeReqNotSupp attEcode = 0x06 |
| 71 |
attEcodeInvalidOffset attEcode = 0x07 |
| 72 |
attEcodeAuthorization attEcode = 0x08 |
| 73 |
attEcodePrepQueueFull attEcode = 0x09 |
| 74 |
attEcodeAttrNotFound attEcode = 0x0a |
| 75 |
attEcodeAttrNotLong attEcode = 0x0b |
| 76 |
attEcodeInsuffEncrKeySize attEcode = 0x0c |
| 77 |
attEcodeInvalAttrValueLen attEcode = 0x0d |
| 78 |
attEcodeUnlikely attEcode = 0x0e |
| 79 |
attEcodeInsuffEnc attEcode = 0x0f |
| 80 |
attEcodeUnsuppGrpType attEcode = 0x10 |
| 81 |
attEcodeInsuffResources attEcode = 0x11 |
| 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: |
| 89 |
return "reserved error code" |
| 90 |
case i >= 0x80 && i <= 0x9F: |
| 91 |
return "reserved error code" |
| 92 |
case i >= 0xA0 && i <= 0xDF: |
| 93 |
return "reserved error code" |
| 94 |
case i >= 0xE0 && i <= 0xFF: |
| 95 |
return "profile or service error" |
| 96 |
default: |
| 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 |
|
| 127 |
|
| 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 |
|
| 149 |
|
| 150 |
func (e attErr) Marshal() []byte { |
| 151 |
|
| 152 |
return []byte{attOpError, e.opcode, byte(e.attr), byte(e.attr >> 8), byte(e.status)} |
| 153 |
} |