alipay_third.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <soc.h>
  5. #include <zephyr.h>
  6. #include <board.h>
  7. #include <drivers/rtc.h>
  8. #include <drivers/flash.h>
  9. #include <sys/sys_heap.h>
  10. #include <bt_manager.h>
  11. #include <partition/partition.h>
  12. #include "alipay_third.h"
  13. #include "alipay_common.h"
  14. #include "wxpay_common.h"
  15. #include "alipay_rom.h"
  16. #include "vendor_ble_alipay.h"
  17. #include "vendor_ble_wxpay.h"
  18. #define TIMEZONE 8 //时区配置:BEIJIN
  19. #define ALIPAY_STORE_SIZE 0x12000 //NOR存储空间大小
  20. #define ALIPAY_HEAP_SIZE 0x2000 //堆空间大小
  21. unsigned int ALIPAY_STORE_START=0x1C0000;//NOR存储空间起始地址(大小为0x12000)
  22. const unsigned int PAYLIB_LCD_WIDTH=360;//实际屏幕大小宽度
  23. const unsigned int PAYLIB_LCD_HIGH=360;//实际屏幕大小高度
  24. const unsigned int ALIPAY_BOND_CODE_SIDE=4;//支付宝绑定码放大倍数
  25. const unsigned int ALIPAY_PAY_CODE_SIDE=7;//支付宝付款码放大倍数
  26. const unsigned int WXPAY_BOND_CODE_SIDE=4;//微信绑定码放大倍数
  27. const unsigned int WXPAY_PAY_CODE_SIDE=7;//微信付款码放大倍数
  28. static const struct device *flash_dev = NULL;//SPINOR设备驱动
  29. static const struct device *rtc_dev = NULL;//RTC设备驱动
  30. static struct k_delayed_work alipay_work;//实现alipay定时调用
  31. static struct k_delayed_work wxpay_work;//实现wxpay定时调用
  32. /**********************
  33. 蓝牙从机通过支付宝profile向主机发送数据,该接口会由lib库直接调用。
  34. 内部已实现队列发送,用户只需要实现ble发送功能。
  35. ***********************/
  36. void alipay_slave_send_noti_data(unsigned char buf[],unsigned char len)
  37. {
  38. vendor_ble_alipay_write(buf,len);
  39. }
  40. /*****************************************
  41. 定时器超时触发的回调 该接口会由lib库直接调用。
  42. alipay_send_ble_data_proc 为lib中的发送数据处理函数。
  43. ******************************************/
  44. void alipay_send_ble_timeout(void)
  45. {
  46. alipay_send_ble_data_proc();
  47. }
  48. /************************
  49. //定时器启动接口,该接口会由lib库直接调用。
  50. 用户需实现启动定时器功能,定时器设定为单次,时间间隔为30~50ms
  51. **************************/
  52. void alipay_send_ble_timer_start(void)
  53. {
  54. printk("wxpay_send_ble_timer_start\r\n");
  55. k_delayed_work_submit(&alipay_work, K_MSEC(50));
  56. }
  57. /**********************
  58. 蓝牙从机通过微信profile向主机发送数据,该接口会由lib库直接调用。
  59. 内部已实现队列发送,用户只需要实现ble发送功能。
  60. ***********************/
  61. void wxpay_slave_send_noti_data(unsigned char buf[],unsigned char len)
  62. {
  63. vendor_ble_wxpay_write(buf,len);
  64. }
  65. /*****************************************
  66. 定时器超时触发的回调 该接口会由lib库直接调用。
  67. wxpay_send_ble_data_proc 为lib中的发送数据处理函数。
  68. ******************************************/
  69. void wxpay_send_ble_timeout(void)
  70. {
  71. wxpay_send_ble_data_proc();
  72. }
  73. /************************
  74. //定时器启动接口,该接口会由lib库直接调用。
  75. 用户需实现启动定时器功能,定时器设定为单次,时间间隔为30~50ms
  76. **************************/
  77. void wxpay_send_ble_timer_start(void)
  78. {
  79. printk("wxpay_send_ble_timer_start\r\n");
  80. k_delayed_work_submit(&wxpay_work, K_MSEC(50));
  81. }
  82. /***************************************************
  83. //实现获取mac地址 研究下mac地址显示方向传入的数值需和手机端显示一致
  84. mac_addr:lib库会通过此数组获取6字节mac 地址
  85. *****************************************************/
  86. void third_paylib_get_mac_addr(unsigned char mac_addr[])
  87. {
  88. int ret = 0;
  89. bt_addr_le_t addr;
  90. uint8_t *mac = addr.a.val;
  91. ret = bt_manager_get_ble_mac(&addr);
  92. if (!ret) {
  93. mac_addr[0] = mac[5];
  94. mac_addr[1] = mac[4];
  95. mac_addr[2] = mac[3];
  96. mac_addr[3] = mac[2];
  97. mac_addr[4] = mac[1];
  98. mac_addr[5] = mac[0];
  99. }
  100. }
  101. /************************
  102. //实现获取unix时间搓
  103. ***************************/
  104. unsigned int third_paylib_get_unix_second(void)
  105. {
  106. unsigned int unix_second = 0;
  107. struct rtc_time rtc_time = {0};
  108. if (rtc_dev) {
  109. rtc_get_time(rtc_dev, &rtc_time);
  110. rtc_tm_to_time(&rtc_time, &unix_second);
  111. unix_second = unix_second - TIMEZONE * 3600; // convert to UTC
  112. }
  113. return unix_second;
  114. }
  115. /***************************
  116. //实现128bit的随机数 建议使用强伪随机数或真随机数
  117. rand_buf:随机数值 lib库会通过此数组获取16字节随机数
  118. len:固定返回16字节
  119. ************************************/
  120. void third_rand_get_value(unsigned char rand_buf[],unsigned char *len)
  121. {
  122. int *pbuf = (int*)rand_buf;
  123. int idx;
  124. srand(sys_read32(T2_CNT));
  125. for (idx = 0; idx < 4; idx ++) {
  126. pbuf[idx] = rand();
  127. }
  128. *len=16;
  129. }
  130. /***************************
  131. //实现flash读函数,注意根据实际外部flash配置修改头文件中的ALIPAY_STORE_START 宏!!!!!!!!!
  132. lib库会根据ALIPAY_STORE_START 向外部flash申请0x12000大小的存储空间。该空间不能被其他程序使用
  133. addr:为flash读取地址
  134. read_data:flash读取数据数组.
  135. len:flash读取数据长度
  136. ************************************/
  137. void third_paylib_flash_read_data(unsigned int addr,unsigned char read_data[],unsigned short len)
  138. {
  139. //printk("third_paylib_flash_read_data 0x%x %d\r\n", addr, len);
  140. if (flash_dev) {
  141. flash_read(flash_dev, addr, read_data, len);
  142. }
  143. }
  144. /***************************
  145. //实现flash写函数,注意根据实际外部flash配置修改头文件中的ALIPAY_STORE_START 宏!!!!!!!!!
  146. lib库会根据ALIPAY_STORE_START 向外部flash申请0x12000大小的存储空间。该空间不能被其他程序使用
  147. addr:为flash写入地址
  148. write_data:flash写入数据数组.
  149. len:flash写入数据长度
  150. ************************************/
  151. void third_paylib_flash_write_data(unsigned int addr,unsigned char write_data[],unsigned short len)
  152. {
  153. //printk("third_paylib_flash_write_data 0x%x %d\r\n", addr, len);
  154. if (flash_dev) {
  155. flash_write(flash_dev, addr, write_data, len);
  156. }
  157. }
  158. /***************************
  159. //实现flash擦除函数,擦除一页0x1000大小 注意根据实际外部flash配置修改头文件中的ALIPAY_STORE_START 宏!!!!!!!!!
  160. lib库会根据ALIPAY_STORE_START 向外部flash申请0x12000大小的存储空间。该空间不能被其他程序使用
  161. ************************************/
  162. void third_paylib_flash_erase_page(unsigned int addr)
  163. {
  164. unsigned int head;
  165. if (flash_dev) {
  166. flash_read(flash_dev, addr, &head, sizeof(head));
  167. if (head != 0xffffffff) {
  168. printk("third_paylib_flash_erase_page 0x%x\r\n", addr);
  169. flash_erase(flash_dev, addr, 0x1000);
  170. }
  171. }
  172. }
  173. /****************
  174. 动态申请内存
  175. *****************/
  176. unsigned char * third_wxcodepay_malloc(size_t size)
  177. {
  178. unsigned char *ptr = (unsigned char *)malloc(size);
  179. //printk("third_wxcodepay_malloc %d 0x%x\r\n", size, (unsigned int)ptr);
  180. return ptr;
  181. }
  182. /****************
  183. * @brief 动态内存重分配接口
  184. * @note 注意需要和标准库实现一致,即realloc不影响原部分内存
  185. *****************/
  186. unsigned char * third_wxcodepay_realloc(void* buf, size_t new_size)
  187. {
  188. unsigned char *ptr = (unsigned char *)realloc(buf, new_size);
  189. //printk("third_wxcodepay_realloc %d 0x%x\r\n", new_size, (unsigned int)ptr);
  190. return ptr;
  191. }
  192. /****************
  193. 动态内存释放接口
  194. *****************/
  195. void third_wxcodepay_free(void* buf)
  196. {
  197. //printk("third_wxcodepay_free 0x%x\r\n", (unsigned int)buf);
  198. free(buf);
  199. }
  200. /****************
  201. 支付宝打印调试
  202. ***********************/
  203. void third_alipay_printf(const char *format, int32_t value)
  204. {
  205. printk(format, value);
  206. }
  207. /****************
  208. 微信打印调试
  209. ***********************/
  210. void third_wxpay_printf(const char *format, int32_t value)
  211. {
  212. printk(format, value);
  213. }
  214. #ifdef CONFIG_ALIPAY_WXPAY_ROM
  215. uint32_t third_paylib_flash_get_start(void)
  216. {
  217. return ALIPAY_STORE_START;
  218. }
  219. const static alipay_os_api_t alipay_os_api =
  220. {
  221. .alipay_slave_write = alipay_slave_send_noti_data,
  222. .alipay_ble_timer_start = alipay_send_ble_timer_start,
  223. .wxpay_slave_write = wxpay_slave_send_noti_data,
  224. .wxpay_ble_timer_start = wxpay_send_ble_timer_start,
  225. .get_mac_addr = third_paylib_get_mac_addr,
  226. .get_unix_second = third_paylib_get_unix_second,
  227. .rand_get_value = third_rand_get_value,
  228. .flash_get_start = third_paylib_flash_get_start,
  229. .flash_read_data = third_paylib_flash_read_data,
  230. .flash_write_data = third_paylib_flash_write_data,
  231. .flash_erase_page = third_paylib_flash_erase_page,
  232. .printf = third_alipay_printf,
  233. };
  234. #endif
  235. /****************
  236. 设备初始化
  237. ***********************/
  238. static int third_sys_init(const struct device *dev)
  239. {
  240. const struct partition_entry *user_part;
  241. ARG_UNUSED(dev);
  242. user_part = partition_get_part(PARTITION_FILE_ID_IMAGE_SCRATCH);
  243. if (user_part) {
  244. if (user_part->size < ALIPAY_STORE_SIZE) {
  245. LOG_ERR("paylib partition too small!");
  246. } else {
  247. ALIPAY_STORE_START = user_part->offset;
  248. LOG_INF("paylib partition: 0x%x (0x%x)", user_part->offset, user_part->size);
  249. }
  250. } else {
  251. LOG_ERR("paylib partition NOT find!");
  252. }
  253. flash_dev = device_get_binding(CONFIG_SPI_FLASH_NAME);
  254. if (flash_dev == NULL) {
  255. LOG_ERR("Could not get %s device\n", CONFIG_SPI_FLASH_NAME);
  256. }
  257. rtc_dev = device_get_binding(CONFIG_RTC_0_NAME);
  258. if (rtc_dev == NULL) {
  259. LOG_ERR("Could not get %s device\n", CONFIG_RTC_0_NAME);
  260. }
  261. k_delayed_work_init(&alipay_work, (k_work_handler_t)alipay_send_ble_timeout);
  262. k_delayed_work_init(&wxpay_work, (k_work_handler_t)wxpay_send_ble_timeout);
  263. #ifdef CONFIG_ALIPAY_WXPAY_ROM
  264. alipay_rom_init(&alipay_os_api);
  265. #endif
  266. return 0;
  267. }
  268. SYS_INIT(third_sys_init, POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY);