Skip to content

Commit

Permalink
Merge pull request #2763 from particle-iot/sc-126826/set-credentials-…
Browse files Browse the repository at this point in the history
…ctrl-req

Add set credentials usb request
  • Loading branch information
scott-brust authored May 28, 2024
2 parents 1c34048 + e0b3430 commit b3b92e3
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 2 deletions.
2 changes: 1 addition & 1 deletion proto_defs/shared
6 changes: 6 additions & 0 deletions proto_defs/src/control/wifi_new.pb.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ PB_BIND(particle_ctrl_wifi_ScanNetworksReply, particle_ctrl_wifi_ScanNetworksRep
PB_BIND(particle_ctrl_wifi_ScanNetworksReply_Network, particle_ctrl_wifi_ScanNetworksReply_Network, AUTO)


PB_BIND(particle_ctrl_wifi_SetNetworkCredentialsRequest, particle_ctrl_wifi_SetNetworkCredentialsRequest, 2)


PB_BIND(particle_ctrl_wifi_SetNetworkCredentialsReply, particle_ctrl_wifi_SetNetworkCredentialsReply, AUTO)





54 changes: 53 additions & 1 deletion proto_defs/src/control/wifi_new.pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,23 @@ typedef struct _particle_ctrl_wifi_RemoveKnownNetworkRequest {
pb_callback_t ssid;
} particle_ctrl_wifi_RemoveKnownNetworkRequest;

/* *
Store network credential but do not attempt to power on wifi or connect.
On success, the network credentials get saved to a persistent storage.
On failure, there was not enough room to store the credentials */
typedef struct _particle_ctrl_wifi_ScanNetworksReply {
pb_callback_t networks;
pb_callback_t networks; /* Network SSID */
} particle_ctrl_wifi_ScanNetworksReply;

typedef struct _particle_ctrl_wifi_ScanNetworksRequest {
char dummy_field;
} particle_ctrl_wifi_ScanNetworksRequest;

typedef struct _particle_ctrl_wifi_SetNetworkCredentialsReply {
char dummy_field;
} particle_ctrl_wifi_SetNetworkCredentialsReply;

/* *
Network credentials. */
typedef struct _particle_ctrl_wifi_Credentials {
Expand Down Expand Up @@ -144,6 +153,16 @@ typedef struct _particle_ctrl_wifi_JoinNewNetworkRequest {
bool hidden;
} particle_ctrl_wifi_JoinNewNetworkRequest;

typedef PB_BYTES_ARRAY_T(6) particle_ctrl_wifi_SetNetworkCredentialsRequest_bssid_t;
typedef struct _particle_ctrl_wifi_SetNetworkCredentialsRequest {
pb_callback_t ssid;
particle_ctrl_wifi_SetNetworkCredentialsRequest_bssid_t bssid;
particle_ctrl_wifi_Security security;
particle_ctrl_wifi_Credentials credentials;
particle_ctrl_Interface interface_config;
bool hidden;
} particle_ctrl_wifi_SetNetworkCredentialsRequest;


/* Helper constants for enums */
#define _particle_ctrl_wifi_Security_MIN particle_ctrl_wifi_Security_NO_SECURITY
Expand Down Expand Up @@ -177,6 +196,8 @@ extern "C" {
#define particle_ctrl_wifi_ScanNetworksRequest_init_default {0}
#define particle_ctrl_wifi_ScanNetworksReply_init_default {{{NULL}, NULL}}
#define particle_ctrl_wifi_ScanNetworksReply_Network_init_default {{{NULL}, NULL}, {0, {0}}, _particle_ctrl_wifi_Security_MIN, 0, 0}
#define particle_ctrl_wifi_SetNetworkCredentialsRequest_init_default {{{NULL}, NULL}, {0, {0}}, _particle_ctrl_wifi_Security_MIN, particle_ctrl_wifi_Credentials_init_default, particle_ctrl_Interface_init_default, 0}
#define particle_ctrl_wifi_SetNetworkCredentialsReply_init_default {0}
#define particle_ctrl_wifi_Credentials_init_zero {_particle_ctrl_wifi_CredentialsType_MIN, {{NULL}, NULL}}
#define particle_ctrl_wifi_JoinNewNetworkRequest_init_zero {{{NULL}, NULL}, {0, {0}}, _particle_ctrl_wifi_Security_MIN, particle_ctrl_wifi_Credentials_init_zero, particle_ctrl_Interface_init_zero, 0}
#define particle_ctrl_wifi_JoinNewNetworkReply_init_zero {0}
Expand All @@ -194,6 +215,8 @@ extern "C" {
#define particle_ctrl_wifi_ScanNetworksRequest_init_zero {0}
#define particle_ctrl_wifi_ScanNetworksReply_init_zero {{{NULL}, NULL}}
#define particle_ctrl_wifi_ScanNetworksReply_Network_init_zero {{{NULL}, NULL}, {0, {0}}, _particle_ctrl_wifi_Security_MIN, 0, 0}
#define particle_ctrl_wifi_SetNetworkCredentialsRequest_init_zero {{{NULL}, NULL}, {0, {0}}, _particle_ctrl_wifi_Security_MIN, particle_ctrl_wifi_Credentials_init_zero, particle_ctrl_Interface_init_zero, 0}
#define particle_ctrl_wifi_SetNetworkCredentialsReply_init_zero {0}

/* Field tags (for use in manual encoding/decoding) */
#define particle_ctrl_wifi_GetKnownNetworksReply_networks_tag 1
Expand All @@ -220,6 +243,12 @@ extern "C" {
#define particle_ctrl_wifi_JoinNewNetworkRequest_credentials_tag 4
#define particle_ctrl_wifi_JoinNewNetworkRequest_interface_config_tag 5
#define particle_ctrl_wifi_JoinNewNetworkRequest_hidden_tag 6
#define particle_ctrl_wifi_SetNetworkCredentialsRequest_ssid_tag 1
#define particle_ctrl_wifi_SetNetworkCredentialsRequest_bssid_tag 2
#define particle_ctrl_wifi_SetNetworkCredentialsRequest_security_tag 3
#define particle_ctrl_wifi_SetNetworkCredentialsRequest_credentials_tag 4
#define particle_ctrl_wifi_SetNetworkCredentialsRequest_interface_config_tag 5
#define particle_ctrl_wifi_SetNetworkCredentialsRequest_hidden_tag 6

/* Struct field encoding specification for nanopb */
#define particle_ctrl_wifi_Credentials_FIELDLIST(X, a) \
Expand Down Expand Up @@ -326,6 +355,23 @@ X(a, STATIC, SINGULAR, INT32, rssi, 5)
#define particle_ctrl_wifi_ScanNetworksReply_Network_CALLBACK pb_default_field_callback
#define particle_ctrl_wifi_ScanNetworksReply_Network_DEFAULT NULL

#define particle_ctrl_wifi_SetNetworkCredentialsRequest_FIELDLIST(X, a) \
X(a, CALLBACK, SINGULAR, STRING, ssid, 1) \
X(a, STATIC, SINGULAR, BYTES, bssid, 2) \
X(a, STATIC, SINGULAR, UENUM, security, 3) \
X(a, STATIC, SINGULAR, MESSAGE, credentials, 4) \
X(a, STATIC, SINGULAR, MESSAGE, interface_config, 5) \
X(a, STATIC, SINGULAR, BOOL, hidden, 6)
#define particle_ctrl_wifi_SetNetworkCredentialsRequest_CALLBACK pb_default_field_callback
#define particle_ctrl_wifi_SetNetworkCredentialsRequest_DEFAULT NULL
#define particle_ctrl_wifi_SetNetworkCredentialsRequest_credentials_MSGTYPE particle_ctrl_wifi_Credentials
#define particle_ctrl_wifi_SetNetworkCredentialsRequest_interface_config_MSGTYPE particle_ctrl_Interface

#define particle_ctrl_wifi_SetNetworkCredentialsReply_FIELDLIST(X, a) \

#define particle_ctrl_wifi_SetNetworkCredentialsReply_CALLBACK NULL
#define particle_ctrl_wifi_SetNetworkCredentialsReply_DEFAULT NULL

extern const pb_msgdesc_t particle_ctrl_wifi_Credentials_msg;
extern const pb_msgdesc_t particle_ctrl_wifi_JoinNewNetworkRequest_msg;
extern const pb_msgdesc_t particle_ctrl_wifi_JoinNewNetworkReply_msg;
Expand All @@ -343,6 +389,8 @@ extern const pb_msgdesc_t particle_ctrl_wifi_GetCurrentNetworkReply_msg;
extern const pb_msgdesc_t particle_ctrl_wifi_ScanNetworksRequest_msg;
extern const pb_msgdesc_t particle_ctrl_wifi_ScanNetworksReply_msg;
extern const pb_msgdesc_t particle_ctrl_wifi_ScanNetworksReply_Network_msg;
extern const pb_msgdesc_t particle_ctrl_wifi_SetNetworkCredentialsRequest_msg;
extern const pb_msgdesc_t particle_ctrl_wifi_SetNetworkCredentialsReply_msg;

/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
#define particle_ctrl_wifi_Credentials_fields &particle_ctrl_wifi_Credentials_msg
Expand All @@ -362,6 +410,8 @@ extern const pb_msgdesc_t particle_ctrl_wifi_ScanNetworksReply_Network_msg;
#define particle_ctrl_wifi_ScanNetworksRequest_fields &particle_ctrl_wifi_ScanNetworksRequest_msg
#define particle_ctrl_wifi_ScanNetworksReply_fields &particle_ctrl_wifi_ScanNetworksReply_msg
#define particle_ctrl_wifi_ScanNetworksReply_Network_fields &particle_ctrl_wifi_ScanNetworksReply_Network_msg
#define particle_ctrl_wifi_SetNetworkCredentialsRequest_fields &particle_ctrl_wifi_SetNetworkCredentialsRequest_msg
#define particle_ctrl_wifi_SetNetworkCredentialsReply_fields &particle_ctrl_wifi_SetNetworkCredentialsReply_msg

/* Maximum encoded size of messages (where known) */
/* particle_ctrl_wifi_Credentials_size depends on runtime parameters */
Expand All @@ -373,6 +423,7 @@ extern const pb_msgdesc_t particle_ctrl_wifi_ScanNetworksReply_Network_msg;
/* particle_ctrl_wifi_GetCurrentNetworkReply_size depends on runtime parameters */
/* particle_ctrl_wifi_ScanNetworksReply_size depends on runtime parameters */
/* particle_ctrl_wifi_ScanNetworksReply_Network_size depends on runtime parameters */
/* particle_ctrl_wifi_SetNetworkCredentialsRequest_size depends on runtime parameters */
#define particle_ctrl_wifi_ClearKnownNetworksReply_size 0
#define particle_ctrl_wifi_ClearKnownNetworksRequest_size 0
#define particle_ctrl_wifi_GetCurrentNetworkRequest_size 0
Expand All @@ -381,6 +432,7 @@ extern const pb_msgdesc_t particle_ctrl_wifi_ScanNetworksReply_Network_msg;
#define particle_ctrl_wifi_JoinNewNetworkReply_size 0
#define particle_ctrl_wifi_RemoveKnownNetworkReply_size 0
#define particle_ctrl_wifi_ScanNetworksRequest_size 0
#define particle_ctrl_wifi_SetNetworkCredentialsReply_size 0

#ifdef __cplusplus
} /* extern "C" */
Expand Down
1 change: 1 addition & 0 deletions system/inc/system_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ typedef enum ctrl_request_type {
CTRL_REQUEST_WIFI_CLEAR_KNOWN_NETWORKS = 504,
CTRL_REQUEST_WIFI_GET_CURRENT_NETWORK = 505,
CTRL_REQUEST_WIFI_SCAN_NETWORKS = 506,
CTRL_REQUEST_WIFI_SET_NETWORK_CREDENTIALS = 507,
// Cellular network management
CTRL_REQUEST_CELLULAR_SET_ACCESS_POINT = 550,
CTRL_REQUEST_CELLULAR_GET_ACCESS_POINT = 551,
Expand Down
32 changes: 32 additions & 0 deletions system/src/control/wifi_new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,38 @@ int scanNetworks(ctrl_request* req) {
return 0;
}

int setNetworkCredentials(ctrl_request* req) {
PB(SetNetworkCredentialsRequest) pbReq = {};
DecodedCString dSsid(&pbReq.ssid);
DecodedCString dPwd(&pbReq.credentials.password);
CHECK(decodeRequestMessage(req, PB(SetNetworkCredentialsRequest_fields), &pbReq));
// Parse new network configuration
if (pbReq.credentials.type != PB(CredentialsType_NO_CREDENTIALS) &&
pbReq.credentials.type != PB(CredentialsType_PASSWORD)) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}
WifiCredentials cred;
cred.type((WifiCredentials::Type)pbReq.credentials.type);
if (pbReq.credentials.type == PB(CredentialsType_PASSWORD)) {
cred.password(dPwd.data);
}
WifiNetworkConfig conf;
conf.ssid(dSsid.data);
MacAddress bssid = INVALID_MAC_ADDRESS;
bssidFromPb(&bssid, pbReq.bssid);
conf.bssid(bssid);
conf.hidden(pbReq.hidden);
conf.security((WifiSecurity)pbReq.security);
conf.credentials(std::move(cred));

const auto wifiMgr = wifiNetworkManager();
CHECK_TRUE(wifiMgr, SYSTEM_ERROR_UNKNOWN);
CHECK(wifiMgr->setNetworkConfig(conf, WifiNetworkConfigFlag::NONE));
system_notify_event(network_credentials, network_credentials_added);

return 0;
}

} // particle::ctrl::wifi

} // particle::ctrl
Expand Down
1 change: 1 addition & 0 deletions system/src/control/wifi_new.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int removeKnownNetwork(ctrl_request* req);
int clearKnownNetworks(ctrl_request* req);
int getCurrentNetwork(ctrl_request* req);
int scanNetworks(ctrl_request* req);
int setNetworkCredentials(ctrl_request* req);

} // particle::ctrl::wifi

Expand Down
4 changes: 4 additions & 0 deletions system/src/system_control_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ void SystemControl::processRequest(ctrl_request* req, ControlRequestChannel* /*
setResult(req, ctrl::wifi::scanNetworks(req));
break;
}
case CTRL_REQUEST_WIFI_SET_NETWORK_CREDENTIALS: {
setResult(req, ctrl::wifi::setNetworkCredentials(req));
break;
}
#endif // HAL_PLATFORM_NCP && HAL_PLATFORM_WIFI
#if HAL_PLATFORM_NCP && HAL_PLATFORM_CELLULAR
case CTRL_REQUEST_CELLULAR_SET_ACCESS_POINT: {
Expand Down

0 comments on commit b3b92e3

Please sign in to comment.