/* * Getter and setter functions */ static int _get_addr_long(xbee_t *dev, uint8_t *val, size_t len) { uint8_t cmd[2]; resp_t resp; if (len < IEEE802154_LONG_ADDRESS_LEN) { return -EOVERFLOW; } /* read 4 high byte - AT command: SH*/ cmd[0] = 'S'; cmd[1] = 'H'; _api_at_cmd(dev, cmd, 2, &resp); if (resp.status == 0) { memcpy(val, resp.data, 4); } else { return -ECANCELED; } /* read next 4 byte - AT command: SL */ cmd[1] = 'L'; _api_at_cmd(dev, cmd, 2, &resp); if (resp.status == 0) { memcpy(val + 4, resp.data, 4); return IEEE802154_LONG_ADDRESS_LEN; } return -ECANCELED; }
static int _set_addr(xbee_t *dev, uint8_t *val, size_t len) { uint8_t cmd[4]; resp_t resp; /* device only supports setting the short address */ if (len != 2) { return -ENOTSUP; } cmd[0] = 'M'; cmd[1] = 'Y'; cmd[2] = val[0]; cmd[3] = val[1]; #ifdef MODULE_SIXLOWPAN /* https://tools.ietf.org/html/rfc4944#section-12 requires the first bit to * 0 for unicast addresses */ val[1] &= 0x7F; #endif _api_at_cmd(dev, cmd, 4, &resp); if (resp.status == 0) { memcpy(dev->addr_short, val, 2); return 2; } return -ECANCELED; }
static int _set_encryption(xbee_t *dev, uint8_t *val) { uint8_t cmd[3]; resp_t resp; /* get the current state of Encryption */ cmd[0] = 'E'; cmd[1] = 'E'; _api_at_cmd(dev, cmd, 2, &resp); /* Prevent writing the same value in EE. */ if (val[0] != resp.data[0] ){ cmd[0] = 'E'; cmd[1] = 'E'; cmd[2] = val[0]; _api_at_cmd(dev, cmd, 3, &resp); } if (resp.status == 0) { return 2; } return -ECANCELED; }
static int _set_short_addr(xbee_t *dev, uint8_t *address) { uint8_t cmd[4]; resp_t resp; cmd[0] = 'M'; cmd[1] = 'Y'; cmd[2] = address[0]; cmd[3] = address[1]; _api_at_cmd(dev, cmd, 4, &resp); return resp.status; }
static int _set_channel(xbee_t *dev, uint8_t *val, size_t len) { uint8_t cmd[3]; resp_t resp; if (len != 2 || val[1] != 0) { return -EINVAL; } cmd[0] = 'C'; cmd[1] = 'H'; cmd[2] = val[0]; _api_at_cmd(dev, cmd, 3, &resp); if (resp.status == 0) { return 2; } return -EINVAL; }
static int _set_panid(xbee_t *dev, uint8_t *val, size_t len) { uint8_t cmd[4]; resp_t resp; if (len != 2) { return -EINVAL; } cmd[0] = 'I'; cmd[1] = 'D'; cmd[2] = val[1]; cmd[3] = val[0]; _api_at_cmd(dev, cmd, 4, &resp); if (resp.status == 0) { return 2; } return -EINVAL; }
static int _get_panid(xbee_t *dev, uint8_t *val, size_t max) { uint8_t cmd[2]; resp_t resp; if (max < 2) { return -EOVERFLOW; } cmd[0] = 'I'; cmd[1] = 'D'; _api_at_cmd(dev, cmd, 2, &resp); if (resp.status == 0) { val[0] = resp.data[1]; val[1] = resp.data[0]; return 2; } return -ECANCELED; }
static int _get_addr_short(xbee_t *dev, uint8_t *val, size_t len) { uint8_t cmd[2]; resp_t resp; if (len < 2) { return -EOVERFLOW; } cmd[0] = 'M'; cmd[1] = 'Y'; _api_at_cmd(dev, cmd, 2, &resp); if (resp.status == 0) { memcpy(val, resp.data, 2); return 2; } return -ECANCELED; }
static int _set_encryption_key(xbee_t *dev, uint8_t *val, size_t len) { uint8_t cmd[18]; resp_t resp; if (len != 16) { /* the AES key is 128bit, 16 byte */ return -EINVAL; } cmd[0] = 'K'; cmd[1] = 'Y'; for (int i = 0; i < 16; i++) { /* Append the key to the KY API AT command */ cmd[i + 2] = val[i]; } _api_at_cmd(dev, cmd, 18, &resp); if (resp.status == 0) { return 2; } return -ECANCELED; }