luat_lib_otp.c 3.1 KB

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