utzig commented on PR #1120:
URL: https://github.com/apache/mynewt-nimble/pull/1120#issuecomment-1102410220

   
   <!-- style-bot -->
   
   ## Style check summary
   
   ### Our coding style is 
[here!](https://github.com/apache/mynewt-core/blob/master/CODING_STANDARDS.md)
   
   
   #### nimble/host/mesh/src/proxy_srv.c
   <details>
   
   ```diff
   @@ -60,312 +60,328 @@
    
    static int conn_count;
    
   -struct bt_mesh_proxy_client *find_client(uint16_t conn_handle)
   -{
   -    int i;
   -
   -    for (i = 0; i < ARRAY_SIZE(clients); i++) {
   -            if (clients[i].cli->conn_handle == conn_handle) {
   -                    return &clients[i];
   -            }
   -    }
   -    return NULL;
   +struct bt_mesh_proxy_client *
   +find_client(uint16_t conn_handle)
   +{
   +    int i;
   +
   +    for (i = 0; i < ARRAY_SIZE(clients); i++) {
   +        if (clients[i].cli->conn_handle == conn_handle) {
   +            return &clients[i];
   +        }
   +    }
   +    return NULL;
    }
    
    /* Next subnet in queue to be advertised */
    static struct bt_mesh_subnet *beacon_sub;
    
   -static int filter_set(struct bt_mesh_proxy_client *client,
   -                  struct os_mbuf *buf)
   -{
   -    uint8_t type;
   -
   -    if (buf->om_len < 1) {
   -            BT_WARN("Too short Filter Set message");
   -            return -EINVAL;
   -    }
   -
   -    type = net_buf_simple_pull_u8(buf);
   -    BT_DBG("type 0x%02x", type);
   -
   -    switch (type) {
   -            case 0x00:
   -                    (void)memset(client->filter, 0, sizeof(client->filter));
   -                    client->filter_type = ACCEPT;
   -                    break;
   -            case 0x01:
   -                    (void)memset(client->filter, 0, sizeof(client->filter));
   -                    client->filter_type = REJECT;
   -                    break;
   -            default:
   -                    BT_WARN("Prohibited Filter Type 0x%02x", type);
   -                    return -EINVAL;
   -    }
   -
   -    return 0;
   -}
   -
   -static void filter_add(struct bt_mesh_proxy_client *client, uint16_t addr)
   -{
   -    int i;
   -
   -    BT_DBG("addr 0x%04x", addr);
   -
   -    if (addr == BT_MESH_ADDR_UNASSIGNED) {
   -            return;
   -    }
   -
   -    for (i = 0; i < ARRAY_SIZE(client->filter); i++) {
   -            if (client->filter[i] == addr) {
   -                    return;
   -            }
   -    }
   -
   -    for (i = 0; i < ARRAY_SIZE(client->filter); i++) {
   -            if (client->filter[i] == BT_MESH_ADDR_UNASSIGNED) {
   -                    client->filter[i] = addr;
   -                    return;
   -            }
   -    }
   -}
   -
   -static void filter_remove(struct bt_mesh_proxy_client *client, uint16_t 
addr)
   -{
   -    int i;
   -
   -    BT_DBG("addr 0x%04x", addr);
   -
   -    if (addr == BT_MESH_ADDR_UNASSIGNED) {
   -            return;
   -    }
   -
   -    for (i = 0; i < ARRAY_SIZE(client->filter); i++) {
   -            if (client->filter[i] == addr) {
   -                    client->filter[i] = BT_MESH_ADDR_UNASSIGNED;
   -                    return;
   -            }
   -    }
   -}
   -
   -static void send_filter_status(struct bt_mesh_proxy_client *client,
   -                           struct bt_mesh_net_rx *rx,
   -                           struct os_mbuf *buf)
   -{
   -    struct bt_mesh_net_tx tx = {
   -            .sub = rx->sub,
   -            .ctx = &rx->ctx,
   -            .src = bt_mesh_primary_addr(),
   -    };
   -    uint16_t filter_size;
   -    int i, err;
   -
   -    /* Configuration messages always have dst unassigned */
   -    tx.ctx->addr = BT_MESH_ADDR_UNASSIGNED;
   -
   -    net_buf_simple_init(buf, 10);
   -
   -    net_buf_simple_add_u8(buf, CFG_FILTER_STATUS);
   -
   -    if (client->filter_type == ACCEPT) {
   -            net_buf_simple_add_u8(buf, 0x00);
   -    } else {
   -            net_buf_simple_add_u8(buf, 0x01);
   -    }
   -
   -    for (filter_size = 0U, i = 0; i < ARRAY_SIZE(client->filter); i++) {
   -            if (client->filter[i] != BT_MESH_ADDR_UNASSIGNED) {
   -                    filter_size++;
   -            }
   -    }
   -
   -    net_buf_simple_add_be16(buf, filter_size);
   -
   -    BT_DBG("%u bytes: %s", buf->om_len, bt_hex(buf->om_data, buf->om_len));
   -
   -    err = bt_mesh_net_encode(&tx, buf, true);
   -    if (err) {
   -            BT_ERR("Encoding Proxy cfg message failed (err %d)", err);
   -            return;
   -    }
   -
   -    err = bt_mesh_proxy_msg_send(client->cli, BT_MESH_PROXY_CONFIG, buf);
   -    if (err) {
   -            BT_ERR("Failed to send proxy cfg message (err %d)", err);
   -    }
   -}
   -
   -static void proxy_filter_recv(uint16_t conn_handle,
   -                          struct bt_mesh_net_rx *rx, struct os_mbuf *buf)
   -{
   -    struct bt_mesh_proxy_client *client;
   -    uint8_t opcode;
   -
   -    client = find_client(conn_handle);
   -
   -    opcode = net_buf_simple_pull_u8(buf);
   -    switch (opcode) {
   -    case CFG_FILTER_SET:
   -            filter_set(client, buf);
   -            send_filter_status(client, rx, buf);
   -            break;
   -            case CFG_FILTER_ADD:
   -                    while (buf->om_len >= 2) {
   -                            uint16_t addr;
   -
   -                            addr = net_buf_simple_pull_be16(buf);
   -                            filter_add(client, addr);
   -                    }
   -                    send_filter_status(client, rx, buf);
   -                    break;
   -            case CFG_FILTER_REMOVE:
   -                    while (buf->om_len >= 2) {
   -                            uint16_t addr;
   -
   -                            addr = net_buf_simple_pull_be16(buf);
   -                            filter_remove(client, addr);
   -                    }
   -                    send_filter_status(client, rx, buf);
   -                    break;
   -            default:
   -                    BT_WARN("Unhandled configuration OpCode 0x%02x", 
opcode);
   -                    break;
   -    }
   -}
   -
   -static void proxy_cfg(struct bt_mesh_proxy_role *role)
   -{
   -    struct os_mbuf *buf = NET_BUF_SIMPLE(BT_MESH_NET_MAX_PDU_LEN);
   -    struct bt_mesh_net_rx rx;
   -    int err;
   -
   -    err = bt_mesh_net_decode(role->buf, BT_MESH_NET_IF_PROXY_CFG,
   -                             &rx, buf);
   -    if (err) {
   -            BT_ERR("Failed to decode Proxy Configuration (err %d)", err);
   -            return;
   -    }
   -
   -    rx.local_match = 1U;
   -
   -    if (bt_mesh_rpl_check(&rx, NULL)) {
   -            BT_WARN("Replay: src 0x%04x dst 0x%04x seq 0x%06x",
   -                    rx.ctx.addr, rx.ctx.recv_dst, rx.seq);
   -            return;
   -    }
   -
   -    /* Remove network headers */
   -    net_buf_simple_pull(buf, BT_MESH_NET_HDR_LEN);
   -
   -    BT_DBG("%u bytes: %s", buf->om_len, bt_hex(buf->om_data, buf->om_len));
   -
   -    if (buf->om_len < 1) {
   -            BT_WARN("Too short proxy configuration PDU");
   -            return;
   -    }
   -
   -    proxy_filter_recv(role->conn_handle, &rx, buf);
   -}
   -
   -static void proxy_msg_recv(struct bt_mesh_proxy_role *role)
   -{
   -    switch (role->msg_type) {
   -    case BT_MESH_PROXY_NET_PDU:
   -            BT_DBG("Mesh Network PDU");
   -            bt_mesh_net_recv(role->buf, 0, BT_MESH_NET_IF_PROXY);
   -            break;
   -    case BT_MESH_PROXY_BEACON:
   -            BT_DBG("Mesh Beacon PDU");
   -            bt_mesh_beacon_recv(role->buf);
   -            break;
   -    case BT_MESH_PROXY_CONFIG:
   -            BT_DBG("Mesh Configuration PDU");
   -            proxy_cfg(role);
   -            break;
   -    default:
   -            BT_WARN("Unhandled Message Type 0x%02x", role->msg_type);
   -            break;
   -    }
   -}
   -
   -static int beacon_send(struct bt_mesh_proxy_client *client, struct 
bt_mesh_subnet *sub)
   -{
   -    struct os_mbuf *buf = NET_BUF_SIMPLE(23);
   -    int rc;
   -
   -    net_buf_simple_init(buf, 1);
   -    bt_mesh_beacon_create(sub, buf);
   -
   -    rc = bt_mesh_proxy_msg_send(client->cli, BT_MESH_PROXY_BEACON, buf);
   -    os_mbuf_free_chain(buf);
   -    return rc;
   -}
   -
   -static int send_beacon_cb(struct bt_mesh_subnet *sub, void *cb_data)
   -{
   -    struct bt_mesh_proxy_client *client = cb_data;
   -
   -    return beacon_send(client, sub);
   -}
   -
   -static void proxy_send_beacons(struct ble_npl_event *work)
   -{
   -    struct bt_mesh_proxy_client *client;
   -
   -    client = ble_npl_event_get_arg(work);
   -
   -    (void)bt_mesh_subnet_find(send_beacon_cb, client);
   -}
   -
   -void bt_mesh_proxy_beacon_send(struct bt_mesh_subnet *sub)
   -{
   -    int i;
   -
   -    if (!sub) {
   -            /* NULL means we send on all subnets */
   -            bt_mesh_subnet_foreach(bt_mesh_proxy_beacon_send);
   -            return;
   -    }
   -
   -    for (i = 0; i < ARRAY_SIZE(clients); i++) {
   -            if (clients[i].cli) {
   -                    beacon_send(&clients[i], sub);
   -            }
   -    }
   -}
   -
   -static void node_id_start(struct bt_mesh_subnet *sub)
   -{
   -    sub->node_id = BT_MESH_NODE_IDENTITY_RUNNING;
   -    sub->node_id_start = k_uptime_get_32();
   -}
   -
   -void bt_mesh_proxy_identity_start(struct bt_mesh_subnet *sub)
   -{
   -    node_id_start(sub);
   -
   -    /* Prioritize the recently enabled subnet */
   -    beacon_sub = sub;
   -}
   -
   -void bt_mesh_proxy_identity_stop(struct bt_mesh_subnet *sub)
   -    {
   -    sub->node_id = BT_MESH_NODE_IDENTITY_STOPPED;
   -    sub->node_id_start = 0U;
   -}
   -
   -int bt_mesh_proxy_identity_enable(void)
   -{
   -    BT_DBG("");
   -
   -    if (!bt_mesh_is_provisioned()) {
   -            return -EAGAIN;
   -    }
   -
   -    if (bt_mesh_subnet_foreach(node_id_start)) {
   -            bt_mesh_adv_update();
   -    }
   -
   -    return 0;
   +static int
   +filter_set(struct bt_mesh_proxy_client *client,
   +           struct os_mbuf *buf)
   +{
   +    uint8_t type;
   +
   +    if (buf->om_len < 1) {
   +        BT_WARN("Too short Filter Set message");
   +        return -EINVAL;
   +    }
   +
   +    type = net_buf_simple_pull_u8(buf);
   +    BT_DBG("type 0x%02x", type);
   +
   +    switch (type) {
   +    case 0x00:
   +        (void)memset(client->filter, 0, sizeof(client->filter));
   +        client->filter_type = ACCEPT;
   +        break;
   +    case 0x01:
   +        (void)memset(client->filter, 0, sizeof(client->filter));
   +        client->filter_type = REJECT;
   +        break;
   +    default:
   +        BT_WARN("Prohibited Filter Type 0x%02x", type);
   +        return -EINVAL;
   +    }
   +
   +    return 0;
   +}
   +
   +static void
   +filter_add(struct bt_mesh_proxy_client *client, uint16_t addr)
   +{
   +    int i;
   +
   +    BT_DBG("addr 0x%04x", addr);
   +
   +    if (addr == BT_MESH_ADDR_UNASSIGNED) {
   +        return;
   +    }
   +
   +    for (i = 0; i < ARRAY_SIZE(client->filter); i++) {
   +        if (client->filter[i] == addr) {
   +            return;
   +        }
   +    }
   +
   +    for (i = 0; i < ARRAY_SIZE(client->filter); i++) {
   +        if (client->filter[i] == BT_MESH_ADDR_UNASSIGNED) {
   +            client->filter[i] = addr;
   +            return;
   +        }
   +    }
   +}
   +
   +static void
   +filter_remove(struct bt_mesh_proxy_client *client, uint16_t addr)
   +{
   +    int i;
   +
   +    BT_DBG("addr 0x%04x", addr);
   +
   +    if (addr == BT_MESH_ADDR_UNASSIGNED) {
   +        return;
   +    }
   +
   +    for (i = 0; i < ARRAY_SIZE(client->filter); i++) {
   +        if (client->filter[i] == addr) {
   +            client->filter[i] = BT_MESH_ADDR_UNASSIGNED;
   +            return;
   +        }
   +    }
   +}
   +
   +static void
   +send_filter_status(struct bt_mesh_proxy_client *client,
   +                   struct bt_mesh_net_rx *rx,
   +                   struct os_mbuf *buf)
   +{
   +    struct bt_mesh_net_tx tx = {
   +        .sub = rx->sub,
   +        .ctx = &rx->ctx,
   +        .src = bt_mesh_primary_addr(),
   +    };
   +    uint16_t filter_size;
   +    int i, err;
   +
   +    /* Configuration messages always have dst unassigned */
   +    tx.ctx->addr = BT_MESH_ADDR_UNASSIGNED;
   +
   +    net_buf_simple_init(buf, 10);
   +
   +    net_buf_simple_add_u8(buf, CFG_FILTER_STATUS);
   +
   +    if (client->filter_type == ACCEPT) {
   +        net_buf_simple_add_u8(buf, 0x00);
   +    } else {
   +        net_buf_simple_add_u8(buf, 0x01);
   +    }
   +
   +    for (filter_size = 0U, i = 0; i < ARRAY_SIZE(client->filter); i++) {
   +        if (client->filter[i] != BT_MESH_ADDR_UNASSIGNED) {
   +            filter_size++;
   +        }
   +    }
   +
   +    net_buf_simple_add_be16(buf, filter_size);
   +
   +    BT_DBG("%u bytes: %s", buf->om_len, bt_hex(buf->om_data, buf->om_len));
   +
   +    err = bt_mesh_net_encode(&tx, buf, true);
   +    if (err) {
   +        BT_ERR("Encoding Proxy cfg message failed (err %d)", err);
   +        return;
   +    }
   +
   +    err = bt_mesh_proxy_msg_send(client->cli, BT_MESH_PROXY_CONFIG, buf);
   +    if (err) {
   +        BT_ERR("Failed to send proxy cfg message (err %d)", err);
   +    }
   +}
   +
   +static void
   +proxy_filter_recv(uint16_t conn_handle,
   +                  struct bt_mesh_net_rx *rx, struct os_mbuf *buf)
   +{
   +    struct bt_mesh_proxy_client *client;
   +    uint8_t opcode;
   +
   +    client = find_client(conn_handle);
   +
   +    opcode = net_buf_simple_pull_u8(buf);
   +    switch (opcode) {
   +    case CFG_FILTER_SET:
   +        filter_set(client, buf);
   +        send_filter_status(client, rx, buf);
   +        break;
   +    case CFG_FILTER_ADD:
   +        while (buf->om_len >= 2) {
   +            uint16_t addr;
   +
   +            addr = net_buf_simple_pull_be16(buf);
   +            filter_add(client, addr);
   +        }
   +        send_filter_status(client, rx, buf);
   +        break;
   +    case CFG_FILTER_REMOVE:
   +        while (buf->om_len >= 2) {
   +            uint16_t addr;
   +
   +            addr = net_buf_simple_pull_be16(buf);
   +            filter_remove(client, addr);
   +        }
   +        send_filter_status(client, rx, buf);
   +        break;
   +    default:
   +        BT_WARN("Unhandled configuration OpCode 0x%02x", opcode);
   +        break;
   +    }
   +}
   +
   +static void
   +proxy_cfg(struct bt_mesh_proxy_role *role)
   +{
   +    struct os_mbuf *buf = NET_BUF_SIMPLE(BT_MESH_NET_MAX_PDU_LEN);
   +    struct bt_mesh_net_rx rx;
   +    int err;
   +
   +    err = bt_mesh_net_decode(role->buf, BT_MESH_NET_IF_PROXY_CFG,
   +                 &rx, buf);
   +    if (err) {
   +        BT_ERR("Failed to decode Proxy Configuration (err %d)", err);
   +        return;
   +    }
   +
   +    rx.local_match = 1U;
   +
   +    if (bt_mesh_rpl_check(&rx, NULL)) {
   +        BT_WARN("Replay: src 0x%04x dst 0x%04x seq 0x%06x",
   +            rx.ctx.addr, rx.ctx.recv_dst, rx.seq);
   +        return;
   +    }
   +
   +    /* Remove network headers */
   +    net_buf_simple_pull(buf, BT_MESH_NET_HDR_LEN);
   +
   +    BT_DBG("%u bytes: %s", buf->om_len, bt_hex(buf->om_data, buf->om_len));
   +
   +    if (buf->om_len < 1) {
   +        BT_WARN("Too short proxy configuration PDU");
   +        return;
   +    }
   +
   +    proxy_filter_recv(role->conn_handle, &rx, buf);
   +}
   +
   +static void
   +proxy_msg_recv(struct bt_mesh_proxy_role *role)
   +{
   +    switch (role->msg_type) {
   +    case BT_MESH_PROXY_NET_PDU:
   +        BT_DBG("Mesh Network PDU");
   +        bt_mesh_net_recv(role->buf, 0, BT_MESH_NET_IF_PROXY);
   +        break;
   +    case BT_MESH_PROXY_BEACON:
   +        BT_DBG("Mesh Beacon PDU");
   +        bt_mesh_beacon_recv(role->buf);
   +        break;
   +    case BT_MESH_PROXY_CONFIG:
   +        BT_DBG("Mesh Configuration PDU");
   +        proxy_cfg(role);
   +        break;
   +    default:
   +        BT_WARN("Unhandled Message Type 0x%02x", role->msg_type);
   +        break;
   +    }
   +}
   +
   +static int
   +beacon_send(struct bt_mesh_proxy_client *client, struct bt_mesh_subnet *sub)
   +{
   +    struct os_mbuf *buf = NET_BUF_SIMPLE(23);
   +    int rc;
   +
   +    net_buf_simple_init(buf, 1);
   +    bt_mesh_beacon_create(sub, buf);
   +
   +    rc = bt_mesh_proxy_msg_send(client->cli, BT_MESH_PROXY_BEACON, buf);
   +    os_mbuf_free_chain(buf);
   +    return rc;
   +}
   +
   +static int
   +send_beacon_cb(struct bt_mesh_subnet *sub, void *cb_data)
   +{
   +    struct bt_mesh_proxy_client *client = cb_data;
   +
   +    return beacon_send(client, sub);
   +}
   +
   +static void
   +proxy_send_beacons(struct ble_npl_event *work)
   +{
   +    struct bt_mesh_proxy_client *client;
   +
   +    client = ble_npl_event_get_arg(work);
   +
   +    (void)bt_mesh_subnet_find(send_beacon_cb, client);
   +}
   +
   +void
   +bt_mesh_proxy_beacon_send(struct bt_mesh_subnet *sub)
   +{
   +    int i;
   +
   +    if (!sub) {
   +        /* NULL means we send on all subnets */
   +        bt_mesh_subnet_foreach(bt_mesh_proxy_beacon_send);
   +        return;
   +    }
   +
   +    for (i = 0; i < ARRAY_SIZE(clients); i++) {
   +        if (clients[i].cli) {
   +            beacon_send(&clients[i], sub);
   +        }
   +    }
   +}
   +
   +static void
   +node_id_start(struct bt_mesh_subnet *sub)
   +{
   +    sub->node_id = BT_MESH_NODE_IDENTITY_RUNNING;
   +    sub->node_id_start = k_uptime_get_32();
   +}
   +
   +void
   +bt_mesh_proxy_identity_start(struct bt_mesh_subnet *sub)
   +{
   +    node_id_start(sub);
   +
   +    /* Prioritize the recently enabled subnet */
   +    beacon_sub = sub;
   +}
   +
   +void
   +bt_mesh_proxy_identity_stop(struct bt_mesh_subnet *sub)
   +{
   +    sub->node_id = BT_MESH_NODE_IDENTITY_STOPPED;
   +    sub->node_id_start = 0U;
   +}
   +
   +int
   +bt_mesh_proxy_identity_enable(void)
   +{
   +    BT_DBG("");
   +
   +    if (!bt_mesh_is_provisioned()) {
   +        return -EAGAIN;
   +    }
   +
   +    if (bt_mesh_subnet_foreach(node_id_start)) {
   +        bt_mesh_adv_update();
   +    }
   +
   +    return 0;
    }
    
    #define ID_TYPE_NET  0x00
   ```
   
   </details>


-- 
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.

To unsubscribe, e-mail: commits-unsubscr...@mynewt.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to