Skip to content

Commit 9698a08

Browse files
committed
refactor acm function
1 parent 85d9925 commit 9698a08

File tree

1 file changed

+73
-65
lines changed

1 file changed

+73
-65
lines changed

src/class/cdc/cdc_host.c

Lines changed: 73 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -545,38 +545,42 @@ static void cdch_internal_control_complete(tuh_xfer_t* xfer)
545545
xfer->complete_cb(xfer);
546546
}
547547

548+
static bool acm_set_control_line_state(cdch_interface_t* p_cdc, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
549+
tusb_control_request_t const request = {
550+
.bmRequestType_bit = {
551+
.recipient = TUSB_REQ_RCPT_INTERFACE,
552+
.type = TUSB_REQ_TYPE_CLASS,
553+
.direction = TUSB_DIR_OUT
554+
},
555+
.bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE,
556+
.wValue = tu_htole16(line_state),
557+
.wIndex = tu_htole16((uint16_t) p_cdc->bInterfaceNumber),
558+
.wLength = 0
559+
};
560+
561+
p_cdc->user_control_cb = complete_cb;
562+
563+
tuh_xfer_t xfer = {
564+
.daddr = p_cdc->daddr,
565+
.ep_addr = 0,
566+
.setup = &request,
567+
.buffer = NULL,
568+
.complete_cb = cdch_internal_control_complete,
569+
.user_data = user_data
570+
};
571+
572+
TU_ASSERT(tuh_control_xfer(&xfer));
573+
return true;
574+
}
575+
548576
bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
549577
{
550578
cdch_interface_t* p_cdc = get_itf(idx);
551579
TU_VERIFY(p_cdc && support_line_request(p_cdc));
552-
553580
TU_LOG_CDCH("CDC Set Control Line State\r\n");
554581

555582
if(p_cdc->serial_protocol == SERIAL_PROTOCOL_ACM ) {
556-
tusb_control_request_t const request = {
557-
.bmRequestType_bit = {
558-
.recipient = TUSB_REQ_RCPT_INTERFACE,
559-
.type = TUSB_REQ_TYPE_CLASS,
560-
.direction = TUSB_DIR_OUT
561-
},
562-
.bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE,
563-
.wValue = tu_htole16(line_state),
564-
.wIndex = tu_htole16((uint16_t) p_cdc->bInterfaceNumber),
565-
.wLength = 0
566-
};
567-
568-
p_cdc->user_control_cb = complete_cb;
569-
tuh_xfer_t xfer = {
570-
.daddr = p_cdc->daddr,
571-
.ep_addr = 0,
572-
.setup = &request,
573-
.buffer = NULL,
574-
.complete_cb = cdch_internal_control_complete,
575-
.user_data = user_data
576-
};
577-
578-
TU_ASSERT(tuh_control_xfer(&xfer));
579-
return true;
583+
return acm_set_control_line_state(p_cdc, line_state, complete_cb, user_data);
580584
}
581585
#if CFG_TUH_CDC_FTDI
582586
else if (p_cdc->serial_protocol == SERIAL_PROTOCOL_FTDI) {
@@ -588,42 +592,45 @@ bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_c
588592
}
589593
}
590594

