#include #include #include #include #include #include #include #include #include #include #include #include "alipay_third.h" #include "alipay_common.h" #include "wxpay_common.h" #include "alipay_rom.h" #include "vendor_ble_alipay.h" #include "vendor_ble_wxpay.h" #define TIMEZONE 8 //时区配置:BEIJIN #define ALIPAY_STORE_SIZE 0x12000 //NOR存储空间大小 #define ALIPAY_HEAP_SIZE 0x2000 //堆空间大小 unsigned int ALIPAY_STORE_START=0x1C0000;//NOR存储空间起始地址(大小为0x12000) const unsigned int PAYLIB_LCD_WIDTH=360;//实际屏幕大小宽度 const unsigned int PAYLIB_LCD_HIGH=360;//实际屏幕大小高度 const unsigned int ALIPAY_BOND_CODE_SIDE=4;//支付宝绑定码放大倍数 const unsigned int ALIPAY_PAY_CODE_SIDE=7;//支付宝付款码放大倍数 const unsigned int WXPAY_BOND_CODE_SIDE=4;//微信绑定码放大倍数 const unsigned int WXPAY_PAY_CODE_SIDE=7;//微信付款码放大倍数 static const struct device *flash_dev = NULL;//SPINOR设备驱动 static const struct device *rtc_dev = NULL;//RTC设备驱动 static struct k_delayed_work alipay_work;//实现alipay定时调用 static struct k_delayed_work wxpay_work;//实现wxpay定时调用 /********************** 蓝牙从机通过支付宝profile向主机发送数据,该接口会由lib库直接调用。 内部已实现队列发送,用户只需要实现ble发送功能。 ***********************/ void alipay_slave_send_noti_data(unsigned char buf[],unsigned char len) { vendor_ble_alipay_write(buf,len); } /***************************************** 定时器超时触发的回调 该接口会由lib库直接调用。 alipay_send_ble_data_proc 为lib中的发送数据处理函数。 ******************************************/ void alipay_send_ble_timeout(void) { alipay_send_ble_data_proc(); } /************************ //定时器启动接口,该接口会由lib库直接调用。 用户需实现启动定时器功能,定时器设定为单次,时间间隔为30~50ms **************************/ void alipay_send_ble_timer_start(void) { printk("wxpay_send_ble_timer_start\r\n"); k_delayed_work_submit(&alipay_work, K_MSEC(50)); } /********************** 蓝牙从机通过微信profile向主机发送数据,该接口会由lib库直接调用。 内部已实现队列发送,用户只需要实现ble发送功能。 ***********************/ void wxpay_slave_send_noti_data(unsigned char buf[],unsigned char len) { vendor_ble_wxpay_write(buf,len); } /***************************************** 定时器超时触发的回调 该接口会由lib库直接调用。 wxpay_send_ble_data_proc 为lib中的发送数据处理函数。 ******************************************/ void wxpay_send_ble_timeout(void) { wxpay_send_ble_data_proc(); } /************************ //定时器启动接口,该接口会由lib库直接调用。 用户需实现启动定时器功能,定时器设定为单次,时间间隔为30~50ms **************************/ void wxpay_send_ble_timer_start(void) { printk("wxpay_send_ble_timer_start\r\n"); k_delayed_work_submit(&wxpay_work, K_MSEC(50)); } /*************************************************** //实现获取mac地址 研究下mac地址显示方向传入的数值需和手机端显示一致 mac_addr:lib库会通过此数组获取6字节mac 地址 *****************************************************/ void third_paylib_get_mac_addr(unsigned char mac_addr[]) { int ret = 0; bt_addr_le_t addr; uint8_t *mac = addr.a.val; ret = bt_manager_get_ble_mac(&addr); if (!ret) { mac_addr[0] = mac[5]; mac_addr[1] = mac[4]; mac_addr[2] = mac[3]; mac_addr[3] = mac[2]; mac_addr[4] = mac[1]; mac_addr[5] = mac[0]; } } /************************ //实现获取unix时间搓 ***************************/ unsigned int third_paylib_get_unix_second(void) { unsigned int unix_second = 0; struct rtc_time rtc_time = {0}; if (rtc_dev) { rtc_get_time(rtc_dev, &rtc_time); rtc_tm_to_time(&rtc_time, &unix_second); unix_second = unix_second - TIMEZONE * 3600; // convert to UTC } return unix_second; } /*************************** //实现128bit的随机数 建议使用强伪随机数或真随机数 rand_buf:随机数值 lib库会通过此数组获取16字节随机数 len:固定返回16字节 ************************************/ void third_rand_get_value(unsigned char rand_buf[],unsigned char *len) { int *pbuf = (int*)rand_buf; int idx; srand(sys_read32(T2_CNT)); for (idx = 0; idx < 4; idx ++) { pbuf[idx] = rand(); } *len=16; } /*************************** //实现flash读函数,注意根据实际外部flash配置修改头文件中的ALIPAY_STORE_START 宏!!!!!!!!! lib库会根据ALIPAY_STORE_START 向外部flash申请0x12000大小的存储空间。该空间不能被其他程序使用 addr:为flash读取地址 read_data:flash读取数据数组. len:flash读取数据长度 ************************************/ void third_paylib_flash_read_data(unsigned int addr,unsigned char read_data[],unsigned short len) { //printk("third_paylib_flash_read_data 0x%x %d\r\n", addr, len); if (flash_dev) { flash_read(flash_dev, addr, read_data, len); } } /*************************** //实现flash写函数,注意根据实际外部flash配置修改头文件中的ALIPAY_STORE_START 宏!!!!!!!!! lib库会根据ALIPAY_STORE_START 向外部flash申请0x12000大小的存储空间。该空间不能被其他程序使用 addr:为flash写入地址 write_data:flash写入数据数组. len:flash写入数据长度 ************************************/ void third_paylib_flash_write_data(unsigned int addr,unsigned char write_data[],unsigned short len) { //printk("third_paylib_flash_write_data 0x%x %d\r\n", addr, len); if (flash_dev) { flash_write(flash_dev, addr, write_data, len); } } /*************************** //实现flash擦除函数,擦除一页0x1000大小 注意根据实际外部flash配置修改头文件中的ALIPAY_STORE_START 宏!!!!!!!!! lib库会根据ALIPAY_STORE_START 向外部flash申请0x12000大小的存储空间。该空间不能被其他程序使用 ************************************/ void third_paylib_flash_erase_page(unsigned int addr) { unsigned int head; if (flash_dev) { flash_read(flash_dev, addr, &head, sizeof(head)); if (head != 0xffffffff) { printk("third_paylib_flash_erase_page 0x%x\r\n", addr); flash_erase(flash_dev, addr, 0x1000); } } } /**************** 动态申请内存 *****************/ unsigned char * third_wxcodepay_malloc(size_t size) { unsigned char *ptr = (unsigned char *)malloc(size); //printk("third_wxcodepay_malloc %d 0x%x\r\n", size, (unsigned int)ptr); return ptr; } /**************** * @brief 动态内存重分配接口 * @note 注意需要和标准库实现一致,即realloc不影响原部分内存 *****************/ unsigned char * third_wxcodepay_realloc(void* buf, size_t new_size) { unsigned char *ptr = (unsigned char *)realloc(buf, new_size); //printk("third_wxcodepay_realloc %d 0x%x\r\n", new_size, (unsigned int)ptr); return ptr; } /**************** 动态内存释放接口 *****************/ void third_wxcodepay_free(void* buf) { //printk("third_wxcodepay_free 0x%x\r\n", (unsigned int)buf); free(buf); } /**************** 支付宝打印调试 ***********************/ void third_alipay_printf(const char *format, int32_t value) { printk(format, value); } /**************** 微信打印调试 ***********************/ void third_wxpay_printf(const char *format, int32_t value) { printk(format, value); } #ifdef CONFIG_ALIPAY_WXPAY_ROM uint32_t third_paylib_flash_get_start(void) { return ALIPAY_STORE_START; } const static alipay_os_api_t alipay_os_api = { .alipay_slave_write = alipay_slave_send_noti_data, .alipay_ble_timer_start = alipay_send_ble_timer_start, .wxpay_slave_write = wxpay_slave_send_noti_data, .wxpay_ble_timer_start = wxpay_send_ble_timer_start, .get_mac_addr = third_paylib_get_mac_addr, .get_unix_second = third_paylib_get_unix_second, .rand_get_value = third_rand_get_value, .flash_get_start = third_paylib_flash_get_start, .flash_read_data = third_paylib_flash_read_data, .flash_write_data = third_paylib_flash_write_data, .flash_erase_page = third_paylib_flash_erase_page, .printf = third_alipay_printf, }; #endif /**************** 设备初始化 ***********************/ static int third_sys_init(const struct device *dev) { const struct partition_entry *user_part; ARG_UNUSED(dev); user_part = partition_get_part(PARTITION_FILE_ID_IMAGE_SCRATCH); if (user_part) { if (user_part->size < ALIPAY_STORE_SIZE) { LOG_ERR("paylib partition too small!"); } else { ALIPAY_STORE_START = user_part->offset; LOG_INF("paylib partition: 0x%x (0x%x)", user_part->offset, user_part->size); } } else { LOG_ERR("paylib partition NOT find!"); } flash_dev = device_get_binding(CONFIG_SPI_FLASH_NAME); if (flash_dev == NULL) { LOG_ERR("Could not get %s device\n", CONFIG_SPI_FLASH_NAME); } rtc_dev = device_get_binding(CONFIG_RTC_0_NAME); if (rtc_dev == NULL) { LOG_ERR("Could not get %s device\n", CONFIG_RTC_0_NAME); } k_delayed_work_init(&alipay_work, (k_work_handler_t)alipay_send_ble_timeout); k_delayed_work_init(&wxpay_work, (k_work_handler_t)wxpay_send_ble_timeout); #ifdef CONFIG_ALIPAY_WXPAY_ROM alipay_rom_init(&alipay_os_api); #endif return 0; } SYS_INIT(third_sys_init, POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY);