You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2019/10/01 10:30:40 UTC

[GitHub] [mynewt-mcumgr] mkiiskila commented on a change in pull request #27: Update mynewt port of mcumgr and various fixes

mkiiskila commented on a change in pull request #27: Update mynewt port of mcumgr and various fixes
URL: https://github.com/apache/mynewt-mcumgr/pull/27#discussion_r329958876
 
 

 ##########
 File path: cborattr/src/cborattr.c
 ##########
 @@ -421,4 +437,181 @@ cbor_read_mbuf_attrs(struct os_mbuf *m, uint16_t off, uint16_t len,
     }
     return cbor_read_object(&value, attrs);
 }
+
+static int
+cbor_write_arr_val(struct CborEncoder *enc,
+                   const struct cbor_out_arr_val_t *arr)
+{
+    struct CborEncoder arr_enc;
+    size_t i;
+    int rc;
+
+    rc = cbor_encoder_create_array(enc, &arr_enc, arr->len);
+    if (rc != 0) {
+        return SYS_ENOMEM;
+    }
+
+    for (i = 0; i < arr->len; i++) {
+        rc = cbor_write_val(&arr_enc, &arr->elems[i]);
+        if (rc != 0) {
+            return SYS_ENOMEM;
+        }
+    }
+
+    rc = cbor_encoder_close_container(enc, &arr_enc);
+    if (rc != 0) {
+        return SYS_ENOMEM;
+    }
+
+    return 0;
+}
+
+static int
+cbor_write_val(struct CborEncoder *enc, const struct cbor_out_val_t *val)
+{
+    int len;
+    int rc;
+
+    switch (val->type) {
+    case CborAttrNullType:
+        rc = cbor_encode_null(enc);
+        break;
+
+    case CborAttrBooleanType:
+        rc = cbor_encode_boolean(enc, val->boolean);
+        break;
+
+    case CborAttrIntegerType:
+        rc = cbor_encode_int(enc, val->integer);
+        break;
+
+    case CborAttrUnsignedIntegerType:
+        rc = cbor_encode_uint(enc, val->uinteger);
+        break;
+
+#if FLOAT_SUPPORT
+    case CborAttrFloatType:
+        rc = cbor_encode_float(enc, val->fval);
+        break;
+
+    case CborAttrDoubleType:
+        rc = cbor_encode_double(enc, val->real);
+        break;
+#endif
+
+    case CborAttrByteStringType:
+        if (val->bytestring.data == NULL &&
+            val->bytestring.len != 0) {
+
+            return SYS_EINVAL;
+        }
+
+        rc = cbor_encode_byte_string(enc, val->bytestring.data,
+                                     val->bytestring.len);
+        break;
+
+    case CborAttrTextStringType:
+        if (val->string == NULL) {
+            len = 0;
+        } else {
+            len = strlen(val->string);
+        }
+        rc = cbor_encode_text_string(enc, val->string, len);
+        break;
+
+    case CborAttrObjectType:
+        rc = cbor_write_object(enc, val->obj);
+        break;
+
+    case CborAttrArrayType:
+        rc = cbor_write_arr_val(enc, &val->array);
+        break;
+
+    default:
+        return SYS_ENOTSUP;
+    }
+
+    if (rc != 0) {
+        return SYS_ENOMEM;
+    }
+
+    return 0;
+}
+
+static int
+cbor_write_attr(struct CborEncoder *enc, const struct cbor_out_attr_t *attr)
+{
+    int len;
+    int rc;
+
+    if (attr->omit) {
+        return 0;
+    }
+
+    len = strlen(attr->attribute);
+    rc = cbor_encode_text_string(enc, attr->attribute, len);
 
 Review comment:
   CBOR encoded data does not have name in front of a value. To allow for that, I suggest not creating the encoded string if attr->attribute is NULL, or if strlen() of that thing is 0.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services