luat_lib_otp.c 3.0 KB

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