aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichał Narajowski <michal.narajowski@codecoup.pl>2016-10-05 06:28:27 -0400
committerMarcel Holtmann <marcel@holtmann.org>2016-10-06 05:52:29 -0400
commit1b422066658b7cc985fa020066b72d28159d858f (patch)
tree3a09eedeb83122cfaefcaa5a17fc41d3f0205261
parent7ddb30c7471ed69b75ae4c2601d45cbda5d390ff (diff)
Bluetooth: Refactor append name and appearance
Use eir_append_data to remove code duplication. Signed-off-by: Michał Narajowski <michal.narajowski@codecoup.pl> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
-rw-r--r--net/bluetooth/hci_request.c44
-rw-r--r--net/bluetooth/hci_request.h23
-rw-r--r--net/bluetooth/mgmt.c21
3 files changed, 38 insertions, 50 deletions
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 3c44c54a056f..e2288421fe6b 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -21,8 +21,6 @@
21 SOFTWARE IS DISCLAIMED. 21 SOFTWARE IS DISCLAIMED.
22*/ 22*/
23 23
24#include <asm/unaligned.h>
25
26#include <net/bluetooth/bluetooth.h> 24#include <net/bluetooth/bluetooth.h>
27#include <net/bluetooth/hci_core.h> 25#include <net/bluetooth/hci_core.h>
28#include <net/bluetooth/mgmt.h> 26#include <net/bluetooth/mgmt.h>
@@ -992,46 +990,39 @@ static u8 append_local_name(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
992 /* complete name fits and is eq to max short name len or smaller */ 990 /* complete name fits and is eq to max short name len or smaller */
993 if (complete_len <= max_len && 991 if (complete_len <= max_len &&
994 complete_len <= HCI_MAX_SHORT_NAME_LENGTH) { 992 complete_len <= HCI_MAX_SHORT_NAME_LENGTH) {
995 ptr[0] = complete_len + 1; 993 return eir_append_data(ptr, ad_len, EIR_NAME_COMPLETE,
996 ptr[1] = EIR_NAME_COMPLETE; 994 hdev->dev_name, complete_len);
997 memcpy(ptr + 2, hdev->dev_name, complete_len);
998
999 return ad_len + complete_len + 2;
1000 } 995 }
1001 996
1002 /* short name set and fits */ 997 /* short name set and fits */
1003 if (short_len && short_len <= max_len) { 998 if (short_len && short_len <= max_len) {
1004 ptr[0] = short_len + 1; 999 return eir_append_data(ptr, ad_len, EIR_NAME_SHORT,
1005 ptr[1] = EIR_NAME_SHORT; 1000 hdev->short_name, short_len);
1006 memcpy(ptr + 2, hdev->short_name, short_len);
1007
1008 return ad_len + short_len + 2;
1009 } 1001 }
1010 1002
1011 /* no short name set so shorten complete name */ 1003 /* no short name set so shorten complete name */
1012 if (!short_len) { 1004 if (!short_len) {
1013 ptr[0] = max_len + 1; 1005 return eir_append_data(ptr, ad_len, EIR_NAME_SHORT,
1014 ptr[1] = EIR_NAME_SHORT; 1006 hdev->dev_name, max_len);
1015 memcpy(ptr + 2, hdev->dev_name, max_len);
1016
1017 return ad_len + max_len + 2;
1018 } 1007 }
1019 1008
1020 return ad_len; 1009 return ad_len;
1021} 1010}
1022 1011
1012static u8 append_appearance(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
1013{
1014 return eir_append_le16(ptr, ad_len, EIR_APPEARANCE, hdev->appearance);
1015}
1016
1023static u8 create_default_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) 1017static u8 create_default_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
1024{ 1018{
1025 u8 scan_rsp_len = 0; 1019 u8 scan_rsp_len = 0;
1026 1020
1027 if (hdev->appearance) { 1021 if (hdev->appearance) {
1028 ptr[0] = 3; 1022 scan_rsp_len = append_appearance(hdev, ptr, scan_rsp_len);
1029 ptr[1] = EIR_APPEARANCE;
1030 put_unaligned_le16(hdev->appearance, ptr + 2);
1031 scan_rsp_len += 4;
1032 } 1023 }
1033 1024
1034 return append_local_name(hdev, ptr + scan_rsp_len, scan_rsp_len); 1025 return append_local_name(hdev, ptr, scan_rsp_len);
1035} 1026}
1036 1027
1037static u8 create_instance_scan_rsp_data(struct hci_dev *hdev, u8 instance, 1028static u8 create_instance_scan_rsp_data(struct hci_dev *hdev, u8 instance,
@@ -1048,18 +1039,13 @@ static u8 create_instance_scan_rsp_data(struct hci_dev *hdev, u8 instance,
1048 instance_flags = adv_instance->flags; 1039 instance_flags = adv_instance->flags;
1049 1040
1050 if ((instance_flags & MGMT_ADV_FLAG_APPEARANCE) && hdev->appearance) { 1041 if ((instance_flags & MGMT_ADV_FLAG_APPEARANCE) && hdev->appearance) {
1051 ptr[0] = 3; 1042 scan_rsp_len = append_appearance(hdev, ptr, scan_rsp_len);
1052 ptr[1] = EIR_APPEARANCE;
1053 put_unaligned_le16(hdev->appearance, ptr + 2);
1054 scan_rsp_len += 4;
1055 ptr += 4;
1056 } 1043 }
1057 1044
1058 memcpy(ptr, adv_instance->scan_rsp_data, 1045 memcpy(&ptr[scan_rsp_len], adv_instance->scan_rsp_data,
1059 adv_instance->scan_rsp_len); 1046 adv_instance->scan_rsp_len);
1060 1047
1061 scan_rsp_len += adv_instance->scan_rsp_len; 1048 scan_rsp_len += adv_instance->scan_rsp_len;
1062 ptr += adv_instance->scan_rsp_len;
1063 1049
1064 if (instance_flags & MGMT_ADV_FLAG_LOCAL_NAME) 1050 if (instance_flags & MGMT_ADV_FLAG_LOCAL_NAME)
1065 scan_rsp_len = append_local_name(hdev, ptr, scan_rsp_len); 1051 scan_rsp_len = append_local_name(hdev, ptr, scan_rsp_len);
diff --git a/net/bluetooth/hci_request.h b/net/bluetooth/hci_request.h
index ac1e11006f38..6b06629245a8 100644
--- a/net/bluetooth/hci_request.h
+++ b/net/bluetooth/hci_request.h
@@ -20,6 +20,8 @@
20 SOFTWARE IS DISCLAIMED. 20 SOFTWARE IS DISCLAIMED.
21*/ 21*/
22 22
23#include <asm/unaligned.h>
24
23#define hci_req_sync_lock(hdev) mutex_lock(&hdev->req_lock) 25#define hci_req_sync_lock(hdev) mutex_lock(&hdev->req_lock)
24#define hci_req_sync_unlock(hdev) mutex_unlock(&hdev->req_lock) 26#define hci_req_sync_unlock(hdev) mutex_unlock(&hdev->req_lock)
25 27
@@ -103,3 +105,24 @@ static inline void hci_update_background_scan(struct hci_dev *hdev)
103 105
104void hci_request_setup(struct hci_dev *hdev); 106void hci_request_setup(struct hci_dev *hdev);
105void hci_request_cancel_all(struct hci_dev *hdev); 107void hci_request_cancel_all(struct hci_dev *hdev);
108
109static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type,
110 u8 *data, u8 data_len)
111{
112 eir[eir_len++] = sizeof(type) + data_len;
113 eir[eir_len++] = type;
114 memcpy(&eir[eir_len], data, data_len);
115 eir_len += data_len;
116
117 return eir_len;
118}
119
120static inline u16 eir_append_le16(u8 *eir, u16 eir_len, u8 type, u16 data)
121{
122 eir[eir_len++] = sizeof(type) + sizeof(data);
123 eir[eir_len++] = type;
124 put_unaligned_le16(data, &eir[eir_len]);
125 eir_len += sizeof(data);
126
127 return eir_len;
128}
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index 19b8a5e9420d..736038085feb 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -867,27 +867,6 @@ static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
867 sizeof(rp)); 867 sizeof(rp));
868} 868}
869 869
870static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
871 u8 data_len)
872{
873 eir[eir_len++] = sizeof(type) + data_len;
874 eir[eir_len++] = type;
875 memcpy(&eir[eir_len], data, data_len);
876 eir_len += data_len;
877
878 return eir_len;
879}
880
881static inline u16 eir_append_le16(u8 *eir, u16 eir_len, u8 type, u16 data)
882{
883 eir[eir_len++] = sizeof(type) + sizeof(data);
884 eir[eir_len++] = type;
885 put_unaligned_le16(data, &eir[eir_len]);
886 eir_len += sizeof(data);
887
888 return eir_len;
889}
890
891static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir) 870static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir)
892{ 871{
893 u16 eir_len = 0; 872 u16 eir_len = 0;