简介

本文是mesh小卡gateway工程的应用修改记录

配网问题

  • gateway无法加超过5个设备!?
    • 暂时用分配“固定地址”给设备来应付。

客户端程序架构(gateway例子)

  • 工程初始化
    • 修改配网参数
  • 应用
    • 串口处理
    • vendor模型
      • 控制DIDO

修改配网参数

为了暂时不受“地址数”限制,把每个入网设备的地址都分配成固定地址。。
在绑定config模型时,获取设备密钥失败导致,具体什么原因后面再查

  1. case PROV_EVT_PROVDATA_INFO_REQ:
  2. CONSOLE_OUT ("Recvd PROV_EVT_PROVDATA_INFO_REQ\n");
  3. CONSOLE_OUT ("Status - 0x%04X\n", event_result);
  4. /* Send Provisioning Data */
  5. CONSOLE_OUT ("Send Provisioning Data...\n");
  6. /* Update the next device address if provisioned devices are present in database */
  7. retval = MS_access_cm_get_prov_devices_list
  8. (
  9. &prov_dev_list[0],
  10. &num_entries,
  11. &pointer_entries
  12. );
  13. if ((API_SUCCESS == retval) /*&&
  14. (0 != num_entries)*/)
  15. {
  16. // UI_prov_data.uaddr = prov_dev_list[num_entries - 1].uaddr +
  17. // prov_dev_list[num_entries - 1].num_elements;
  18. //UI_prov_data.uaddr = pointer_entries;
  19. UI_prov_data.uaddr = 0x0003;//重点
  20. }
  21. printf("Updating Provisioning Start Addr to 0x%04X\n", UI_prov_data.uaddr);
  22. //Get network key
  23. MS_access_cm_get_netkey_at_offset(0,0,UI_prov_data.netkey);
  24. //Get key refresh state
  25. MS_access_cm_get_key_refresh_phase(0,&key_refresh_state);
  26. key_refresh_state = (MS_ACCESS_KEY_REFRESH_PHASE_2 == key_refresh_state) ? 0x01 : 0x00;
  27. UI_prov_data.ivindex = ms_iv_index.iv_index;
  28. UI_prov_data.flags = ((ms_iv_index.iv_update_state & 0x01) << 1) | key_refresh_state;
  29. blebrr_scan_pl(FALSE); //by hq
  30. retval = MS_prov_data (phandle, &UI_prov_data);
  31. CONSOLE_OUT ("Retval - 0x%04X\n", retval);
  32. break;

串口处理

接收:
接收串口发来的数据,解析后发送vendor模型信息
例:dd 01 01 00 00 00 20 00 00 00 01 ee 00

  1. if (events & BLEMESH_UART_RX_EVT)
  2. {
  3. WaitMs(10);//要加这句才能正常使用
  4. cmdstr[cmdlen - 1] = '\0';//要加这句才能正常使用
  5. if((cmdstr[0]==0xDD)&&(cmdstr[11]==0xEE)&&cmdlen)
  6. {
  7. if(blebrr_prov_started == MS_FALSE)
  8. {
  9. UI_vendor_model_set_raw_addr();
  10. uint8_t ack = cmdstr[1];
  11. uint16_t index = cmdstr[2]+(cmdstr[3]<<8);
  12. uint8_t ch = cmdstr[4];
  13. uint8_t val = cmdstr[5];
  14. uint32_t delayms = cmdstr[6]+(cmdstr[7]<<8)+(cmdstr[8]<<16)+(cmdstr[9]<<24);
  15. uint8_t mode = cmdstr[10];
  16. printf("[UART_rx] ack:%d,index:%d,ch:%d,val:%d,delayms:%d,mode:%d\r\n",
  17. ack,index,ch,val,delayms,mode);
  18. /*
  19. 1:是否应答
  20. 2:设备号
  21. 3:通道(0:DO1 ,1:DO2 ,2:DI1 ,3:DI2)
  22. 4:值(0/1)
  23. 5:延时时间
  24. 6:模式(0:常闭,1:常开)
  25. */
  26. UI_vendor_model_sett(ack,index,ch,val,delayms,mode);//重点
  27. }
  28. }
  29. cmdlen = 0;
  30. return ( events ^ BLEMESH_UART_RX_EVT );
  31. }

