lib/rgbpwm/src/rgbpwm_smp.c
struct os_mbuf*
rgbpwm_get_txcfg_mbuf(int cfg_index)
{
    int rc;
    CborEncoder payload_enc;
    struct mgmt_cbuf n_b;
    struct cbor_mbuf_writer writer;
    struct mgmt_hdr *hdr;
    struct os_mbuf *rsp;
    int num_cfgs = 7;
    char *cfg_ptrs[] = {"pwm_freq", "mode", "local_delay", "colours0","colours1","colours2","colours3"};
    char name_str[CONF_MAX_NAME_LEN];

    if (cfg_index >= num_cfgs) {
        return 0;
    }
    rsp = os_msys_get_pkthdr(0, 0);
    if (!rsp) {
        return 0;
    }

    hdr = (struct mgmt_hdr *) os_mbuf_extend(rsp, sizeof(struct mgmt_hdr));
    if (!hdr) {
        goto exit_err;
    }
    hdr->nh_len = 0;
    hdr->nh_flags = 0;
    hdr->nh_op = MGMT_OP_WRITE;
    hdr->nh_group = htons(MGMT_GROUP_ID_CONFIG);
    hdr->nh_seq = 0;
    hdr->nh_id = CONF_NMGR_OP;

    cbor_mbuf_writer_init(&writer, rsp);
    cbor_encoder_init(&n_b.encoder, &writer.enc, 0);
    rc = cbor_encoder_create_map(&n_b.encoder, &payload_enc, CborIndefiniteLength);
    if (rc != 0) {
        goto exit_err;
    }

    struct mgmt_cbuf *cb = &n_b;
    CborError g_err = CborNoError;

    g_err |= cbor_encode_text_stringz(&payload_enc, "name");
    snprintf(name_str, sizeof(name_str)-1, "rgbpwm/%s", cfg_ptrs[cfg_index]);
    g_err |= cbor_encode_text_stringz(&payload_enc, name_str);
    g_err |= cbor_encode_text_stringz(&payload_enc, "val");
    g_err |= cbor_encode_text_stringz(&payload_enc, rgbpwm_conf_get(1, &cfg_ptrs[cfg_index], 0, 0));
    g_err |= cbor_encode_text_stringz(&payload_enc, "save");
    g_err |= cbor_encode_boolean(&payload_enc, true);
    console_printf("# txcfg: '%s' -> '%s'\n", name_str, rgbpwm_conf_get(1, &cfg_ptrs[cfg_index], 0, 0));

    rc = cbor_encoder_close_container(&cb->encoder, &payload_enc);
    if (rc != 0) {
        goto exit_err;
    }
    hdr->nh_len += cbor_encode_bytes_written(&cb->encoder);
    hdr->nh_len = htons(hdr->nh_len);

    return rsp;
exit_err:
    console_printf("something went wrong\n");
    os_mbuf_free_chain(rsp);
    return 0;
}
/**
 * @fn rgbpwm_get_txcfg_mbuf()
 *
 * @brief creates a mbuf packet to set the colour configuration remotely
 * @param cfg_idx - index into vector of config names
 * @returns struct os_mbuf* on success, NULL on fail
 *
 */
