Fork of BlueNRG library to be compatible with bluetooth demo application
Dependents: Nucleo_BLE_Demo Nucleo_BLE_Demo
Fork of Nucleo_BLE_BlueNRG by
Payload.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 00019 #ifndef __PAYLOAD_H__ 00020 #define __PAYLOAD_H__ 00021 00022 class UnitPayload 00023 { 00024 public: 00025 uint8_t length; 00026 uint8_t id; 00027 uint8_t *data; 00028 uint8_t *idptr; 00029 00030 00031 00032 void set_length(uint8_t l) { 00033 length=l; 00034 } 00035 00036 void set_id(uint8_t i) { 00037 id=i; 00038 } 00039 00040 void set_data(uint8_t* data1) { 00041 for(int j=0;j<length;j++) 00042 { 00043 data[j]=data1[j]; 00044 } 00045 } 00046 00047 uint8_t get_length() { 00048 return length; 00049 } 00050 00051 uint8_t get_id() { 00052 return id; 00053 } 00054 00055 uint8_t* get_data() { 00056 return data; 00057 } 00058 00059 }; 00060 00061 class Payload { 00062 UnitPayload *payload; 00063 int stringLength; 00064 int payloadUnitCount; 00065 00066 public: 00067 Payload(const uint8_t *tokenString, uint8_t string_ength); 00068 Payload(); 00069 uint8_t getPayloadUnitCount(); 00070 00071 uint8_t getIDAtIndex(int index); 00072 uint8_t getLengthAtIndex(int index); 00073 uint8_t* getDataAtIndex(int index); 00074 int8_t getInt8AtIndex(int index); 00075 uint16_t getUint16AtIndex(int index); 00076 uint8_t* getSerializedAdDataAtIndex(int index); 00077 }; 00078 00079 00080 class PayloadUnit { 00081 private: 00082 uint8_t* lenPtr; 00083 uint8_t* adTypePtr; 00084 uint8_t* dataPtr; 00085 00086 public: 00087 PayloadUnit() { 00088 lenPtr = NULL; 00089 adTypePtr = NULL; 00090 dataPtr = NULL; 00091 } 00092 00093 PayloadUnit(uint8_t *len, uint8_t *adType, uint8_t* data) { 00094 lenPtr = len; 00095 adTypePtr = adType; 00096 dataPtr = data; 00097 } 00098 00099 void setLenPtr(uint8_t *len) { 00100 lenPtr = len; 00101 } 00102 00103 void setAdTypePtr(uint8_t *adType) { 00104 adTypePtr = adType; 00105 } 00106 00107 void setDataPtr(uint8_t *data) { 00108 dataPtr = data; 00109 } 00110 00111 uint8_t* getLenPtr() { 00112 return lenPtr; 00113 } 00114 00115 uint8_t* getAdTypePtr() { 00116 return adTypePtr; 00117 } 00118 00119 uint8_t* getDataPtr() { 00120 return dataPtr; 00121 } 00122 00123 void printDataAsHex() { 00124 int i = 0; 00125 printf("AdData="); 00126 for(i=0; i<*lenPtr-1; i++) { 00127 printf("0x%x ", dataPtr[i]); 00128 } 00129 printf("\n"); 00130 } 00131 00132 void printDataAsString() { 00133 int i = 0; 00134 printf("AdData="); 00135 for(i=0; i<*lenPtr; i++) { 00136 printf("%c", dataPtr[i]); 00137 } 00138 printf("\n"); 00139 } 00140 00141 }; 00142 00143 class PayloadPtr { 00144 private: 00145 PayloadUnit *unit; 00146 int payloadUnitCount; 00147 public: 00148 PayloadPtr(const uint8_t *tokenString, uint8_t string_ength) { 00149 // initialize private data members 00150 int stringLength = string_ength; 00151 payloadUnitCount = 0; 00152 00153 int index = 0; 00154 while(index!=stringLength) { 00155 int len=tokenString[index]; 00156 index=index+1+len; 00157 payloadUnitCount++; 00158 } 00159 00160 // allocate memory to unit 00161 unit = new PayloadUnit[payloadUnitCount]; 00162 int i = 0; 00163 int nextUnitOffset = 0; 00164 00165 while(i<payloadUnitCount) { 00166 unit[i].setLenPtr((uint8_t *)tokenString+nextUnitOffset); 00167 unit[i].setAdTypePtr((uint8_t *)tokenString+nextUnitOffset+1); 00168 unit[i].setDataPtr((uint8_t *)tokenString+nextUnitOffset+2); 00169 00170 nextUnitOffset += *unit[i].getLenPtr()+1; 00171 i++; 00172 00173 } 00174 } 00175 00176 PayloadUnit getUnitAtIndex(int index) { 00177 return unit[index]; 00178 } 00179 00180 int getPayloadUnitCount() { return payloadUnitCount; } 00181 00182 00183 }; 00184 00185 #endif // __PAYLOAD_H__
Generated on Sat Jul 16 2022 01:53:16 by 1.7.2