发送:
在收到vendor服务端返回的应答之后,通过串口转发出去

  1. case MS_ACCESS_VENDORMODEL_NOTIFY_OPCODE:
  2. {
  3. uint8_t ack;
  4. uint16_t index;
  5. uint8_t ch;
  6. uint8_t val;
  7. uint32_t delayms;
  8. uint8_t mode;
  9. data_param = state_params->vendormodel_param;
  10. marker = 0;
  11. MS_UNPACK_LE_1_BYTE(&ack, data_param+marker);
  12. marker += 1;
  13. MS_UNPACK_LE_2_BYTE(&index, data_param+marker);
  14. marker += 2;
  15. MS_UNPACK_LE_1_BYTE(&ch, data_param+marker);
  16. marker += 1;
  17. MS_UNPACK_LE_1_BYTE(&val, data_param+marker);
  18. marker += 1;
  19. MS_UNPACK_LE_4_BYTE(&delayms, data_param+marker);
  20. marker += 4;
  21. MS_UNPACK_LE_1_BYTE(&mode, data_param+marker);
  22. marker += 1;
  23. CONSOLE_OUT("[UART_ACK] ack:%d,index:%d,ch:%d,val:%d,delayms:%d,mode:%d\r\n",
  24. ack,index,ch,val,delayms,mode);//重点
  25. }

vendor模型

发送vendor信息到服务端的处理函数

  1. void UI_vendor_model_sett(uint8_t ack,uint16_t index,uint8_t ch,uint8_t val,uint32_t delayms,uint8_t mode)
  2. {
  3. API_RESULT retval;
  4. MS_NET_ADDR addr;
  5. UCHAR is_prov_req;
  6. UCHAR buffer[256+2];
  7. UINT16 marker;
  8. MS_NET_ADDR dst_addr;
  9. MS_ACCESS_VENDORMODEL_STATE_PARAMS set_param;
  10. is_prov_req = MS_TRUE;
  11. marker = 0;
  12. retval = MS_access_cm_get_primary_unicast_address(&addr);//获取自身的地址!?
  13. if (API_SUCCESS == retval)
  14. {
  15. if (MS_NET_ADDR_UNASSIGNED != addr)
  16. {
  17. /* Set Provisioning is not Required */
  18. is_prov_req = MS_FALSE;
  19. }
  20. }
  21. if (MS_FALSE == is_prov_req)
  22. {
  23. // CONSOLE_OUT
  24. // ("Send Vendor Model Set\n");
  25. /* Get Model Publication Address and check if valid */
  26. UI_GET_RAW_DATA_DST_ADDR(dst_addr);
  27. retval = ui_check_destnation_address(dst_addr);
  28. if(retval == API_SUCCESS)
  29. {
  30. uint16 len = 10;
  31. marker = 0;
  32. MS_PACK_LE_2_BYTE_VAL(&buffer[marker], len);
  33. marker += 2;
  34. MS_PACK_LE_1_BYTE_VAL(&buffer[marker], ack);
  35. marker += 1;
  36. MS_PACK_LE_2_BYTE_VAL(&buffer[marker], index);
  37. marker += 2;
  38. MS_PACK_LE_1_BYTE_VAL(&buffer[marker], ch);
  39. marker += 1;
  40. MS_PACK_LE_1_BYTE_VAL(&buffer[marker], val);
  41. marker += 1;
  42. MS_PACK_LE_4_BYTE_VAL(&buffer[marker], delayms);
  43. marker += 4;
  44. MS_PACK_LE_1_BYTE_VAL(&buffer[marker], mode);
  45. marker += 1;
  46. set_param.vendormodel_param = &buffer[0];
  47. CONSOLE_OUT("[UART_TX] ack:%d,index:%d,ch:%d,val:%d,delayms:%d,mode:%d\r\n",
  48. ack,index,ch,val,delayms,mode);
  49. retval = MS_vendormodel_client_send_reliable_pdu
  50. (
  51. MS_ACCESS_VENDORMODEL_NOTIFY_OPCODE,
  52. &set_param,
  53. dst_addr
  54. );
  55. if(retval != API_SUCCESS)
  56. {
  57. printf("send error %x\n",retval);
  58. }
  59. }
  60. else
  61. {
  62. CONSOLE_OUT
  63. ("Error destnation address\n");
  64. }
  65. }
  66. else
  67. {
  68. // osal_stop_timerEx(bleMesh_TaskID, BLEMESH_PDU_TX_OVERRUN);
  69. CONSOLE_OUT
  70. ("An Unprovisioned Device\n");
  71. }
  72. }