Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

121 lines
3.6 KiB

  1. /*-
  2. * Copyright 2022 John-Mark Gurney.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. * SUCH DAMAGE.
  24. *
  25. */
  26. #include <stdbool.h>
  27. #include <stdint.h>
  28. #include <stdlib.h>
  29. #include <sysinit.h>
  30. SET_DECLARE(sysinit_set, struct sysinit);
  31. void __attribute__ ((constructor))
  32. sysinit_run(void)
  33. {
  34. const int cnt = SET_COUNT(sysinit_set);
  35. uint16_t *idxarray = alloca(sizeof(uint16_t) * cnt);
  36. #if defined(VERBOSE_SYSINIT)
  37. uint16_t last;
  38. bool verbose;
  39. #endif
  40. int i;
  41. for (i = 0; i < cnt; i++)
  42. idxarray[i] = i;
  43. /*
  44. * following mostly copied from FreeBSD sys/kern/init_main.c
  45. * which has license:
  46. * SPDX-License-Identifier: BSD-4-Clause
  47. *
  48. * Copyright (c) 1995 Terrence R. Lambert
  49. * All rights reserved.
  50. *
  51. * Copyright (c) 1982, 1986, 1989, 1991, 1992, 1993
  52. * The Regents of the University of California. All rights reserved.
  53. * (c) UNIX System Laboratories, Inc.
  54. *
  55. * as copying this to RAM might be expensive, use a stack
  56. * allocated indirection array to do the sorting instead.
  57. */
  58. uint16_t *sipp;
  59. uint16_t *xipp;
  60. uint16_t save;
  61. /*
  62. * Perform a bubble sort of the system initialization objects by
  63. * their subsystem (primary key) and order (secondary key).
  64. */
  65. #define GI(x) (SET_BEGIN(sysinit_set)[(x)])
  66. #define GI_SUBSYSORDER(x) (GI((x))->si_subsys_order)
  67. #define GI_SUBSYS(x) (GET_SUBSYS(GI_SUBSYSORDER(x)))
  68. #define GI_ORDER(x) (GET_ORDER(GI_SUBSYSORDER(x)))
  69. for (sipp = &idxarray[0]; sipp < &idxarray[cnt]; sipp++) {
  70. for (xipp = sipp + 1; xipp < &idxarray[cnt]; xipp++) {
  71. if (GI_SUBSYS(*sipp) < GI_SUBSYS(*xipp) ||
  72. (GI_SUBSYS(*sipp) == GI_SUBSYS(*xipp) &&
  73. GI_ORDER(*sipp) <= GI_ORDER(*xipp)))
  74. continue; /* skip*/
  75. save = *sipp;
  76. *sipp = *xipp;
  77. *xipp = save;
  78. }
  79. }
  80. #if defined(VERBOSE_SYSINIT)
  81. last = SI_SUB_COPYRIGHT;
  82. verbose = 0;
  83. #endif
  84. /*
  85. * Traverse the (now) ordered list of system initialization tasks.
  86. * Perform each task, and continue on to the next task.
  87. */
  88. for (sipp = &idxarray[0]; sipp < &idxarray[cnt]; sipp++) {
  89. if (GI_SUBSYS(*sipp) == SI_SUB_DUMMY)
  90. continue; /* skip dummy task(s)*/
  91. #if defined(VERBOSE_SYSINIT)
  92. if (GI_SUBSYS(*sipp) > last && verbose_sysinit != 0) {
  93. verbose = 1;
  94. last = GI_SUBSYS(*sipp);
  95. printf("subsystem %x\n", last);
  96. }
  97. if (verbose) {
  98. printf(" %p(%p)... ", GI(*sipp)->func,
  99. GI(*sipp)->udata);
  100. }
  101. #endif
  102. /* Call function */
  103. (GI(*sipp)->si_func)(GI(*sipp)->si_udata);
  104. #if defined(VERBOSE_SYSINIT)
  105. if (verbose)
  106. printf("done.\n");
  107. #endif
  108. }
  109. }