Skip to content

Commit

Permalink
Fix error when sasl is not available, add cancel request
Browse files Browse the repository at this point in the history
  • Loading branch information
sylane committed Oct 11, 2024
1 parent 292fb40 commit 39be342
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
39 changes: 29 additions & 10 deletions docs/grisp_connect_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ release name and version and if update is enabled.

**`result`**: JSON Object

| key | value | type | description |
|-----------------|-----------|----------|------------------------------------------------------------------|
| relname | string | required | The name of the release running currently on the device |
| relvsn | string | required | The version of the release running currently on the device |
| update_enabled | boolean | required | If updating is enbaled on the device |
| boot_source | map | optional | {"type": "system", "id": ID} or {"type": "removable"} |
| update_status | string | optional | `"ready"`, `"updating"`, `"failed"`, or `"updated"` |
| update_progress | integer | optional | The progress as a percentage |
| update_message | string | optional | Message describing the current state of the system |
| action_required | boolean | optional | `null`, `"reboot"`, `"remove_sdcard_and_reboot"` or `"validate"` |
| key | value | type | description |
|-----------------|----------------|----------|------------------------------------------------------------------|
| relname | string or null | required | The name of the release running currently on the device |
| relvsn | string or null | required | The version of the release running currently on the device |
| update_enabled | boolean | required | If updating is enbaled on the device |
| boot_source | map | optional | `{"type": "system", "id": ID}` or `{"type": "removable"}` |
| update_status | string | optional | `"ready"`, `"updating"`, `"failed"`, or `"updated"` |
| update_progress | integer | optional | The progress as a percentage |
| update_message | string | optional | Message describing the current state of the system |
| action_required | boolean | optional | `null`, `"reboot"`, `"remove_sdcard_and_reboot"` or `"validate"` |

Meaning of the status:

Expand Down Expand Up @@ -117,6 +117,25 @@ This should only be called if the new software is functioning as expected.
</p>
</details>

<details><summary><i>Post - Cancel the current update</i></summary>
<p>

**`params`:**
| key (required *) | value | description |
| ----------------- | -------- | -------------------------- |
| `"type"` * | string | `"cancel"` |

**`result`**: `"ok"`

**`error`**:

| Error Content | When it Happens |
| ----------------------------------------------------| -------------------------------- |
| `{code: -10, message: "grisp_updater_unavailable"}` | Grisp updater app is not running |

</p>
</details>

### Notifications

