AVRCP: use a vtable to simplify PDU parsing/handling
Subsystems (2)
Luiz Augusto von Dentz
maintainerPATCH v2 1/5 AVRCP: use a vtable to simplify PDU parsing/handling
From: Luiz Augusto von Dentz <[email protected]>
This simplify a bit the handling by introducing common checks before calling the handler callback, it is also much easier to add/remove new PDUs in this way.
--- audio/control.c | 321 +++++++++++++++++++++---------------------------------- 1 files changed, 124 insertions(+), 197 deletions(-)
audio/control.c +124 -197
@@ -986,8 +986,9 @@ static void mp_set_media_attributes(struct control *control,avctp_send_event (control,AVRCP_EVENT_TRACK_CHANGED ,NULL ); } -static int avrcp_handle_get_capabilities (struct control *control, -struct avrcp_spec_avc_pdu *pdu) +static uint8_t avrcp_handle_get_capabilities (struct control *control, +struct avrcp_spec_avc_pdu *pdu, +uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);unsigned int i;
@@ -1008,31 +1009,35 @@ static int avrcp_handle_get_capabilities(struct control *control, pdu->params_len =htons (2 + (3 *G_N_ELEMENTS (company_ids))); pdu->params[1 ] =G_N_ELEMENTS (company_ids); -return 2 + (3 *G_N_ELEMENTS (company_ids)); +return CTYPE_STABLE ;case CAP_EVENTS_SUPPORTED : pdu->params_len =htons (4 ); pdu->params[1 ] =2 ; pdu->params[2 ] =AVRCP_EVENT_PLAYBACK_STATUS_CHANGED ; pdu->params[3 ] =AVRCP_EVENT_TRACK_CHANGED ; -return 4 ; +return CTYPE_STABLE ; } err: + pdu->params_len =htons (1 ); pdu->params[0 ] =E_INVALID_PARAM ; -return -EINVAL ; + +return CTYPE_REJECTED ; } -static int avrcp_handle_list_player_attributes (struct control *control, -struct avrcp_spec_avc_pdu *pdu) +static uint8_t avrcp_handle_list_player_attributes (struct control *control, +struct avrcp_spec_avc_pdu *pdu, +uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);struct media_player *mp = control->mp;unsigned int i;if (len !=0 ) { + pdu->params_len =htons (1 ); pdu->params[0 ] =E_INVALID_PARAM ; -return -EINVAL ; +return CTYPE_REJECTED ; }if (!mp)
@@ -1052,11 +1057,12 @@ done: pdu->params[0 ] = len; pdu->params_len =htons (len +1 ); -return len +1 ; +return CTYPE_STABLE ; } -static int avrcp_handle_list_player_values (struct control *control, -struct avrcp_spec_avc_pdu *pdu) +static uint8_t avrcp_handle_list_player_values (struct control *control, +struct avrcp_spec_avc_pdu *pdu, +uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);struct media_player *mp = control->mp;
@@ -1077,15 +1083,17 @@ static int avrcp_handle_list_player_values(struct control *control, pdu->params[0 ] = len; pdu->params_len =htons (len +1 ); -return len +1 ; +return CTYPE_STABLE ; err: + pdu->params_len =htons (1 ); pdu->params[0 ] =E_INVALID_PARAM ; -return -EINVAL ; +return CTYPE_REJECTED ; } -static int avrcp_handle_get_element_attributes (struct control *control, -struct avrcp_spec_avc_pdu *pdu) +static uint8_t avrcp_handle_get_element_attributes (struct control *control, +struct avrcp_spec_avc_pdu *pdu, +uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);uint64_t *identifier = (void *) &pdu->params[0 ];
@@ -1145,14 +1153,16 @@ done: pdu->params[0 ] = len; pdu->params_len =htons (pos); -return pos; +return CTYPE_STABLE ; err: + pdu->params_len =htons (1 ); pdu->params[0 ] =E_INVALID_PARAM ; -return -EINVAL ; +return CTYPE_REJECTED ; } -static int avrcp_handle_get_current_player_value (struct control *control, -struct avrcp_spec_avc_pdu *pdu) +static uint8_t avrcp_handle_get_current_player_value (struct control *control, +struct avrcp_spec_avc_pdu *pdu, +uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);struct media_player *mp = control->mp;
@@ -1202,19 +1212,21 @@ static int avrcp_handle_get_current_player_value(struct control *control, pdu->params[0 ] = len; pdu->params_len =htons (2 * len +1 ); -return 2 * len +1 ; +return CTYPE_STABLE ; }error ("No valid attributes in request" ); err: + pdu->params_len =htons (1 ); pdu->params[0 ] =E_INVALID_PARAM ; -return -EINVAL ; +return CTYPE_REJECTED ; } -static int avrcp_handle_set_player_value (struct control *control, -struct avrcp_spec_avc_pdu *pdu) +static uint8_t avrcp_handle_set_player_value (struct control *control, +struct avrcp_spec_avc_pdu *pdu, +uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);unsigned int i;
@@ -1256,16 +1268,38 @@ static int avrcp_handle_set_player_value(struct control *control,if (len) { pdu->params_len =0 ; -return 0 ; +return CTYPE_STABLE ; } err: + pdu->params_len =htons (1 ); pdu->params[0 ] =E_INVALID_PARAM ; -return -EINVAL ; +return CTYPE_REJECTED ; } -static int avrcp_handle_ct_battery_status (struct control *control, -struct avrcp_spec_avc_pdu *pdu) +static uint8_t avrcp_handle_displayable_charset (struct control *control, +struct avrcp_spec_avc_pdu *pdu, +uint8_t transaction) +{ +uint16_t len =ntohs (pdu->params_len); + +if (len <3 ) { + pdu->params_len =htons (1 ); + pdu->params[0 ] =E_INVALID_PARAM ; +return CTYPE_REJECTED ; + } + +/* +* We acknowledge the commands, but we always use UTF-8 for +* encoding since CT is obliged to support it. +*/ + pdu->params_len =0 ; +return CTYPE_STABLE ; +} + +static uint8_t avrcp_handle_ct_battery_status (struct control *control, +struct avrcp_spec_avc_pdu *pdu, +uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);const char *valstr;
@@ -1282,15 +1316,17 @@ static int avrcp_handle_ct_battery_status(struct control *control, DBUS_TYPE_STRING, &valstr); pdu->params_len =0 ; -return 0 ; +return CTYPE_STABLE ; err: + pdu->params_len =htons (1 ); pdu->params[0 ] =E_INVALID_PARAM ; -return -EINVAL ; +return CTYPE_REJECTED ; } -static int avrcp_handle_get_play_status (struct control *control, -struct avrcp_spec_avc_pdu *pdu) +static uint8_t avrcp_handle_get_play_status (struct control *control, +struct avrcp_spec_avc_pdu *pdu, +uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);uint32_t elapsed;
@@ -1298,8 +1334,9 @@ static int avrcp_handle_get_play_status(struct control *control,uint8_t status;if (len !=0 ) { + pdu->params_len =htons (1 ); pdu->params[0 ] =E_INVALID_PARAM ; -return -EINVAL ; +return CTYPE_REJECTED ; }if (control->mp) {
@@ -1319,10 +1356,10 @@ static int avrcp_handle_get_play_status(struct control *control, pdu->params_len =htons (9 ); -return 9 ; +return CTYPE_STABLE ; } -static int avrcp_handle_register_notification (struct control *control, +static uint8_t avrcp_handle_register_notification (struct control *control,struct avrcp_spec_avc_pdu *pdu,uint8_t transaction) {
@@ -1369,24 +1406,59 @@ static int avrcp_handle_register_notification(struct control *control, pdu->params_len =htons (len); -return len; +return CTYPE_INTERIM ; err: + pdu->params_len =htons (1 ); pdu->params[0 ] =E_INVALID_PARAM ; -return -EINVAL ; +return CTYPE_REJECTED ; } +static struct pdu_handler { +uint8_t pdu_id; +uint8_t code; +uint8_t (*func) (struct control *control, +struct avrcp_spec_avc_pdu *pdu, +uint8_t transaction); +} handlers[] = { + {AVRCP_GET_CAPABILITIES ,CTYPE_STATUS , + avrcp_handle_get_capabilities }, + {AVRCP_LIST_PLAYER_ATTRIBUTES ,CTYPE_STATUS , + avrcp_handle_list_player_attributes }, + {AVRCP_LIST_PLAYER_VALUES ,CTYPE_STATUS , + avrcp_handle_list_player_values }, + {AVRCP_GET_ELEMENT_ATTRIBUTES ,CTYPE_STATUS , + avrcp_handle_get_element_attributes }, + {AVRCP_GET_CURRENT_PLAYER_VALUE ,CTYPE_STATUS , + avrcp_handle_get_current_player_value }, + {AVRCP_SET_PLAYER_VALUE ,CTYPE_CONTROL , + avrcp_handle_set_player_value }, + {AVRCP_GET_PLAYER_ATTRIBUTE_TEXT ,CTYPE_STATUS , +NULL }, + {AVRCP_GET_PLAYER_VALUE_TEXT ,CTYPE_STATUS , +NULL }, + {AVRCP_DISPLAYABLE_CHARSET ,CTYPE_STATUS , + avrcp_handle_displayable_charset }, + {AVRCP_CT_BATTERY_STATUS ,CTYPE_STATUS , + avrcp_handle_ct_battery_status }, + {AVRCP_GET_PLAY_STATUS ,CTYPE_STATUS , + avrcp_handle_get_play_status }, + {AVRCP_REGISTER_NOTIFICATION ,CTYPE_NOTIFY , + avrcp_handle_register_notification }, + { }, +}; +/* handle vendordep pdu inside an avctp packet */ static int handle_vendordep_pdu (struct control *control,struct avctp_header *avctp,struct avrcp_header *avrcp,int operand_count) { +struct pdu_handler *handler;struct avrcp_spec_avc_pdu *pdu = (void *) avrcp +AVRCP_HEADER_LENGTH ;uint32_t company_id = (pdu->company_id[0 ] <<16 ) | (pdu->company_id[1 ] <<8 ) | (pdu->company_id[2 ]); -int len;if (company_id !=IEEEID_BTSIG || pdu->packet_type !=AVCTP_PACKET_SINGLE ) {
@@ -1397,177 +1469,32 @@ static int handle_vendordep_pdu(struct control *control, pdu->packet_type =0 ; pdu->rsvd =0 ; -if (operand_count +3 <AVRCP_SPECAVCPDU_HEADER_LENGTH ) { - pdu->params[0 ] =E_INVALID_COMMAND ; +if (operand_count +3 <AVRCP_SPECAVCPDU_HEADER_LENGTH ) goto err_metadata; - } - -switch (pdu->pdu_id) { -case AVRCP_GET_CAPABILITIES : -if (avrcp->code !=CTYPE_STATUS ) { - pdu->params[0 ] =E_INVALID_COMMAND ; - goto err_metadata; - } - - len =avrcp_handle_get_capabilities (control, pdu); -if (len <0 ) - goto err_metadata; - - avrcp->code =CTYPE_STABLE ; - -break ; -case AVRCP_LIST_PLAYER_ATTRIBUTES : -if (avrcp->code !=CTYPE_STATUS ) { - pdu->params[0 ] =E_INVALID_COMMAND ; - goto err_metadata; - } - len =avrcp_handle_list_player_attributes (control, pdu); -if (len <0 ) - goto err_metadata; - - avrcp->code =CTYPE_STABLE ; - -break ; -case AVRCP_LIST_PLAYER_VALUES : -if (avrcp->code !=CTYPE_STATUS ) { - pdu->params[0 ] =E_INVALID_COMMAND ; - goto err_metadata; - } - - len =avrcp_handle_list_player_values (control, pdu); -if (len <0 ) - goto err_metadata; - - avrcp->code =CTYPE_STABLE ; - -break ; -case AVRCP_GET_ELEMENT_ATTRIBUTES : -if (avrcp->code !=CTYPE_STATUS ) { - pdu->params[0 ] =E_INVALID_COMMAND ; - goto err_metadata; - } - - len =avrcp_handle_get_element_attributes (control, pdu); -if (len <0 ) - goto err_metadata; - - avrcp->code =CTYPE_STABLE ; - -break ; -case AVRCP_GET_CURRENT_PLAYER_VALUE : -if (avrcp->code !=CTYPE_STATUS ) { - pdu->params[0 ] =E_INVALID_COMMAND ; - goto err_metadata; - } - - len =avrcp_handle_get_current_player_value (control, pdu); -if (len <0 ) - goto err_metadata; - - avrcp->code =CTYPE_STABLE ; - -break ; -case AVRCP_SET_PLAYER_VALUE : -if (avrcp->code !=CTYPE_CONTROL ) { - pdu->params[0 ] =E_INVALID_COMMAND ; - goto err_metadata; - } - - len =avrcp_handle_set_player_value (control, pdu); -if (len <0 ) - goto err_metadata; - - avrcp->code =CTYPE_STABLE ; - -break ; -case AVRCP_GET_PLAYER_ATTRIBUTE_TEXT : -case AVRCP_GET_PLAYER_VALUE_TEXT : -if (avrcp->code !=CTYPE_STATUS ) { - pdu->params[0 ] =E_INVALID_COMMAND ; - goto err_metadata; - } +for (handler = handlers; handler; handler++) { +if (handler->pdu_id == pdu->pdu_id) +break ; + } -/* -* As per sec. 5.2.5 of AVRCP 1.3 spec, this command is -* expected to be used only for extended attributes, i.e. -* custom attributes defined by the application. As we -* currently don't have any such attribute, we respond with -* invalid param id. -*/ - pdu->params[0 ] =E_INVALID_PARAM ; +if (!handler || handler->code != avrcp->code) { + pdu->params[0 ] =E_INVALID_COMMAND ; goto err_metadata; -case AVRCP_DISPLAYABLE_CHARSET : -if (avrcp->code !=CTYPE_STATUS ) { - pdu->params[0 ] =E_INVALID_COMMAND ; - goto err_metadata; - } - -if (pdu->params[0 ] <3 ) { - pdu->params[0 ] =E_INVALID_PARAM ; - goto err_metadata; - } - -/* -* We acknowledge the commands, but we always use UTF-8 for -* encoding since CT is obliged to support it. -*/ - pdu->params_len =0 ; - avrcp->code =CTYPE_STABLE ; - len =0 ; - -break ; -case AVRCP_CT_BATTERY_STATUS : -if (avrcp->code !=CTYPE_STATUS ) { - pdu->params[0 ] =E_INVALID_COMMAND ; - goto err_metadata; - } - - len =avrcp_handle_ct_battery_status (control, pdu); -if (len <0 ) - goto err_metadata; - - avrcp->code =CTYPE_STABLE ; - -break ; -case AVRCP_GET_PLAY_STATUS : -if (avrcp->code !=CTYPE_STATUS ) { - pdu->params[0 ] =E_INVALID_COMMAND ; - goto err_metadata; - } - - len =avrcp_handle_get_play_status (control, pdu); -if (len <0 ) - goto err_metadata; - - avrcp->code =CTYPE_STABLE ; - -break ; -case AVRCP_REGISTER_NOTIFICATION : -if (avrcp->code !=CTYPE_NOTIFY ) { - pdu->params[0 ] =E_INVALID_COMMAND ; - goto err_metadata; - } - - len =avrcp_handle_register_notification (control, pdu, - avctp->transaction); -if (len <0 ) - goto err_metadata; - - avrcp->code =CTYPE_INTERIM ; + } -break ; -default : -/* Invalid pdu_id */ - pdu->params[0 ] =E_INVALID_COMMAND ; +if (!handler->func) { + pdu->params[0 ] =E_INVALID_PARAM ; goto err_metadata; } -return AVRCP_HEADER_LENGTH +AVRCP_SPECAVCPDU_HEADER_LENGTH + len; + avrcp->code = handler->func (control, pdu, avctp->transaction); + +return AVRCP_HEADER_LENGTH +AVRCP_SPECAVCPDU_HEADER_LENGTH + +ntohs (pdu->params_len); err_metadata: - avrcp->code =CTYPE_REJECTED ; pdu->params_len =htons (1 ); + avrcp->code =CTYPE_REJECTED ;return AVRCP_HEADER_LENGTH +AVRCP_SPECAVCPDU_HEADER_LENGTH +1 ; }
Signature · 1 line
1.7.6.1
Luiz Augusto von Dentz
maintainerPATCH v2 2/5 AVRCP: rename avrcp_header to avc_header
From: Luiz Augusto von Dentz <[email protected]>
AVCTP carries AV/C packets/PDUs not AVRCP as avrcp_header suggests.
--- audio/control.c | 96 +++++++++++++++++++++++++++--------------------------- 1 files changed, 48 insertions(+), 48 deletions(-)
audio/control.c +48 -48
@@ -212,14 +212,14 @@ struct avctp_header { } __attribute__ ((packed));#define AVCTP_HEADER_LENGTH 3 -struct avrcp_header { +struct avc_header {uint8_t code:4 ;uint8_t _hdr0:4 ;uint8_t subunit_id:3 ;uint8_t subunit_type:5 ;uint8_t opcode; } __attribute__ ((packed)); -#define AVRCP_HEADER_LENGTH 3 +#define AVC_HEADER_LENGTH 3struct avrcp_spec_avc_pdu {uint8_t company_id[3 ];
@@ -242,14 +242,14 @@ struct avctp_header { } __attribute__ ((packed));#define AVCTP_HEADER_LENGTH 3 -struct avrcp_header { +struct avc_header {uint8_t _hdr0:4 ;uint8_t code:4 ;uint8_t subunit_type:5 ;uint8_t subunit_id:3 ;uint8_t opcode; } __attribute__ ((packed)); -#define AVRCP_HEADER_LENGTH 3 +#define AVC_HEADER_LENGTH 3struct avrcp_spec_avc_pdu {uint8_t company_id[3 ];
@@ -721,12 +721,12 @@ static const char *battery_status_to_str(enum battery_status status)static int avctp_send_event (struct control *control,uint8_t id,void *data) { -uint8_t buf[AVCTP_HEADER_LENGTH +AVRCP_HEADER_LENGTH + +uint8_t buf[AVCTP_HEADER_LENGTH +AVC_HEADER_LENGTH +AVRCP_SPECAVCPDU_HEADER_LENGTH +9 ];struct avctp_header *avctp = (void *) buf; -struct avrcp_header *avrcp = (void *) &buf[AVCTP_HEADER_LENGTH ]; +struct avc_header *avc = (void *) &buf[AVCTP_HEADER_LENGTH ];struct avrcp_spec_avc_pdu *pdu = (void *) &buf[AVCTP_HEADER_LENGTH + -AVRCP_HEADER_LENGTH ]; +AVC_HEADER_LENGTH ];int sk;uint16_t size;
@@ -743,9 +743,9 @@ static int avctp_send_event(struct control *control, uint8_t id, void *data) avctp->cr =AVCTP_RESPONSE ; avctp->pid =htons (AV_REMOTE_SVCLASS_ID ); - avrcp->code =CTYPE_CHANGED ; - avrcp->subunit_type =SUBUNIT_PANEL ; - avrcp->opcode =OP_VENDORDEP ; + avc->code =CTYPE_CHANGED ; + avc->subunit_type =SUBUNIT_PANEL ; + avc->opcode =OP_VENDORDEP ; pdu->company_id[0 ] =IEEEID_BTSIG >>16 ; pdu->company_id[1 ] = (IEEEID_BTSIG >>8 ) &0xFF ;
@@ -780,7 +780,7 @@ static int avctp_send_event(struct control *control, uint8_t id, void *data) } pdu->params_len =htons (size); - size +=AVCTP_HEADER_LENGTH +AVRCP_HEADER_LENGTH + + size +=AVCTP_HEADER_LENGTH +AVC_HEADER_LENGTH +AVRCP_SPECAVCPDU_HEADER_LENGTH ; sk =g_io_channel_unix_get_fd (control->io);
@@ -1451,19 +1451,19 @@ static struct pdu_handler {/* handle vendordep pdu inside an avctp packet */ static int handle_vendordep_pdu (struct control *control,struct avctp_header *avctp, -struct avrcp_header *avrcp, +struct avc_header *avc,int operand_count) {struct pdu_handler *handler; -struct avrcp_spec_avc_pdu *pdu = (void *) avrcp +AVRCP_HEADER_LENGTH ; +struct avrcp_spec_avc_pdu *pdu = (void *) avc +AVC_HEADER_LENGTH ;uint32_t company_id = (pdu->company_id[0 ] <<16 ) | (pdu->company_id[1 ] <<8 ) | (pdu->company_id[2 ]);if (company_id !=IEEEID_BTSIG || pdu->packet_type !=AVCTP_PACKET_SINGLE ) { - avrcp->code =CTYPE_NOT_IMPLEMENTED ; -return AVRCP_HEADER_LENGTH ; + avc->code =CTYPE_NOT_IMPLEMENTED ; +return AVC_HEADER_LENGTH ; } pdu->packet_type =0 ;
@@ -1477,7 +1477,7 @@ static int handle_vendordep_pdu(struct control *control,break ; } -if (!handler || handler->code != avrcp->code) { +if (!handler || handler->code != avc->code) { pdu->params[0 ] =E_INVALID_COMMAND ; goto err_metadata; }
@@ -1487,16 +1487,16 @@ static int handle_vendordep_pdu(struct control *control, goto err_metadata; } - avrcp->code = handler->func (control, pdu, avctp->transaction); + avc->code = handler->func (control, pdu, avctp->transaction); -return AVRCP_HEADER_LENGTH +AVRCP_SPECAVCPDU_HEADER_LENGTH + +return AVC_HEADER_LENGTH +AVRCP_SPECAVCPDU_HEADER_LENGTH +ntohs (pdu->params_len); err_metadata: pdu->params_len =htons (1 ); - avrcp->code =CTYPE_REJECTED ; + avc->code =CTYPE_REJECTED ; -return AVRCP_HEADER_LENGTH +AVRCP_SPECAVCPDU_HEADER_LENGTH +1 ; +return AVC_HEADER_LENGTH +AVRCP_SPECAVCPDU_HEADER_LENGTH +1 ; }static void avctp_disconnected (struct audio_device *dev)
@@ -1593,7 +1593,7 @@ static gboolean control_cb(GIOChannel *chan, GIOCondition cond,struct control *control = data;unsigned char buf[1024 ], *operands;struct avctp_header *avctp; -struct avrcp_header *avrcp; +struct avc_header *avc;int ret, packet_size, operand_count, sock;if (cond & (G_IO_ERR |G_IO_HUP |G_IO_NVAL ))
@@ -1622,65 +1622,65 @@ static gboolean control_cb(GIOChannel *chan, GIOCondition cond, avctp->cr, avctp->ipid, ntohs(avctp->pid)); ret -=sizeof (struct avctp_header ); -if ((unsigned int ) ret <sizeof (struct avrcp_header )) { +if ((unsigned int ) ret <sizeof (struct avc_header )) {error ("Too small AVRCP packet" ); goto failed; } - avrcp = (struct avrcp_header *) (buf +sizeof (struct avctp_header )); + avc = (struct avc_header *) (buf +sizeof (struct avctp_header )); - ret -=sizeof (struct avrcp_header ); + ret -=sizeof (struct avc_header ); - operands = buf +sizeof (struct avctp_header ) +sizeof (struct avrcp_header ); + operands = buf +sizeof (struct avctp_header ) +sizeof (struct avc_header ); operand_count = ret; -DBG ("AVRCP %s 0x%01X, subunit_type 0x%02X, subunit_id 0x%01X, " +DBG ("AV/C %s 0x%01X, subunit_type 0x%02X, subunit_id 0x%01X, " "opcode 0x%02X, %d operands" , avctp->cr ?"response" :"command" , - avrcp->code, avrcp->subunit_type, avrcp->subunit_id, - avrcp->opcode, operand_count); + avc->code, avc->subunit_type, avc->subunit_id, + avc->opcode, operand_count);if (avctp->packet_type !=AVCTP_PACKET_SINGLE ) { avctp->cr =AVCTP_RESPONSE ; - avrcp->code =CTYPE_NOT_IMPLEMENTED ; + avc->code =CTYPE_NOT_IMPLEMENTED ; }else if (avctp->pid !=htons (AV_REMOTE_SVCLASS_ID )) { avctp->ipid =1 ; avctp->cr =AVCTP_RESPONSE ; packet_size =sizeof (*avctp); }else if (avctp->cr ==AVCTP_COMMAND && - avrcp->code ==CTYPE_CONTROL && - avrcp->subunit_type ==SUBUNIT_PANEL && - avrcp->opcode ==OP_PASSTHROUGH ) { + avc->code ==CTYPE_CONTROL && + avc->subunit_type ==SUBUNIT_PANEL && + avc->opcode ==OP_PASSTHROUGH ) {handle_panel_passthrough (control, operands, operand_count); avctp->cr =AVCTP_RESPONSE ; - avrcp->code =CTYPE_ACCEPTED ; + avc->code =CTYPE_ACCEPTED ; }else if (avctp->cr ==AVCTP_COMMAND && - avrcp->code ==CTYPE_STATUS && - (avrcp->opcode ==OP_UNITINFO - || avrcp->opcode ==OP_SUBUNITINFO )) { + avc->code ==CTYPE_STATUS && + (avc->opcode ==OP_UNITINFO + || avc->opcode ==OP_SUBUNITINFO )) { avctp->cr =AVCTP_RESPONSE ; - avrcp->code =CTYPE_STABLE ; + avc->code =CTYPE_STABLE ;/* The first operand should be 0x07 for the UNITINFO response. * Neither AVRCP (section 22.1, page 117) nor AVC Digital * Interface Command Set (section 9.2.1, page 45) specs * explain this value but both use it */ -if (operand_count >=1 && avrcp->opcode ==OP_UNITINFO ) +if (operand_count >=1 && avc->opcode ==OP_UNITINFO ) operands[0 ] =0x07 ;if (operand_count >=2 ) operands[1 ] =SUBUNIT_PANEL <<3 ; -DBG ("reply to %s" , avrcp->opcode ==OP_UNITINFO ? +DBG ("reply to %s" , avc->opcode ==OP_UNITINFO ?"OP_UNITINFO" :"OP_SUBUNITINFO" ); }else if (avctp->cr ==AVCTP_COMMAND && - avrcp->opcode ==OP_VENDORDEP ) { + avc->opcode ==OP_VENDORDEP ) {int r_size; operand_count -=3 ; avctp->cr =AVCTP_RESPONSE ; - r_size =handle_vendordep_pdu (control, avctp, avrcp, + r_size =handle_vendordep_pdu (control, avctp, avc, operand_count); packet_size =AVCTP_HEADER_LENGTH + r_size; }else { avctp->cr =AVCTP_RESPONSE ; - avrcp->code =CTYPE_REJECTED ; + avc->code =CTYPE_REJECTED ; } ret =write (sock, buf, packet_size);
@@ -2092,10 +2092,10 @@ static DBusMessage *control_is_connected(DBusConnection *conn,static int avctp_send_passthrough (struct control *control,uint8_t op) { -unsigned char buf[AVCTP_HEADER_LENGTH +AVRCP_HEADER_LENGTH +2 ]; +unsigned char buf[AVCTP_HEADER_LENGTH +AVC_HEADER_LENGTH +2 ];struct avctp_header *avctp = (void *) buf; -struct avrcp_header *avrcp = (void *) &buf[AVCTP_HEADER_LENGTH ]; -uint8_t *operands = &buf[AVCTP_HEADER_LENGTH +AVRCP_HEADER_LENGTH ]; +struct avc_header *avc = (void *) &buf[AVCTP_HEADER_LENGTH ]; +uint8_t *operands = &buf[AVCTP_HEADER_LENGTH +AVC_HEADER_LENGTH ];int sk =g_io_channel_unix_get_fd (control->io);static uint8_t transaction =0 ;
@@ -2106,9 +2106,9 @@ static int avctp_send_passthrough(struct control *control, uint8_t op) avctp->cr =AVCTP_COMMAND ; avctp->pid =htons (AV_REMOTE_SVCLASS_ID ); - avrcp->code =CTYPE_CONTROL ; - avrcp->subunit_type =SUBUNIT_PANEL ; - avrcp->opcode =OP_PASSTHROUGH ; + avc->code =CTYPE_CONTROL ; + avc->subunit_type =SUBUNIT_PANEL ; + avc->opcode =OP_PASSTHROUGH ; operands[0 ] = op &0x7f ; operands[1 ] =0 ;
Signature · 1 line
1.7.6.1
Luiz Augusto von Dentz
maintainerPATCH v2 3/5 AVRCP: rename avrcp_spec_avc_pdu to avrcp_header
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
Since old avrcp_header got renamed to avc_header now we can use avrcp_header for AVRCP PDUs.
--- audio/control.c | 44 ++++++++++++++++++++++---------------------- 1 files changed, 22 insertions(+), 22 deletions(-)
audio/control.c +22 -22
@@ -221,7 +221,7 @@ struct avc_header { } __attribute__ ((packed));#define AVC_HEADER_LENGTH 3 -struct avrcp_spec_avc_pdu { +struct avrcp_header {uint8_t company_id[3 ];uint8_t pdu_id;uint8_t packet_type:2 ;
@@ -229,7 +229,7 @@ struct avrcp_spec_avc_pdu {uint16_t params_len;uint8_t params[0 ]; } __attribute__ ((packed)); -#define AVRCP_SPECAVCPDU_HEADER_LENGTH 7 +#define AVRCP_HEADER_LENGTH 7#elif __BYTE_ORDER == __BIG_ENDIAN
@@ -251,7 +251,7 @@ struct avc_header { } __attribute__ ((packed));#define AVC_HEADER_LENGTH 3 -struct avrcp_spec_avc_pdu { +struct avrcp_header {uint8_t company_id[3 ];uint8_t pdu_id;uint8_t rsvd:6 ;
@@ -259,7 +259,7 @@ struct avrcp_spec_avc_pdu {uint16_t params_len;uint8_t params[0 ]; } __attribute__ ((packed)); -#define AVRCP_SPECAVCPDU_HEADER_LENGTH 7 +#define AVRCP_HEADER_LENGTH 7#else #error "Unknown byte order"
@@ -722,10 +722,10 @@ static const char *battery_status_to_str(enum battery_status status)static int avctp_send_event (struct control *control,uint8_t id,void *data) {uint8_t buf[AVCTP_HEADER_LENGTH +AVC_HEADER_LENGTH + -AVRCP_SPECAVCPDU_HEADER_LENGTH +9 ]; +AVRCP_HEADER_LENGTH +9 ];struct avctp_header *avctp = (void *) buf;struct avc_header *avc = (void *) &buf[AVCTP_HEADER_LENGTH ]; -struct avrcp_spec_avc_pdu *pdu = (void *) &buf[AVCTP_HEADER_LENGTH + +struct avrcp_header *pdu = (void *) &buf[AVCTP_HEADER_LENGTH +AVC_HEADER_LENGTH ];int sk;uint16_t size;
@@ -781,7 +781,7 @@ static int avctp_send_event(struct control *control, uint8_t id, void *data) pdu->params_len =htons (size); size +=AVCTP_HEADER_LENGTH +AVC_HEADER_LENGTH + -AVRCP_SPECAVCPDU_HEADER_LENGTH ; +AVRCP_HEADER_LENGTH ; sk =g_io_channel_unix_get_fd (control->io);
@@ -987,7 +987,7 @@ static void mp_set_media_attributes(struct control *control, }static uint8_t avrcp_handle_get_capabilities (struct control *control, -struct avrcp_spec_avc_pdu *pdu, +struct avrcp_header *pdu,uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);
@@ -1027,7 +1027,7 @@ err: }static uint8_t avrcp_handle_list_player_attributes (struct control *control, -struct avrcp_spec_avc_pdu *pdu, +struct avrcp_header *pdu,uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);
@@ -1061,7 +1061,7 @@ done: }static uint8_t avrcp_handle_list_player_values (struct control *control, -struct avrcp_spec_avc_pdu *pdu, +struct avrcp_header *pdu,uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);
@@ -1092,7 +1092,7 @@ err: }static uint8_t avrcp_handle_get_element_attributes (struct control *control, -struct avrcp_spec_avc_pdu *pdu, +struct avrcp_header *pdu,uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);
@@ -1161,7 +1161,7 @@ err: }static uint8_t avrcp_handle_get_current_player_value (struct control *control, -struct avrcp_spec_avc_pdu *pdu, +struct avrcp_header *pdu,uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);
@@ -1225,7 +1225,7 @@ err: }static uint8_t avrcp_handle_set_player_value (struct control *control, -struct avrcp_spec_avc_pdu *pdu, +struct avrcp_header *pdu,uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);
@@ -1278,7 +1278,7 @@ err: }static uint8_t avrcp_handle_displayable_charset (struct control *control, -struct avrcp_spec_avc_pdu *pdu, +struct avrcp_header *pdu,uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);
@@ -1298,7 +1298,7 @@ static uint8_t avrcp_handle_displayable_charset(struct control *control, }static uint8_t avrcp_handle_ct_battery_status (struct control *control, -struct avrcp_spec_avc_pdu *pdu, +struct avrcp_header *pdu,uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);
@@ -1325,7 +1325,7 @@ err: }static uint8_t avrcp_handle_get_play_status (struct control *control, -struct avrcp_spec_avc_pdu *pdu, +struct avrcp_header *pdu,uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);
@@ -1360,7 +1360,7 @@ static uint8_t avrcp_handle_get_play_status(struct control *control, }static uint8_t avrcp_handle_register_notification (struct control *control, -struct avrcp_spec_avc_pdu *pdu, +struct avrcp_header *pdu,uint8_t transaction) {uint16_t len =ntohs (pdu->params_len);
@@ -1418,7 +1418,7 @@ static struct pdu_handler {uint8_t pdu_id;uint8_t code;uint8_t (*func) (struct control *control, -struct avrcp_spec_avc_pdu *pdu, +struct avrcp_header *pdu,uint8_t transaction); } handlers[] = { { AVRCP_GET_CAPABILITIES, CTYPE_STATUS,
@@ -1455,7 +1455,7 @@ static int handle_vendordep_pdu(struct control *control, int operand_count) {struct pdu_handler *handler; -struct avrcp_spec_avc_pdu *pdu = (void *) avc +AVC_HEADER_LENGTH ; +struct avrcp_header *pdu = (void *) avc +AVC_HEADER_LENGTH ;uint32_t company_id = (pdu->company_id[0 ] <<16 ) | (pdu->company_id[1 ] <<8 ) | (pdu->company_id[2 ]);
@@ -1469,7 +1469,7 @@ static int handle_vendordep_pdu(struct control *control, pdu->packet_type =0 ; pdu->rsvd =0 ; -if (operand_count +3 <AVRCP_SPECAVCPDU_HEADER_LENGTH ) +if (operand_count +3 <AVRCP_HEADER_LENGTH ) goto err_metadata;for (handler = handlers; handler; handler++) {
@@ -1489,14 +1489,14 @@ static int handle_vendordep_pdu(struct control *control, avc->code = handler->func (control, pdu, avctp->transaction); -return AVC_HEADER_LENGTH +AVRCP_SPECAVCPDU_HEADER_LENGTH + +return AVC_HEADER_LENGTH +AVRCP_HEADER_LENGTH +ntohs (pdu->params_len); err_metadata: pdu->params_len =htons (1 ); avc->code =CTYPE_REJECTED ; -return AVC_HEADER_LENGTH +AVRCP_SPECAVCPDU_HEADER_LENGTH +1 ; +return AVC_HEADER_LENGTH +AVRCP_HEADER_LENGTH +1 ; }static void avctp_disconnected (struct audio_device *dev)
Signature · 1 line
1.7.6.1
Luiz Augusto von Dentz
maintainerPATCH v2 4/5 AVRCP: split AVCTP specific code from control.c
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
The code is moved to avctp.c to simplify control.c
--- Makefile.am | 1 + audio/avctp.c | 1030 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ audio/avctp.h | 95 +++++ audio/control.c | 875 +++++++---------------------------------------- audio/control.h | 14 - audio/device.c | 1 + 6 files changed, 1244 insertions(+), 772 deletions(-) create mode 100644 audio/avctp.c create mode 100644 audio/avctp.h
Makefile.am +1 -0
@@ -143,6 +143,7 @@ builtin_sources += audio/main.c \ audio/gateway.h audio/gateway.c \ audio/headset.h audio/headset.c \ audio/control.h audio/control.c \ + audio/avctp.h audio/avctp.c \ audio/device.h audio/device.c \ audio/source.h audio/source.c \ audio/sink.h audio/sink.c \
audio/avctp.c new file +1030 -0
1,030 lines not on this page · raw message
audio/avctp.h new file +95 -0
95 lines not on this page · raw message
audio/control.c +117 -758
1,308 lines not on this page · raw message
audio/control.h +0 -14
@@ -25,20 +25,6 @@#define AUDIO_CONTROL_INTERFACE "org.bluez.Control"#define MEDIA_PLAYER_INTERFACE "org.bluez.MediaPlayer" -typedef enum { -AVCTP_STATE_DISCONNECTED =0 , -AVCTP_STATE_CONNECTING , -AVCTP_STATE_CONNECTED -}avctp_state_t ; - -typedef void (*avctp_state_cb ) (struct audio_device *dev, -avctp_state_t old_state, -avctp_state_t new_state, -void *user_data); - -unsigned int avctp_add_state_cb (avctp_state_cb cb,void *user_data); -gboolean avctp_remove_state_cb (unsigned int id); -int avrcp_register (DBusConnection *conn,const bdaddr_t *src,GKeyFile *config);void avrcp_unregister (const bdaddr_t *src);
audio/device.c +1 -0
@@ -52,6 +52,7 @@#include "unix.h" #include "avdtp.h" #include "control.h" +#include "avctp.h" #include "headset.h" #include "gateway.h" #include "sink.h"
Signature · 1 line
1.7.6.1
Luiz Augusto von Dentz
maintainerPATCH v2 5/5 AVRCP: move handling of vendor dependent PDU from control.c to avrcp.c
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
This helps to isolate AVRCP PDU handling and MediaPlayer interface.
--- Makefile.am | 1 + audio/avctp.c | 5 + audio/avctp.h | 1 + audio/avrcp.c | 1661 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ audio/avrcp.h | 34 ++ audio/control.c | 1636 +------------------------------------------------------ audio/control.h | 13 +- audio/device.c | 4 + audio/device.h | 2 + audio/manager.c | 10 +- 10 files changed, 1720 insertions(+), 1647 deletions(-) create mode 100644 audio/avrcp.c create mode 100644 audio/avrcp.h
Makefile.am +1 -0
@@ -144,6 +144,7 @@ builtin_sources += audio/main.c \ audio/headset.h audio/headset.c \ audio/control.h audio/control.c \ audio/avctp.h audio/avctp.c \ + audio/avrcp.h audio/avrcp.c \ audio/device.h audio/device.c \ audio/source.h audio/source.c \ audio/sink.h audio/sink.c \
audio/avctp.c +5 -0
@@ -1028,3 +1028,8 @@ void avctp_disconnect(struct avctp *session)avctp_set_state (session,AVCTP_STATE_DISCONNECTED ); } + +struct avctp *avctp_get (const bdaddr_t *src,const bdaddr_t *dst) +{ +return avctp_get_internal (src, dst); +}
audio/avctp.h +1 -0
@@ -83,6 +83,7 @@ int avctp_register(const bdaddr_t *src, gboolean master);void avctp_unregister (const bdaddr_t *src);struct avctp *avctp_connect (const bdaddr_t *src,const bdaddr_t *dst); +struct avctp *avctp_get (const bdaddr_t *src,const bdaddr_t *dst);void avctp_disconnect (struct avctp *session); unsigned int avctp_register_pdu_handler(uint8_t opcode, avctp_pdu_cb cb,
audio/avrcp.c new file +1661 -0
1,661 lines not on this page · raw message
audio/avrcp.h new file +34 -0
@@ -0,0 +1,34 @@ +/* +* +* BlueZ - Bluetooth protocol stack for Linux +* +* Copyright (C) 2006-2010 Nokia Corporation +* Copyright (C) 2004-2010 Marcel Holtmann <[email protected]> +* +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +* +*/ + +#define MEDIA_PLAYER_INTERFACE "org.bluez.MediaPlayer" + +int avrcp_register (DBusConnection *conn,const bdaddr_t *src,GKeyFile *config); +void avrcp_unregister (const bdaddr_t *src); + +gboolean avrcp_connect (struct audio_device *dev); +void avrcp_disconnect (struct audio_device *dev); + +struct media_player *media_player_init (struct audio_device *dev); +void media_player_unregister (struct audio_device *dev);
audio/control.c +4 -1632
1,283 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
1.7.6.1
Part of the diff is not shown here. View the raw message.
Hi Luiz
Quoted 9 lines · Luiz Augusto von Dentz
From: Luiz Augusto von Dentz <[email protected]>
This simplify a bit the handling by introducing common checks before calling the handler callback, it is also much easier to add/remove new PDUs in this way.
---
audio/control.c | 321 +++++++++++++++++++++----------------------------------1 files changed, 124 insertions(+), 197 deletions(-)
Ack.
Lucas De Marchi
Re: [PATCH BlueZ 2/5 v2] AVRCP: rename avrcp_header to avc_header
In reply to Luiz Augusto von DentzHi Luiz,
Quoted 6 lines · Luiz Augusto von Dentz
From: Luiz Augusto von Dentz <[email protected]>
AVCTP carries AV/C packets/PDUs not AVRCP as
avrcp_headersuggests.
---
audio/control.c | 96 +++++++++++++++++++++++++++---------------------------1 files changed, 48 insertions(+), 48 deletions(-)
Ack
Lucas De Marchi
Luiz Augusto von Dentz
maintainerRe: [PATCH BlueZ 4/5 v2] AVRCP: split AVCTP specific code from control.c
In reply to a message not in the archiveHi David,
Quoted 2 lines · On Tue, Sep 13, 2011 at 6:46 PM, [email protected]…
However, I am concerned that you have also moved the basic AVRCP messages -- UNITINFO, SUBUNITINFO, and PASSTHROUGH --
Except they are not AVRCP at all, they are AV/C and AV/C panel as you can see in e.g Table 3.2: Application Layer Feature to Procedure Mapping. Now I could have created and avc.c to handle that but I felt that was too much to begin with. As for PASSTHROUGH only those define in Table 4.6: AVRCP Specific Vendor Unique PASS THROUGH command are AVRCP specific, they part of 1.4, but for that we don't need to pass the whole PDU.
Quoted 1 line
down into the AVCTP layer, which sort of messes things up again, IMO.
Hmm, so mixing AV/C with AVRCP is better than mixing with AV/C with AVCTP? I don't think so, there are clearly 2 level of function selection, first level defined by AV/C opcode (e.g. UNITINFO, SUBUNITINFO, PASSTHROUGH and VENDOR DEPENDENT) and the second by AVRCP pdu id.
Quoted 23 lines
Quoted 23 lines
+static
size_thandle_subunit_info(struct avctp *session, +uint8_ttransaction,uint8_t*code,
+uint8_t*subunit,uint8_t*operands,
+size_toperand_count, void*user_data)audio/avctp.c new file +16
-0+{ + if (*code != AVC_CTYPE_STATUS) { + *code = AVC_CTYPE_REJECTED; + return 0; + } + + *code = AVC_CTYPE_STABLE; + + /* The first operand should be 0x07 for the UNITINFO response. + * Neither AVRCP (section 22.1, page 117) nor AVC Digital + * Interface Command Set (section 9.2.1, page 45) specs + * explain this value but both use it */ + if (operand_count >= 2) + operands[1] = AVC_SUBUNIT_PANEL << 3; + + DBG("reply to AVC_OP_SUBUNITINFO");
Quoted 8 lines
audio/avctp.c line 202
Quoted 4 lines
audio/avctp.c new file +4
-0+ + return 0; +} +IMO, this
avctp_pdu_handlerbelongs at the AVRCP layer, not in AVCTP.
Well those headers are not defined on AVRCP either, are they?
Quoted 43 lines
Quoted 43 lines
+static void
avctp_set_state(struct avctp *session, avctp_state_t new_state)audio/avctp.c new file +6
-0+{ + GSList *l; + struct audio_device *dev; + avctp_state_t old_state = session->state; + + dev = manager_get_device(&session->server->src, &session->dst,FALSE);
+ if (dev == NULL) {
+error("avdtp_set_state(): no matching audio device"); + return;
+ }audio/avctp.c new file +30
-0+ + session->state = new_state; + + for (l = callbacks; l != NULL; l = l->next) { + struct avctp_state_callback *cb = l->data; + cb->cb(dev, old_state, new_state, cb->user_data); + } + + switch (new_state) { + case AVCTP_STATE_DISCONNECTED: + DBG("AVCTP Disconnected"); + + avctp_disconnected(session); + + if (old_state != AVCTP_STATE_CONNECTED) + break; + + if (!audio_device_is_active(dev, NULL)) + audio_device_set_authorized(dev, FALSE); + + break; + case AVCTP_STATE_CONNECTING: + DBG("AVCTP Connecting"); + break; + case AVCTP_STATE_CONNECTED: + DBG("AVCTP Connected"); + break; + default: + error("Invalid AVCTP state %d", new_state); + return;
Quoted 8 lines
Review
Quoted 3 lines
+ }
(quoted diff) +2
-0+} +In 1.3, passing AVC packets limited to 512 bytes, AVCTP-level fragmentation will be rare (how often does MTU drop below 672 bytes?).
Not sure how this has anything to do with the quoted code, but note that doing this type of checks is easier with AVCTP and AV/C in the same file.
Quoted 7 lines
However, in 1.4 with browsing, we will need to add AVCTP-packet reassembly for mass metadata received from the TG. This must not be confused with the AVRCP-layer Request Continuing calls, which are used to reassemble metadata sent on the control channel (Get Element Attributes) that exceeds the AVC max length.
Yep, but it is nothing it that can't be handled since the operands size is given to AVRCP layer.
Signature · 1 line
Luiz Augusto von Dentz
Hi Luiz,
Quoted 6 lines · Luiz Augusto von Dentz
This simplify a bit the handling by introducing common checks before calling the handler callback, it is also much easier to add/remove new PDUs in this way.
--- audio/control.c | 321 +++++++++++++++++++++---------------------------------- 1 files changed, 124 insertions(+), 197 deletions(-)
All five patches applied. Thanks.
Johan