Refactoring bnep code to reduce redundancy
cover Refactoring bnep code to reduce redundancy
This patch set contains refactoring of bnep functionality, minor fixes in android/pan and adding PAN profile NAP sdp record to android.
Ravi kumar Veeramally (10):
bnep: Rename bnep_kill_connection to bnep_conndel
bnep: Rename send ctrl_rsp and make it global
bnep: Move bnep related calls to bnep.h|c profiles/network/server: Delete function which does nothing profiles/network: Move pan sdp record function bnep and make it global android/pan: Remove channel unref which causing disconnection android/pan: Fix missing cleanup calls
android/pan: Fix minor white space
android/pan: Free connected pan devices on profile unregister call android/pan: Add PAN NAP sdp record fo server role
android/pan.c | 40 +++++++- profiles/network/bnep.c | 209 ++++++++++++++++++++++++++++++++++++++++- profiles/network/bnep.h | 10 +- profiles/network/connection.c | 2 +- profiles/network/manager.c | 2 - profiles/network/server.c | 213 +----------------------------------------- profiles/network/server.h | 1 - 7 files changed, 256 insertions(+), 221 deletions(-)
Signature · 1 line
1.8.3.2
PATCH 1/10 bnep: Rename bnep_kill_connection to bnep_conndel
Renaming bnep_kill_connection to bnep_conndel to maintain consistency with bnep_connadd function name.
--- android/pan.c | 2 +- profiles/network/bnep.c | 2 +- profiles/network/bnep.h | 3 +-- profiles/network/connection.c | 2 +- profiles/network/server.c | 2 +- 5 files changed, 5 insertions(+), 6 deletions(-)
android/pan.c +1 -1
@@ -288,7 +288,7 @@ static void bt_pan_disconnect(const void *buf, uint16_t len) }bnep_if_down (dev->iface); -bnep_kill_connection (&dst); +bnep_conndel (&dst);bt_pan_notify_conn_state (dev,HAL_PAN_STATE_DISCONNECTED );pan_device_free (dev);
profiles/network/bnep.c +1 -1
@@ -160,7 +160,7 @@ int bnep_cleanup(void)return 0 ; } -int bnep_kill_connection (const bdaddr_t *dst) +int bnep_conndel (const bdaddr_t *dst) {struct bnep_conndel_req req;
profiles/network/bnep.h +1 -2
@@ -28,9 +28,8 @@ uint16_t bnep_service_id(const char *svc);const char *bnep_uuid (uint16_t id);const char *bnep_name (uint16_t id); -int bnep_kill_connection (const bdaddr_t *dst); -int bnep_connadd (int sk,uint16_t role,char *dev); +int bnep_conndel (const bdaddr_t *dst);int bnep_if_up (const char *devname);int bnep_if_down (const char *devname);int bnep_add_to_bridge (const char *devname,const char *bridge);
profiles/network/connection.c +1 -1
@@ -171,7 +171,7 @@ static void connection_destroy(DBusConnection *conn, void *user_data)if (nc->state ==CONNECTED ) {bnep_if_down (nc->dev); -bnep_kill_connection (device_get_address (nc->peer->device)); +bnep_conndel (device_get_address (nc->peer->device)); }else if (nc->io)cancel_connection (nc, -EIO ); }
profiles/network/server.c +1 -1
@@ -608,7 +608,7 @@ static void server_remove_sessions(struct network_server *ns)bnep_del_from_bridge (session->dev, ns->bridge);bnep_if_down (session->dev); -bnep_kill_connection (&session->dst); +bnep_conndel (&session->dst); }g_slist_free_full (ns->sessions, session_free);
Signature · 1 line
1.8.3.2
PATCH 2/10 bnep: Rename send ctrl_rsp and make it global
In reply to Ravi kumar VeeramallyRenaming send_bnep_ctrl_rsp to bnep_send_ctrl_rsp and moving to bnep.h. It is required in android/*.
--- profiles/network/bnep.c | 11 +++++++++++ profiles/network/bnep.h | 2 ++ profiles/network/server.c | 13 +------------ 3 files changed, 14 insertions(+), 12 deletions(-)
profiles/network/bnep.c +11 -0
@@ -464,3 +464,14 @@ int bnep_del_from_bridge(const char *devname, const char *bridge)return 0 ; } + +ssize_t bnep_send_ctrl_rsp (int sk,uint8_t type,uint8_t ctrl,uint16_t resp) +{ +struct bnep_control_rsp rsp; + + rsp.type = type; + rsp.ctrl = ctrl; + rsp.resp =htons (resp); + +return send (sk, &rsp,sizeof (rsp),0 ); +}
profiles/network/bnep.h +2 -0
@@ -39,3 +39,5 @@ typedef void (*bnep_connect_cb) (GIOChannel *chan, char *iface, int err, void *data);int bnep_connect (int sk,uint16_t src,uint16_t dst,bnep_connect_cb conn_cb,void *data); + +ssize_t bnep_send_ctrl_rsp (int sk,uint8_t type,uint8_t ctrl,uint16_t resp);
profiles/network/server.c +1 -12
@@ -251,17 +251,6 @@ static sdp_record_t *server_record_new(const char *name, uint16_t id)return record; } -static ssize_t send_bnep_ctrl_rsp (int sk,uint16_t val) -{ -struct bnep_control_rsp rsp; - - rsp.type =BNEP_CONTROL ; - rsp.ctrl =BNEP_SETUP_CONN_RSP ; - rsp.resp =htons (val); - -return send (sk, &rsp,sizeof (rsp),0 ); -} -static int server_connadd (struct network_server *ns,struct network_session *session,uint16_t dst_role)
@@ -462,7 +451,7 @@ static gboolean bnep_setup(GIOChannel *chan, rsp =BNEP_SUCCESS ; reply: -send_bnep_ctrl_rsp (sk, rsp); +bnep_send_ctrl_rsp (sk,BNEP_CONTROL ,BNEP_SETUP_CONN_RSP , rsp);return FALSE; }
Signature · 1 line
1.8.3.2
PATCH 3/10 bnep: Move bnep related calls to bnep.h|c
In reply to Ravi kumar VeeramallyMoving bnep related calls to bnep.h|c to reduce redundancy while using same in android/*.
--- profiles/network/bnep.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++ profiles/network/bnep.h | 5 +++- profiles/network/server.c | 66 ---------------------------------------------- 3 files changed, 71 insertions(+), 67 deletions(-)
profiles/network/bnep.c +67 -0
@@ -475,3 +475,70 @@ ssize_t bnep_send_ctrl_rsp(int sk, uint8_t type, uint8_t ctrl, uint16_t resp)return send (sk, &rsp,sizeof (rsp),0 ); } + +uint16_t bnep_setup_chk (uint16_t dst,uint16_t src) +{ +/* Allowed PAN Profile scenarios */ +switch (dst) { +case BNEP_SVC_NAP : +case BNEP_SVC_GN : +if (src ==BNEP_SVC_PANU ) +return 0 ; +return BNEP_CONN_INVALID_SRC ; +case BNEP_SVC_PANU : +if (src ==BNEP_SVC_PANU || src ==BNEP_SVC_GN || + src ==BNEP_SVC_NAP ) +return 0 ; + +return BNEP_CONN_INVALID_SRC ; + } + +return BNEP_CONN_INVALID_DST ; +} + +uint16_t bnep_setup_decode (struct bnep_setup_conn_req *req,uint16_t *dst, +uint16_t *src) +{ +const uint8_t bt_base[] = {0x00 ,0x00 ,0x10 ,0x00 ,0x80 ,0x00 , +0x00 ,0x80 ,0x5F ,0x9B ,0x34 ,0xFB }; +uint8_t *dest, *source; +uint32_t val; + + dest = req->service; + source = req->service + req->uuid_size; + +switch (req->uuid_size) { +case 2 :/* UUID16 */ + *dst =bt_get_be16 (dest); + *src =bt_get_be16 (source); +break ; +case 16 :/* UUID128 */ +/* Check that the bytes in the UUID, except the service ID +* itself, are correct. The service ID is checked in +* bnep_setup_chk(). */ +if (memcmp (&dest[4 ], bt_base,sizeof (bt_base)) !=0 ) +return BNEP_CONN_INVALID_DST ; +if (memcmp (&source[4 ], bt_base,sizeof (bt_base)) !=0 ) +return BNEP_CONN_INVALID_SRC ; + +/* Intentional no-break */ + +case 4 :/* UUID32 */ + val =bt_get_be32 (dest); +if (val >0xffff ) +return BNEP_CONN_INVALID_DST ; + + *dst = val; + + val =bt_get_be32 (source); +if (val >0xffff ) +return BNEP_CONN_INVALID_SRC ; + + *src = val; +break ; +default : +return BNEP_CONN_INVALID_SVC ; + } + +return BNEP_SUCCESS ; +}
profiles/network/bnep.h +4 -1
@@ -40,4 +40,7 @@ typedef void (*bnep_connect_cb) (GIOChannel *chan, char *iface, int err,int bnep_connect (int sk,uint16_t src,uint16_t dst,bnep_connect_cb conn_cb,void *data); -ssize_t bnep_send_ctrl_rsp (int sk,uint8_t type,uint8_t ctrl,uint16_t resp); +ssize_t bnep_send_ctrl_rsp (int sk,uint8_t type,uint8_t ctrl,uint16_t resp); +uint16_t bnep_setup_chk (uint16_t dst_role,uint16_t src_role); +uint16_t bnep_setup_decode (struct bnep_setup_conn_req *req,uint16_t *dst, +uint16_t *src);
profiles/network/server.c +0 -66
72 lines not on this page · raw message
Signature · 1 line
1.8.3.2
PATCH 4/10 profiles/network/server: Delete function which does nothing
In reply to Ravi kumar Veeramally--- profiles/network/manager.c | 2 -- profiles/network/server.c | 4 ---- profiles/network/server.h | 1 - 3 files changed, 7 deletions(-)
profiles/network/manager.c +0 -2
@@ -200,8 +200,6 @@ static int network_init(void)static void network_exit (void ) { -server_exit (); -btd_profile_unregister (&panu_profile);btd_profile_unregister (&gn_profile);btd_profile_unregister (&nap_profile);
profiles/network/server.c +0 -4
@@ -493,10 +493,6 @@ int server_init(gboolean secure)return 0 ; } -void server_exit (void ) -{ -} -static uint32_t register_server_record (struct network_server *ns) {sdp_record_t *record;
profiles/network/server.h +0 -1
@@ -22,6 +22,5 @@*/ int server_init (gboolean secure); -void server_exit (void );int server_register (struct btd_adapter *adapter,uint16_t id);int server_unregister (struct btd_adapter *adapter,uint16_t id);
Signature · 1 line
1.8.3.2
PATCH 5/10 profiles/network: Move pan sdp record function bnep and make it global
In reply to Ravi kumar VeeramallyMoving pan sdp record function bnep, it is required in android/pan. Even though it is not exactly related to bnep, but bnep.h|c is dbus free files and shared with android/*.
--- profiles/network/bnep.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++ profiles/network/bnep.h | 2 + profiles/network/server.c | 128 +-------------------------------------------- 3 files changed, 132 insertions(+), 127 deletions(-)
profiles/network/bnep.c +129 -0
@@ -39,6 +39,8 @@#include <bluetooth/bluetooth.h> #include <bluetooth/l2cap.h> #include <bluetooth/bnep.h> +#include <bluetooth/sdp.h> +#include <bluetooth/sdp_lib.h> #include <glib.h>
@@ -542,3 +544,130 @@ uint16_t bnep_setup_decode(struct bnep_setup_conn_req *req, uint16_t *dst,return BNEP_SUCCESS ; } + +sdp_record_t *pan_record (const char *name,uint16_t id,gboolean security) +{ +sdp_list_t *svclass, *pfseq, *apseq, *root, *aproto; +uuid_t root_uuid, pan, l2cap, bnep; +sdp_profile_desc_t profile[1 ]; +sdp_list_t *proto[2 ]; +sdp_data_t *v, *p; +uint16_t psm =BNEP_PSM , version =0x0100 ; +uint16_t security_desc = (security ?0x0001 :0x0000 ); +uint16_t net_access_type =0xfffe ; +uint32_t max_net_access_rate =0 ; +const char *desc ="Network service" ; +sdp_record_t *record; + + record =sdp_record_alloc (); +if (!record) +return NULL ; + + record->attrlist =NULL ; + record->pattern =NULL ; + +switch (id) { +case BNEP_SVC_NAP : +sdp_uuid16_create (&pan,NAP_SVCLASS_ID ); + svclass =sdp_list_append (NULL , &pan); +sdp_set_service_classes (record, svclass); + +sdp_uuid16_create (&profile[0 ].uuid,NAP_PROFILE_ID ); + profile[0 ].version =0x0100 ; + pfseq =sdp_list_append (NULL , &profile[0 ]); +sdp_set_profile_descs (record, pfseq); + +sdp_set_info_attr (record, name,NULL , desc); + +sdp_attr_add_new (record,SDP_ATTR_NET_ACCESS_TYPE , +SDP_UINT16 , &net_access_type); +sdp_attr_add_new (record,SDP_ATTR_MAX_NET_ACCESSRATE , +SDP_UINT32 , &max_net_access_rate); +break ; +case BNEP_SVC_GN : +sdp_uuid16_create (&pan,GN_SVCLASS_ID ); + svclass =sdp_list_append (NULL , &pan); +sdp_set_service_classes (record, svclass); + +sdp_uuid16_create (&profile[0 ].uuid,GN_PROFILE_ID ); + profile[0 ].version =0x0100 ; + pfseq =sdp_list_append (NULL , &profile[0 ]); +sdp_set_profile_descs (record, pfseq); + +sdp_set_info_attr (record, name,NULL , desc); +break ; +case BNEP_SVC_PANU : +sdp_uuid16_create (&pan,PANU_SVCLASS_ID ); + svclass =sdp_list_append (NULL , &pan); +sdp_set_service_classes (record, svclass); + +sdp_uuid16_create (&profile[0 ].uuid,PANU_PROFILE_ID ); + profile[0 ].version =0x0100 ; + pfseq =sdp_list_append (NULL , &profile[0 ]); +sdp_set_profile_descs (record, pfseq); + +sdp_set_info_attr (record, name,NULL , desc); +break ; +default : +sdp_record_free (record); +return NULL ; + } + +sdp_uuid16_create (&root_uuid,PUBLIC_BROWSE_GROUP ); + root =sdp_list_append (NULL , &root_uuid); +sdp_set_browse_groups (record, root); + +sdp_uuid16_create (&l2cap,L2CAP_UUID ); + proto[0 ] =sdp_list_append (NULL , &l2cap); + p =sdp_data_alloc (SDP_UINT16 , &psm); + proto[0 ] =sdp_list_append (proto[0 ], p); + apseq =sdp_list_append (NULL , proto[0 ]); + +sdp_uuid16_create (&bnep,BNEP_UUID ); + proto[1 ] =sdp_list_append (NULL , &bnep); + v =sdp_data_alloc (SDP_UINT16 , &version); + proto[1 ] =sdp_list_append (proto[1 ], v); + +/* Supported protocols */ + { +uint16_t ptype[] = { +0x0800 ,/* IPv4 */ +0x0806 ,/* ARP */ + }; +sdp_data_t *head, *pseq; +int p; + +for (p =0 , head =NULL ; p <2 ; p++) { +sdp_data_t *data =sdp_data_alloc (SDP_UINT16 , + &ptype[p]); +if (head) +sdp_seq_append (head, data); +else + head = data; + } + pseq =sdp_data_alloc (SDP_SEQ16 , head); + proto[1 ] =sdp_list_append (proto[1 ], pseq); + } + + apseq =sdp_list_append (apseq, proto[1 ]); + + aproto =sdp_list_append (NULL , apseq); +sdp_set_access_protos (record, aproto); + +sdp_add_lang_attr (record); + +sdp_attr_add_new (record,SDP_ATTR_SECURITY_DESC , +SDP_UINT16 , &security_desc); + +sdp_data_free (p); +sdp_data_free (v); +sdp_list_free (apseq,NULL ); +sdp_list_free (root,NULL ); +sdp_list_free (aproto,NULL ); +sdp_list_free (proto[0 ],NULL ); +sdp_list_free (proto[1 ],NULL ); +sdp_list_free (svclass,NULL ); +sdp_list_free (pfseq,NULL ); + +return record; +}
profiles/network/bnep.h +2 -0
@@ -44,3 +44,5 @@ ssize_t bnep_send_ctrl_rsp(int sk, uint8_t type, uint8_t ctrl, uint16_t resp);uint16_t bnep_setup_chk (uint16_t dst_role,uint16_t src_role);uint16_t bnep_setup_decode (struct bnep_setup_conn_req *req,uint16_t *dst,uint16_t *src); + +sdp_record_t *pan_record (const char *name,uint16_t id,gboolean security);
profiles/network/server.c +1 -127
140 lines not on this page · raw message
Signature · 1 line
1.8.3.2
PATCH 6/10 android/pan: Remove channel unref which causing disconnection
In reply to Ravi kumar VeeramallyChannel unreferencing here causing calling watchdog_cb and eventually connection is dismissed immediately. Channel is already freed in bnep.c:free_bnep_connect().
--- android/pan.c | 2 -- 1 file changed, 2 deletions(-)
android/pan.c +0 -2
@@ -156,8 +156,6 @@ static void bnep_conn_cb(GIOChannel *chan, char *iface, int err, void *data) dev->watch =g_io_add_watch (chan,G_IO_ERR |G_IO_HUP |G_IO_NVAL , bnep_watchdog_cb, dev); -g_io_channel_unref (dev->io); - dev->io =NULL ; }static void connect_cb (GIOChannel *chan,GError *err,gpointer data)
Signature · 1 line
1.8.3.2
PATCH 7/10 android/pan: Fix missing cleanup calls
In reply to Ravi kumar Veeramally--- android/pan.c | 3 +++ 1 file changed, 3 insertions(+)
android/pan.c +3 -0
@@ -128,6 +128,8 @@ static gboolean bnep_watchdog_cb(GIOChannel *chan, GIOCondition cond,DBG ("%s disconnected" , dev->iface); +bnep_if_down (dev->iface); +bnep_conndel (&dev->dst);bt_pan_notify_conn_state (dev,HAL_PAN_STATE_DISCONNECTED );pan_device_free (dev);
@@ -142,6 +144,7 @@ static void bnep_conn_cb(GIOChannel *chan, char *iface, int err, void *data)if (err <0 ) {error ("bnep connect req failed: %s" ,strerror (-err)); +bnep_conndel (&dev->dst);bt_pan_notify_conn_state (dev,HAL_PAN_STATE_DISCONNECTED );pan_device_free (dev);return ;
Signature · 1 line
1.8.3.2
PATCH 8/10 android/pan: Fix minor white space
In reply to Ravi kumar Veeramally--- android/pan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
android/pan.c +1 -1
@@ -257,7 +257,7 @@ static void bt_pan_connect(const void *buf, uint16_t len) devices =g_slist_append (devices, dev);bt_pan_notify_conn_state (dev,HAL_PAN_STATE_CONNECTING ); - status =HAL_STATUS_SUCCESS ; + status =HAL_STATUS_SUCCESS ; failed:ipc_send_rsp (HAL_SERVICE_ID_PAN ,HAL_OP_PAN_CONNECT , status);
Signature · 1 line
1.8.3.2
PATCH 9/10 android/pan: Free connected pan devices on profile unregister call
In reply to Ravi kumar Veeramally--- android/pan.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
android/pan.c +10 -0
@@ -361,10 +361,20 @@ bool bt_pan_register(const bdaddr_t *addr)return true; } +static void free_pan_devices (gpointer data,gpointer user_data) +{ +struct pan_device *dev = data; + +bt_pan_notify_conn_state (dev,HAL_PAN_STATE_DISCONNECTED ); +pan_device_free (dev); +} +void bt_pan_unregister (void ) {DBG ("" ); +g_slist_foreach (devices, free_pan_devices,NULL ); + devices =NULL ;bnep_cleanup ();ipc_unregister (HAL_SERVICE_ID_PAN );
Signature · 1 line
1.8.3.2
PATCH 10/10 android/pan: Add PAN NAP sdp record fo server role
In reply to Ravi kumar VeeramallyAdding PAN profile NAP SDP record on profile register call and remove on unregister call.
--- android/pan.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
android/pan.c +21 -0
@@ -44,9 +44,12 @@#include "utils.h" #include "bluetooth.h" +#define SVC_HINT_CAPTURING 0x08 +static bdaddr_t adapter_addr;GSList *devices =NULL ;uint8_t local_role =HAL_PAN_ROLE_NONE ; +static uint32_t record_id =0 ;struct pan_device {char iface[16 ];
@@ -343,6 +346,7 @@ static const struct ipc_handler cmd_handlers[] = {bool bt_pan_register (const bdaddr_t *addr) { +sdp_record_t *rec =NULL ;int err;DBG ("" );
@@ -355,6 +359,21 @@ bool bt_pan_register(const bdaddr_t *addr)return false; } + rec =pan_record ("bnepnap" ,BNEP_SVC_NAP , TRUE); +if (!rec) { +bnep_cleanup (); +return false; + } + +if (bt_adapter_add_record (rec,SVC_HINT_CAPTURING ) <0 ) { +error ("Failed to register on PAN record" ); +sdp_record_free (rec); +bnep_cleanup (); +return false; + } + + record_id = rec->handle; +ipc_register (HAL_SERVICE_ID_PAN , cmd_handlers,G_N_ELEMENTS (cmd_handlers));
@@ -373,6 +392,8 @@ void bt_pan_unregister(void) {DBG ("" ); +bt_adapter_remove_record (record_id); +g_slist_foreach (devices, free_pan_devices,NULL ); devices =NULL ;bnep_cleanup ();
Signature · 1 line
1.8.3.2
Hi Ravi,
Quoted 6 lines · Ravi kumar Veeramally
bnep: Rename
bnep_kill_connectiontobnep_conndel
bnep: Rename sendctrl_rspand make it global
bnep: Move bnep related calls to bnep.h|c profiles/network/server: Delete function which does nothing android/pan: Fix missing cleanup calls android/pan: Fix minor white space
The above patches have been applied. As we discussed offline, the others were either dropped or need some extra work
Johan