luat_lib_otp.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. @module otp
  3. @summary OTP操作库
  4. @version 1.0
  5. @date 2021.12.08
  6. @tag LUAT_USE_OTP
  7. @demo otp
  8. */
  9. #include "luat_base.h"
  10. #include "luat_otp.h"
  11. #define LUAT_LOG_TAG "otp"
  12. #include "luat_log.h"
  13. /*
  14. 读取指定OTP区域读取数据
  15. @api otp.read(zone, offset, len)
  16. @int 区域, 通常为0/1/2/3, 与具体硬件相关
  17. @int 偏移量
  18. @int 读取长度, 单位字节, 必须是4的倍数, 不能超过4096字节
  19. @return string 成功返回字符串, 否则返回nil
  20. @usage
  21. local otpdata = otp.read(0, 0, 64)
  22. if otpdata then
  23. log.info("otp", otpdata:toHex())
  24. end
  25. */
  26. static int l_otp_read(lua_State *L) {
  27. int zone;
  28. int offset;
  29. int len;
  30. zone = luaL_checkinteger(L, 1);
  31. offset = luaL_checkinteger(L, 2);
  32. len = luaL_checkinteger(L, 3);
  33. if (zone < 0 || zone > 16) {
  34. return 0;
  35. }
  36. if (offset < 0 || offset > 4*1024) {
  37. return 0;
  38. }
  39. if (len < 0 || len > 4*1024) {
  40. return 0;
  41. }
  42. if (offset + len > 4*1024) {
  43. return 0;
  44. }
  45. luaL_Buffer buff;
  46. luaL_buffinitsize(L, &buff, len);
  47. int ret = luat_otp_read(zone, buff.b, (size_t)offset, (size_t)len);
  48. if (ret >= 0) {
  49. lua_pushlstring(L, buff.b, ret);
  50. return 1;
  51. }
  52. else {
  53. return 0;
  54. }
  55. };
  56. /*
  57. 往指定OTP区域写入数据
  58. @api otp.write(zone, data, offset)
  59. @int 区域, 通常为0/1/2/3, 与具体硬件相关
  60. @string 数据, 长度必须是4个倍数
  61. @int 偏移量
  62. @return booL 成功返回true,否则返回false
  63. */
  64. static int l_otp_write(lua_State *L) {
  65. int zone;
  66. int offset;
  67. size_t len;
  68. const char* data;
  69. zone = luaL_checkinteger(L, 1);
  70. data = luaL_checklstring(L, 2, &len);
  71. offset = luaL_checkinteger(L, 3);
  72. if (zone < 0 || zone > 16) {
  73. return 0;
  74. }
  75. if (offset < 0 || offset > 4*1024) {
  76. return 0;
  77. }
  78. if (len > 4*1024) {
  79. return 0;
  80. }
  81. if (offset + len > 4*1024) {
  82. return 0;
  83. }
  84. int ret = luat_otp_write(zone, (char*)data, (size_t)offset, len);
  85. lua_pushboolean(L, ret == 0 ? 1 : 0);
  86. return 1;
  87. };
  88. /*
  89. 擦除指定OTP区域
  90. @api otp.erase(zone)
  91. @int 区域, 通常为0/1/2/3, 与具体硬件相关
  92. @return bool 成功返回true,否则返回false
  93. */
  94. static int l_otp_erase(lua_State *L) {
  95. int zone;
  96. zone = luaL_checkinteger(L, 1);
  97. int ret = luat_otp_erase(zone, 0, 1024);
  98. lua_pushboolean(L, ret == 0 ? 1 : 0);
  99. return 1;
  100. };
  101. /*
  102. 锁定OTP区域. 特别注意!!一旦加锁即无法解锁,OTP变成只读!!!
  103. @api otp.lock(zone)
  104. @return bool 成功返回true,否则返回false
  105. */
  106. static int l_otp_lock(lua_State *L) {
  107. int ret = luat_otp_lock(0);
  108. lua_pushboolean(L, ret == 0 ? 1 : 0);
  109. return 1;
  110. };
  111. #include "rotable2.h"
  112. static const rotable_Reg_t reg_otp[] =
  113. {
  114. {"read", ROREG_FUNC(l_otp_read)},
  115. {"write", ROREG_FUNC(l_otp_write)},
  116. {"erase", ROREG_FUNC(l_otp_erase)},
  117. {"lock", ROREG_FUNC(l_otp_lock)},
  118. { NULL, ROREG_INT(0) }
  119. };
  120. LUAMOD_API int luaopen_otp( lua_State *L ) {
  121. luat_newlib2(L, reg_otp);
  122. return 1;
  123. }