HW layer for the Nucleo board, it only work with old BLE_API
Dependents: Hello_BLE F446RE-BLE
Fork of X_NUCLEO_IDB0XA1 by
utils/inc/Payload.h@18:6d5f4c638f6a, 2014-08-08 (annotated)
- Committer:
- hemddabral
- Date:
- Fri Aug 08 10:45:53 2014 +0000
- Revision:
- 18:6d5f4c638f6a
- Parent:
- 14:baa7a1464517
- Child:
- 58:027c65a54097
(1) Device name characteristic update (2) More efficient scheme of parsing advertising data implemented
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hemddabral | 12:a5d7db4a1e49 | 1 | /* mbed Microcontroller Library |
hemddabral | 12:a5d7db4a1e49 | 2 | * Copyright (c) 2006-2013 ARM Limited |
hemddabral | 12:a5d7db4a1e49 | 3 | * |
hemddabral | 12:a5d7db4a1e49 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
hemddabral | 12:a5d7db4a1e49 | 5 | * you may not use this file except in compliance with the License. |
hemddabral | 12:a5d7db4a1e49 | 6 | * You may obtain a copy of the License at |
hemddabral | 12:a5d7db4a1e49 | 7 | * |
hemddabral | 12:a5d7db4a1e49 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
hemddabral | 12:a5d7db4a1e49 | 9 | * |
hemddabral | 12:a5d7db4a1e49 | 10 | * Unless required by applicable law or agreed to in writing, software |
hemddabral | 12:a5d7db4a1e49 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
hemddabral | 12:a5d7db4a1e49 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
hemddabral | 12:a5d7db4a1e49 | 13 | * See the License for the specific language governing permissions and |
hemddabral | 12:a5d7db4a1e49 | 14 | * limitations under the License. |
hemddabral | 12:a5d7db4a1e49 | 15 | */ |
hemddabral | 12:a5d7db4a1e49 | 16 | |
hemddabral | 12:a5d7db4a1e49 | 17 | #include "mbed.h" |
hemddabral | 12:a5d7db4a1e49 | 18 | |
hemddabral | 12:a5d7db4a1e49 | 19 | #ifndef __PAYLOAD_H__ |
hemddabral | 12:a5d7db4a1e49 | 20 | #define __PAYLOAD_H__ |
hemddabral | 12:a5d7db4a1e49 | 21 | |
hemddabral | 12:a5d7db4a1e49 | 22 | class UnitPayload |
hemddabral | 12:a5d7db4a1e49 | 23 | { |
hemddabral | 12:a5d7db4a1e49 | 24 | public: |
hemddabral | 12:a5d7db4a1e49 | 25 | uint8_t length; |
hemddabral | 18:6d5f4c638f6a | 26 | uint8_t id; |
hemddabral | 12:a5d7db4a1e49 | 27 | uint8_t *data; |
hemddabral | 18:6d5f4c638f6a | 28 | uint8_t *idptr; |
hemddabral | 18:6d5f4c638f6a | 29 | |
hemddabral | 18:6d5f4c638f6a | 30 | |
hemddabral | 18:6d5f4c638f6a | 31 | |
hemddabral | 12:a5d7db4a1e49 | 32 | void set_length(uint8_t l) { |
hemddabral | 12:a5d7db4a1e49 | 33 | length=l; |
hemddabral | 12:a5d7db4a1e49 | 34 | } |
hemddabral | 12:a5d7db4a1e49 | 35 | |
hemddabral | 12:a5d7db4a1e49 | 36 | void set_id(uint8_t i) { |
hemddabral | 12:a5d7db4a1e49 | 37 | id=i; |
hemddabral | 12:a5d7db4a1e49 | 38 | } |
hemddabral | 12:a5d7db4a1e49 | 39 | |
hemddabral | 12:a5d7db4a1e49 | 40 | void set_data(uint8_t* data1) { |
hemddabral | 12:a5d7db4a1e49 | 41 | for(int j=0;j<length;j++) |
hemddabral | 12:a5d7db4a1e49 | 42 | { |
hemddabral | 12:a5d7db4a1e49 | 43 | data[j]=data1[j]; |
hemddabral | 12:a5d7db4a1e49 | 44 | } |
hemddabral | 12:a5d7db4a1e49 | 45 | } |
hemddabral | 12:a5d7db4a1e49 | 46 | |
hemddabral | 12:a5d7db4a1e49 | 47 | uint8_t get_length() { |
hemddabral | 12:a5d7db4a1e49 | 48 | return length; |
hemddabral | 12:a5d7db4a1e49 | 49 | } |
hemddabral | 12:a5d7db4a1e49 | 50 | |
hemddabral | 12:a5d7db4a1e49 | 51 | uint8_t get_id() { |
hemddabral | 12:a5d7db4a1e49 | 52 | return id; |
hemddabral | 12:a5d7db4a1e49 | 53 | } |
hemddabral | 12:a5d7db4a1e49 | 54 | |
hemddabral | 12:a5d7db4a1e49 | 55 | uint8_t* get_data() { |
hemddabral | 12:a5d7db4a1e49 | 56 | return data; |
hemddabral | 12:a5d7db4a1e49 | 57 | } |
hemddabral | 12:a5d7db4a1e49 | 58 | |
hemddabral | 12:a5d7db4a1e49 | 59 | }; |
hemddabral | 12:a5d7db4a1e49 | 60 | |
hemddabral | 12:a5d7db4a1e49 | 61 | class Payload { |
hemddabral | 12:a5d7db4a1e49 | 62 | UnitPayload *payload; |
hemddabral | 12:a5d7db4a1e49 | 63 | int stringLength; |
hemddabral | 12:a5d7db4a1e49 | 64 | int payloadUnitCount; |
hemddabral | 12:a5d7db4a1e49 | 65 | |
hemddabral | 12:a5d7db4a1e49 | 66 | public: |
hemddabral | 12:a5d7db4a1e49 | 67 | Payload(const uint8_t *tokenString, uint8_t string_ength); |
hemddabral | 12:a5d7db4a1e49 | 68 | Payload::Payload(); |
hemddabral | 12:a5d7db4a1e49 | 69 | uint8_t Payload::getPayloadUnitCount(); |
hemddabral | 12:a5d7db4a1e49 | 70 | |
hemddabral | 12:a5d7db4a1e49 | 71 | uint8_t Payload::getIDAtIndex(int index); |
hemddabral | 12:a5d7db4a1e49 | 72 | uint8_t Payload::getLengthAtIndex(int index); |
hemddabral | 12:a5d7db4a1e49 | 73 | uint8_t* Payload::getDataAtIndex(int index); |
hemddabral | 14:baa7a1464517 | 74 | int8_t getInt8AtIndex(int index); |
hemddabral | 18:6d5f4c638f6a | 75 | uint16_t getUint16AtIndex(int index); |
hemddabral | 18:6d5f4c638f6a | 76 | uint8_t* getSerializedAdDataAtIndex(int index); |
hemddabral | 12:a5d7db4a1e49 | 77 | }; |
hemddabral | 12:a5d7db4a1e49 | 78 | |
hemddabral | 18:6d5f4c638f6a | 79 | |
hemddabral | 18:6d5f4c638f6a | 80 | class PayloadUnit { |
hemddabral | 18:6d5f4c638f6a | 81 | private: |
hemddabral | 18:6d5f4c638f6a | 82 | uint8_t* lenPtr; |
hemddabral | 18:6d5f4c638f6a | 83 | uint8_t* adTypePtr; |
hemddabral | 18:6d5f4c638f6a | 84 | uint8_t* dataPtr; |
hemddabral | 18:6d5f4c638f6a | 85 | |
hemddabral | 18:6d5f4c638f6a | 86 | public: |
hemddabral | 18:6d5f4c638f6a | 87 | PayloadUnit() { |
hemddabral | 18:6d5f4c638f6a | 88 | lenPtr = NULL; |
hemddabral | 18:6d5f4c638f6a | 89 | adTypePtr = NULL; |
hemddabral | 18:6d5f4c638f6a | 90 | dataPtr = NULL; |
hemddabral | 18:6d5f4c638f6a | 91 | } |
hemddabral | 18:6d5f4c638f6a | 92 | |
hemddabral | 18:6d5f4c638f6a | 93 | PayloadUnit(uint8_t *len, uint8_t *adType, uint8_t* data) { |
hemddabral | 18:6d5f4c638f6a | 94 | lenPtr = len; |
hemddabral | 18:6d5f4c638f6a | 95 | adTypePtr = adType; |
hemddabral | 18:6d5f4c638f6a | 96 | dataPtr = data; |
hemddabral | 18:6d5f4c638f6a | 97 | } |
hemddabral | 18:6d5f4c638f6a | 98 | |
hemddabral | 18:6d5f4c638f6a | 99 | void setLenPtr(uint8_t *len) { |
hemddabral | 18:6d5f4c638f6a | 100 | lenPtr = len; |
hemddabral | 18:6d5f4c638f6a | 101 | } |
hemddabral | 18:6d5f4c638f6a | 102 | |
hemddabral | 18:6d5f4c638f6a | 103 | void setAdTypePtr(uint8_t *adType) { |
hemddabral | 18:6d5f4c638f6a | 104 | adTypePtr = adType; |
hemddabral | 18:6d5f4c638f6a | 105 | } |
hemddabral | 18:6d5f4c638f6a | 106 | |
hemddabral | 18:6d5f4c638f6a | 107 | void setDataPtr(uint8_t *data) { |
hemddabral | 18:6d5f4c638f6a | 108 | dataPtr = data; |
hemddabral | 18:6d5f4c638f6a | 109 | } |
hemddabral | 18:6d5f4c638f6a | 110 | |
hemddabral | 18:6d5f4c638f6a | 111 | uint8_t* getLenPtr() { |
hemddabral | 18:6d5f4c638f6a | 112 | return lenPtr; |
hemddabral | 18:6d5f4c638f6a | 113 | } |
hemddabral | 18:6d5f4c638f6a | 114 | |
hemddabral | 18:6d5f4c638f6a | 115 | uint8_t* getAdTypePtr() { |
hemddabral | 18:6d5f4c638f6a | 116 | return adTypePtr; |
hemddabral | 18:6d5f4c638f6a | 117 | } |
hemddabral | 18:6d5f4c638f6a | 118 | |
hemddabral | 18:6d5f4c638f6a | 119 | uint8_t* getDataPtr() { |
hemddabral | 18:6d5f4c638f6a | 120 | return dataPtr; |
hemddabral | 18:6d5f4c638f6a | 121 | } |
hemddabral | 18:6d5f4c638f6a | 122 | |
hemddabral | 18:6d5f4c638f6a | 123 | void printDataAsHex() { |
hemddabral | 18:6d5f4c638f6a | 124 | int i = 0; |
hemddabral | 18:6d5f4c638f6a | 125 | printf("AdData="); |
hemddabral | 18:6d5f4c638f6a | 126 | for(i=0; i<*lenPtr-1; i++) { |
hemddabral | 18:6d5f4c638f6a | 127 | printf("0x%x ", dataPtr[i]); |
hemddabral | 18:6d5f4c638f6a | 128 | } |
hemddabral | 18:6d5f4c638f6a | 129 | printf("\n"); |
hemddabral | 18:6d5f4c638f6a | 130 | } |
hemddabral | 18:6d5f4c638f6a | 131 | |
hemddabral | 18:6d5f4c638f6a | 132 | void printDataAsString() { |
hemddabral | 18:6d5f4c638f6a | 133 | int i = 0; |
hemddabral | 18:6d5f4c638f6a | 134 | printf("AdData="); |
hemddabral | 18:6d5f4c638f6a | 135 | for(i=0; i<*lenPtr; i++) { |
hemddabral | 18:6d5f4c638f6a | 136 | printf("%c", dataPtr[i]); |
hemddabral | 18:6d5f4c638f6a | 137 | } |
hemddabral | 18:6d5f4c638f6a | 138 | printf("\n"); |
hemddabral | 18:6d5f4c638f6a | 139 | } |
hemddabral | 18:6d5f4c638f6a | 140 | |
hemddabral | 18:6d5f4c638f6a | 141 | }; |
hemddabral | 18:6d5f4c638f6a | 142 | |
hemddabral | 18:6d5f4c638f6a | 143 | class PayloadPtr { |
hemddabral | 18:6d5f4c638f6a | 144 | private: |
hemddabral | 18:6d5f4c638f6a | 145 | PayloadUnit *unit; |
hemddabral | 18:6d5f4c638f6a | 146 | int payloadUnitCount; |
hemddabral | 18:6d5f4c638f6a | 147 | public: |
hemddabral | 18:6d5f4c638f6a | 148 | PayloadPtr::PayloadPtr(const uint8_t *tokenString, uint8_t string_ength) { |
hemddabral | 18:6d5f4c638f6a | 149 | // initialize private data members |
hemddabral | 18:6d5f4c638f6a | 150 | int stringLength = string_ength; |
hemddabral | 18:6d5f4c638f6a | 151 | payloadUnitCount = 0; |
hemddabral | 18:6d5f4c638f6a | 152 | |
hemddabral | 18:6d5f4c638f6a | 153 | int index = 0; |
hemddabral | 18:6d5f4c638f6a | 154 | while(index!=stringLength) { |
hemddabral | 18:6d5f4c638f6a | 155 | int len=tokenString[index]; |
hemddabral | 18:6d5f4c638f6a | 156 | index=index+1+len; |
hemddabral | 18:6d5f4c638f6a | 157 | payloadUnitCount++; |
hemddabral | 18:6d5f4c638f6a | 158 | } |
hemddabral | 18:6d5f4c638f6a | 159 | |
hemddabral | 18:6d5f4c638f6a | 160 | // allocate memory to unit |
hemddabral | 18:6d5f4c638f6a | 161 | unit = new PayloadUnit[payloadUnitCount]; |
hemddabral | 18:6d5f4c638f6a | 162 | int i = 0; |
hemddabral | 18:6d5f4c638f6a | 163 | int nextUnitOffset = 0; |
hemddabral | 18:6d5f4c638f6a | 164 | |
hemddabral | 18:6d5f4c638f6a | 165 | while(i<payloadUnitCount) { |
hemddabral | 18:6d5f4c638f6a | 166 | unit[i].setLenPtr((uint8_t *)tokenString+nextUnitOffset); |
hemddabral | 18:6d5f4c638f6a | 167 | unit[i].setAdTypePtr((uint8_t *)tokenString+nextUnitOffset+1); |
hemddabral | 18:6d5f4c638f6a | 168 | unit[i].setDataPtr((uint8_t *)tokenString+nextUnitOffset+2); |
hemddabral | 18:6d5f4c638f6a | 169 | |
hemddabral | 18:6d5f4c638f6a | 170 | nextUnitOffset += *unit[i].getLenPtr()+1; |
hemddabral | 18:6d5f4c638f6a | 171 | i++; |
hemddabral | 18:6d5f4c638f6a | 172 | |
hemddabral | 18:6d5f4c638f6a | 173 | } |
hemddabral | 18:6d5f4c638f6a | 174 | } |
hemddabral | 18:6d5f4c638f6a | 175 | |
hemddabral | 18:6d5f4c638f6a | 176 | PayloadUnit getUnitAtIndex(int index) { |
hemddabral | 18:6d5f4c638f6a | 177 | return unit[index]; |
hemddabral | 18:6d5f4c638f6a | 178 | } |
hemddabral | 18:6d5f4c638f6a | 179 | |
hemddabral | 18:6d5f4c638f6a | 180 | int getPayloadUnitCount() { return payloadUnitCount; } |
hemddabral | 18:6d5f4c638f6a | 181 | |
hemddabral | 18:6d5f4c638f6a | 182 | |
hemddabral | 18:6d5f4c638f6a | 183 | }; |
hemddabral | 18:6d5f4c638f6a | 184 | |
hemddabral | 12:a5d7db4a1e49 | 185 | #endif // __PAYLOAD_H__ |