rt_random.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2011-2023, Shanghai Real-Thread Electronic Technology Co.,Ltd
  3. *
  4. * Change Logs:
  5. * Date Author Notes
  6. * 2020-12-18 quanzhao the first version
  7. */
  8. #include <time.h>
  9. #include <string.h>
  10. #include <rtthread.h>
  11. static struct rt_device random_dev;
  12. static unsigned long seed;
  13. static rt_uint16_t calc_random(void)
  14. {
  15. seed = 214013L * seed + 2531011L;
  16. return (seed >> 16) & 0x7FFF; /* return bits 16~30 */
  17. }
  18. static rt_ssize_t random_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  19. {
  20. rt_uint16_t rand;
  21. ssize_t ret = 0;
  22. while (size >= sizeof(rand))
  23. {
  24. /* update rand */
  25. rand = calc_random();
  26. *(rt_uint16_t *)buffer = rand;
  27. buffer = (char *)buffer + sizeof(rand);
  28. ret += sizeof(rand);
  29. size -= sizeof(rand);
  30. }
  31. if (size)
  32. {
  33. rand = calc_random();
  34. memcpy(buffer, &rand, size);
  35. ret += size;
  36. }
  37. return ret;
  38. }
  39. static rt_ssize_t random_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  40. {
  41. ssize_t ret = sizeof(seed) < size ? sizeof(seed) : size;
  42. rt_memcpy(&seed, buffer, ret);
  43. return ret;
  44. }
  45. static rt_err_t random_control(rt_device_t dev, int cmd, void *args)
  46. {
  47. return RT_EOK;
  48. }
  49. #ifdef RT_USING_DEVICE_OPS
  50. const static struct rt_device_ops random_ops =
  51. {
  52. RT_NULL,
  53. RT_NULL,
  54. RT_NULL,
  55. random_read,
  56. random_write,
  57. random_control
  58. };
  59. #endif
  60. int random_device_init(void)
  61. {
  62. static rt_bool_t init_ok = RT_FALSE;
  63. if (init_ok)
  64. {
  65. return 0;
  66. }
  67. RT_ASSERT(!rt_device_find("random"));
  68. random_dev.type = RT_Device_Class_Miscellaneous;
  69. #ifdef RT_USING_DEVICE_OPS
  70. random_dev.ops = &random_ops;
  71. #else
  72. random_dev.init = RT_NULL;
  73. random_dev.open = RT_NULL;
  74. random_dev.close = RT_NULL;
  75. random_dev.read = random_read;
  76. random_dev.write = random_write;
  77. random_dev.control = random_control;
  78. #endif
  79. /* no private */
  80. random_dev.user_data = RT_NULL;
  81. rt_device_register(&random_dev, "random", RT_DEVICE_FLAG_RDWR);
  82. init_ok = RT_TRUE;
  83. return 0;
  84. }
  85. INIT_DEVICE_EXPORT(random_device_init);
  86. static struct rt_device urandom_dev;
  87. static unsigned long useed;
  88. static rt_uint16_t calc_urandom(void)
  89. {
  90. useed = 214013L * useed + 2531011L;
  91. return (useed >> 16) & 0x7FFF; /* return bits 16~30 */
  92. }
  93. static rt_ssize_t random_uread(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  94. {
  95. rt_uint16_t rand;
  96. ssize_t ret = 0;
  97. while (size >= sizeof(rand))
  98. {
  99. /* update rand */
  100. rand = calc_urandom();
  101. *(rt_uint16_t *)buffer = rand;
  102. buffer = (char *)buffer + sizeof(rand);
  103. ret += sizeof(rand);
  104. size -= sizeof(rand);
  105. }
  106. if (size)
  107. {
  108. rand = calc_urandom();
  109. memcpy(buffer, &rand, size);
  110. ret += size;
  111. }
  112. return ret;
  113. }
  114. static rt_ssize_t random_uwrite(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  115. {
  116. ssize_t ret = sizeof(useed) < size ? sizeof(useed) : size;
  117. rt_memcpy(&useed, buffer, ret);
  118. return ret;
  119. }
  120. static rt_err_t random_ucontrol(rt_device_t dev, int cmd, void *args)
  121. {
  122. return RT_EOK;
  123. }
  124. #ifdef RT_USING_DEVICE_OPS
  125. const static struct rt_device_ops urandom_ops =
  126. {
  127. RT_NULL,
  128. RT_NULL,
  129. RT_NULL,
  130. random_uread,
  131. random_uwrite,
  132. random_ucontrol
  133. };
  134. #endif
  135. int urandom_device_init(void)
  136. {
  137. static rt_bool_t init_ok = RT_FALSE;
  138. if (init_ok)
  139. {
  140. return 0;
  141. }
  142. RT_ASSERT(!rt_device_find("urandom"));
  143. urandom_dev.type = RT_Device_Class_Miscellaneous;
  144. #ifdef RT_USING_DEVICE_OPS
  145. urandom_dev.ops = &urandom_ops;
  146. #else
  147. urandom_dev.init = RT_NULL;
  148. urandom_dev.open = RT_NULL;
  149. urandom_dev.close = RT_NULL;
  150. urandom_dev.read = random_uread;
  151. urandom_dev.write = random_uwrite;
  152. urandom_dev.control = random_ucontrol;
  153. #endif
  154. /* no private */
  155. urandom_dev.user_data = RT_NULL;
  156. rt_device_register(&urandom_dev, "urandom", RT_DEVICE_FLAG_RDWR);
  157. init_ok = RT_TRUE;
  158. return 0;
  159. }
  160. INIT_DEVICE_EXPORT(urandom_device_init);