Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | /* * Copyright (c) 2019 Peter Bigot Consulting, LLC * * SPDX-License-Identifier: Apache-2.0 */ #include <kernel.h> #include <sys/onoff.h> #include <syscall_handler.h> #define CLIENT_NOTIFY_METHOD_MASK 0x03 #define CLIENT_VALID_FLAGS_MASK 0x07 #define SERVICE_CONFIG_FLAGS \ (ONOFF_SERVICE_START_SLEEPS \ | ONOFF_SERVICE_STOP_SLEEPS \ | ONOFF_SERVICE_RESET_SLEEPS) #define SERVICE_REFS_MAX UINT16_MAX #define SERVICE_STATE_OFF 0 #define SERVICE_STATE_ON ONOFF_SERVICE_INTERNAL_BASE #define SERVICE_STATE_TRANSITION (ONOFF_SERVICE_INTERNAL_BASE << 1) #define SERVICE_STATE_TO_ON (SERVICE_STATE_TRANSITION | SERVICE_STATE_ON) #define SERVICE_STATE_TO_OFF (SERVICE_STATE_TRANSITION | SERVICE_STATE_OFF) #define SERVICE_STATE_MASK (SERVICE_STATE_ON | SERVICE_STATE_TRANSITION) static void set_service_state(struct onoff_service *srv, u32_t state) { srv->flags &= ~SERVICE_STATE_MASK; srv->flags |= (state & SERVICE_STATE_MASK); } static int validate_args(const struct onoff_service *srv, struct onoff_client *cli) { if ((srv == NULL) || (cli == NULL)) { return -EINVAL; } int rv = 0; u32_t mode = cli->flags; /* Reject unexpected flags. */ if (mode != (cli->flags & CLIENT_VALID_FLAGS_MASK)) { return -EINVAL; } /* Validate configuration based on mode */ switch (mode & CLIENT_NOTIFY_METHOD_MASK) { case ONOFF_CLIENT_NOTIFY_SPINWAIT: break; case ONOFF_CLIENT_NOTIFY_CALLBACK: if (cli->async.callback.handler == NULL) { rv = -EINVAL; } break; case ONOFF_CLIENT_NOTIFY_SIGNAL: if (cli->async.signal == NULL) { rv = -EINVAL; } break; default: rv = -EINVAL; break; } /* Clear the result here instead of in all callers. */ if (rv == 0) { cli->result = 0; } return rv; } int onoff_service_init(struct onoff_service *srv, onoff_service_transition_fn start, onoff_service_transition_fn stop, onoff_service_transition_fn reset, u32_t flags) { if ((flags & SERVICE_CONFIG_FLAGS) != flags) { return -EINVAL; } if ((start == NULL) || (stop == NULL)) { return -EINVAL; } *srv = (struct onoff_service)ONOFF_SERVICE_INITIALIZER(start, stop, reset, flags); return 0; } static void notify_one(struct onoff_service *srv, struct onoff_client *cli, int res) { unsigned int flags = cli->flags; /* Store the result, and notify if requested. */ cli->result = res; cli->flags = 0; switch (flags & CLIENT_NOTIFY_METHOD_MASK) { case ONOFF_CLIENT_NOTIFY_SPINWAIT: break; case ONOFF_CLIENT_NOTIFY_CALLBACK: cli->async.callback.handler(srv, cli, cli->async.callback.user_data, res); break; #ifdef CONFIG_POLL case ONOFF_CLIENT_NOTIFY_SIGNAL: k_poll_signal_raise(cli->async.signal, res); break; #endif /* CONFIG_POLL */ default: __ASSERT_NO_MSG(false); } } static void notify_all(struct onoff_service *srv, sys_slist_t *list, int res) { while (!sys_slist_is_empty(list)) { sys_snode_t *node = sys_slist_get_not_empty(list); struct onoff_client *cli = CONTAINER_OF(node, struct onoff_client, node); notify_one(srv, cli, res); } } static void onoff_start_notify(struct onoff_service *srv, int res) { k_spinlock_key_t key = k_spin_lock(&srv->lock); sys_slist_t clients = srv->clients; /* Can't have a queued releaser during start */ __ASSERT_NO_MSG(srv->releaser == NULL); /* If the start failed log an error and leave the rest of the * state in place for diagnostics. * * If the start succeeded record a reference for all clients * and set the state to ON. There must be at least one client * left to receive the result. * * In either case reset the client queue and notify all * clients of operation completion. */ if (res < 0) { srv->flags &= ~SERVICE_STATE_TRANSITION; srv->flags |= ONOFF_SERVICE_HAS_ERROR; } else { sys_snode_t *node; unsigned int refs = 0U; set_service_state(srv, SERVICE_STATE_ON); SYS_SLIST_FOR_EACH_NODE(&clients, node) { refs += 1U; } /* Update the reference count, or fail if the count * would overflow. */ if (srv->refs > (SERVICE_REFS_MAX - refs)) { srv->flags |= ONOFF_SERVICE_HAS_ERROR; } else { srv->refs += refs; } __ASSERT_NO_MSG(srv->refs > 0U); } sys_slist_init(&srv->clients); k_spin_unlock(&srv->lock, key); notify_all(srv, &clients, res); } int onoff_request(struct onoff_service *srv, struct onoff_client *cli) { bool add_client = false; /* add client to pending list */ bool start = false; /* invoke start transition */ bool notify = false; /* do client notification */ int rv = validate_args(srv, cli); if (rv < 0) { return rv; } k_spinlock_key_t key = k_spin_lock(&srv->lock); if ((srv->flags & ONOFF_SERVICE_HAS_ERROR) != 0) { rv = -EIO; goto out; } /* Reject if this would overflow the reference count. */ if (srv->refs == SERVICE_REFS_MAX) { rv = -EAGAIN; goto out; } u32_t state = srv->flags & SERVICE_STATE_MASK; switch (state) { case SERVICE_STATE_TO_OFF: /* Queue to start after release */ __ASSERT_NO_MSG(srv->releaser != NULL); add_client = true; rv = 3; break; case SERVICE_STATE_OFF: /* Reject if in a non-thread context and start could * wait. */ if ((k_is_in_isr() || k_is_pre_kernel()) && ((srv->flags & ONOFF_SERVICE_START_SLEEPS) != 0U)) { rv = -EWOULDBLOCK; break; } /* Start with first request while off */ __ASSERT_NO_MSG(srv->refs == 0); set_service_state(srv, SERVICE_STATE_TO_ON); start = true; add_client = true; rv = 2; break; case SERVICE_STATE_TO_ON: /* Already starting, just queue it */ add_client = true; rv = 1; break; case SERVICE_STATE_ON: /* Just increment the reference count */ notify = true; break; default: rv = -EINVAL; break; } out: if (add_client) { sys_slist_append(&srv->clients, &cli->node); } else if (notify) { srv->refs += 1; } k_spin_unlock(&srv->lock, key); if (start) { __ASSERT_NO_MSG(srv->start != NULL); srv->start(srv, onoff_start_notify); } else if (notify) { notify_one(srv, cli, 0); } return rv; } static void onoff_stop_notify(struct onoff_service *srv, int res) { bool notify_clients = false; int client_res = res; bool start = false; k_spinlock_key_t key = k_spin_lock(&srv->lock); sys_slist_t clients = srv->clients; struct onoff_client *releaser = srv->releaser; /* If the stop operation failed log an error and leave the * rest of the state in place. * * If it succeeded remove the last reference and transition to * off. * * In either case remove the last reference, and notify all * waiting clients of operation completion. */ if (res < 0) { srv->flags &= ~SERVICE_STATE_TRANSITION; srv->flags |= ONOFF_SERVICE_HAS_ERROR; notify_clients = true; } else if (sys_slist_is_empty(&clients)) { set_service_state(srv, SERVICE_STATE_OFF); } else if ((k_is_in_isr() || k_is_pre_kernel()) && ((srv->flags & ONOFF_SERVICE_START_SLEEPS) != 0U)) { set_service_state(srv, SERVICE_STATE_OFF); notify_clients = true; client_res = -EWOULDBLOCK; } else { set_service_state(srv, SERVICE_STATE_TO_ON); start = true; } __ASSERT_NO_MSG(releaser); srv->refs -= 1U; srv->releaser = NULL; __ASSERT_NO_MSG(srv->refs == 0); /* Remove the clients if there was an error or a delayed start * couldn't be initiated, because we're resolving their * operation with an error. */ if (notify_clients) { sys_slist_init(&srv->clients); } k_spin_unlock(&srv->lock, key); /* Notify the releaser. If there was an error, notify any * pending requests; otherwise if there are pending requests * start the transition to ON. */ notify_one(srv, releaser, res); if (notify_clients) { notify_all(srv, &clients, client_res); } else if (start) { srv->start(srv, onoff_start_notify); } } int onoff_release(struct onoff_service *srv, struct onoff_client *cli) { bool stop = false; /* invoke stop transition */ bool notify = false; /* do client notification */ int rv = validate_args(srv, cli); if (rv < 0) { return rv; } k_spinlock_key_t key = k_spin_lock(&srv->lock); if ((srv->flags & ONOFF_SERVICE_HAS_ERROR) != 0) { rv = -EIO; goto out; } u32_t state = srv->flags & SERVICE_STATE_MASK; switch (state) { case SERVICE_STATE_ON: /* Stay on if release leaves a client. */ if (srv->refs > 1U) { notify = true; rv = 1; break; } /* Reject if in non-thread context but stop could * wait */ if ((k_is_in_isr() || k_is_pre_kernel()) && ((srv->flags & ONOFF_SERVICE_STOP_SLEEPS) != 0)) { rv = -EWOULDBLOCK; break; } stop = true; set_service_state(srv, SERVICE_STATE_TO_OFF); srv->releaser = cli; rv = 2; break; case SERVICE_STATE_TO_ON: rv = -EBUSY; break; case SERVICE_STATE_OFF: case SERVICE_STATE_TO_OFF: rv = -EALREADY; break; default: rv = -EINVAL; } out: if (notify) { srv->refs -= 1U; } k_spin_unlock(&srv->lock, key); if (stop) { __ASSERT_NO_MSG(srv->stop != NULL); srv->stop(srv, onoff_stop_notify); } else if (notify) { notify_one(srv, cli, 0); } return rv; } static void onoff_reset_notify(struct onoff_service *srv, int res) { k_spinlock_key_t key = k_spin_lock(&srv->lock); sys_slist_t clients = srv->clients; /* If the reset failed clear the transition flag but otherwise * leave the state unchanged. * * If it was successful clear the reference count and all * flags except capability flags (sets to SERVICE_STATE_OFF). */ if (res < 0) { srv->flags &= ~SERVICE_STATE_TRANSITION; } else { __ASSERT_NO_MSG(srv->refs == 0U); srv->refs = 0U; srv->flags &= SERVICE_CONFIG_FLAGS; } sys_slist_init(&srv->clients); k_spin_unlock(&srv->lock, key); notify_all(srv, &clients, res); } int onoff_service_reset(struct onoff_service *srv, struct onoff_client *cli) { if (srv->reset == NULL) { return -ENOTSUP; } bool reset = false; int rv = validate_args(srv, cli); if (rv < 0) { return rv; } /* Reject if in a non-thread context and reset could wait. */ if ((k_is_in_isr() || k_is_pre_kernel()) && ((srv->flags & ONOFF_SERVICE_RESET_SLEEPS) != 0U)) { return -EWOULDBLOCK; } k_spinlock_key_t key = k_spin_lock(&srv->lock); if ((srv->flags & ONOFF_SERVICE_HAS_ERROR) == 0) { rv = -EALREADY; goto out; } if ((srv->flags & SERVICE_STATE_TRANSITION) == 0) { reset = true; srv->flags |= SERVICE_STATE_TRANSITION; } out: if (rv >= 0) { sys_slist_append(&srv->clients, &cli->node); } k_spin_unlock(&srv->lock, key); if (reset) { srv->reset(srv, onoff_reset_notify); } return rv; } int onoff_cancel(struct onoff_service *srv, struct onoff_client *cli) { int rv = validate_args(srv, cli); if (rv < 0) { return rv; } rv = -EALREADY; k_spinlock_key_t key = k_spin_lock(&srv->lock); u32_t state = srv->flags & SERVICE_STATE_MASK; /* Can't remove the last client waiting for the in-progress * transition, as there would be nobody to receive the * completion notification, which might indicate a service * error. */ if (sys_slist_find_and_remove(&srv->clients, &cli->node)) { rv = 0; if (sys_slist_is_empty(&srv->clients) && (state != SERVICE_STATE_TO_OFF)) { rv = -EWOULDBLOCK; sys_slist_append(&srv->clients, &cli->node); } } else if (srv->releaser == cli) { /* must be waiting for TO_OFF to complete */ rv = -EWOULDBLOCK; } k_spin_unlock(&srv->lock, key); if (rv == 0) { notify_one(srv, cli, -ECANCELED); } return rv; } |