aem_adapter_dev.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * @FileName : aem_adapter_dev.c
  3. * @Author : AEM Team
  4. * @CreateDate : 2024/04/01 17:57:10
  5. * @Description :
  6. **/
  7. #include <string.h>
  8. #include <property_manager.h>
  9. #ifndef CONFIG_SIMULATOR
  10. #include <acts_bluetooth/host_interface.h>
  11. #include <bt_manager_ble.h>
  12. #endif
  13. #include "aem_adapter_dev.h"
  14. #include "aem_adapter_motor.h"
  15. #include "aem_log.h"
  16. #include "power_manager.h"
  17. #include "aem_app_err_if.h"
  18. #include "device_info_cfg.h"
  19. #ifdef CONFIG_SIMULATOR
  20. #include <time.h>
  21. #include <windows.h>
  22. #endif
  23. #ifdef CONFIG_RTC_ACTS
  24. #include <drivers/rtc.h>
  25. #endif
  26. #ifndef CONFIG_SIMULATOR
  27. #include <drivers/power_supply.h>
  28. #include <drivers/hrtimer.h>
  29. #endif
  30. static uint8_t s_brightness_value = 0;
  31. static void aem_disp_set_brightness(uint8_t value)
  32. {
  33. #ifndef CONFIG_SIMULATOR
  34. extern int aem_lv_display_set_brightness(uint8_t brightness);
  35. aem_lv_display_set_brightness(value);
  36. AEM_LOG_I("%s %d", __func__, value);
  37. #endif
  38. s_brightness_value = value;
  39. }
  40. static uint8_t aem_disp_get_brightness(void)
  41. {
  42. return s_brightness_value;
  43. }
  44. static int get_ble_mac_addr(uint8_t *mac, uint8_t len)
  45. {
  46. int ret;
  47. #ifndef CONFIG_SIMULATOR
  48. bt_addr_le_t addr_le;
  49. if (len != 6)
  50. {
  51. return -EINVAL;
  52. }
  53. bt_manager_get_ble_mac(&addr_le);
  54. for (int i = 0; i < 6; i++)
  55. {
  56. mac[i] = addr_le.a.val[5 - i];
  57. }
  58. #endif
  59. return 0;
  60. }
  61. static int get_sn_str(uint8_t *sn, uint8_t len)
  62. {
  63. int ret;
  64. #ifndef CONFIG_SIMULATOR
  65. if (len != 16 || NULL == sn)
  66. {
  67. return -EINVAL;
  68. }
  69. sn = "123456789abcde";
  70. #endif
  71. return 0;
  72. }
  73. #define MAC_STR_LEN (12 + 1)
  74. static int get_bt_mac_addr(uint8_t *mac, uint8_t len)
  75. {
  76. int ret = 0;
  77. char mac_str[MAC_STR_LEN];
  78. #ifndef CONFIG_SIMULATOR
  79. if (len != 6)
  80. {
  81. return -EINVAL;
  82. }
  83. ret = property_get(CFG_BT_MAC, mac_str, (MAC_STR_LEN - 1));
  84. if (ret > 0)
  85. {
  86. hex2bin(mac_str, 12, mac, len);
  87. }
  88. #endif
  89. return ret;
  90. }
  91. static uint8_t get_battery_percentage(void)
  92. {
  93. int percentage = 0;
  94. #ifndef CONFIG_SIMULATOR
  95. percentage = power_manager_get_battery_capacity();
  96. if (percentage < 0)
  97. {
  98. percentage = 0;
  99. }
  100. #endif
  101. return percentage;
  102. }
  103. static uint16_t get_battery_vol(void)
  104. {
  105. uint16_t value = 0;
  106. int vol = 0;
  107. #ifndef CONFIG_SIMULATOR
  108. vol = power_manager_get_battery_vol();
  109. if (vol < 0)
  110. {
  111. value = 0;
  112. }
  113. else
  114. {
  115. value = vol / 1000;
  116. }
  117. #endif
  118. return value;
  119. }
  120. static uint8_t get_charge_status(void)
  121. {
  122. int status = 0;
  123. #ifndef CONFIG_SIMULATOR
  124. status = power_manager_get_dc5v_status();
  125. if (status < 0)
  126. {
  127. status = 0;
  128. }
  129. #endif
  130. return status;
  131. }
  132. static uint8_t cnt_1s = 0;
  133. #ifndef CONFIG_SIMULATOR
  134. static struct hrtimer g_wait_for_set_time;
  135. static void htimer_fun(struct hrtimer *ttimer, void *expiry_fn_arg)
  136. {
  137. if (cnt_1s > 0)
  138. {
  139. cnt_1s--;
  140. }
  141. }
  142. static void htimer_wait_for_set_time(unsigned int ms)
  143. {
  144. hrtimer_init(&g_wait_for_set_time, htimer_fun, NULL);
  145. hrtimer_start(&g_wait_for_set_time, 1000*ms, 1000*ms);
  146. }
  147. #endif
  148. extern uint8_t bySetHour;
  149. extern uint16_t wSetYear;
  150. extern uint8_t bySetTimeStep;
  151. extern uint8_t read_time_data[7];
  152. static uint8_t get_rtc_time(aem_time_t *time)
  153. {
  154. uint8_t ret = AEM_OK;
  155. if (time == NULL)
  156. {
  157. return AEM_ERR_INVALID_PARAM;
  158. }
  159. #ifdef CONFIG_RTC_ACTS
  160. struct rtc_time rtc_time;
  161. const struct device *rtc = device_get_binding(CONFIG_RTC_0_NAME);
  162. if (rtc)
  163. {
  164. //rtc_get_time(rtc, &rtc_time);
  165. //time->year = rtc_time.tm_year + 1900;
  166. //time->month = rtc_time.tm_mon + 1;
  167. //time->day = rtc_time.tm_mday;
  168. //time->wday = rtc_time.tm_wday;
  169. //time->hour = rtc_time.tm_hour;
  170. //time->min = rtc_time.tm_min;
  171. //time->second = rtc_time.tm_sec;
  172. //time->ms = rtc_time.tm_ms;
  173. #ifndef CONFIG_SIMULATOR
  174. if (bySetTimeStep == 1)
  175. {
  176. cnt_1s = 255;
  177. //k_msleep(3000); //延时1.5s跳转画面,保证时间或日期能够更新
  178. bySetTimeStep = 2;
  179. }
  180. else if (bySetTimeStep == 3)
  181. {
  182. if (cnt_1s == 255)
  183. {
  184. cnt_1s = 2;
  185. //k_msleep(2000); //延时1.5s跳转画面,保证时间或日期能够更新
  186. if (hrtimer_is_running(&g_wait_for_set_time))
  187. {
  188. hrtimer_restart(&g_wait_for_set_time);
  189. }
  190. else
  191. htimer_wait_for_set_time(750);
  192. }
  193. if (cnt_1s == 0)
  194. {
  195. hrtimer_stop(&g_wait_for_set_time);
  196. if (bySetTimeStep == 3) //防止bySetTimeStep在其他地方被修改
  197. bySetTimeStep = 0;
  198. }
  199. }
  200. #endif
  201. if (bySetTimeStep == 0)
  202. {
  203. time->year = read_time_data[6] + 2000;
  204. time->month = read_time_data[5];
  205. time->wday = read_time_data[4];
  206. time->day = read_time_data[3];
  207. time->hour = read_time_data[2];
  208. time->min = read_time_data[1];
  209. time->second = read_time_data[0];
  210. }
  211. }
  212. #elif defined(CONFIG_SIMULATOR)
  213. SYSTEMTIME lt;
  214. GetLocalTime(&lt);
  215. time->year = lt.wYear;
  216. time->month = lt.wMonth;
  217. time->day = lt.wDay;
  218. time->wday = lt.wDayOfWeek;
  219. time->hour = lt.wHour;
  220. time->min = lt.wMinute;
  221. time->second = lt.wSecond;
  222. time->ms = lt.wMilliseconds;
  223. #endif /* CONFIG_RTC_ACTS */
  224. return ret;
  225. }
  226. static int set_rtc_time(aem_time_t *time)
  227. {
  228. int ret = 0;
  229. if (time == NULL)
  230. {
  231. return AEM_ERR_INVALID_PARAM;
  232. }
  233. #ifdef CONFIG_RTC_ACTS
  234. struct rtc_time rtc_time;
  235. const struct device *rtc = device_get_binding(CONFIG_RTC_0_NAME);
  236. if (rtc)
  237. {
  238. rtc_time.tm_year = time->year;
  239. rtc_time.tm_mon = time->month;
  240. rtc_time.tm_mday = time->day;
  241. rtc_time.tm_wday = time->wday;
  242. rtc_time.tm_hour = time->hour;
  243. rtc_time.tm_min = time->min;
  244. rtc_time.tm_sec = time->second;
  245. rtc_time.tm_ms = time->ms;
  246. rtc_time.tm_year -= 1900;
  247. rtc_time.tm_mon -= 1;
  248. ret = rtc_set_time(rtc, &rtc_time);
  249. if (ret)
  250. {
  251. AEM_LOG_E("set time error ret=%d", ret);
  252. }
  253. }
  254. #endif /* CONFIG_RTC_ACTS */
  255. return ret;
  256. }
  257. aem_dev_info_t get_device_info(void)
  258. {
  259. aem_dev_info_t info = { 0 };
  260. //info.device_id = DEVICE_ID;
  261. sprintf(info.device_id, "%s", DEVICE_ID);
  262. info.major_v = MAJOR_VERSION;
  263. info.minor_v = MINOR_VERSION;
  264. info.micro_v = MICRO_VERSION;
  265. sprintf(info.device_name, "%s", DEVICE_NAME);
  266. return info;
  267. }
  268. static aem_bat_charge_upate_evt_api_t s_bat_charge_upate_evt_cb = NULL;
  269. uint8_t aem_bat_charge_upate_evt_cb_register(aem_bat_charge_upate_evt_api_t cb)
  270. {
  271. s_bat_charge_upate_evt_cb = cb;
  272. return 0;
  273. }
  274. void aem_adapter_dev_update_bat_charge_event(uint16_t cmd)
  275. {
  276. AEM_LOG_I("update_bat_charge_event %d \r\n", cmd);
  277. #ifndef CONFIG_SIMULATOR
  278. switch (cmd)
  279. {
  280. case BAT_CHG_EVENT_CHARGE_START:
  281. case BAT_CHG_EVENT_CHARGE_STOP:
  282. case BAT_CHG_EVENT_CHARGE_FULL:
  283. case BAT_CHG_EVENT_VOLTAGE_CHANGE:
  284. if (s_bat_charge_upate_evt_cb != NULL)
  285. {
  286. s_bat_charge_upate_evt_cb(cmd);
  287. }
  288. break;
  289. default:
  290. break;
  291. }
  292. #endif // !CONFIG_SIMULATOR
  293. }
  294. static bool check_bat_is_lowpower(void)
  295. {
  296. #ifndef CONFIG_SIMULATOR
  297. return power_manager_check_bat_is_lowpower();
  298. #else
  299. return 0;
  300. #endif // !CONFIG_SIMULATOR
  301. }
  302. static const aem_device_ops_t s_device_ops = {
  303. .disp_set_brightness = aem_disp_set_brightness,
  304. .disp_get_brightness = aem_disp_get_brightness,
  305. .get_bt_mac_addr = get_ble_mac_addr,
  306. .get_ble_mac_addr = get_ble_mac_addr,
  307. .get_sn_str = get_sn_str,
  308. .get_battery_percentage = get_battery_percentage,
  309. .get_battery_vol = get_battery_vol,
  310. .get_charge_status = get_charge_status,
  311. .get_rtc_time = get_rtc_time,
  312. .set_rtc_time = set_rtc_time,
  313. #ifndef CONFIG_SIMULATOR
  314. .motor_get_level = aem_dev_motor_get_level,
  315. .motor_set_level = aem_dev_motor_set_level,
  316. .motor_start = aem_dev_motor_start,
  317. .motor_stop = aem_dev_motor_stop,
  318. .motor_control = aem_dev_motor_control,
  319. .motor_set_freq = aem_dev_motor_set_freq,
  320. #endif
  321. .get_device_info = get_device_info,
  322. .check_bat_is_lowpower = check_bat_is_lowpower,
  323. };
  324. const aem_device_ops_t *aem_get_device_ops(void)
  325. {
  326. #ifndef CONFIG_SIMULATOR
  327. aem_motor_ctx_init();
  328. #endif
  329. return &s_device_ops;
  330. }