 /*
   Basic MQTT-SN over Serial Bridge example
-  (experimental) 
+  (experimental)
 
    Setup:
    - Sends the MQTT-SN packages not over the nRF24L01+ Transceiver but over SoftwareSerial to a PC
    - On the PC run the Serial Port Bridge from https://github.com/njh/mqtt-sn-tools to forward them via TCP to a MQTT-SN Broker
-   
+
    Function:
    - reads a string from Serial and publishes the PUBLISH_TOPIC ("Bt/Ex/PubSub/Out") topic
-   - subscribes to the SUBSCRIBE_TOPIC ("Bt/Ex/PubSub/In") topic and writes the reiceived data to the Serial 
+   - subscribes to the SUBSCRIBE_TOPIC ("Bt/Ex/PubSub/In") topic and writes the reiceived data to the Serial
 */
 
 #include <SPI.h>

Bt::MqttSnClient client(serialPacketSocket, GATEWAY_NODE_ID, CLIENT_ID, &subscribeCallback);

void setup() {
   while (!Serial);
   Serial.begin(9600);
   Serial1.begin(9600);
   Serial << endl << endl << endl << "*** MQTT-SN PubSub example ***" << endl;
   Serial << endl; 
   Serial << " - Node      = " << CLIENT_NODE_ID << endl;
   Serial << " - Pup-Topic = " << PUBLISH_TOPIC << endl;
   Serial << " - Sub-Topic = " << SUBSCRIBE_TOPIC << endl;
   Serial << "try connect ..." << endl;

   while (!client.connect(30)) {
      Serial << "... connect failed ..." << endl;
      delay(1000);   
      Serial << "... retry connect ..." << endl;
   }
  
   Serial << "... connected" << endl;

   Serial << "subscribe" << endl;

   client.subscribe(SUBSCRIBE_TOPIC);

   Serial << "out> <Start>" << endl;

   client.publish(PUBLISH_TOPIC,"<Start>");
   
}

void loop() {
   client.loop();     
   if (Serial.available()) {
      String out = readString();
      Serial << "out> " << out << endl;
      client.publish(PUBLISH_TOPIC,out.c_str());
      if (out == String("<quit>")) {
         quit();
      }
   }
}

void subscribeCallback(const char* iTopic, const char*  iData) {
  Serial << "in> " << iData << endl;    
}

String readString() {

}

void quit() {
  Serial << "<quit>" << endl;
  client.disconnect();
  Serial << "Hit reboot" << endl;
  while (true) {}
}