@fn rgbpwm_get_txcfg_mbuf()
@brief creates a mbuf packet to set the colour configuration remotely
@param cfg_idx - index into vector of config names
@returns struct os_mbuf* on success, NULL on fail
@fn rgbpwm_get_txcfg_mbuf () @brief creates a mbuf packet to set the colour configuration remotely @param cfg_idx - index into vector of config names @returns struct os_mbuf * on success NULL on fail
struct os_mbuf*
rgbpwm_get_txcfg_mbuf(int cfg_index)
{
    int rc;
    CborEncoder payload_enc;
    struct mgmt_cbuf n_b;
    struct cbor_mbuf_writer writer;
    struct mgmt_hdr *hdr;
    struct os_mbuf *rsp;
    int num_cfgs = 7;
    char *cfg_ptrs[] = {"pwm_freq", "mode", "local_delay", "colours0","colours1","colours2","colours3"};
    char name_str[CONF_MAX_NAME_LEN];
    if (cfg_index >= num_cfgs) {
        return 0;
    }
    rsp = os_msys_get_pkthdr(0, 0);
    if (!rsp) {
        return 0;
    }
    hdr = (struct mgmt_hdr *) os_mbuf_extend(rsp, sizeof(struct mgmt_hdr));
    if (!hdr) {
        goto exit_err;
    }
    hdr->nh_len = 0;
    hdr->nh_flags = 0;
    hdr->nh_op = MGMT_OP_WRITE;
    hdr->nh_group = htons(MGMT_GROUP_ID_CONFIG);
    hdr->nh_seq = 0;
    hdr->nh_id = CONF_NMGR_OP;
    cbor_mbuf_writer_init(&writer, rsp);
    cbor_encoder_init(&n_b.encoder, &writer.enc, 0);
    rc = cbor_encoder_create_map(&n_b.encoder, &payload_enc, CborIndefiniteLength);
    if (rc != 0) {
        goto exit_err;
    }
    struct mgmt_cbuf *cb = &n_b;
    CborError g_err = CborNoError;
    g_err |= cbor_encode_text_stringz(&payload_enc, "name");
    snprintf(name_str, sizeof(name_str)-1, "rgbpwm/%s", cfg_ptrs[cfg_index]);
    g_err |= cbor_encode_text_stringz(&payload_enc, name_str);
    g_err |= cbor_encode_text_stringz(&payload_enc, "val");
    g_err |= cbor_encode_text_stringz(&payload_enc, rgbpwm_conf_get(1, &cfg_ptrs[cfg_index], 0, 0));
    g_err |= cbor_encode_text_stringz(&payload_enc, "save");
    g_err |= cbor_encode_boolean(&payload_enc, true);
    console_printf("# txcfg: '%s' -> '%s'\n", name_str, rgbpwm_conf_get(1, &cfg_ptrs[cfg_index], 0, 0));
    rc = cbor_encoder_close_container(&cb->encoder, &payload_enc);
    if (rc != 0) {
        goto exit_err;
    }
    hdr->nh_len += cbor_encode_bytes_written(&cb->encoder);
    hdr->nh_len = htons(hdr->nh_len);
    return rsp;
exit_err:
    console_printf("something went wrong\n");
    os_mbuf_free_chain(rsp);
    return 0;
}
struct os_mbuf * rgbpwm_get_txcfg_mbuf ( int cfg_index ) { int rc ; CborEncoder payload_enc ; struct mgmt_cbuf n_b ; struct cbor_mbuf_writer writer ; struct mgmt_hdr * hdr ; struct os_mbuf * rsp ; int num_cfgs = 7 ; char * cfg_ptrs [ ] = { " " , " " , " " , " " , " " , " " , " " } ; char name_str [ CONF_MAX_NAME_LEN ] ; if ( cfg_index >= num_cfgs ) { return 0 ; } rsp = os_msys_get_pkthdr ( 0 , 0 ) ; if ( ! rsp ) { return 0 ; } hdr = ( struct mgmt_hdr * ) os_mbuf_extend ( rsp , sizeof ( struct mgmt_hdr ) ) ; if ( ! hdr ) { goto exit_err ; } hdr -> nh_len = 0 ; hdr -> nh_flags = 0 ; hdr -> nh_op = MGMT_OP_WRITE ; hdr -> nh_group = htons ( MGMT_GROUP_ID_CONFIG ) ; hdr -> nh_seq = 0 ; hdr -> nh_id = CONF_NMGR_OP ; cbor_mbuf_writer_init ( & writer , rsp ) ; cbor_encoder_init ( & n_b . encoder , & writer . enc , 0 ) ; rc = cbor_encoder_create_map ( & n_b . encoder , & payload_enc , CborIndefiniteLength ) ; if ( rc != 0 ) { goto exit_err ; } struct mgmt_cbuf * cb = & n_b ; CborError g_err = CborNoError ; g_err |= cbor_encode_text_stringz ( & payload_enc , " " ) ; snprintf ( name_str , sizeof ( name_str ) - 1 , " " , cfg_ptrs [ cfg_index ] ) ; g_err |= cbor_encode_text_stringz ( & payload_enc , name_str ) ; g_err |= cbor_encode_text_stringz ( & payload_enc , " " ) ; g_err |= cbor_encode_text_stringz ( & payload_enc , rgbpwm_conf_get ( 1 , & cfg_ptrs [ cfg_index ] , 0 , 0 ) ) ; g_err |= cbor_encode_text_stringz ( & payload_enc , " " ) ; g_err |= cbor_encode_boolean ( & payload_enc , true ) ; console_printf ( " \n " , name_str , rgbpwm_conf_get ( 1 , & cfg_ptrs [ cfg_index ] , 0 , 0 ) ) ; rc = cbor_encoder_close_container ( & cb -> encoder , & payload_enc ) ; if ( rc != 0 ) { goto exit_err ; } hdr -> nh_len += cbor_encode_bytes_written ( & cb -> encoder ) ; hdr -> nh_len = htons ( hdr -> nh_len ) ; return rsp ; exit_err : console_printf ( " \n " ) ; os_mbuf_free_chain ( rsp ) ; return 0 ; }
@fn rgbpwm_get_txcfg_mbuf()
 @brief creates a mbuf packet to set the colour configuration remotely
 @param cfg_idx - index into vector of config names
 @returns struct os_mbuf* on success, NULL on fail
@fn rgbpwm_get_txcfg_mbuf () @brief creates a mbuf packet to set the colour configuration remotely @param cfg_idx - index into vector of config names @returns struct os_mbuf * on success NULL on fail