mcu_ctrl_board.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #include "crc16.h"
  2. #include "mcu_ctrl_board.h"
  3. #include "../../rtu_linux_modules/swapi/subjects/serial/serial.h"
  4. // 模块名称
  5. static const char MODULE_NAME[] = "MCUCtrlBoard";
  6. // 告诉编译器使用一字节内存对齐
  7. #pragma pack(push, 1)
  8. // 单片机控制板运行配置(小端序)
  9. typedef struct {
  10. uint8_t version[4]; // 版本号
  11. uint8_t ctrl_mode; // 定时控制模式(0=光控, 1=时控)
  12. uint8_t light_duration; // 光控定时时长(单位: 小时, 范围: 1-10, 仅光控模式有效)
  13. uint8_t start_hour; // 时控开始时间(小时, 0-23, 一天24小时, 仅时控模式有效)
  14. uint8_t end_hour; // 时控结束时间(小时, 0-23, 一天24小时, 仅时控模式有效)
  15. uint8_t tp_intvl_min[2];// 拍照间隔(分钟,其值不得超过当前控制模式下允许的最大值)
  16. } MCUScheduleCfg;
  17. // 恢复之前原有内存字节对齐方式
  18. #pragma pack(pop)
  19. // 定义与单片机进行通讯的结构体
  20. typedef struct
  21. {
  22. void *h; // 打开的串口句柄
  23. char work_state[4]; // 工作状态位图(共32个状态)
  24. void *state_rwlock; // 状态位图读写锁
  25. EnvSensorData data[24]; // 接收传感器采集的环境数据
  26. void *edata_rwlock; // 环境数据读写锁
  27. } SMCBCom;
  28. static MCUScheduleCfg cfg_params;
  29. static SMCBCom s_myCom;
  30. // 声明Go中的判断RTU是否已联网在线的函数(Go实现, 供C/C++调用)
  31. extern int RTU_IsInetAvailable(); // 0未联网, 1已联网
  32. // 声明Go中的判断RTU是否已同步时间的函数(Go实现, 供C/C++调用)
  33. extern int RTU_IsSyncedNtpTime(); // 0未同步, 1已同步
  34. // 声明Go中的判断RTU是否已登录后台服务器(Go实现, 供C/C++调用)
  35. extern int RTU_IsLoginMqServer(); // 0未登录, 1已登录
  36. // 声明Go中发送一组 温湿度光照数据的函数(Go实现, 供C/C++调用)
  37. extern int RTU_SendGrpEnvData(EnvSensorData *pGrpData, int cnt/*数量, 一次全发是24*/); // 0成功, <0时失败
  38. // 声明Go中发送一条 温湿度光照数据的函数(Go实现, 供C/C++调用)
  39. extern int RTU_SendOneEnvData(EnvSensorData *pOneData); // 0成功, <0时失败
  40. // 声明Go中通知RTU需要进行一次拍照的函数(Go实现, 供C/C++调用)
  41. extern int RTU_RequestTakePhoto(); // 0成功, <0时失败
  42. // 声明Go中加载MCU配置项"运行参数"的函数(Go实现, 供C/C++调用)
  43. extern int RTU_LoadMCUParamsCfg(); // 0成功, <0时失败
  44. // 声明Go中通知RTU进行一次掉电关机的函数(Go实现, 供C/C++调用)
  45. extern int RTU_NotifyPwrWillOff(); // 0成功, <0时失败
  46. // 声明Go中判断MCU固件是否需要升级的函数(Go实现, 供C/C++调用)
  47. // 返回: 0不需要升级, 1需要升级
  48. extern int MCU_IsNeedUpgrade(const char *ver/*当前的固件版本*/);
  49. // 声明Go中升级MCU固件的函数(强制覆盖写, Go实现, 供C/C++调用)
  50. extern int MCU_UpgradeFirmware(); // 0成功, <0时失败
  51. // 反转字节, 可用于处理大小端字节序的相互转换,"n"不必是2的倍数
  52. static inline void swap_bytes(char *a, size_t n)
  53. {
  54. for(char *p = a, *q = a + n - 1; p < q; p++, q--) { char t = *p; *p = *q; *q = t; }
  55. }
  56. // 获取32位秒级时间戳(存在"​​2038-01-19 03:14:07 UTC​​"溢出风险)
  57. static inline uint32_t now_sec32(void)
  58. {
  59. return (uint32_t)time(NULL);
  60. }
  61. // 发送数据前, 处理帧内容的转义, 返回添加转义后的数据帧及其长度
  62. static int frame_doEscapeChars(const unsigned char *src, int len, unsigned char **dst)
  63. {
  64. if(!src ||!dst || len < 2 || src[0]!= 0x7D || src[len-1]!= 0x7E) return -1;
  65. unsigned char *ptr = (unsigned char *)sw_heap_malloc(2+2*(len-2));
  66. if(!ptr) return -2;
  67. ptr[0] = src[0]; int j = 1;
  68. for(int i = 1; i < (len-1); i++)
  69. {
  70. if(0);
  71. else if(src[i] == 0x7D) { ptr[j++] = 0x7D; ptr[j++] = 0x5D; }
  72. else if(src[i] == 0x7E) { ptr[j++] = 0x7D; ptr[j++] = 0x5E; }
  73. else ptr[j++] = src[i];
  74. }
  75. ptr[j++] = src[len-1]; *dst = ptr;
  76. return j;
  77. }
  78. // 接收数据后, 反转义帧数据内容, 返回去掉转义后的原始帧及其长度
  79. static int frame_unescapeChars(const unsigned char *src, int len, unsigned char **dst)
  80. {
  81. if(!src ||!dst || len < 2 || src[0]!= 0x7D || src[len-1]!= 0x7E) return -1;
  82. unsigned char *ptr = (unsigned char *)sw_heap_malloc(2+1*(len-2));
  83. if(!ptr) return -2;
  84. ptr[0] = src[0]; int j = 1;
  85. for(int i = 1; i < (len-1); i++)
  86. {
  87. if(0);
  88. else if(src[i] == 0x7D && src[i+1] == 0x5D) { ptr[j++] = 0x7D; i++; }
  89. else if(src[i] == 0x7D && src[i+1] == 0x5E) { ptr[j++] = 0x7E; i++; }
  90. else ptr[j++] = src[i];
  91. }
  92. ptr[j++] = src[len-1]; *dst = ptr;
  93. return j;
  94. }
  95. // 接收、处理来自MCU控制板的数据报文帧, 串口-线程回调
  96. #define MIN_FRAME_LEN 6 // 最小帧(数据区为零)的长度
  97. static int comio_data_recv_proc(unsigned long wParam/*传递打开的串口句柄*/, unsigned long lParam/*保留暂未使用*/)
  98. {
  99. SMCBCom *pComIO = &s_myCom; void *pSerial = pComIO->h;
  100. const unsigned char *pRecvBuf = serial_get_recv_buffer(pSerial); int nRecvBytes = serial_get_recv_buffer_bytes(pSerial);
  101. const char *log_prefix = serial_get_log_prefix(pSerial); int ret; bool bPrintRecvBuf = true;
  102. unsigned char *req = NULL; int nReqBytes; u_int16_t crc1, crc2, crc3; int nReqDataLen;
  103. unsigned char sendBuf[MAX_LINE_CHARS] = { 0 }; int nSendBytes; bool is_mcu_need_upgrade = false;
  104. unsigned char *rsp = NULL; int nRspBytes; char log_rsp[MAX_LINE_CHARS] = { 0 }; static bool bTakenPhoto = false; /*标记本次是否已拍照*/
  105. // 0, 判定接收有效的请求帧
  106. if((nRecvBytes == 1 && pRecvBuf[0] != 0x7D) || (nRecvBytes == 2 && pRecvBuf[1] == 0x5D)) goto ret_p3;
  107. else if(nRecvBytes >= MIN_FRAME_LEN && pRecvBuf[nRecvBytes-1] == 0x7E) goto ret_p1;
  108. else goto ret_p4;
  109. // 1, 收到一个完整的请求帧
  110. ret_p1:
  111. // 1.1, 打印接收到的帧内容
  112. bPrintRecvBuf = false;
  113. serial_printf_recv_buffer(pSerial, LEVEL_DEBUG);
  114. sw_log_debug("[%s] %s received a request(%d bytes)", MODULE_NAME, log_prefix, nRecvBytes);
  115. // 1.2, 去掉转义得到原始帧
  116. ret = frame_unescapeChars(pRecvBuf, nRecvBytes, &req);
  117. if(ret < MIN_FRAME_LEN)
  118. {
  119. sw_log_error("[%s] %s failed to unescape the received request(%d)!!", MODULE_NAME, log_prefix, ret);
  120. goto ret_p3;
  121. }
  122. nReqBytes = ret;
  123. // 1.3, 该请求帧合法性校验
  124. crc1 = MAKEWORD(req[nReqBytes-3], req[nReqBytes-2]);
  125. crc2 = CRC16(req+1, nReqBytes-4);
  126. if(crc1 != crc2)
  127. {
  128. sw_log_error("[%s] %s received a request with invalid crc16(0x%02x != 0x%02x)!!", MODULE_NAME, log_prefix, crc1, crc2);
  129. goto ret_p3;
  130. }
  131. // 1.4, 解析处理该帧的请求
  132. nReqDataLen = nReqBytes-MIN_FRAME_LEN; // 数据区长度
  133. switch(req[2])
  134. {
  135. case 0x01: // MCU查询RTU的工作状态
  136. if(nReqDataLen != 1) { sw_log_error("[%s] %s received a request with invalid data length!!", MODULE_NAME, log_prefix); goto ret_p3; }
  137. int pwr_reason = (int)req[3];
  138. bool bTakePhoto = ((pwr_reason == 1 || pwr_reason == 2) ? true : false);
  139. if(bTakePhoto && !bTakenPhoto) { RTU_RequestTakePhoto()/*请求RTU进行拍照(异步执行)*/; bTakenPhoto = true/*只请求一次*/; }
  140. unsigned char tmpBuf1[10] = { 0x7D, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7E };
  141. sw_rwlock_wrlock(pComIO->state_rwlock, WAIT_FOREVER);
  142. if(RTU_IsInetAvailable() == 1 && RTU_IsLoginMqServer() == 1) sw_chararry_setBit(pComIO->work_state, 4, 0, 0x01); // 联机
  143. else sw_chararry_setBit(pComIO->work_state, 4, 0, 0x00); // 脱机
  144. for(int i = 0; i < 4; i++) { tmpBuf1[3+i] = pComIO->work_state[3-i]; } // 小端转大端
  145. sw_rwlock_unlock(pComIO->state_rwlock);
  146. crc3 = CRC16(tmpBuf1+1, 6);
  147. memcpy(&tmpBuf1[7], &crc3, 2); // 小端字节序
  148. nSendBytes = sizeof(tmpBuf1); memcpy(sendBuf, tmpBuf1, nSendBytes);
  149. goto ret_p2;
  150. case 0x02: // 24小时温湿度光照数据, 每组14字节, 正常是24组
  151. case 0x05: // RTU单次的请求MCU, 发送一组当前实时的环境数据
  152. if(nReqDataLen <= 0 || (nReqDataLen%14) != 0 || (nReqDataLen/14) <= 0 || (nReqDataLen/14) > 24)
  153. { sw_log_error("[%s] %s received a request with invalid data length!!", MODULE_NAME, log_prefix); goto ret_p3; }
  154. int groupCnt = nReqDataLen/14;
  155. if(req[2] == 0x05 && groupCnt != 1)
  156. { sw_log_error("[%s] %s received a request with invalid data length!!", MODULE_NAME, log_prefix); goto ret_p3; }
  157. sw_rwlock_wrlock(pComIO->edata_rwlock, WAIT_FOREVER);
  158. for(int i = 0; i < groupCnt; i++)
  159. {
  160. memcpy(&pComIO->data[i].timestamp, req+3 +i*14, 4); // 时间戳(秒, 存在"​​2038-01-19 03:14:07 UTC​​"溢出风险)
  161. swap_bytes((char *)&pComIO->data[i].timestamp, 4); // 大端转小端
  162. memcpy(&pComIO->data[i].temperature, req+7 +i*14, 2); // 温度, 单位: 0.1°C
  163. swap_bytes((char *)&pComIO->data[i].temperature, 2); // 大端转小端
  164. memcpy(&pComIO->data[i].humidity, req+9 +i*14, 2); // 湿度, 单位: 0.1%RH
  165. swap_bytes((char *)&pComIO->data[i].humidity, 2); // 大端转小端
  166. memcpy(&pComIO->data[i].illuminance, req+11+i*14, 4); // 光照, 单位: lux
  167. swap_bytes((char *)&pComIO->data[i].illuminance, 4); // 大端转小端
  168. memcpy(&pComIO->data[i].voltage, req+15+i*14, 2); // 电压, 单位: 0.1V
  169. swap_bytes((char *)&pComIO->data[i].voltage, 2); // 大端转小端
  170. }
  171. if(req[2] == 0x02) RTU_SendGrpEnvData(&pComIO->data[0], groupCnt); // 异步发送一组环境数据到RTU
  172. if(req[2] == 0x05) RTU_SendOneEnvData(&pComIO->data[0]); // 异步发送一条环境数据到RTU
  173. sw_rwlock_unlock(pComIO->edata_rwlock);
  174. if(req[2] == 0x05) goto ret_p3; // 应答帧不需要再回复
  175. unsigned char tmpBuf2[6] = { 0x7D, 0x01, 0x02, 0x81, 0xE1, 0x7E };
  176. nSendBytes = sizeof(tmpBuf2); memcpy(sendBuf, tmpBuf2, nSendBytes);
  177. goto ret_p2;
  178. case 0x03: // MCU请求RTU, 进行对时
  179. if(nReqDataLen != 4) { sw_log_error("[%s] %s received a request with invalid data length!!", MODULE_NAME, log_prefix); goto ret_p3; }
  180. unsigned char tmpBuf3[10] = { 0x7D, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7E };
  181. if(RTU_IsSyncedNtpTime() == 1)
  182. { // 使用RTU时间同步返回
  183. uint32_t now = now_sec32();
  184. swap_bytes((char *)&now, 4); // 小端转大端
  185. memcpy(&tmpBuf3[3], &now, 4);
  186. }
  187. else
  188. { // 使用MCU时间原路返回
  189. tmpBuf3[3] = req[3]; tmpBuf3[4] = req[4]; tmpBuf3[5] = req[5]; tmpBuf3[6] = req[6];
  190. uint32_t now = ((uint32_t)req[3] << 24) | ((uint32_t)req[4] << 16) | ((uint32_t)req[5] << 8) | (uint32_t)req[6];
  191. char timestamp[MAX_LINE_CHARS]; sprintf(timestamp, "%u", now);
  192. if(xsetlocaltime(timestamp) == 0) sw_log_info("[%s] %s set local time to %s with MCU clock", MODULE_NAME, log_prefix, timestamp);
  193. else sw_log_warn("[%s] %s failed to set local time to %s with MCU clock!", MODULE_NAME, log_prefix, timestamp);
  194. }
  195. crc3 = CRC16(tmpBuf3+1, 6);
  196. memcpy(&tmpBuf3[7], &crc3, 2); // 小端字节序
  197. nSendBytes = sizeof(tmpBuf3); memcpy(sendBuf, tmpBuf3, nSendBytes);
  198. goto ret_p2;
  199. case 0x04: // MCU请求RTU, 同步参数
  200. if(nReqDataLen != 4) { sw_log_error("[%s] %s received a request with invalid data length!!", MODULE_NAME, log_prefix); goto ret_p3; }
  201. if(RTU_LoadMCUParamsCfg() != 0) goto ret_p3;
  202. unsigned char tmpBuf4[16] = { 0x7D, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7E };
  203. tmpBuf4[3] = cfg_params.version[3]; tmpBuf4[4] = cfg_params.version[2];
  204. tmpBuf4[5] = cfg_params.version[1]; tmpBuf4[6] = cfg_params.version[0];
  205. tmpBuf4[7] = cfg_params.ctrl_mode; // 定时控制模式
  206. tmpBuf4[8] = cfg_params.light_duration; // 光控定时时长
  207. tmpBuf4[9] = cfg_params.start_hour; // 时控开始时间
  208. tmpBuf4[10] = cfg_params.end_hour; // 时控结束时间
  209. tmpBuf4[11] = cfg_params.tp_intvl_min[1]; // 拍照时间间隔
  210. tmpBuf4[12] = cfg_params.tp_intvl_min[0]; // 拍照时间间隔
  211. crc3 = CRC16(tmpBuf4+1, 12);
  212. memcpy(&tmpBuf4[13], &crc3, 2); // 小端字节序
  213. nSendBytes = sizeof(tmpBuf4); memcpy(sendBuf, tmpBuf4, nSendBytes);
  214. goto ret_p2;
  215. case 0x06: // MCU通知RTU, 准备掉电
  216. if(nReqDataLen != 1) { sw_log_error("[%s] %s received a request with invalid data length!!", MODULE_NAME, log_prefix); goto ret_p3; }
  217. unsigned char tmpBuf6[10] = { 0x7D, 0x01, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7E };
  218. uint32_t work_state;
  219. sw_rwlock_rdlock(pComIO->state_rwlock, WAIT_FOREVER);
  220. memcpy(&work_state, pComIO->work_state, 4); // 保持小端序
  221. for(int i = 0; i < 4; i++) { tmpBuf6[3+i] = pComIO->work_state[3-i]; } // 小端转大端
  222. sw_rwlock_unlock(pComIO->state_rwlock);
  223. crc3 = CRC16(tmpBuf6+1, 6);
  224. memcpy(&tmpBuf6[7], &crc3, 2); // 小端字节序
  225. if(work_state == 0x00000000 || work_state == 0x00000001)
  226. { // 空闲状态, 通知RTU准备掉电, 非空闲状态时MCU应继续查询等待
  227. RTU_NotifyPwrWillOff();
  228. sw_log_warn("[%s] %s notified \"RTU\" to power off!", MODULE_NAME, log_prefix);
  229. }
  230. nSendBytes = sizeof(tmpBuf6); memcpy(sendBuf, tmpBuf6, nSendBytes);
  231. goto ret_p2;
  232. case 0x07: // MCU请求RTU, 升级固件
  233. if(nReqDataLen != 5) { sw_log_error("[%s] %s received a request with invalid data length!!", MODULE_NAME, log_prefix); goto ret_p3; }
  234. unsigned char ver1 = req[4], ver2 = req[5], ver3 = req[6], ver4 = req[7];
  235. char version[MAX_LINE_CHARS]; sprintf(version, "%u.%u.%u.%u", ver1, ver2, ver3, ver4);
  236. unsigned char tmpBuf7[7] = { 0x7D, 0x01, 0x07, 0x00, 0x00, 0x00, 0x7E }; // 默认不需要升级
  237. if(MCU_IsNeedUpgrade(version) == 1) { tmpBuf7[3] = 0x01;/* 需要升级 */ is_mcu_need_upgrade = true; }
  238. crc3 = CRC16(tmpBuf7+1, 3);
  239. memcpy(&tmpBuf7[4], &crc3, 2); // 小端字节序
  240. nSendBytes = sizeof(tmpBuf7); memcpy(sendBuf, tmpBuf7, nSendBytes);
  241. goto ret_p2;
  242. default:
  243. sw_log_warn("[%s] %s unknown a data req(type=0x%02x)!", MODULE_NAME, log_prefix, req[2]);
  244. goto ret_p3;
  245. }
  246. // 2, 回复该请求帧的应答包
  247. ret_p2:
  248. ret = frame_doEscapeChars(sendBuf, nSendBytes, &rsp);
  249. if(ret < MIN_FRAME_LEN)
  250. {
  251. sw_log_error("[%s] %s failed to escape the response(%d)!!", MODULE_NAME, log_prefix, ret);
  252. goto ret_p3;
  253. }
  254. nRspBytes = ret;
  255. xstrfromhex(rsp, nRspBytes, log_rsp, NULL, NULL);
  256. ret = serial_send_data(pComIO->h, rsp, nRspBytes);
  257. if(ret == nRspBytes) sw_log_debug("[%s] %s sent a response(%d bytes)(hex): %s", MODULE_NAME, log_prefix, nRspBytes, log_rsp);
  258. else { sw_log_error("[%s] %s failed to send a response(%d bytes, ret=%d)(hex): %s!!", MODULE_NAME, log_prefix, nRspBytes, ret, log_rsp); goto ret_p3; }
  259. if(req[2] == 0x07 && is_mcu_need_upgrade) {
  260. MCU_UpgradeFirmware(); // 强制覆盖写, 升级MCU固件(异步执行)
  261. }
  262. // 3, 清空接收缓存区(重置)
  263. ret_p3:
  264. if(req) sw_heap_free(req);
  265. if(rsp) sw_heap_free(rsp);
  266. if(nRecvBytes > 0 && bPrintRecvBuf)
  267. {
  268. serial_printf_recv_buffer(pSerial, LEVEL_DEBUG);
  269. sw_log_warn("[%s] %s discarded %d bytes of meaningless data!", MODULE_NAME, log_prefix, nRecvBytes);
  270. }
  271. if(nRecvBytes > 0) serial_clear_recv_buffer(pSerial);
  272. // 4, 持续累加的接收新数据
  273. ret_p4:
  274. return 1;
  275. }
  276. // 打开与MCU控制板的通讯, 返回: 0成功, <0时失败
  277. int MCBComInit()
  278. {
  279. s_myCom.state_rwlock = sw_rwlock_create();
  280. s_myCom.edata_rwlock = sw_rwlock_create();
  281. if(!s_myCom.state_rwlock || !s_myCom.edata_rwlock)
  282. {
  283. sw_log_error("[%s] failed to create the rwlocks!!", MODULE_NAME);
  284. MCBComExit(); return -1;
  285. }
  286. #ifdef _DEBUG // 上位机单元测试时使用
  287. const char *serialName = "/dev/ttyS0"; int baudrate = 115200;
  288. #else
  289. const char *serialName = "/dev/ttymxc2"; int baudrate = 115200;
  290. #endif
  291. const char *parityCheck = "none"; // 无校检
  292. s_myCom.h = serial_open(serialName, baudrate, parityCheck, \
  293. comio_data_recv_proc, comio_data_recv_proc, NULL);
  294. if(!s_myCom.h)
  295. {
  296. sw_log_error("[%s] failed to open the \"%s:%d(%s parity)\" device!!", \
  297. MODULE_NAME, serialName, baudrate, parityCheck);
  298. MCBComExit(); return -2;
  299. }
  300. return 0;
  301. }
  302. // 关闭与MCU控制板的通讯, 返回: 0成功, <0时失败
  303. int MCBComExit()
  304. {
  305. if(s_myCom.h) serial_close(s_myCom.h, WAITTHRD_SAFEEXIT_TIMEOUT);
  306. if(s_myCom.state_rwlock) sw_rwlock_destroy(s_myCom.state_rwlock);
  307. if(s_myCom.edata_rwlock) sw_rwlock_destroy(s_myCom.edata_rwlock);
  308. s_myCom.h = NULL; s_myCom.state_rwlock = NULL; s_myCom.edata_rwlock = NULL;
  309. memset(&s_myCom.data, 0, sizeof(s_myCom.data));
  310. return 0;
  311. }
  312. // 配置单片机控制板-运行参数, 返回: 0成功, <0时失败
  313. int MCBConfParameters(uint32_t version, uint8_t ctrl_mode, uint8_t light_duration, uint8_t start_hour, uint8_t end_hour, uint16_t tp_intvl_min)
  314. {
  315. memcpy(cfg_params.version, &version, 4);
  316. cfg_params.ctrl_mode = ctrl_mode;
  317. cfg_params.light_duration = light_duration;
  318. cfg_params.start_hour = start_hour;
  319. cfg_params.end_hour = end_hour;
  320. memcpy(cfg_params.tp_intvl_min, &tp_intvl_min, 2);
  321. return 0;
  322. }
  323. // 设置工作状态位图-固件运维, 返回: 0成功, <0时失败
  324. int MCBSetMNTStateBit(uint8_t state/*1bit位, 0x00:空闲, 0x01:运维中*/)
  325. {
  326. if(!s_myCom.state_rwlock || (state != 0x00 && state != 0x01)) return -1;
  327. sw_rwlock_wrlock(s_myCom.state_rwlock, WAIT_FOREVER);
  328. if(state == 0x01) sw_chararry_setBit(s_myCom.work_state, 4, 1, 0x01); // 升级程序
  329. else sw_chararry_setBit(s_myCom.work_state, 4, 1, 0x00); // 正常工作
  330. sw_rwlock_unlock(s_myCom.state_rwlock);
  331. return 0;
  332. }
  333. // 设置工作状态位图-相机拍照, 返回: 0成功, <0时失败
  334. int MCBSetCamStateBit(uint8_t state/*2bit位, 0x00:空闲, 0x01:拍照中, 0x10:上传中, 0x11:保留*/)
  335. {
  336. if(!s_myCom.state_rwlock || (state != 0x00 && state != 0x01 && state != 0x10)) return -1;
  337. sw_rwlock_wrlock(s_myCom.state_rwlock, WAIT_FOREVER);
  338. switch(state)
  339. {
  340. case 0x00: // 空闲
  341. sw_chararry_setBit(s_myCom.work_state, 4, 2, 0x00);
  342. sw_chararry_setBit(s_myCom.work_state, 4, 3, 0x00);
  343. break;
  344. case 0x01: // 拍照中
  345. sw_chararry_setBit(s_myCom.work_state, 4, 2, 0x01);
  346. sw_chararry_setBit(s_myCom.work_state, 4, 3, 0x00);
  347. break;
  348. case 0x10: // 上传中
  349. sw_chararry_setBit(s_myCom.work_state, 4, 2, 0x00);
  350. sw_chararry_setBit(s_myCom.work_state, 4, 3, 0x01);
  351. break;
  352. }
  353. sw_rwlock_unlock(s_myCom.state_rwlock);
  354. return 0;
  355. }
  356. // 设置工作状态位图-环境数据, 返回: 0成功, <0时失败
  357. int MCBSetEnvStateBit(uint8_t state/*2bit位, 0x00:空闲, 0x01:收集中, 0x10:上传中, 0x11:保留*/)
  358. {
  359. if(!s_myCom.state_rwlock || (state != 0x00 && state != 0x01 && state != 0x10)) return -1;
  360. sw_rwlock_wrlock(s_myCom.state_rwlock, WAIT_FOREVER);
  361. switch(state)
  362. {
  363. case 0x00: // 空闲
  364. sw_chararry_setBit(s_myCom.work_state, 4, 4, 0x00);
  365. sw_chararry_setBit(s_myCom.work_state, 4, 5, 0x00);
  366. break;
  367. case 0x01: // 收集中
  368. sw_chararry_setBit(s_myCom.work_state, 4, 4, 0x01);
  369. sw_chararry_setBit(s_myCom.work_state, 4, 5, 0x00);
  370. break;
  371. case 0x10: // 上传中
  372. sw_chararry_setBit(s_myCom.work_state, 4, 4, 0x00);
  373. sw_chararry_setBit(s_myCom.work_state, 4, 5, 0x01);
  374. break;
  375. }
  376. sw_rwlock_unlock(s_myCom.state_rwlock);
  377. return 0;
  378. }
  379. // 请求获取当前实时-环境数据, 返回: 0成功, <0时失败
  380. int MCBReqEnvCurData()
  381. {
  382. if(!s_myCom.h) return -1;
  383. unsigned char req[6] = { 0x7D, 0x01, 0x05, 0xC0, 0x23, 0x7E };
  384. int ret = serial_send_data(s_myCom.h, req, sizeof(req));
  385. if(ret != sizeof(req)) return -2;
  386. sw_log_debug("[%s] sent a request(6 bytes)(hex): %s", MODULE_NAME, "7d0105c0237e");
  387. return 0;
  388. }
  389. #ifdef _DEBUG
  390. void *MCBGetSerialHandle()
  391. {
  392. return s_myCom.h;
  393. }
  394. #endif