现在的位置: 首页 > 综合 > 正文

从命令行获取参数address_parse函数

2013年08月11日 ⁄ 综合 ⁄ 共 1196字 ⁄ 字号 评论关闭

从命令行获取参数,先看代码:

static void address_parse(
    BACNET_ADDRESS * dst,
    int argc,
    char *argv[])
{
    unsigned mac[6];
    unsigned port;
    int count = 0;
    int index = 0;

    if (argc > 0) {
        count =
            sscanf(argv[0], "%u.%u.%u.%u:%u", &mac[0], &mac[1], &mac[2],
            &mac[3], &port);
        if (count == 5) {
            dst->mac_len = 6;
            for (index = 0; index < 4; index++) {
                dst->mac[index] = mac[index];
            }
            encode_unsigned16(&dst->mac[4], port);
        } else {
            count =
                sscanf(argv[0], "%x:%x:%x:%x:%x:%x", &mac[0], &mac[1], &mac[2],
                &mac[3], &mac[4], &mac[5]);
            dst->mac_len = count;
            for (index = 0; index < MAX_MAC_LEN; index++) {
                if (index < count) {
                    dst->mac[index] = mac[index];
                } else {
                    dst->mac[index] = 0;
                }
            }
        }
    }
    dst->net = 0;
    dst->len = 0;
    for (index = 0; index < MAX_MAC_LEN; index++) {
        dst->adr[index] = 0;
    }
}其中encode_unsigned16为无符号数的编码,假设输入地址是19.168.1.100,端口号是47808那么转换成0xBAC0,那么数组中存放的是192.168.1.100:47808

int encode_unsigned16(
    uint8_t * apdu,
    uint16_t value)
{
    apdu[0] = (uint8_t) ((value & 0xff00) >> 8);
    apdu[1] = (uint8_t) (value & 0x00ff);

    return 2;
}

 

抱歉!评论已关闭.