LoRa_Miun_Lib

Dependents:   Smart_Miun

Committer:
biwa1400
Date:
Wed Oct 11 10:28:46 2017 +0000
Revision:
4:522bba1c5fa1
20171011

Who changed what in which revision?

UserRevisionLine numberNew contents of line
biwa1400 4:522bba1c5fa1 1 #include "MIUN.LoRa.h"
biwa1400 4:522bba1c5fa1 2 #include "mbed.h"
biwa1400 4:522bba1c5fa1 3 #include "mDot.h"
biwa1400 4:522bba1c5fa1 4 #include "MacEvents.h"
biwa1400 4:522bba1c5fa1 5 #include "MTSLog.h"
biwa1400 4:522bba1c5fa1 6 #include "MTSText.h"
biwa1400 4:522bba1c5fa1 7 #include <vector>
biwa1400 4:522bba1c5fa1 8 #include <string>
biwa1400 4:522bba1c5fa1 9
biwa1400 4:522bba1c5fa1 10
biwa1400 4:522bba1c5fa1 11 MIUN::LoRa::LoRa():macCommandEvent(*this)
biwa1400 4:522bba1c5fa1 12 {
biwa1400 4:522bba1c5fa1 13
biwa1400 4:522bba1c5fa1 14 //1. Set dot
biwa1400 4:522bba1c5fa1 15 dot = mDot::getInstance();
biwa1400 4:522bba1c5fa1 16
biwa1400 4:522bba1c5fa1 17 //2. Register Event
biwa1400 4:522bba1c5fa1 18 dot->setEvents(&macCommandEvent);
biwa1400 4:522bba1c5fa1 19
biwa1400 4:522bba1c5fa1 20 //3. getDefaultSleepTime
biwa1400 4:522bba1c5fa1 21 //defaultSleepTime = readSleepTime();
biwa1400 4:522bba1c5fa1 22 defaultSleepTime = 30; //seconds
biwa1400 4:522bba1c5fa1 23
biwa1400 4:522bba1c5fa1 24 //4. set batteryLevel
biwa1400 4:522bba1c5fa1 25 batteryLevel = 255;
biwa1400 4:522bba1c5fa1 26
biwa1400 4:522bba1c5fa1 27 //5. Print Version
biwa1400 4:522bba1c5fa1 28 logInfo("Library Version: %s", dot->getId().c_str());
biwa1400 4:522bba1c5fa1 29
biwa1400 4:522bba1c5fa1 30
biwa1400 4:522bba1c5fa1 31 }
biwa1400 4:522bba1c5fa1 32
biwa1400 4:522bba1c5fa1 33 void MIUN::LoRa::reset()
biwa1400 4:522bba1c5fa1 34 {
biwa1400 4:522bba1c5fa1 35 // start from a well-known state
biwa1400 4:522bba1c5fa1 36 logInfo("defaulting Dot configuration");
biwa1400 4:522bba1c5fa1 37 dot->resetConfig();
biwa1400 4:522bba1c5fa1 38 dot->resetNetworkSession();
biwa1400 4:522bba1c5fa1 39 }
biwa1400 4:522bba1c5fa1 40
biwa1400 4:522bba1c5fa1 41
biwa1400 4:522bba1c5fa1 42 /*** public method ***/
biwa1400 4:522bba1c5fa1 43
biwa1400 4:522bba1c5fa1 44 std::string MIUN::LoRa::receive(int* port)
biwa1400 4:522bba1c5fa1 45 {
biwa1400 4:522bba1c5fa1 46 int32_t ret;
biwa1400 4:522bba1c5fa1 47 vector<uint8_t> receive_data;
biwa1400 4:522bba1c5fa1 48 if ((ret = dot->recv(receive_data)) != mDot::MDOT_OK)
biwa1400 4:522bba1c5fa1 49 {
biwa1400 4:522bba1c5fa1 50 logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
biwa1400 4:522bba1c5fa1 51 return "";
biwa1400 4:522bba1c5fa1 52 }
biwa1400 4:522bba1c5fa1 53 else
biwa1400 4:522bba1c5fa1 54 {
biwa1400 4:522bba1c5fa1 55 //1. Set Receive Payload
biwa1400 4:522bba1c5fa1 56 string str(receive_data.begin(),receive_data.end());
biwa1400 4:522bba1c5fa1 57 //2. Set Receive Port
biwa1400 4:522bba1c5fa1 58 *port = receivePort;
biwa1400 4:522bba1c5fa1 59 receivePort = -1;
biwa1400 4:522bba1c5fa1 60 return str;
biwa1400 4:522bba1c5fa1 61 }
biwa1400 4:522bba1c5fa1 62 }
biwa1400 4:522bba1c5fa1 63
biwa1400 4:522bba1c5fa1 64
biwa1400 4:522bba1c5fa1 65 bool MIUN::LoRa::send_basic(std::string input_data)
biwa1400 4:522bba1c5fa1 66 {
biwa1400 4:522bba1c5fa1 67 int32_t ret;
biwa1400 4:522bba1c5fa1 68 vector<uint8_t> sent_data;
biwa1400 4:522bba1c5fa1 69 logInfo("Send with %s", dot->getDateRateDetails(dot->getTxDataRate()).c_str());
biwa1400 4:522bba1c5fa1 70 for (std::string::iterator it = input_data.begin(); it != input_data.end(); it++)
biwa1400 4:522bba1c5fa1 71 {
biwa1400 4:522bba1c5fa1 72 sent_data.push_back((uint8_t) *it);
biwa1400 4:522bba1c5fa1 73 }
biwa1400 4:522bba1c5fa1 74 std::vector<uint8_t> sendData;
biwa1400 4:522bba1c5fa1 75 sendData.push_back(0x80);
biwa1400 4:522bba1c5fa1 76 /*
biwa1400 4:522bba1c5fa1 77 for (int i=0;i<commandLength;i++)
biwa1400 4:522bba1c5fa1 78 {
biwa1400 4:522bba1c5fa1 79 sendData.push_back(command[i]);
biwa1400 4:522bba1c5fa1 80 }
biwa1400 4:522bba1c5fa1 81 */
biwa1400 4:522bba1c5fa1 82 if (dot->injectMacCommand(sendData) != mDot::MDOT_OK)
biwa1400 4:522bba1c5fa1 83 {
biwa1400 4:522bba1c5fa1 84 logError("failed to injectMacCommand");
biwa1400 4:522bba1c5fa1 85 }
biwa1400 4:522bba1c5fa1 86 else
biwa1400 4:522bba1c5fa1 87 {
biwa1400 4:522bba1c5fa1 88 logInfo("injectMacCommand Successful");
biwa1400 4:522bba1c5fa1 89 }
biwa1400 4:522bba1c5fa1 90 if ((ret = dot->send(sent_data)) != mDot::MDOT_OK)
biwa1400 4:522bba1c5fa1 91 {
biwa1400 4:522bba1c5fa1 92 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
biwa1400 4:522bba1c5fa1 93 return false;
biwa1400 4:522bba1c5fa1 94 }
biwa1400 4:522bba1c5fa1 95 else
biwa1400 4:522bba1c5fa1 96 {
biwa1400 4:522bba1c5fa1 97 logInfo("successfully sent data to gateway");
biwa1400 4:522bba1c5fa1 98 return true;
biwa1400 4:522bba1c5fa1 99 }
biwa1400 4:522bba1c5fa1 100
biwa1400 4:522bba1c5fa1 101 }
biwa1400 4:522bba1c5fa1 102
biwa1400 4:522bba1c5fa1 103 std::string MIUN::LoRa::sendReceive(std::string payload, int port, int* receivePort)
biwa1400 4:522bba1c5fa1 104 {
biwa1400 4:522bba1c5fa1 105 if(send(payload,port))
biwa1400 4:522bba1c5fa1 106 {
biwa1400 4:522bba1c5fa1 107 return receive(receivePort);
biwa1400 4:522bba1c5fa1 108 }
biwa1400 4:522bba1c5fa1 109 else
biwa1400 4:522bba1c5fa1 110 {
biwa1400 4:522bba1c5fa1 111 return "";
biwa1400 4:522bba1c5fa1 112 }
biwa1400 4:522bba1c5fa1 113 }
biwa1400 4:522bba1c5fa1 114
biwa1400 4:522bba1c5fa1 115
biwa1400 4:522bba1c5fa1 116 void MIUN::LoRa::sleep(const uint32_t& interval_s)
biwa1400 4:522bba1c5fa1 117 {
biwa1400 4:522bba1c5fa1 118 uint32_t sleep_time = std::max(interval_s*1000, (uint32_t)dot->getNextTxMs()) / 1000;
biwa1400 4:522bba1c5fa1 119 logInfo("sleep %u seconds until next free channel",sleep_time);
biwa1400 4:522bba1c5fa1 120 dot->sleep(sleep_time, mDot::RTC_ALARM, true);
biwa1400 4:522bba1c5fa1 121 }
biwa1400 4:522bba1c5fa1 122
biwa1400 4:522bba1c5fa1 123 void MIUN::LoRa::sleepWaitingNextFreeChannel()
biwa1400 4:522bba1c5fa1 124 {
biwa1400 4:522bba1c5fa1 125 uint32_t sleep_time =std::max((uint32_t)10*1000, (uint32_t)dot->getNextTxMs()) / 1000;
biwa1400 4:522bba1c5fa1 126 logInfo("sleep %u seconds until next free channel",sleep_time);
biwa1400 4:522bba1c5fa1 127 dot->sleep(sleep_time, mDot::RTC_ALARM, false);
biwa1400 4:522bba1c5fa1 128 }
biwa1400 4:522bba1c5fa1 129
biwa1400 4:522bba1c5fa1 130 void MIUN::LoRa::sleep()
biwa1400 4:522bba1c5fa1 131 {
biwa1400 4:522bba1c5fa1 132 if(sleepTime!=0)
biwa1400 4:522bba1c5fa1 133 {
biwa1400 4:522bba1c5fa1 134 sleep(sleepTime);
biwa1400 4:522bba1c5fa1 135 }
biwa1400 4:522bba1c5fa1 136 }
biwa1400 4:522bba1c5fa1 137
biwa1400 4:522bba1c5fa1 138 bool MIUN::LoRa::joinNetwork ()
biwa1400 4:522bba1c5fa1 139 {
biwa1400 4:522bba1c5fa1 140 if (!dot->getNetworkJoinStatus())
biwa1400 4:522bba1c5fa1 141 {
biwa1400 4:522bba1c5fa1 142 //1. Try Connect
biwa1400 4:522bba1c5fa1 143 if(tryConnectNet() == false)
biwa1400 4:522bba1c5fa1 144 {
biwa1400 4:522bba1c5fa1 145 return false;
biwa1400 4:522bba1c5fa1 146 }
biwa1400 4:522bba1c5fa1 147 //2. reset SF
biwa1400 4:522bba1c5fa1 148 if(dot->getAdr() == true)
biwa1400 4:522bba1c5fa1 149 {
biwa1400 4:522bba1c5fa1 150 resetSF();
biwa1400 4:522bba1c5fa1 151 }
biwa1400 4:522bba1c5fa1 152 }
biwa1400 4:522bba1c5fa1 153 return true;
biwa1400 4:522bba1c5fa1 154 }
biwa1400 4:522bba1c5fa1 155
biwa1400 4:522bba1c5fa1 156
biwa1400 4:522bba1c5fa1 157
biwa1400 4:522bba1c5fa1 158
biwa1400 4:522bba1c5fa1 159
biwa1400 4:522bba1c5fa1 160 void MIUN::LoRa::networkConfig(std::string network_name,
biwa1400 4:522bba1c5fa1 161 std::string network_passphrase,
biwa1400 4:522bba1c5fa1 162 uint8_t retransTimes,
biwa1400 4:522bba1c5fa1 163 uint8_t joinDelay,
biwa1400 4:522bba1c5fa1 164 bool ADR,
biwa1400 4:522bba1c5fa1 165 uint8_t sf)
biwa1400 4:522bba1c5fa1 166 {
biwa1400 4:522bba1c5fa1 167 //1. Reset
biwa1400 4:522bba1c5fa1 168 reset();
biwa1400 4:522bba1c5fa1 169
biwa1400 4:522bba1c5fa1 170 //2. make sure library logging is turned on
biwa1400 4:522bba1c5fa1 171 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
biwa1400 4:522bba1c5fa1 172
biwa1400 4:522bba1c5fa1 173 //3. Change mode to AUTO_OTA
biwa1400 4:522bba1c5fa1 174 changeModeToAUTO_OTA();
biwa1400 4:522bba1c5fa1 175
biwa1400 4:522bba1c5fa1 176
biwa1400 4:522bba1c5fa1 177 //4. Change network name
biwa1400 4:522bba1c5fa1 178 changeNetworkName(network_name);
biwa1400 4:522bba1c5fa1 179
biwa1400 4:522bba1c5fa1 180 //5. Change Password
biwa1400 4:522bba1c5fa1 181 changePassword(network_passphrase);
biwa1400 4:522bba1c5fa1 182
biwa1400 4:522bba1c5fa1 183 //6. Change Ack
biwa1400 4:522bba1c5fa1 184 changeAck(retransTimes);
biwa1400 4:522bba1c5fa1 185
biwa1400 4:522bba1c5fa1 186 //7. Public Network
biwa1400 4:522bba1c5fa1 187 changePublicNetwork(true);
biwa1400 4:522bba1c5fa1 188
biwa1400 4:522bba1c5fa1 189 //8. Change Join Delay
biwa1400 4:522bba1c5fa1 190 changeJoinDelay(joinDelay);
biwa1400 4:522bba1c5fa1 191
biwa1400 4:522bba1c5fa1 192 //9. Set Adapt Datarate
biwa1400 4:522bba1c5fa1 193 changeAdaptSF(ADR);
biwa1400 4:522bba1c5fa1 194
biwa1400 4:522bba1c5fa1 195 //9. Change sf
biwa1400 4:522bba1c5fa1 196 changeSF(ADR);
biwa1400 4:522bba1c5fa1 197
biwa1400 4:522bba1c5fa1 198 //10. Save Default Time
biwa1400 4:522bba1c5fa1 199 //saveSleepTime(defaultSleepTime);
biwa1400 4:522bba1c5fa1 200
biwa1400 4:522bba1c5fa1 201 //10. Save Setting
biwa1400 4:522bba1c5fa1 202 saveSetting();
biwa1400 4:522bba1c5fa1 203
biwa1400 4:522bba1c5fa1 204
biwa1400 4:522bba1c5fa1 205 }
biwa1400 4:522bba1c5fa1 206
biwa1400 4:522bba1c5fa1 207
biwa1400 4:522bba1c5fa1 208 /*
biwa1400 4:522bba1c5fa1 209 void MIUN::LoRa::saveSleepTime(uint32_t sleepTime)
biwa1400 4:522bba1c5fa1 210 {
biwa1400 4:522bba1c5fa1 211 dot->saveUserFile("sleepTm",(void*)&sleepTime,4);
biwa1400 4:522bba1c5fa1 212 }
biwa1400 4:522bba1c5fa1 213
biwa1400 4:522bba1c5fa1 214 uint32_t MIUN::LoRa::readSleepTime()
biwa1400 4:522bba1c5fa1 215 {
biwa1400 4:522bba1c5fa1 216 uint8_t intBuffer[4];
biwa1400 4:522bba1c5fa1 217 dot->readUserFile("sleepTm",intBuffer,4);
biwa1400 4:522bba1c5fa1 218 return sleepTime = (((uint32_t)intBuffer[3])<<24)+
biwa1400 4:522bba1c5fa1 219 (((uint32_t)intBuffer[2])<<16)+
biwa1400 4:522bba1c5fa1 220 (((uint32_t)intBuffer[1])<<8)+
biwa1400 4:522bba1c5fa1 221 intBuffer[0];
biwa1400 4:522bba1c5fa1 222 }
biwa1400 4:522bba1c5fa1 223 */
biwa1400 4:522bba1c5fa1 224
biwa1400 4:522bba1c5fa1 225 bool MIUN::LoRa::checkSleepTime(uint32_t sleepTime)
biwa1400 4:522bba1c5fa1 226 {
biwa1400 4:522bba1c5fa1 227 if(sleepTime<60*60*24)
biwa1400 4:522bba1c5fa1 228 {
biwa1400 4:522bba1c5fa1 229 return true;
biwa1400 4:522bba1c5fa1 230 }
biwa1400 4:522bba1c5fa1 231 logError("Wrong Sleep Time");
biwa1400 4:522bba1c5fa1 232 return false;
biwa1400 4:522bba1c5fa1 233 }
biwa1400 4:522bba1c5fa1 234
biwa1400 4:522bba1c5fa1 235
biwa1400 4:522bba1c5fa1 236
biwa1400 4:522bba1c5fa1 237 void MIUN::LoRa::setFPending(bool isPending)
biwa1400 4:522bba1c5fa1 238 {
biwa1400 4:522bba1c5fa1 239 uint8_t macArray[50]={'\0'};
biwa1400 4:522bba1c5fa1 240 memcpy(macArray,dot->_mote.GetMac(),50);
biwa1400 4:522bba1c5fa1 241 for(int i=0;i<50;i++)
biwa1400 4:522bba1c5fa1 242 {
biwa1400 4:522bba1c5fa1 243 logError("mac: %.2x", macArray[i]);
biwa1400 4:522bba1c5fa1 244
biwa1400 4:522bba1c5fa1 245 }
biwa1400 4:522bba1c5fa1 246
biwa1400 4:522bba1c5fa1 247 }
biwa1400 4:522bba1c5fa1 248
biwa1400 4:522bba1c5fa1 249 void MIUN::LoRa::setSleepTime(uint32_t inSleepTime)
biwa1400 4:522bba1c5fa1 250 {
biwa1400 4:522bba1c5fa1 251 if(checkSleepTime(inSleepTime)==true)
biwa1400 4:522bba1c5fa1 252 {
biwa1400 4:522bba1c5fa1 253 sleepTime = inSleepTime;
biwa1400 4:522bba1c5fa1 254 logInfo("SetSleepTimeTo: %u", inSleepTime);
biwa1400 4:522bba1c5fa1 255 }
biwa1400 4:522bba1c5fa1 256
biwa1400 4:522bba1c5fa1 257 }
biwa1400 4:522bba1c5fa1 258
biwa1400 4:522bba1c5fa1 259 void MIUN::LoRa::changeSF(const uint8_t& sf)
biwa1400 4:522bba1c5fa1 260 {
biwa1400 4:522bba1c5fa1 261 uint8_t current_SF = dot->getTxDataRate();
biwa1400 4:522bba1c5fa1 262 if (current_SF != sf)
biwa1400 4:522bba1c5fa1 263 {
biwa1400 4:522bba1c5fa1 264 logInfo("changing spreading factor from %s to %s", dot->getDateRateDetails(current_SF).c_str(), dot->getDateRateDetails(sf).c_str());
biwa1400 4:522bba1c5fa1 265 if (dot->setTxDataRate(sf)!= mDot::MDOT_OK) {
biwa1400 4:522bba1c5fa1 266 logError("failed to change SF to %s", dot->getDateRateDetails(sf).c_str());
biwa1400 4:522bba1c5fa1 267 }
biwa1400 4:522bba1c5fa1 268 }
biwa1400 4:522bba1c5fa1 269 }
biwa1400 4:522bba1c5fa1 270
biwa1400 4:522bba1c5fa1 271 void MIUN::LoRa::changeAdaptSF(bool adaptSF)
biwa1400 4:522bba1c5fa1 272 {
biwa1400 4:522bba1c5fa1 273 bool current_adaptSF = dot->getAdr();
biwa1400 4:522bba1c5fa1 274 if (current_adaptSF != adaptSF)
biwa1400 4:522bba1c5fa1 275 {
biwa1400 4:522bba1c5fa1 276 logInfo("changing AdaptSF from %s to %s", current_adaptSF ? "on" : "off", adaptSF ? "on" : "off");
biwa1400 4:522bba1c5fa1 277 if (dot->setAdr(adaptSF) != mDot::MDOT_OK) {
biwa1400 4:522bba1c5fa1 278 logError("failed to set AdaptSF to %s", adaptSF ? "on" : "off");
biwa1400 4:522bba1c5fa1 279 }
biwa1400 4:522bba1c5fa1 280 }
biwa1400 4:522bba1c5fa1 281 }
biwa1400 4:522bba1c5fa1 282
biwa1400 4:522bba1c5fa1 283 void MIUN::LoRa::changePublicNetwork(bool public_network)
biwa1400 4:522bba1c5fa1 284 {
biwa1400 4:522bba1c5fa1 285 bool current_public_network = dot->getPublicNetwork();
biwa1400 4:522bba1c5fa1 286 if (current_public_network != public_network)
biwa1400 4:522bba1c5fa1 287 {
biwa1400 4:522bba1c5fa1 288 logInfo("changing public network from %s to %s", current_public_network ? "on" : "off", public_network ? "on" : "off");
biwa1400 4:522bba1c5fa1 289 if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) {
biwa1400 4:522bba1c5fa1 290 logError("failed to set public network to %s", public_network ? "on" : "off");
biwa1400 4:522bba1c5fa1 291 }
biwa1400 4:522bba1c5fa1 292 }
biwa1400 4:522bba1c5fa1 293 }
biwa1400 4:522bba1c5fa1 294
biwa1400 4:522bba1c5fa1 295 void MIUN::LoRa::changeModeToAUTO_OTA()
biwa1400 4:522bba1c5fa1 296 {
biwa1400 4:522bba1c5fa1 297 if (dot->getJoinMode() != mDot::AUTO_OTA)
biwa1400 4:522bba1c5fa1 298 {
biwa1400 4:522bba1c5fa1 299 logInfo("changing network join mode to AUTO_OTA");
biwa1400 4:522bba1c5fa1 300 if (dot->setJoinMode(mDot::AUTO_OTA) != mDot::MDOT_OK)
biwa1400 4:522bba1c5fa1 301 {
biwa1400 4:522bba1c5fa1 302 logError("failed to set network join mode to AUTO_OTA");
biwa1400 4:522bba1c5fa1 303 }
biwa1400 4:522bba1c5fa1 304 }
biwa1400 4:522bba1c5fa1 305 }
biwa1400 4:522bba1c5fa1 306
biwa1400 4:522bba1c5fa1 307 void MIUN::LoRa::changePassword(const std::string& network_passphrase)
biwa1400 4:522bba1c5fa1 308 {
biwa1400 4:522bba1c5fa1 309 std::string current_network_passphrase = dot->getNetworkPassphrase();
biwa1400 4:522bba1c5fa1 310 if (current_network_passphrase != network_passphrase)
biwa1400 4:522bba1c5fa1 311 {
biwa1400 4:522bba1c5fa1 312 logInfo("changing network passphrase from \"%s\" to \"%s\"", current_network_passphrase.c_str(), network_passphrase.c_str());
biwa1400 4:522bba1c5fa1 313 if (dot->setNetworkPassphrase(network_passphrase) != mDot::MDOT_OK)
biwa1400 4:522bba1c5fa1 314 {
biwa1400 4:522bba1c5fa1 315 logError("failed to set network passphrase to \"%s\"", network_passphrase.c_str());
biwa1400 4:522bba1c5fa1 316 }
biwa1400 4:522bba1c5fa1 317 }
biwa1400 4:522bba1c5fa1 318 }
biwa1400 4:522bba1c5fa1 319
biwa1400 4:522bba1c5fa1 320 void MIUN::LoRa::changeNetworkName(const std::string& network_name)
biwa1400 4:522bba1c5fa1 321 {
biwa1400 4:522bba1c5fa1 322 std::string current_network_name = dot->getNetworkName();
biwa1400 4:522bba1c5fa1 323 if (current_network_name != network_name)
biwa1400 4:522bba1c5fa1 324 {
biwa1400 4:522bba1c5fa1 325 if (current_network_name != network_name)
biwa1400 4:522bba1c5fa1 326 {
biwa1400 4:522bba1c5fa1 327 logInfo("changing network name from \"%s\" to \"%s\"", current_network_name.c_str(), network_name.c_str());
biwa1400 4:522bba1c5fa1 328 if (dot->setNetworkName(network_name) != mDot::MDOT_OK) {
biwa1400 4:522bba1c5fa1 329 logError("failed to set network name to \"%s\"", network_name.c_str());
biwa1400 4:522bba1c5fa1 330 }
biwa1400 4:522bba1c5fa1 331 }
biwa1400 4:522bba1c5fa1 332 }
biwa1400 4:522bba1c5fa1 333 }
biwa1400 4:522bba1c5fa1 334
biwa1400 4:522bba1c5fa1 335 void MIUN::LoRa::changeJoinDelay(uint8_t joinDelay)
biwa1400 4:522bba1c5fa1 336 {
biwa1400 4:522bba1c5fa1 337 logInfo("changing join delay");
biwa1400 4:522bba1c5fa1 338 if (dot->setJoinDelay(joinDelay) != mDot::MDOT_OK)
biwa1400 4:522bba1c5fa1 339 {
biwa1400 4:522bba1c5fa1 340 logError("failed to set join delay to %u", joinDelay);
biwa1400 4:522bba1c5fa1 341 }
biwa1400 4:522bba1c5fa1 342 }
biwa1400 4:522bba1c5fa1 343
biwa1400 4:522bba1c5fa1 344 void MIUN::LoRa::changeAck(const uint8_t& retries)
biwa1400 4:522bba1c5fa1 345 {
biwa1400 4:522bba1c5fa1 346 logInfo("Set Ack to %u", retries);
biwa1400 4:522bba1c5fa1 347 if (dot->setAck(retries) != mDot::MDOT_OK) {
biwa1400 4:522bba1c5fa1 348 logError("failed to set Ack to %u", retries);
biwa1400 4:522bba1c5fa1 349 }
biwa1400 4:522bba1c5fa1 350 }
biwa1400 4:522bba1c5fa1 351
biwa1400 4:522bba1c5fa1 352
biwa1400 4:522bba1c5fa1 353
biwa1400 4:522bba1c5fa1 354 void MIUN::LoRa::saveSetting()
biwa1400 4:522bba1c5fa1 355 {
biwa1400 4:522bba1c5fa1 356 logInfo("saving configuration");
biwa1400 4:522bba1c5fa1 357 if (!dot->saveConfig())
biwa1400 4:522bba1c5fa1 358 {
biwa1400 4:522bba1c5fa1 359 logError("failed to save configuration");
biwa1400 4:522bba1c5fa1 360 }
biwa1400 4:522bba1c5fa1 361 }
biwa1400 4:522bba1c5fa1 362
biwa1400 4:522bba1c5fa1 363 void MIUN::LoRa::showInfo()
biwa1400 4:522bba1c5fa1 364 {
biwa1400 4:522bba1c5fa1 365 // display configuration and library version information
biwa1400 4:522bba1c5fa1 366 logInfo("=====================");
biwa1400 4:522bba1c5fa1 367 logInfo("general configuration");
biwa1400 4:522bba1c5fa1 368 logInfo("=====================");
biwa1400 4:522bba1c5fa1 369 logInfo("version ------------------ %s", dot->getId().c_str());
biwa1400 4:522bba1c5fa1 370 logInfo("device ID/EUI ------------ %s", mts::Text::bin2hexString(dot->getDeviceId()).c_str());
biwa1400 4:522bba1c5fa1 371 logInfo("frequency band ----------- %s", mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str());
biwa1400 4:522bba1c5fa1 372 if (dot->getFrequencySubBand() != mDot::FB_EU868) {
biwa1400 4:522bba1c5fa1 373 logInfo("frequency sub band ------- %u", dot->getFrequencySubBand());
biwa1400 4:522bba1c5fa1 374 }
biwa1400 4:522bba1c5fa1 375 logInfo("public network ----------- %s", dot->getPublicNetwork() ? "on" : "off");
biwa1400 4:522bba1c5fa1 376 logInfo("=========================");
biwa1400 4:522bba1c5fa1 377 logInfo("credentials configuration");
biwa1400 4:522bba1c5fa1 378 logInfo("=========================");
biwa1400 4:522bba1c5fa1 379 logInfo("device class ------------- %s", dot->getClass().c_str());
biwa1400 4:522bba1c5fa1 380 logInfo("network join mode -------- %s", mDot::JoinModeStr(dot->getJoinMode()).c_str());
biwa1400 4:522bba1c5fa1 381 if (dot->getJoinMode() == mDot::MANUAL || dot->getJoinMode() == mDot::PEER_TO_PEER) {
biwa1400 4:522bba1c5fa1 382 logInfo("network address ---------- %s", mts::Text::bin2hexString(dot->getNetworkAddress()).c_str());
biwa1400 4:522bba1c5fa1 383 logInfo("network session key------- %s", mts::Text::bin2hexString(dot->getNetworkSessionKey()).c_str());
biwa1400 4:522bba1c5fa1 384 logInfo("data session key---------- %s", mts::Text::bin2hexString(dot->getDataSessionKey()).c_str());
biwa1400 4:522bba1c5fa1 385 } else {
biwa1400 4:522bba1c5fa1 386 logInfo("network name ------------- %s", dot->getNetworkName().c_str());
biwa1400 4:522bba1c5fa1 387 logInfo("network phrase ----------- %s", dot->getNetworkPassphrase().c_str());
biwa1400 4:522bba1c5fa1 388 logInfo("network EUI -------------- %s", mts::Text::bin2hexString(dot->getNetworkId()).c_str());
biwa1400 4:522bba1c5fa1 389 logInfo("network KEY -------------- %s", mts::Text::bin2hexString(dot->getNetworkKey()).c_str());
biwa1400 4:522bba1c5fa1 390
biwa1400 4:522bba1c5fa1 391 logInfo("session NWK KEY -------------- %s", mts::Text::bin2hexString(dot->getNetworkSessionKey()).c_str());
biwa1400 4:522bba1c5fa1 392 logInfo("session APP KEY -------------- %s", mts::Text::bin2hexString(dot->getDataSessionKey()).c_str());
biwa1400 4:522bba1c5fa1 393 }
biwa1400 4:522bba1c5fa1 394 logInfo("========================");
biwa1400 4:522bba1c5fa1 395 logInfo("communication parameters");
biwa1400 4:522bba1c5fa1 396 logInfo("========================");
biwa1400 4:522bba1c5fa1 397 if (dot->getJoinMode() == mDot::PEER_TO_PEER) {
biwa1400 4:522bba1c5fa1 398 logInfo("TX frequency ------------- %lu", dot->getTxFrequency());
biwa1400 4:522bba1c5fa1 399 } else {
biwa1400 4:522bba1c5fa1 400 logInfo("acks --------------------- %s, %u attempts", dot->getAck() > 0 ? "on" : "off", dot->getAck());
biwa1400 4:522bba1c5fa1 401 }
biwa1400 4:522bba1c5fa1 402 logInfo("TX datarate -------------- %s", mDot::DataRateStr(dot->getTxDataRate()).c_str());
biwa1400 4:522bba1c5fa1 403 logInfo("TX power ----------------- %lu dBm", dot->getTxPower());
biwa1400 4:522bba1c5fa1 404 logInfo("atnenna gain ------------- %u dBm", dot->getAntennaGain());
biwa1400 4:522bba1c5fa1 405 logInfo("channels -------------");
biwa1400 4:522bba1c5fa1 406 std::vector<uint32_t>::iterator channelItreator;
biwa1400 4:522bba1c5fa1 407 uint32_t i=0;
biwa1400 4:522bba1c5fa1 408 for(channelItreator = dot->getChannels().begin();channelItreator!=dot->getChannels().end();channelItreator++)
biwa1400 4:522bba1c5fa1 409 {
biwa1400 4:522bba1c5fa1 410 logInfo("channel(%u) ------------- %u Hz",i++,*channelItreator);
biwa1400 4:522bba1c5fa1 411 }
biwa1400 4:522bba1c5fa1 412 }
biwa1400 4:522bba1c5fa1 413
biwa1400 4:522bba1c5fa1 414
biwa1400 4:522bba1c5fa1 415
biwa1400 4:522bba1c5fa1 416
biwa1400 4:522bba1c5fa1 417
biwa1400 4:522bba1c5fa1 418 mDot& MIUN::LoRa::getHandler()
biwa1400 4:522bba1c5fa1 419 {
biwa1400 4:522bba1c5fa1 420 return *dot;
biwa1400 4:522bba1c5fa1 421 }
biwa1400 4:522bba1c5fa1 422
biwa1400 4:522bba1c5fa1 423 /*** private function ***/
biwa1400 4:522bba1c5fa1 424
biwa1400 4:522bba1c5fa1 425 bool MIUN::LoRa::tryConnectNet()
biwa1400 4:522bba1c5fa1 426 {
biwa1400 4:522bba1c5fa1 427 int32_t j_attempts = 0;
biwa1400 4:522bba1c5fa1 428 int32_t ret = mDot::MDOT_ERROR;
biwa1400 4:522bba1c5fa1 429
biwa1400 4:522bba1c5fa1 430 // attempt to join the network
biwa1400 4:522bba1c5fa1 431 while (ret != mDot::MDOT_OK)
biwa1400 4:522bba1c5fa1 432 {
biwa1400 4:522bba1c5fa1 433 logInfo("attempt %d to join network", ++j_attempts);
biwa1400 4:522bba1c5fa1 434 ret = dot->joinNetwork();
biwa1400 4:522bba1c5fa1 435 if (ret != mDot::MDOT_OK) {
biwa1400 4:522bba1c5fa1 436 logError("failed to join network %d:%s It will retry...", ret, mDot::getReturnCodeString(ret).c_str());
biwa1400 4:522bba1c5fa1 437 // in some frequency bands we need to wait until another channel is available before transmitting again
biwa1400 4:522bba1c5fa1 438 uint32_t delay_s = (dot->getNextTxMs() / 1000) + 1;
biwa1400 4:522bba1c5fa1 439 if (delay_s < 2)
biwa1400 4:522bba1c5fa1 440 {
biwa1400 4:522bba1c5fa1 441 logInfo("waiting %lu s until next free channel", delay_s);
biwa1400 4:522bba1c5fa1 442 wait(delay_s);
biwa1400 4:522bba1c5fa1 443 } else {
biwa1400 4:522bba1c5fa1 444 logInfo("sleeping %lu s until next free channel", delay_s);
biwa1400 4:522bba1c5fa1 445 sleep(delay_s);
biwa1400 4:522bba1c5fa1 446 }
biwa1400 4:522bba1c5fa1 447 }
biwa1400 4:522bba1c5fa1 448
biwa1400 4:522bba1c5fa1 449 if(j_attempts >3)
biwa1400 4:522bba1c5fa1 450 {
biwa1400 4:522bba1c5fa1 451 logError("failed to join network %d:%s, give up.", ret, mDot::getReturnCodeString(ret).c_str());
biwa1400 4:522bba1c5fa1 452 return false;
biwa1400 4:522bba1c5fa1 453 }
biwa1400 4:522bba1c5fa1 454 }
biwa1400 4:522bba1c5fa1 455 return true;
biwa1400 4:522bba1c5fa1 456 }
biwa1400 4:522bba1c5fa1 457
biwa1400 4:522bba1c5fa1 458
biwa1400 4:522bba1c5fa1 459
biwa1400 4:522bba1c5fa1 460 bool MIUN::LoRa::send(std::string input_data, int port)
biwa1400 4:522bba1c5fa1 461 {
biwa1400 4:522bba1c5fa1 462 //1. Set Port
biwa1400 4:522bba1c5fa1 463 dot->setAppPort(port);
biwa1400 4:522bba1c5fa1 464 //1. if it use ADR
biwa1400 4:522bba1c5fa1 465 if(dot->getAdr() == true)
biwa1400 4:522bba1c5fa1 466 {
biwa1400 4:522bba1c5fa1 467 int i=0;
biwa1400 4:522bba1c5fa1 468 while(!send_basic(input_data))
biwa1400 4:522bba1c5fa1 469 {
biwa1400 4:522bba1c5fa1 470 i++;
biwa1400 4:522bba1c5fa1 471 if(i>3)
biwa1400 4:522bba1c5fa1 472 {
biwa1400 4:522bba1c5fa1 473 // 1. increase SF
biwa1400 4:522bba1c5fa1 474 if(increaseSF()==false)
biwa1400 4:522bba1c5fa1 475 {
biwa1400 4:522bba1c5fa1 476 //1. Clean Network Session
biwa1400 4:522bba1c5fa1 477 dot->resetNetworkSession();
biwa1400 4:522bba1c5fa1 478 //2. Reset SF
biwa1400 4:522bba1c5fa1 479 resetSF();
biwa1400 4:522bba1c5fa1 480 tryConnectNet();
biwa1400 4:522bba1c5fa1 481 return false;
biwa1400 4:522bba1c5fa1 482 }
biwa1400 4:522bba1c5fa1 483 }
biwa1400 4:522bba1c5fa1 484 sleepWaitingNextFreeChannel();
biwa1400 4:522bba1c5fa1 485 }
biwa1400 4:522bba1c5fa1 486
biwa1400 4:522bba1c5fa1 487 }
biwa1400 4:522bba1c5fa1 488 else
biwa1400 4:522bba1c5fa1 489 {
biwa1400 4:522bba1c5fa1 490 return send_basic(input_data);
biwa1400 4:522bba1c5fa1 491 }
biwa1400 4:522bba1c5fa1 492 return true;
biwa1400 4:522bba1c5fa1 493 }
biwa1400 4:522bba1c5fa1 494
biwa1400 4:522bba1c5fa1 495
biwa1400 4:522bba1c5fa1 496 bool MIUN::LoRa::increaseSF()
biwa1400 4:522bba1c5fa1 497 {
biwa1400 4:522bba1c5fa1 498 uint8_t current_SF = dot->getTxDataRate();
biwa1400 4:522bba1c5fa1 499 if(current_SF>0)
biwa1400 4:522bba1c5fa1 500 {
biwa1400 4:522bba1c5fa1 501 changeSF(--current_SF);
biwa1400 4:522bba1c5fa1 502 return true;
biwa1400 4:522bba1c5fa1 503 }
biwa1400 4:522bba1c5fa1 504 return false;
biwa1400 4:522bba1c5fa1 505 }
biwa1400 4:522bba1c5fa1 506
biwa1400 4:522bba1c5fa1 507 bool MIUN::LoRa::decreaseSF()
biwa1400 4:522bba1c5fa1 508 {
biwa1400 4:522bba1c5fa1 509 uint8_t current_SF = dot->getTxDataRate();
biwa1400 4:522bba1c5fa1 510 if(current_SF<5)
biwa1400 4:522bba1c5fa1 511 {
biwa1400 4:522bba1c5fa1 512 changeSF(++current_SF);
biwa1400 4:522bba1c5fa1 513 return true;
biwa1400 4:522bba1c5fa1 514 }
biwa1400 4:522bba1c5fa1 515 return false;
biwa1400 4:522bba1c5fa1 516 }
biwa1400 4:522bba1c5fa1 517
biwa1400 4:522bba1c5fa1 518 void MIUN::LoRa::resetSF()
biwa1400 4:522bba1c5fa1 519 {
biwa1400 4:522bba1c5fa1 520 changeSF(5);
biwa1400 4:522bba1c5fa1 521 }
biwa1400 4:522bba1c5fa1 522
biwa1400 4:522bba1c5fa1 523
biwa1400 4:522bba1c5fa1 524 void MIUN::LoRa::setBatteryLevel(uint8_t batteryLevelValue)
biwa1400 4:522bba1c5fa1 525 {
biwa1400 4:522bba1c5fa1 526 batteryLevel = batteryLevelValue;
biwa1400 4:522bba1c5fa1 527 }
biwa1400 4:522bba1c5fa1 528
biwa1400 4:522bba1c5fa1 529
biwa1400 4:522bba1c5fa1 530
biwa1400 4:522bba1c5fa1 531
biwa1400 4:522bba1c5fa1 532
biwa1400 4:522bba1c5fa1 533
biwa1400 4:522bba1c5fa1 534
biwa1400 4:522bba1c5fa1 535
biwa1400 4:522bba1c5fa1 536 MIUN::MacCommandEvent::MacCommandEvent(LoRa& loraHandle):loraHandle(loraHandle)
biwa1400 4:522bba1c5fa1 537 {
biwa1400 4:522bba1c5fa1 538 }
biwa1400 4:522bba1c5fa1 539
biwa1400 4:522bba1c5fa1 540 void MIUN::MacCommandEvent::RxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr, lora::DownlinkControl ctrl, uint8_t slot)
biwa1400 4:522bba1c5fa1 541 {
biwa1400 4:522bba1c5fa1 542 /*
biwa1400 4:522bba1c5fa1 543 for(int i=0;i<20;i++)
biwa1400 4:522bba1c5fa1 544 {
biwa1400 4:522bba1c5fa1 545 logInfo("Payload %.2x", payload[i]);
biwa1400 4:522bba1c5fa1 546 }
biwa1400 4:522bba1c5fa1 547 */
biwa1400 4:522bba1c5fa1 548 if(payload[0] == 0xa0 || payload[0] == 0x60)
biwa1400 4:522bba1c5fa1 549 {
biwa1400 4:522bba1c5fa1 550 //1. Process Mac Command
biwa1400 4:522bba1c5fa1 551 int macCommandLength = payload[5] & 0x0f;
biwa1400 4:522bba1c5fa1 552 if(macCommandLength>0)
biwa1400 4:522bba1c5fa1 553 {
biwa1400 4:522bba1c5fa1 554 uint8_t macCommand[macCommandLength];
biwa1400 4:522bba1c5fa1 555 memcpy(macCommand,payload+8,macCommandLength);
biwa1400 4:522bba1c5fa1 556
biwa1400 4:522bba1c5fa1 557 for(int i=0;i<macCommandLength;i++)
biwa1400 4:522bba1c5fa1 558 {
biwa1400 4:522bba1c5fa1 559 logInfo("Mac Command: %.2x", macCommand[i]);
biwa1400 4:522bba1c5fa1 560 }
biwa1400 4:522bba1c5fa1 561
biwa1400 4:522bba1c5fa1 562 switch(macCommand[0])
biwa1400 4:522bba1c5fa1 563 {
biwa1400 4:522bba1c5fa1 564 case decreaseSF:
biwa1400 4:522bba1c5fa1 565 if(macCommandLength == decreaseSF_length)
biwa1400 4:522bba1c5fa1 566 {
biwa1400 4:522bba1c5fa1 567 processSFDecrease(macCommand);
biwa1400 4:522bba1c5fa1 568 }
biwa1400 4:522bba1c5fa1 569 break;
biwa1400 4:522bba1c5fa1 570
biwa1400 4:522bba1c5fa1 571 case increaseSF:
biwa1400 4:522bba1c5fa1 572 if(macCommandLength == increaseSF_length)
biwa1400 4:522bba1c5fa1 573 {
biwa1400 4:522bba1c5fa1 574 processSFIncrease(macCommand);
biwa1400 4:522bba1c5fa1 575 }
biwa1400 4:522bba1c5fa1 576 break;
biwa1400 4:522bba1c5fa1 577
biwa1400 4:522bba1c5fa1 578 case changeSleepTime:
biwa1400 4:522bba1c5fa1 579 if(macCommandLength == changeSleepTime_length)
biwa1400 4:522bba1c5fa1 580 {
biwa1400 4:522bba1c5fa1 581 processChangeSleepTm(macCommand);
biwa1400 4:522bba1c5fa1 582 }
biwa1400 4:522bba1c5fa1 583 break;
biwa1400 4:522bba1c5fa1 584
biwa1400 4:522bba1c5fa1 585 }
biwa1400 4:522bba1c5fa1 586 }
biwa1400 4:522bba1c5fa1 587
biwa1400 4:522bba1c5fa1 588 //2. Receive Port
biwa1400 4:522bba1c5fa1 589 if((size-12-macCommandLength)>0)
biwa1400 4:522bba1c5fa1 590 {
biwa1400 4:522bba1c5fa1 591 loraHandle.receivePort = payload[8+macCommandLength];
biwa1400 4:522bba1c5fa1 592 }
biwa1400 4:522bba1c5fa1 593 else
biwa1400 4:522bba1c5fa1 594 {
biwa1400 4:522bba1c5fa1 595 loraHandle.receivePort = -1;
biwa1400 4:522bba1c5fa1 596 }
biwa1400 4:522bba1c5fa1 597 }
biwa1400 4:522bba1c5fa1 598 }
biwa1400 4:522bba1c5fa1 599
biwa1400 4:522bba1c5fa1 600 void MIUN::MacCommandEvent::processChangeSleepTm(uint8_t *macCommand)
biwa1400 4:522bba1c5fa1 601 {
biwa1400 4:522bba1c5fa1 602
biwa1400 4:522bba1c5fa1 603 uint32_t sleepTime_s = (((uint32_t)macCommand[4])<<24)+
biwa1400 4:522bba1c5fa1 604 (((uint32_t)macCommand[3])<<16)+
biwa1400 4:522bba1c5fa1 605 (((uint32_t)macCommand[2])<<8)+
biwa1400 4:522bba1c5fa1 606 macCommand[1];
biwa1400 4:522bba1c5fa1 607 logInfo("Change Sleep Time: %u", sleepTime_s);
biwa1400 4:522bba1c5fa1 608 loraHandle.setSleepTime(sleepTime_s);
biwa1400 4:522bba1c5fa1 609
biwa1400 4:522bba1c5fa1 610 }
biwa1400 4:522bba1c5fa1 611
biwa1400 4:522bba1c5fa1 612 void MIUN::MacCommandEvent::processSFDecrease(uint8_t *payload)
biwa1400 4:522bba1c5fa1 613 {
biwa1400 4:522bba1c5fa1 614 loraHandle.decreaseSF();
biwa1400 4:522bba1c5fa1 615 }
biwa1400 4:522bba1c5fa1 616
biwa1400 4:522bba1c5fa1 617 void MIUN::MacCommandEvent::processSFIncrease(uint8_t *payload)
biwa1400 4:522bba1c5fa1 618 {
biwa1400 4:522bba1c5fa1 619 loraHandle.increaseSF();
biwa1400 4:522bba1c5fa1 620 }
biwa1400 4:522bba1c5fa1 621
biwa1400 4:522bba1c5fa1 622 uint8_t MIUN::MacCommandEvent::MeasureBattery()
biwa1400 4:522bba1c5fa1 623 {
biwa1400 4:522bba1c5fa1 624 return loraHandle.batteryLevel;
biwa1400 4:522bba1c5fa1 625 }
biwa1400 4:522bba1c5fa1 626
biwa1400 4:522bba1c5fa1 627