linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Stotland, Inga" <inga.stotland@intel.com>
To: "jakub.witowski@silvair.com" <jakub.witowski@silvair.com>,
	"linux-bluetooth@vger.kernel.org"
	<linux-bluetooth@vger.kernel.org>
Cc: "michal.lowas-rzechonek@silvair.com" 
	<michal.lowas-rzechonek@silvair.com>
Subject: Re: [PATCH BlueZ v6 1/3] mesh: Extract read_* functions in mesh-config-json
Date: Tue, 30 Jul 2019 23:18:07 +0000	[thread overview]
Message-ID: <5805488b1228a044dd5c9261b7fafd53acdd467b.camel@intel.com> (raw)
In-Reply-To: <20190729134047.21033-2-michal.lowas-rzechonek@silvair.com>

[-- Attachment #1: Type: text/plain, Size: 3899 bytes --]

Hi Jakub,

On Mon, 2019-07-29 at 15:40 +0200, Michał Lowas-Rzechonek wrote:
> From: Jakub Witowski <jakub.witowski@silvair.com>
> 
> This is a small improvement of read_node function readability.
> ---
>  mesh/mesh-config-json.c | 68 +++++++++++++++++++++++++++++--------
> ----
>  1 file changed, 49 insertions(+), 19 deletions(-)
> 
> diff --git a/mesh/mesh-config-json.c b/mesh/mesh-config-json.c
> index e3baf5dc6..c63883a3d 100644
> --- a/mesh/mesh-config-json.c
> +++ b/mesh/mesh-config-json.c
> @@ -297,6 +297,51 @@ static json_object *jarray_key_del(json_object
> *jarray, int16_t idx)
>  	return jarray_new;
>  }
>  
> +static bool read_unicast_address(json_object *jobj,
> +							uint16_t
> *unicast)
> +{
> +	json_object *jvalue;
> +	char *str;
> +
> +	if (!json_object_object_get_ex(jobj, "unicastAddress",
> &jvalue))
> +		return false;
> +
> +	str = (char *)json_object_get_string(jvalue);
> +	if (sscanf(str, "%04hx", unicast) != 1)
> +		return false;
> +
> +	return true;
> +}
> +
> +static bool read_default_ttl(json_object *jobj,
> +								uint8_t
> *ttl)
> +{
> +	json_object *jvalue;
> +	int val;
> +
> +	if (!json_object_object_get_ex(jobj, "defaultTTL", &jvalue))
> +		return false;
> +
> +	val = json_object_get_int(jvalue);
> +
> +	if (val < 0 || val == 1 || val > DEFAULT_TTL)
> +		return false;

New line here to separate from "if" statement

> +	*ttl = (uint8_t) val;
> +
> +	return true;
> +}
> +
> +static bool read_seq_number(json_object *jobj, uint32_t *seq_number)
> +{
> +	json_object *jvalue;
> +
> +	if (!json_object_object_get_ex(jobj, "sequenceNumber",
> &jvalue))
> +		return false;
> +
> +	*seq_number = json_object_get_int(jvalue);
> +	return true;
> +}
> +
>  static bool read_iv_index(json_object *jobj, uint32_t *idx, bool
> *update)
>  {
>  	int tmp;
> @@ -424,7 +469,7 @@ fail:
>  	return false;
>  }
>  
> -static bool read_net_keys(json_object *jobj,  struct
> mesh_config_node *node)
> +static bool read_net_keys(json_object *jobj, struct mesh_config_node
> *node)
>  {
>  	json_object *jarray;
>  	int len;
> @@ -1294,7 +1339,6 @@ static bool read_net_transmit(json_object
> *jobj, struct mesh_config_node *node)
>  static bool read_node(json_object *jnode, struct mesh_config_node
> *node)
>  {
>  	json_object *jvalue;
> -	char *str;
>  
>  	if (!read_iv_index(jnode, &node->iv_index, &node->iv_update)) {
>  		l_info("Failed to read IV index");
> @@ -1318,25 +1362,11 @@ static bool read_node(json_object *jnode,
> struct mesh_config_node *node)
>  
>  	parse_features(jnode, node);
>  
> -	if (!json_object_object_get_ex(jnode, "unicastAddress",
> &jvalue)) {
> -		l_info("Bad config: Unicast address must be present");
> -		return false;
> -	}
> -
> -	str = (char *)json_object_get_string(jvalue);
> -	if (sscanf(str, "%04hx", &node->unicast) != 1)
> -		return false;
> -
> -	if (json_object_object_get_ex(jnode, "defaultTTL", &jvalue)) {
> -		int ttl = json_object_get_int(jvalue);
> +	read_unicast_address(jnode, &node->unicast);
>  

The return status needs to be checked: unicast must be present


> -		if (ttl < 0 || ttl == 1 || ttl > DEFAULT_TTL)
> -			return false;
> -		node->ttl = (uint8_t) ttl;
> -	}
> +	read_default_ttl(jnode, &node->ttl);
>  
I would prefer to keep ttl and sequesnce number parsing as they are
now:
they are not mandatory, but we don't want to allow for bad format,
i.e., we dont want to fail if the fields are absent, but we do want to
fail if the format is wrong.

> -	if (json_object_object_get_ex(jnode, "sequenceNumber",
> &jvalue))
> -		node->seq_number = json_object_get_int(jvalue);
> +	read_seq_number(jnode, &node->seq_number);
>  
>  	/* Check for required "elements" property */
>  	if (!json_object_object_get_ex(jnode, "elements", &jvalue))

Regards,
Inga

[-- Attachment #2: smime.p7s --]
[-- Type: application/x-pkcs7-signature, Size: 3265 bytes --]

  reply	other threads:[~2019-07-30 23:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-29 13:40 [PATCH BlueZ v6 0/3] Implement org.bluez.mesh.Network1.Import() D-Bus API Michał Lowas-Rzechonek
2019-07-29 13:40 ` [PATCH BlueZ v6 1/3] mesh: Extract read_* functions in mesh-config-json Michał Lowas-Rzechonek
2019-07-30 23:18   ` Stotland, Inga [this message]
2019-07-29 13:40 ` [PATCH BlueZ v6 2/3] mesh: Add documentation for Import() D-Bus API Michał Lowas-Rzechonek
2019-07-29 13:40 ` [PATCH BlueZ v6 3/3] mesh: Implement Import() D-Bus API of org.bluez.mesh.Network1 interface Michał Lowas-Rzechonek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5805488b1228a044dd5c9261b7fafd53acdd467b.camel@intel.com \
    --to=inga.stotland@intel.com \
    --cc=jakub.witowski@silvair.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=michal.lowas-rzechonek@silvair.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).