@@ -63,25 +63,15 @@ handle_rpc_messages([{error, _Code, _Msg, _Data, _ID} = E | Batch], Replies) ->
6363 handle_rpc_messages (Batch , [handle_response (E )| Replies ]);
6464handle_rpc_messages ([{internal_error , _ , _ } = E | Batch ], Replies ) ->
6565 ? LOG_ERROR (" JsonRPC: ~p " ,[E ]),
66- handle_rpc_messages (Batch ,
67- [grisp_connect_jsonrpc :format_error (E )| Replies ]).
66+ handle_rpc_messages (Batch , Replies ).
6867
69- handle_request (? method_get , #{type := <<" partition_state" >>} = _Params , ID ) ->
70- Info = get_partition_info (),
71- Reply = case Info of
72- #{state := _State ,
73- message := _Msg ,
74- action_required := _ActionRequired } = Response ->
75- {result , Response , ID };
76- {error , Reason } ->
77- ReasonBinary = iolist_to_binary (io_lib :format (" ~p " , [Reason ])),
78- grisp_connect_jsonrpc :format_error ({internal_error , ReasonBinary , ID })
79- end ,
80- {send_response , grisp_connect_jsonrpc :encode (Reply )};
68+ handle_request (? method_get , #{type := <<" system_info" >>} = _Params , ID ) ->
69+ Info = grisp_connect_updater :system_info (),
70+ {send_response , grisp_connect_jsonrpc :encode ({result , Info , ID })};
8171handle_request (? method_post , #{type := <<" start_update" >>} = Params , ID ) ->
8272 try
8373 URL = maps :get (url , Params ),
84- Reply = case start_update (URL ) of
74+ Reply = case grisp_connect_updater : start_update (URL ) of
8575 {error , grisp_updater_unavailable } ->
8676 {error , - 10 , grisp_updater_unavailable , undefined , ID };
8777 {error , already_updating } ->
@@ -102,7 +92,9 @@ handle_request(?method_post, #{type := <<"start_update">>} = Params, ID) ->
10292 {internal_error , invalid_params , ID })}
10393 end ;
10494handle_request (? method_post , #{type := <<" validate" >>}, ID ) ->
105- Reply = case grisp_updater :validate () of
95+ Reply = case grisp_connect_updater :validate () of
96+ {error , grisp_updater_unavailable } ->
97+ {error , - 10 , grisp_updater_unavailable , undefined , ID };
10698 {error , {validate_from_unbooted , PartitionIndex }} ->
10799 {error , - 13 , validate_from_unbooted , PartitionIndex , ID };
108100 {error , Reason } ->
@@ -112,7 +104,18 @@ handle_request(?method_post, #{type := <<"validate">>}, ID) ->
112104 {result , ok , ID }
113105 end ,
114106 {send_response , grisp_connect_jsonrpc :encode (Reply )};
115- handle_request (_ , _ , ID ) ->
107+ handle_request (? method_post , #{type := <<" reboot" >>}, ID ) ->
108+ grisp_connect_client :reboot (),
109+ {send_response , grisp_connect_jsonrpc :encode ({result , ok , ID })};
110+ handle_request (? method_post , #{type := <<" cancel" >>}, ID ) ->
111+ Reply = case grisp_connect_updater :cancel () of
112+ {error , grisp_updater_unavailable } ->
113+ {error , - 10 , grisp_updater_unavailable , undefined , ID };
114+ ok ->
115+ {result , ok , ID }
116+ end ,
117+ {send_response , grisp_connect_jsonrpc :encode (Reply )};
118+ handle_request (_T , _P , ID ) ->
116119 Error = {internal_error , method_not_found , ID },
117120 FormattedError = grisp_connect_jsonrpc :format_error (Error ),
118121 {send_response , grisp_connect_jsonrpc :encode (FormattedError )}.
@@ -126,79 +129,6 @@ handle_response(Response) ->
126129 end ,
127130 {handle_response , ID , Reply }.
128131
129- start_update (URL ) ->
130- case is_running (grisp_updater ) of
131- true -> grisp_updater :start (URL ,
132- grisp_connect_updater_progress ,
133- #{client => self ()}, #{});
134- false -> {error , grisp_updater_unavailable }
135- end .
136-
137- get_partition_info () ->
138- case is_running (grisp_updater ) of
139- true ->
140- Info = grisp_updater :info (),
141- #{boot := Boot , valid := Valid , next := Next } = Info ,
142- ActionRequired = maps :get (action_required , Info , false ),
143- case evaluate_partition_state (Boot , Valid , Next ) of
144- new_boot ->
145- #{state => <<" new" >>,
146- message => <<" New partition booted, validation required" >>,
147- action_required => ActionRequired };
148- update_pending ->
149- #{state => <<" old" >>,
150- message => <<" Reboot required to load new partition" >>,
151- action_required => ActionRequired };
152- no_update_pending ->
153- #{state => <<" old_no_update" >>,
154- message => <<" No update pending, running old partition" >>,
155- action_required => ActionRequired };
156- _ ->
157- #{state => <<" unknown" >>,
158- message => <<" Unknown partition state" >>,
159- action_required => ActionRequired }
160- end ;
161- false -> {error , grisp_updater_unavailable }
162- end .
163-
164- evaluate_partition_state (BootPartition , ValidPartition , NextPartition ) ->
165- case {BootPartition , ValidPartition , NextPartition } of
166- % Case 1: Booting from removable media, but system has a pending update
167- {#{type := removable },
168- #{type := system , id := ValidId },
169- #{type := system , id := NextId }}
170- when ValidId =/= NextId -> update_pending ;
171- % Case 2: Booted from system partition, but a different system partition is pending update
172- {#{type := system , id := BootId },
173- #{type := system , id := ValidId },
174- #{type := system , id := NextId }}
175- when BootId == ValidId , ValidId =/= NextId -> update_pending ;
176- % Case 3: Booted from a new partition, validation required
177- {#{type := system , id := BootId },
178- #{type := system , id := ValidId },
179- _ }
180- when BootId =/= ValidId -> new_boot ;
181- % Case 4: Booted from removable media, no update pending
182- {#{type := removable },
183- #{type := system , id := ValidId },
184- #{type := system , id := NextId }}
185- when ValidId == NextId -> no_update_pending ;
186- % Case 5: Booted from system partition, no update pending
187- {#{type := system , id := BootId },
188- _ ,
189- #{type := system , id := NextId }}
190- when NextId == BootId -> no_update_pending ;
191- % Default case: Unknown partition state
192- _ -> unknown_state
193- end .
194-
195- is_running (AppName ) ->
196- Apps = application :which_applications (),
197- case [App || {App , _Desc , _VSN } <- Apps , App =:= AppName ] of
198- [] -> false ;
199- [_ ] -> true
200- end .
201-
202132error_atom (- 1 ) -> device_not_linked ;
203133error_atom (- 2 ) -> token_expired ;
204134error_atom (- 3 ) -> device_already_linked ;
0 commit comments