595+
bool acm_set_line_coding(cdch_interface_t* p_cdc, cdc_line_coding_t const* line_coding, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
596+
tusb_control_request_t const request = {
597+
.bmRequestType_bit = {
598+
.recipient = TUSB_REQ_RCPT_INTERFACE,
599+
.type = TUSB_REQ_TYPE_CLASS,
600+
.direction = TUSB_DIR_OUT
601+
},
602+
.bRequest = CDC_REQUEST_SET_LINE_CODING,
603+
.wValue = 0,
604+
.wIndex = tu_htole16(p_cdc->bInterfaceNumber),
605+
.wLength = tu_htole16(sizeof(cdc_line_coding_t))
606+
};
607+
608+
// use usbh enum buf to hold line coding since user line_coding variable does not live long enough
609+
uint8_t* enum_buf = usbh_get_enum_buf();
610+
memcpy(enum_buf, line_coding, sizeof(cdc_line_coding_t));
611+
612+
p_cdc->user_control_cb = complete_cb;
613+
tuh_xfer_t xfer = {
614+
.daddr = p_cdc->daddr,
615+
.ep_addr = 0,
616+
.setup = &request,
617+
.buffer = enum_buf,
618+
.complete_cb = cdch_internal_control_complete,
619+
.user_data = user_data
620+
};
621+
622+
TU_ASSERT(tuh_control_xfer(&xfer));
623+
return true;
624+
}
625+
591626
bool tuh_cdc_set_line_coding(uint8_t idx, cdc_line_coding_t const* line_coding, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
592627
{
593628
cdch_interface_t* p_cdc = get_itf(idx);
594629
TU_VERIFY(p_cdc && support_line_request(p_cdc));
595-
596630
TU_LOG_CDCH("CDC Set Line Conding\r\n");
597631

598632
if (p_cdc->serial_protocol == SERIAL_PROTOCOL_ACM) {
599-
tusb_control_request_t const request = {
600-
.bmRequestType_bit = {
601-
.recipient = TUSB_REQ_RCPT_INTERFACE,
602-
.type = TUSB_REQ_TYPE_CLASS,
603-
.direction = TUSB_DIR_OUT
604-
},
605-
.bRequest = CDC_REQUEST_SET_LINE_CODING,
606-
.wValue = 0,
607-
.wIndex = tu_htole16(p_cdc->bInterfaceNumber),
608-
.wLength = tu_htole16(sizeof(cdc_line_coding_t))
609-
};
610-
611-
// use usbh enum buf to hold line coding since user line_coding variable does not live long enough
612-
uint8_t* enum_buf = usbh_get_enum_buf();
613-
memcpy(enum_buf, line_coding, sizeof(cdc_line_coding_t));
614-
615-
p_cdc->user_control_cb = complete_cb;
616-
tuh_xfer_t xfer = {
617-
.daddr = p_cdc->daddr,
618-
.ep_addr = 0,
619-
.setup = &request,
620-
.buffer = enum_buf,
621-
.complete_cb = cdch_internal_control_complete,
622-
.user_data = user_data
623-
};
624-
625-
TU_ASSERT(tuh_control_xfer(&xfer));
626-
return true;
633+
return acm_set_line_coding(p_cdc, line_coding, complete_cb, user_data);
627634
}
628635
#if CFG_TUH_CDC_FTDI
629636
else if (p_cdc->serial_protocol == SERIAL_PROTOCOL_FTDI) {
@@ -733,9 +740,9 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
733740
enum
734741
{
735742
// ACM
736-
CONFIG_SET_CONTROL_LINE_STATE,
737-
CONFIG_SET_LINE_CODING,
738-
CONFIG_COMPLETE,
743+
CONFIG_ACM_SET_CONTROL_LINE_STATE,
744+
CONFIG_ACM_SET_LINE_CODING,
745+
CONFIG_ACM_COMPLETE,
739746
};
740747

741748
static bool open_ep_stream_pair(cdch_interface_t* p_cdc , tusb_desc_endpoint_t const *desc_ep)
@@ -864,7 +871,7 @@ static void set_config_complete(cdch_interface_t * p_cdc, uint8_t idx, uint8_t i
864871
usbh_driver_set_config_complete(p_cdc->daddr, itf_num);
865872
}
866873

867-
static void process_cdc_config(tuh_xfer_t* xfer)
874+
static void process_acm_config(tuh_xfer_t* xfer)
868875
{
869876
uintptr_t const state = xfer->user_data;
870877
uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
@@ -874,28 +881,29 @@ static void process_cdc_config(tuh_xfer_t* xfer)
874881

875882
switch(state)
876883
{
877-
case CONFIG_SET_CONTROL_LINE_STATE:
884+
case CONFIG_ACM_SET_CONTROL_LINE_STATE:
878885
#if CFG_TUH_CDC_LINE_CONTROL_ON_ENUM
879886
if (p_cdc->acm_capability.support_line_request)
880887
{
881-
TU_ASSERT( tuh_cdc_set_control_line_state(idx, CFG_TUH_CDC_LINE_CONTROL_ON_ENUM, process_cdc_config, CONFIG_SET_LINE_CODING), );
888+
TU_ASSERT(tuh_cdc_set_control_line_state(idx, CFG_TUH_CDC_LINE_CONTROL_ON_ENUM, process_acm_config,
889+
CONFIG_ACM_SET_LINE_CODING), );
882890
break;
883891
}
884892
#endif
885893
TU_ATTR_FALLTHROUGH;
886894

887-
case CONFIG_SET_LINE_CODING:
895+
case CONFIG_ACM_SET_LINE_CODING:
888896
#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
889897
if (p_cdc->acm_capability.support_line_request)
890898
{
891899
cdc_line_coding_t line_coding = CFG_TUH_CDC_LINE_CODING_ON_ENUM;
892-
TU_ASSERT( tuh_cdc_set_line_coding(idx, &line_coding, process_cdc_config, CONFIG_COMPLETE), );
900+
TU_ASSERT(tuh_cdc_set_line_coding(idx, &line_coding, process_acm_config, CONFIG_ACM_COMPLETE), );
893901
break;
894902
}
895903
#endif
896904
TU_ATTR_FALLTHROUGH;
897905

898-
case CONFIG_COMPLETE:
906+
case CONFIG_ACM_COMPLETE:
899907
// itf_num+1 to account for data interface as well
900908
set_config_complete(p_cdc, idx, itf_num+1);
901909
break;
@@ -921,8 +929,8 @@ bool cdch_set_config(uint8_t daddr, uint8_t itf_num)
921929

922930
switch (p_cdc->serial_protocol) {
923931
case SERIAL_PROTOCOL_ACM:
924-
xfer.user_data = CONFIG_SET_CONTROL_LINE_STATE;
925-
process_cdc_config(&xfer);
932+
xfer.user_data = CONFIG_ACM_SET_CONTROL_LINE_STATE;
933+
process_acm_config(&xfer);
926934
break;
927935

928936
#if CFG_TUH_CDC_FTDI

0 commit comments

Comments
 (0)