- char atcmd_str[3];
- struct xbee_atcmd_pgm *cmd_pgm;
- struct xbee_atcmd cmd;
- union {
- uint8_t u8;
- uint16_t u16;
- uint32_t u32;
- int16_t s16;
- } __attribute__((packed)) *result;
-
- if (ctx == NULL) {
- printf("no context\n");
- return -1;
- }
-
- /* get AT command from frame */
- memcpy(atcmd_str, &frame->cmd, 2);
- atcmd_str[2] = '\0';
-
- /* see if it matches query */
- if (memcmp(atcmd_str, ctx->atcmd_query, 2)) {
- printf("invalid response <%c%c><%s><%s>\n",
- frame->cmd & 0xFF,
- (frame->cmd >> 8) & 0xFF,
- atcmd_str, ctx->atcmd_query);
- return -1;
- }
-
- /* see if it exists */
- cmd_pgm = xbee_atcmd_lookup_name(atcmd_str);
- if (cmd_pgm == NULL) {
- printf("unknown response\n");
- return -1;
- }
- memcpy_P(&cmd, cmd_pgm, sizeof(cmd));
-
- /* bad status */
- if (frame->status == 1) {
- printf("Status is error\n");
- return -1;
- }
- else if (frame->status == 2) {
- printf("Invalid command\n");
- return -1;
- }
- else if (frame->status == 3) {
- printf("Invalid parameter\n");
- return -1;
- }
- else if (frame->status != 0) {
- printf("Unknown status error %d\n", frame->status);
- return -1;
- }
-
- /* callback */
- if (ctx->func != NULL)
- ctx->func(frame, len, ctx->arg);
-
- /* dump frame */
- result = (void *)frame->data;
- len -= offsetof(struct xbee_atresp_hdr, data);
- if (cmd.flags & XBEE_ATCMD_F_PARAM_U8 && len == sizeof(uint8_t))
- printf("<%s> is 0x%x\n", atcmd_str, result->u8);
- else if (cmd.flags & XBEE_ATCMD_F_PARAM_U16 && len == sizeof(uint16_t))
- printf("<%s> is 0x%x\n", atcmd_str, ntohs(result->u16));
- else if (cmd.flags & XBEE_ATCMD_F_PARAM_U32 && len == sizeof(uint32_t))
- printf("<%s> is 0x%"PRIx32"\n", atcmd_str, ntohl(result->u32));
- else if (cmd.flags & XBEE_ATCMD_F_PARAM_S16 && len == sizeof(int16_t))
- printf("<%s> is %d\n", atcmd_str, ntohs(result->s16));
- else if (len == 0)
- printf("no data, status ok\n");
- else
- hexdump("atcmd answer", frame->data, len);
-
-
- return 0;