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