<details><summary><code>update</code> <code>{"type":"software_update_event"}</code> - notify the current progess of grisp_updater </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/grisp_connect_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ handle_request(?method_post, #{type := <<"validate">>}, ID) ->
handle_request(?method_post, #{type := <<"reboot">>}, ID) ->
grisp_connect_client:reboot(),
{send_response, grisp_connect_jsonrpc:encode({result, ok, ID})};
handle_request(?method_post, #{type := <<"cancel">>}, ID) ->
Reply = case grisp_connect_updater:cancel() of
{error, grisp_updater_unavailable} ->
{error, -10, grisp_updater_unavailable, undefined, ID};
ok ->
{result, ok, ID}
end,
{send_response, grisp_connect_jsonrpc:encode(Reply)};
handle_request(_T, _P, ID) ->
Error = {internal_error, method_not_found, ID},
FormattedError = grisp_connect_jsonrpc:format_error(Error),
Expand Down
18 changes: 11 additions & 7 deletions src/grisp_connect_updater.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
-export([system_info/0]).
-export([start_update/1]).
-export([validate/0]).
-export([cancel/0]).


%--- API -----------------------------------------------------------------------
Expand All @@ -28,7 +29,10 @@ system_info() ->
catch
exit:{noproc, _} ->
% Running in a shell
#{relname => undefined, relvsn => undefined}
#{relname => null, relvsn => null};
exit:undef ->
% Sasl is not running
#{relname => null, relvsn => null}
end,
UpdateInfo = update_info(),
maps:merge(RelInfo, UpdateInfo).
Expand All @@ -47,6 +51,12 @@ validate() ->
false -> {error, grisp_updater_unavailable}
end.

cancel() ->
case is_running(grisp_updater) of
true -> grisp_updater:cancel();
false -> {error, grisp_updater_unavailable}
end.


%--- INTERNAL FUNCTIONS --------------------------------------------------------

Expand Down Expand Up @@ -75,7 +85,6 @@ update_info() ->
#{update_enabled => true,
boot_source => Boot,
update_status => ready,
update_progress => 0,
update_message => <<"Device ready for update">>};
% Ready for update from valid system
{ready,
Expand All @@ -86,7 +95,6 @@ update_info() ->
#{update_enabled => true,
boot_source => Boot,
update_status => ready,
update_progress => 0,
update_message => <<"Device ready for update">>};
% Updating
{{updating, Stats}, Boot, _, _} ->
Expand All @@ -103,7 +111,6 @@ update_info() ->
#{update_enabled => true,
boot_source => Boot,
update_status => failed,
update_progress => 0,
update_message => <<"Device update failed">>};
% Update succeed
{{success, _Stats},
Expand All @@ -113,7 +120,6 @@ update_info() ->
when ValidId =/= NextId ->
#{update_status => updated,
boot_source => Boot,
update_progress => 100,
update_message => <<"Device updated, reboot required to validate the update">>,
action_required => reboot};
% Booted from removable after update
Expand All @@ -125,7 +131,6 @@ update_info() ->
#{update_enabled => true,
boot_source => Boot,
update_status => updated,
update_progress => 100,
update_message => <<"Device updated but the SD card wasn't removed before rebooting">>,
action_required => remove_sdcard_and_reboot};
% Updated and rebooted
Expand All @@ -137,7 +142,6 @@ update_info() ->
#{update_enabled => true,
boot_source => Boot,
update_status => updated,
update_progress => 100,
update_message => <<"Device updated, validation required">>,
action_required => validate};
_ ->
Expand Down
25 changes: 21 additions & 4 deletions src/grisp_connect_updater_progress.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ progress_warning(State, Reason, Msg) ->
<<"update">>,
<<"software_update_event">>,
#{event => warning,
reason => Reason,
message => Msg}),
reason => as_json(Reason),
message => as_json_string(Msg)}),
{ok, State}.

progress_error(#state{}, Stats, Reason, Msg) ->
Expand All @@ -72,8 +72,8 @@ progress_error(#state{}, Stats, Reason, Msg) ->
<<"update">>,
<<"software_update_event">>,
#{event => error,
reason => Reason,
message => Msg,
reason => as_json(Reason),
message => as_json_string(Msg),
percentage => UpdatePercentage}),
ok.

Expand All @@ -88,6 +88,23 @@ progress_done(#state{}, _Stats) ->

%--- Internal Functions --------------------------------------------------------

as_json_string(undefined) -> null;
as_json_string(Value) when is_binary(Value) -> Value;
as_json_string(Value) when is_atom(Value) -> Value;
as_json_string(Value) when is_integer(Value) -> integer_to_binary(Value);
as_json_string(Value) when is_float(Value) -> float_to_binary(Value);
as_json_string(Value) when is_list(Value) -> list_to_binary(Value).

as_json(undefined) -> null;
as_json(Value) when is_binary(Value) -> Value;
as_json(Value) when is_atom(Value) -> Value;
as_json(Value) when is_integer(Value) -> Value;
as_json(Value) when is_float(Value) -> Value;
as_json(Value) when is_list(Value) -> [as_json(V) || V <- Value];
as_json(Value) when is_map(Value) ->
maps:from_list([{as_json_string(K), as_json(V)} || {K, V} <- maps:to_list(Value)]).


progress_percent(Stats) ->
#{data_total := Total, data_checked := Checked,
data_skipped := Skipped, data_written := Written} = Stats,
Expand Down

0 comments on commit 39be342

Please sign in to comment.