LoRa_Miun_Lib

Dependents:   Smart_Miun

Committer:
biwa1400
Date:
Wed Oct 11 10:17:10 2017 +0000
Revision:
3:8849166ce7e5
20171011;

Who changed what in which revision?

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