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