协议栈主要是针对应用层的编码,因此我们只需要结合VTS中的 BACnet Application Layer Detail 来对源码进行分析, 写属性源文件是wp.c,
这个文件中一共包含以下函数:
int wp_encode_apdu( uint8_t * apdu, uint8_t invoke_id , BACNET_WRITE_PROPERTY_DATA * wpdata);
int wp_decode_service_request( uint8_t * apdu, unsigned apdu_len, BACNET_WRITE_PROPERTY_DATA * wpdata);
先看看第一个函数,这是一个发送写属性,对具有写属性的PDU进行编码的函数
int wp_encode_apdu(
uint8_t * apdu,
uint8_t invoke_id,
BACNET_WRITE_PROPERTY_DATA * wpdata)
{
int apdu_len = 0; /* total length of the apdu, return value */
int len = 0; /* total length of the apdu, return value */
if (apdu) {
apdu[0] = PDU_TYPE_CONFIRMED_SERVICE_REQUEST;
apdu[1] = encode_max_segs_max_apdu(0, MAX_APDU);
apdu[2] = invoke_id;
apdu[3] = SERVICE_CONFIRMED_WRITE_PROPERTY; /* service choice */
apdu_len = 4;
len =
encode_context_object_id(&apdu[apdu_len], 0, wpdata->object_type,
wpdata->object_instance);
apdu_len += len;
len =
encode_context_enumerated(&apdu[apdu_len], 1,
wpdata->object_property);
apdu_len += len;
/* optional array index; ALL is -1 which is assumed when 0missing */
if (wpdata->array_index != BACNET_ARRAY_ALL) {
len =
encode_context_unsigned(&apdu[apdu_len], 2,
wpdata->array_index);
apdu_len += len;
}
/* propertyValue */
len = encode_opening_tag(&apdu[apdu_len], 3);
apdu_len += len;
for (len = 0; len < wpdata->application_data_len; len++) {
apdu[apdu_len + len] = wpdata->application_data[len];
}
apdu_len += wpdata->application_data_len;
len = encode_closing_tag(&apdu[apdu_len], 3);
apdu_len += len;
/* optional priority - 0 if not set, 1..16 if set */
if (wpdata->priority != BACNET_NO_PRIORITY) {
len =
encode_context_unsigned(&apdu[apdu_len], 4, wpdata->priority);
apdu_len += len;
}
}
return apdu_len;
}