Hi Hendrik,
On 12/5/2011 4:50 AM, Hendrik Sattler wrote:
Am 05.12.2011 12:46, schrieb Hemant Gupta:
[...]
+static int mgmt_passkey_reply(int index, bdaddr_t *bdaddr, uint32_t
passkey)
+{
+ char buf[MGMT_HDR_SIZE + sizeof(struct mgmt_cp_user_passkey_reply)];
+ struct mgmt_hdr *hdr = (void *) buf;
+ size_t buf_len;
+ char addr[18];
+
+ ba2str(bdaddr, addr);
+ DBG("index %d addr %s passkey %06u", index, addr, passkey);
+
+ memset(buf, 0, sizeof(buf));
+
+ if (passkey == INVALID_PASSKEY) {
+ struct mgmt_cp_user_passkey_neg_reply *cp;
+
+ hdr->opcode = htobs(MGMT_OP_USER_PASSKEY_NEG_REPLY);
+ hdr->len = htobs(sizeof(*cp));
+ hdr->index = htobs(index);
+
+ cp = (void *) &buf[sizeof(*hdr)];
By definition, that the same as:
cp = (void *) (hdr + 1);
And you can do it in the same line as the definition of *cp.
+ bacpy(&cp->bdaddr, bdaddr);
+
+ buf_len = sizeof(*hdr) + sizeof(*cp);
+ } else {
+ struct mgmt_cp_user_passkey_reply *cp;
+
+ hdr->opcode = htobs(MGMT_OP_USER_PASSKEY_REPLY);
+ hdr->len = htobs(sizeof(*cp));
+ hdr->index = htobs(index);
+
+ cp = (void *) &buf[sizeof(*hdr)];
+ bacpy(&cp->bdaddr, bdaddr);
+ cp->passkey = htobl(passkey);
+
+ buf_len = sizeof(*hdr) + sizeof(*cp);
+ }
Ever wondered why if and else look almost the same? They should be
merged as far as possible.
I generally agree with this statement, but think it doesn't really apply
here. Since the definition of "cp" is different between the IF and the
ELSE, the generated code is quite different, even though they look the
same: The sizeof(*cp) is different, the MGMT_OP Opcode is different,
and the structure members might be named the same, but since they are in
different structures, they are in fact different as well.
The only thing that could be safely "shared" is the hdr->index, I think.
--
Brian Gix
bgix@xxxxxxxxxxxxxx
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum
--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html