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 0585dc5 commit 31d17b5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
40 changes: 30 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,26 @@ 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 |
| `{code: -14, message: "not_updating"}` | Not currently updating |

</p>
</details>

### Notifications

<details><summary><code>update</code> <code>{"type":"software_update_event"}</code> - notify the current progess of grisp_updater </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/grisp_connect_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ 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};
{error, not_updating} ->
{error, -14, not_updating, 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

0 comments on commit 31d17b5

Please sign in to comment.