123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- #if defined(_UDPROUTER)
- #if defined(_TTNROUTER)
- #error "Please make sure that either _UDPROUTER or _TTNROUTER are defined but not both"
- #endif
- bool connectUdp()
- {
- bool ret = false;
- unsigned int localPort = _LOCUDPPORT;
- #if _DUSB>=1
- if (debug>=1) {
- Serial.print(F("Local UDP port="));
- Serial.println(localPort);
- }
- #endif
- if (Udp.begin(localPort) == 1) {
- #if _DUSB>=1
- if (debug>=1) Serial.println(F("Connection successful"));
- #endif
- ret = true;
- }
- else{
- #if _DUSB>=1
- if (debug>=1) Serial.println("Connection failed");
- #endif
- }
- return(ret);
- }
- int readUdp(int packetSize)
- {
- uint8_t protocol;
- uint16_t token;
- uint8_t ident;
- uint8_t buff[32];
- uint8_t buff_down[RX_BUFF_SIZE];
- if (WlanConnect(10) < 0) {
- #if _DUSB>=1
- Serial.print(F("readUdp: ERROR connecting to WLAN"));
- if (debug>=2) Serial.flush();
- #endif
- Udp.flush();
- yield();
- return(-1);
- }
- yield();
-
- if (packetSize > RX_BUFF_SIZE) {
- #if _DUSB>=1
- Serial.print(F("readUdp:: ERROR package of size: "));
- Serial.println(packetSize);
- #endif
- Udp.flush();
- return(-1);
- }
-
-
-
- if (Udp.read(buff_down, packetSize) < packetSize) {
- #if _DUSB>=1
- Serial.println(F("A readUdp:: Reading less chars"));
- return(-1);
- #endif
- }
-
- IPAddress remoteIpNo = Udp.remoteIP();
-
- unsigned int remotePortNo = Udp.remotePort();
- if (remotePortNo == 123) {
-
- #if _DUSB>=1
- if ( debug>=0 ) {
- Serial.println(F("A readUdp:: NTP msg rcvd"));
- }
- #endif
- gwayConfig.ntpErr++;
- gwayConfig.ntpErrTime = now();
- return(0);
- }
-
-
- else {
- uint8_t *data = (uint8_t *) ((uint8_t *)buff_down + 4);
- protocol = buff_down[0];
- token = buff_down[2]*256 + buff_down[1];
- ident = buff_down[3];
- #if _DUSB>=1
- if ((debug>1) && (pdebug & P_MAIN)) {
- Serial.print(F("M readUdp:: message waiting="));
- Serial.print(ident);
- Serial.println();
- }
- #endif
-
- switch (ident) {
-
-
-
- case PKT_PUSH_DATA:
- #if _DUSB>=1
- if (debug >=1) {
- Serial.print(F("PKT_PUSH_DATA:: size ")); Serial.print(packetSize);
- Serial.print(F(" From ")); Serial.print(remoteIpNo);
- Serial.print(F(", port ")); Serial.print(remotePortNo);
- Serial.print(F(", data: "));
- for (int i=0; i<packetSize; i++) {
- Serial.print(buff_down[i],HEX);
- Serial.print(':');
- }
- Serial.println();
- if (debug>=2) Serial.flush();
- }
- #endif
- break;
-
-
-
- case PKT_PUSH_ACK:
- #if _DUSB>=1
- if (( debug>=2) && (pdebug & P_MAIN )) {
- Serial.print(F("M PKT_PUSH_ACK:: size "));
- Serial.print(packetSize);
- Serial.print(F(" From "));
- Serial.print(remoteIpNo);
- Serial.print(F(", port "));
- Serial.print(remotePortNo);
- Serial.print(F(", token: "));
- Serial.println(token, HEX);
- Serial.println();
- }
- #endif
- break;
-
- case PKT_PULL_DATA:
- #if _DUSB>=1
- Serial.print(F(" Pull Data"));
- Serial.println();
- #endif
- break;
-
-
-
- case PKT_PULL_RESP:
- #if _DUSB>=1
- if (( debug>=0 ) && ( pdebug & P_MAIN )) {
- Serial.println(F("M readUdp:: PKT_PULL_RESP received"));
- }
- #endif
-
-
- _state=S_TX;
- sendTime = micros();
-
- if (sendPacket(data, packetSize-4) < 0) {
- #if _DUSB>=1
- if ( debug>=0 ) {
- Serial.println(F("A readUdp:: Error: PKT_PULL_RESP sendPacket failed"));
- }
- #endif
- return(-1);
- }
-
- buff[0]=buff_down[0];
- buff[1]=buff_down[1];
- buff[2]=buff_down[2];
-
- buff[3]=PKT_TX_ACK;
- buff[4]=MAC_array[0];
- buff[5]=MAC_array[1];
- buff[6]=MAC_array[2];
- buff[7]=0xFF;
- buff[8]=0xFF;
- buff[9]=MAC_array[3];
- buff[10]=MAC_array[4];
- buff[11]=MAC_array[5];
- buff[12]=0;
- #if _DUSB>=1
- if (( debug >= 2 ) && ( pdebug & P_MAIN )) {
- Serial.println(F("M readUdp:: TX buff filled"));
- }
- #endif
-
- Udp.beginPacket(remoteIpNo, remotePortNo);
- if (Udp.write((unsigned char *)buff, 12) != 12) {
- #if _DUSB>=1
- if (debug>=0)
- Serial.println("A readUdp:: Error: PKT_PULL_ACK UDP write");
- #endif
- }
- else {
- #if _DUSB>=1
- if (( debug>=0 ) && ( pdebug & P_TX )) {
- Serial.print(F("M PKT_TX_ACK:: micros="));
- Serial.println(micros());
- }
- #endif
- }
- if (!Udp.endPacket()) {
- #if _DUSB>=1
- if (( debug>=0 ) && ( pdebug & P_MAIN )) {
- Serial.println(F("M PKT_PULL_DATALL Error Udp.endpaket"));
- }
- #endif
- }
-
- yield();
- #if _DUSB>=1
- if (( debug >=1 ) && (pdebug & P_MAIN )) {
- Serial.print(F("M PKT_PULL_RESP:: size "));
- Serial.print(packetSize);
- Serial.print(F(" From "));
- Serial.print(remoteIpNo);
- Serial.print(F(", port "));
- Serial.print(remotePortNo);
- Serial.print(F(", data: "));
- data = buff_down + 4;
- data[packetSize] = 0;
- Serial.print((char *)data);
- Serial.println(F("..."));
- }
- #endif
- break;
-
- case PKT_PULL_ACK:
- #if _DUSB>=1
- if (( debug >= 2 ) && (pdebug & P_MAIN )) {
- Serial.print(F("M PKT_PULL_ACK:: size ")); Serial.print(packetSize);
- Serial.print(F(" From ")); Serial.print(remoteIpNo);
- Serial.print(F(", port ")); Serial.print(remotePortNo);
- Serial.print(F(", data: "));
- for (int i=0; i<packetSize; i++) {
- Serial.print(buff_down[i],HEX);
- Serial.print(':');
- }
- Serial.println();
- }
- #endif
- break;
-
- default:
- #if GATEWAYMGT==1
-
- gateway_mgt(packetSize, buff_down);
- #else
- #endif
- #if _DUSB>=1
- Serial.print(F(", ERROR ident not recognized="));
- Serial.println(ident);
- #endif
- break;
- }
- #if _DUSB>=2
- if (debug>=1) {
- Serial.print(F("readUdp:: returning="));
- Serial.println(packetSize);
- }
- #endif
-
- return packetSize;
- }
- }
- int sendUdp(IPAddress server, int port, uint8_t *msg, int length) {
-
- if (WlanConnect(3) < 0) {
- #if _DUSB>=1
- if (( debug>=0 ) && ( pdebug & P_MAIN )) {
- Serial.print(F("M sendUdp: ERROR connecting to WiFi"));
- Serial.flush();
- }
- #endif
- Udp.flush();
- yield();
- return(0);
- }
- yield();
-
- #if _DUSB>=1
- if (( debug>=3 ) && ( pdebug & P_MAIN )) {
- Serial.println(F("M WiFi connected"));
- }
- #endif
- if (!Udp.beginPacket(server, (int) port)) {
- #if _DUSB>=1
- if (( debug>=1 ) && ( pdebug & P_MAIN )) {
- Serial.println(F("M sendUdp:: Error Udp.beginPacket"));
- }
- #endif
- return(0);
- }
-
- yield();
-
- if (Udp.write((unsigned char *)msg, length) != length) {
- #if _DUSB>=1
- if (( debug<=1 ) && ( pdebug & P_MAIN )) {
- Serial.println(F("M sendUdp:: Error write"));
- }
- #endif
- Udp.endPacket();
- return(0);
- }
-
- yield();
-
- if (!Udp.endPacket()) {
- #if _DUSB>=1
- if (debug>=1) {
- Serial.println(F("sendUdp:: Error Udp.endPacket"));
- Serial.flush();
- }
- #endif
- return(0);
- }
- return(1);
- }
- void pullData() {
- uint8_t pullDataReq[12];
- int pullIndex=0;
- int i;
-
- uint8_t token_h = (uint8_t)rand();
- uint8_t token_l = (uint8_t)rand();
-
-
- pullDataReq[0] = PROTOCOL_VERSION;
- pullDataReq[1] = token_h;
- pullDataReq[2] = token_l;
- pullDataReq[3] = PKT_PULL_DATA;
-
- pullDataReq[4] = MAC_array[0];
- pullDataReq[5] = MAC_array[1];
- pullDataReq[6] = MAC_array[2];
- pullDataReq[7] = 0xFF;
- pullDataReq[8] = 0xFF;
- pullDataReq[9] = MAC_array[3];
- pullDataReq[10] = MAC_array[4];
- pullDataReq[11] = MAC_array[5];
-
-
- pullIndex = 12;
-
-
-
- uint8_t *pullPtr;
- pullPtr = pullDataReq,
- #ifdef _TTNSERVER
- sendUdp(ttnServer, _TTNPORT, pullDataReq, pullIndex);
- yield();
- #endif
- #if _DUSB>=1
- if (pullPtr != pullDataReq) {
- Serial.println(F("pullPtr != pullDatReq"));
- Serial.flush();
- }
- #endif
- #ifdef _THINGSERVER
- sendUdp(thingServer, _THINGPORT, pullDataReq, pullIndex);
- #endif
- #if _DUSB>=1
- if (( debug>=2 ) && ( pdebug & P_MAIN )) {
- yield();
- Serial.print(F("M PKT_PULL_DATA request, len=<"));
- Serial.print(pullIndex);
- Serial.print(F("> "));
- for (i=0; i<pullIndex; i++) {
- Serial.print(pullDataReq[i],HEX);
- Serial.print(':');
- }
- Serial.println();
- if (debug>=2) Serial.flush();
- }
- #endif
- return;
- }
- void sendstat() {
- uint8_t status_report[STATUS_SIZE];
- char stat_timestamp[32];
- time_t t;
- char clat[10]={0};
- char clon[10]={0};
- int stat_index=0;
- uint8_t token_h = (uint8_t)rand();
- uint8_t token_l = (uint8_t)rand();
-
-
- status_report[0] = PROTOCOL_VERSION;
- status_report[1] = token_h;
- status_report[2] = token_l;
- status_report[3] = PKT_PUSH_DATA;
-
-
- status_report[4] = MAC_array[0];
- status_report[5] = MAC_array[1];
- status_report[6] = MAC_array[2];
- status_report[7] = 0xFF;
- status_report[8] = 0xFF;
- status_report[9] = MAC_array[3];
- status_report[10] = MAC_array[4];
- status_report[11] = MAC_array[5];
- stat_index = 12;
-
- t = now();
-
-
- sprintf(stat_timestamp, "%04d-%02d-%02d %02d:%02d:%02d CET", year(),month(),day(),hour(),minute(),second());
- yield();
-
- ftoa(lat,clat,5);
- ftoa(lon,clon,5);
-
-
- delay(1);
-
- int j = snprintf((char *)(status_report + stat_index), STATUS_SIZE-stat_index,
- "{\"stat\":{\"time\":\"%s\",\"lati\":%s,\"long\":%s,\"alti\":%i,\"rxnb\":%u,\"rxok\":%u,\"rxfw\":%u,\"ackr\":%u.0,\"dwnb\":%u,\"txnb\":%u,\"pfrm\":\"%s\",\"mail\":\"%s\",\"desc\":\"%s\"}}",
- stat_timestamp, clat, clon, (int)alt, statc.msg_ttl, statc.msg_ok, statc.msg_down, 0, 0, 0, platform, email, description);
-
- yield();
- stat_index += j;
- status_report[stat_index] = 0;
- #if _DUSB>=1
- if (( debug>=2 ) && ( pdebug & P_MAIN )) {
- Serial.print(F("M stat update: <"));
- Serial.print(stat_index);
- Serial.print(F("> "));
- Serial.println((char *)(status_report+12));
- }
- #endif
- if (stat_index > STATUS_SIZE) {
- #if _DUSB>=1
- Serial.println(F("A sendstat:: ERROR buffer too big"));
- #endif
- return;
- }
-
-
- #ifdef _TTNSERVER
- sendUdp(ttnServer, _TTNPORT, status_report, stat_index);
- yield();
- #endif
- #ifdef _THINGSERVER
- sendUdp(thingServer, _THINGPORT, status_report, stat_index);
- #endif
- return;
- }
- #endif
|