Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MQTTSNGWEncapsulatedPacket.cpp
00001 /************************************************************************************** 00002 * Copyright (c) 2018, Tomoaki Yamaguchi 00003 * 00004 * All rights reserved. This program and the accompanying materials 00005 * are made available under the terms of the Eclipse Public License v1.0 00006 * and Eclipse Distribution License v1.0 which accompany this distribution. 00007 * 00008 * The Eclipse Public License is available at 00009 * http://www.eclipse.org/legal/epl-v10.html 00010 * and the Eclipse Distribution License is available at 00011 * http://www.eclipse.org/org/documents/edl-v10.php. 00012 * 00013 * Contributors: 00014 * Tomoaki Yamaguchi - initial API and implementation and/or initial documentation 00015 **************************************************************************************/ 00016 #include "MQTTSNGWPacket.h" 00017 #include "MQTTSNGWEncapsulatedPacket.h" 00018 #include "MQTTSNPacket.h" 00019 #include <string.h> 00020 00021 using namespace MQTTSNGW; 00022 using namespace std; 00023 00024 WirelessNodeId::WirelessNodeId() 00025 : 00026 _len{0}, 00027 _nodeId{0} 00028 { 00029 00030 } 00031 00032 WirelessNodeId::~WirelessNodeId() 00033 { 00034 if ( _nodeId ) 00035 { 00036 free(_nodeId); 00037 } 00038 } 00039 00040 void WirelessNodeId::setId(uint8_t* id, uint8_t len) 00041 { 00042 if ( _nodeId ) 00043 { 00044 free(_nodeId); 00045 } 00046 uint8_t* buf = (uint8_t*)malloc(len); 00047 if ( buf ) 00048 { 00049 memcpy(buf, id, len); 00050 _len = len; 00051 _nodeId = buf; 00052 } 00053 else 00054 { 00055 _nodeId = nullptr; 00056 _len = 0; 00057 } 00058 } 00059 00060 void WirelessNodeId::setId(WirelessNodeId* id) 00061 { 00062 setId(id->_nodeId, id->_len); 00063 } 00064 00065 bool WirelessNodeId::operator ==(WirelessNodeId& id) 00066 { 00067 if ( _len == id._len ) 00068 { 00069 return memcmp(_nodeId, id._nodeId, _len) == 0; 00070 } 00071 else 00072 { 00073 return false; 00074 } 00075 } 00076 00077 /* 00078 * Class MQTTSNGWEncapsulatedPacket 00079 */ 00080 MQTTSNGWEncapsulatedPacket::MQTTSNGWEncapsulatedPacket() 00081 : _mqttsn{0}, 00082 _ctrl{0} 00083 { 00084 00085 } 00086 00087 MQTTSNGWEncapsulatedPacket::MQTTSNGWEncapsulatedPacket(MQTTSNPacket* packet) 00088 : _mqttsn{packet}, 00089 _ctrl{0} 00090 { 00091 00092 } 00093 00094 MQTTSNGWEncapsulatedPacket::~MQTTSNGWEncapsulatedPacket() 00095 { 00096 /* Do not delete the MQTTSNPacket. MQTTSNPacket is deleted by delete Event */ 00097 } 00098 00099 int MQTTSNGWEncapsulatedPacket::unicast(SensorNetwork* network, SensorNetAddress* sendTo) 00100 { 00101 uint8_t buf[MQTTSNGW_MAX_PACKET_SIZE]; 00102 int len = serialize(buf); 00103 return network->unicast(buf, len, sendTo); 00104 } 00105 00106 int MQTTSNGWEncapsulatedPacket::serialize(uint8_t* buf) 00107 { 00108 int len = 0; 00109 buf[0] = _id._len + 3; 00110 buf[1] = MQTTSN_ENCAPSULATED; 00111 buf[2] = _ctrl; 00112 memcpy( buf + 3, _id._nodeId, _id._len); 00113 if ( _mqttsn ) 00114 { 00115 len = _mqttsn->getPacketLength(); 00116 memcpy(buf + buf[0], _mqttsn->getPacketData(), len); 00117 } 00118 return buf[0] + len; 00119 } 00120 00121 int MQTTSNGWEncapsulatedPacket::desirialize(unsigned char* buf, unsigned short len) 00122 { 00123 if ( _mqttsn ) 00124 { 00125 delete _mqttsn; 00126 _mqttsn = nullptr; 00127 } 00128 00129 _ctrl = buf[2]; 00130 _id.setId(buf + 3, buf[0] - 3); 00131 00132 _mqttsn = new MQTTSNPacket; 00133 _mqttsn->desirialize(buf + buf[0], len - buf[0]); 00134 return buf[0]; 00135 } 00136 00137 int MQTTSNGWEncapsulatedPacket::getType(void) 00138 { 00139 return MQTTSN_ENCAPSULATED; 00140 } 00141 00142 const char* MQTTSNGWEncapsulatedPacket::getName() 00143 { 00144 return MQTTSNPacket_name(MQTTSN_ENCAPSULATED); 00145 } 00146 00147 MQTTSNPacket* MQTTSNGWEncapsulatedPacket::getMQTTSNPacket(void) 00148 { 00149 return _mqttsn; 00150 } 00151 00152 WirelessNodeId* MQTTSNGWEncapsulatedPacket::getWirelessNodeId(void) 00153 { 00154 return &_id; 00155 } 00156 00157 void MQTTSNGWEncapsulatedPacket::setWirelessNodeId(WirelessNodeId* id) 00158 { 00159 _id.setId(id); 00160 } 00161 00162 char* MQTTSNGWEncapsulatedPacket::print(char* pbuf) 00163 { 00164 char* ptr = pbuf; 00165 char** pptr = &pbuf; 00166 00167 uint8_t buf[MQTTSNGW_MAX_PACKET_SIZE]; 00168 int len = serialize(buf); 00169 int size = len > SIZE_OF_LOG_PACKET ? SIZE_OF_LOG_PACKET : len; 00170 00171 for (int i = 1; i < size; i++) 00172 { 00173 sprintf(*pptr, " %02X", *(buf + i)); 00174 *pptr += 3; 00175 } 00176 **pptr = 0; 00177 return ptr; 00178 } 00179
Generated on Wed Jul 13 2022 10:46:03 by
1.7.2