PX4 Firmware
PX4 Autopilot Software http://px4.io
ArmAuthorization.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017 Intel Corporation. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 #include "ArmAuthorization.h"
34 
35 #include <string.h>
36 #include <unistd.h>
37 
38 #include <px4_platform_common/defines.h>
39 #include <px4_platform_common/px4_config.h>
40 #include <lib/parameters/param.h>
41 #include <systemlib/mavlink_log.h>
45 
47 static int command_ack_sub = -1;
48 
50 
52 
53 static enum {
59 
60 union {
61  struct {
63  union {
68  } __attribute__((packed)) struct_value;
69  int32_t param_value;
71 
72 static uint8_t *system_id;
73 
74 static uint8_t _auth_method_arm_req_check();
75 static uint8_t _auth_method_two_arm_check();
76 
77 static uint8_t (*arm_check_method[ARM_AUTH_METHOD_LAST])() = {
80 };
81 
83 {
84  vehicle_command_s vcmd{};
86  vcmd.command = vehicle_command_s::VEHICLE_CMD_ARM_AUTHORIZATION_REQUEST;
87  vcmd.target_system = arm_parameters.struct_value.authorizer_system_id;
88 
89  uORB::PublicationQueued<vehicle_command_s> vcmd_pub{ORB_ID(vehicle_command)};
90  vcmd_pub.publish(vcmd);
91 }
92 
94 {
95  switch (state) {
96  case ARM_AUTH_IDLE:
97  /* no authentication in process? handle bellow */
98  break;
99 
101  return vehicle_command_ack_s::VEHICLE_RESULT_ACCEPTED;
102 
103  default:
104  return vehicle_command_ack_s::VEHICLE_RESULT_DENIED;
105  }
106 
107  /* handling ARM_AUTH_IDLE */
109 
111  auth_timeout = now + (arm_parameters.struct_value.auth_method_param.auth_method_arm_timeout_msec * 1000);
113 
114  while (now < auth_timeout) {
115  arm_auth_update(now);
116 
117  if (state != ARM_AUTH_WAITING_AUTH && state != ARM_AUTH_WAITING_AUTH_WITH_ACK) {
118  break;
119  }
120 
121  /* 0.5ms */
122  px4_usleep(500);
123  now = hrt_absolute_time();
124  }
125 
126  switch (state) {
129  state = ARM_AUTH_IDLE;
130  mavlink_log_critical(mavlink_log_pub, "Arm auth: No response");
131  break;
132 
133  default:
134  break;
135  }
136 
137  return state == ARM_AUTH_MISSION_APPROVED ?
138  vehicle_command_ack_s::VEHICLE_RESULT_ACCEPTED : vehicle_command_ack_s::VEHICLE_RESULT_DENIED;
139 }
140 
142 {
143  switch (state) {
144  case ARM_AUTH_IDLE:
145  /* no authentication in process? handle bellow */
146  break;
147 
149  return vehicle_command_ack_s::VEHICLE_RESULT_ACCEPTED;
150 
153  return vehicle_command_ack_s::VEHICLE_RESULT_TEMPORARILY_REJECTED;
154 
155  default:
156  return vehicle_command_ack_s::VEHICLE_RESULT_DENIED;
157  }
158 
159  /* handling ARM_AUTH_IDLE */
161 
163  auth_timeout = now + (arm_parameters.struct_value.auth_method_param.auth_method_arm_timeout_msec * 1000);
165 
166  mavlink_log_critical(mavlink_log_pub, "Arm auth: Requesting authorization...");
167 
168  return vehicle_command_ack_s::VEHICLE_RESULT_TEMPORARILY_REJECTED;
169 }
170 
171 uint8_t arm_auth_check()
172 {
173  if (arm_parameters.struct_value.authentication_method < ARM_AUTH_METHOD_LAST) {
174  return arm_check_method[arm_parameters.struct_value.authentication_method]();
175  }
176 
177  return vehicle_command_ack_s::VEHICLE_RESULT_DENIED;
178 }
179 
180 void arm_auth_update(hrt_abstime now, bool param_update)
181 {
182  if (param_update) {
184  }
185 
186  switch (state) {
189  /* handle bellow */
190  break;
191 
193  if (now > auth_timeout) {
195  }
196 
197  return;
198 
199  case ARM_AUTH_IDLE:
200  default:
201  return;
202  }
203 
204  /*
205  * handling ARM_AUTH_WAITING_AUTH, ARM_AUTH_WAITING_AUTH_WITH_ACK
206  */
207  vehicle_command_ack_s command_ack;
208  bool updated = false;
209 
210  orb_check(command_ack_sub, &updated);
211 
212  if (updated) {
213  orb_copy(ORB_ID(vehicle_command_ack), command_ack_sub, &command_ack);
214  }
215 
216  if (updated
217  && command_ack.command == vehicle_command_s::VEHICLE_CMD_ARM_AUTHORIZATION_REQUEST
218  && command_ack.target_system == *system_id) {
219  switch (command_ack.result) {
220  case vehicle_command_ack_s::VEHICLE_RESULT_IN_PROGRESS:
222  break;
223 
224  case vehicle_command_ack_s::VEHICLE_RESULT_ACCEPTED:
226  "Arm auth: Authorized for the next %u seconds",
227  command_ack.result_param2);
229  auth_timeout = now + (command_ack.result_param2 * 1000000);
230  return;
231 
232  case vehicle_command_ack_s::VEHICLE_RESULT_TEMPORARILY_REJECTED:
233  mavlink_log_critical(mavlink_log_pub, "Arm auth: Temporarily rejected");
235  return;
236 
237  case vehicle_command_ack_s::VEHICLE_RESULT_DENIED:
238  default:
239  switch (command_ack.result_param1) {
240  case vehicle_command_ack_s::ARM_AUTH_DENIED_REASON_NONE:
241  /* Authorizer will send reason to ground station */
242  break;
243 
244  case vehicle_command_ack_s::ARM_AUTH_DENIED_REASON_INVALID_WAYPOINT:
245  mavlink_log_critical(mavlink_log_pub, "Arm auth: Denied, waypoint %i have a invalid value", command_ack.result_param2);
246  break;
247 
248  case vehicle_command_ack_s::ARM_AUTH_DENIED_REASON_TIMEOUT:
249  mavlink_log_critical(mavlink_log_pub, "Arm auth: Denied by timeout in authorizer");
250  break;
251 
252  case vehicle_command_ack_s::ARM_AUTH_DENIED_REASON_AIRSPACE_IN_USE:
253  mavlink_log_critical(mavlink_log_pub, "Arm auth: Denied because airspace is in use");
254  break;
255 
256  case vehicle_command_ack_s::ARM_AUTH_DENIED_REASON_BAD_WEATHER:
257  mavlink_log_critical(mavlink_log_pub, "Arm auth: Denied because of bad weather");
258  break;
259 
260  case vehicle_command_ack_s::ARM_AUTH_DENIED_REASON_GENERIC:
261  default:
262  mavlink_log_critical(mavlink_log_pub, "Arm auth: Denied");
263  }
264 
266  return;
267  }
268  }
269 
270  if (now > auth_timeout) {
271  mavlink_log_critical(mavlink_log_pub, "Arm auth: No response");
273  }
274 }
275 
276 void arm_auth_init(orb_advert_t *mav_log_pub, uint8_t *sys_id)
277 {
278  system_id = sys_id;
279  param_arm_parameters = param_find("COM_ARM_AUTH");
280  command_ack_sub = orb_subscribe(ORB_ID(vehicle_command_ack));
281  mavlink_log_pub = mav_log_pub;
282 }
283 
285 {
286  return (enum arm_auth_methods) arm_parameters.struct_value.authentication_method;
287 }
int32_t param_value
static orb_advert_t * mavlink_log_pub
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer)
Definition: uORB.cpp:90
static param_t param_arm_parameters
static enum @74 state
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
Definition: parameters.cpp:589
static hrt_abstime auth_timeout
void arm_auth_update(hrt_abstime now, bool param_update)
uint8_t arm_auth_check()
static uint8_t(* arm_check_method[ARM_AUTH_METHOD_LAST])()
Global flash based parameter store.
union @75 arm_parameters
int orb_subscribe(const struct orb_metadata *meta)
Definition: uORB.cpp:75
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
uint16_t auth_method_arm_timeout_msec
arm_auth_methods
static uint8_t _auth_method_arm_req_check()
union @75::@76::@77 auth_method_param
static void arm_auth_request_msg_send()
struct __attribute__((__packed__)) reading_msg
Definition: leddar_one.cpp:80
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
uint8_t authentication_method
__EXPORT param_t param_find(const char *name)
Look up a parameter by name.
Definition: parameters.cpp:370
void arm_auth_init(orb_advert_t *mav_log_pub, uint8_t *sys_id)
int orb_check(int handle, bool *updated)
Definition: uORB.cpp:95
static int command_ack_sub
static uint8_t * system_id
enum arm_auth_methods arm_auth_method_get()
uint8_t authorizer_system_id
uint16_t auth_method_two_arm_timeout_msec
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
uint32_t param_t
Parameter handle.
Definition: param.h:98
static uint8_t _auth_method_two_arm_check()