Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(network): add NCM connect/disconnect status #2254

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/class/net/ncm_device.c
Expand Up @@ -102,6 +102,11 @@ struct ecm_notify_struct
uint32_t downlink, uplink;
};

enum {
NETWORK_DISCONNECT = 0,
NETWORK_CONNECTED
} network_state_t;

typedef struct
{
uint8_t itf_num; // Index number of Management Interface, +1 for Data Interface
Expand Down Expand Up @@ -158,6 +163,11 @@ CFG_TUD_MEM_SECTION CFG_TUSB_MEM_ALIGN tu_static uint8_t receive_ntb[CFG_TUD_NCM

tu_static ncm_interface_t ncm_interface;

static void ncm_report(void);

// default network state is connected, in case not actively call tud_network_connect
static uint8_t network_state = NETWORK_CONNECTED;

/*
* Set up the NTB state in ncm_interface to be ready to add datagrams.
*/
Expand Down Expand Up @@ -248,6 +258,42 @@ void tud_network_recv_renew(void)
tud_network_recv_cb(receive_ntb + ndp->datagram[i].wDatagramIndex, ndp->datagram[i].wDatagramLength);
}

bool tud_network_disconnect(void)
{
// already disconnected
if (network_state == NETWORK_DISCONNECT) {
return true;
}

if (!ncm_interface.report_pending) {
ncm_interface.report_state = REPORT_CONNECTED;
network_state = NETWORK_DISCONNECT;
ncm_report();
} else {
// busy...
return false;
}
return true;
}

bool tud_network_connect(void)
{
// already connected
if (network_state == NETWORK_CONNECTED) {
return true;
}

if (!ncm_interface.report_pending) {
ncm_interface.report_state = REPORT_CONNECTED;
network_state = NETWORK_CONNECTED;
ncm_report();
return true;
} else {
// busy...
return false;
}
}

//--------------------------------------------------------------------+
// USBD Driver API
//--------------------------------------------------------------------+
Expand Down Expand Up @@ -331,6 +377,7 @@ static void ncm_report(void)
ncm_interface.report_pending = true;
} else if (ncm_interface.report_state == REPORT_CONNECTED) {
ncm_notify_connected.header.wIndex = ncm_interface.itf_num;
ncm_notify_connected.header.wValue = network_state;
usbd_edpt_xfer(rhport, ncm_interface.ep_notif, (uint8_t *) &ncm_notify_connected, sizeof(ncm_notify_connected));
ncm_interface.report_state = REPORT_DONE;
ncm_interface.report_pending = true;
Expand Down
6 changes: 6 additions & 0 deletions src/class/net/net_device.h
Expand Up @@ -101,6 +101,12 @@ extern uint8_t tud_network_mac_address[6];
// callback to client providing optional indication of internal state of network driver
void tud_network_link_state_cb(bool state);

// indicate to network disconnect, only work in NCM
bool tud_network_disconnect(void);

// indicate to network connect, only work in NCM
bool tud_network_connect(void);

//--------------------------------------------------------------------+
// INTERNAL USBD-CLASS DRIVER API
//--------------------------------------------------------------------+
Expand Down