Skip to content

Commit

Permalink
working wifi connect parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
KenVanHoeylandt committed Jan 13, 2024
1 parent 200c223 commit e3dad31
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 23 deletions.
11 changes: 7 additions & 4 deletions components/furi/src/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ static AppFlags app_get_flags_default(AppType type);

// region Alloc/free

App app_alloc(const AppManifest* manifest, Bundle* _Nullable bundle) {
App app_alloc(const AppManifest* manifest, Bundle* _Nullable parameters) {
AppData* data = malloc(sizeof(AppData));
*data = (AppData) {
.mutex = furi_mutex_alloc(FuriMutexTypeRecursive),
.state = APP_STATE_INITIAL,
.flags = app_get_flags_default(manifest->type),
.manifest = manifest,
.bundle = bundle,
.parameters = parameters,
.data = NULL
};
return (App*)data;
}

void app_free(App app) {
AppData* data = (AppData*)app;
if (data->parameters) {
bundle_free(data->parameters);
}
furi_mutex_free(data->mutex);
free(data);
}
Expand Down Expand Up @@ -114,10 +117,10 @@ void app_set_data(App app, void* value) {
* Consider creating MutableBundle vs Bundle.
* Consider not exposing bundle, but expose `app_get_bundle_int(key)` methods with locking in it.
*/
Bundle* _Nullable app_get_bundle(App app) {
Bundle* _Nullable app_get_parameters(App app) {
AppData* data = (AppData*)app;
app_lock(data);
Bundle* bundle = data->bundle;
Bundle* bundle = data->parameters;
app_unlock(data);
return bundle;
}
Expand Down
6 changes: 3 additions & 3 deletions components/furi/src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ typedef void* App;

/** @brief Create an app
* @param manifest
* @param bundle optional bundle. memory ownership is transferred to App
* @param parameters optional bundle. memory ownership is transferred to App
* @return
*/
App app_alloc(const AppManifest* manifest, Bundle* _Nullable bundle);
App app_alloc(const AppManifest* manifest, Bundle* _Nullable parameters);
void app_free(App app);

void app_set_state(App app, AppState state);
Expand All @@ -44,7 +44,7 @@ void app_set_flags(App app, AppFlags flags);
void* _Nullable app_get_data(App app);
void app_set_data(App app, void* data);

Bundle* _Nullable app_get_bundle(App app);
Bundle* _Nullable app_get_parameters(App app);

#ifdef __cplusplus
}
Expand Down
5 changes: 3 additions & 2 deletions components/furi/src/app_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ typedef struct {
const AppManifest* manifest;
AppState state;
AppFlags flags;
/** @brief Optional arguments
/** @brief Optional parameters to start the app with
* When these are stored in the app struct, the struct takes ownership.
* Do not mutate after app creation.
*/
Bundle* _Nullable bundle;
Bundle* _Nullable parameters;
/** @brief @brief Contextual data related to the running app's instance
* The app can attach its data to this.
* The lifecycle is determined by the on_start and on_stop methods in the AppManifest.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static void app_show(App app, lv_obj_t* parent) {

wifi_connect_lock(wifi);
wifi->view_enabled = true;
wifi_connect_view_create(&wifi->view, &wifi->bindings, parent);
wifi_connect_view_create(app, wifi, parent);
wifi_connect_view_update(&wifi->view, &wifi->bindings, &wifi->state);
wifi_connect_unlock(wifi);
}
Expand All @@ -93,7 +93,7 @@ static void app_hide(App app) {
}

static void app_start(App app) {
WifiConnect* wifi_connect = wifi_connect_alloc();
WifiConnect* wifi_connect = wifi_connect_alloc(app);
app_set_data(app, wifi_connect);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "mutex.h"
#include "services/wifi/wifi.h"
#include "wifi_connect_bindings.h"
#include "wifi_connect_state.h"
#include "wifi_connect_view.h"

#ifdef __cplusplus
Expand All @@ -21,7 +23,7 @@ void wifi_connect_lock(WifiConnect* wifi);

void wifi_connect_unlock(WifiConnect* wifi);

void wifi_connect_request_view_update(WifiConnect * wifi);
void wifi_connect_request_view_update(WifiConnect* wifi);

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#define WIFI_CONNECT_PARAM_SSID "ssid" // String
#define WIFI_CONNECT_PARAM_PASSWORD "password" // String

#ifdef __cplusplus
}
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <stdbool.h>
#include "app.h"
#include "services/wifi/wifi.h"

#ifdef __cplusplus
Expand All @@ -12,7 +13,6 @@ extern "C" {
*/
typedef struct {
WifiRadioState radio_state;
uint8_t connect_ssid[33];
bool connection_error;
} WifiConnectState;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@
#include "lvgl.h"
#include "ui/spacer.h"
#include "ui/style.h"
#include "wifi_connect.h"
#include "wifi_connect_bundle.h"
#include "wifi_connect_state.h"

#define TAG "wifi_connect_view"

static void on_connect(lv_event_t* event) {
WifiConnectBindings* bindings = (WifiConnectBindings*)event->user_data;
bindings->on_connect_ssid("On The Fence", "butallowedtoenter", bindings->on_connect_ssid_context);
WifiConnect* wifi = (WifiConnect*)event->user_data;
WifiConnectView* view = &wifi->view;
const char* ssid = lv_textarea_get_text(view->ssid_textarea);
const char* password = lv_textarea_get_text(view->password_textarea);

WifiConnectBindings* bindings = &wifi->bindings;
bindings->on_connect_ssid(
ssid,
password,
bindings->on_connect_ssid_context
);
}

// TODO: Standardize dialogs
void wifi_connect_view_create(WifiConnectView* view, WifiConnectBindings* bindings, lv_obj_t* parent) {
void wifi_connect_view_create(App app, void* wifi, lv_obj_t* parent) {
WifiConnect* wifi_connect = (WifiConnect*)wifi;
WifiConnectView* view = &wifi_connect->view;
// TODO: Standardize this into "window content" function?
// TODO: It can then be dynamically determined based on screen res and size
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
Expand All @@ -33,6 +44,8 @@ void wifi_connect_view_create(WifiConnectView* view, WifiConnectBindings* bindin
lv_label_set_text(password_label, "Password:");
view->password_textarea = lv_textarea_create(parent);
lv_textarea_set_one_line(view->password_textarea, true);
lv_textarea_set_password_show_time(view->password_textarea, 0);
lv_textarea_set_password_mode(view->password_textarea, true);

lv_obj_t* button_container = lv_obj_create(parent);
lv_obj_set_width(button_container, LV_PCT(100));
Expand All @@ -48,9 +61,25 @@ void wifi_connect_view_create(WifiConnectView* view, WifiConnectBindings* bindin
lv_obj_t* connect_label = lv_label_create(view->connect_button);
lv_label_set_text(connect_label, "Connect");
lv_obj_center(connect_label);
lv_obj_add_event_cb(view->connect_button, &on_connect, LV_EVENT_CLICKED, bindings);
lv_obj_add_event_cb(view->connect_button, &on_connect, LV_EVENT_CLICKED, wifi);

// Init from app parameters
Bundle* _Nullable bundle = app_get_parameters(app);
if (bundle) {
char* ssid;
if (bundle_opt_string(bundle, WIFI_CONNECT_PARAM_SSID, &ssid)) {
lv_textarea_set_text(view->ssid_textarea, ssid);
}

char* password;
if (bundle_opt_string(bundle, WIFI_CONNECT_PARAM_PASSWORD, &password)) {
lv_textarea_set_text(view->password_textarea, password);
}
}
}

void wifi_connect_view_update(WifiConnectView* view, WifiConnectBindings* bindings, WifiConnectState* state) {
lv_textarea_set_text(view->ssid_textarea, (const char*)state->connect_ssid);
UNUSED(view);
UNUSED(bindings);
UNUSED(state);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ typedef struct {
lv_obj_t* cancel_button;
} WifiConnectView;

void wifi_connect_view_create(WifiConnectView* view, WifiConnectBindings* bindings, lv_obj_t* parent);
void wifi_connect_view_create(App app, void* wifi, lv_obj_t* parent);
void wifi_connect_view_update(WifiConnectView* view, WifiConnectBindings* bindings, WifiConnectState* state);

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "wifi_manage.h"

#include "app.h"
#include "apps/system/wifi_connect/wifi_connect_bundle.h"
#include "esp_lvgl_port.h"
#include "furi_core.h"
#include "services/loader/loader.h"
Expand All @@ -11,7 +12,10 @@
static void wifi_manage_event_callback(const void* message, void* context);

static void on_connect(const char* ssid) {
loader_start_app("wifi_connect", false, NULL);
Bundle bundle = bundle_alloc();
bundle_put_string(bundle, WIFI_CONNECT_PARAM_SSID, ssid);
bundle_put_string(bundle, WIFI_CONNECT_PARAM_PASSWORD, ""); // TODO: Implement from cache
loader_start_app("wifi_connect", false, bundle);
}

static void on_disconnect() {
Expand Down
6 changes: 5 additions & 1 deletion components/tactility/src/services/loader/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ LoaderStatus loader_start_app(const char* id, bool blocking, Bundle* _Nullable b
LoaderMessage message = {
.type = LoaderMessageTypeAppStart,
.start.id = id,
.start.bundle = bundle,
.status_value = &result,
.api_lock = blocking ? api_lock_alloc_locked() : NULL
};
Expand Down Expand Up @@ -263,7 +264,10 @@ static int32_t loader_main(void* p) {
switch (message.type) {
case LoaderMessageTypeAppStart:
// TODO: add bundle
message.status_value->value = loader_do_start_by_id(message.start.id, NULL);
message.status_value->value = loader_do_start_by_id(
message.start.id,
message.start.bundle
);
if (message.api_lock) {
api_lock_unlock(message.api_lock);
}
Expand Down
1 change: 1 addition & 0 deletions components/tactility/src/services/loader/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ typedef struct {
* @brief Close any running app, then start new one. Blocking.
* @param[in] id application name or id
* @param[in] blocking application arguments
* @param[in] bundle optional bundle. Ownership is transferred to Loader.
* @return LoaderStatus
*/
LoaderStatus loader_start_app(const char* id, bool blocking, Bundle* _Nullable bundle);
Expand Down
1 change: 1 addition & 0 deletions components/tactility/src/services/loader/loader_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef enum {

typedef struct {
const char* id;
Bundle* _Nullable bundle;
} LoaderMessageAppStart;

typedef struct {
Expand Down

0 comments on commit e3dad31

Please sign in to comment.