ssilverman / libCBOR

Library for processing CBOR-encoded data (RFC 7049).
BSD 3-Clause "New" or "Revised" License
4 stars 3 forks source link

[question] How to do this code below with this lib? #2

Closed ericonetto closed 6 years ago

ericonetto commented 6 years ago

This code is for a msgbpack lib. I want to do the same with libCBOR

Could you help me?

bool read_message(WiFiClient * stream) {
  bool res = true;
  if(stream->available() > 0) {
    Serial.println("Stream avaiable");
    uint8_t i;
    char buf[8];
    uint32_t map_size;
    uint32_t r_size;
    uint8_t pin;
    bool level;
    res &= msgpck_map_next(stream);
    Serial.println("Stream msgpck_map_next:" + String(res));
    if(!res)
      return false;
    res &= msgpck_read_map_size(stream, &map_size);
    Serial.println("Stream msgpck_read_map_size:" + String(res));
    if(!res)
      return false;
    res &= (map_size == 2);
    res &= msgpck_read_string(stream, buf, 3, &r_size);
    Serial.println("Stream msgpck_read_string:" + String(res));
    if(!res)
      return false;
    res &= (buf[0] == 'p');
    res &= (buf[1] == 'i');
    res &= (buf[2] == 'n');
    Serial.println("Stream pin?:" + String(res));
    res &= msgpck_read_integer(stream, &pin, 1);
    Serial.println("Stream msgpck_read_integer:" + String(res));
    if(!res)
      return false;
    res &= msgpck_read_string(stream, buf, 3, &r_size);
    Serial.println("Stream msgpck_read_string:" + String(res));
    if(!res)
      return false;
    res &= (buf[0] == 'v');
    res &= (buf[1] == 'a');
    res &= (buf[2] == 'l');
    Serial.println("Stream val?:" + String(res));
    Serial.println("Stream val=:" + String(buf));
    res &= msgpck_read_bool(stream, &level);
    Serial.println("Stream msgpck_read_bool:" + String(res));
    if(!res)
      return false;

    if((pin == 6) || (pin == 7)){}
      //digitalWrite(pin, level);
  }
  return res;
}

void setup() {
  Serial.begin(115200);
  Serial.println("SETUP STARTED");                        //Serial connection
  WiFi.begin("yeloh", "!yellow2016");   //WiFi connection

  while (WiFi.status() != WL_CONNECTED) {  //Wait for the WiFI connection completion

    delay(500);
    Serial.println("Waiting for connection");

  }
  Serial.println("SETUP FINISHED");
}

void loop() {

 if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status

   //POST("http://192.168.1.80:3000/messagepack/unpack",);

   HTTPClient http;

   Serial.print("[HTTP] begin...\n");

   // configure server and url
   http.begin("http://192.168.1.80:3000/messagepack/pack");
   //http.begin("192.168.1.12", 80, "/test.html");

   Serial.print("[HTTP] POST...\n");
   // start connection and send HTTP header
   http.addHeader("Content-Type", "application/json");
   int httpCode = http.POST("{\"pin\":6, \"val\":true}");
   if(httpCode > 0) {
       // HTTP header has been send and Server response header has been handled
       Serial.printf("[HTTP] POST... code: %d\n", httpCode);

       // file found at server
       if(httpCode == HTTP_CODE_OK) {

           // get tcp stream
           WiFiClient * stream = http.getStreamPtr();

           read_message(stream);

       }
   } else {
       Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
   }

   http.end();

 }
  delay(30000);  //Send a request every 30 seconds

}
ssilverman commented 6 years ago

The expectXXX helper functions in CBOR_parsing.h will be useful for this. Have you looked at the example code?

ericonetto commented 6 years ago

@ssilverman I think Im close, but still my application isn't recognizing the returned CBOR. Could you have a look?: https://github.com/ericonetto/konker-esp8266-binary-msgs-tests-CBOR- The returned CBOR from my server is: a2 64 66 6c 61 67 f5 64 74 65 78 74 64 6f 69 6f 69 Tah means:

A2 # map(2) 64 # text(4) 666C6167 # "flag" F5 # primitive(21) 64 # text(4) 74657874 # "text" 64 # text(4) 6F696F69 # "oioi"

But I get this from my code (except it isnt an array... I will change the msg)

[HTTP] begin... [HTTP] POST... [HTTP] POST... code: 200 Not self describe Array 2 not found

ssilverman commented 6 years ago

It looks like you're directly using the example code, which expects a "self-describe tag" at the start of the stream. Your stream is missing this. Also, by the time the first "expectValue that is a tag" call fails, the stream has already advanced, and it has already consumed the "map(2)" data item, so now the stream is out of sync with the parsing code.

ericonetto commented 6 years ago

Totaly make sense. I was confused by the "self-describe tag", I thought it was something buit in CBOR messages, something that describe the message. Well I will remove the "self-describe tag" part then. But anyway I'm curius, could you explain what was this "kSelfDescribeTag" in the example? Thank you so much. I think your lib will be very helpful! After I finish this test code I've shown, you could refer it in the examples to help more people. By the way the GIT of the service is here https://github.com/KonkerLabs/konker-binary-services

ssilverman commented 6 years ago

See RFC7049, Section 2.4.5 for more info, but essentially they're a tag that indicates that the following data is CBOR data. Its value is "mostly" unique and so allows quick determination of the data type.