简介
配网问题
- gateway无法加超过5个设备!?
- 暂时用分配“固定地址”给设备来应付。
客户端程序架构(gateway例子)
- 工程初始化
- 修改配网参数
- 应用
- 串口处理
- vendor模型
- 控制DIDO
修改配网参数
为了暂时不受“地址数”限制,把每个入网设备的地址都分配成固定地址。。
在绑定config模型时,获取设备密钥失败导致,具体什么原因后面再查
case PROV_EVT_PROVDATA_INFO_REQ:
CONSOLE_OUT ("Recvd PROV_EVT_PROVDATA_INFO_REQ\n");
CONSOLE_OUT ("Status - 0x%04X\n", event_result);
/* Send Provisioning Data */
CONSOLE_OUT ("Send Provisioning Data...\n");
/* Update the next device address if provisioned devices are present in database */
retval = MS_access_cm_get_prov_devices_list
(
&prov_dev_list[0],
&num_entries,
&pointer_entries
);
if ((API_SUCCESS == retval) /*&&
(0 != num_entries)*/)
{
// UI_prov_data.uaddr = prov_dev_list[num_entries - 1].uaddr +
// prov_dev_list[num_entries - 1].num_elements;
//UI_prov_data.uaddr = pointer_entries;
UI_prov_data.uaddr = 0x0003;//重点
}
printf("Updating Provisioning Start Addr to 0x%04X\n", UI_prov_data.uaddr);
//Get network key
MS_access_cm_get_netkey_at_offset(0,0,UI_prov_data.netkey);
//Get key refresh state
MS_access_cm_get_key_refresh_phase(0,&key_refresh_state);
key_refresh_state = (MS_ACCESS_KEY_REFRESH_PHASE_2 == key_refresh_state) ? 0x01 : 0x00;
UI_prov_data.ivindex = ms_iv_index.iv_index;
UI_prov_data.flags = ((ms_iv_index.iv_update_state & 0x01) << 1) | key_refresh_state;
blebrr_scan_pl(FALSE); //by hq
retval = MS_prov_data (phandle, &UI_prov_data);
CONSOLE_OUT ("Retval - 0x%04X\n", retval);
break;
串口处理
接收:
接收串口发来的数据,解析后发送vendor模型信息
例:dd 01 01 00 00 00 20 00 00 00 01 ee 00
if (events & BLEMESH_UART_RX_EVT)
{
WaitMs(10);//要加这句才能正常使用
cmdstr[cmdlen - 1] = '\0';//要加这句才能正常使用
if((cmdstr[0]==0xDD)&&(cmdstr[11]==0xEE)&&cmdlen)
{
if(blebrr_prov_started == MS_FALSE)
{
UI_vendor_model_set_raw_addr();
uint8_t ack = cmdstr[1];
uint16_t index = cmdstr[2]+(cmdstr[3]<<8);
uint8_t ch = cmdstr[4];
uint8_t val = cmdstr[5];
uint32_t delayms = cmdstr[6]+(cmdstr[7]<<8)+(cmdstr[8]<<16)+(cmdstr[9]<<24);
uint8_t mode = cmdstr[10];
printf("[UART_rx] ack:%d,index:%d,ch:%d,val:%d,delayms:%d,mode:%d\r\n",
ack,index,ch,val,delayms,mode);
/*
1:是否应答
2:设备号
3:通道(0:DO1 ,1:DO2 ,2:DI1 ,3:DI2)
4:值(0/1)
5:延时时间
6:模式(0:常闭,1:常开)
*/
UI_vendor_model_sett(ack,index,ch,val,delayms,mode);//重点
}
}
cmdlen = 0;
return ( events ^ BLEMESH_UART_RX_EVT );
}
发送:
在收到vendor服务端返回的应答之后,通过串口转发出去
case MS_ACCESS_VENDORMODEL_NOTIFY_OPCODE:
{
uint8_t ack;
uint16_t index;
uint8_t ch;
uint8_t val;
uint32_t delayms;
uint8_t mode;
data_param = state_params->vendormodel_param;
marker = 0;
MS_UNPACK_LE_1_BYTE(&ack, data_param+marker);
marker += 1;
MS_UNPACK_LE_2_BYTE(&index, data_param+marker);
marker += 2;
MS_UNPACK_LE_1_BYTE(&ch, data_param+marker);
marker += 1;
MS_UNPACK_LE_1_BYTE(&val, data_param+marker);
marker += 1;
MS_UNPACK_LE_4_BYTE(&delayms, data_param+marker);
marker += 4;
MS_UNPACK_LE_1_BYTE(&mode, data_param+marker);
marker += 1;
CONSOLE_OUT("[UART_ACK] ack:%d,index:%d,ch:%d,val:%d,delayms:%d,mode:%d\r\n",
ack,index,ch,val,delayms,mode);//重点
}
vendor模型
发送vendor信息到服务端的处理函数
void UI_vendor_model_sett(uint8_t ack,uint16_t index,uint8_t ch,uint8_t val,uint32_t delayms,uint8_t mode)
{
API_RESULT retval;
MS_NET_ADDR addr;
UCHAR is_prov_req;
UCHAR buffer[256+2];
UINT16 marker;
MS_NET_ADDR dst_addr;
MS_ACCESS_VENDORMODEL_STATE_PARAMS set_param;
is_prov_req = MS_TRUE;
marker = 0;
retval = MS_access_cm_get_primary_unicast_address(&addr);//获取自身的地址!?
if (API_SUCCESS == retval)
{
if (MS_NET_ADDR_UNASSIGNED != addr)
{
/* Set Provisioning is not Required */
is_prov_req = MS_FALSE;
}
}
if (MS_FALSE == is_prov_req)
{
// CONSOLE_OUT
// ("Send Vendor Model Set\n");
/* Get Model Publication Address and check if valid */
UI_GET_RAW_DATA_DST_ADDR(dst_addr);
retval = ui_check_destnation_address(dst_addr);
if(retval == API_SUCCESS)
{
uint16 len = 10;
marker = 0;
MS_PACK_LE_2_BYTE_VAL(&buffer[marker], len);
marker += 2;
MS_PACK_LE_1_BYTE_VAL(&buffer[marker], ack);
marker += 1;
MS_PACK_LE_2_BYTE_VAL(&buffer[marker], index);
marker += 2;
MS_PACK_LE_1_BYTE_VAL(&buffer[marker], ch);
marker += 1;
MS_PACK_LE_1_BYTE_VAL(&buffer[marker], val);
marker += 1;
MS_PACK_LE_4_BYTE_VAL(&buffer[marker], delayms);
marker += 4;
MS_PACK_LE_1_BYTE_VAL(&buffer[marker], mode);
marker += 1;
set_param.vendormodel_param = &buffer[0];
CONSOLE_OUT("[UART_TX] ack:%d,index:%d,ch:%d,val:%d,delayms:%d,mode:%d\r\n",
ack,index,ch,val,delayms,mode);
retval = MS_vendormodel_client_send_reliable_pdu
(
MS_ACCESS_VENDORMODEL_NOTIFY_OPCODE,
&set_param,
dst_addr
);
if(retval != API_SUCCESS)
{
printf("send error %x\n",retval);
}
}
else
{
CONSOLE_OUT
("Error destnation address\n");
}
}
else
{
// osal_stop_timerEx(bleMesh_TaskID, BLEMESH_PDU_TX_OVERRUN);
CONSOLE_OUT
("An Unprovisioned Device\n");
}
}