Advertisement
Guest User

Untitled

a guest
Sep 30th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. #ifndef MIKROTIK_H
  2. #define MIKROTIK_H
  3.  
  4. #include <WiFi.h>
  5. #include "logger.h"
  6. #include "logger.h"
  7.  
  8. class Mikrotik
  9. {
  10.  
  11.   public:
  12.     static WiFiClient client;
  13.     static String mtkIp;
  14.     static int mtkPort;
  15.     static String mtkUser;
  16.     static String mtkPass;
  17.     static int mtkRequestInterval;
  18.     static TaskHandle_t ifUserWifiConnectedHandle;
  19.  
  20.     static void init()
  21.     {
  22.         Logger::syslog(__func__);
  23.         if (config["mtkIp"] == "" || config["mtkPort"] == "" || config["mtkUser"] == "" || config["mtkPass"] == "" || config["mtkRequestInterval"] == "")
  24.         {
  25.             Logger::syslog("I dont have mtk config set, will delete task");
  26.             vTaskDelete(NULL);
  27.         }
  28.         mtkIp = config["mtkIp"].c_str();
  29.         mtkPort = config["mtkPort"].toInt();
  30.         mtkUser = config["mtkUser"];
  31.         mtkPass = config["mtkPass"];
  32.         mtkRequestInterval = config["mtkRequestInterval"].toInt();
  33.         Logger::syslog("mtkIp: " + String(mtkIp) + " ,mtkPort: " + String(mtkPort) + ", mtkRequestInterval: " + String(mtkRequestInterval));
  34.  
  35.         xTaskCreate(
  36.             ifUserWifiConnectedTask,     /* Task function. */
  37.             "ifUserWifiConnectedTask",   /* name of task. */
  38.             10000,                       /* Stack size of task */
  39.             NULL,                        /* parameter of the task */
  40.             1,                           /* priority of the task */
  41.             &ifUserWifiConnectedHandle); /* Task handle to keep track of created task */
  42.     }
  43.  
  44.     static void ifUserWifiConnectedTask(void *pvParameters)
  45.     {
  46.         Logger::syslog(__func__);
  47.         String readBuff;
  48.         vTaskDelay(10000);
  49.         while (!WiFi.isConnected())
  50.             vTaskDelay(5000 / portTICK_RATE_MS);
  51.         while (true)
  52.         {
  53.             while (!WiFi.isConnected())
  54.                 vTaskDelay(5000 / portTICK_RATE_MS);
  55.             Logger::syslog("Starting cycle");
  56.             if (!client.connect(mtkIp.c_str(), mtkPort))
  57.             {
  58.                 Logger::syslog("Unable to connect");
  59.                 vTaskDelay(mtkRequestInterval);
  60.                 continue;
  61.             }
  62.             sendCmd("/login");
  63.             sendCmd("=name=" + mtkUser);
  64.             sendCmd("=password=" + mtkPass, true);
  65.  
  66.             vTaskDelay(1000);
  67.             while (client.available())
  68.             {
  69.                 readBuff = client.readString();
  70.             }
  71.            
  72.             if (readBuff.indexOf("done") != -1)
  73.             {
  74.                 sendCmd("/caps-man/registration-table/print", false);
  75.                 sendCmd("=.proplist=mac-address",true);
  76.                 vTaskDelay(1000);
  77.                 while (client.available())
  78.                 {
  79.                     readBuff = client.readString();
  80.                 }
  81.                 Serial.println(readBuff);
  82.  
  83.             }
  84.  
  85.             client.stop();
  86.             Logger::syslog("Reading completed");
  87.             vTaskDelay(mtkRequestInterval);
  88.         }
  89.         vTaskDelete(NULL);
  90.     }
  91.  
  92.     static void sendCmd(String cmd, bool sendEnd = false)
  93.     {
  94.         writeLength(cmd.length());
  95.         client.print(cmd);
  96.         if (sendEnd)
  97.             client.print(char(0));
  98.     }
  99.  
  100.     static void writeLength(int messageLength)
  101.     {
  102.         char *encodedLengthData; // encoded length to send to the api socket
  103.         char *lengthData;        // exactly what is in memory at &iLen integer
  104.  
  105.         encodedLengthData = (char *)calloc(sizeof(int), 1);
  106.         lengthData = (char *)&messageLength;
  107.         if (messageLength < 0x80)
  108.         {
  109.             encodedLengthData[0] = (char)messageLength;
  110.             client.write(encodedLengthData, 1);
  111.         }
  112.         else if (messageLength < 0x4000)
  113.         { // write 2 bytes
  114.             encodedLengthData[0] = lengthData[1] | 0x80;
  115.             encodedLengthData[1] = lengthData[0];
  116.             client.write(encodedLengthData, 2);
  117.         }
  118.         else if (messageLength < 0x200000)
  119.         { // write 3 bytes
  120.  
  121.             encodedLengthData[0] = lengthData[2] | 0xc0;
  122.             encodedLengthData[1] = lengthData[1];
  123.             encodedLengthData[2] = lengthData[0];
  124.  
  125.             client.write(encodedLengthData, 3);
  126.         }
  127.         else if (messageLength < 0x10000000)
  128.         { // write 4 bytes (untested)
  129.             encodedLengthData[0] = lengthData[3] | 0xe0;
  130.             encodedLengthData[1] = lengthData[2];
  131.             encodedLengthData[2] = lengthData[1];
  132.             encodedLengthData[3] = lengthData[0];
  133.             client.write(encodedLengthData, 4);
  134.         }
  135.         else
  136.         { // this should never happen
  137.             Logger::syslog("Length of word is: " + String(messageLength));
  138.             Logger::syslog("Word is too long.\n");
  139.         }
  140.  
  141.         delete[] encodedLengthData;
  142.     }
  143. };
  144.  
  145. WiFiClient Mikrotik::client;
  146. String Mikrotik::mtkIp;
  147. int Mikrotik::mtkPort;
  148. String Mikrotik::mtkUser;
  149. String Mikrotik::mtkPass;
  150. int Mikrotik::mtkRequestInterval;
  151. TaskHandle_t Mikrotik::ifUserWifiConnectedHandle;
  152.  
  153. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement